              return 0
          return session["iterations"]
  
  
  class SessionsSummary():
      """ Calculations for analysis summary stats """
  
      def __init__(self, session):
          logger.debug("Initializing %s: (session: %s)", self.__class__.__name__, session)
          self.session = session
          logger.debug("Initialized %s", self.__class__.__name__)
 
</s> remove     def __init__(self, session):
        logger.debug("Initializing %s: (session: %s)", self.__class__.__name__, session)
        self.session = session
        logger.debug("Initialized %s", self.__class__.__name__)
 </s> add         Parameters
        ----------
        session_id: int
            The session id to compile the stats for
        timestamps:
            The time stamp summary data for the given session id </s> remove     def get_iterations_for_session(self, session_id):
        """ Return the number of iterations for the given session id """
        session = self.state["sessions"].get(str(session_id), None)
        if session is None:
            logger.warning("No session data found for session id: %s", session_id)
            return 0
        return session["iterations"]
 </s> add         logger.debug("time_stats: %s", self._time_stats) </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 </s> remove     def __init__(self, root, cli_opts, statusbar, session):
        logger.debug("Initializing %s: (root %s, cli_opts: %s, statusbar: %s, session: %s)",
                     self.__class__.__name__, root, cli_opts, statusbar, session)
 </s> add     def __init__(self, root, cli_opts, statusbar):
        logger.debug("Initializing %s: (root %s, cli_opts: %s, statusbar: %s)",
                     self.__class__.__name__, root, cli_opts, statusbar) </s> remove     @property
    def loss_keys(self):
        """ list: The loss keys for the current session, or loss keys for all sessions. """
        if self.session_id is None:
            retval = self._total_loss_keys
        else:
            retval = self.session["loss_names"]
        return retval
 </s> add     Parameters
    ----------
    session: :class:`GlobalSession`
        The loaded or currently training session
    """
    def __init__(self, session):
        logger.debug("Initializing %s: (session: %s)", self.__class__.__name__, session)
        self._session = session
        self._state = session._state
        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.
              return 0
          return session["iterations"]
  
  
        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.
        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.
  
      def __init__(self, session):
          logger.debug("Initializing %s: (session: %s)", self.__class__.__name__, session)
          self.session = session
          logger.debug("Initialized %s", self.__class__.__name__)
keep keep keep keep 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