Beispiel #1
0
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)
Beispiel #2
0
def to_petab_files(
    timecourse: Timecourse,
    condition_location: TYPE_PATH,
    timecourse_location: TYPE_PATH,
) -> pd.DataFrame:
    dfs = to_petab_dataframes(timecourse)
    petab.write_condition_df(dfs[CONDITION], str(condition_location))
    write_timecourse_df(dfs[TIMECOURSE], str(timecourse_location))
Beispiel #3
0
def test_write_condition_df():
    """Test conditions.write_condition_df."""
    condition_df = pd.DataFrame(
        data={
            CONDITION_ID: ['condition1', 'condition2'],
            CONDITION_NAME: ['Condition 1', 'Condition 2'],
            'fixedParameter1': [1.0, 2.0]
        }).set_index(CONDITION_ID)

    with tempfile.NamedTemporaryFile(mode='w', delete=True) as fh:
        file_name = fh.name
        petab.write_condition_df(condition_df, file_name)
        re_df = petab.get_condition_df(file_name)
        assert (condition_df == re_df).all().all()
def test_write_condition_df():
    """Test conditions.write_condition_df."""
    condition_df = pd.DataFrame(
        data={
            CONDITION_ID: ['condition1', 'condition2'],
            CONDITION_NAME: ['Condition 1', 'Condition 2'],
            'fixedParameter1': [1.0, 2.0]
        }).set_index(CONDITION_ID)

    with tempfile.TemporaryDirectory() as temp_dir:
        file_name = Path(temp_dir) / "conditions.tsv"
        petab.write_condition_df(condition_df, file_name)
        re_df = petab.get_condition_df(file_name)
        assert (condition_df == re_df).all().all()
Beispiel #5
0
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')
Beispiel #7
0
    OBSERVABLE_ID: ['obs_a'],
    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"]
    }]