Example #1
0
    def __init__(self, sbml, data_quantities, measurement_map):
        '''
        Constructor.

        :param measurement_map: A dictionary that maps the names of quantities to measurements to their respective (numpy) arrays.
        '''
        self.sbml = sbml
        self.r = RoadRunner(sbml)
        # self.r.integrator.stiff = False
        # self.r.integrator.minimum_time_step = 0.0001
        # self.r.integrator.maximum_time_step = 1.
        # self.residuals = []
        #print(self.r.getFloatingSpeciesIds())

        self.timepoints = unique(hstack(a[:, 0] for a in data_quantities))
        self.reset()

        self.measurement_map = measurement_map
        # map a quantity to its mean measured value
        self.mean_measurement_map = {
            quantity: mean(values[:, 1])
            for quantity, values in self.measurement_map.items()
        }

        # keep track of the number of times a measurement is used
        # (check correct number of residuals)
        self.measurement_count = OrderedDict(
            (quantity, 0) for quantity in self.measurement_map)
        self.penalty_scale = 1.
Example #2
0
    def __init__(self, sbml, measured_quantities, param_list,
                 reference_param_values, time_start, time_end, n):
        '''
        Constructor.

        :param measured_quantities: A list of the measured quantities.
        :param reference_param_values: The vector of parameter values in the reference state.
        :param time_start: Start time of the simulation.
        :param time_end: End time of the simulation.
        :param n: Number of intervals/points in simulation.
        '''
        self.sbml = sbml
        self.r = RoadRunner(sbml)
        self.time_start = time_start
        self.time_end = time_end
        self.n = n
        self.r.selections = ['time'] + measured_quantities
        self.measured_quantities = measured_quantities
        self.param_list = param_list
        self.setParameterVector(reference_param_values)

        sim = self.r.simulate(time_start, time_end, n)
        self.reference_time = array(sim[:, 0])
        self.reference_quantities = array(sim[:, 1:])
        self.reference_quantity_means_squared = mean(self.reference_quantities,
                                                     axis=0)**2
        # print(self.reference_quantity_means_squared)

        self.penalty_scale = 1.
Example #3
0
    def plotQuantity(self, quantity, param_values):
        ''' Plot a simulated quantity vs its data points using Tellurium.'''
        from data import name_to_id_map, conc_indices
        quantity_id = name_to_id_map[quantity]
        iq = self.measured_quantities.index(quantity_id)
        reference_data = array(self.scaled_data[:, iq])

        r = RoadRunner(self.sbml)
        self._setParameterVector(param_values, self.param_list, r)
        r.reset()
        t_now = 0.
        residuals = zeros((len(self.time_values), ))
        residuals[0] = r[quantity_id] - reference_data[0]

        for it_next in range(1, len(self.time_values)):
            self._setParameterVector(param_values, self.param_list, r)
            r.simulate(t_now, self.time_values[it_next], 100)
            t_now = self.time_values[it_next]
            residuals[it_next] = r[quantity_id] - reference_data[it_next]
        r.reset()
        s = r.simulate(0, float(self.time_values[-1]), 1000,
                       ['time', quantity_id])

        # print(quantity, sqrt(mean(residuals**2))/self.reference_value_means[iq])

        import tellurium as te
        te.plot(self.time_values,
                reference_data,
                scatter=True,
                name=quantity + ' data',
                show=False,
                error_y_pos=maximum(residuals, 0),
                error_y_neg=-minimum(residuals, 0))
        te.plot(s[:, 0], s[:, 1], name=quantity + ' sim')
Example #4
0
    def __init__(self, sbml):
        self.sbml = sbml
        self.r = RoadRunner(sbml)
        from data import measured_quantity_ids, exp_data
        self.measured_quantity_ids = measured_quantity_ids
        self.reference_values = exp_data
        self.reference_value_means_squared = mean(self.reference_values, axis=0)**2
        from params import new_param_ids as param_ids
        self.param_list = param_ids

        self.penalty_scale = 1.
Example #5
0
 def __init__(self,_path=''):
     RoadRunner.__init__(self)
     
     # self.sbmlFullPath=_sbmlFullPath
     self.path=_path # relative path to the SBML model - CC3D uses only relative paths . In some rare cases when users do some hacking they may set self.path to be absolute path. 
     self.absPath='' # absolute path of the SBML file. Internal use only e.g.  for debugging purposes - and is not serialized
     self.stepSize=1.0
     self.timeStart=0.0
     self.timeEnd=1.0
     
     self.__state={}
Example #6
0
    def __init__(self, _path=''):
        RoadRunner.__init__(self)

        # self.sbmlFullPath=_sbmlFullPath
        self.path = _path  # relative path to the SBML model - CC3D uses only relative paths . In some rare cases when users do some hacking they may set self.path to be absolute path.
        self.absPath = ''  # absolute path of the SBML file. Internal use only e.g.  for debugging purposes - and is not serialized
        self.stepSize = 1.0
        self.timeStart = 0.0
        self.timeEnd = 1.0

        self.__state = {}
