Skip to content

Commit dde5478

Browse files
authored
Convenience method to quickly output PEtab problem to files (#71)
- new method `petab.Problem.to_files_generic` to output a PEtab problem to a folder of generically-named files - new argument `petab.Problem.to_files(..., prefix_path=...)` to specify a prefix for all output paths
1 parent ad1229e commit dde5478

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

petab/problem.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
"""PEtab Problem class"""
22

33
import os
4+
# Renamed to `Path_` to avoid unknown error during Sphinx doc build
5+
from pathlib import Path as Path_
46
import tempfile
7+
from typing import Dict, Iterable, List, Optional, Union
58
from warnings import warn
69

710
import pandas as pd
811
import libsbml
9-
from typing import Optional, List, Union, Dict, Iterable
1012
from . import (parameter_mapping, measurements, conditions, parameters,
1113
sampling, sbml, yaml, core, observables, format_version)
1214
from .C import * # noqa: F403
@@ -264,6 +266,52 @@ def from_combine(filename: str) -> 'Problem':
264266

265267
return problem
266268

269+
def to_files_generic(
270+
self,
271+
prefix_path: Union[str, Path_],
272+
) -> None:
273+
"""Save a PEtab problem to generic file names.
274+
275+
The PEtab problem YAML file is always created. PEtab data files are
276+
only created if the PEtab problem contains corresponding data (e.g. a
277+
PEtab visualization TSV file is only created if the PEtab problem has
278+
one).
279+
280+
Arguments:
281+
prefix_path: Specify a prefix to all paths, to avoid specifying the
282+
prefix for all paths individually. NB: the prefix is added to paths
283+
before `relative_paths` is handled downstream in
284+
`petab.yaml.create_problem_yaml`.
285+
286+
Returns:
287+
The path to the PEtab problem YAML file.
288+
"""
289+
prefix_path = Path_(prefix_path)
290+
291+
# Generate generic filenames for data tables in the PEtab problem that
292+
# contain data.
293+
filenames = {}
294+
for table_name in [
295+
'condition',
296+
'measurement',
297+
'parameter',
298+
'observable',
299+
'visualization',
300+
]:
301+
if getattr(self, f'{table_name}_df') is not None:
302+
filenames[f'{table_name}_file'] = f'{table_name}s.tsv'
303+
304+
if self.sbml_document is not None:
305+
filenames['sbml_file'] = 'model.xml'
306+
307+
filenames['yaml_file'] = 'problem.yaml'
308+
309+
self.to_files(**filenames, prefix_path=prefix_path)
310+
311+
if prefix_path is None:
312+
return filenames['yaml_file']
313+
return str(prefix_path / filenames['yaml_file'])
314+
267315
def to_files(self,
268316
sbml_file: Optional[str] = None,
269317
condition_file: Optional[str] = None,
@@ -272,6 +320,7 @@ def to_files(self,
272320
visualization_file: Optional[str] = None,
273321
observable_file: Optional[str] = None,
274322
yaml_file: Optional[str] = None,
323+
prefix_path: Optional[Union[str, Path_]] = None,
275324
relative_paths: bool = True,) -> None:
276325
"""
277326
Write PEtab tables to files for this problem
@@ -290,6 +339,9 @@ def to_files(self,
290339
visualization_file: Visualization table destination
291340
observable_file: Observables table destination
292341
yaml_file: YAML file destination
342+
prefix_path: Specify a prefix to all paths, to avoid specifying the
343+
prefix for all paths individually. NB: the prefix is added to paths
344+
before `relative_paths` is handled.
293345
relative_paths: whether all paths in the YAML file should be
294346
relative to the location of the YAML file. If `False`, then paths
295347
are left unchanged.
@@ -298,6 +350,21 @@ def to_files(self,
298350
ValueError:
299351
If a destination was provided for a non-existing entity.
300352
"""
353+
if prefix_path is not None:
354+
prefix_path = Path_(prefix_path)
355+
356+
def add_prefix(path0: Union[None, str, Path_]) -> str:
357+
if path0 is None:
358+
return path0
359+
return str(prefix_path / path0)
360+
361+
sbml_file = add_prefix(sbml_file)
362+
condition_file = add_prefix(condition_file)
363+
measurement_file = add_prefix(measurement_file)
364+
parameter_file = add_prefix(parameter_file)
365+
observable_file = add_prefix(observable_file)
366+
visualization_file = add_prefix(visualization_file)
367+
yaml_file = add_prefix(yaml_file)
301368

302369
if sbml_file:
303370
if self.sbml_document is not None:

0 commit comments

Comments
 (0)