<commit_before># ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2013, Numenta, Inc.  Unless you have an agreement
# with Numenta, Inc., for a separate license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero Public License for more details.
#
# You should have received a copy of the GNU Affero Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------

"""
Encapsulation of HTM network that implements the base
:class:`~nupic.frameworks.opf.model.Model` to perform temporal prediction.
"""

import copy
import math
import os
import json
import itertools
import logging
import traceback
from collections import deque
from operator import itemgetter
from functools import wraps

import numpy

from nupic.frameworks.opf.model import Model
from nupic.data import SENTINEL_VALUE_FOR_MISSING_DATA
from nupic.data.field_meta import FieldMetaSpecial, FieldMetaInfo
from nupic.encoders import MultiEncoder, DeltaEncoder
from nupic.engine import Network
from nupic.support.fshelpers import makeDirectoryFromAbsolutePath
from nupic.frameworks.opf.opf_utils import (InferenceType,
                                            InferenceElement,
                                            SensorInput,
                                            ClassifierInput,
                                            initLogger)

try:
  import capnp
except ImportError:
  capnp = None
if capnp:
  from nupic.frameworks.opf.HTMPredictionModelProto_capnp \
    import HTMPredictionModelProto


DEFAULT_LIKELIHOOD_THRESHOLD = 0.0001
DEFAULT_MAX_PREDICTIONS_PER_STEP = 8

DEFAULT_ANOMALY_TRAINRECORDS = 4000
DEFAULT_ANOMALY_THRESHOLD = 1.1
DEFAULT_ANOMALY_CACHESIZE = 10000


def requireAnomalyModel(func):
  """
  Decorator for functions that require anomaly models.
  """
  @wraps(func)
  def _decorator(self, *args, **kwargs):
    if not self.getInferenceType() == InferenceType.TemporalAnomaly:
      raise RuntimeError("Method required a TemporalAnomaly model.")
    if self._getAnomalyClassifier() is None:
      raise RuntimeError("Model does not support this command. Model must"
          "be an active anomalyDetector model.")
    return func(self, *args, **kwargs)
  return _decorator



class NetworkInfo(object):
  """ Data type used as return value type by
  HTMPredictionModel.__createCLANetwork()
  """

  def __init__(self, net, statsCollectors):
    """
    net:          The CLA Network instance
    statsCollectors:
                  Sequence of 0 or more CLAStatistic-based instances
    """
    self.net = net
    self.statsCollectors = statsCollectors
    return

  def __repr__(self):
    return "NetworkInfo(net=%r, statsCollectors=%r)" % (
              self.net, self.statsCollectors)



class HTMPredictionModel(Model):
  """

  This model is for temporal predictions multiple steps ahead.

  :param inferenceType: (:class:`~nupic.frameworks.opf.opf_utils.InferenceType`)

  :param predictedField: (string) The field to predict for multistep prediction.

  :param sensorParams: (dict) specifying the sensor parameters.

  :param spEnable: (bool) Whether or not to use a spatial pooler.

  :param spParams: (dict) specifying the spatial pooler parameters. These are
      passed to the spatial pooler.

  :param trainSPNetOnlyIfRequested: (bool) If set, don't create an SP network
      unless the user requests SP metrics.

  :param tmEnable: (bool) Whether to use a temporal memory.

  :param tmParams: (dict) specifying the temporal memory parameters. These are
      passed to the temporal memory.

  :param clEnable: (bool) Whether to use the classifier. If false, the
      classifier will not be created and no predictions will be generated.

  :param clParams: (dict) specifying the classifier parameters. These are passed
      to the classifier.

  :param anomalyParams: (dict) Anomaly detection parameters

  :param minLikelihoodThreshold: (float) The minimum likelihood value to include
      in inferences.  Currently only applies to multistep inferences.

  :param maxPredictionsPerStep: (int) Maximum number of predictions to include
      for each step in inferences. The predictions with highest likelihood are
      included.
  """

  __supportedInferenceKindSet = set((InferenceType.TemporalNextStep,
                                     InferenceType.TemporalClassification,
                                     InferenceType.NontemporalClassification,
                                     InferenceType.NontemporalAnomaly,
                                     InferenceType.TemporalAnomaly,
                                     InferenceType.TemporalMultiStep,
                                     InferenceType.NontemporalMultiStep))

  __myClassName = "HTMPredictionModel"


  def __init__(self,
      sensorParams={},
      inferenceType=InferenceType.TemporalNextStep,
      predictedField=None,
      spEnable=True,
      spParams={},

      # TODO: We can't figure out what this is. Remove?
      trainSPNetOnlyIfRequested=False,
      tmEnable=True,
      tmParams={},
      clEnable=True,
      clParams={},
      anomalyParams={},
      minLikelihoodThreshold=DEFAULT_LIKELIHOOD_THRESHOLD,
      maxPredictionsPerStep=DEFAULT_MAX_PREDICTIONS_PER_STEP,
      network=None):
    if not inferenceType in self.__supportedInferenceKindSet:
      raise ValueError("{0} received incompatible inference type: {1}"\
                       .format(self.__class__, inferenceType))

    # Call super class constructor
    super(HTMPredictionModel, self).__init__(inferenceType)

    # self.__restoringFromState is set to True by our __setstate__ method<commit_msg>Removed predictedField from HTMPredictionModel constructor (#3600)<commit_after># ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2013, Numenta, Inc.  Unless you have an agreement
# with Numenta, Inc., for a separate license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero Public License for more details.
#
# You should have received a copy of the GNU Affero Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------

