Skip to content

Commit

Permalink
meta-regression fully functional
Browse files Browse the repository at this point in the history
  • Loading branch information
bwallace committed Nov 18, 2011
1 parent ad21ecc commit 7c0a5d7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
42 changes: 26 additions & 16 deletions meta_py_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,14 @@ def draw_network(edge_list, unconnected_vertices, network_path = '"./r_tmp/netwo
ro.r("dev.off()")
return "r_tmp/network.png"

def ma_dataset_to_simple_continuous_robj(table_model, var_name="tmp_obj"):
def ma_dataset_to_simple_continuous_robj(table_model, var_name="tmp_obj", \
covs_to_include=None):
r_str = None

# grab the study names. note: the list is pulled out in reverse order from the
# model, so we, er, reverse it.
studies = table_model.get_studies()
#pyqtRemoveInputHook()
#pdb.set_trace()

study_names = ", ".join(["'" + study.name + "'" for study in studies])
studies.reverse()

Expand All @@ -274,10 +274,8 @@ def ma_dataset_to_simple_continuous_robj(table_model, var_name="tmp_obj"):

#cov_str = gen_cov_str(table_model.dataset, studies)
cov_str = list_of_cov_value_objects_str(table_model.dataset,\
[study.name for study in studies])

#pyqtRemoveInputHook()
#pdb.set_trace()
[study.name for study in studies],\
cov_list=covs_to_include)


# first try and construct an object with raw data
Expand All @@ -295,7 +293,8 @@ def ma_dataset_to_simple_continuous_robj(table_model, var_name="tmp_obj"):
r_str = "%s <- new('ContinuousData', \
N1=c(%s), mean1=c(%s), sd1=c(%s), \
N2=c(%s), mean2=c(%s), sd2=c(%s), \
y=c(%s), SE=c(%s), study.names=c(%s), covariates=%s)" \
y=c(%s), SE=c(%s), study.names=c(%s),\
covariates=%s)" \
% (var_name, Ns1_str, means1_str, SDs1_str, \
Ns2_str, means2_str, SDs2_str, \
ests_str, SEs_str, study_names, cov_str)
Expand All @@ -322,7 +321,7 @@ def _get_str(M, col_index, reverse=True):


def ma_dataset_to_simple_binary_robj(table_model, var_name="tmp_obj",
include_raw_data=True):
include_raw_data=True, covs_to_include=None):
'''
This converts a DatasetModel to an OpenMetaData (OMData) R object. We use type DatasetModel
rather than a DataSet model directly to access the current variables. Furthermore, this allows
Expand All @@ -348,9 +347,10 @@ def ma_dataset_to_simple_binary_robj(table_model, var_name="tmp_obj",
SEs_str = ", ".join(_to_strs(SEs))

# generate the covariate string
cov_str = gen_cov_str(table_model.dataset, studies)


#cov_str = gen_cov_str(table_model.dataset, studies)
cov_str = list_of_cov_value_objects_str(table_model.dataset,\
[study.name for study in studies],\
cov_list=covs_to_include)
# first try and construct an object with raw data
if include_raw_data and table_model.included_studies_have_raw_data():
print "ok; raw data has been entered for all included studies"
Expand Down Expand Up @@ -416,7 +416,7 @@ def _sanitize_for_R(str):
return str.encode('latin-1', 'ignore')

def ma_dataset_to_simple_diagnostic_robj(table_model, var_name="tmp_obj", \
metric="Sens"):
metric="Sens", covs_to_include=None):
'''
This converts a DatasetModel to an OpenMetaData (OMData) R object. We use type DatasetModel
rather than a DataSet model directly to access the current variables. Furthermore, this allows
Expand All @@ -437,7 +437,9 @@ def ma_dataset_to_simple_diagnostic_robj(table_model, var_name="tmp_obj", \
y_SEs_str = ", ".join(_to_strs(y_SEs))

# generate the covariate string
cov_str = gen_cov_str(table_model.dataset, studies)
cov_str = list_of_cov_value_objects_str(table_model.dataset,\
[study.name for study in studies],
cov_list=covs_to_include)

# first try and construct an object with raw data
if table_model.included_studies_have_raw_data():
Expand Down Expand Up @@ -665,9 +667,11 @@ def _gen_cov_vals_obj_str(cov, study_names, dataset):
return r_str


def list_of_cov_value_objects_str(dataset, study_names):
def list_of_cov_value_objects_str(dataset, study_names, cov_list=None):
r_cov_str = []
cov_list = dataset.covariates
if cov_list is None:
# then use all covariates that belong to the dataset
cov_list = dataset.covariates
for cov in cov_list:
r_cov_str.append(_gen_cov_vals_obj_str(cov, study_names, dataset))
r_cov_str = "list(" + ",".join(r_cov_str) + ")"
Expand Down Expand Up @@ -705,6 +709,12 @@ def run_meta_regression(dataset, study_names, cov_list, data_name="tmp_obj", \
ro.r(r_str)
result = ro.r("%s" % results_name)

#pyqtRemoveInputHook()
#pdb.set_trace()
if "try-error" in str(result):
# uh-oh, there was an error
return str([msg for msg in result][0])

parsed_results = parse_out_results(result)

return parsed_results
Expand Down
25 changes: 18 additions & 7 deletions meta_reg_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ def cancel(self):
self.reject()

def run_meta_reg(self):
#selected_cov = self.cov_cbo_box.currentText()
#print selected_cov

meta_py_r.ma_dataset_to_simple_binary_robj(self.model, include_raw_data=False)
selected_covariates = []
for cov, chk_box in self.covs_and_check_boxes:
if chk_box.isChecked():
Expand All @@ -34,16 +30,27 @@ def run_meta_reg(self):
# for all of the selected covariates
cov_d = self.model.dataset.get_values_for_cov(cov.name)

# note that we create a 'binary' data object (regardless
# of the data type) -- any variant of OMData will due here
meta_py_r.ma_dataset_to_simple_binary_robj(self.model, include_raw_data=False,\
covs_to_include=selected_covariates)

studies = []
for study in [study.name for study in self.model.dataset.studies]:
if study != '' and cov_d[study] is not None and cov_d[study] != '':
studies.append(study)

#result = meta_py_r.run_binary_fixed_meta_regression(selected_cov)
result = meta_py_r.run_meta_regression(self.model.dataset, studies,\
selected_covariates)
self.parent().analysis(result)
self.accept()
if isinstance(result, str):
# then there was an error!
QMessageBox.critical(self,
"Whoops.",
"Sorry, there was an error performing the regression.\n%s" % \
result)
else:
self.parent().analysis(result)
self.accept()

def _populate_combo_box(self):
studies = self.model.get_studies(only_if_included=True)
Expand All @@ -62,6 +69,10 @@ def _populate_chk_boxes(self):
cov_vals = [study.covariate_dict[cov.name] for study in studies]
if not None in cov_vals:
chk_box = QCheckBox(cov.name)
if len(self.covs_and_check_boxes)==0:
# check the first covariate by default
# (this is arbitrary)
chk_box.setChecked(True)
chk_box_layout.addWidget(chk_box)
self.covs_and_check_boxes.append((cov, chk_box))

Expand Down

0 comments on commit 7c0a5d7

Please sign in to comment.