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 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 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
# time in days since volunteer exposure # each line in the array is an individual volunteer #SARS_CoV2_sputum.csv and SARS_CoV2_nasal.csv # SARS-CoV-2 data - 9 patients, # for each patient - viral loads from lungs (sputum) and from nasal cavity (swab) # viral levels in log10(RNA copies / ml sputum), ... # respectively log10(RNA copies / nasal swab) # time in days since symptoms onset # corresponding lines in the two arrays belong to an individual patient #SARS.csv # SARS data recorded from 12 patients; # included them just for comparison, probably too few datapoints for model inference # viral levels in log10(RNA copies / ml of nasopharingeal aspirate) # time - only three samples per patient, at 5, 10 and 15 days post symptoms onset # Fit parameters to ts1 from SBstoat.modelFitter import ModelFitter fitter = ModelFitter(ANTIMONY_MODEL, "Influenza-1.txt", ["beta", "kappa", "delta", "p", "c"]) fitter.fitModel() print(fitter.reportFit()) fitter.plotFitAll(numRow=2, numCol=2) fitter.plotResiduals(numRow=2, numCol=2) # Get estimates of parameters fitter.bootstrap(numIteration=2000, reportInterval=500) print(fitter.getFittedParameterStds())