"""
Encapsulation of HTM network that implements the base
:class:`~nupic.frameworks.opf.model.Model` to perform temporal prediction.
"""

import copy
import math
import os
import json
import itertools
import logging
import traceback
from collections import deque
from operator import itemgetter
from functools import wraps

import numpy

from nupic.frameworks.opf.model import Model
from nupic.data import SENTINEL_VALUE_FOR_MISSING_DATA
from nupic.data.field_meta import FieldMetaSpecial, FieldMetaInfo
from nupic.encoders import MultiEncoder, DeltaEncoder
from nupic.engine import Network
from nupic.support.fshelpers import makeDirectoryFromAbsolutePath
from nupic.frameworks.opf.opf_utils import (InferenceType,
                                            InferenceElement,
                                            SensorInput,
                                            ClassifierInput,
                                            initLogger)

try:
  import capnp
except ImportError:
  capnp = None
if capnp:
  from nupic.frameworks.opf.HTMPredictionModelProto_capnp \
    import HTMPredictionModelProto


DEFAULT_LIKELIHOOD_THRESHOLD = 0.0001
DEFAULT_MAX_PREDICTIONS_PER_STEP = 8

DEFAULT_ANOMALY_TRAINRECORDS = 4000
DEFAULT_ANOMALY_THRESHOLD = 1.1
DEFAULT_ANOMALY_CACHESIZE = 10000


def requireAnomalyModel(func):
  """
  Decorator for functions that require anomaly models.
  """
  @wraps(func)
  def _decorator(self, *args, **kwargs):
    if not self.getInferenceType() == InferenceType.TemporalAnomaly:
      raise RuntimeError("Method required a TemporalAnomaly model.")
    if self._getAnomalyClassifier() is None:
      raise RuntimeError("Model does not support this command. Model must"
          "be an active anomalyDetector model.")
    return func(self, *args, **kwargs)
  return _decorator



class NetworkInfo(object):
  """ Data type used as return value type by
  HTMPredictionModel.__createCLANetwork()
  """

  def __init__(self, net, statsCollectors):
    """
    net:          The CLA Network instance
    statsCollectors:
                  Sequence of 0 or more CLAStatistic-based instances
    """
    self.net = net
    self.statsCollectors = statsCollectors
    return

  def __repr__(self):
    return "NetworkInfo(net=%r, statsCollectors=%r)" % (
              self.net, self.statsCollectors)



class HTMPredictionModel(Model):
  """

  This model is for temporal predictions multiple steps ahead. After creating
  this model, you must call
  :meth:`~nupic.frameworks.opf.model.Model.enableInference` to specify a
  predicted field, like this:

  .. code-block:: python

     model.enableInference({"predictedField": "myPredictedField"})

  Where ``myPredictedField`` is the field name in your data input that should be
  predicted.

  :param inferenceType: (:class:`~nupic.frameworks.opf.opf_utils.InferenceType`)

  :param sensorParams: (dict) specifying the sensor parameters.

  :param spEnable: (bool) Whether or not to use a spatial pooler.

  :param spParams: (dict) specifying the spatial pooler parameters. These are
      passed to the spatial pooler.

  :param trainSPNetOnlyIfRequested: (bool) If set, don't create an SP network
      unless the user requests SP metrics.

  :param tmEnable: (bool) Whether to use a temporal memory.

  :param tmParams: (dict) specifying the temporal memory parameters. These are
      passed to the temporal memory.

  :param clEnable: (bool) Whether to use the classifier. If false, the
      classifier will not be created and no predictions will be generated.

  :param clParams: (dict) specifying the classifier parameters. These are passed
      to the classifier.

  :param anomalyParams: (dict) Anomaly detection parameters

  :param minLikelihoodThreshold: (float) The minimum likelihood value to include
      in inferences.  Currently only applies to multistep inferences.

  :param maxPredictionsPerStep: (int) Maximum number of predictions to include
      for each step in inferences. The predictions with highest likelihood are
      included.
  """

  __supportedInferenceKindSet = set((InferenceType.TemporalNextStep,
                                     InferenceType.TemporalClassification,
                                     InferenceType.NontemporalClassification,
                                     InferenceType.NontemporalAnomaly,
                                     InferenceType.TemporalAnomaly,
                                     InferenceType.TemporalMultiStep,
                                     InferenceType.NontemporalMultiStep))

  __myClassName = "HTMPredictionModel"


  def __init__(self,
      sensorParams={},
      inferenceType=InferenceType.TemporalNextStep,
      spEnable=True,
      spParams={},

      # TODO: We can't figure out what this is. Remove?
      trainSPNetOnlyIfRequested=False,
      tmEnable=True,
      tmParams={},
      clEnable=True,
      clParams={},
      anomalyParams={},
      minLikelihoodThreshold=DEFAULT_LIKELIHOOD_THRESHOLD,
      maxPredictionsPerStep=DEFAULT_MAX_PREDICTIONS_PER_STEP,
      network=None):
    if not inferenceType in self.__supportedInferenceKindSet:
      raise ValueError("{0} received incompatible inference type: {1}"\
                       .format(self.__class__, inferenceType))

    # Call super class constructor
    super(HTMPredictionModel, self).__init__(inferenceType)

    # self.__restoringFromState is set to True by our __setstate__ method
{'fraction_non_alphanumeric': 0.08624891894016826, 'fraction_numerical': 0.004245616793773096, 'mean_word_length': 2.848714069591528, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5824189', 'n_tokens_mistral': 3482, 'n_tokens_neox': 3288, 'n_words': 1124}