Skip to content

Commit

Permalink
manually merge #190
Browse files Browse the repository at this point in the history
  • Loading branch information
0u812 committed Jul 17, 2017
1 parent c49caad commit 2335264
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tellurium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@
# Distributed computing
from .analysis.stochasticmodel import StochasticSimulationModel
from .analysis.parameterestimation import ParameterEstimation
from .analysis.sensitivityanalysis import SensitivityAnalysis

from .tellurium import (
# distrib. parameter scan
distributed_parameter_scanning, sample_plot, plotImage,
# distrib. stochastic sims & fitting
distributed_stochastic_simulation, plot_distributed_stochastic, plot_stochastic_result
distributed_stochastic_simulation, plot_distributed_stochastic, plot_stochastic_result, distributed_sensitivity_analysis
)

# Bifurcations
Expand Down
113 changes: 113 additions & 0 deletions tellurium/analysis/sensitivityanalysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
class SensitivityAnalysis(object):
def __init__(self,model=None,sbml=False,conservedMoietyAnalysis=True):
self._model = model
self._sbml = sbml
self._conservedMoietyAnalysis = conservedMoietyAnalysis
self._filename = None
self._simulation = None
self._bounds = None
self._args = None
self._allowLog = False


@property
def model(self):
return self._model

@model.setter
def model(self, value):
self._model = value

@model.deleter
def model(self):
del self._model

@property
def sbml(self):
return self._sbml

@sbml.setter
def sbml(self, value):
self._sbml = value

@sbml.deleter
def sbml(self):
del self._sbml

@property
def conservedMoietyAnalysis(self):
return self._conservedMoietyAnalysis

@conservedMoietyAnalysis.setter
def conservedMoietyAnalysis(self, value):
self._conservedMoietyAnalysis = value

@conservedMoietyAnalysis.deleter
def conservedMoietyAnalysis(self):
del self._conservedMoietyAnalysis


@property
def simulation(self):
return self._simulation

@simulation.setter
def simulation(self, value):
self._simulation = value

@simulation.deleter
def simulation(self):
del self._simulation


@property
def filename(self):
return self._filename

@filename.setter
def filename(self, value):
self._filename = value

@filename.deleter
def filename(self):
del self._filename


@property
def bounds(self):
return self._bounds

@bounds.setter
def bounds(self, value):
self._bounds = value

@bounds.deleter
def bounds(self):
del self._bounds

@property
def args(self):
return self._args

@args.setter
def args(self, value):
self._args = value

@args.deleter
def args(self):
del self._args

@property
def allowLog(self):
return self._allowLog

@allowLog.setter
def allowLog(self, value):
self._allowLog = value

@allowLog.deleter
def allowLog(self):
del self._allowLog



18 changes: 18 additions & 0 deletions tellurium/analysis/simulator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Simulator:
def __init__(self):
self.computed_vales = {}

def pre_simulation(self,model_roadrunner):
model_roadrunner.simulate(0,1000,5000)

def simulate_sensitivity(self, model_roadrunner):
self.push("r1b_k2",model_roadrunner.getCC('PP_K', 'r1b_k2'))
self.push("r8_a8",model_roadrunner.getCC('PP_K', 'r8a_a8'))
self.push("r10a_a10",model_roadrunner.getCC('PP_K', 'r10a_a10'))

def push(self, key, value):
self.computed_vales[key] = value

def get_computed_values(self):
return(self.computed_vales)

86 changes: 86 additions & 0 deletions tellurium/tellurium.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import sys
import warnings
import importlib

# NOTE: not needed since we now require matplotlib >= 2.0.0
# check availability of property cycler (matplotlib 1.5ish)
Expand Down Expand Up @@ -354,6 +355,89 @@ def spark_work(model_with_parameters):

return(sc.parallelize(list_of_models,len(list_of_models)).map(spark_work).collect())

def distributed_sensitivity_analysis(sc,senitivity_analysis_model):
def spark_sensitivity_analysis(model_with_parameters):
import tellurium as te

sa_model = model_with_parameters[0]

parameters = model_with_parameters[1]
class_name = importlib.import_module(sa_model.filename)

user_defined_simulator = getattr(class_name, dir(class_name)[0])
sa_model.simulation = user_defined_simulator()

if(sa_model.sbml):
model_roadrunner = te.loadAntimonyModel(te.sbmlToAntimony(sa_model.model))
else:
model_roadrunner = te.loadAntimonyModel(sa_model.model)

model_roadrunner.conservedMoietyAnalysis = sa_model.conservedMoietyAnalysis


#Running PreSimulation
model_roadrunner = sa_model.simulation.presimulator(model_roadrunner)

#Running Analysis
computations = {}
model_roadrunner = sa_model.simulation.simulator(model_roadrunner,computations)

_analysis = [None,None]

#Setting the Parameter Variables
_analysis[0] = {}
for i_param,param_names in enumerate(sa_model.bounds.keys()):

_analysis[0][param_names] = parameters[i_param]
setattr(model_roadrunner, param_names, parameters[i_param])

_analysis[1] = computations

return _analysis


if(senitivity_analysis_model.bounds is None):
print("Bounds are Undefined.")
return

#Get the Filename
sc.addPyFile(senitivity_analysis_model.filename)
senitivity_analysis_model.filename = os.path.basename(senitivity_analysis_model.filename).split(".")[0]


params = []
if(senitivity_analysis_model.allowLog):
for bound_values in senitivity_analysis_model.bounds.values():
if(len(bound_values) > 2):
params.append(np.logspace(bound_values[0], bound_values[1], bound_values[2]))
elif(len(bound_values == 2)):
params.append(np.logspace(bound_values[0], bound_values[1], 3))
else:
print("Improper Boundaries Defined")
return;
else:
for bound_values in senitivity_analysis_model.bounds.values():
if(len(bound_values) > 2):
params.append(np.linspace(bound_values[0], bound_values[1], bound_values[2]))
elif(len(bound_values == 2)):
params.append(np.linspace(bound_values[0], bound_values[1], 3))
else:
print("Improper Boundaries Defined")
return;

samples = perform_sampling(np.meshgrid(*params))
samples = zip([senitivity_analysis_model]*len(samples),samples)
return(sc.parallelize(samples,len(samples)).map(spark_sensitivity_analysis).collect())


def perform_sampling(mesh):
samples = []
mesh = [items.flatten() for items in mesh]
for i in range(len(mesh[0])):
samples.append([items[i] for items in mesh])
return(samples)


def loada(ant):
"""Load model from Antimony string.
Expand Down Expand Up @@ -738,3 +822,5 @@ def RoadRunner(*args):
return ExtendedRoadRunner(*args)

roadrunner.RoadRunner = ExtendedRoadRunner


0 comments on commit 2335264

Please sign in to comment.