Example #7
0
 def run(self, d, n, o, l, v):
     settings_file = join(d, n, '{n}-settings.txt'.format(n=n))
     settings = SettingsParser.from_file(settings_file)
     sbml_file = join(d, n, '{n}-sbml-l{l}v{v}.xml'.format(n=n, l=l, v=v))
     r = RoadRunner(sbml_file)
     r.selections = ['time'] + settings.amount + settings.concentration
     results = r.simulate(settings.start, settings.duration,
                          settings.steps + 1)
     from pandas import DataFrame
     print(r.selections)
     DataFrame(results,
               columns=r.selections).to_csv(join(o, '.'.join([n, 'csv'])),
                                            index=False,
                                            encoding='utf-8')
    def __init__(self, _path='', _modelString='', sbml=None):
        if sbml is None:
            RoadRunner.__init__(self)
        else:
            RoadRunner.__init__(self, sbml)

        # self.sbmlFullPath=_sbmlFullPath
        self.path = _path  # relative path to the SBML model - CC3D uses only relative paths . In some rare cases when users do some hacking they may set self.path to be absolute path.
        self.absPath = ''  # absolute path of the SBML file. Internal use only e.g.  for debugging purposes - and is not serialized
        self.modelString = ''  # SBML model string - used when translating model specifications
        self.stepSize = 1.0
        self.timeStart = 0.0
        self.timeEnd = 1.0

        self.__state = {}
Example #9
0
def set_integrator_settings(r: roadrunner.RoadRunner, **kwargs) -> None:
    """ Set integrator settings. """
    integrator = r.getIntegrator()
    for key, value in kwargs.items():
        # adapt the absolute_tolerance relative to the amounts
        if key == "absolute_tolerance":
            value = value * min(r.model.getCompartmentVolumes())
        integrator.setValue(key, value)
    return integrator
Example #10
0
def set_timecourse_selections(r: roadrunner.RoadRunner,
                              selections: List[str] = None) -> None:
    """ Sets the full model selections. """
    if not selections:
        r_model = r.model  # type: roadrunner.ExecutableModel

        r.timeCourseSelections = ["time"] \
                                 + r_model.getFloatingSpeciesIds() \
                                 + r_model.getBoundarySpeciesIds() \
                                 + r_model.getGlobalParameterIds() \
                                 + r_model.getReactionIds() \
                                 + r_model.getCompartmentIds()
        r.timeCourseSelections += [
            f'[{key}]' for key in (r_model.getFloatingSpeciesIds() +
                                   r_model.getBoundarySpeciesIds())
        ]
    else:
        r.timeCourseSelections = selections
 def plotQuantity(self, identifier, bars=True):
     ''' Plot a simulated quantity vs its data points using Tellurium.
     The residuals should already be calculated.'''
     data = self.measurement_map[identifier]
     # data contains one column of time and one column of values
     import tellurium as te
     te.plot(data[:, 0],
             data[:, 1],
             scatter=True,
             name=identifier + ' data',
             show=False,
             error_y_pos=maximum(array(self.quantity_residuals[identifier]),
                                 0),
             error_y_neg=-minimum(
                 array(self.quantity_residuals[identifier]), 0))
     # simulate and plot the model
     r = RoadRunner(self.sbml)
     s = r.simulate(0, self.timepoints[-1], 1000, ['time', identifier])
     te.plot(s[:, 0], s[:, 1], name=identifier + ' sim')
Example #12
0
    def __init__(self, sbml):
        self.sbml = sbml
        self.r = RoadRunner(sbml)
        # self.r.integrator = 'rk4'
        # print(self.r.integrator)
        # self.r.integrator.absolute_tolerance = 1e-10
        # self.r.integrator.relative_tolerance = 1e-4
        from data import measured_quantity_ids, exp_data, norm_data
        self.measured_quantity_ids = measured_quantity_ids
        self.reference_values = exp_data
        # self.reference_value_means_squared = mean(self.reference_values, axis=0)**2
        self.reference_norms = maximum(norm_data, 0.0005)
        # self.reference_norms_squared = maximum(norm_data, 0.0005)**2
        self.quantity_norms = array([
            0.0466906, 0.1666200, 0.1325631, 0.0757189, 0.1266561, 0.11634345,
            0.1143341, 0.1104889, 0.2808254, 0.1275417, 0.1163108, 0.26822369,
            0.1866278, 0.1555892, 0.4453806, 0.1598696, 0.1222940, 0.14366202,
            0.1259413, 0.1473395, 0.1407897, 0.1246730, 0.1153060, 0.12375571,
            0.1814630, 0.2199502, 0.1608888, 0.1903940, 0.1242392, 0.11704526,
            0.1287076, 0.1319431, 0.1324937, 0.0690382, 0.1316586, 2.15109423,
            0.1215476, 0.1414125, 0.1112873, 0.1297432, 0.1288462, 0.1155096,
            0.1203922, 0.18807463
        ])**2
        # UDP-D-glucose excluded because it has buggy errors
        self.overall_norm = float(
            mean(
                array([
                    0.0466906, 0.1666200, 0.1325631, 0.0757189, 0.1266561,
                    0.11634345, 0.1143341, 0.1104889, 0.2808254, 0.1275417,
                    0.1163108, 0.26822369, 0.1866278, 0.1555892, 0.4453806,
                    0.1598696, 0.1222940, 0.14366202, 0.1259413, 0.1473395,
                    0.1407897, 0.1246730, 0.1153060, 0.12375571, 0.1814630,
                    0.2199502, 0.1608888, 0.1903940, 0.1242392, 0.11704526,
                    0.1287076, 0.1319431, 0.1324937, 0.0690382, 0.1316586,
                    0.1215476, 0.1414125, 0.1112873, 0.1297432, 0.1288462,
                    0.1155096, 0.1203922, 0.18807463
                ])))
        from params import param_ids
        self.param_list = param_ids

        self.penalty_scale = 1.
