def test_to_csv_single_result_no_path(self): test_data = Trajectory({'time': [0]}, model=Model('test_model'), solver_name='test_solver_name') result = Results(data=[test_data]) test_nametag = "test_nametag" test_stamp = "test_stamp"
def test_to_csv_single_result_no_nametag(self): test_model = Model('test_model') test_data = Trajectory(data={'time': [0]}, model=test_model) with tempfile.TemporaryDirectory() as tempdir: result.to_csv(nametag=test_nametag, path=tempdir) assert len(os.listdir(tempdir)) is not 0
def test_to_csv_single_result_no_nametag(self): test_model = Model('test_model') test_data = Trajectory(data={'time': [0]}, model=test_model) result = Results(data=[test_data]) result.solver_name = 'test_solver' test_stamp = "test_stamp" with tempfile.TemporaryDirectory() as tempdir: result.to_csv(stamp=test_stamp, path=tempdir) assert len(os.listdir(tempdir)) is not 0
def test_xscale_plot(self): from unittest import mock trajectory = Trajectory(data={ 'time': [0.], 'foo': [1.] }, model=Model('test_model')) results = Results(data=[trajectory]) with mock.patch('matplotlib.pyplot.xscale') as mock_xscale: results.plot(xscale='log') mock_xscale.assert_called_with('log')
def test_pickle_stable_plot_iterate(self): from unittest import mock from gillespy2.core.results import _plot_iterate trajectory = Trajectory(data={ 'time': [0.], 'foo': [1.] }, model=Model('test_model')) import pickle trajectory_unpickled = pickle.loads(pickle.dumps(trajectory)) import matplotlib with mock.patch('matplotlib.pyplot.plot') as mock_method_before_pickle: _plot_iterate(trajectory) with mock.patch('matplotlib.pyplot.plot') as mock_method_after_pickle: _plot_iterate(trajectory_unpickled) assert mock_method_before_pickle.call_args_list == mock_method_after_pickle.call_args_list
def test_plotly_layout_args(self): from unittest import mock trajectory = Trajectory(data={ 'time': [0.], 'foo': [1.] }, model=Model('test_model')) results = Results(data=[trajectory]) with mock.patch('plotly.offline.init_notebook_mode') as mock_notebook: with mock.patch('plotly.graph_objs.Scatter') as mock_scatter: with mock.patch('plotly.graph_objs.Layout') as mock_layout: results.plotplotly(return_plotly_figure=True, xscale='log') mock_layout.assert_called_with(showlegend=True, title='', xaxis_title='Time ', xscale='log', yaxis_title='Species Population')
def test_plotly_std_dev_range_layout_args(self): from unittest import mock from unittest.mock import MagicMock trajectory = Trajectory(data={ 'time': [0.], 'foo': [1.] }, model=Model('test_model')) results = Results(data=[trajectory]) _plotly_iterate = MagicMock() with mock.patch('plotly.offline.init_notebook_mode') as mock_notebook: with mock.patch('plotly.graph_objs.Scatter') as mock_scatter: with mock.patch('plotly.graph_objs.Layout') as mock_layout: results.plotplotly_std_dev_range(return_plotly_figure=True, xscale='log') mock_layout.assert_called_with(legend={'traceorder': 'normal'}, showlegend=True, title='Standard Deviation Range', xaxis_title='Time ', xscale='log', yaxis_title='Species Population')
def run(self, solver=None, timeout=0, t=None, show_labels=True, cpp_support=False, **solver_args): """ Function calling simulation of the model. There are a number of parameters to be set here. :param solver: The solver by which to simulate the model. This solver object may be initialized separately to specify an algorithm. Optional, defaults to ssa solver. :type solver: gillespy.GillesPySolver :param timeout: Allows a time_out value in seconds to be sent to a signal handler, restricting simulation run-time :type timeout: int :param t: End time of simulation :type t: int :param solver_args: Solver-specific arguments to be passed to solver.run() :return If show_labels is False, returns a numpy array of arrays of species population data. If show_labels is True,returns a Results object that inherits UserList and contains one or more Trajectory objects that inherit UserDict. Results object supports graphing and csv export. To pause a simulation and retrieve data before the simulation, keyboard interrupt the simulation by pressing control+c or pressing stop on a jupyter notebook. To resume a simulation, pass your previously ran results into the run method, and set t = to the time you wish the resuming simulation to end (run(resume=results, t=x)). Pause/Resume is only supported for SINGLE TRAJECTORY simulations. T MUST BE SET OR UNEXPECTED BEHAVIOR MAY OCCUR. """ if not show_labels: from gillespy2.core import log log.warning('show_labels = False is deprecated. Future releases ' 'of GillesPy2 may not support this feature.') if t is None: t = self.tspan[-1] if solver is None: solver = self.get_best_solver() try: solver_results, rc = solver.run(model=self, t=t, increment=self.tspan[-1] - self.tspan[-2], timeout=timeout, **solver_args) except Exception as e: if cpp_support is False: if not isinstance(solver, str): if solver.name == 'SSACSolver' or solver.name == 'VariableSSACSolver': from gillespy2.core import log log.warning( "Please install/configure 'g++' and 'make' on your" " system, to ensure that GillesPy2 C solvers will" " run properly.") raise SimulationError( "argument 'solver={}' to run() failed. Reason Given: {}". format(solver, e)) if rc == 33: from gillespy2.core import log log.warning('GillesPy2 simulation exceeded timeout.') if hasattr(solver_results[0], 'shape'): return solver_results if len(solver_results) > 0: results_list = [] for i in range(0, len(solver_results)): temp = Trajectory(data=solver_results[i], model=self, solver_name=solver.name, rc=rc) results_list.append(temp) results = Results(results_list) if show_labels == False: results = results.to_array() return results if len(solver_results) > 0: results_list = [] for i in range(0, len(solver_results)): temp = Trajectory(data=solver_results[i], model=self, solver_name=solver.name, rc=rc) results_list.append(temp) results = Results(results_list) if show_labels == False: results = results.to_array() return results else: raise ValueError( "number_of_trajectories must be non-negative and non-zero")