diff --git a/.gitignore b/.gitignore index 39aa9026..f9bf9c80 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ temp_testing/* build dist Issues/rule_keywords/test_DeleteMolecules_changed.bngl +.jules/ +__pycache__/ diff --git a/bionetgen/core/exc.py b/bionetgen/core/exc.py index 11f5307f..f4f25a8a 100644 --- a/bionetgen/core/exc.py +++ b/bionetgen/core/exc.py @@ -93,3 +93,14 @@ def __init__( self.model = model self.message = message super().__init__(self.message) + + +class BNGSimulatorError(BNGError): + """Error related to simulating a BNG model.""" + + def __init__( + self, + message="There was an issue simulating your BNG model", + ): + self.message = message + super().__init__(self.message) diff --git a/bionetgen/simulator/csimulator.py b/bionetgen/simulator/csimulator.py index 34a6bdcd..f99ccd30 100644 --- a/bionetgen/simulator/csimulator.py +++ b/bionetgen/simulator/csimulator.py @@ -4,7 +4,8 @@ from distutils import ccompiler from .bngsimulator import BNGSimulator from bionetgen.main import BioNetGen -from bionetgen.core.exc import BNGCompileError +from bionetgen.core.exc import BNGCompileError, BNGSimulatorError +from bionetgen.core.utils.logging import BNGLogger # This allows access to the CLIs config setup app = BioNetGen() @@ -12,6 +13,8 @@ conf = app.config["bionetgen"] def_bng_path = conf["bngpath"] +logger = BNGLogger() + class RESULT(ctypes.Structure): _fields_ = [ @@ -55,16 +58,24 @@ def set_species_init(self, arr): """ Set the initial species values array """ - # TODO: Transition to BNGErrors and logging - assert len(arr) == self.num_spec_init + if len(arr) != self.num_spec_init: + logger.error( + f"Expected {self.num_spec_init} initial species values, got {len(arr)}" + ) + raise BNGSimulatorError( + f"Expected {self.num_spec_init} initial species values, got {len(arr)}" + ) self.species_init = np.array(arr, dtype=np.float64) def set_parameters(self, arr): """ Set the parameter values array """ - # TODO: Transition to BNGErrors and logging - assert len(arr) == self.num_params + if len(arr) != self.num_params: + logger.error(f"Expected {self.num_params} parameters, got {len(arr)}") + raise BNGSimulatorError( + f"Expected {self.num_params} parameters, got {len(arr)}" + ) self.parameters = np.array(arr, dtype=np.float64) def simulate(self, t_start=0, t_end=100, n_steps=100): @@ -141,7 +152,9 @@ class CSimulator(BNGSimulator): def __init__(self, model_file, generate_network=False): # check cvode library paths if (conf.get("cvode_include") is None) or (conf.get("cvode_lib") is None): - print("CVODE include and library paths are not set, compilation won't work") + logger.warning( + "CVODE include and library paths are not set, compilation won't work" + ) # let's load the model first if isinstance(model_file, str): # load model file @@ -162,7 +175,8 @@ def __init__(self, model_file, generate_network=False): ) os.chdir(cd) else: - print(f"model format not recognized: {model_file}") + logger.error(f"model format not recognized: {model_file}") + raise BNGSimulatorError(f"model format not recognized: {model_file}") # set compiler self.compiler = ccompiler.new_compiler() self.compiler.add_include_dir(conf.get("cvode_include"))