Example #13
0
    def plotQuantity(self, identifier, param_values, n, bars=True):
        ''' Plot a simulated quantity vs its data points using Tellurium.'''
        i = self.measured_quantities.index(identifier)
        reference_quantity = self.reference_quantities[:, i]

        r = RoadRunner(self.sbml)
        if param_values is not None:
            self._setParameterVector(param_values, self.param_list, r)
        s = r.simulate(0, float(self.reference_time[-1]), n,
                       ['time', identifier])
        simulated_quantity = s[:, 1]
        residuals = simulated_quantity - reference_quantity
        # relative residuals
        # print('avg relative deviation for ', identifier, ': {:.1f}'.format(mean(abs(residuals/reference_quantity))*100.), '%')
        # residuals normalized to overall mean
        print(
            'avg relative deviation for ', identifier, ': {:.1f}'.format(
                mean(abs(residuals)) / mean(abs(reference_quantity)) * 100.),
            '%')
        r.reset()
        s = r.simulate(0, float(self.reference_time[-1]), 1000,
                       ['time', identifier])

        import tellurium as te
        te.plot(self.reference_time,
                reference_quantity,
                scatter=True,
                name=identifier + ' data',
                show=False,
                error_y_pos=maximum(residuals, 0),
                error_y_neg=-minimum(residuals, 0))
        te.plot(s[:, 0], s[:, 1], name=identifier + ' sim')
    def __init__(self, sbml, data_quantities, measurement_map):
        '''
        Constructor.

        :param measurement_map: A dictionary that maps the names of quantities to measurements to their respective (numpy) arrays.
        '''
        self.sbml = sbml
        self.r = RoadRunner(sbml)
        self.residuals = []
        #print(self.r.getFloatingSpeciesIds())

        self.timepoints = unique(hstack(a[:, 0] for a in data_quantities))
        self.reset()

        self.measurement_map = measurement_map

        # keep track of the number of times a measurement is used
        # (check correct number of residuals)
        self.measurement_count = OrderedDict(
            (quantity, 0) for quantity in self.measurement_map)
        self.quantity_residuals = dict(
            (quantity, list()) for quantity in self.measurement_map)
Example #15
0
    def __init__(self, sbml):
        '''
        Constructor.

        :param measured_quantities: A list of the measured quantities.
        :param scaled_data: The scaled reference data.
        :param scaled_error: The scaled reference error.
        '''
        from params import param_ids
        from data import time_values, conc_ids, scaled_data, scaled_error
        self.sbml = sbml
        self.r = RoadRunner(sbml)
        self.r.selections = ['time'] + conc_ids
        self.time_values = time_values
        assert self.time_values[0] == 0.
        self.measured_quantities = conc_ids
        self.param_list = param_ids
        self.reference_values = scaled_data
        self.reference_value_means = mean(self.reference_values, axis=0)
        self.scaled_data = scaled_data
        self.scaled_error = scaled_error

        self.penalty_scale = 1.
Example #16
0
    def __init__(self, sbml):
        self.sbml = sbml
        self.r = RoadRunner(sbml)
        self.r.integrator.absolute_tolerance = 1e-15
        self.r.integrator.relative_tolerance = 1e-9
        self.r.integrator.stiff = True
        from observables import observables
        self.measured_quantity_ids = observables
        from json import load
        with open(join(dirname(realpath(__file__)), 'exp_data.json')) as f:
            self.reference_value_collection = [array(a) for a in load(f)]
            self.reference_value_means_squared_collection = []
            for a in self.reference_value_collection:
                self.reference_value_means_squared_collection.append(
                    mean(a, axis=0)**2)
        with open(join(dirname(realpath(__file__)), 'exp_y0.json')) as f:
            self.exp_y0_collection = [array(a) for a in load(f)]
        with open(join(dirname(realpath(__file__)), 'stimuli.json')) as f:
            self.stimuli_collection = [d for d in load(f)]
        from parameters import param_ids
        self.param_list = param_ids

        self.penalty_scale = 1.
Example #17
0
def _worker(src, seeds, results, sim_args, sim_kwargs):
    # create a rr obj

    print("starting worker processes: " + str(os.getpid()))

    n = 0

    r = RoadRunner(src)

    # remove and apply the selections
    # no sense in re-evaluating selection list each time.
    if 'sel' in sim_kwargs:
        r.selections = sim_kwargs['sel']
        del sim_kwargs['sel']

    if 'selections' in sim_kwargs:
        r.selections = sim_kwargs['selections']
        del sim_kwargs['selections']

    # might have forgot to reset sim,
    # default is to reset sim between each simulation.
    if not 'reset' in sim_kwargs:
        Logger.log(
            Logger.LOG_WARNING,
            "simulate args missing reset, defaulting to reset between simulations"
        )
        sim_kwargs['reset'] = True

    # read until None,
    # blocks if queue is empty
    for seed in iter(seeds.get, None):
        sim_kwargs['seed'] = seed
        result = r.simulate(*sim_args, **sim_kwargs)
        results.put(result)
        n += 1

    print("process {} finished with {} simulations".format(os.getpid(), n))
Example #18
0
def _worker(src, seeds, results, sim_args, sim_kwargs):
    # create a rr obj

    print("starting worker processes: " + str(os.getpid()))

    n = 0;

          
    r = RoadRunner(src);

    # remove and apply the selections 
    # no sense in re-evaluating selection list each time.
    if 'sel' in sim_kwargs:
        r.selections = sim_kwargs['sel']
        del sim_kwargs['sel']

    if 'selections' in sim_kwargs:
        r.selections = sim_kwargs['selections']
        del sim_kwargs['selections']


    # might have forgot to reset sim, 
    # default is to reset sim between each simulation.
    if not 'reset' in sim_kwargs:
        Logger.log(Logger.LOG_WARNING, 
                   "simulate args missing reset, defaulting to reset between simulations")
        sim_kwargs['reset'] = True

    # read until None, 
    # blocks if queue is empty
    for seed in iter(seeds.get, None):
        sim_kwargs['seed'] = seed
        result = r.simulate(*sim_args, **sim_kwargs)
        results.put(result)
        n += 1

    print("process {} finished with {} simulations".format(os.getpid(), n))
