Beispiel #1
0
 def plotFitAll(self,
                isMultiple=False,
                params: lmfit.Parameters = None,
                numPoint: int = None,
                **kwargs):
     """
     Plots the fitted with observed data over time.
 
     Parameters
     ----------
     isMultiple: bool
         plots all variables on a single plot
     #@expand
     """
     self._updateFit(params, numPoint)
     bandLowTS = None
     bandHighTS = None
     if self.bootstrapResult is not None:
         statistic = self.bootstrapResult.simulate(numPoint=100,
                                                   numSample=1000)
         self._plotFittedTS = statistic.meanTS
         bandLowTS = statistic.percentileDct[LOW_PERCENTILE]
         bandHighTS = statistic.percentileDct[HIGH_PERCENTILE]
     analyzer = ResidualsAnalyzer(self.observedTS,
                                  self._plotFittedTS,
                                  residualsTS=self.residualsTS,
                                  bandLowTS=bandLowTS,
                                  bandHighTS=bandHighTS,
                                  isPlot=self._isPlot)
     self._addKeyword(kwargs, po.NUM_ROW, 2)
     analyzer.plotFittedObservedOverTime(**kwargs)
Beispiel #2
0
    def plotFitAll(self,
                   params: lmfit.Parameters = None,
                   numPoint: int = None,
                   **kwargs):
        """
        Plots the fitted with observed data over time.

        Parameters
        ----------
        #@expand
        """
        if numPoint is None:
            numPoint = len(self.observedTS)
        fittedTS = self._getFittedTS(params, numPoint)
        bandLowTS = None
        bandHighTS = None
        if self.bootstrapResult is not None:
            statistic = self.bootstrapResult.simulate(numPoint=100,
                                                      numSample=1000)
            fittedTS = statistic.meanTS
            bandLowTS = statistic.percentileDct[LOW_PERCENTILE]
            bandHighTS = statistic.percentileDct[HIGH_PERCENTILE]
        analyzer = ResidualsAnalyzer(self.observedTS,
                                     fittedTS,
                                     residualsTS=self.residualsTS,
                                     bandLowTS=bandLowTS,
                                     bandHighTS=bandHighTS,
                                     isPlot=self._isPlot)
        self._addKeyword(kwargs, po.NUM_ROW, 2)
        analyzer.plotFittedObservedOverTime(**kwargs)
Beispiel #3
0
 def setUp(self):
     self.observedTS = OBSERVED_TS
     self.fittedTS = FITTED_TS
     self.fitter = FITTER
     self.statistic = self.fitter.bootstrapResult.simulate(numPoint=30,
                                                           numSample=300)
     self.analyzer = ResidualsAnalyzer(self.observedTS,
                                       self.fittedTS,
                                       isPlot=IS_PLOT)
Beispiel #4
0
 def testPlotFittedObservedOverTime1(self):
     if IGNORE_TEST:
         return
     analyzer = ResidualsAnalyzer(
         self.observedTS,
         self.statistic.meanTS,
         bandLowTS=self.statistic.percentileDct[LOW_PERCENTILE],
         bandHighTS=self.statistic.percentileDct[HIGH_PERCENTILE],
         isPlot=IS_PLOT)
     analyzer.plotFittedObservedOverTime(numCol=3, numRow=2)
     #
     self.analyzer.plotFittedObservedOverTime(numCol=3, numRow=2)
Beispiel #5
0
 def testPlotFittedObservedOverTime2(self):
     if IGNORE_TEST:
         return
     meanTS = self.fittedTS.copy(isInitialize=True)
     stdTS = self.fittedTS.copy(isInitialize=True)
     for col in meanTS.colnames:
         meanTS[col] = 2
         stdTS[col] = 1
     analyzer = ResidualsAnalyzer(self.observedTS,
                                  self.fittedTS,
                                  meanFittedTS=meanTS,
                                  stdFittedTS=stdTS,
                                  isPlot=IS_PLOT)
     analyzer.plotFittedObservedOverTime(numCol=3, numRow=2)
Beispiel #6
0
class TestReidualAnalyzer(unittest.TestCase):
    def setUp(self):
        self.observedTS = OBSERVED_TS
        self.fittedTS = FITTED_TS
        self.fitter = FITTER
        self.statistic = self.fitter.bootstrapResult.simulate(numPoint=30,
                                                              numSample=300)
        self.analyzer = ResidualsAnalyzer(self.observedTS,
                                          self.fittedTS,
                                          isPlot=IS_PLOT)

    def testPlotResidualsOverTime(self):
        if IGNORE_TEST:
            return
        self.analyzer.plotResidualsOverTime(numCol=3,
                                            numRow=2,
                                            ylim=[-1.5, 1.5])

    def testPlotFittedObservedOverTime1(self):
        if IGNORE_TEST:
            return
        analyzer = ResidualsAnalyzer(
            self.observedTS,
            self.statistic.meanTS,
            bandLowTS=self.statistic.percentileDct[LOW_PERCENTILE],
            bandHighTS=self.statistic.percentileDct[HIGH_PERCENTILE],
            isPlot=IS_PLOT)
        analyzer.plotFittedObservedOverTime(numCol=3, numRow=2)
        #
        self.analyzer.plotFittedObservedOverTime(numCol=3, numRow=2)

    def testPlotFittedObservedOverTime2(self):
        if IGNORE_TEST:
            return
        meanTS = self.fittedTS.copy(isInitialize=True)
        stdTS = self.fittedTS.copy(isInitialize=True)
        for col in meanTS.colnames:
            meanTS[col] = 2
            stdTS[col] = 1
        analyzer = ResidualsAnalyzer(self.observedTS,
                                     self.fittedTS,
                                     meanFittedTS=meanTS,
                                     stdFittedTS=stdTS,
                                     isPlot=IS_PLOT)
        analyzer.plotFittedObservedOverTime(numCol=3, numRow=2)

    def testPlotResidualsHistograms(self):
        if IGNORE_TEST:
            return
        self.analyzer.plotResidualsHistograms(ylim=[0, 5],
                                              xlim=[0, 6],
                                              titlePosition=[.3, .9],
                                              bins=10)

    def testPlotAll(self):
        if IGNORE_TEST:
            return
        self.analyzer.plotAll()
