예제 #1
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
예제 #2
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()