Example #19
0
    def plotQuantity(self, quantity_id, param_values):
        ''' Plot a simulated quantity vs its data points using Tellurium.'''
        from data import time_values, measured_quantity_ids, measured_quantity_id_to_name_map
        quantity_name = measured_quantity_id_to_name_map[quantity_id]
        iq = measured_quantity_ids.index(quantity_id)
        reference_data = array(self.reference_values[:, iq])

        r = RoadRunner(self.sbml)
        self._setParameterVector(param_values, self.param_list, r)
        r.reset()
        # r.resetAll()
        # r.resetToOrigin()
        # r.integrator = 'euler'
        # r.integrator = 'rk4'
        # r.integrator.subdivision_steps = 1000
        # r.integrator.absolute_tolerance = 1e-10
        # r.integrator.relative_tolerance = 1e-4
        # r.integrator.initial_time_step = 0.001
        # r.integrator.minimum_time_step = 0.0001
        # r.integrator.maximum_time_step = 0.1
        # print(r.integrator)
        sim = r.simulate(0., 119., 120, ['time', quantity_id])
        assert sim.shape[0] == reference_data.shape[0]
        residuals = sim[:, 1] - reference_data

        r.reset()
        # r.resetAll()
        # r.resetToOrigin()
        s = r.simulate(0, float(time_values[-1]), 121, ['time', quantity_id])

        # print('mse for {}: '.format(quantity_name), sqrt(mean((residuals**2)/(self.reference_norms[:,iq]**2))))

        import tellurium as te
        te.plot(time_values,
                reference_data,
                scatter=True,
                name=quantity_name + ' data',
                show=False,
                title=quantity_name,
                error_y_pos=maximum(residuals, 0),
                error_y_neg=-minimum(residuals, 0))
        te.plot(s[:, 0], s[:, 1], name=quantity_name + ' sim')
Example #20
0
def clamp_species(r: roadrunner.RoadRunner,
                  sids,
                  boundary_condition=True) -> roadrunner.RoadRunner:
    """ Clamp/Free specie(s) via setting boundaryCondition=True/False.

    This requires changing the SBML and ODE system.

    :param r: roadrunner.RoadRunner
    :param sids: sid or iterable of sids
    :param boundary_condition: boolean flag to clamp (True) or free (False) species
    :return: modified roadrunner.RoadRunner
    """
    # get model for current SBML state
    sbml_str = r.getCurrentSBML()
    doc = libsbml.readSBMLFromString(sbml_str)  # type: libsbml.SBMLDocument
    model = doc.getModel()  # type: libsbml.Model

    if isinstance(sids, str):
        sids = [sids]

    for sid in sids:
        # set boundary conditions
        sbase = model.getElementBySId(sid)  # type: libsbml.SBase
        if not sbase:
            logging.error("No element for SId in model: {}".format(sid))
            return None
        else:
            if sbase.getTypeCode() == libsbml.SBML_SPECIES:
                species = sbase  # type: libsbml.Species
                species.setBoundaryCondition(boundary_condition)
            else:
                logging.error(
                    "SId in clamp does not match species: {}".format(sbase))
                return None

    # create modified roadrunner instance
    sbmlmod_str = libsbml.writeSBMLToString(doc)
    rmod = load_model(sbmlmod_str)  # type: roadrunner.RoadRunner
    set_timecourse_selections(rmod, r.timeCourseSelections)

    return rmod
Example #21
0
def species_df(r: roadrunner.RoadRunner) -> pd.DataFrame:
    """
    Create FloatingSpecies DataFrame.
    :return: pandas DataFrame
    """
    r_model = r.model  # type: roadrunner.ExecutableModel
    sbml_str = r.getCurrentSBML()

    doc = libsbml.readSBMLFromString(sbml_str)  # type: libsbml.SBMLDocument
    model = doc.getModel()  # type: libsbml.Model

    sids = r_model.getFloatingSpeciesIds() + r_model.getBoundarySpeciesIds()
    species = [model.getSpecies(sid)
               for sid in sids]  # type: List[libsbml.Species]

    data = {
        'sid':
        sids,
        'concentration':
        np.concatenate([
            r_model.getFloatingSpeciesConcentrations(),
            r_model.getBoundarySpeciesConcentrations()
        ],
                       axis=0),
        'amount':
        np.concatenate([
            r.model.getFloatingSpeciesAmounts(),
            r.model.getBoundarySpeciesAmounts()
        ],
                       axis=0),
        'unit': [s.getUnits() for s in species],
        'constant': [s.getConstant() for s in species],
        'boundaryCondition': [s.getBoundaryCondition() for s in species],
        'name': [s.getName() for s in species],
    }

    return pd.DataFrame(data,
                        columns=[
                            'sid', 'concentration', 'amount', 'unit',
                            'constant', 'boundaryCondition', 'species', 'name'
                        ])
Example #22
0
def parameter_df(r: roadrunner.RoadRunner) -> pd.DataFrame:
    """
    Create GlobalParameter DataFrame.
    :return: pandas DataFrame
    """
    r_model = r.model  # type: roadrunner.ExecutableModel
    doc = libsbml.readSBMLFromString(
        r.getCurrentSBML())  # type: libsbml.SBMLDocument
    model = doc.getModel()  # type: libsbml.Model
    sids = r_model.getGlobalParameterIds()
    parameters = [model.getParameter(sid)
                  for sid in sids]  # type: List[libsbml.Parameter]
    data = {
        'sid': sids,
        'value': r_model.getGlobalParameterValues(),
        'unit': [p.units for p in parameters],
        'constant': [p.constant for p in parameters],
        'name': [p.name for p in parameters],
    }
    df = pd.DataFrame(data,
                      columns=['sid', 'value', 'unit', 'constant', 'name'])
    return df
