Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for simulation initials, params as DataFrames #416

Merged
merged 3 commits into from Feb 20, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -330,6 +330,10 @@ def _process_incoming_initials(self, new_initials):
if new_initials is None:
return None

# If new_initials is a pandas dataframe, convert to a dict
if pd and isinstance(new_initials, pd.DataFrame):
new_initials = new_initials.to_dict(orient='list')

# Check if new_initials is a dict, and if so validate the keys
# (ComplexPatterns)
if isinstance(new_initials, dict):
@@ -356,6 +360,11 @@ def _process_incoming_initials(self, new_initials):
'values'.format(cplx_pat))
else:
if not isinstance(new_initials, np.ndarray):
if not isinstance(new_initials, list):

This comment has been minimized.

Copy link
@jmuhlich

jmuhlich Feb 19, 2019

Member

Could this nested-not bit be refactored? Maybe normalize list->ndarray up top where DataFrame is normalized to dict, then this whole else can become elif isinstance(new_initials, np.ndarray) and the ValueError can be moved to a final else at the bottom.

raise ValueError(
'Implicit conversion of data type "{}" is not '
'supported. Please supply initials as a numpy array '
'or a pandas DataFrame.'.format(type(new_initials)))
new_initials = np.array(new_initials, copy=False)
# if new_initials is a 1D array, convert to a 2D array of length 1
if len(new_initials.shape) == 1:
@@ -451,6 +460,11 @@ def param_values(self, new_params):
def _process_incoming_params(self, new_params):
if new_params is None:
return None

# Convert pandas dataframe to dictionary
if pd and isinstance(new_params, pd.DataFrame):
new_params = new_params.to_dict(orient='list')

if isinstance(new_params, dict):
n_sims = 1
if len(new_params) > 0:
@@ -469,6 +483,11 @@ def _process_incoming_params(self, new_params):
"must be equal length")
else:
if not isinstance(new_params, np.ndarray):
if not isinstance(new_params, list):

This comment has been minimized.

Copy link
@jmuhlich

jmuhlich Feb 19, 2019

Member

This would need the same treatment as _process_incoming_initials above.

raise ValueError(
'Implicit conversion of data type "{}" is not '
'supported. Please supply parameters as a numpy array '
'or a pandas DataFrame.'.format(type(new_params)))
new_params = np.array(new_params)
# if new_params is a 1D array, convert to a 2D array of length 1
if len(new_params.shape) == 1:
@@ -6,6 +6,7 @@
from pysb.simulator import ScipyOdeSimulator
from pysb.examples import robertson, earm_1_0
import unittest
import pandas as pd


class TestScipySimulatorBase(object):
@@ -112,6 +113,18 @@ def test_y0_as_dictionary_with_bound_species(self):
self.mon('B')(b=None): 0})
assert np.allclose(simres.observables['AB_complex'][0], 100)

def test_y0_as_dataframe(self):
initials_dict = {self.mon('A')(a=None): [0],
self.mon('B')(b=1) % self.mon('A')(a=1): [100],
self.mon('B')(b=None): [0]}
initials_df = pd.DataFrame(initials_dict)
simres = self.sim.run(initials=initials_df)
assert np.allclose(simres.observables['AB_complex'][0], 100)

@raises(ValueError)
def test_y0_as_pandas_series(self):
self.sim.run(initials=pd.Series())

@raises(TypeError)
def test_y0_non_numeric_value(self):
"""Test y0 with non-numeric value."""
@@ -123,6 +136,14 @@ def test_param_values_as_dictionary(self):
# kbindAB=0 should ensure no AB_complex is produced.
assert np.allclose(simres.observables["AB_complex"], 0)

def test_param_values_as_dataframe(self):
simres = self.sim.run(param_values=pd.DataFrame({'kbindAB': [0]}))
assert np.allclose(simres.observables['AB_complex'], 0)

@raises(ValueError)
def test_param_values_as_pandas_series(self):
self.sim.run(param_values=pd.Series())

def test_param_values_as_list_ndarray(self):
"""Test param_values as a list and ndarray."""
orig_param_values = self.sim.param_values
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.