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')
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 plotQuantity(self, quantity_id, param_values, exp_number): ''' Plot a simulated quantity vs its data points using Tellurium.''' self.setExperimentNumber(exp_number) iq = self.measured_quantity_ids.index(quantity_id) reference_data = array(self.reference_values[:, iq]) # self.r.reset() # self.r.resetAll() # # r.resetToOrigin() # self.setParameterVector(param_values, exponential=False) # sim = self.r.simulate(0., 30., 16, ['time', quantity_id]) # # print(self.r.getReactionRates()) # assert sim.shape[0] == reference_data.shape[0] # residuals = sim[:,1] - reference_data self.r.reset() self.r.resetAll() # r.resetToOrigin() self.setParameterVector(param_values, exponential=False) s = self.r.simulate(0, 25., 100, ['time', quantity_id]) import tellurium as te # te.plot(sim[:,0], reference_data, scatter=True, # name=quantity_id+' data', show=False, # title=quantity_id, # error_y_pos=maximum(residuals,0), # error_y_neg=-minimum(residuals,0)) print(quantity_id + ' sim') te.plot(s[:, 0], s[:, 1], name=quantity_id + ' sim')
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')
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 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')
r = te.loadAntimonyModel(antimonyString); r.integrator = 'gillespie';/ r.integrator.seed = 1234; r.integrator.variable_step_size = False; # selections specifies the output variables in a simulation selections = ['time'] + r.getBoundarySpeciesIds() + r.getFloatingSpeciesIds() numberOfOutputs = len(selections) timesToSimulate = 10; pointsToSimulate = 101; sumOfSimulations = np.zeros(shape = [pointsToSimulate, numberOfOutputs]) for k in range(timesToSimulate): r.resetToOrigin() currentSim = r.simulate(0, 24, pointsToSimulate, selections) # we record the sum of the simulations to calculate the average later sumOfSimulations += currentSim # since show = false, each trace is added to the current plot instead of starting a new one # this is analogous to MATLAB hold on. Alpha causes the opacity to be 50%, so the new lines won't crowd each other out #r.plot(currentSim, alpha = 0.5, show = False) # adds the mean curve for each specified selection. Adds legend, titles, labels to the plot. fig = te.plot(currentSim[:, 0], sumOfSimulations[:,1:]/timesToSimulate, names = [x + ' (mean)' for x in selections[1:]], title = "Stochastic simulation", xtitle = "time", ytitle = "copies" ) r.simulate(0, 48, 1000, ["time", "Rep", "GeneOff", "GeneOn", "Mol"])
from ast import literal_eval client = InfluxDBClient('luna') tstart = arrow.get('2018-12-17 05:24:10') results = client.query( 'SELECT island_id,best_f FROM champion', database= 'com.how2cell.sabaody.biopredyn.b4-driver.2c8d8bda-aef3-4e45-af0d-25b98a7ff686' ) # pprint(results) for result in results: for point in result: t = arrow.get(point['time']) if tstart == None or t < tstart: tstart = t timepoints_by_island = {} for result in results: for point in result: t = arrow.get(point['time']) - tstart island_id = point['island_id'] best_f = literal_eval(point['best_f'])[0] timepoints_by_island.setdefault(island_id, []).append( (t.seconds, best_f)) # print(t.seconds,island_id,best_f) import tellurium as te for island_id, series in timepoints_by_island.items(): trace = array(sorted(series, key=lambda t: t[0])) te.plot(trace[:, 0], trace[:, 1], tag=island_id, show=False) te.show()
s = r.simulate(0, 40) results.append(s) r.plot(s, show=False, loc=None, color='black', alpha=0.7) te.show() # #### Combining Simulations # # You can combine two timecourse simulations and change e.g. parameter values in between each simulation. The `gillespie` method simulates up to the given end time `10`, after which you can make arbitrary changes to the model, then simulate again. # # When using the `te.plot` function, you can pass the parameter `names`, which controls the names that will be used in the figure legend, and `tags`, which ensures that traces with the same tag will be drawn with the same color. # In[3]: import tellurium as te import numpy as np r = te.loada('S1 -> S2; k1*S1; k1 = 0.02; S1 = 100') r.setSeed(1234) for k in range(1, 20): r.resetToOrigin() res1 = r.gillespie(0, 10) # change in parameter after the first half of the simulation r.k1 = r.k1*20 res2 = r.gillespie (10, 20) sim = np.vstack([res1, res2]) te.plot(sim[:,0], sim[:,1:], alpha=0.7, names=['S1', 'S2'], tags=['S1', 'S2'], show=False) te.show()