Exemplo n.º 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)
Exemplo n.º 2
0
def test_write_parameter_df():
    """Test parameters.write_parameter_df."""
    parameter_df = pd.DataFrame(data={
        PARAMETER_ID: ['par1', 'par2'],
        PARAMETER_NAME: ['parname1', 'parname2'],
    }).set_index(PARAMETER_ID)

    with tempfile.NamedTemporaryFile(mode='w', delete=True) as fh:
        file_name = fh.name
        petab.write_parameter_df(parameter_df, file_name)
        re_df = petab.get_parameter_df(file_name)
        assert (parameter_df == re_df).all().all()
Exemplo n.º 3
0
def test_write_parameter_df():
    """Test parameters.write_parameter_df."""
    parameter_df = pd.DataFrame(data={
        PARAMETER_ID: ['par1', 'par2'],
        # Test utf8 characters
        PARAMETER_NAME: ['ɑ', 'β'],
    }).set_index(PARAMETER_ID)

    with tempfile.TemporaryDirectory() as temp_dir:
        file_name = Path(temp_dir) / "parameters.tsv"
        petab.write_parameter_df(parameter_df, file_name)
        re_df = petab.get_parameter_df(file_name)
        assert (parameter_df == re_df).all().all()
Exemplo n.º 4
0
def create_test_data(measurement_file_name, parameter_file_name, yaml_config,
                     yaml_file_name_test, model_output_dir, model_name,
                     hdf5_file_name):
    """Create some synthetic data to emulate a test set"""

    test_measurement_file_name = \
        "-testset".join(os.path.splitext(measurement_file_name))
    test_parameter_file_name = \
        "-testset".join(os.path.splitext(parameter_file_name))

    # measurements
    df = petab.get_measurement_df(measurement_file_name)
    df.loc[df.observableParameters == 'scaling_x1_common', 'measurement'] = \
        df.loc[df.observableParameters == 'scaling_x1_common', 'measurement'] \
        * 2.0
    df.loc[~df.observableParameters.isnull(), 'observableParameters'] = \
        df.loc[~df.observableParameters.isnull(), 'observableParameters'] \
        + "_test"
    petab.write_parameter_df(df, test_measurement_file_name)

    # parameters
    df = petab.get_parameter_df(parameter_file_name)
    df.rename(index={
        'scaling_x1_common': 'scaling_x1_common_test',
        'offset_x2_batch_0': 'offset_x2_batch_0_test',
        'offset_x2_batch_1': 'offset_x2_batch_1_test'
    },
              inplace=True)
    petab.write_parameter_df(df, test_parameter_file_name)

    # yaml
    yaml_config[ptc.PARAMETER_FILE] = test_parameter_file_name
    yaml_config[ptc.PROBLEMS][0][ptc.MEASUREMENT_FILES][0] = \
        test_measurement_file_name
    with open(yaml_file_name_test, 'w') as outfile:
        yaml.dump(yaml_config, outfile, default_flow_style=False)

    generate_hdf5_file(yaml_file=yaml_file_name_test,
                       model_output_dir=model_output_dir,
                       hdf5_file_name="-testset".join(
                           os.path.splitext(hdf5_file_name)),
                       model_name=model_name)
Exemplo n.º 5
0
def write_problem(
    test_id: int,
    parameter_df: pd.DataFrame,
    condition_dfs: Union[List[pd.DataFrame], pd.DataFrame],
    observable_dfs: Union[List[pd.DataFrame], pd.DataFrame],
    measurement_dfs: Union[List[pd.DataFrame], pd.DataFrame],
    sbml_files: Union[List[str], str] = None,
) -> None:
    """Write problem to files.

    Parameters
    ----------
    test_id: Identifier of the test.
    parameter_df: PEtab parameter table.
    condition_dfs: PEtab condition tables.
    observable_dfs: PEtab observable tables.
    measurement_dfs: PEtab measurement tables.
    sbml_files: PEtab SBML files. If None, then the default
        petabtests.DEFAULT_MODEL_FILE is used.
    """
    print(f"Writing case {test_id}...")
    # convenience
    if isinstance(condition_dfs, pd.DataFrame):
        condition_dfs = [condition_dfs]
    if isinstance(observable_dfs, pd.DataFrame):
        observable_dfs = [observable_dfs]
    if isinstance(measurement_dfs, pd.DataFrame):
        measurement_dfs = [measurement_dfs]
    if isinstance(sbml_files, str):
        sbml_files = [sbml_files]

    # id to string
    id_str = test_id_str(test_id)
    dir_ = os.path.join(CASES_DIR, id_str)

    # petab yaml
    config = {
        FORMAT_VERSION:
        petab.__format_version__,
        PROBLEMS: [
            {
                SBML_FILES: [],
                CONDITION_FILES: [],
                MEASUREMENT_FILES: [],
                OBSERVABLE_FILES: [],
            },
        ]
    }

    # copy models
    if sbml_files is None:
        sbml_files = [DEFAULT_MODEL_FILE]
    copied_sbml_files = []
    for i_sbml, sbml_file in enumerate(sbml_files):
        if len(sbml_files) == 1:
            copied_sbml_file = '_model.xml'
        else:
            copied_sbml_file = f'_model{i_sbml}.xml'
        copyfile(os.path.join(dir_, sbml_file),
                 os.path.join(dir_, copied_sbml_file))
        copied_sbml_files.append(copied_sbml_file)
    config[PROBLEMS][0][SBML_FILES] = copied_sbml_files

    # write parameters
    parameters_file = '_parameters.tsv'
    petab.write_parameter_df(parameter_df, os.path.join(dir_, parameters_file))
    config[PARAMETER_FILE] = parameters_file

    # write conditions
    _write_dfs_to_files(id_str, 'conditions', petab.write_condition_df,
                        condition_dfs, config[PROBLEMS][0][CONDITION_FILES])

    # write observables
    _write_dfs_to_files(id_str, 'observables', petab.write_observable_df,
                        observable_dfs, config[PROBLEMS][0][OBSERVABLE_FILES])

    # write measurements
    _write_dfs_to_files(id_str, 'measurements', petab.write_measurement_df,
                        measurement_dfs,
                        config[PROBLEMS][0][MEASUREMENT_FILES])

    # validate petab yaml
    petab.validate(config, path_prefix=dir_)

    # write yaml
    yaml_file = problem_yaml_name(test_id)
    with open(os.path.join(dir_, yaml_file), 'w') as outfile:
        yaml.dump(config, outfile, default_flow_style=False)

    # validate written PEtab files
    problem = petab.Problem.from_yaml(os.path.join(dir_, yaml_file))
    petab.lint_problem(problem)
