Skip to content

Commit

Permalink
Add model type constants; add type code to class; add list of known t…
Browse files Browse the repository at this point in the history
…ypes
  • Loading branch information
dweindl committed May 19, 2022
1 parent 507c196 commit 7d9dae8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
4 changes: 4 additions & 0 deletions petab/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
MODEL_TYPE_SBML = 'sbml'

known_model_types = {MODEL_TYPE_SBML}

from .model import Model # noqa F401
16 changes: 13 additions & 3 deletions petab/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import abc
from typing import Any, Iterable, Tuple
from . import MODEL_TYPE_SBML, known_model_types


class Model:
Expand All @@ -24,6 +25,12 @@ def from_file(filepath_or_buffer: Any) -> Model:
"""
...

@classmethod
@property
@abc.abstractmethod
def type_id(cls):
...

@abc.abstractmethod
def get_parameter_ids(self) -> Iterable[str]:
"""Get all parameter IDs from this model
Expand Down Expand Up @@ -114,9 +121,12 @@ def model_factory(filepath_or_buffer: Any, model_language: str) -> Model:
:param model_language: PEtab model language ID for the given model
:returns: A :py:class:`Model` instance representing the given model
"""

if model_language == "sbml":
if model_language == MODEL_TYPE_SBML:
from .sbml_model import SbmlModel
return SbmlModel.from_file(filepath_or_buffer)

raise ValueError(f"Unsupported model format: {model_language}")
if model_language in known_model_types:
raise NotImplementedError(
f"Unsupported model format: {model_language}")

raise ValueError(f"Unknown model format: {model_language}")
4 changes: 4 additions & 0 deletions petab/models/sbml_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@

import libsbml

from . import MODEL_TYPE_SBML
from .model import Model
from ..sbml import (get_sbml_model, is_sbml_consistent, load_sbml_from_string,
log_sbml_errors)


class SbmlModel(Model):
"""PEtab wrapper for SBML models"""

type_id = MODEL_TYPE_SBML

def __init__(
self,
sbml_model: libsbml.Model = None,
Expand Down
7 changes: 5 additions & 2 deletions petab/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from . import (conditions, core, format_version, measurements, observables,
parameter_mapping, parameters, sampling, sbml, yaml)
from .C import * # noqa: F403
from .models import MODEL_TYPE_SBML
from .models.model import Model, model_factory
from .models.sbml_model import SbmlModel

Expand Down Expand Up @@ -127,7 +128,8 @@ def from_files(
"future version. Use `petab.Problem.from_yaml instead.",
DeprecationWarning, stacklevel=2)

model = model_factory(sbml_file, 'sbml') if sbml_file else None
model = model_factory(sbml_file, MODEL_TYPE_SBML) \
if sbml_file else None

condition_df = conditions.get_condition_df(condition_file) \
if condition_file else None
Expand Down Expand Up @@ -219,7 +221,8 @@ def from_yaml(yaml_config: Union[Dict, Path, str]) -> 'Problem':
get_path(yaml_config[PARAMETER_FILE])) \
if yaml_config[PARAMETER_FILE] else None

model = model_factory(get_path(problem0[SBML_FILES][0]), 'sbml') \
model = model_factory(get_path(problem0[SBML_FILES][0]),
MODEL_TYPE_SBML) \
if problem0[SBML_FILES] else None

if problem0[MEASUREMENT_FILES]:
Expand Down

0 comments on commit 7d9dae8

Please sign in to comment.