Utilities

Common Models

nupic.frameworks.opf.common_models.cluster_params.getScalarMetricWithTimeOfDayAnomalyParams(metricData, minVal=None, maxVal=None, minResolution=None, tmImplementation='cpp')

Return a dict that can be used to create an anomaly model via nupic.frameworks.opf.model_factory.ModelFactory.create().

Example:

from nupic.frameworks.opf.model_factory import ModelFactory
from nupic.frameworks.opf.common_models.cluster_params import (
  getScalarMetricWithTimeOfDayAnomalyParams)

params = getScalarMetricWithTimeOfDayAnomalyParams(
  metricData=[0],
  tmImplementation="cpp",
  minVal=0.0,
  maxVal=100.0)

model = ModelFactory.create(modelConfig=params["modelConfig"])
model.enableLearning()
model.enableInference(params["inferenceArgs"])
Parameters:
  • metricData – numpy array of metric data. Used to calculate minVal and maxVal if either is unspecified
  • minVal – minimum value of metric. Used to set up encoders. If None will be derived from metricData.
  • maxVal – maximum value of metric. Used to set up input encoders. If None will be derived from metricData
  • minResolution – minimum resolution of metric. Used to set up encoders. If None, will use default value of 0.001.
  • tmImplementation – (string) specifying type of temporal memory implementation. Valid strings : ["cpp", "tm_cpp"]
Returns:

(dict) containing modelConfig and inferenceArgs top-level properties. The value of the modelConfig property is for passing to create() method as the modelConfig parameter. The inferenceArgs property is for passing to the resulting model’s enableInference() method as the inferenceArgs parameter.

Note

The timestamp field corresponds to input c0; the predicted field corresponds to input c1.

Inference Utilities

class nupic.frameworks.opf.opf_utils.InferenceType

Bases: nupic.support.enum.Enum

Enum: one of the following:

  • TemporalNextStep
  • TemporalClassification
  • NontemporalClassification
  • TemporalAnomaly
  • NontemporalAnomaly
  • TemporalMultiStep
  • NontemporalMultiStep
static isTemporal(inferenceType)
Parameters:inferenceType – (InferenceType)
Returns:(bool) True if the inference type is ‘temporal’, i.e. requires a temporal memory in the network.
class nupic.frameworks.opf.opf_utils.InferenceElement

Bases: nupic.support.enum.Enum

The concept of InferenceElements is a key part of the OPF. A model’s inference may have multiple parts to it. For example, a model may output both a prediction and an anomaly score. Models output their set of inferences as a dictionary that is keyed by the enumerated type InferenceElement. Each entry in an inference dictionary is considered a separate inference element, and is handled independently by the OPF.

static getInputElement(inferenceElement)

Get the sensor input element that corresponds to the given inference element. This is mainly used for metrics and prediction logging

Parameters:inferenceElement – (InferenceElement)
Returns:(string) name of sensor input element
static getMaxDelay(inferences)
Parameters:inferences – (dict) where the keys are InferenceElement objects.
Returns:(int) the maximum delay for the InferenceElement objects in the inference dictionary.
static getTemporalDelay(inferenceElement, key=None)
Parameters:
  • inferenceElement – (InferenceElement) value being delayed
  • key – (string) If the inference is a dictionary type, this specifies key for the sub-inference that is being delayed.
Returns:

(int) the number of records that elapse between when an inference is made and when the corresponding input record will appear. For example, a multistep prediction for 3 timesteps out will have a delay of 3.

static isTemporal(inferenceElement)

Note

This should only be checked IF THE MODEL’S INFERENCE TYPE IS ALSO TEMPORAL. That is, a temporal model CAN have non-temporal inference elements, but a non-temporal model CANNOT have temporal inference elements.

Parameters:inferenceElement – (InferenceElement)
Returns:(bool) True if the inference from this time step is predicted the input for the NEXT time step.

Input Utilities

class nupic.frameworks.opf.opf_utils.SensorInput(dataRow=None, dataDict=None, dataEncodings=None, sequenceReset=None, category=None)

Represents the mapping of a given inputRecord by the sensor region’s encoder.

This represents the input record, as it appears right before it is encoded. This may differ from the raw input in that certain input fields (such as DateTime fields) may be split into multiple encoded fields.

Parameters:
  • dataRow – A data row that is the sensor’s sourceOut mapping of the supplied inputRecord.
  • dataEncodings – A list of the corresponding bit-array encodings of each value in “dataRow”
  • sequenceReset – The sensor’s “resetOut” signal (0 or 1) emitted by the sensor’s compute logic on the supplied inputRecord; provided for analysis and diagnostics.
  • dataDict – The raw encoded input to the sensor
  • category – the categoryOut on the sensor region
class nupic.frameworks.opf.opf_utils.ClassifierInput(dataRow=None, bucketIndex=None)

Represents the mapping of a given inputRecord by the classifier input encoder.

Parameters:
  • dataRow – A data row that is the sensor’s “sourceOut” mapping of the supplied inputRecord. See SensorInput class for additional details.
  • bucketIndex – (int) the classifier input encoder’s mapping of the dataRow.

Helpers

This file contains utility functions that may be imported by clients of the framework. Functions that are used only by the prediction framework should be in opf_utils.py

TODO: Rename as helpers.py once we’re ready to replace the legacy
helpers.py
helpers.loadExperiment(path)

Loads the experiment description file from the path.

Parameters:path – (string) The path to a directory containing a description.py file or the file itself.
Returns:(config, control)
helpers.loadExperimentDescriptionScriptFromDir(experimentDir)

Loads the experiment description python script from the given experiment directory.

Parameters:experimentDir – (string) experiment directory path
Returns:module of the loaded experiment description scripts
helpers.getExperimentDescriptionInterfaceFromModule(module)
Parameters:module – imported description.py module
Returns:(nupic.frameworks.opf.exp_description_api.DescriptionIface) represents the experiment description
opf_utils.validateOpfJsonValue(value, opfJsonSchemaFilename)

Validate a python object against an OPF json schema file

Parameters:
  • value – target python object to validate (typically a dictionary)
  • opfJsonSchemaFilename – (string) OPF json schema filename containing the json schema object. (e.g., opfTaskControlSchema.json)
Raises:

jsonhelpers.ValidationError when value fails json validation

opf_utils.initLogger(obj)

Helper function to create a logger object for the current object with the standard Numenta prefix.

Parameters:obj – (object) to add a logger to
opf_utils.matchPatterns(patterns, keys)

Returns a subset of the keys that match any of the given patterns

Parameters:
  • patterns – (list) regular expressions to match
  • keys – (list) keys to search for matches