Exemplo n.º 6
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')
Exemplo n.º 8
0
def write_problem(test_id: int,
                  parameter_df: pd.DataFrame,
                  condition_dfs: Union[List[pd.DataFrame], pd.DataFrame],
                  observable_dfs: Union[List[pd.DataFrame], pd.DataFrame],
                  measurement_dfs: Union[List[pd.DataFrame], pd.DataFrame],
                  model_files: Union[List[str], str],
                  format_: str = 'sbml') -> None:
    """Write problem to files.

    Parameters
    ----------
    test_id: Identifier of the test.
    parameter_df: PEtab parameter table.
    condition_dfs: PEtab condition tables.
    observable_dfs: PEtab observable tables.
    measurement_dfs: PEtab measurement tables.
    model_files: PEtab SBML/PySB files.
    format: Model format (SBML/PySB)
    """
    print(f"Writing case {test_id} {format_} ...")
    # convenience
    if isinstance(condition_dfs, pd.DataFrame):
        condition_dfs = [condition_dfs]
    if isinstance(observable_dfs, pd.DataFrame):
        observable_dfs = [observable_dfs]
    if isinstance(measurement_dfs, pd.DataFrame):
        measurement_dfs = [measurement_dfs]
    if isinstance(model_files, str):
        model_files = [model_files]

    # id to string
    dir_ = case_dir(test_id, format_)

    # petab yaml
    config = {
        FORMAT_VERSION:
        petab.__format_version__,
        PROBLEMS: [
            {
                SBML_FILES: [],
                CONDITION_FILES: [],
                MEASUREMENT_FILES: [],
                OBSERVABLE_FILES: [],
            },
        ]
    }

    if format_ == 'sbml':
        suffix = '.xml'
    else:
        suffix = '.py'

    # copy models
    copied_model_files = []
    for i_sbml, model_file in enumerate(model_files):
        if len(model_files) == 1:
            copied_model_file = f'_model{suffix}'
        else:
            copied_model_file = f'_model{i_sbml}{suffix}'
        copyfile(os.path.join(dir_, model_file),
                 os.path.join(dir_, copied_model_file))
        copied_model_files.append(copied_model_file)
    config[PROBLEMS][0][SBML_FILES] = copied_model_files

    # write parameters
    parameters_file = '_parameters.tsv'
    petab.write_parameter_df(parameter_df, os.path.join(dir_, parameters_file))
    config[PARAMETER_FILE] = parameters_file

    # write conditions
    _write_dfs_to_files(dir_, 'conditions', petab.write_condition_df,
                        condition_dfs, config[PROBLEMS][0][CONDITION_FILES])

    # write observables
    _write_dfs_to_files(dir_, 'observables', petab.write_observable_df,
                        observable_dfs, config[PROBLEMS][0][OBSERVABLE_FILES])

    # write measurements
    _write_dfs_to_files(dir_, 'measurements', petab.write_measurement_df,
                        measurement_dfs,
                        config[PROBLEMS][0][MEASUREMENT_FILES])

    # validate petab yaml
    petab.validate(config, path_prefix=dir_)

    # write yaml
    yaml_file = problem_yaml_name(test_id)
    with open(os.path.join(dir_, yaml_file), 'w') as outfile:
        yaml.dump(config, outfile, default_flow_style=False)

    # validate written PEtab files
    problem = petab.Problem.from_yaml(os.path.join(dir_, yaml_file))
    petab.lint_problem(problem)
Exemplo n.º 9
0
}).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"]
    }]
}
petab.write_yaml(yaml_config, "conversion_reaction.yaml")