From dc45893efb168b92548594e55742d84f7c00a7fe Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 13 Apr 2026 13:43:17 +0000 Subject: [PATCH 1/2] Refactor CSimulator error handling to use BNGLogger and BNGSimulatorError Co-authored-by: akutuva21 <44119804+akutuva21@users.noreply.github.com> --- bionetgen/core/exc.py | 11 +++++++++++ bionetgen/simulator/csimulator.py | 26 +++++++++++++++++++------- 2 files changed, 30 insertions(+), 7 deletions(-) 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..2a5527e4 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,7 @@ 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 +173,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")) From c0fe1c83c68180e203d393f0afd57e73e47cbf32 Mon Sep 17 00:00:00 2001 From: akutuva21 Date: Mon, 13 Apr 2026 10:20:28 -0400 Subject: [PATCH 2/2] chore: PR #92 remove forbidden artifacts and run black --- .gitignore | 2 ++ bionetgen/simulator/csimulator.py | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) 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/simulator/csimulator.py b/bionetgen/simulator/csimulator.py index 2a5527e4..f99ccd30 100644 --- a/bionetgen/simulator/csimulator.py +++ b/bionetgen/simulator/csimulator.py @@ -152,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): - logger.warning("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