def test_collect_data(self): basico.add_parameter('quantity', initial_value=1) mod = basico.get_current_model().getModel() assert (isinstance(mod, COPASI.CModel)) mod.applyInitialValues() data = basico.model_info._collect_data(cns=[ 'CN=Root,Model=The Brusselator,Reference=Time', 'CN=Root,Model=The Brusselator,Vector=Compartments[compartment],Reference=InitialVolume', 'CN=Root,Model=The Brusselator,Vector=Compartments[compartment],Reference=Rate', 'CN=Root,Model=The Brusselator,Vector=Compartments[compartment],Reference=Volume', 'CN=Root,Model=The Brusselator,Vector=Compartments[compartment],Vector=Metabolites[X],Reference=InitialParticleNumber', 'CN=Root,Model=The Brusselator,Vector=Compartments[compartment],Vector=Metabolites[X],Reference=ParticleNumber', 'CN=Root,Model=The Brusselator,Vector=Compartments[compartment],Vector=Metabolites[X],Reference=ParticleNumberRate', 'CN=Root,Model=The Brusselator,Vector=Compartments[compartment],Vector=Metabolites[X],Reference=Rate', 'CN=Root,Model=The Brusselator,Vector=Compartments[compartment],Vector=Metabolites[X],Reference=Concentration', 'CN=Root,Model=The Brusselator,Vector=Compartments[compartment],Vector=Metabolites[X],Reference=InitialConcentration', 'CN=Root,Model=The Brusselator,Vector=Values[quantity],Reference=Value', 'CN=Root,Model=The Brusselator,Vector=Values[quantity],Reference=InitialValue', 'CN=Root,Model=The Brusselator,Vector=Values[quantity],Reference=Rate', 'CN=Root,Model=The Brusselator,Vector=Reactions[R1],Reference=Flux', 'CN=Root,Model=The Brusselator,Vector=Reactions[R1],Reference=ParticleFlux', 'CN=Root,Model=The Brusselator,Vector=Reactions[R1],ParameterGroup=Parameters,Parameter=k1,Reference=Value' ]) data2 = basico.model_info._collect_data(names=[ 'Time', 'Compartments[compartment].InitialVolume', 'Compartments[compartment].Rate', 'Compartments[compartment].Volume', 'X.InitialParticleNumber', 'X.ParticleNumber', 'X.InitialParticleNumberRate', 'X.ParticleNumberRate', 'X.Rate', '[X]', '[X]_0', 'Values[quantity]', 'Values[quantity].InitialValue', 'Values[quantity].Rate', '(R1).Flux', '(R1).ParticleFlux', '(R1).k1' ]) self.assertTrue(data2 is not None)
def _scan_item_to_dict(item, model=None): if model is None: model = basico.get_current_model() if isinstance(item, int): problem = model.getTask('Scan').getProblem() if item >= problem.getNumberOfScanItems(): logging.error('No Scan item: {0}'.format(item)) return None item = problem.getScanItem(item) assert (isinstance(item, COPASI.CCopasiParameter)) int_type = item.getParameter('Type').getIntValue() type_name = _scan_type_to_name(int_type) cn = item.getParameter('Object').getStringValue() if item.getParameter('Object') else None num_steps = item.getParameter('Number of steps').getIntValue() if item.getParameter('Number of steps') else None min_val = item.getParameter('Minimum').getDblValue() if item.getParameter('Minimum') else None max_val = item.getParameter('Maximum').getDblValue() if item.getParameter('Maximum') else None log = item.getParameter('log').getBoolValue() if item.getParameter('log') else None current = { 'type': type_name, 'num_steps': num_steps, } if log is not None: current['log'] = log if min_val is not None: current['min'] = min_val if max_val is not None: current['max'] = max_val if int_type == COPASI.CScanProblem.SCAN_RANDOM: dist = _scan_distribution_type_to_name(item.getParameter('Distribution type').getUIntValue()) \ if item.getParameter('Distribution type') else None current['distribution'] = dist elif int_type == COPASI.CScanProblem.SCAN_LINEAR: values = item.getParameter('Values').getStringValue() if item.getParameter('Values') else None if values: values = values.replace(',', ' ') values = values.split(' ') values = [float(v) for v in values] use_values = item.getParameter('Use Values').getBoolValue() if item.getParameter('Use Values') else None current['values'] = values current['use_values'] = use_values if cn: obj = model.getObject(COPASI.CCommonName(cn)) if not obj: logging.warning('missing object in scan item: {0}'.format(cn)) else: current['item'] = obj.getObjectDisplayName() current['cn'] = cn return current
def copasi_aic(): """Calculates the AIC based on the last parameter estimation run SSR = weighted sum of squares N = number of data points K = number of parameters being estimated (note that this is one more than the number of parameters in COPASI, since the SSR is also a parameter being estimated; so this is basically K = parameters + 1) AIC = Akaike's information criterion AICc= corrected Akaike's information criterion (for low N) AIC = N * log(SSR/N) + 2*K AICc = N * log(SSR/N) + 2*K*(K+1)/(N-K-1) :return: the aic based on above calculation :rtype: float """ prob = basico.get_current_model().getTask("Parameter Estimation").getProblem() N = prob.getExperimentSet().getValidValueCount() K = prob.getOptItemSize() + 1 SSR = prob.getSolutionValue() if isnan(SSR) or N == 0: return float("inf") return N * log(SSR / N) + 2 * K
def create_data_handler(output_selection, during=None, after=None, before=None, model=None): """Creates an output handler for the given selection :param output_selection: list of display names or cns, of elements to capture :type output_selection: [str] :param during: optional list of elements from the output selection, that should be collected during the run of the task :type during: [str] :param after: optional list of elements from the output selection, that should be collected after the run of the task :type after: [str] :param before: optional list of elements from the output selection, that should be collected before the run of the task :type before: [str] :param model: the model in which to resolve the display names :return: tuple of the data handler from which to retrieve output later, and their columns :rtype: (COPASI.CDataHandler, []) """ if model is None: model = basico.get_current_model() dh = COPASI.CDataHandler() columns = [] for name in output_selection: if name.startswith('CN='): obj = model.getObject(COPASI.CCommonName(name)) if not obj: logging.warning('no object for cn {0}'.format(name)) continue cn = name columns.append(obj.getObjectDisplayName()) else: obj = model.findObjectByDisplayName(name) if not obj: logging.warning('no object for name {0}'.format(name)) continue if isinstance(obj, COPASI.CModel): obj = obj.getValueReference() cn = obj.getCN().getString() columns.append(name) if during is None or (during is not None and name in during): dh.addDuringName(COPASI.CRegisteredCommonName(cn)) if after and name in after: dh.addAfterName(COPASI.CRegisteredCommonName(cn)) if before and name in before: dh.addAfterName(COPASI.CRegisteredCommonName(cn)) return dh, columns
def evaluate_model(test_model, evaluation=default_evaluation, temp_dir=None, delete_temp_files=True, sim_dfs=None, sol_dfs=None, temp_files=None): """evaluates the given test model and updates it with the calculated metrics and estimated parameters :param test_model: the model to test :type test_model: petab_select.Model :param evaluation: optional function to evaluate the test model with. defaults to func:`.default_evaluation` :type evaluation: () -> pandasDataFrame :param temp_dir: optional temp directory to store the files in (otherwise the os temp dir will be used) :type temp_dir: str or None :param delete_temp_files: boolean indicating whether temp files should be deleted :type delete_temp_files: bool :param sim_dfs: optional array, in which simulation data frames will be returned :type sim_dfs: [] or None :param sol_dfs: optional array in which found parameters will be returned :type sol_dfs: [] or None :param temp_files: optional array that returns filenames of temp files created during the run :type temp_files: [] or None :return: COPASI objective value of the evaluation :rtype: float """ # create petab problem pp = test_model.to_petab()['petab_problem'] created_temp_dir = False if temp_dir is None: temp_dir = tempfile.mkdtemp() created_temp_dir = True if not os.path.exists(temp_dir): os.makedirs(temp_dir, exist_ok=True) model_id = test_model.model_id files = core.write_problem_to(pp, temp_dir, model_id) # load into basico out_name = 'cps_{0}'.format(model_id) cps_file = os.path.join(temp_dir, out_name + '.cps') core.load_petab(files['problem'], temp_dir, out_name) files = list(files.values()) files.append(cps_file) files = files + basico.get_experiment_filenames() # run parameter estimation sol = evaluation() if sol_dfs: sol_dfs.append(sol) simulation_results = basico.get_simulation_results(values_only=True, solution=sol) basico.prune_simulation_results(simulation_results) sim_df = basico.petab.create_simulation_df(pp.measurement_df, simulation_results) if sim_dfs: sim_dfs.append(sim_df) # compute metrics llh = basico.petab.petab_llh(pp, sim_df) test_model.set_criterion(Criterion.LLH, llh) task = basico.get_current_model().getTask("Parameter Estimation") prob = task.getProblem() obj = prob.getSolutionValue() test_model.compute_criterion(Criterion.AIC) test_model.compute_criterion(Criterion.AICC) test_model.compute_criterion(Criterion.BIC) # update estimated parameters for param_id in test_model.parameters: value = test_model.parameters[param_id] if str(value) != 'estimate': # not isnan(value): # we only want to include what we estimated continue name = 'Values[{0}]'.format(param_id) if name in sol.index: test_model.estimated_parameters[param_id] = sol.loc[name].sol # write result for testing result_file = os.path.join(temp_dir, 'result_{0}.yaml'.format(model_id)) files.append(result_file) test_model.to_yaml(result_file) # delete temp files if needed if delete_temp_files: for file in files: os.remove(file) if created_temp_dir: # since we created the temp dir, lets get rid of it os.rmdir(temp_dir) elif temp_files is not None: # add temp files to the list of temp files: temp_files = temp_files + files return obj
def setUp(self): basico.load_example('brusselator') self.assertEqual(basico.get_current_model().getModel().getObjectName(), 'The Brusselator')