Example #23
0
from roadrunner import RoadRunner, Logger

#Logger.setLevel(Logger.LOG_DEBUG) # too verbose
Logger.setLevel(Logger.LOG_WARNING)

# Load model

rr = RoadRunner('/tmp/tx.sbml')
#rr = RoadRunner('/tmp/tx-reduced.sbml')
#rr = RoadRunner('/tmp/antsbml.xml')

# Simulate model
results = rr.simulate(0, 10, 30, integrator='gillespie')

print(results)

# Write results to temp file
with open('/tmp/sim_res.csv', 'w') as f:
    f.write(str(results))

# Plot
rr.plot()
Example #24
0
class B4Problem(TimecourseSimBiopredyn):
    ''' Class that performs a timecourse simulation
    and calculates the residuals for b4.'''
    def __init__(self, sbml):
        '''
        Constructor.

        :param measured_quantities: A list of the measured quantities.
        :param scaled_data: The scaled reference data.
        :param scaled_error: The scaled reference error.
        '''
        from params import param_ids
        from data import time_values, conc_ids, scaled_data, scaled_error
        self.sbml = sbml
        self.r = RoadRunner(sbml)
        self.r.selections = ['time'] + conc_ids
        self.time_values = time_values
        assert self.time_values[0] == 0.
        self.measured_quantities = conc_ids
        self.param_list = param_ids
        self.reference_values = scaled_data
        self.reference_value_means = mean(self.reference_values, axis=0)
        self.scaled_data = scaled_data
        self.scaled_error = scaled_error

        self.penalty_scale = 1.

    def evaluate_original(self, x):
        # type: (array) -> SupportsFloat
        """
        Evaluate the objective function and return the result. Identical to original
        biopredynbench evaluation, i.e. divides by scaled error. Difficult to compare
        across models.
        """
        from interruptingcow import timeout
        self.reset()
        self.setParameterVector(x)
        self.r.reset()

        def worker():
            t_now = 0.
            scaled_residuals = zeros(
                (len(self.time_values), len(self.measured_quantities)))
            for it_next in range(1, len(self.time_values)):
                t_now = self.r.simulate(t_now, self.time_values[it_next], 100)
                t_now = self.time_values[it_next]
                if self.divergent():
                    return 1e9 * self.penalty_scale
                for iq, q in enumerate(self.measured_quantities):
                    scaled_residuals[
                        it_next - 1,
                        iq] = (self.r[q] - self.scaled_data[it_next - 1, iq]
                               ) / self.scaled_error[it_next - 1, iq]
            return sqrt(mean(scaled_residuals**2.))

        try:
            with timeout(10, StalledSimulation):
                return worker()
        except (RuntimeError, StalledSimulation):
            # if convergence fails, use a penalty score
            return 1e9 * self.penalty_scale

    def evaluate(self, x):
        # type: (array) -> SupportsFloat
        """
        Evaluate the objective function and return the result.
        """
        from interruptingcow import timeout
        self.reset()
        self.setParameterVector(x)
        self.r.reset()

        def worker():
            t_now = 0.
            scaled_residuals = zeros(
                (len(self.time_values), len(self.measured_quantities)))
            for it_next in range(0, len(self.time_values)):
                for iq, q in enumerate(self.measured_quantities):
                    scaled_residuals[it_next,
                                     iq] = (self.r[q] -
                                            self.scaled_data[it_next, iq]
                                            ) / self.reference_value_means[iq]
                if it_next + 1 < len(self.time_values):
                    self.r.simulate(t_now, self.time_values[it_next + 1], 100)
                    t_now = self.time_values[it_next + 1]
                if self.divergent():
                    return 1e9 * self.penalty_scale
            # print('normalized errors: ', sqrt(mean(scaled_residuals**2, axis=0)))
            return sqrt(mean(scaled_residuals**2.))

        try:
            with timeout(10, StalledSimulation):
                return worker()
        except (RuntimeError, StalledSimulation):
            # if convergence fails, use a penalty score
            return 1e9 * self.penalty_scale

    def plotQuantity(self, quantity, param_values):
        ''' Plot a simulated quantity vs its data points using Tellurium.'''
        from data import name_to_id_map, conc_indices
        quantity_id = name_to_id_map[quantity]
        iq = self.measured_quantities.index(quantity_id)
        reference_data = array(self.scaled_data[:, iq])

        r = RoadRunner(self.sbml)
        self._setParameterVector(param_values, self.param_list, r)
        r.reset()
        t_now = 0.
        residuals = zeros((len(self.time_values), ))
        residuals[0] = r[quantity_id] - reference_data[0]

        for it_next in range(1, len(self.time_values)):
            self._setParameterVector(param_values, self.param_list, r)
            r.simulate(t_now, self.time_values[it_next], 100)
            t_now = self.time_values[it_next]
            residuals[it_next] = r[quantity_id] - reference_data[it_next]
        r.reset()
        s = r.simulate(0, float(self.time_values[-1]), 1000,
                       ['time', quantity_id])

        # print(quantity, sqrt(mean(residuals**2))/self.reference_value_means[iq])

        import tellurium as te
        te.plot(self.time_values,
                reference_data,
                scatter=True,
                name=quantity + ' data',
                show=False,
                error_y_pos=maximum(residuals, 0),
                error_y_neg=-minimum(residuals, 0))
        te.plot(s[:, 0], s[:, 1], name=quantity + ' sim')
