Пример #1
0
 def testFitBootstrap(self):
     if IGNORE_TEST:
         return
     study = ModelStudy(th.ANTIMONY_MODEL, DATA_PATHS,
           parametersToFit=PARAMETERS_TO_FIT,
           dirStudyPath=SERIALIZE_DIR, isPlot=IS_PLOT, useSerialized=False)
     study.bootstrap(numIteration=10)
     for fitter in study.fitterDct.values():
         self.assertIsNotNone(fitter.bootstrapResult)
Пример #2
0
 def runVirus(self, values):
     ANTIMONY_MODEL = '''
         // Equations
         E1: T -> E ; beta*T*V ; // Target cells to exposed
         E2: E -> I ; kappa*E ;  // Exposed cells to infected
         E3: -> V ; p*I ;        // Virus production by infected cells
         E4: V -> ; c*V ;        // Virus clearance
         E5: I -> ; delta*I      // Death of infected cells    
     
         // Parameters - from the Influenza article,
             
         beta = 3.2e-5;  // rate of transition of target(T) to exposed(E) cells, in units of 1/[V] * 1/day
         kappa = 4.0;    // rate of transition from exposed(E) to infected(I) cells, in units of 1/day
         delta = 5.2;    // rate of death of infected cells(I), in units of 1/day
         p = 4.6e-2;     // rate virus(V) producion by infected cells(I), in units of [V]/day
         c = 5.2;        // rate of virus clearance, in units of 1/day
     
         // Initial conditions
         T = 4E+8 // estimate of the total number of susceptible epithelial cells
                  // in upper respiratory tract)
         E = 0
         I = 0
         V = 0.75 // the dose of virus in TCID50 in Influenza experiment; could be V=0 and I = 20 instead for a natural infection
         
         // Computed values
         log10V := log10(V)
     
     '''
     dataSource = self.mkTimeSeries(values, "log10V")
     parameterDct = dict(
         beta=(0, 10e-5, 3.2e-5),
         kappa=(0, 10, 4.0),
         delta=(0, 10, 5.2),
         p=(0, 1, 4.6e-2),
         c=(0, 10, 5.2),
     )
     if IGNORE_TEST:
         logger = logs.Logger(logLevel=logs.LEVEL_MAX)
     else:
         logger = LOGGER
     study = ModelStudy(ANTIMONY_MODEL, [dataSource],
                        parameterDct=parameterDct,
                        selectedColumns=["log10V"],
                        doSerialize=False,
                        useSerialized=False,
                        logger=logger)
     study.bootstrap(numIteration=100)
     fitter = list(study.fitterDct.values())[0]
     if IS_PLOT and (fitter.bootstrapResult is not None):
         study.plotFitAll()
     fitter = study.fitterDct["src_1"]
     for name in parameterDct.keys():
         if fitter.bootstrapResult is not None:
             value = fitter.bootstrapResult.params.valuesdict()[name]
             self.assertIsNotNone(value)
Пример #3
0
def runStudy(model, dirStudyPath):
    study = ModelStudy(model,                     # Antimony model to evaluate
                   dataSourceDct,                 # Data sources to use for fitting
                   parameterDct=parameterDct,     # Parameters and their value ranges
                   dirStudyPath=dirStudyPath,     # Where to store the results of bootstrapping
                   selectedColumns=["log10V"],    # Output column is computed in the assignment rule
                   doSerialize=DO_SERIALIZE,      # Save the results of bootstrapping
                   useSerialized=USE_SERIALIZED)  # Use previously calculated bootstrap results if they are present

    study.bootstrap(numIteration=NUM_BOOTSTRAP_ITERATION)  # Do bootstrapping
    print("\n\n")
    study.plotFitAll(ylim=[0, 9])                          # Plot fitted and observed values with band plots for confidence
    print("\n\n")
    study.plotParameterEstimates()                         # Plot the parameter estimates for each data source
Пример #4
0
class TestModelStudy(unittest.TestCase):

    def setUp(self):
        self._remove()
        self.parametersToFit = list(th.PARAMETER_DCT.keys())
        self.study = ModelStudy(th.ANTIMONY_MODEL, DATA_PATHS,
              parametersToFit=PARAMETERS_TO_FIT,
              dirStudyPath=SERIALIZE_DIR, isPlot=IS_PLOT, useSerialized=True)
    
    def tearDown(self):
        self._remove()

    def _remove(self):
        for ffile in FILES:
            if os.path.isfile(ffile):
                os.remove(ffile)
        for ddir in DIRS:
            if os.path.isdir(ddir):
                shutil.rmtree(ddir)

    def testConstructor1(self):
        if IGNORE_TEST:
            return
        self.assertGreater(len(self.study.fitterDct.values()), 0)
        # Ensure that ModelFitters are serialized correctly
        study = ModelStudy(th.ANTIMONY_MODEL, DATA_PATHS,
              parametersToFit=self.parametersToFit,
              dirStudyPath=SERIALIZE_DIR, isPlot=IS_PLOT)
        for name in self.study.instanceNames:
            self.assertEqual(study.fitterDct[name].modelSpecification,
                  self.study.fitterDct[name].modelSpecification)

    def testFitModel(self):
        if IGNORE_TEST:
            return
        self.study.fitModel()
        names = [v for v in self.study.fitterDct.keys()]
        params0 = self.study.fitterDct[names[0]].params
        params1 = self.study.fitterDct[names[1]].params
        dct0 = params0.valuesdict()
        dct1 = params1.valuesdict()
        if IGNORE_ACCURACY:
            return
        for key, value in dct0.items():
            self.assertTrue(np.isclose(value, dct1[key], rtol=0.5)) 

    def testFitBootstrap(self):
        if IGNORE_TEST:
            return
        study = ModelStudy(th.ANTIMONY_MODEL, DATA_PATHS,
              parametersToFit=PARAMETERS_TO_FIT,
              dirStudyPath=SERIALIZE_DIR, isPlot=IS_PLOT, useSerialized=False)
        study.bootstrap(numIteration=10)
        for fitter in study.fitterDct.values():
            self.assertIsNotNone(fitter.bootstrapResult)

    def testPlotFitAll(self):
        if IGNORE_TEST:
            return
        self.study.fitModel()
        self.study.plotFitAll()
        #
        self.study.bootstrap()
        self.study.plotFitAll()

    def testPlotParameterEstimates(self):
        if IGNORE_TEST:
            return
        self.study.bootstrap(numIteration=20)
        self.study.plotParameterEstimates()