class AbstractPublicationBiasWizard(QtGui.QWizard): def __init__(self, model, parent=None): super(AbstractPublicationBiasWizard, self).__init__(parent) self.setOption(QWizard.HaveFinishButtonOnEarlyPages,True) self.model = model self.last_analysis = model.get_last_analysis_selections() # selections from last analysis of whatever type # Initialize pages that we will need to access later self.data_location_page = DataLocationPage(model=model) self.setPage(Page_DataLocation, self.data_location_page) self.refine_studies_page = RefineStudiesPage(model=model) self.setPage(Page_RefineStudies, self.refine_studies_page) self.summary_page = SummaryPage() self.setPage(Page_Summary, self.summary_page) self.setWizardStyle(QWizard.ClassicStyle) QObject.connect(self, SIGNAL("currentIdChanged(int)"), self._change_size) def _change_size(self, pageid): print("changing size") self.adjustSize() def nextId(self): raise NotImplementedError("Not implemented") #### Getters # Data location def get_data_location(self): return self.data_location_page.get_data_locations() # Refine Studies page def get_included_studies_in_proper_order(self): all_studies = self.model.get_studies_in_current_order() included_studies = self.refine_studies_page.get_included_studies() included_studies_in_order = [study for study in all_studies if study in included_studies] return included_studies_in_order def save_selections(self): # returns a bool # Should the selections be saved? return self.summary_page.save_selections()
class MetaRegressionWizard(QtGui.QWizard): def __init__(self, model, parent=None): super(MetaRegressionWizard, self).__init__(parent) self.model = model last_analysis=model.get_last_analysis_selections() self.analysis_label = "Meta-Regression" self.setWindowTitle(self.analysis_label) self.setup_pages(model=model, last_analysis=last_analysis) self.setWizardStyle(QWizard.ClassicStyle) # Automatically resize wizard on each page flip self.currentIdChanged.connect(self._change_size) def _change_size(self, pageid): print("changing size") self.adjustSize() def setup_pages(self, model, last_analysis): self.choose_effect_size_page = ChooseEffectSizePage(add_generic_effect=True, data_type=last_analysis['data_type'], metric=last_analysis['metric'], var_groups = model.get_variable_groups()) self.setPage(Page_ChooseEffectSize, self.choose_effect_size_page) self.data_location_page = DataLocationPage(model=model) self.setPage(Page_DataLocation, self.data_location_page) self.refine_studies_page = RefineStudiesPage(model=model) self.setPage(Page_RefineStudies, self.refine_studies_page) self.meta_reg_details_page = MetaRegDetailsPage( fixed_effects=last_analysis['fixed_effects'], random_effects_method = last_analysis['random_effects_method'], analysis_type=last_analysis['analysis_type'], # PARAMETRIC or BOOTSTRAP conf_level=last_analysis['conf_level'], phylogen=last_analysis['phylogen']) self.setPage(Page_MetaRegDetails, self.meta_reg_details_page) self.select_covariates_page = SelectCovariatesPage( model=model, previously_included_covs=last_analysis['included_covariates']) self.setPage(Page_SelectCovariates, self.select_covariates_page) self.reference_value_page = ReferenceValuePage( model=model, prev_cov_to_ref_level=last_analysis['cov_2_ref_values']) self.setPage(Page_ReferenceValues, self.reference_value_page) self.summary_page = SummaryPage() self.setPage(Page_Summary, self.summary_page) # weird stuff self.cond_means_pg = CondMeansPage(model=model) self.setPage(Page_CondMeans, self.cond_means_pg) self.bootstrap_page = BootstrapPage(model=model) self.setPage(Page_Bootstrap, self.bootstrap_page) def _require_categorical(self): return self.meta_reg_details_page.get_output_type() == CONDITIONAL_MEANS def nextId(self): current_id = self.currentId() return self.nextId_helper(current_id) def nextId_helper(self, page_id): if page_id in [Page_MetaRegDetails, Page_Bootstrap, Page_ReferenceValues]: analysis_type = self.get_analysis_type() # PARAMETRIC or BOOTSTRAP output_type = self.get_output_type() # NORMAL or CONDITIONAL_MEANS if page_id == Page_ChooseEffectSize: return Page_DataLocation elif page_id == Page_DataLocation: return Page_RefineStudies elif page_id == Page_RefineStudies: return Page_SelectCovariates elif page_id == Page_SelectCovariates: return Page_MetaRegDetails elif page_id == Page_MetaRegDetails: if self._categorical_covariates_selected(): return Page_ReferenceValues else: if analysis_type == BOOTSTRAP: return Page_Bootstrap elif output_type == CONDITIONAL_MEANS: return Page_CondMeans else: return Page_Summary elif page_id == Page_Bootstrap: if output_type == CONDITIONAL_MEANS: return Page_CondMeans else: return Page_Summary elif page_id == Page_CondMeans: return Page_Summary elif page_id == Page_ReferenceValues: if analysis_type == BOOTSTRAP: return Page_Bootstrap elif output_type == CONDITIONAL_MEANS: return Page_CondMeans else: return Page_Summary elif page_id == Page_Summary: return -1 def _categorical_covariates_selected(self): '''are categorical variables selected?''' included_covariates = self.get_included_covariates() categorical_covariates = [cov for cov in included_covariates if cov.get_type()==CATEGORICAL] return len(categorical_covariates) > 0 ################ Getters to get data from wizard ######################### # Effect Size Page def get_data_type_and_metric(self): ''' returns tuple (data_type, metric) ''' return self.choose_effect_size_page.get_data_type_and_metric() # Data location page def get_data_location(self): return self.data_location_page.get_data_locations() # Refine studies page def get_included_studies_in_proper_order(self): all_studies = self.model.get_studies_in_current_order() included_studies = self.refine_studies_page.get_included_studies() included_studies_in_order = [study for study in all_studies if study in included_studies] return included_studies_in_order # Select covariates Page def get_included_covariates(self): return self.select_covariates_page.get_included_covariates() def get_included_interactions(self): return self.select_covariates_page.get_interactions() # Meta regression details page def using_fixed_effects(self): return self.meta_reg_details_page.get_using_fixed_effects() def get_conf_level(self): return self.meta_reg_details_page.get_confidence_level() def get_random_effects_method(self): return self.meta_reg_details_page.get_random_effects_method() def get_phylogen(self): return self.meta_reg_details_page.get_phylogen() def get_analysis_type(self): # NORMAL or BOOTSTRAP return self.meta_reg_details_page.get_analysis_type() def get_output_type(self): # PARAMETRIC or CONDITIONAL_MEANS return self.meta_reg_details_page.get_output_type() def get_btt(self): # (a var or interaction, None, 'covariate', 'interaction' i.e. the type) return self.meta_reg_details_page.get_btt() # Reference Values page def get_covariate_reference_levels(self): return self.reference_value_page.get_covariate_reference_levels() # Conditional Means page def get_meta_reg_cond_means_info(self): # returns a tuple (cat. cov to stratify over, the values for the other covariates) return self.cond_means_pg.get_meta_reg_cond_means_data() # Bootstrap page def get_bootstrap_params(self): return self.bootstrap_page.get_bootstrap_params() # Summary page def save_selections(self): return self.summary_page.save_selections() ######################### end of getters ################################# # Summary Page def get_summary(self): ''' Make a summary string to show the user at the end of the wizard summarizing most of the user selections ''' summary = wizard_summary( wizard=self, next_id_helper=self.nextId_helper, summary_page_id=Page_Summary, analysis_label=self.analysis_label ) return summary
class DynamicWizard(QtGui.QWizard): def __init__(self, model, wizard_parameters, parent=None): super(DynamicWizard, self).__init__(parent) self.model = model self.wizard_parameters = wizard_parameters self.setWindowTitle(self.wizard_parameters['WIZARD.WINDOW.TITLE']) self.setWizardStyle(QWizard.ClassicStyle) self._create_wizard_pages() QObject.connect(self, SIGNAL("currentIdChanged(int)"), self._change_size) def _change_size(self, pageid): print("changing size") self.adjustSize() def _create_wizard_pages(self): ''' Initialize wizard pages specified by wizard parameters ''' print "wizard parameters: %s" % str(self.wizard_parameters) for page_key, page_parameters in self.wizard_parameters['WIZARD.PAGES'].items(): page_arguments = {} print "page key: %s, page parameters: %s" % (page_key, str(page_parameters)) if page_key == 'DATALOCATION': if 'SHOW.RAW.DATA' in page_parameters: page_arguments['show_raw_data'] = page_parameters['SHOW.RAW.DATA'] print "Page arguments: %s" % str(page_arguments) self.data_location_page = DataLocationPage(model=self.model, **page_arguments) self.setPage(Page_DataLocation, self.data_location_page) elif page_key == 'REFINESTUDIES': self.refine_studies_page = RefineStudiesPage(model=self.model) self.setPage(Page_RefineStudies, self.refine_studies_page) elif page_key == 'SUMMARY': self.summary_page = SummaryPage() self.setPage(Page_Summary, self.summary_page) def nextId(self): page_id = self.currentId() return self.next_id_helper(page_id) def next_id_helper(self, page_id): pages = self.wizard_parameters['WIZARD.PAGES'].keys() # pages in order pageids = [pageid[pagekey] for pagekey in pages] currentpageindex = pageids.index(page_id) if currentpageindex == len(pageids) - 1: # End wizard if on last page return -1 # go to next page return pageids[currentpageindex+1] #### Getters # Data location def get_data_location(self): return self.data_location_page.get_data_locations() # Refine Studies page def get_included_studies_in_proper_order(self): all_studies = self.model.get_studies_in_current_order() included_studies = self.refine_studies_page.get_included_studies() included_studies_in_order = [study for study in all_studies if study in included_studies] return included_studies_in_order # Summary Page def get_summary(self): ''' Make a summary string to show the user at the end of the wizard summarizing most of the user selections ''' return wizard_summary(wizard=self, next_id_helper=self.next_id_helper, summary_page_id=Page_Summary, analysis_label="Failsafe Analysis") def save_selections(self): # returns a bool # Should the selections be saved? return self.summary_page.save_selections()
class PermutationWizard(QtGui.QWizard): # if meta_reg mode is True, the wizard will do a permutation # test for a meta-regression, otherwise it does a standard # meta-analysis def __init__(self, model, meta_reg_mode=False, parent=None): super(PermutationWizard, self).__init__(parent) self.model = model last_analysis = model.get_last_analysis_selections() self.meta_reg_mode = meta_reg_mode # Set window title if meta_reg_mode: self.analysis_label = "Permuted Meta-Regression" else: self.analysis_label = "Permuted Meta-Analysis" self.setWindowTitle(self.analysis_label) self.setup_pages(model=model, last_analysis=last_analysis) self.setWizardStyle(QWizard.ClassicStyle) # Automatically resize wizard on each page flip self.currentIdChanged.connect(self.adjustSize) def setup_pages(self, model, last_analysis): self.choose_effect_size_page = ChooseEffectSizePage( add_generic_effect=True, data_type=last_analysis['data_type'], metric=last_analysis['metric'], var_groups = model.get_variable_groups()) self.setPage(Page_ChooseEffectSize, self.choose_effect_size_page) self.data_location_page = DataLocationPage(model=model) self.setPage(Page_DataLocation, self.data_location_page) self.refine_studies_page = RefineStudiesPage(model=model) self.setPage(Page_RefineStudies, self.refine_studies_page) self.meta_reg_details_page = MetaRegDetailsPage( fixed_effects=last_analysis['fixed_effects'], random_effects_method=last_analysis['random_effects_method'], analysis_type=last_analysis['analysis_type'], # PARAMETRIC or BOOTSTRAP conf_level=last_analysis['conf_level'], phylogen=last_analysis['phylogen'], disable_analysis_type_groupbox=True, disable_output_type_groupbox=True) self.setPage(Page_MetaRegDetails, self.meta_reg_details_page) self.ma_details_page = MADetailsPage() self.setPage(Page_MADetails, self.ma_details_page) self.perm_pg = PermuationPage() self.setPage(Page_Permutation, self.perm_pg) self.select_covariates_page = SelectCovariatesPage( model=model, previously_included_covs=last_analysis['included_covariates'], disable_require_categorical=True) self.setPage(Page_SelectCovariates, self.select_covariates_page) self.reference_value_page = ReferenceValuePage( model=model, prev_cov_to_ref_level=last_analysis['cov_2_ref_values']) self.setPage(Page_ReferenceValues, self.reference_value_page) self.summary_page = SummaryPage() self.setPage(Page_Summary, self.summary_page) def nextId(self): current_id = self.currentId() return self.nextId_helper(current_id) def nextId_helper(self, page_id): if self.meta_reg_mode: if page_id == Page_ChooseEffectSize: return Page_DataLocation elif page_id == Page_DataLocation: return Page_RefineStudies elif page_id == Page_RefineStudies: return Page_SelectCovariates elif page_id == Page_SelectCovariates: return Page_MetaRegDetails elif page_id == Page_MetaRegDetails: if self._categorical_covariates_selected(): return Page_ReferenceValues else: return Page_Permutation elif page_id == Page_ReferenceValues: return Page_Permutation elif page_id == Page_Permutation: return Page_Summary elif page_id == Page_Summary: return -1 else: # non-meta regression mode (no moderators) if page_id == Page_ChooseEffectSize: return Page_DataLocation elif page_id == Page_DataLocation: return Page_RefineStudies elif page_id == Page_RefineStudies: return Page_MADetails elif page_id == Page_MADetails: return Page_Permutation elif page_id == Page_ReferenceValues: return Page_Permutation elif page_id == Page_Permutation: return Page_Summary elif page_id == Page_Summary: return -1 def _categorical_covariates_selected(self): '''are categorical variables selected?''' included_covariates = self.get_included_covariates() categorical_covariates = [cov for cov in included_covariates if cov.get_type()==CATEGORICAL] return len(categorical_covariates) > 0 ################# getters ######################### def get_parameters(self): data_type_and_metric = self.choose_effect_size_page.get_data_type_and_metric() data_location = self.data_location_page.get_data_locations() studies = self.refine_studies_page.get_included_studies_in_proper_order() parameters = { 'data_type_and_metric': data_type_and_metric, 'data_location': data_location, 'studies': studies, } parameters.update(self.perm_pg.get_choices()) # meta-regression specific if self.meta_reg_mode: covariates = self.select_covariates_page.get_included_covariates() interactions = self.select_covariates_page.get_interactions() _fixed_effects = self.meta_reg_details_page.get_using_fixed_effects() level = self.meta_reg_details_page.get_confidence_level() _random_effects_method = self.meta_reg_details_page.get_random_effects_method() method = _random_effects_method if not _fixed_effects else FIXED_EFFECTS_METHOD_STR phylogen = self.meta_reg_details_page.get_phylogen() btt = self.meta_reg_details_page.get_btt() reference_values = self.reference_value_page.get_covariate_reference_levels() parameters.update({ 'covariates': covariates, 'interactions': interactions, 'level': level, 'method': method, 'phylogen': phylogen, 'btt': btt, 'reference_values': reference_values, }) else: # ma specific (no moderators) method = self.ma_details_page.get_method() intercept = self.ma_details_page.get_intercept() weighted = self.ma_details_page.get_weighted_least_squares() knha = self.ma_details_page.get_knha() level = self.ma_details_page.get_confidence_level() digits = self.ma_details_page.get_digits() parameters.update({ 'method':method, 'intercept':intercept, 'weighted':weighted, 'knha':knha, 'level':level, 'digits':digits, }) return parameters # Summary page def save_selections(self): return self.summary_page.save_selections() # Effect Size Page def get_data_type_and_metric(self): ''' returns tuple (data_type, metric) ''' return self.choose_effect_size_page.get_data_type_and_metric() # Data location page def get_data_location(self): return self.data_location_page.get_data_locations() # Refine studies page def get_included_studies_in_proper_order(self): all_studies = self.model.get_studies_in_current_order() included_studies = self.refine_studies_page.get_included_studies() included_studies_in_order = [study for study in all_studies if study in included_studies] return included_studies_in_order # Select covariates Page def get_included_covariates(self): return self.select_covariates_page.get_included_covariates() def get_included_interactions(self): return self.select_covariates_page.get_interactions() ######################### end of getters ################################# # Summary Page def get_summary(self): ''' Make a summary string to show the user at the end of the wizard summarizing most of the user selections ''' summary = wizard_summary( wizard=self, next_id_helper=self.nextId_helper, summary_page_id=Page_Summary, analysis_label=self.analysis_label ) return summary
class MiMaWizard(QtGui.QWizard): def __init__(self, model, parent=None): super(MiMaWizard, self).__init__(parent) self.analysis_label = "Multiply-Imputed Meta Analysis/Regression" self.model = model self.imp_results = None last_analysis = model.get_last_analysis_selections() # selections from last analysis of whatever type # Initialize pages that we will need to access later self.parameters_page = MetaAnalysisParametersPage(method="FE" if last_analysis['fixed_effects'] else last_analysis['random_effects_method'], level=model.get_conf_level(), digits=model.get_precision(), knha=last_analysis['knha']) self.setPage(Page_Parameters, self.parameters_page) # choose effect size self.choose_effect_size_page = ChooseEffectSizePage(add_generic_effect=True, data_type=last_analysis['data_type'], metric=last_analysis['metric'], var_groups = model.get_variable_groups()) self.setPage(Page_ChooseEffectSize, self.choose_effect_size_page) # data location page self.data_location_page = DataLocationPage(model=model) self.setPage(Page_DataLocation, self.data_location_page) # refine studies page self.refine_studies_page = RefineStudiesPage(model=model) self.setPage(Page_RefineStudies, self.refine_studies_page) # summary page self.summary_page = SummaryPage() self.setPage(Page_Summary, self.summary_page) # Mice Parameters Page self.mice_params_page = MiceParametersPage() self.setPage(Page_MiceParameters, self.mice_params_page) # Covariate Select Page (for choosing covariates to impute with) self.cov_select_page = CovariateSelectPage(model = self.model) self.setPage(Page_CovariateSelect, self.cov_select_page) # Select Covariates page (for choosing covariates to do with regression with) self.select_covariates_page = SelectCovariatesPage( model=model, previously_included_covs=last_analysis['included_covariates'], min_covariates=0, allow_covs_with_missing_data=True) self.setPage(Page_SelectCovariates, self.select_covariates_page) # Reference Value Page self.reference_value_page = ReferenceValuePage( model=model, prev_cov_to_ref_level=last_analysis['cov_2_ref_values']) self.setPage(Page_ReferenceValues, self.reference_value_page) # Mice Output page self.mice_output_page = MiceOutputPage() self.setPage(Page_MiceOutput, self.mice_output_page) self.setWizardStyle(QWizard.ClassicStyle) self.setStartId(Page_ChooseEffectSize) self.setOption(QWizard.HaveFinishButtonOnEarlyPages,True) QObject.connect(self, SIGNAL("currentIdChanged(int)"), self._change_size) self.setWindowTitle("Multiply-Imputed Meta-Analysis") # Meta analysis nextID def nextId_helper(self, page_id): if page_id == Page_ChooseEffectSize: return Page_DataLocation elif page_id == Page_DataLocation: return Page_RefineStudies elif page_id == Page_RefineStudies: return Page_Parameters elif page_id == Page_Parameters: # meta analysis parameters return Page_MiceParameters elif page_id == Page_MiceParameters: # mice parameters return Page_CovariateSelect elif page_id == Page_CovariateSelect: return Page_MiceOutput elif page_id == Page_MiceOutput: return Page_SelectCovariates elif page_id == Page_SelectCovariates: if self._categorical_covariates_selected(): return Page_ReferenceValues else: return Page_Summary elif page_id == Page_ReferenceValues: return Page_Summary elif page_id == Page_Summary: return -1 def nextId(self): next_id = self.nextId_helper(self.currentId()) return next_id def _change_size(self, pageid): print("changing size") self.adjustSize() def _require_categorical(self): # just to make select covariates page happy return False def _categorical_covariates_selected(self): '''are categorical variables selected?''' included_covariates = self.get_included_covariates() categorical_covariates = [cov for cov in included_covariates if cov.get_type()==CATEGORICAL] return len(categorical_covariates) > 0 # My details page def get_analysis_parameters(self): return self.parameters_page.get_parameters() # refine studies page def get_included_studies_in_proper_order(self): all_studies = self.model.get_studies_in_current_order() included_studies = self.refine_studies_page.get_included_studies() included_studies_in_order = [study for study in all_studies if study in included_studies] return included_studies_in_order # data location page def get_data_location(self): return self.data_location_page.get_data_locations() # choose effect size page def get_data_type_and_metric(self): ''' returns tuple (data_type, metric) ''' return self.choose_effect_size_page.get_data_type_and_metric() def _run_imputation(self): imp_results = python_to_R.impute(model=self.model, studies=self.get_included_studies_in_proper_order(), covariates=self.get_covariates_for_imputation(), m=self.get_m(), maxit=self.get_maxit(), defaultMethod_rstring=self.get_defaultMethod_rstring()) self.imp_results = imp_results return imp_results def get_imputation_summary(self): return self.imp_results['summary'] # def get_imputation_choices(self): # covariates = self.get_included_covariates() # covariates in original order # # imputation_choices = python_to_R.imputation_dataframes_to_pylist_of_ordered_dicts( # self.imp_results['imputations'], # covariates) # return imputation_choices def get_source_data(self): # an ordered dict mapping covariates --> values to see which ones are # none return self.imp_results['source_data'] def get_imputations(self): return self.imp_results['imputations'] # R list of imputations ######## getters ########### ### Covariate Select Page def get_covariates_for_imputation(self): return self.cov_select_page.get_included_covariates() ### Mice Parameters Page def get_m(self): # number of multiple imputations return self.mice_params_page.get_m() def get_maxit(self): # number of iterations return self.mice_params_page.get_maxit() def get_defaultMethod_rstring(self): return self.mice_params_page.get_defaultMethod_rstring() ### Reference values page def get_covariate_reference_levels(self): return self.reference_value_page.get_covariate_reference_levels() # Select covariates Page def get_included_covariates(self): # covariates for regression (not imputation) return self.select_covariates_page.get_included_covariates() def get_included_interactions(self): return self.select_covariates_page.get_interactions() # Summary page def save_selections(self): return self.summary_page.save_selections() # Summary Page def get_summary(self): ''' Make a summary string to show the user at the end of the wizard summarizing most of the user selections ''' return wizard_summary(wizard=self, next_id_helper=self.nextId_helper, summary_page_id=Page_Summary, analysis_label=self.analysis_label)