Example #25
0
class TimecourseSimValidate(TimecourseSimAligned):
    ''' Validates convergence to a given set of parameters.
    Generates datapoints on a grid for measured_quantities.'''
    def __init__(self, sbml, measured_quantities, param_list,
                 reference_param_values, time_start, time_end, n):
        '''
        Constructor.

        :param measured_quantities: A list of the measured quantities.
        :param reference_param_values: The vector of parameter values in the reference state.
        :param time_start: Start time of the simulation.
        :param time_end: End time of the simulation.
        :param n: Number of intervals/points in simulation.
        '''
        self.sbml = sbml
        self.r = RoadRunner(sbml)
        self.time_start = time_start
        self.time_end = time_end
        self.n = n
        self.r.selections = ['time'] + measured_quantities
        self.measured_quantities = measured_quantities
        self.param_list = param_list
        self.setParameterVector(reference_param_values)

        sim = self.r.simulate(time_start, time_end, n)
        self.reference_time = array(sim[:, 0])
        self.reference_quantities = array(sim[:, 1:])
        self.reference_quantity_means_squared = mean(self.reference_quantities,
                                                     axis=0)**2
        # print(self.reference_quantity_means_squared)

        self.penalty_scale = 1.

    def plotQuantity(self, identifier, param_values, n, bars=True):
        ''' Plot a simulated quantity vs its data points using Tellurium.'''
        i = self.measured_quantities.index(identifier)
        reference_quantity = self.reference_quantities[:, i]

        r = RoadRunner(self.sbml)
        if param_values is not None:
            self._setParameterVector(param_values, self.param_list, r)
        s = r.simulate(0, float(self.reference_time[-1]), n,
                       ['time', identifier])
        simulated_quantity = s[:, 1]
        residuals = simulated_quantity - reference_quantity
        # relative residuals
        # print('avg relative deviation for ', identifier, ': {:.1f}'.format(mean(abs(residuals/reference_quantity))*100.), '%')
        # residuals normalized to overall mean
        print(
            'avg relative deviation for ', identifier, ': {:.1f}'.format(
                mean(abs(residuals)) / mean(abs(reference_quantity)) * 100.),
            '%')
        r.reset()
        s = r.simulate(0, float(self.reference_time[-1]), 1000,
                       ['time', identifier])

        import tellurium as te
        te.plot(self.reference_time,
                reference_quantity,
                scatter=True,
                name=identifier + ' data',
                show=False,
                error_y_pos=maximum(residuals, 0),
                error_y_neg=-minimum(residuals, 0))
        te.plot(s[:, 0], s[:, 1], name=identifier + ' sim')
Example #26
0
def benchmark_loading_time():
    import arrow
    time_start = arrow.utcnow()
    r = RoadRunner('../../../../../sbml/b2.xml')
    delta_t = arrow.utcnow() - time_start
    print('Loading time:', delta_t)
class TimecourseModel(Evaluator):
    ''' Class that performs a timecourse simulation
    and calculates the residuals for b4.'''
    def __init__(self, sbml, data_quantities, measurement_map):
        '''
        Constructor.

        :param measurement_map: A dictionary that maps the names of quantities to measurements to their respective (numpy) arrays.
        '''
        self.sbml = sbml
        self.r = RoadRunner(sbml)
        self.residuals = []
        #print(self.r.getFloatingSpeciesIds())

        self.timepoints = unique(hstack(a[:, 0] for a in data_quantities))
        self.reset()

        self.measurement_map = measurement_map

        # keep track of the number of times a measurement is used
        # (check correct number of residuals)
        self.measurement_count = OrderedDict(
            (quantity, 0) for quantity in self.measurement_map)
        self.quantity_residuals = dict(
            (quantity, list()) for quantity in self.measurement_map)

    def calcResiduals(self, t):
        ''' Try to calculate residuals at the current time t
        and add them to self.residuals.
        If they do not exist for certain datasets at time t,
        just pass over the dataset.'''
        self.usage_map = dict((q, False) for q in self.measurement_map)
        for quantity in self.measurement_map.keys():
            self.tryAddResidual(t, self.r[quantity], quantity)

    def tryAddResidual(self, t, predicted_value, identifier):
        ''' Append a residual to the list of residuals.
            Call with a single value from a simulation and pass
            array of measurements for that quantity.
            If there is no measurement at this time point (t), do nothing.'''
        a = self.measurement_map[identifier]
        try:
            # if there is a measurement a this timepoint, append to list
            r = predicted_value - valueAtTime(a, t)
            self.residuals.append(r)
            self.quantity_residuals[identifier].append(r)
            # increment the residual use count (check all measurements are used exactly once)
            self.measurement_count[identifier] += 1
            self.usage_map[identifier] = True
        except MissingValue:
            # no measurement at this timepoint, do nothing
            return

    def plotQuantity(self, identifier, bars=True):
        ''' Plot a simulated quantity vs its data points using Tellurium.
        The residuals should already be calculated.'''
        data = self.measurement_map[identifier]
        # data contains one column of time and one column of values
        import tellurium as te
        te.plot(data[:, 0],
                data[:, 1],
                scatter=True,
                name=identifier + ' data',
                show=False,
                error_y_pos=maximum(array(self.quantity_residuals[identifier]),
                                    0),
                error_y_neg=-minimum(
                    array(self.quantity_residuals[identifier]), 0))
        # simulate and plot the model
        r = RoadRunner(self.sbml)
        s = r.simulate(0, self.timepoints[-1], 1000, ['time', identifier])
        te.plot(s[:, 0], s[:, 1], name=identifier + ' sim')

    def MSE(self):
        ''' Calc the MSE for all residuals.
        Call this after calculating all residuals.'''
        r = array(self.residuals)
        return (r**2).mean()

    def simulateToNextTime(self):
        t_begin = self.t
        t_end = self.timepoints[self.next_ti]
        delta = t_end - t_begin
        stepsize = 0.1
        steps = int(max(100, delta / stepsize))
        self.r.simulate(t_begin, t_end, steps)
        return t_end

    def reset(self):
        self.r.resetAll()
        self.t = self.timepoints[0]
        # next time index
        self.next_ti = 0

    def buildResidualList(self):
        # simulate to the first timepoint (not necessarily zero)
        delta = self.timepoints[0]
        stepsize = 0.1
        steps = int(max(100, delta / stepsize))
        self.r.simulate(0, delta, steps)
        self.next_ti = 1
        if len(self.timepoints) < 2:
            raise RuntimeError('Expected at least two timepoints')
        # calculate the residuals
        self.calcResiduals(self.t)
        # simulate to the rest of the timepoints
        while self.next_ti < self.timepoints.shape[0]:
            self.t = self.simulateToNextTime()
            self.calcResiduals(self.t)
            self.next_ti += 1

    def setParameterVector(self, x, param_list):
        # type: (array, List) -> None
        # TODO: sample in log space
        expect(
            len(x) == len(param_list),
            'Wrong length for parameter vector - expected {} but got {}'.
            format(len(param_list), len(x)))
        for i, v in enumerate(x):
            self.r[param_list[i]] = v

    def evaluate(self, x):
        # type: (array) -> SupportsFloat
        """
        Evaluate and return the objective function.
        """
        self.reset()
        self.setParameterVector(x)
        try:
            self.buildResidualList()
        except RuntimeError:
            # if convergence fails, use a penalty score
            return 1e9
        return self.MSE()

    def getUsageByQuantity(self):
        '''
        Calculates the number of times a given quantity is used.
        Should be equal to the number of datap oints for that quantity
        if all goes well.
        '''
        total = 0
        total_used = 0
        usage_for_quantity = OrderedDict()

        for q in self.measurement_count:
            a = self.measurement_map[q]
            used = self.measurement_count[q]
            usage_for_quantity[q] = used
            n = a.shape[0]
            total += n
            total_used += used

        return (total, total_used, usage_for_quantity)

    def printDatapointUsage(self):
        ''' For debugging. Make sure every data point is
        used.'''
        total, total_used, usage_for_quantity = self.getUsageByQuantity()
        for q, used in usage_for_quantity.keys():
            a = self.measurement_map[q]
            n = a.shape[0]
            print('Usage for {}: {}/{}'.format(q, used, n))
        print('*** Total usage: {}/{} ({:.1f}%)'.format(
            total_used, total, 100. * total_used / total))
