Skip to content

Commit

Permalink
Change ThinLayer missing dependency handling to give RuntimeError
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrohwer committed Feb 23, 2022
1 parent a07c940 commit b9269cc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
14 changes: 12 additions & 2 deletions pyenzyme/thinlayers/TL_Copasi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@
from pyenzyme import EnzymeMLDocument
from pyenzyme.thinlayers import BaseThinLayer
import os

import COPASI
import pandas as pd
from builtins import enumerate

_COPASI_IMPORT_ERROR = None
try:
import COPASI
except ModuleNotFoundError as e:
_COPASI_IMPORT_ERROR = """
ThinLayerCopasi is not available.
To use it, please install the following dependencies:
{}
""".format(e)

class ThinLayerCopasi(BaseThinLayer):

Expand All @@ -36,6 +43,9 @@ def __init__(self, path, outdir,
:param measurement_ids: the measurement ids or all
:param init_file: optional initialization file for fit items
"""
# check dependencies
if _COPASI_IMPORT_ERROR:
raise RuntimeError(_COPASI_IMPORT_ERROR)

# initialize base class, let it do the reading
BaseThinLayer.__init__(
Expand Down
21 changes: 16 additions & 5 deletions pyenzyme/thinlayers/TL_Pysces.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@
import numpy as np
import pandas as pd
import os
import pysces
import lmfit
import copy

from typing import Union, Optional
from pyenzyme.thinlayers.TL_Base import BaseThinLayer

_PYSCES_IMPORT_ERROR = None
try:
import pysces
import lmfit
except ModuleNotFoundError as e:
_PYSCES_IMPORT_ERROR = """
ThinLayerPysces is not available.
To use it, please install the following dependencies:
{}
""".format(e)

class ThinLayerPysces(BaseThinLayer):

Expand All @@ -28,6 +35,10 @@ def __init__(
init_file: Optional[str] = None
):

# check dependencies
if _PYSCES_IMPORT_ERROR:
raise RuntimeError(_PYSCES_IMPORT_ERROR)

super().__init__(path, measurement_ids, init_file)

# Convert model to PSC and get experimental data
Expand Down Expand Up @@ -187,7 +198,7 @@ def _get_pysces_model(self, model_dir: str):
# Finally, load the PSC model
self.model = pysces.model(sbmlfile_name, dir=model_dir)

def _calculate_residual(self, parameters: lmfit.Parameters) -> np.ndarray:
def _calculate_residual(self, parameters) -> np.ndarray:
"""Function that will be optimized"""

simulated_data = self._simulate_experiment(parameters)
Expand All @@ -197,7 +208,7 @@ def _calculate_residual(self, parameters: lmfit.Parameters) -> np.ndarray:

return np.array(self.experimental_data - simulated_data)

def _simulate_experiment(self, parameters: lmfit.Parameters):
def _simulate_experiment(self, parameters):
"""Performs simulation based on the PySCeS model"""

self.model.SetQuiet()
Expand Down
21 changes: 2 additions & 19 deletions pyenzyme/thinlayers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
import warnings

from .TL_Base import BaseThinLayer
from .TL_Strenda import ThinLayerStrendaML

try:
from .TL_Pysces import ThinLayerPysces
except ModuleNotFoundError as e:
print(
"%s - ThinLayerPysces now returns 'None', please provide the dependencies to use this module." % str(e)
)
ThinLayerPysces = None

try:
from .TL_Copasi import ThinLayerCopasi
except ModuleNotFoundError as e:
print(
"%s - ThinLayerCopasi now returns 'None', please provide the dependencies to use this module." % str(e)
)
ThinLayerCopasi = None

from .TL_Pysces import ThinLayerPysces
from .TL_Copasi import ThinLayerCopasi

0 comments on commit b9269cc

Please sign in to comment.