                       smooth_amount, flatten_outliers, is_totals)
  
          warnings.simplefilter("ignore", np.RankWarning)
  
          self.session = session
          self.display = display
          self.loss_keys = loss_keys
          self.selections = selections
          self.is_totals = is_totals
          self.args = {"avg_samples": avg_samples,
                       "smooth_amount": smooth_amount,
                       "flatten_outliers": flatten_outliers}
          self.iterations = 0
          self.stats = None
          self.refresh()
          logger.debug("Initialized %s", self.__class__.__name__)
  
      def refresh(self):
          """ Refresh the stats """
 
</s> remove         if not self.session.initialized:
 </s> add         if not Session.is_loaded: </s> remove     """ Class to pull raw data for given session(s) and perform calculations """
    def __init__(self, session, display="loss", loss_keys=["loss"], selections=["raw"],
                 avg_samples=500, smooth_amount=0.90, flatten_outliers=False, is_totals=False):
        logger.debug("Initializing %s: (session: %s, display: %s, loss_keys: %s, selections: %s, "
                     "avg_samples: %s, smooth_amount: %s, flatten_outliers: %s, is_totals: %s",
                     self.__class__.__name__, session, display, loss_keys, selections, avg_samples,
                     smooth_amount, flatten_outliers, is_totals)
 </s> add     """ Class that performs calculations on the :class:`GlobalSession` raw data for the given
    session id.

    Parameters
    ----------
    session_id: int or ``None``
        The session id number for the selected session from the Analysis tab. Should be ``None``
        if all sessions are being calculated
    display: {"loss", "rate"}, optional
        Whether to display a graph for loss or training rate. Default: `"loss"`
    loss_keys: list, optional
        The list of loss keys to display on the graph. Default: `["loss"]`
    selections: list, optional
        The selected annotations to display. Default: `["raw"]`
    avg_samples: int, optional
        The number of samples to use for performing moving average calculation. Default: `500`.
    smooth_amount: float, optional
        The amount of smoothing to apply for performing smoothing calculation. Default: `0.9`.
    flatten_outliers: bool, optional
        ``True`` if values significantly away from the average should be excluded, otherwise
        ``False``. Default: ``False``
    """
    def __init__(self, session_id,
                 display="loss",
                 loss_keys="loss",
                 selections="raw",
                 avg_samples=500,
                 smooth_amount=0.90,
                 flatten_outliers=False):
        logger.debug("Initializing %s: (session_id: %s, display: %s, loss_keys: %s, "
                     "selections: %s, avg_samples: %s, smooth_amount: %s, flatten_outliers: %s)",
                     self.__class__.__name__, session_id, display, loss_keys, selections,
                     avg_samples, smooth_amount, flatten_outliers) </s> remove         self.iterations = 0
        self.stats = self._get_raw()
        self.get_calculations()
        self.remove_raw()
 </s> add         self._iterations = 0
        self._get_raw()
        self._get_calculations()
        self._remove_raw() </s> remove class SessionsSummary():
    """ Calculations for analysis summary stats """
 </s> add         If a training session is running, then updates the training sessions stats only.
        """
        if self._per_session_stats is None:
            logger.debug("Collating per session stats")
            compiled = list()
            for session_id, ts_data in self._time_stats.items():
                logger.debug("Compiling session ID: %s", session_id)
                if self._state is None:
                    logger.debug("Session state dict doesn't exist. Most likely task has been "
                                 "terminated during compilation")
                    return
                compiled.append(self._collate_stats(session_id, ts_data))

            self._per_session_stats = list(sorted(compiled, key=lambda k: k["session"]))

        elif self._session.is_training:
            logger.debug("Collating per session stats for latest training data")
            session_id = self._session.session_ids[-1]
            ts_data = self._time_stats[session_id]

            if session_id > len(self._per_session_stats):
                self._per_session_stats.append(self._collate_stats(session_id, ts_data))

            stats = self._per_session_stats[-1]

            stats["start"] = ts_data["start_time"]
            stats["end"] = ts_data["end_time"]
            stats["elapsed"] = int(stats["end"] - stats["start"])
            stats["iterations"] = ts_data["iterations"]
            stats["rate"] = (((stats["batch"] * 2) * stats["iterations"])
                             / stats["elapsed"] if stats["elapsed"] != 0 else 0)
        logger.debug("per_session_stats: %s", self._per_session_stats)

    def _collate_stats(self, session_id, timestamps):
        """ Collate the session summary statistics for the given session ID. </s> remove     @property
    def time_stats(self):
        """ Return session time stats """
        ts_data = self.session.tb_logs.get_timestamps()
        time_stats = {sess_id: {"start_time": min(timestamps) if timestamps else 0,
                                "end_time": max(timestamps) if timestamps else 0,
                                "datapoints": len(timestamps) if timestamps else 0}
                      for sess_id, timestamps in ts_data.items()}
        return time_stats
 </s> add         Returns
        -------
        dict
            The collated session summary statistics
        """
        timestamps = self._time_stats[session_id]
        elapsed = int(timestamps["end_time"] - timestamps["start_time"])
        batchsize = self._session.batch_sizes.get(session_id, 0)
        retval = dict(
            session=session_id,
            start=timestamps["start_time"],
            end=timestamps["end_time"],
            elapsed=elapsed,
            rate=(((batchsize * 2) * timestamps["iterations"]) / elapsed if elapsed != 0 else 0),
            batch=batchsize,
            iterations=timestamps["iterations"])
        logger.debug(retval)
        return retval
        self._session_id = session_id

        self._display = display
        self._loss_keys = loss_keys if isinstance(loss_keys, list) else [loss_keys]
        self._selections = selections if isinstance(selections, list) else [selections]
        self._is_totals = session_id is None
        self._args = dict(avg_samples=avg_samples,
                          smooth_amount=smooth_amount,
                          flatten_outliers=flatten_outliers)
        self._iterations = 0
        self._limit = 0
        self._start_iteration = 0
        self._stats = dict()
                       smooth_amount, flatten_outliers, is_totals)
  
          warnings.simplefilter("ignore", np.RankWarning)
  
        self._session_id = session_id

        self._display = display
        self._loss_keys = loss_keys if isinstance(loss_keys, list) else [loss_keys]
        self._selections = selections if isinstance(selections, list) else [selections]
        self._is_totals = session_id is None
        self._args = dict(avg_samples=avg_samples,
                          smooth_amount=smooth_amount,
                          flatten_outliers=flatten_outliers)
        self._iterations = 0
        self._limit = 0
        self._start_iteration = 0
        self._stats = dict()
        self._session_id = session_id

        self._display = display
        self._loss_keys = loss_keys if isinstance(loss_keys, list) else [loss_keys]
        self._selections = selections if isinstance(selections, list) else [selections]
        self._is_totals = session_id is None
        self._args = dict(avg_samples=avg_samples,
                          smooth_amount=smooth_amount,
                          flatten_outliers=flatten_outliers)
        self._iterations = 0
        self._limit = 0
        self._start_iteration = 0
        self._stats = dict()
        self._session_id = session_id

        self._display = display
        self._loss_keys = loss_keys if isinstance(loss_keys, list) else [loss_keys]
        self._selections = selections if isinstance(selections, list) else [selections]
        self._is_totals = session_id is None
        self._args = dict(avg_samples=avg_samples,
                          smooth_amount=smooth_amount,
                          flatten_outliers=flatten_outliers)
        self._iterations = 0
        self._limit = 0
        self._start_iteration = 0
        self._stats = dict()
        self._session_id = session_id

        self._display = display
        self._loss_keys = loss_keys if isinstance(loss_keys, list) else [loss_keys]
        self._selections = selections if isinstance(selections, list) else [selections]
        self._is_totals = session_id is None
        self._args = dict(avg_samples=avg_samples,
                          smooth_amount=smooth_amount,
                          flatten_outliers=flatten_outliers)
        self._iterations = 0
        self._limit = 0
        self._start_iteration = 0
        self._stats = dict()
        self._session_id = session_id

        self._display = display
        self._loss_keys = loss_keys if isinstance(loss_keys, list) else [loss_keys]
        self._selections = selections if isinstance(selections, list) else [selections]
        self._is_totals = session_id is None
        self._args = dict(avg_samples=avg_samples,
                          smooth_amount=smooth_amount,
                          flatten_outliers=flatten_outliers)
        self._iterations = 0
        self._limit = 0
        self._start_iteration = 0
        self._stats = dict()
        self._session_id = session_id

        self._display = display
        self._loss_keys = loss_keys if isinstance(loss_keys, list) else [loss_keys]
        self._selections = selections if isinstance(selections, list) else [selections]
        self._is_totals = session_id is None
        self._args = dict(avg_samples=avg_samples,
                          smooth_amount=smooth_amount,
                          flatten_outliers=flatten_outliers)
        self._iterations = 0
        self._limit = 0
        self._start_iteration = 0
        self._stats = dict()
        self._session_id = session_id

        self._display = display
        self._loss_keys = loss_keys if isinstance(loss_keys, list) else [loss_keys]
        self._selections = selections if isinstance(selections, list) else [selections]
        self._is_totals = session_id is None
        self._args = dict(avg_samples=avg_samples,
                          smooth_amount=smooth_amount,
                          flatten_outliers=flatten_outliers)
        self._iterations = 0
        self._limit = 0
        self._start_iteration = 0
        self._stats = dict()
        self._session_id = session_id

        self._display = display
        self._loss_keys = loss_keys if isinstance(loss_keys, list) else [loss_keys]
        self._selections = selections if isinstance(selections, list) else [selections]
        self._is_totals = session_id is None
        self._args = dict(avg_samples=avg_samples,
                          smooth_amount=smooth_amount,
                          flatten_outliers=flatten_outliers)
        self._iterations = 0
        self._limit = 0
        self._start_iteration = 0
        self._stats = dict()
        self._session_id = session_id

        self._display = display
        self._loss_keys = loss_keys if isinstance(loss_keys, list) else [loss_keys]
        self._selections = selections if isinstance(selections, list) else [selections]
        self._is_totals = session_id is None
        self._args = dict(avg_samples=avg_samples,
                          smooth_amount=smooth_amount,
                          flatten_outliers=flatten_outliers)
        self._iterations = 0
        self._limit = 0
        self._start_iteration = 0
        self._stats = dict()
        self._session_id = session_id

        self._display = display
        self._loss_keys = loss_keys if isinstance(loss_keys, list) else [loss_keys]
        self._selections = selections if isinstance(selections, list) else [selections]
        self._is_totals = session_id is None
        self._args = dict(avg_samples=avg_samples,
                          smooth_amount=smooth_amount,
                          flatten_outliers=flatten_outliers)
        self._iterations = 0
        self._limit = 0
        self._start_iteration = 0
        self._stats = dict()
          self.refresh()
          logger.debug("Initialized %s", self.__class__.__name__)
  
      def refresh(self):
          """ Refresh the stats """
keep keep keep keep replace replace replace replace replace replace replace replace replace replace keep keep keep keep keep
 GUI - Stats optimization (#1067)

* Faster stat loading + caching

* Compress data in cache

* Optimize some calculations

* Vectorize smoothing

* stats.Calculations optimized

* Load latest training data from live iterator

* Add options to training graph 
lib/gui/stats.py