Example #28
0
def simulate(r: roadrunner.RoadRunner):
    r.timeCourseSelections = ["time", "Aglc", "[Aglc]", "R1", "Vc"]
    s = r.simulate(start=0, end=2, steps=100)
    return pd.DataFrame(s, columns=s.colnames)
Example #29
0
import numpy as np
from roadrunner import RoadRunner
from timeit import default_timer

r = RoadRunner('WolfGlycolysis.xml')

#orders = 5
#for multiplier in np.logspace(0,orders,orders+1):
    ##print('multiplier = {}'.format(multiplier))
    #t1 = default_timer()
    #r.reset()
    #r.simulate(0,2*int(multiplier),1000*int(multiplier))
    #t2 = default_timer()
    #print('{} simulation, duration = {}'.format(2*int(multiplier), t2-t1))
    ##r.plot()

# COPASI 4:20
t1 = default_timer()
r.reset()
r.simulate(0,20000,200000)
t2 = default_timer()
Example #30
0
    def plotQuantity(self, quantity_id, param_values):
        ''' Plot a simulated quantity vs its data points using Tellurium.'''
        from data import measured_quantity_ids, measured_quantity_id_to_name_map
        from data import t1, t2, time_end, n1, n2, n3
        quantity_name = measured_quantity_id_to_name_map.get(quantity_id,quantity_id)
        iq = measured_quantity_ids.index(quantity_id)
        reference_data = array(self.reference_values[:,iq])

        r = RoadRunner(self.sbml)
        r.reset()
        r.resetAll()
        r.resetToOrigin()
        self._setParameterVector(param_values, self.param_list, r)
        # r.oneStep(0., 10)
        # print('plotQuantity OAA\' = {}'.format(r["OAA'"]))
        s1 = r.simulate(0., t1, n1, ['time', quantity_id])
        s2 = r.simulate(t1, t2, n2, ['time', quantity_id])
        # print(quantity_id)
        # if quantity_id == 'GLC':
            # print('BM before {}'.format(r['[BM]']))
            # print(iq)
            # print(reference_data)
        s3 = r.simulate(t2, time_end, n3, ['time', quantity_id])
        sim = vstack((
            s1,
            s2,
            s3,
            ))
        assert sim.shape[0] == reference_data.shape[0]
        residuals = sim[:,1] - reference_data

        r.reset()
        r.resetAll()
        r.resetToOrigin()
        self._setParameterVector(param_values, self.param_list, r)
        # self._setParameterVector(param_values, self.param_list, r)
        s = r.simulate(0,time_end,1000,['time',quantity_id])

        import tellurium as te
        te.plot(sim[:,0], reference_data, scatter=True,
            name=quantity_name+' data', show=False,
            title=quantity_name,
            error_y_pos=maximum(residuals,0),
            error_y_neg=-minimum(residuals,0))
        te.plot(s[:,0], s[:,1], name=quantity_name+' sim')
Example #31
0
from b3problem import B3Problem
from os.path import join, dirname, abspath, realpath
from numpy import allclose, isclose