Beispiel #7
0
 def plotResiduals(self,
                   params: lmfit.Parameters = None,
                   numPoint: int = None,
                   **kwargs):
     """
     Plots residuals of a fit over time.
 
     Parameters
     ----------
     #@expand
     """
     self._updateFit(params, numPoint)
     analyzer = ResidualsAnalyzer(self.observedTS,
                                  self._plotFittedTS,
                                  residualsTS=self.residualsTS,
                                  isPlot=self._isPlot)
     analyzer.plotResidualsOverTime(**kwargs)
Beispiel #8
0
 def plotResidualsAll(self,
                      params: lmfit.Parameters = None,
                      numPoint: int = None,
                      **kwargs):
     """
     Plots a set of residual plots
 
     Parameters
     ----------
     #@expand
     """
     self._updateFit(params, numPoint)
     analyzer = ResidualsAnalyzer(self.observedTS,
                                  self._plotFittedTS,
                                  residualsTS=self.residualsTS,
                                  isPlot=self._isPlot)
     analyzer.plotAll(**kwargs)
Beispiel #9
0
 def plot(self, kind: str, params=None, numPoint=None, **kwargs):
     """
     High level plot routine.
     A figure may contain 1 or more plot, and each plot may contain
     1 or more curve. Figure (f), plot (p), and curve (c) are the possible
     scope of an option.  If the option is singled valued, then it applies
     to all instances of its scope. If it has multiple values, then the
     index of the value corresponds to the instance within the scope.
     A figure scope is always single valued.
 
     Parameters
     ----------
     kind: name of plot type
         FitAll: plots of fitted, observed data over time
         ParameterEstimatePairs: Pairwise plots of bootstrap estimates
         ParameterHistograms: histographs of bootstrap parameter values
         Residuals: plot residuals over time
         ResidualsAll: do all residuals plots
         ResidualsHistograms: histogram of residual values
         ResidualAutoCorrelations: Autocorrelations of residuals
         ResidualCrossCorrelations: Cross correlations of residuals
     alpha: (c) transparency; in [0, 1] (float)
     bins: (f) number of bins in a histogram plot (int)
     color: (c) color of the line (str, default="blue")
     columns: (f) list of columns to plot (list, default=[])
     figsize: (f) (horizontal width, vertical height) (list, default=[8, 6])
     legend: (p) tuple of str for legend (list)
     linestyle: (c) line style (str)
     marker: (c) marker for line (str)
     markersize: (c) size of marker for the line; >0 (float)
     num_row: (f) rows of plots (int)
     num_col: (f) columns of plots (int)
     subplot_width_space: (f) horizontal space between plots (float)
     timeseries2: (f) second timeseries
     title: (p) plot title (str)
     title_fontsize: (p) point size for title (float)
     title_position: (p) (x, y) relative position; x,y in [0, 1]
     suptitle: (f) figure title (str)
     xlabel: (f) x axis title (str)
     xlim: (f) order pair of lower and upper (list)
     xticklabels: (f) list of labels for x ticks (list)
     ylabel: (f) label for x axis (str)
     ylim: (f) order pair of lower and upper (str)
     yticklabels: (f) list of labels for y ticks (list)
     """
     MODULE_KINDS = [
         'ResidualsAll',
         'Residuals',
         'FitAll',
         'ParameterEstimatePairs',
         'ParameterHistograms',
     ]
     RESIDUAL_MODULE_KINDS = [
         'ResidualsHistograms',
         'ResidualAutoCorrelations',
         'ResidualCrossCorrelations',
     ]
     kinds = list(MODULE_KINDS)
     kinds.extend(RESIDUAL_MODULE_KINDS)
     if not kind in kinds:
         raise ValueError("Invalid plot kind. Options are: %s" % str(kinds))
     if kind in MODULE_KINDS:
         statement = "self.plot%s(**kwargs)" % kind
         exec(statement)
     else:
         self._updateFit(params, numPoint)
         analyzer = ResidualsAnalyzer(
             self.observedTS,
             self._plotFittedTS,
             meanFittedTS=self.bootstrapResult.fittedStatistic.meanTS,
             stdFittedTS=self.bootstrapResult.fittedStatistic.stdTS,
             residualsTS=self.residualsTS,
             isPlot=self._isPlot)
         statement = "analyzer.plot%s(**kwargs)" % kind
         exec(statement)