modelbase2 0.5.0__py3-none-any.whl → 0.7.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
modelbase2/npe.py CHANGED
@@ -174,7 +174,8 @@ def train_torch_ss_estimator(
174
174
  n_hidden = max(2 * len(features.columns) * len(targets.columns), 10)
175
175
  n_outputs = len(targets.columns)
176
176
  approximator = MLP(
177
- n_inputs=len(features.columns), layers=[n_hidden, n_hidden, n_outputs]
177
+ n_inputs=len(features.columns),
178
+ neurons_per_layer=[n_hidden, n_hidden, n_outputs],
178
179
  ).to(device)
179
180
 
180
181
  features_ = torch.Tensor(features.to_numpy(), device=device)
@@ -17,8 +17,7 @@ def get_km_and_kcat_from_brenda(
17
17
  You can obtain the database from https://www.brenda-enzymes.org/download.php
18
18
  """
19
19
  brenda = Brenda()
20
- if brenda_path is not None:
21
- brenda.read_database(brenda_path)
20
+ brenda.read_database(brenda_path)
22
21
 
23
22
  kms, kcats = brenda.get_kms_and_kcats(
24
23
  ec=ec,
@@ -1,16 +1,14 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import ast
4
- import inspect
5
4
  import re
6
- import textwrap
7
5
  from datetime import UTC, datetime
8
6
  from typing import TYPE_CHECKING, Any, cast
9
7
 
10
- import dill
11
8
  import libsbml
12
9
  import numpy as np
13
10
 
11
+ from modelbase2.experimental.source_tools import get_fn_ast
14
12
  from modelbase2.sbml._data import AtomicUnit, Compartment
15
13
  from modelbase2.types import Derived
16
14
 
@@ -322,17 +320,7 @@ def _tree_to_sbml(
322
320
 
323
321
 
324
322
  def _sbmlify_fn(fn: Callable, user_args: list[str]) -> libsbml.ASTNode:
325
- try:
326
- source = inspect.getsource(fn)
327
- except OSError: # could not get source code
328
- source = dill.source.getsource(fn)
329
-
330
- tree = ast.parse(textwrap.dedent(source))
331
- if not isinstance(fn_def := tree.body[0], ast.FunctionDef):
332
- msg = "Not a function"
333
- raise TypeError(msg)
334
-
335
- return _tree_to_sbml(fn_def, args=user_args)
323
+ return _tree_to_sbml(get_fn_ast(fn), args=user_args)
336
324
 
337
325
 
338
326
  ##########################################################################
@@ -492,6 +492,16 @@ def _codgen(name: str, sbml: Parser) -> Path:
492
492
  else:
493
493
  variables[k] = v.size
494
494
 
495
+ # Ensure non-zero value for initial assignments
496
+ # EXPLAIN: we need to do this for the first round of get_dependent to work
497
+ # otherwise we run into a ton of DivisionByZero errors.
498
+ # Since the values are overwritte afterwards, it doesn't really matter anyways
499
+ for k in sbml.initial_assignment:
500
+ if k in parameters and parameters[k] == 0:
501
+ parameters[k] = 1
502
+ if k in variables and variables[k] == 0:
503
+ variables[k] = 1
504
+
495
505
  derived_str = "\n ".join(
496
506
  f"m.add_derived('{k}', fn={k}, args={v.args})" for k, v in sbml.derived.items()
497
507
  )
@@ -539,7 +549,7 @@ def get_model() -> Model:
539
549
  {variables_str}
540
550
  {derived_str}
541
551
  {rxn_str}
542
- args = m.get_args()
552
+ args = m.get_dependent()
543
553
  {initial_assignment_source}
544
554
  return m
545
555
  """
modelbase2/scan.py CHANGED
@@ -35,7 +35,7 @@ import pandas as pd
35
35
 
36
36
  from modelbase2.parallel import Cache, parallelise
37
37
  from modelbase2.simulator import Simulator
38
- from modelbase2.types import ProtocolByPars, SteadyStates, TimeCourseByPars
38
+ from modelbase2.types import ProtocolByPars, SteadyStates, TimeCourseByPars, unwrap
39
39
 
40
40
  if TYPE_CHECKING:
41
41
  from collections.abc import Callable
@@ -325,10 +325,10 @@ def _steady_state_worker(
325
325
 
326
326
  """
327
327
  try:
328
- c, v = (
328
+ c, v = unwrap(
329
329
  Simulator(model, y0=y0)
330
330
  .simulate_to_steady_state(rel_norm=rel_norm)
331
- .get_full_concs_and_fluxes()
331
+ .get_result()
332
332
  )
333
333
  except ZeroDivisionError:
334
334
  c = None
@@ -353,10 +353,10 @@ def _time_course_worker(
353
353
 
354
354
  """
355
355
  try:
356
- c, v = (
356
+ c, v = unwrap(
357
357
  Simulator(model, y0=y0)
358
358
  .simulate_time_course(time_points=time_points)
359
- .get_full_concs_and_fluxes()
359
+ .get_result()
360
360
  )
361
361
  except ZeroDivisionError:
362
362
  c = None
@@ -382,13 +382,13 @@ def _protocol_worker(
382
382
  TimeCourse: Object containing protocol series concentrations and fluxes.
383
383
 
384
384
  """
385
- c, v = (
385
+ c, v = unwrap(
386
386
  Simulator(model, y0=y0)
387
387
  .simulate_over_protocol(
388
388
  protocol=protocol,
389
389
  time_points_per_step=time_points_per_step,
390
390
  )
391
- .get_full_concs_and_fluxes()
391
+ .get_result()
392
392
  )
393
393
  time_points = np.linspace(
394
394
  0,
@@ -467,7 +467,7 @@ def steady_state(
467
467
  )
468
468
  concs.index = idx
469
469
  fluxes.index = idx
470
- return SteadyStates(concs, fluxes, parameters=parameters)
470
+ return SteadyStates(concs=concs, fluxes=fluxes, parameters=parameters)
471
471
 
472
472
 
473
473
  def time_course(