problem = B3Problem(
    abspath(
        join(dirname(realpath(__file__)), '..', '..', '..', '..', '..', 'sbml',
             'b3.xml')))

from roadrunner import RoadRunner
orig = RoadRunner(
    abspath(
        join(dirname(realpath(__file__)), '..', '..', '..', '..', '..', 'sbml',
             'b3-original.xml')))
# print('Original OAA\' = {}'.format(orig["OAA'"]))
new = RoadRunner(
    abspath(
        join(dirname(realpath(__file__)), '..', '..', '..', '..', '..', 'sbml',
             'b3.xml')))

from data import GLC_ss_value_map
for q in GLC_ss_value_map.keys():
    if new[q] != GLC_ss_value_map[q]:
        print('discrepancy: {} = {} vs {}'.format(q, new[q],
                                                  GLC_ss_value_map[q]))

# for rxn_id in new.model.getReactionIds():
#     if orig[rxn_id] != new[rxn_id]:
#         print('discrepancy: {} = {} vs {}'.format(rxn_id, orig[rxn_id], new[rxn_id]))
#
# quantities = ['[Emp]','e_Emp_kcat_f','FBP','e_Emp_Kfbp','e_Emp_kcat_r','PG3','e_Emp_Kpg3']
Example #32
0
class B3Problem(TimecourseSimBiopredyn):
    ''' Class that performs a timecourse simulation
    and evaluates the objective function for b1.'''

    def __init__(self, sbml):
        self.sbml = sbml
        self.r = RoadRunner(sbml)
        from data import measured_quantity_ids, exp_data
        self.measured_quantity_ids = measured_quantity_ids
        self.reference_values = exp_data
        self.reference_value_means_squared = mean(self.reference_values, axis=0)**2
        from params import new_param_ids as param_ids
        self.param_list = param_ids

        self.penalty_scale = 1.


    def evaluate(self, x):
        # type: (array) -> SupportsFloat
        """
        Evaluate and return the objective function.
        """
        from interruptingcow import timeout
        # from data import time_end, n_points
        from data import t1, t2, time_end, n1, n2, n3
        self.reset()
        self.r.resetAll()
        self.r.resetToOrigin()
        self.setParameterVector(x)
        self.r.reset()
        def worker():
            # sim = array(self.r.simulate(0., time_end, n_points, self.measured_quantity_ids))

            s1 = self.r.simulate(0., t1, n1, self.measured_quantity_ids)
            s2 = self.r.simulate(t1, t2, n2, self.measured_quantity_ids)
            s3 = self.r.simulate(t2, time_end, n3, self.measured_quantity_ids)
            sim = vstack((
                s1,
                s2,
                s3,
                ))
            residuals = sim-self.reference_values
            normalized_mse_per_quantity = mean(residuals**2,axis=0)/self.reference_value_means_squared
            # from pprint import pprint
            # print('residuals:')
            # pprint({id: value for id,value in zip(self.measured_quantity_ids, sqrt(normalized_mse_per_quantity))})
            return sqrt(mean(normalized_mse_per_quantity))
        try:
            with timeout(10, StalledSimulation):
                return worker()
        except (RuntimeError, StalledSimulation):
            # if convergence fails, use a penalty score
            return 1e9*self.penalty_scale


    def plotQuantity(self, quantity_id, param_values):
        ''' Plot a simulated quantity vs its data points using Tellurium.'''
        from data import measured_quantity_ids, measured_quantity_id_to_name_map
        from data import t1, t2, time_end, n1, n2, n3
        quantity_name = measured_quantity_id_to_name_map.get(quantity_id,quantity_id)
        iq = measured_quantity_ids.index(quantity_id)
        reference_data = array(self.reference_values[:,iq])

        r = RoadRunner(self.sbml)
        r.reset()
        r.resetAll()
        r.resetToOrigin()
        self._setParameterVector(param_values, self.param_list, r)
        # r.oneStep(0., 10)
        # print('plotQuantity OAA\' = {}'.format(r["OAA'"]))
        s1 = r.simulate(0., t1, n1, ['time', quantity_id])
        s2 = r.simulate(t1, t2, n2, ['time', quantity_id])
        # print(quantity_id)
        # if quantity_id == 'GLC':
            # print('BM before {}'.format(r['[BM]']))
            # print(iq)
            # print(reference_data)
        s3 = r.simulate(t2, time_end, n3, ['time', quantity_id])
        sim = vstack((
            s1,
            s2,
            s3,
            ))
        assert sim.shape[0] == reference_data.shape[0]
        residuals = sim[:,1] - reference_data

        r.reset()
        r.resetAll()
        r.resetToOrigin()
        self._setParameterVector(param_values, self.param_list, r)
        # self._setParameterVector(param_values, self.param_list, r)
        s = r.simulate(0,time_end,1000,['time',quantity_id])

        import tellurium as te
        te.plot(sim[:,0], reference_data, scatter=True,
            name=quantity_name+' data', show=False,
            title=quantity_name,
            error_y_pos=maximum(residuals,0),
            error_y_neg=-minimum(residuals,0))
        te.plot(s[:,0], s[:,1], name=quantity_name+' sim')
        # print('deviation for {}: {}/{} = {}'.format(quantity_id, sqrt(mean(residuals**2)), sqrt(self.reference_value_means_squared[iq]),  sqrt(mean(residuals**2)/self.reference_value_means_squared[iq])))


    def getParameterValue(self,param_index):
        from params import param_index_to_name_map
        try:
            return self.r[param_index_to_name_map[param_index]]
        except KeyError:
            raise KeyError('No such parameter for index {}, must be 0-1758 inclusive'.format(param_index))