Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ temp_testing/*
build
dist
Issues/rule_keywords/test_DeleteMolecules_changed.bngl
.jules/
__pycache__/
11 changes: 11 additions & 0 deletions bionetgen/core/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
28 changes: 21 additions & 7 deletions bionetgen/simulator/csimulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
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()
app.setup()
conf = app.config["bionetgen"]
def_bng_path = conf["bngpath"]

logger = BNGLogger()


class RESULT(ctypes.Structure):
_fields_ = [
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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"))
Expand Down
Loading