def petab_problem(minimal_sbml_model): # pylint: disable=W0621 """Test petab problem.""" # create test model document, model = minimal_sbml_model p = model.createParameter() p.setId('fixedParameter1') p.setName('FixedParameter1') p = model.createParameter() p.setId('observable_1') p.setName('Observable 1') measurement_df = pd.DataFrame(data={ OBSERVABLE_ID: ['obs1', 'obs2'], OBSERVABLE_PARAMETERS: ['', 'p1;p2'], NOISE_PARAMETERS: ['p3;p4', 'p5'] }) condition_df = pd.DataFrame(data={ CONDITION_ID: ['condition1', 'condition2'], CONDITION_NAME: ['', 'Condition 2'], 'fixedParameter1': [1.0, 2.0] }).set_index(CONDITION_ID) parameter_df = pd.DataFrame(data={ PARAMETER_ID: ['dynamicParameter1', 'dynamicParameter2'], PARAMETER_NAME: ['', '...'], }).set_index(PARAMETER_ID) observable_df = pd.DataFrame(data={ OBSERVABLE_ID: ['observable_1'], OBSERVABLE_NAME: ['julius'], OBSERVABLE_FORMULA: ['observable_1'], NOISE_FORMULA: [1], }).set_index(OBSERVABLE_ID) with tempfile.TemporaryDirectory() as temp_dir: sbml_file_name = Path(temp_dir, "model.xml") libsbml.writeSBMLToFile(document, str(sbml_file_name)) measurement_file_name = Path(temp_dir, "measurements.tsv") petab.write_measurement_df(measurement_df, measurement_file_name) condition_file_name = Path(temp_dir, "conditions.tsv") petab.write_condition_df(condition_df, condition_file_name) parameter_file_name = Path(temp_dir, "parameters.tsv") petab.write_parameter_df(parameter_df, parameter_file_name) observable_file_name = Path(temp_dir, "observables.tsv") petab.write_observable_df(observable_df, observable_file_name) yield petab.Problem.from_files( sbml_file=sbml_file_name, measurement_file=measurement_file_name, condition_file=condition_file_name, parameter_file=parameter_file_name, observable_files=observable_file_name)
def test_write_measurement_df(): """Test measurements.get_measurement_df.""" measurement_df = pd.DataFrame( data={ OBSERVABLE_ID: ['obs1', 'obs2'], OBSERVABLE_PARAMETERS: ['', 'p1;p2'], NOISE_PARAMETERS: ['p3;p4', 'p5'] }) with tempfile.NamedTemporaryFile(mode='w', delete=True) as fh: file_name = fh.name petab.write_measurement_df(measurement_df, file_name) re_df = petab.get_measurement_df(file_name).replace(np.nan, '') assert (measurement_df == re_df).all().all()
def test_write_measurement_df(): """Test measurements.get_measurement_df.""" measurement_df = pd.DataFrame( data={ OBSERVABLE_ID: ['obs1', 'obs2'], OBSERVABLE_PARAMETERS: ['', 'p1;p2'], NOISE_PARAMETERS: ['p3;p4', 'p5'] }) with tempfile.TemporaryDirectory() as temp_dir: file_name = Path(temp_dir) / "parameters.tsv" petab.write_measurement_df(measurement_df, file_name) re_df = petab.get_measurement_df(file_name).replace(np.nan, '') assert (measurement_df == re_df).all().all()
def test_concat_measurements(): a = pd.DataFrame({MEASUREMENT: [1.0]}) b = pd.DataFrame({TIME: [1.0]}) with tempfile.TemporaryDirectory() as temp_dir: filename_a = Path(temp_dir) / "measurements.tsv" petab.write_measurement_df(a, filename_a) expected = pd.DataFrame({ MEASUREMENT: [1.0, nan], TIME: [nan, 1.0] }) assert expected.equals( petab.concat_tables([a, b], petab.measurements.get_measurement_df)) assert expected.equals( petab.concat_tables([filename_a, b], petab.measurements.get_measurement_df))
def petab_yaml_path(yaml2sbml_model_string): petab_path = Path('output') / TEST_ID petab_path.mkdir(parents=True, exist_ok=True) petab_yaml_filename = 'petab.yaml' measurement_filename = 'measurements.tsv' with NamedTemporaryFile('w') as yaml2sbml_file: yaml2sbml_file.write(yaml2sbml_model_string) yaml2sbml_file.flush() yaml2sbml.yaml2petab( yaml_dir=yaml2sbml_file.name, output_dir=str(petab_path), sbml_name=TEST_ID, petab_yaml_name=petab_yaml_filename, measurement_table_name=measurement_filename, ) # Dummy measurements # FIXME replace with expected values... # FIXME use timecourse ID as simulation condition ID T = np.linspace(0, 100, 1001) X = [1 for _ in T] measurement_df = pd.DataFrame( data={ 'observableId': 'observable_x_', 'simulationConditionId': 'condition1', 'time': [f'{t:.1f}' for t in T], 'measurement': X, }) measurement_df = petab.get_measurement_df(measurement_df) petab.write_measurement_df( measurement_df, str(petab_path / measurement_filename), ) return petab_path / petab_yaml_filename
def test_combine_archive(minimal_sbml_model): """Test `create_combine_archive` and `Problem.from_combine`""" # Create test files document, _ = minimal_sbml_model # Create tables with arbitrary content measurement_df = pd.DataFrame( data={ OBSERVABLE_ID: ['obs1', 'obs2'], OBSERVABLE_PARAMETERS: ['', 'p1;p2'], NOISE_PARAMETERS: ['p3;p4', 'p5'] }) condition_df = pd.DataFrame( data={ CONDITION_ID: ['condition1', 'condition2'], CONDITION_NAME: ['', 'Condition 2'], 'fixedParameter1': [1.0, 2.0] }) condition_df.set_index(CONDITION_ID, inplace=True) parameter_df = pd.DataFrame( data={ PARAMETER_ID: ['dynamicParameter1', 'dynamicParameter2'], PARAMETER_NAME: ['', '...'], }) parameter_df.set_index(PARAMETER_ID, inplace=True) observable_df = pd.DataFrame( data={ OBSERVABLE_ID: ['observable_1'], OBSERVABLE_FORMULA: ['observable_1'], NOISE_FORMULA: [1], }) observable_df.set_index(OBSERVABLE_ID, inplace=True) sbml_file_name = 'model.xml' measurement_file_name = 'measurements.tsv' condition_file_name = 'conditions.tsv' parameter_file_name = 'parameters.tsv' observable_file_name = 'observables.tsv' yaml_file_name = 'test.yaml' yaml_config = { FORMAT_VERSION: petab.__format_version__, PARAMETER_FILE: parameter_file_name, PROBLEMS: [{ SBML_FILES: [sbml_file_name], MEASUREMENT_FILES: [measurement_file_name], CONDITION_FILES: [condition_file_name], OBSERVABLE_FILES: [observable_file_name] }] } with tempfile.TemporaryDirectory(prefix='petab_test_combine_archive') \ as tempdir: # Write test data petab.write_sbml(document, os.path.join(tempdir, sbml_file_name)) petab.write_measurement_df( measurement_df, os.path.join(tempdir, measurement_file_name)) petab.write_parameter_df(parameter_df, os.path.join(tempdir, parameter_file_name)) petab.write_observable_df(observable_df, os.path.join(tempdir, observable_file_name)) petab.write_condition_df(condition_df, os.path.join(tempdir, condition_file_name)) petab.write_yaml(yaml_config, os.path.join(tempdir, yaml_file_name)) archive_file_name = os.path.join(tempdir, 'test.omex') # Create COMBINE archive petab.create_combine_archive(os.path.join(tempdir, yaml_file_name), archive_file_name, family_name="Tester") # Read COMBINE archive problem = petab.Problem.from_combine(archive_file_name) assert problem.parameter_df is not None assert problem.condition_df is not None assert problem.measurement_df is not None assert problem.observable_df is not None
PARAMETER_NAME: parameter_dict0['name'], PARAMETER_SCALE: LIN, NOMINAL_VALUE: 1, ESTIMATE: 0, } else: raise NotImplementedError(parameter_dict0['id']) parameter_dicts.append(parameter_dict) ## Noise parameter_dicts.append({ PARAMETER_ID: noise, PARAMETER_NAME: noise, PARAMETER_SCALE: LOG10, LOWER_BOUND: '1e-12', UPPER_BOUND: '1e3', NOMINAL_VALUE: 0.1, ESTIMATE: 1, }) condition_df = petab.get_condition_df( pd.DataFrame({CONDITION_ID: [condition_id]})) observable_df = petab.get_observable_df(pd.DataFrame(observable_dicts)) measurement_df = petab.get_measurement_df(pd.DataFrame(measurement_dicts)) parameter_df = petab.get_parameter_df(pd.DataFrame(parameter_dicts)) petab.write_condition_df(condition_df, 'output/petab/conditions.tsv') petab.write_observable_df(observable_df, 'output/petab/observables.tsv') petab.write_measurement_df(measurement_df, 'output/petab/measurements.tsv') petab.write_parameter_df(parameter_df, 'output/petab/parameters.tsv') shutil.copy('input/petab_problem.yaml', 'output/petab/petab_problem.yaml')
OBSERVABLE_FORMULA: ['A'], NOISE_FORMULA: [sigma] }).set_index([OBSERVABLE_ID]) parameter_df = pd.DataFrame( data={ PARAMETER_ID: ['k1', 'k2'], PARAMETER_SCALE: [LOG] * 2, LOWER_BOUND: [1e-5] * 2, UPPER_BOUND: [1e5] * 2, NOMINAL_VALUE: [k1, k2], ESTIMATE: [1, 1], }).set_index(PARAMETER_ID) petab.write_condition_df(condition_df, "conditions.tsv") petab.write_measurement_df(measurement_df, "measurements.tsv") petab.write_observable_df(observable_df, "observables.tsv") petab.write_parameter_df(parameter_df, "parameters.tsv") yaml_config = { FORMAT_VERSION: 1, PARAMETER_FILE: "parameters.tsv", PROBLEMS: [{ SBML_FILES: ["model_conversion_reaction.xml"], CONDITION_FILES: ["conditions.tsv"], MEASUREMENT_FILES: ["measurements.tsv"], OBSERVABLE_FILES: ["observables.tsv"] }] }