Пример #1
0
 def __init__(self, sbmlPath:str,
       parametersToFit:typing.List[str]=None,
       selectedColumns:typing.List[str]=None,
       **kwargs):
     """
     Parameters
     ----------
     sbmlPath: path or URL to SBML files
     selectedColumns: names of floating species used in fitting
     parametersToFit: names of parameters to fit
     kwargs: passed to ModelFitter constructor
     """
     if "logger" in kwargs.keys():
         self.logger = kwargs["logger"]
     else:
         self.logger = logs.Logger()
     #
     self.sbmlPath = sbmlPath
     self.roadRunner = self._initializeRoadrunner()
     self.selectedColumns = selectedColumns
     self.parametersToFit = self._getSetableParameters(parametersToFit)
     self.kwargs = kwargs
     self.parametersToFit = self._getSetableParameters(parametersToFit)
     self.parameterValueDct = {p: self.roadRunner[p]
           for p in self.parametersToFit}
     self.fitModelResult = TestHarnessResult()
     self.bootstrapResult = TestHarnessResult()
     self._validate()
Пример #2
0
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_STATUS, logPerformance=IS_TEST)
    optimizerMethod = SBstoat.OptimizerMethod(SBstoat.METHOD_LEASTSQ,
                                              {"max_nfev": 100})
    fitter = mf.ModelFitter(
        MODEL,
        BENCHMARK_PATH,
        ["k1", "k2"],
        selectedColumns=['S1', 'S3'],
        isPlot=IS_PLOT,
        logger=logger,
        fitterMethods=[optimizerMethod],
        bootstrapMethods=[optimizerMethod],
        isProgressBar=False,
    )
    fitter.fitModel()
    startTime = time.time()
    fitter.bootstrap(numIteration=numIteration, isParallel=IS_PARALLEL)
    elapsedTime = time.time() - startTime
    if IS_TEST:
        print(fitter.logger.formatPerformanceDF())
    fitter.plotFitAll()
    return elapsedTime
Пример #3
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)
Пример #4
0
def main(maxNfev=MAX_NFEV):
    """
    Calculates the time to run the benchmark.

    Parameters
    ----------
    numPopulation: int
    
    Returns
    -------
    float: time in seconds
    """
    logger = logs.Logger(logLevel=logs.LEVEL_SUPPRESS, logPerformance=IS_TEST)
    models = [MODEL for _ in range(NUM_MODEL)]
    parametersList = [PARAMETERS for _ in range(NUM_MODEL)]
    optimizerMethod = SBstoat.OptimizerMethod(method="differential_evolution",
                                              kwargs={
                                                  "popsize": 10,
                                                  'max_nfev': maxNfev
                                              })
    startTime = time.time()
    suiteFitter = mkSuiteFitter(models,
                                OBSERVED_FILES,
                                parametersList,
                                MODEL_NAMES,
                                logger=logger,
                                fitterMethods=[optimizerMethod])
    if NUM_FOLD == 1:
        suiteFitter.fitSuite()
    else:
        suiteFitter.crossValidate(NUM_FOLD, isParallel=IS_PARALLEL)
    elapsedTime = time.time() - startTime
    if IS_TEST:
        print(suiteFitter.reportFit())
    if IS_PLOT:
        suiteFitter.plotFitAll()
        suiteFitter.plotResidualsSSQ()
    return elapsedTime
Пример #5
0
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
Пример #6
0
import unittest

#matplotlib.use('TkAgg')


def remove(ffile):
    if os.path.isfile(ffile):
        os.remove(ffile)


IGNORE_TEST = False
IS_PLOT = False
TIMESERIES = th.getTimeseries()
DIR = os.path.dirname(os.path.abspath(__file__))
LOG_FILE = os.path.join(DIR, "testModelFitterBootstrap.log")
LOGGER = logs.Logger()
if IGNORE_TEST:
    # Write log to std output
    FITTER = th.getFitter(cls=mfb.ModelFitterBootstrap)
else:
    FITTER = th.getFitter(cls=mfb.ModelFitterBootstrap, logger=LOGGER)
FITTER.fitModel()
NUM_ITERATION = 50
FILE_SERIALIZE = os.path.join(DIR, "modelFitterBootstrap.pcl")
FILES = [FILE_SERIALIZE]
MEAN_UNIFORM = 0.5  # Mean of uniform distribution
STD_UNIFORM = np.sqrt(1.0 / 12)  # Standard deviation of uniform

remove(LOG_FILE)  # Clean log file on each run

Пример #7
0
import unittest

#matplotlib.use('TkAgg')


def remove(ffile):
    if os.path.isfile(ffile):
        os.remove(ffile)


IGNORE_TEST = False
IS_PLOT = False
TIMESERIES = th.getTimeseries()
DIR = os.path.dirname(os.path.abspath(__file__))
LOG_FILE = os.path.join(DIR, "testModelFitterBootstrap.log")
LOGGER = logs.Logger(logLevel=logs.LEVEL_RESULT)
if IGNORE_TEST:
    # Write log to std output
    FITTER = th.getFitter(cls=mfb.ModelFitterBootstrap)
else:
    FITTER = th.getFitter(cls=mfb.ModelFitterBootstrap, logger=LOGGER)
FITTER.fitModel()
NUM_ITERATION = 10
FILE_SERIALIZE = os.path.join(DIR, "modelFitterBootstrap.pcl")
FILES = [FILE_SERIALIZE]
MEAN_UNIFORM = 0.5  # Mean of uniform distribution
STD_UNIFORM = np.sqrt(1.0 / 12)  # Standard deviation of uniform

remove(LOG_FILE)  # Clean log file on each run