def testBootstrapAccuracy(self): if IGNORE_TEST: return model = """ J1: S1 -> S2; k1*S1 J2: S2 -> S3; k2*S2 S1 = 1; S2 = 0; S3 = 0; k1 = 0; k2 = 0; """ columns = ["S1", "S3"] fitter = ModelFitter(model, BENCHMARK_PATH, ["k1", "k2"], selectedColumns=columns, isPlot=IS_PLOT) fitter.fitModel() print(fitter.reportFit()) print(fitter.getParameterMeans()) fitter.bootstrap(numIteration=1000, reportInterval=500) #calcObservedFunc=ModelFitter.calcObservedTSNormal, std=0.01) fitter.plotParameterEstimatePairs(['k1', 'k2'], markersize=2) print("Mean: %s" % str(fitter.getParameterMeans())) print("Std: %s" % str(fitter.getParameterStds())) fitter.reportBootstrap()
def testWolfBug(self): if IGNORE_TEST: return trueParameterDct = { "J1_n": 4, "J4_kp": 76411, "J5_k": 80, "J6_k": 9.7, "J9_k": 28, } parametersToFit = [ SBstoat.Parameter("J1_n", lower=1, value=1, upper=8), # 4 SBstoat.Parameter("J4_kp", lower=3600, value=36000, upper=150000), #76411 SBstoat.Parameter("J5_k", lower=10, value=10, upper=160), # 80 SBstoat.Parameter("J6_k", lower=1, value=1, upper=10), # 9.7 SBstoat.Parameter("J9_k", lower=1, value=50, upper=50), # 28 ] ts = NamedTimeseries(csvPath=WOLF_DATA) methods = [] for optName in ["differential_evolution", "leastsq"]: methods.append(SBstoat.OptimizerMethod(optName, {cn.MAX_NFEV: 10})) fitter = ModelFitter(WOLF_MODEL, ts, parametersToFit=parametersToFit, fitterMethods=methods) fitter.fitModel() for name in [p.name for p in parametersToFit]: expected = trueParameterDct[name] actual = fitter.params.valuesdict()[name] self.assertLess(np.abs(np.log10(expected) - np.log10(actual)), 1.5) self.assertTrue(name in fitter.reportFit())
def evaluate(self, stdResiduals:float=0.1, relError:float=0.1, endTime:float=10.0, numPoint:int=30, fractionParameterDeviation:float=0.5, numIteration=NUM_BOOTSTRAP_ITERATION): """ Evaluates model fitting accuracy and bootstrapping for model Parameters ---------- stdResiduals: Standard deviations of variable used in constructing reiduals relError: relative error of parameter used in evaluation endTime: ending time of the simulation numPoint: number of points in the simulatin fractionParameterDeviation: fractional amount that the parameter can vary """ # Construct synthetic observations if self.selectedColumns is None: data = self.roadRunner.simulate(0, endTime, numPoint) else: allColumns = list(self.selectedColumns) if not TIME in allColumns: allColumns.append(TIME) data = self.roadRunner.simulate(0, endTime, numPoint, allColumns) bracketTime = "[%s]" % TIME if bracketTime in data.colnames: # Exclude any column named '[time]' idx = data.colnames.index(bracketTime) dataArr = np.delete(data, idx, axis=1) colnames = list(data.colnames) colnames.remove(bracketTime) colnames = [s[1:-1] if s != TIME else s for s in colnames] simTS = NamedTimeseries(array=dataArr, colnames=colnames) else: simTS = NamedTimeseries(namedArray=data) synthesizer = ObservationSynthesizerRandomErrors( fittedTS=simTS, std=stdResiduals) observedTS = synthesizer.calculate() # Construct the parameter ranges parameterDct = {} for name in self.parameterValueDct.keys(): lower = self.parameterValueDct[name]*(1 - fractionParameterDeviation) upper = self.parameterValueDct[name]*(1 + fractionParameterDeviation) value = np.random.uniform(lower, upper) parameterDct[name] = (lower, upper, value) # Create the fitter fitter = ModelFitter(self.roadRunner, observedTS, selectedColumns=self.selectedColumns, parameterDct=parameterDct, **self.kwargs) msg = "Fitting the parameters %s" % str(self.parameterValueDct.keys()) self.logger.result(msg) # Evaluate the fit fitter.fitModel() self._recordResult(fitter.params, relError, self.fitModelResult) # Evaluate bootstrap fitter.bootstrap(numIteration=numIteration) if fitter.bootstrapResult is not None: if fitter.bootstrapResult.numSimulation > 0: self._recordResult(fitter.bootstrapResult.params, relError, self.bootstrapResult)
def testFitSuite(self): if IGNORE_TEST: return self._init(numModel=1) fitter = ModelFitter(self.modelSpecifications[0], self.datasets[0], parametersToFit=self.parameterNamesCollection[0]) fitter.fitModel() self.fitter.fitSuite() valuesDct1 = fitter.params.valuesdict() valuesDct2 = self.fitter.params.valuesdict() for name, value in valuesDct1.items(): self.assertLess(np.abs(valuesDct2[name] - value), 0.5)
def testWolfBug(self): if IGNORE_TEST: return fullDct = { #"J1_n": (1, 1, 8), # 4 #"J4_kp": (3600, 36000, 150000), #76411 #"J5_k": (10, 10, 160), # 80 #"J6_k": (1, 1, 10), # 9.7 "J9_k": (1, 50, 50), # 28 } for parameter in fullDct.keys(): logger = Logger(logLevel=LEVEL_MAX) logger = Logger() ts = NamedTimeseries(csvPath=WOLF_DATA) parameterDct = {parameter: fullDct[parameter]} fitter = ModelFitter(WOLF_MODEL, ts[0:100], parameterDct=parameterDct, logger=logger, fitterMethods=[ "differential_evolution", "leastsq"]) fitter.fitModel() self.assertTrue("J9_k" in fitter.reportFit())
def main(numIteration): """ Calculates the time to run iterations of the benchmark. Parameters ---------- numIteration: int Returns ------- float: time in seconds """ logger = logs.Logger(logLevel=logs.LEVEL_MAX, logPerformance=IS_TEST) fitter = ModelFitter(MODEL, BENCHMARK_PATH, ["k1", "k2"], selectedColumns=['S1', 'S3'], isPlot=IS_PLOT, logger=logger) fitter.fitModel() startTime = time.time() fitter.bootstrap(numIteration=numIteration, reportInterval=numIteration) elapsedTime = time.time() - startTime if IS_TEST: print(fitter.logger.formatPerformanceDF()) fitter.plotFitAll() return elapsedTime
SproutyFunc: -> Spry2; HillTime(V_0, K_0, n_0, t) // Species IVs Spry2 = 0; // Parameter values V_0 = 19.9059673; K_0 = 10153.3568; n_0 = 2.52290790; t := time end ''') # sim = model.simulate(0, 7200, 7201) # model.plot() # quit() fitter = ModelFitter(model, "spry2_2a.txt", ["V_0", "K_0", "n_0"], fitterMethods='differential_evolution', parameterDct={ "V_0": (10, 20, 40), "K_0": (1800, 6000, 20000), "n_0": (1, 2, 12) }) fitter.fitModel() print(fitter.reportFit())
def _makeMikeModel(self, **kwargs): """Makes a model from Mike's data.""" model = te.loada(''' function Fi(v, ri, kf, kr, i, s, p, Kmi, Kms, Kmp, wi, ms, mp) ((ri+(1-ri)*(1/(1+i/Kmi)))^wi)*(kf*(s/Kms)^ms-kr*(p/Kmp)^mp)/((1+(s/Kms))^ms+(1+(p/Kmp))^mp-1) end function F0(v, kf, kr, s, p, Kms, Kmp, ms, mp) (kf*(s/Kms)^ms-kr*(p/Kmp)^mp)/((1+(s/Kms))^ms+(1+(p/Kmp))^mp-1) end function Fa(v, ra, kf, kr, a, s, p, Kma, Kms, Kmp, wa, ms, mp) ((ra+(1-ra)*((a/Kma)/(1+a/Kma)))^wa)*(kf*(s/Kms)^ms-kr*(p/Kmp)^mp)/((1+(s/Kms))^ms+(1+(p/Kmp))^mp-1) end function Fiii(v, ri1, ri2, ri3, kf, kr, i1, i2, i3, s, p, Kmi1, Kmi2, Kmi3, Kms, Kmp, wi1, wi2, wi3, ms, mp) ((ri1+(1-ri1)*(1/(1+i1/Kmi1)))^wi1) * ((ri2+(1-ri2)*(1/(1+i2/Kmi2)))^wi2) * ((ri3+(1-ri3)*(1/(1+i3/Kmi3)))^wi3) * (kf*(s/Kms)^ms-kr*(p/Kmp)^mp)/((1+(s/Kms))^ms+(1+(p/Kmp))^mp-1) end model modular_EGFR_current_128() // Reactions FreeLigand: -> L; Fa(v_0, ra_0, kf_0, kr_0, Lp, E, L, Kma_0, Kms_0, Kmp_0, wa_0, ms_0, mp_0); Phosphotyrosine: -> P; Fi(v_1, ri_1, kf_1, kr_1, Mig6, L, P, Kmi_1, Kms_1, Kmp_1, wi_1, ms_1, mp_1); Ras: -> R; Fiii(v_2, ri1_2, ri2_2, ri3_2, kf_2, kr_2, Spry2, P, E, P, R, Kmi1_2, Kmi2_2, Kmi3_2, Kms_2, Kmp_2, wi1_2, wi2_2, wi3_2, ms_2, mp_2); Erk: -> E; F0(v_3, kf_3, kr_3, R, E, Kms_3, Kmp_3, ms_3, mp_3); // Species IVs Lp = 100; E = 0; L = 1000; Mig6 = 100; P = 0; Spry2 = 10000; R = 0; // Parameter values v_0 = 1; ra_0 = 1; kf_0 = 1; kr_0 = 1; Kma_0 = 1; Kms_0 = 1; Kmp_0 = 1; wa_0 = 1; ms_0 = 1; mp_0 = 1; v_1 = 1; ri_1 = 1; kf_1 = 1; kr_1 = 1; Kmi_1 = 1; Kms_1 = 1; Kmp_1 = 1; wi_1 = 1; ms_1 = 1; mp_1 = 1; v_2 = 1; ri1_2 = 1; ri2_2 = 1; ri3_2 = 1; kf_2 = 1; kr_2 = 1; Kmi1_2 = 1; Kmi2_2 = 1; Kmi3_2 = 1; Kms_2 = 1; Kmp_2 = 1; wi1_2 = 1; wi2_2 = 1; wi3_2 = 1; ms_2 = 1; mp_2 = 1; v_3 = 1; kf_3 = 1; kr_3 = 1; Kms_3 = 1; Kmp_3 = 1; ms_3 = 1; mp_3 = 1; end ''') observedPath = os.path.join(DIR, "mike_bug.csv") fitter = ModelFitter(model, observedPath, [ #"v_0", "ra_0", "kf_0", "kr_0", "Kma_0", "Kms_0", "Kmp_0", "wa_0", "ms_0", #"mp_0", "v_1", "ri_1", "kf_1", "kr_1", "Kmi_1", "Kms_1", "Kmp_1", "wi_1", #"ms_1", "mp_1", "v_2", "ri1_2", "ri2_2", "ri3_2", "kf_2", "kr_2", "Kmi1_2", "Kmi2_2", "Kmi3_2", "Kms_2", "Kmp_2", "wi1_2", "wi2_2", "wi3_2", "ms_2", "mp_2", "v_3", "kf_3", "kr_3", "Kms_3", "Kmp_3", "ms_3", "mp_3"], **kwargs) fitter.fitModel() return fitter