Skip to content

Commit

Permalink
Use empty arrays when there are no observables or expressions (#481)
Browse files Browse the repository at this point in the history
* Use empty arrays when there are no observables or expressions
* Add unit test
  • Loading branch information
ortega2247 committed Jan 29, 2020
1 parent 5dc1604 commit 1f92ce5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
13 changes: 7 additions & 6 deletions pysb/simulator/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,14 +805,15 @@ def __init__(self, simulator, tout, trajectories=None,
len(tout[n]) * len(expr_names)).view(dtype=yexpr_dtype) for n
in range(self.nsims)]
else:
self._yobs = [np.ndarray((len(self.tout[n]),),
dtype=yobs_dtype) for n in range(self.nsims)]
self._yobs = [np.ndarray((len(self.tout[n]),), dtype=yobs_dtype) if obs_names
else np.ndarray((len(self.tout[n]), 0), dtype=yobs_dtype)
for n in range(self.nsims)]
self._yobs_view = [self._yobs[n].view(float).
reshape(len(self._yobs[n]), -1) for n in range(
self.nsims)]
self._yexpr = [np.ndarray((len(self.tout[n]),),
dtype=yexpr_dtype) for n in range(
reshape(len(self._yobs[n]), -1) for n in range(
self.nsims)]
self._yexpr = [np.ndarray((len(self.tout[n]),), dtype=yexpr_dtype) if expr_names
else np.ndarray((len(self.tout[n]), 0), dtype=yexpr_dtype)
for n in range(self.nsims)]
self._yexpr_view = [self._yexpr[n].view(float).reshape(len(
self._yexpr[n]), -1) for n in range(self.nsims)]

Expand Down
13 changes: 13 additions & 0 deletions pysb/tests/test_simulationresult.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pysb.pattern import SpeciesPatternMatcher
import collections
import copy
import io
import pandas as pd


Expand Down Expand Up @@ -190,6 +191,18 @@ def test_save_load():
_check_resultsets_equal(nfres2, nfres2_load)


def test_save_load_observables_expressions():
buff = io.BytesIO()
tspan = np.linspace(0, 100, 100)
sim = ScipyOdeSimulator(tyson_oscillator.model, tspan).run()
sim.save(buff, include_obs_exprs=True)

sim2 = SimulationResult.load(buff)
assert len(sim2.observables) == len(tspan)
# Tyson oscillator doesn't have expressions
assert_raises(ValueError, lambda: sim2.expressions)


def _check_resultsets_equal(res1, res2):
try:
assert np.allclose(res1.species, res2.species)
Expand Down

0 comments on commit 1f92ce5

Please sign in to comment.