From 74e952cea4f79dfa1c3836e1282be4d3f87e0e31 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:30 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20Transition=20csimulator=20init?= =?UTF-8?q?=20asserts=20to=20BNGErrors=20and=20BNGLogger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced plain `assert` statements used during species and parameter initialization in `csimulator.py` with proper error handling. The code now explicitly checks lengths, logs detailed mismatch info using `BNGLogger`, and raises a newly introduced `BNGSimulatorError`. Co-authored-by: akutuva21 <44119804+akutuva21@users.noreply.github.com> --- bionetgen/core/exc.py | 7 +++++++ bionetgen/simulator/csimulator.py | 20 +++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/bionetgen/core/exc.py b/bionetgen/core/exc.py index 11f5307f..a260a1db 100644 --- a/bionetgen/core/exc.py +++ b/bionetgen/core/exc.py @@ -93,3 +93,10 @@ def __init__( self.model = model self.message = message super().__init__(self.message) + +class BNGSimulatorError(BNGError): + """Error related to BNG simulators.""" + + def __init__(self, message="There was an issue with the BNG simulator"): + self.message = message + super().__init__(self.message) diff --git a/bionetgen/simulator/csimulator.py b/bionetgen/simulator/csimulator.py index 34a6bdcd..c72cab53 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() @@ -39,6 +40,7 @@ class CSimWrapper: """ def __init__(self, lib_path, num_params=None, num_spec_init=None): + self.logger = BNGLogger() # we need the result struct to reconstruct the object self.return_struct = RESULT # load the shared library @@ -55,16 +57,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: + self.logger.error( + f"Length of species initialization array ({len(arr)}) does not match expected length ({self.num_spec_init})", + loc=f"{__file__} : CSimWrapper.set_species_init()" + ) + raise BNGSimulatorError(f"Expected {self.num_spec_init} initial species, but 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: + self.logger.error( + f"Length of parameter array ({len(arr)}) does not match expected length ({self.num_params})", + loc=f"{__file__} : CSimWrapper.set_parameters()" + ) + raise BNGSimulatorError(f"Expected {self.num_params} parameters, but got {len(arr)}") self.parameters = np.array(arr, dtype=np.float64) def simulate(self, t_start=0, t_end=100, n_steps=100):