if name == "<STR_LIT>":<EOL><INDENT>self.learningMode = bool(int(value))<EOL>self._epoch = <NUM_LIT:0><EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>self._epoch = <NUM_LIT:0><EOL>if int(value) and not self.inferenceMode:<EOL><INDENT>self._finishLearning()<EOL><DEDENT>self.inferenceMode = bool(int(value))<EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>self._knn.distanceNorm = value<EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>self._knn.distanceMethod = value<EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>self.keepAllDistances = bool(value)<EOL>if not self.keepAllDistances:<EOL><INDENT>if self._protoScores is not None and self._protoScores.shape[<NUM_LIT:0>] > <NUM_LIT:1>:<EOL><INDENT>self._protoScores = self._protoScores[-<NUM_LIT:1>,:]<EOL><DEDENT>if self._protoScores is not None:<EOL><INDENT>self._protoScoreCount = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>self._protoScoreCount = <NUM_LIT:0><EOL><DEDENT><DEDENT><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>self.verbosity = value<EOL>self._knn.verbosity = value<EOL><DEDENT>else:<EOL><INDENT>return PyRegion.setParameter(self, name, index, value)<EOL><DEDENT>


self.confusion = numpy.zeros((<NUM_LIT:1>, <NUM_LIT:1>))<EOL>


self._tapFileIn = open(tapPath + '<STR_LIT>', '<STR_LIT:w>')<EOL>self._tapFileOut = open(tapPath + '<STR_LIT>', '<STR_LIT:w>')<EOL>

Begin writing output tap files.

:param tapPath: (string) base name of the output tap files to write.


if self._tapFileIn is not None:<EOL><INDENT>self._tapFileIn.close()<EOL>self._tapFileIn = None<EOL><DEDENT>if self._tapFileOut is not None:<EOL><INDENT>self._tapFileOut.close()<EOL>self._tapFileOut = None<EOL><DEDENT>


if self._tapFileIn is not None:<EOL><INDENT>for input in inputs:<EOL><INDENT>for k in range(len(input)):<EOL><INDENT>print(input[k], end='<STR_LIT:U+0020>', file=self._tapFileIn)<EOL><DEDENT>print(file=self._tapFileIn)<EOL><DEDENT><DEDENT>

Write inputs to output tap file.

:param inputs: (iter) some inputs.


<EOL>if self._tapFileOut is not None:<EOL><INDENT>for k in range(len(output)):<EOL><INDENT>print(output[k], end='<STR_LIT:U+0020>', file=self._tapFileOut)<EOL><DEDENT>print(file=self._tapFileOut)<EOL><DEDENT>

Write outputs to output tap file.

:param outputs: (iter) some outputs.


def _storeSample(self, inputVector, trueCatIndex, partition=<NUM_LIT:0>):

<EOL>if self._samples is None:<EOL><INDENT>self._samples = numpy.zeros((<NUM_LIT:0>, len(inputVector)), dtype=RealNumpyDType)<EOL>assert self._labels is None<EOL>self._labels = []<EOL><DEDENT>self._samples = numpy.concatenate((self._samples, numpy.atleast_2d(inputVector)), axis=<NUM_LIT:0>)<EOL>self._labels += [trueCatIndex]<EOL>if self._partitions is None:<EOL><INDENT>self._partitions = []<EOL><DEDENT>if partition is None:<EOL><INDENT>partition = <NUM_LIT:0><EOL><DEDENT>self._partitions += [partition]<EOL>

Store a training sample and associated category label


<EOL>if self._useAuxiliary is None:<EOL><INDENT>self._useAuxiliary = False<EOL><DEDENT>if self._firstComputeCall:<EOL><INDENT>self._firstComputeCall = False<EOL>if self._useAuxiliary:<EOL><INDENT>if self._justUseAuxiliary == True:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>inputVector = inputs['<STR_LIT>']<EOL>if self._useAuxiliary==True:<EOL><INDENT>auxVector = inputs['<STR_LIT>']<EOL>if auxVector.dtype != numpy.float32:<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>if self._justUseAuxiliary == True:<EOL><INDENT>inputVector = inputs['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>inputVector = numpy.concatenate([inputVector, inputs['<STR_LIT>']])<EOL><DEDENT><DEDENT>self.handleLogInput([inputVector])<EOL>assert "<STR_LIT>" in inputs, "<STR_LIT>"<EOL>categories = inputs['<STR_LIT>']<EOL>if "<STR_LIT>" in inputs:<EOL><INDENT>assert len(inputs["<STR_LIT>"]) == <NUM_LIT:1>, "<STR_LIT>"<EOL>partInput = inputs['<STR_LIT>']<EOL>assert len(partInput) == <NUM_LIT:1>, "<STR_LIT>"<EOL>partition = int(partInput[<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>partition = None<EOL><DEDENT>if self.inferenceMode:<EOL><INDENT>categoriesOut = outputs['<STR_LIT>']<EOL>probabilitiesOut = outputs['<STR_LIT>']<EOL>if self._doSphering:<EOL><INDENT>inputVector = (inputVector + self._normOffset) * self._normScale<EOL><DEDENT>nPrototypes = <NUM_LIT:0><EOL>if "<STR_LIT>" in outputs:<EOL><INDENT>bestPrototypeIndicesOut = outputs["<STR_LIT>"]<EOL>nPrototypes = len(bestPrototypeIndicesOut)<EOL><DEDENT>winner, inference, protoScores, categoryDistances =self._knn.infer(inputVector, partitionId=partition)<EOL>if not self.keepAllDistances:<EOL><INDENT>self._protoScores = protoScores<EOL><DEDENT>else:<EOL><INDENT>if self._protoScores is None:<EOL><INDENT>self._protoScores = numpy.zeros((<NUM_LIT:1>, protoScores.shape[<NUM_LIT:0>]),<EOL>protoScores.dtype)<EOL>self._protoScores[<NUM_LIT:0>,:] = protoScores<EOL>self._protoScoreCount = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>if self._protoScoreCount == self._protoScores.shape[<NUM_LIT:0>]:<EOL><INDENT>newProtoScores = numpy.zeros((self._protoScores.shape[<NUM_LIT:0>] * <NUM_LIT:2>,<EOL>self._protoScores.shape[<NUM_LIT:1>]),<EOL>self._protoScores.dtype)<EOL>newProtoScores[:self._protoScores.shape[<NUM_LIT:0>],:] = self._protoScores<EOL>self._protoScores = newProtoScores<EOL><DEDENT>self._protoScores[self._protoScoreCount,:] = protoScores<EOL>self._protoScoreCount += <NUM_LIT:1><EOL><DEDENT><DEDENT>self._categoryDistances = categoryDistances<EOL>if self.outputProbabilitiesByDist:<EOL><INDENT>scores = <NUM_LIT:1.0> - self._categoryDistances<EOL><DEDENT>else:<EOL><INDENT>scores = inference<EOL><DEDENT>total = scores.sum()<EOL>if total == <NUM_LIT:0>:<EOL><INDENT>numScores = len(scores)<EOL>probabilities = numpy.ones(numScores) / numScores<EOL><DEDENT>else:<EOL><INDENT>probabilities = scores / total<EOL><DEDENT>nout = min(len(categoriesOut), len(inference))<EOL>categoriesOut.fill(<NUM_LIT:0>)<EOL>categoriesOut[<NUM_LIT:0>:nout] = inference[<NUM_LIT:0>:nout]<EOL>probabilitiesOut.fill(<NUM_LIT:0>)<EOL>probabilitiesOut[<NUM_LIT:0>:nout] = probabilities[<NUM_LIT:0>:nout]<EOL>if self.verbosity >= <NUM_LIT:1>:<EOL><INDENT>print("<STR_LIT>", categoriesOut[<NUM_LIT:0>:nout])<EOL>print("<STR_LIT>", probabilitiesOut[<NUM_LIT:0>:nout])<EOL><DEDENT>if self._scanInfo is not None:<EOL><INDENT>self._scanResults = [tuple(inference[:nout])]<EOL><DEDENT>for category in categories:<EOL><INDENT>if category >= <NUM_LIT:0>:<EOL><INDENT>dims = max(int(category)+<NUM_LIT:1>, len(inference))<EOL>oldDims = len(self.confusion)<EOL>if oldDims < dims:<EOL><INDENT>confusion = numpy.zeros((dims, dims))<EOL>confusion[<NUM_LIT:0>:oldDims, <NUM_LIT:0>:oldDims] = self.confusion<EOL>self.confusion = confusion<EOL><DEDENT>self.confusion[inference.argmax(), int(category)] += <NUM_LIT:1><EOL><DEDENT><DEDENT>if nPrototypes > <NUM_LIT:1>:<EOL><INDENT>bestPrototypeIndicesOut.fill(<NUM_LIT:0>)<EOL>if categoryDistances is not None:<EOL><INDENT>indices = categoryDistances.argsort()<EOL>nout = min(len(indices), nPrototypes)<EOL>bestPrototypeIndicesOut[<NUM_LIT:0>:nout] = indices[<NUM_LIT:0>:nout]<EOL><DEDENT><DEDENT>elif nPrototypes == <NUM_LIT:1>:<EOL><INDENT>if (categoryDistances is not None) and len(categoryDistances):<EOL><INDENT>bestPrototypeIndicesOut[<NUM_LIT:0>] = categoryDistances.argmin()<EOL><DEDENT>else:<EOL><INDENT>bestPrototypeIndicesOut[<NUM_LIT:0>] = <NUM_LIT:0><EOL><DEDENT><DEDENT>self.handleLogOutput(inference)<EOL><DEDENT>if self.learningMode:<EOL><INDENT>if (self.acceptanceProbability < <NUM_LIT:1.0>) and(self._rgen.getReal64() > self.acceptanceProbability):<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>for category in categories:<EOL><INDENT>if category >= <NUM_LIT:0>:<EOL><INDENT>if self._doSphering:<EOL><INDENT>self._storeSample(inputVector, category, partition)<EOL><DEDENT>else:<EOL><INDENT>self._knn.learn(inputVector, category, partition)<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>self._epoch += <NUM_LIT:1><EOL>

Process one input sample. This method is called by the runtime engine.

.. note:: the number of input categories may vary, but the array size is
   fixed to the max number of categories allowed (by a lower region), so
   "unused" indices of the input category array are filled with -1s.

TODO: confusion matrix does not support multi-label classification

:param inputs: (dict) mapping region input names to numpy.array values
:param outputs: (dict) mapping region output names to numpy.arrays that
       should be populated with output values by this method


return self._knn._categoryList<EOL>

Public API for returning the category list. This is a required API of the
NearestNeighbor inspector.

:returns: (list) which has one entry per stored prototype. The value of the
          entry is the category # of that stored prototype.


return self._knn.removeCategory(categoryToRemove)<EOL>

Removes a category.

:param categoryToRemove: (string) label to remove


if self._protoScores is None:<EOL><INDENT>return None<EOL><DEDENT>return self._protoScores[:self._protoScoreCount, :]<EOL>

Like :meth:`~nupic.regions.knn_classifier_region.KNNClassifierRegion.getLatestDistances`,
but returns all the scores if more than one  set is available.
:meth:`~nupic.regions.knn_classifier_region.KNNClassifierRegion.getLatestDistances`
will always just return one set of scores.

:returns: (list) all the prototype distances from all computes available.


if self._doSphering:<EOL><INDENT>self._finishSphering()<EOL><DEDENT>self._knn.finishLearning()<EOL>self._accuracy = None<EOL>

Does nothing. Kept here for API compatibility


<EOL>self._normOffset = self._samples.mean(axis=<NUM_LIT:0>) * -<NUM_LIT:1.0><EOL>self._samples += self._normOffset<EOL>variance = self._samples.var(axis=<NUM_LIT:0>)<EOL>variance[numpy.where(variance == <NUM_LIT:0.0>)] = <NUM_LIT:1.0><EOL>self._normScale  = <NUM_LIT:1.0> / numpy.sqrt(variance)<EOL>self._samples *= self._normScale<EOL>for sampleIndex in range(len(self._labels)):<EOL><INDENT>self._knn.learn(self._samples[sampleIndex],<EOL>self._labels[sampleIndex],<EOL>self._partitions[sampleIndex])<EOL><DEDENT>

Compute normalization constants for each feature dimension
based on the collected training samples.  Then normalize our
training samples using these constants (so that each input
dimension has mean and variance of zero and one, respectively.)
Then feed these "sphered" training samples into the underlying
SVM model.


if name == '<STR_LIT>':<EOL><INDENT>return self.maxCategoryCount<EOL><DEDENT>elif name == '<STR_LIT>':<EOL><INDENT>return self.maxCategoryCount<EOL><DEDENT>elif name == '<STR_LIT>':<EOL><INDENT>return self._bestPrototypeIndexCount if self._bestPrototypeIndexCount else <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>' + name)<EOL><DEDENT>


<EOL>assert isinstance(self.description, str)<EOL>assert isinstance(self.singleNodeOnly, bool)<EOL>assert isinstance(self.inputs, dict)<EOL>assert isinstance(self.outputs, dict)<EOL>assert isinstance(self.parameters, dict)<EOL>assert isinstance(self.commands, dict)<EOL>hasDefaultInput = False<EOL>for k, v in self.inputs.items():<EOL><INDENT>assert isinstance(k, str)<EOL>assert isinstance(v, InputSpec)<EOL>v.invariant()<EOL>if v.isDefaultInput:<EOL><INDENT>assert not hasDefaultInput<EOL>hasDefaultInput = True<EOL><DEDENT><DEDENT>hasDefaultOutput = False<EOL>for k, v in self.outputs.items():<EOL><INDENT>assert isinstance(k, str)<EOL>assert isinstance(v, OutputSpec)<EOL>v.invariant()<EOL>if v.isDefaultOutput:<EOL><INDENT>assert not hasDefaultOutput<EOL>hasDefaultOutput = True<EOL><DEDENT><DEDENT>for k, v in self.parameters.items():<EOL><INDENT>assert isinstance(k, str)<EOL>assert isinstance(v, ParameterSpec)<EOL>v.invariant()<EOL><DEDENT>for k, v in self.commands.items():<EOL><INDENT>assert isinstance(k, str)<EOL>assert isinstance(v, CommandSpec)<EOL>v.invariant()<EOL><DEDENT>

Verify the validity of the node spec object

        The type of each sub-object is verified and then
        the validity of each node spec item is verified by calling
        it invariant() method. It also makes sure that there is at most
        one default input and one default output.


def items2dict(items):<EOL><INDENT>"""<STR_LIT>"""<EOL>d = {}<EOL>for k, v in items.items():<EOL><INDENT>d[k] = v.__dict__<EOL><DEDENT>return d<EOL><DEDENT>self.invariant()<EOL>return dict(description=self.description,<EOL>singleNodeOnly=self.singleNodeOnly,<EOL>inputs=items2dict(self.inputs),<EOL>outputs=items2dict(self.outputs),<EOL>parameters=items2dict(self.parameters),<EOL>commands=items2dict(self.commands))<EOL>

Convert the information of the node spec to a plain dict of basic types

        The description and singleNodeOnly attributes are placed directly in
        the result dicts. The inputs, outputs, parameters and commands dicts
        contain Spec item objects (InputSpec, OutputSpec, etc). Each such object
        is converted also to a plain dict using the internal items2dict() function
        (see bellow).


return '<STR_LIT>'<EOL>

Return the default spatial pooler implementation for this region.


if spatialImp == '<STR_LIT>':<EOL><INDENT>return PYSpatialPooler<EOL><DEDENT>elif spatialImp == '<STR_LIT>':<EOL><INDENT>return CPPSpatialPooler<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError("<STR_LIT>"<EOL>"<STR_LIT>" % (spatialImp))<EOL><DEDENT>

Return the class corresponding to the given spatialImp string


def _buildArgs(f, self=None, kwargs={}):

<EOL>argTuples = getArgumentDescriptions(f)<EOL>argTuples = argTuples[<NUM_LIT:1>:]  <EOL>init = SPRegion.__init__<EOL>ourArgNames = [t[<NUM_LIT:0>] for t in getArgumentDescriptions(init)]<EOL>ourArgNames += [<EOL>'<STR_LIT>',<EOL>]<EOL>for argTuple in argTuples[:]:<EOL><INDENT>if argTuple[<NUM_LIT:0>] in ourArgNames:<EOL><INDENT>argTuples.remove(argTuple)<EOL><DEDENT><DEDENT>if self:<EOL><INDENT>for argTuple in argTuples:<EOL><INDENT>argName = argTuple[<NUM_LIT:0>]<EOL>if argName in kwargs:<EOL><INDENT>argValue = kwargs.pop(argName)<EOL><DEDENT>else:<EOL><INDENT>if len(argTuple) == <NUM_LIT:2>:<EOL><INDENT>raise TypeError("<STR_LIT>" % argName)<EOL><DEDENT>argValue = argTuple[<NUM_LIT:2>]<EOL><DEDENT>setattr(self, argName, argValue)<EOL><DEDENT><DEDENT>return argTuples<EOL>

Get the default arguments from the function and assign as instance vars.

Return a list of 3-tuples with (name, description, defaultValue) for each
  argument to the function.

Assigns all arguments to the function as instance variables of SPRegion.
If the argument was not provided, uses the default value.

Pops any values from kwargs that go to the function.


typeNames = {int: '<STR_LIT>', float: '<STR_LIT>', str: '<STR_LIT>', bool: '<STR_LIT:bool>', tuple: '<STR_LIT>'}<EOL>def getArgType(arg):<EOL><INDENT>t = typeNames.get(type(arg), '<STR_LIT>')<EOL>count = <NUM_LIT:0> if t == '<STR_LIT>' else <NUM_LIT:1><EOL>if t == '<STR_LIT>':<EOL><INDENT>t = typeNames.get(type(arg[<NUM_LIT:0>]), '<STR_LIT>')<EOL>count = len(arg)<EOL><DEDENT>if t == '<STR_LIT:bool>':<EOL><INDENT>t = '<STR_LIT>'<EOL><DEDENT>return (t, count)<EOL><DEDENT>def getConstraints(arg):<EOL><INDENT>t = typeNames.get(type(arg), '<STR_LIT>')<EOL>if t == '<STR_LIT>':<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>elif t == '<STR_LIT:bool>':<EOL><INDENT>return '<STR_LIT:bool>'<EOL><DEDENT>else:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT><DEDENT>SpatialClass = getSPClass(spatialImp)<EOL>sArgTuples = _buildArgs(SpatialClass.__init__)<EOL>spatialSpec = {}<EOL>for argTuple in sArgTuples:<EOL><INDENT>d = dict(<EOL>description=argTuple[<NUM_LIT:1>],<EOL>accessMode='<STR_LIT>',<EOL>dataType=getArgType(argTuple[<NUM_LIT:2>])[<NUM_LIT:0>],<EOL>count=getArgType(argTuple[<NUM_LIT:2>])[<NUM_LIT:1>],<EOL>constraints=getConstraints(argTuple[<NUM_LIT:2>]))<EOL>spatialSpec[argTuple[<NUM_LIT:0>]] = d<EOL><DEDENT>spatialSpec.update(dict(<EOL>columnCount=dict(<EOL>description='<STR_LIT>',<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:1>,<EOL>constraints='<STR_LIT>'),<EOL>inputWidth=dict(<EOL>description='<STR_LIT>',<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:1>,<EOL>constraints='<STR_LIT>'),<EOL>spInputNonZeros=dict(<EOL>description='<STR_LIT>',<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:0>,<EOL>constraints='<STR_LIT>'),<EOL>spOutputNonZeros=dict(<EOL>description='<STR_LIT>',<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:0>,<EOL>constraints='<STR_LIT>'),<EOL>spOverlapDistribution=dict(<EOL>description="""<STR_LIT>""",<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:0>,<EOL>constraints='<STR_LIT>'),<EOL>sparseCoincidenceMatrix=dict(<EOL>description='<STR_LIT>',<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:0>,<EOL>constraints='<STR_LIT>'),<EOL>denseOutput=dict(<EOL>description='<STR_LIT>',<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:0>,<EOL>constraints='<STR_LIT>'),<EOL>spLearningStatsStr=dict(<EOL>description="""<STR_LIT>""",<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:0>,<EOL>constraints='<STR_LIT>'),<EOL>spatialImp=dict(<EOL>description="""<STR_LIT>""",<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:0>,<EOL>constraints='<STR_LIT>'),<EOL>))<EOL>otherSpec = dict(<EOL>learningMode=dict(<EOL>description='<STR_LIT>',<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:1>,<EOL>constraints='<STR_LIT:bool>'),<EOL>inferenceMode=dict(<EOL>description='<STR_LIT>',<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:1>,<EOL>constraints='<STR_LIT:bool>'),<EOL>anomalyMode=dict(<EOL>description='<STR_LIT>',<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:1>,<EOL>constraints='<STR_LIT:bool>'),<EOL>topDownMode=dict(<EOL>description='<STR_LIT>'<EOL>'<STR_LIT>',<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:1>,<EOL>constraints='<STR_LIT:bool>'),<EOL>activeOutputCount=dict(<EOL>description='<STR_LIT>',<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:1>,<EOL>constraints='<STR_LIT>'),<EOL>logPathInput=dict(<EOL>description='<STR_LIT>'<EOL>'<STR_LIT>',<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:0>,<EOL>constraints='<STR_LIT>'),<EOL>logPathOutput=dict(<EOL>description='<STR_LIT>'<EOL>'<STR_LIT>',<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:0>,<EOL>constraints='<STR_LIT>'),<EOL>logPathOutputDense=dict(<EOL>description='<STR_LIT>'<EOL>'<STR_LIT>',<EOL>accessMode='<STR_LIT>',<EOL>dataType='<STR_LIT>',<EOL>count=<NUM_LIT:0>,<EOL>constraints='<STR_LIT>'),<EOL>)<EOL>return spatialSpec, otherSpec<EOL>

Build the additional specs in three groups (for the inspector)

    Use the type of the default argument to set the Spec type, defaulting
    to 'Byte' for None and complex types

    Determines the spatial parameters based on the selected implementation.
    It defaults to SpatialPooler.


for attrName in self._getEphemeralMembersBase():<EOL><INDENT>if attrName != "<STR_LIT>":<EOL><INDENT>if hasattr(self, attrName):<EOL><INDENT>if self._loaded:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>print(self.__class__.__name__, "<STR_LIT>" %attrName)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if not self._loaded:<EOL><INDENT>for attrName in self._getEphemeralMembersBase():<EOL><INDENT>if attrName != "<STR_LIT>":<EOL><INDENT>assert not hasattr(self, attrName)<EOL><DEDENT>else:<EOL><INDENT>assert hasattr(self, attrName)<EOL><DEDENT><DEDENT><DEDENT>self._profileObj = None<EOL>self._iterations = <NUM_LIT:0><EOL>self._initEphemerals()<EOL>self._checkEphemeralMembers()<EOL>

Initialize all ephemeral data members, and give the derived class the
opportunity to do the same by invoking the virtual member _initEphemerals(),
which is intended to be overridden.

NOTE: this is used by both __init__ and __setstate__ code paths.


<EOL>self._spatialPoolerOutput = numpy.zeros(self.columnCount,<EOL>dtype=GetNTAReal())<EOL>self._spatialPoolerInput = numpy.zeros((<NUM_LIT:1>, self.inputWidth),<EOL>dtype=GetNTAReal())<EOL>self._allocateSpatialFDR(None)<EOL>


if self._sfdr:<EOL><INDENT>return<EOL><DEDENT>autoArgs = dict((name, getattr(self, name))<EOL>for name in self._spatialArgNames)<EOL>if ( (self.SpatialClass == CPPSpatialPooler) or<EOL>(self.SpatialClass == PYSpatialPooler) ):<EOL><INDENT>autoArgs['<STR_LIT>'] = [self.columnCount]<EOL>autoArgs['<STR_LIT>'] = [self.inputWidth]<EOL>autoArgs['<STR_LIT>'] = self.inputWidth<EOL>self._sfdr = self.SpatialClass(<EOL>**autoArgs<EOL>)<EOL><DEDENT>


<EOL>if False and self.learningModeand self._iterations > <NUM_LIT:0> and self._iterations <= <NUM_LIT:10>:<EOL><INDENT>import hotshot<EOL>if self._iterations == <NUM_LIT:10>:<EOL><INDENT>print("<STR_LIT>")<EOL>stats = hotshot.stats.load("<STR_LIT>")<EOL>stats.strip_dirs()<EOL>stats.sort_stats('<STR_LIT:time>', '<STR_LIT>')<EOL>stats.print_stats()<EOL><DEDENT>if self._profileObj is None:<EOL><INDENT>print("<STR_LIT>")<EOL>if os.path.exists('<STR_LIT>'):<EOL><INDENT>os.remove('<STR_LIT>')<EOL><DEDENT>self._profileObj = hotshot.Profile("<STR_LIT>", <NUM_LIT:1>, <NUM_LIT:1>)<EOL><DEDENT>self._profileObj.runcall(self._compute, *[inputs, outputs])<EOL><DEDENT>else:<EOL><INDENT>self._compute(inputs, outputs)<EOL><DEDENT>

Run one iteration, profiling it if requested.

:param inputs: (dict) mapping region input names to numpy.array values
:param outputs: (dict) mapping region output names to numpy.arrays that
       should be populated with output values by this method


<EOL>if self._sfdr is None:<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>if not self.topDownMode:<EOL><INDENT>self._iterations += <NUM_LIT:1><EOL>buInputVector = inputs['<STR_LIT>']<EOL>resetSignal = False<EOL>if '<STR_LIT>' in inputs:<EOL><INDENT>assert len(inputs['<STR_LIT>']) == <NUM_LIT:1><EOL>resetSignal = inputs['<STR_LIT>'][<NUM_LIT:0>] != <NUM_LIT:0><EOL><DEDENT>rfOutput = self._doBottomUpCompute(<EOL>rfInput = buInputVector.reshape((<NUM_LIT:1>,buInputVector.size)),<EOL>resetSignal = resetSignal<EOL>)<EOL>outputs['<STR_LIT>'][:] = rfOutput.flat<EOL><DEDENT>else:<EOL><INDENT>topDownIn = inputs.get('<STR_LIT>',None)<EOL>spatialTopDownOut, temporalTopDownOut = self._doTopDownInfer(topDownIn)<EOL>outputs['<STR_LIT>'][:] = spatialTopDownOut<EOL>if temporalTopDownOut is not None:<EOL><INDENT>outputs['<STR_LIT>'][:] = temporalTopDownOut<EOL><DEDENT><DEDENT>outputs['<STR_LIT>'][:] = <NUM_LIT:0><EOL>


def _doBottomUpCompute(self, rfInput, resetSignal):

<EOL>self._conditionalBreak()<EOL>self._spatialPoolerInput = rfInput.reshape(-<NUM_LIT:1>)<EOL>assert(rfInput.shape[<NUM_LIT:0>] == <NUM_LIT:1>)<EOL>inputVector = numpy.array(rfInput[<NUM_LIT:0>]).astype('<STR_LIT>')<EOL>outputVector = numpy.zeros(self._sfdr.getNumColumns()).astype('<STR_LIT>')<EOL>self._sfdr.compute(inputVector, self.learningMode, outputVector)<EOL>self._spatialPoolerOutput[:] = outputVector[:]<EOL>if self._fpLogSP:<EOL><INDENT>output = self._spatialPoolerOutput.reshape(-<NUM_LIT:1>)<EOL>outputNZ = output.nonzero()[<NUM_LIT:0>]<EOL>outStr = "<STR_LIT:U+0020>".join(["<STR_LIT>" % int(token) for token in outputNZ])<EOL>print(output.size, outStr, file=self._fpLogSP)<EOL><DEDENT>if self._fpLogSPInput:<EOL><INDENT>output = rfInput.reshape(-<NUM_LIT:1>)<EOL>outputNZ = output.nonzero()[<NUM_LIT:0>]<EOL>outStr = "<STR_LIT:U+0020>".join(["<STR_LIT>" % int(token) for token in outputNZ])<EOL>print(output.size, outStr, file=self._fpLogSPInput)<EOL><DEDENT>return self._spatialPoolerOutput<EOL>

Do one iteration of inference and/or learning and return the result

Parameters:
--------------------------------------------
rfInput:      Input vector. Shape is: (1, inputVectorLen).
resetSignal:  True if reset is asserted


def _doTopDownInfer(self, topDownInput = None):

return None, None<EOL>

Do one iteration of top-down inference.

Parameters:
--------------------------------------------
tdInput:      Top-down input

retval:     (spatialTopDownOut, temporalTopDownOut)
              spatialTopDownOut is the top down output computed only from the SP,
                using it's current bottom-up output.
              temporalTopDownOut is the top down output computed from the topDown in
               of the level above us.


Doesn't include the spatial, temporal and other parameters

:returns: (dict) The base Spec for SPRegion.


spec = cls.getBaseSpec()<EOL>s, o = _getAdditionalSpecs(spatialImp=getDefaultSPImp())<EOL>spec['<STR_LIT>'].update(s)<EOL>spec['<STR_LIT>'].update(o)<EOL>return spec<EOL>

Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.getSpec`.

The parameters collection is constructed based on the parameters specified
by the various components (spatialSpec, temporalSpec and otherSpec)


return self._sfdr<EOL>

:returns: (:class:`~nupic.algorithms.spatial_pooler.SpatialPooler`) instance
          of the underlying algorithm object.


def getParameter(self, parameterName, index=-<NUM_LIT:1>):

if parameterName == '<STR_LIT>':<EOL><INDENT>return self.columnCount<EOL><DEDENT>elif parameterName == '<STR_LIT>':<EOL><INDENT>return list(self._spatialPoolerInput.reshape(-<NUM_LIT:1>))<EOL><DEDENT>elif parameterName == '<STR_LIT>':<EOL><INDENT>return list(self._spatialPoolerOutput)<EOL><DEDENT>elif parameterName == '<STR_LIT>':<EOL><INDENT>return len(self._spatialPoolerOutput.nonzero()[<NUM_LIT:0>])<EOL><DEDENT>elif parameterName == '<STR_LIT>':<EOL><INDENT>return [len(self._spatialPoolerOutput)] +list(self._spatialPoolerOutput.nonzero()[<NUM_LIT:0>])<EOL><DEDENT>elif parameterName == '<STR_LIT>':<EOL><INDENT>import pdb; pdb.set_trace()<EOL>return [len(self._spatialPoolerInput)] +list(self._spatialPoolerInput.nonzero()[<NUM_LIT:0>])<EOL><DEDENT>elif parameterName == '<STR_LIT>':<EOL><INDENT>try:<EOL><INDENT>return str(self._sfdr.getLearningStats())<EOL><DEDENT>except:<EOL><INDENT>return str(dict())<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return PyRegion.getParameter(self, parameterName, index)<EOL><DEDENT>

Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.getParameter`.

Most parameters are handled automatically by PyRegion's parameter get
mechanism. The ones that need special treatment are explicitly handled here.


def setParameter(self, parameterName, index, parameterValue):

if parameterName in self._spatialArgNames:<EOL><INDENT>setattr(self._sfdr, parameterName, parameterValue)<EOL><DEDENT>elif parameterName == "<STR_LIT>":<EOL><INDENT>self.logPathInput = parameterValue<EOL>if self._fpLogSPInput:<EOL><INDENT>self._fpLogSPInput.close()<EOL>self._fpLogSPInput = None<EOL><DEDENT>if parameterValue:<EOL><INDENT>self._fpLogSPInput = open(self.logPathInput, '<STR_LIT:w>')<EOL><DEDENT><DEDENT>elif parameterName == "<STR_LIT>":<EOL><INDENT>self.logPathOutput = parameterValue<EOL>if self._fpLogSP:<EOL><INDENT>self._fpLogSP.close()<EOL>self._fpLogSP = None<EOL><DEDENT>if parameterValue:<EOL><INDENT>self._fpLogSP = open(self.logPathOutput, '<STR_LIT:w>')<EOL><DEDENT><DEDENT>elif parameterName == "<STR_LIT>":<EOL><INDENT>self.logPathOutputDense = parameterValue<EOL>if self._fpLogSPDense:<EOL><INDENT>self._fpLogSPDense.close()<EOL>self._fpLogSPDense = None<EOL><DEDENT>if parameterValue:<EOL><INDENT>self._fpLogSPDense = open(self.logPathOutputDense, '<STR_LIT:w>')<EOL><DEDENT><DEDENT>elif hasattr(self, parameterName):<EOL><INDENT>setattr(self, parameterName, parameterValue)<EOL><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>' + parameterName)<EOL><DEDENT>

Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.setParameter`.

Set the value of a Spec parameter. Most parameters are handled
automatically by PyRegion's parameter set mechanism. The ones that need
special treatment are explicitly handled here.


return SPRegionProto<EOL>


proto.spatialImp = self.spatialImp<EOL>proto.columnCount = self.columnCount<EOL>proto.inputWidth = self.inputWidth<EOL>proto.learningMode = <NUM_LIT:1> if self.learningMode else <NUM_LIT:0><EOL>proto.inferenceMode = <NUM_LIT:1> if self.inferenceMode else <NUM_LIT:0><EOL>proto.anomalyMode = <NUM_LIT:1> if self.anomalyMode else <NUM_LIT:0><EOL>proto.topDownMode = <NUM_LIT:1> if self.topDownMode else <NUM_LIT:0><EOL>self._sfdr.write(proto.spatialPooler)<EOL>

Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.writeToProto`.

Write state to proto object.

:param proto: SPRegionProto capnproto object


instance = cls(proto.columnCount, proto.inputWidth)<EOL>instance.spatialImp = proto.spatialImp<EOL>instance.learningMode = proto.learningMode<EOL>instance.inferenceMode = proto.inferenceMode<EOL>instance.anomalyMode = proto.anomalyMode<EOL>instance.topDownMode = proto.topDownMode<EOL>spatialImp = proto.spatialImp<EOL>instance._sfdr = getSPClass(spatialImp).read(proto.spatialPooler)<EOL>return instance<EOL>

Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.readFromProto`.

Read state from proto object.

:param proto: SPRegionProto capnproto object


state = self.__dict__.copy()<EOL>for ephemeralMemberName in self._getEphemeralMembersAll():<EOL><INDENT>state.pop(ephemeralMemberName, None)<EOL><DEDENT>return state<EOL>

Return serializable state.  This function will return a version of the
__dict__ with all "ephemeral" members stripped out.  "Ephemeral" members
are defined as those that do not need to be (nor should be) stored
in any kind of persistent file (e.g., NuPIC network XML file.)


self.__dict__.update(state)<EOL>self._loaded = True<EOL>if not hasattr(self, "<STR_LIT>"):<EOL><INDENT>self.SpatialClass = self._sfdr.__class__<EOL><DEDENT>self._initializeEphemeralMembers()<EOL>self._allocateSpatialFDR(None)<EOL>

Set the state of ourself from a serialized state.


if hasattr(self, '<STR_LIT>') and self._sfdr:<EOL><INDENT>self._spatialPoolerOutput = numpy.zeros(self.columnCount,<EOL>dtype=GetNTAReal())<EOL><DEDENT>else:<EOL><INDENT>self._spatialPoolerOutput = None  <EOL><DEDENT>self._fpLogSPInput = None<EOL>self._fpLogSP = None<EOL>self._fpLogSPDense = None<EOL>self.logPathInput = "<STR_LIT>"<EOL>self.logPathOutput = "<STR_LIT>"<EOL>self.logPathOutputDense = "<STR_LIT>"<EOL>

Initialize all ephemerals used by derived classes.


return ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>'<EOL>]<EOL>

Callback that returns a list of all "ephemeral" members (i.e., data members
that should not and/or cannot be pickled.)


return [<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>]<EOL>


return self._getEphemeralMembersBase() + self._getEphemeralMembers()<EOL>

Returns a concatenated list of both the standard base class
ephemeral members, as well as any additional ephemeral members
(e.g., file handles, etc.).


def getParameterArrayCount(self, name, index):

p = self.getParameter(name)<EOL>if (not hasattr(p, '<STR_LIT>')):<EOL><INDENT>raise Exception("<STR_LIT>" % name)<EOL><DEDENT>return len(p)<EOL>

Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.getParameterArrayCount`.

TODO: as a temporary hack, getParameterArrayCount checks to see if there's a
variable, private or not, with that name. If so, it returns the value of the
variable.


def getParameterArray(self, name, index, a):

p = self.getParameter(name)<EOL>if (not hasattr(p, '<STR_LIT>')):<EOL><INDENT>raise Exception("<STR_LIT>" % name)<EOL><DEDENT>if len(p) >  <NUM_LIT:0>:<EOL><INDENT>a[:] = p[:]<EOL><DEDENT>

Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.getParameterArray`.

TODO: as a temporary hack, getParameterArray checks to see if there's a
variable, private or not, with that name. If so, it returns the value of the
variable.


def getParameter(self, name, index=-<NUM_LIT:1>):

if name == "<STR_LIT>":<EOL><INDENT>return self.trainRecords<EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>return self.anomalyThreshold<EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>return self._activeColumnCount<EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>return self._classificationMaxDist<EOL><DEDENT>else:<EOL><INDENT>return PyRegion.getParameter(self, name, index)<EOL><DEDENT>


def setParameter(self, name, index, value):

if name == "<STR_LIT>":<EOL><INDENT>if not (isinstance(value, float) or isinstance(value, int)):<EOL><INDENT>raise HTMPredictionModelInvalidArgument("<STR_LIT>"<EOL>"<STR_LIT>" % (type(value)))<EOL><DEDENT>if len(self._recordsCache) > <NUM_LIT:0> and value < self._recordsCache[<NUM_LIT:0>].ROWID:<EOL><INDENT>raise HTMPredictionModelInvalidArgument("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % (self._recordsCache[<NUM_LIT:0>].ROWID))<EOL><DEDENT>self.trainRecords = value<EOL>self._deleteRangeFromKNN(<NUM_LIT:0>, self._recordsCache[<NUM_LIT:0>].ROWID)<EOL>self._classifyStates()<EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>if not (isinstance(value, float) or isinstance(value, int)):<EOL><INDENT>raise HTMPredictionModelInvalidArgument("<STR_LIT>"<EOL>"<STR_LIT>" % (type(value)))<EOL><DEDENT>self.anomalyThreshold = value<EOL>self._classifyStates()<EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>if not (isinstance(value, float) or isinstance(value, int)):<EOL><INDENT>raise HTMPredictionModelInvalidArgument("<STR_LIT>"<EOL>"<STR_LIT>" % (type(value)))<EOL><DEDENT>self._classificationMaxDist = value<EOL>self._classifyStates()<EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>self._activeColumnCount = value<EOL><DEDENT>else:<EOL><INDENT>return PyRegion.setParameter(self, name, index, value)<EOL><DEDENT>


record = self._constructClassificationRecord(inputs)<EOL>if record.ROWID >= self.getParameter('<STR_LIT>'):<EOL><INDENT>self._classifyState(record)<EOL><DEDENT>self._recordsCache.append(record)<EOL>while len(self._recordsCache) > self.cacheSize:<EOL><INDENT>self._recordsCache.pop(<NUM_LIT:0>)<EOL><DEDENT>self.labelResults = record.anomalyLabel<EOL>self._iteration += <NUM_LIT:1><EOL>

Process one input sample.
This method is called by the runtime engine.


return self.labelResults<EOL>

Get the labels of the previously computed record.

:returns: (list) of strings representing the classification labels


for state in self._recordsCache:<EOL><INDENT>self._classifyState(state)<EOL><DEDENT>


<EOL>if state.ROWID < self.getParameter('<STR_LIT>'):<EOL><INDENT>if not state.setByUser:<EOL><INDENT>state.anomalyLabel = []<EOL>self._deleteRecordsFromKNN([state])<EOL><DEDENT>return<EOL><DEDENT>label = KNNAnomalyClassifierRegion.AUTO_THRESHOLD_CLASSIFIED_LABEL<EOL>autoLabel = label + KNNAnomalyClassifierRegion.AUTO_TAG<EOL>newCategory = self._recomputeRecordFromKNN(state)<EOL>labelList = self._categoryToLabelList(newCategory)<EOL>if state.setByUser:<EOL><INDENT>if label in state.anomalyLabel:<EOL><INDENT>state.anomalyLabel.remove(label)<EOL><DEDENT>if autoLabel in state.anomalyLabel:<EOL><INDENT>state.anomalyLabel.remove(autoLabel)<EOL><DEDENT>labelList.extend(state.anomalyLabel)<EOL><DEDENT>if state.anomalyScore >= self.getParameter('<STR_LIT>'):<EOL><INDENT>labelList.append(label)<EOL><DEDENT>elif label in labelList:<EOL><INDENT>ind = labelList.index(label)<EOL>labelList[ind] = autoLabel<EOL><DEDENT>labelList = list(set(labelList))<EOL>if label in labelList and autoLabel in labelList:<EOL><INDENT>labelList.remove(autoLabel)<EOL><DEDENT>if state.anomalyLabel == labelList:<EOL><INDENT>return<EOL><DEDENT>state.anomalyLabel = labelList<EOL>if state.anomalyLabel == []:<EOL><INDENT>self._deleteRecordsFromKNN([state])<EOL><DEDENT>else:<EOL><INDENT>self._addRecordToKNN(state)<EOL><DEDENT>


<EOL>allSPColumns = inputs["<STR_LIT>"]<EOL>activeSPColumns = allSPColumns.nonzero()[<NUM_LIT:0>]<EOL>score = anomaly.computeRawAnomalyScore(activeSPColumns,<EOL>self._prevPredictedColumns)<EOL>spSize = len(allSPColumns)<EOL>allTPCells = inputs['<STR_LIT>']<EOL>tpSize = len(inputs['<STR_LIT>'])<EOL>classificationVector = numpy.array([])<EOL>if self.classificationVectorType == <NUM_LIT:1>:<EOL><INDENT>classificationVector = numpy.zeros(tpSize)<EOL>activeCellMatrix = inputs["<STR_LIT>"].reshape(tpSize, <NUM_LIT:1>)<EOL>activeCellIdx = numpy.where(activeCellMatrix > <NUM_LIT:0>)[<NUM_LIT:0>]<EOL>if activeCellIdx.shape[<NUM_LIT:0>] > <NUM_LIT:0>:<EOL><INDENT>classificationVector[numpy.array(activeCellIdx, dtype=numpy.uint16)] = <NUM_LIT:1><EOL><DEDENT><DEDENT>elif self.classificationVectorType == <NUM_LIT:2>:<EOL><INDENT>classificationVector = numpy.zeros(spSize+spSize)<EOL>if activeSPColumns.shape[<NUM_LIT:0>] > <NUM_LIT:0>:<EOL><INDENT>classificationVector[activeSPColumns] = <NUM_LIT:1.0><EOL><DEDENT>errorColumns = numpy.setdiff1d(self._prevPredictedColumns,<EOL>activeSPColumns)<EOL>if errorColumns.shape[<NUM_LIT:0>] > <NUM_LIT:0>:<EOL><INDENT>errorColumnIndexes = ( numpy.array(errorColumns, dtype=numpy.uint16) +<EOL>spSize )<EOL>classificationVector[errorColumnIndexes] = <NUM_LIT:1.0><EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise TypeError("<STR_LIT>"<EOL>"<STR_LIT>" % (self.classificationVectorType))<EOL><DEDENT>numPredictedCols = len(self._prevPredictedColumns)<EOL>predictedColumns = allTPCells.nonzero()[<NUM_LIT:0>]<EOL>self._prevPredictedColumns = copy.deepcopy(predictedColumns)<EOL>if self._anomalyVectorLength is None:<EOL><INDENT>self._anomalyVectorLength = len(classificationVector)<EOL><DEDENT>result = _CLAClassificationRecord(<EOL>ROWID=self._iteration, <EOL>anomalyScore=score,<EOL>anomalyVector=classificationVector.nonzero()[<NUM_LIT:0>].tolist(),<EOL>anomalyLabel=[]<EOL>)<EOL>return result<EOL>

Construct a _HTMClassificationRecord based on the state of the model
passed in through the inputs.

Types for self.classificationVectorType:
  1 - TM active cells in learn state
  2 - SP columns concatenated with error from TM column predictions and SP


knn = self._knnclassifier._knn<EOL>prototype_idx = self._knnclassifier.getParameter('<STR_LIT>')<EOL>category = self._labelListToCategoryNumber(record.anomalyLabel)<EOL>if record.ROWID in prototype_idx:<EOL><INDENT>knn.prototypeSetCategory(record.ROWID, category)<EOL>return<EOL><DEDENT>pattern = self._getStateAnomalyVector(record)<EOL>rowID = record.ROWID<EOL>knn.learn(pattern, category, rowID=rowID)<EOL>


prototype_idx = self._knnclassifier.getParameter('<STR_LIT>')<EOL>idsToDelete = ([r.ROWID for r in recordsToDelete if<EOL>not r.setByUser and r.ROWID in prototype_idx])<EOL>nProtos = self._knnclassifier._knn._numPatterns<EOL>self._knnclassifier._knn.removeIds(idsToDelete)<EOL>assert self._knnclassifier._knn._numPatterns == nProtos - len(idsToDelete)<EOL>

Removes the given records from the classifier.

parameters
------------
recordsToDelete - list of records to delete from the classififier


def _deleteRangeFromKNN(self, start=<NUM_LIT:0>, end=None):

prototype_idx = numpy.array(<EOL>self._knnclassifier.getParameter('<STR_LIT>'))<EOL>if end is None:<EOL><INDENT>end = prototype_idx.max() + <NUM_LIT:1><EOL><DEDENT>idsIdxToDelete = numpy.logical_and(prototype_idx >= start,<EOL>prototype_idx < end)<EOL>idsToDelete = prototype_idx[idsIdxToDelete]<EOL>nProtos = self._knnclassifier._knn._numPatterns<EOL>self._knnclassifier._knn.removeIds(idsToDelete.tolist())<EOL>assert self._knnclassifier._knn._numPatterns == nProtos - len(idsToDelete)<EOL>

Removes any stored records within the range from start to
end. Noninclusive of end.

parameters
------------
start - integer representing the ROWID of the start of the deletion range,
end - integer representing the ROWID of the end of the deletion range,
  if None, it will default to end.


inputs = {<EOL>"<STR_LIT>": [None],<EOL>"<STR_LIT>": self._getStateAnomalyVector(record),<EOL>}<EOL>outputs = {"<STR_LIT>": numpy.zeros((<NUM_LIT:1>,)),<EOL>"<STR_LIT>":numpy.zeros((<NUM_LIT:1>,)),<EOL>"<STR_LIT>":numpy.zeros((<NUM_LIT:1>,))}<EOL>classifier_indexes = numpy.array(<EOL>self._knnclassifier.getParameter('<STR_LIT>'))<EOL>valid_idx = numpy.where(<EOL>(classifier_indexes >= self.getParameter('<STR_LIT>')) &<EOL>(classifier_indexes < record.ROWID)<EOL>)[<NUM_LIT:0>].tolist()<EOL>if len(valid_idx) == <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT>self._knnclassifier.setParameter('<STR_LIT>', None, True)<EOL>self._knnclassifier.setParameter('<STR_LIT>', None, False)<EOL>self._knnclassifier.compute(inputs, outputs)<EOL>self._knnclassifier.setParameter('<STR_LIT>', None, True)<EOL>classifier_distances = self._knnclassifier.getLatestDistances()<EOL>valid_distances = classifier_distances[valid_idx]<EOL>if valid_distances.min() <= self._classificationMaxDist:<EOL><INDENT>classifier_indexes_prev = classifier_indexes[valid_idx]<EOL>rowID = classifier_indexes_prev[valid_distances.argmin()]<EOL>indexID = numpy.where(classifier_indexes == rowID)[<NUM_LIT:0>][<NUM_LIT:0>]<EOL>category = self._knnclassifier.getCategoryList()[indexID]<EOL>return category<EOL><DEDENT>return None<EOL>

returns the classified labeling of record


if label not in self.saved_categories:<EOL><INDENT>self.saved_categories.append(label)<EOL><DEDENT>return pow(<NUM_LIT:2>, self.saved_categories.index(label))<EOL>

Since the KNN Classifier stores categories as numbers, we must store each
label as a number. This method converts from a label to a unique number.
Each label is assigned a unique bit so multiple labels may be assigned to
a single record.


categoryNumber = <NUM_LIT:0><EOL>for label in labelList:<EOL><INDENT>categoryNumber += self._labelToCategoryNumber(label)<EOL><DEDENT>return categoryNumber<EOL>

This method takes a list of labels and returns a unique category number.
This enables this class to store a list of categories for each point since
the KNN classifier only stores a single number category for each record.


if category is None:<EOL><INDENT>return []<EOL><DEDENT>labelList = []<EOL>labelNum = <NUM_LIT:0><EOL>while category > <NUM_LIT:0>:<EOL><INDENT>if category % <NUM_LIT:2> == <NUM_LIT:1>:<EOL><INDENT>labelList.append(self.saved_categories[labelNum])<EOL><DEDENT>labelNum += <NUM_LIT:1><EOL>category = category >> <NUM_LIT:1><EOL><DEDENT>return labelList<EOL>

Converts a category number into a list of labels


vector = numpy.zeros(self._anomalyVectorLength)<EOL>vector[state.anomalyVector] = <NUM_LIT:1><EOL>return vector<EOL>

Returns a state's anomaly vertor converting it from spare to dense


def getLabels(self, start=None, end=None):

if len(self._recordsCache) == <NUM_LIT:0>:<EOL><INDENT>return {<EOL>'<STR_LIT>': False,<EOL>'<STR_LIT>': []<EOL>}<EOL><DEDENT>try:<EOL><INDENT>start = int(start)<EOL><DEDENT>except Exception:<EOL><INDENT>start = <NUM_LIT:0><EOL><DEDENT>try:<EOL><INDENT>end = int(end)<EOL><DEDENT>except Exception:<EOL><INDENT>end = self._recordsCache[-<NUM_LIT:1>].ROWID<EOL><DEDENT>if end <= start:<EOL><INDENT>raise HTMPredictionModelInvalidRangeError("<STR_LIT>",<EOL>debugInfo={<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': start,<EOL>'<STR_LIT>': end<EOL>},<EOL>'<STR_LIT>': len(self._recordsCache)<EOL>})<EOL><DEDENT>results = {<EOL>'<STR_LIT>': False,<EOL>'<STR_LIT>': []<EOL>}<EOL>ROWIDX = numpy.array(<EOL>self._knnclassifier.getParameter('<STR_LIT>'))<EOL>validIdx = numpy.where((ROWIDX >= start) & (ROWIDX < end))[<NUM_LIT:0>].tolist()<EOL>categories = self._knnclassifier.getCategoryList()<EOL>for idx in validIdx:<EOL><INDENT>row = dict(<EOL>ROWID=int(ROWIDX[idx]),<EOL>labels=self._categoryToLabelList(categories[idx]))<EOL>results['<STR_LIT>'].append(row)<EOL><DEDENT>return results<EOL>

Get the labels on classified points within range start to end. Not inclusive
of end.

:returns: (dict) with format:

  ::

    {
      'isProcessing': boolean,
      'recordLabels': list of results
    }

  ``isProcessing`` - currently always false as recalculation blocks; used if
  reprocessing of records is still being performed;

  Each item in ``recordLabels`` is of format:

  ::

    {
      'ROWID': id of the row,
      'labels': list of strings
    }


def addLabel(self, start, end, labelName):

if len(self._recordsCache) == <NUM_LIT:0>:<EOL><INDENT>raise HTMPredictionModelInvalidRangeError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>try:<EOL><INDENT>start = int(start)<EOL><DEDENT>except Exception:<EOL><INDENT>start = <NUM_LIT:0><EOL><DEDENT>try:<EOL><INDENT>end = int(end)<EOL><DEDENT>except Exception:<EOL><INDENT>end = int(self._recordsCache[-<NUM_LIT:1>].ROWID)<EOL><DEDENT>startID = self._recordsCache[<NUM_LIT:0>].ROWID<EOL>clippedStart = max(<NUM_LIT:0>, start - startID)<EOL>clippedEnd = max(<NUM_LIT:0>, min( len( self._recordsCache) , end - startID))<EOL>if clippedEnd <= clippedStart:<EOL><INDENT>raise HTMPredictionModelInvalidRangeError("<STR_LIT>",<EOL>debugInfo={<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': start,<EOL>'<STR_LIT>': end<EOL>},<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': clippedStart,<EOL>'<STR_LIT>': clippedEnd<EOL>},<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': startID,<EOL>'<STR_LIT>': self._recordsCache[len(self._recordsCache)-<NUM_LIT:1>].ROWID<EOL>},<EOL>'<STR_LIT>': len(self._recordsCache)<EOL>})<EOL><DEDENT>for state in self._recordsCache[clippedStart:clippedEnd]:<EOL><INDENT>if labelName not in state.anomalyLabel:<EOL><INDENT>state.anomalyLabel.append(labelName)<EOL>state.setByUser = True<EOL>self._addRecordToKNN(state)<EOL><DEDENT><DEDENT>assert len(self.saved_categories) > <NUM_LIT:0><EOL>for state in self._recordsCache[clippedEnd:]:<EOL><INDENT>self._classifyState(state)<EOL><DEDENT>

Add the label labelName to each record with record ROWID in range from
``start`` to ``end``, noninclusive of end.

This will recalculate all points from end to the last record stored in the
internal cache of this classifier.

:param start: (int) start index
:param end: (int) end index (noninclusive)
:param labelName: (string) label name


def removeLabels(self, start=None, end=None, labelFilter=None):

if len(self._recordsCache) == <NUM_LIT:0>:<EOL><INDENT>raise HTMPredictionModelInvalidRangeError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>try:<EOL><INDENT>start = int(start)<EOL><DEDENT>except Exception:<EOL><INDENT>start = <NUM_LIT:0><EOL><DEDENT>try:<EOL><INDENT>end = int(end)<EOL><DEDENT>except Exception:<EOL><INDENT>end = self._recordsCache[-<NUM_LIT:1>].ROWID<EOL><DEDENT>startID = self._recordsCache[<NUM_LIT:0>].ROWID<EOL>clippedStart = <NUM_LIT:0> if start is None else max(<NUM_LIT:0>, start - startID)<EOL>clippedEnd = len(self._recordsCache) if end is None elsemax(<NUM_LIT:0>, min( len( self._recordsCache) , end - startID))<EOL>if clippedEnd <= clippedStart:<EOL><INDENT>raise HTMPredictionModelInvalidRangeError("<STR_LIT>"<EOL>"<STR_LIT>", debugInfo={<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': start,<EOL>'<STR_LIT>': end<EOL>},<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': clippedStart,<EOL>'<STR_LIT>': clippedEnd<EOL>},<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': startID,<EOL>'<STR_LIT>': self._recordsCache[len(self._recordsCache)-<NUM_LIT:1>].ROWID<EOL>},<EOL>'<STR_LIT>': len(self._recordsCache)<EOL>})<EOL><DEDENT>recordsToDelete = []<EOL>for state in self._recordsCache[clippedStart:clippedEnd]:<EOL><INDENT>if labelFilter is not None:<EOL><INDENT>if labelFilter in state.anomalyLabel:<EOL><INDENT>state.anomalyLabel.remove(labelFilter)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>state.anomalyLabel = []<EOL><DEDENT>state.setByUser = False<EOL>recordsToDelete.append(state)<EOL><DEDENT>self._deleteRecordsFromKNN(recordsToDelete)<EOL>self._deleteRangeFromKNN(start, end)<EOL>for state in self._recordsCache[clippedEnd:]:<EOL><INDENT>self._classifyState(state)<EOL><DEDENT>

Remove labels from each record with record ROWID in range from
``start`` to ``end``, noninclusive of end. Removes all records if
``labelFilter`` is None, otherwise only removes the labels equal to
``labelFilter``.

This will recalculate all points from end to the last record stored in the
internal cache of this classifier.

:param start: (int) start index
:param end: (int) end index (noninclusive)
:param labelFilter: (string) label filter


state = self.__dict__.copy()<EOL>state['<STR_LIT>'] = state['<STR_LIT>'].__getstate__()<EOL>state.pop('<STR_LIT>')<EOL>return state<EOL>

Return serializable state.  This function will return a version of the
__dict__ with all "ephemeral" members stripped out.  "Ephemeral" members
are defined as those that do not need to be (nor should be) stored
in any kind of persistent file (e.g., NuPIC network XML file.)


if '<STR_LIT>' not in state or state['<STR_LIT>'] == <NUM_LIT:1>:<EOL><INDENT>knnclassifierProps = state.pop('<STR_LIT>')<EOL>self.__dict__.update(state)<EOL>self._knnclassifier = KNNClassifierRegion(**self._knnclassifierArgs)<EOL>self._knnclassifier.__setstate__(knnclassifierProps)<EOL>self._version = KNNAnomalyClassifierRegion.__VERSION__<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>"<EOL>"<STR_LIT>" % (KNNAnomalyClassifierRegion.__VERSION__))<EOL><DEDENT>

Set the state of ourself from a serialized state.


if name == '<STR_LIT>':<EOL><INDENT>return self._maxLabelOutputs<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>


import inspect<EOL>frameObj = inspect.stack()[<NUM_LIT:2>][<NUM_LIT:0>]<EOL>return inspect.getframeinfo(frameObj)<EOL>

Returns: Traceback namedtuple for our caller's caller


@abstractmethod<EOL><INDENT>def compute(self, inputs, outputs):<DEDENT>

Perform the main computation

        This method is called in each iteration for each phase the node supports.

        Called from the scope of the region's PyRegion.compute() method.

        inputs: dict of numpy arrays (one per input)
        outputs: dict of numpy arrays (one per output)


Return the number of elements in the given output of the region

        Called from the scope of the region's PyRegion.getOutputElementCount() method.

        name: the name of the output


assert not hasattr(self, '<STR_LIT>')<EOL>self.ephemeral = DictObj()<EOL>self.ephemeral.logPathInput = '<STR_LIT>'<EOL>self.ephemeral.logPathOutput = '<STR_LIT>'<EOL>self.ephemeral.logPathOutputDense = '<STR_LIT>'<EOL>self.ephemeral._fpLogInput = None<EOL>self.ephemeral._fpLogOutput = None<EOL>self.ephemeral._fpLogOutputDense = None<EOL>return<EOL>

Initialize ephemeral instance variables (those that aren't serialized)


self.identityPolicy.initialize(self)<EOL>_debugOut(self.identityPolicy.getName())<EOL>

Called by network after all links have been set up


<EOL>self.identityPolicy.compute(inputs, outputs)<EOL>_debugOut(("<STR_LIT>") %(self.identityPolicy.getName(),inputs, outputs))<EOL>return<EOL>

Run one iteration of the region's compute.

The guts of the compute are contained in the _compute() call so that
we can profile it if requested.


assert not (parameterName in self.__dict__ and parameterName in self.ephemeral)<EOL>if parameterName in self.ephemeral:<EOL><INDENT>assert parameterName not in self.__dict__<EOL>return self.ephemeral[parameterName]<EOL><DEDENT>else:<EOL><INDENT>return super(PyRegion, self).getParameter(parameterName, index)<EOL><DEDENT>

Get the value of a NodeSpec parameter. Most parameters are handled
automatically by PyRegion's parameter get mechanism. The ones that need
special treatment are explicitly handled here.


assert not (parameterName in self.__dict__ and parameterName in self.ephemeral)<EOL>if parameterName in self.ephemeral:<EOL><INDENT>if parameterName == "<STR_LIT>":<EOL><INDENT>self.ephemeral.logPathInput = parameterValue<EOL>if self.ephemeral._fpLogInput:<EOL><INDENT>self.ephemeral._fpLogInput.close()<EOL>self.ephemeral._fpLogInput = None<EOL><DEDENT>if parameterValue:<EOL><INDENT>self.ephemeral._fpLogInput = open(self.ephemeral.logPathInput, '<STR_LIT:w>')<EOL><DEDENT><DEDENT>elif parameterName == "<STR_LIT>":<EOL><INDENT>self.ephemeral.logPathOutput = parameterValue<EOL>if self.ephemeral._fpLogOutput:<EOL><INDENT>self.ephemeral._fpLogOutput.close()<EOL>self.ephemeral._fpLogOutput = None<EOL><DEDENT>if parameterValue:<EOL><INDENT>self.ephemeral._fpLogOutput = open(self.ephemeral.logPathOutput, '<STR_LIT:w>')<EOL><DEDENT><DEDENT>elif parameterName == "<STR_LIT>":<EOL><INDENT>self.ephemeral.logPathOutputDense = parameterValue<EOL>if self.ephemeral._fpLogOutputDense:<EOL><INDENT>self.ephemeral._fpLogOutputDense.close()<EOL>self.ephemeral._fpLogOutputDense = None<EOL><DEDENT>if parameterValue:<EOL><INDENT>self.ephemeral._fpLogOutputDense = open(self.ephemeral.logPathOutputDense, '<STR_LIT:w>')<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>' + parameterName)<EOL><DEDENT>return<EOL>

Set the value of a Spec parameter. Most parameters are handled
automatically by PyRegion's parameter set mechanism. The ones that need
special treatment are explicitly handled here.


assert not self.identityPolicy<EOL>assert isinstance(identityPolicyObj, RegionIdentityPolicyBase)<EOL>self.identityPolicy = identityPolicyObj<EOL>return<EOL>

TestRegion command that sets identity policy instance.  The instance
        MUST be derived from TestRegion's RegionIdentityPolicyBase class.

        Users MUST set the identity instance BEFORE running the network

        Exception: AssertionError if identity policy instance has already been set
                   or if the passed-in instance is not derived from
                   RegionIdentityPolicyBase.


assert self.identityPolicy<EOL>return self.identityPolicy<EOL>

TestRegion command that returns the identity policy instance that was
        associated with this TestRegion instance via setIdentityPolicyInstance().

        Returns: a RegionIdentityPolicyBase-based instance that was associated with
                 this TestRegion intstance.

        Exception: AssertionError if no identity policy instance has been set.


proto.breakPdb = self.breakPdb<EOL>proto.breakKomodo = self.breakKomodo<EOL>

Save the region's state.

        The ephemerals and identity policy are excluded from the saved state.

        :param proto: an instance of TestRegionProto to serialize


self.breakPdb = proto.breakPdb<EOL>self.breakKomodo = proto.breakKomodo<EOL>self.__constructEphemeralInstanceVars()<EOL>

Load the state from the given proto instance.

        The saved state does not include the identity policy so this must be
        constructed and set after the region is deserialized. This can be done by
        calling 'setIdentityPolicyInstance'.

        :param proto: an instance of TestRegionProto to load state from


state = self.__dict__.copy()<EOL>state.pop('<STR_LIT>')<EOL>return state<EOL>


assert '<STR_LIT>' not in state<EOL>self.__dict__.update(state)<EOL>self.__constructEphemeralInstanceVars()<EOL>return<EOL>


self._iterNum = <NUM_LIT:0><EOL>if self.dataSource is not None:<EOL><INDENT>self.dataSource.rewind()<EOL><DEDENT>


allFiltersHaveEnoughData = False<EOL>while not allFiltersHaveEnoughData:<EOL><INDENT>data = self.dataSource.getNextRecordDict()<EOL>if not data:<EOL><INDENT>raise StopIteration("<STR_LIT>")<EOL><DEDENT>if "<STR_LIT>" not in data:<EOL><INDENT>data["<STR_LIT>"] = <NUM_LIT:0><EOL><DEDENT>if "<STR_LIT>" not in data:<EOL><INDENT>data["<STR_LIT>"] = <NUM_LIT:0><EOL><DEDENT>if "<STR_LIT>" not in data:<EOL><INDENT>data["<STR_LIT>"] = [None]<EOL><DEDENT>data, allFiltersHaveEnoughData = self.applyFilters(data)<EOL><DEDENT>self.lastRecord = data<EOL>return data<EOL>

Get the next record to encode. Includes getting a record from the
`dataSource` and applying filters. If the filters request more data from the
`dataSource` continue to get data from the `dataSource` until all filters
are satisfied. This method is separate from :meth:`.RecordSensor.compute` so that we can
use a standalone :class:`.RecordSensor` to get filtered data.


if self.verbosity > <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>" % data)<EOL><DEDENT>allFiltersHaveEnoughData = True<EOL>if len(self.preEncodingFilters) > <NUM_LIT:0>:<EOL><INDENT>originalReset = data['<STR_LIT>']<EOL>actualReset = originalReset<EOL>for f in self.preEncodingFilters:<EOL><INDENT>filterHasEnoughData = f.process(data)<EOL>allFiltersHaveEnoughData = (allFiltersHaveEnoughData<EOL>and filterHasEnoughData)<EOL>actualReset = actualReset or data['<STR_LIT>']<EOL>data['<STR_LIT>'] = originalReset<EOL><DEDENT>data['<STR_LIT>'] = actualReset<EOL><DEDENT>return data, allFiltersHaveEnoughData<EOL>

Apply pre-encoding filters. These filters may modify or add data. If a
filter needs another record (e.g. a delta filter) it will request another
record by returning False and the current record will be skipped (but will
still be given to all filters).

We have to be very careful about resets. A filter may add a reset,
but other filters should not see the added reset, each filter sees
the original reset value, and we keep track of whether any filter
adds a reset.

:param data: (dict) The data that will be processed by the filter.
:returns: (tuple) with the data processed by the filter and a boolean to
          know whether or not the filter needs mode data.


def populateCategoriesOut(self, categories, output):

if categories[<NUM_LIT:0>] is None:<EOL><INDENT>output[:] = -<NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>for i, cat in enumerate(categories[:len(output)]):<EOL><INDENT>output[i] = cat<EOL><DEDENT>output[len(categories):] = -<NUM_LIT:1><EOL><DEDENT>

Populate the output array with the category indices.

.. note:: Non-categories are represented with ``-1``.

:param categories: (list) of category strings
:param output: (list) category output, will be overwritten


if not self.topDownMode:<EOL><INDENT>data = self.getNextRecord()<EOL>reset = data["<STR_LIT>"]<EOL>sequenceId = data["<STR_LIT>"]<EOL>categories = data["<STR_LIT>"]<EOL>self.encoder.encodeIntoArray(data, outputs["<STR_LIT>"])<EOL>if self.predictedField is not None and self.predictedField != "<STR_LIT>":<EOL><INDENT>allEncoders = list(self.encoder.encoders)<EOL>if self.disabledEncoder is not None:<EOL><INDENT>allEncoders.extend(self.disabledEncoder.encoders)<EOL><DEDENT>encoders = [e for e in allEncoders<EOL>if e[<NUM_LIT:0>] == self.predictedField]<EOL>if len(encoders) == <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>" % self.predictedField)<EOL><DEDENT>else:<EOL><INDENT>encoder = encoders[<NUM_LIT:0>][<NUM_LIT:1>]<EOL><DEDENT>actualValue = data[self.predictedField]<EOL>outputs["<STR_LIT>"][:] = encoder.getBucketIndices(actualValue)<EOL>if isinstance(actualValue, str):<EOL><INDENT>outputs["<STR_LIT>"][:] = encoder.getBucketIndices(actualValue)<EOL><DEDENT>else:<EOL><INDENT>outputs["<STR_LIT>"][:] = actualValue<EOL><DEDENT><DEDENT>outputs["<STR_LIT>"][:] = self.encoder.getScalars(data)<EOL>self._outputValues["<STR_LIT>"] = self.encoder.getEncodedValues(data)<EOL>encoders = self.encoder.getEncoderList()<EOL>prevOffset = <NUM_LIT:0><EOL>sourceEncodings = []<EOL>bitData = outputs["<STR_LIT>"]<EOL>for encoder in encoders:<EOL><INDENT>nextOffset = prevOffset + encoder.getWidth()<EOL>sourceEncodings.append(bitData[prevOffset:nextOffset])<EOL>prevOffset = nextOffset<EOL><DEDENT>self._outputValues['<STR_LIT>'] = sourceEncodings<EOL>for filter in self.postEncodingFilters:<EOL><INDENT>filter.process(encoder=self.encoder, data=outputs['<STR_LIT>'])<EOL><DEDENT>outputs['<STR_LIT>'][<NUM_LIT:0>] = reset<EOL>outputs['<STR_LIT>'][<NUM_LIT:0>] = sequenceId<EOL>self.populateCategoriesOut(categories, outputs['<STR_LIT>'])<EOL>if self.verbosity >= <NUM_LIT:1>:<EOL><INDENT>if self._iterNum == <NUM_LIT:0>:<EOL><INDENT>self.encoder.pprintHeader(prefix="<STR_LIT>")<EOL><DEDENT>if reset:<EOL><INDENT>print("<STR_LIT>" % sequenceId)<EOL><DEDENT>if self.verbosity >= <NUM_LIT:2>:<EOL><INDENT>print()<EOL><DEDENT><DEDENT>if self.verbosity >= <NUM_LIT:1>:<EOL><INDENT>self.encoder.pprint(outputs["<STR_LIT>"], prefix="<STR_LIT>" % (self._iterNum))<EOL>scalarValues = self.encoder.getScalars(data)<EOL>nz = outputs["<STR_LIT>"].nonzero()[<NUM_LIT:0>]<EOL>print("<STR_LIT>" % (len(nz)), nz)<EOL>print("<STR_LIT>", self.encoder.scalarsToStr(scalarValues))<EOL><DEDENT>if self.verbosity >= <NUM_LIT:2>:<EOL><INDENT>print("<STR_LIT>", str(data))<EOL><DEDENT>if self.verbosity >= <NUM_LIT:3>:<EOL><INDENT>decoded = self.encoder.decode(outputs["<STR_LIT>"])<EOL>print("<STR_LIT>", self.encoder.decodedToStr(decoded))<EOL><DEDENT>self._iterNum += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>spatialTopDownIn = inputs['<STR_LIT>']<EOL>spatialTopDownOut = self.encoder.topDownCompute(spatialTopDownIn)<EOL>values = [elem.value for elem in spatialTopDownOut]<EOL>scalars = [elem.scalar for elem in spatialTopDownOut]<EOL>encodings = [elem.encoding for elem in spatialTopDownOut]<EOL>self._outputValues['<STR_LIT>'] = values<EOL>outputs['<STR_LIT>'][:] = numpy.array(scalars)<EOL>self._outputValues['<STR_LIT>'] = encodings<EOL>temporalTopDownIn = inputs['<STR_LIT>']<EOL>temporalTopDownOut = self.encoder.topDownCompute(temporalTopDownIn)<EOL>values = [elem.value for elem in temporalTopDownOut]<EOL>scalars = [elem.scalar for elem in temporalTopDownOut]<EOL>encodings = [elem.encoding for elem in temporalTopDownOut]<EOL>self._outputValues['<STR_LIT>'] = values<EOL>outputs['<STR_LIT>'][:] = numpy.array(scalars)<EOL>self._outputValues['<STR_LIT>'] = encodings<EOL>assert len(spatialTopDownOut) == len(temporalTopDownOut), (<EOL>"<STR_LIT>"<EOL>"<STR_LIT:size>")<EOL><DEDENT>

Get a record from the dataSource and encode it.

Overrides :meth:`nupic.bindings.regions.PyRegion.PyRegion.compute`.


def _convertNonNumericData(self, spatialOutput, temporalOutput, output):

encoders = self.encoder.getEncoderList()<EOL>types = self.encoder.getDecoderOutputFieldTypes()<EOL>for i, (encoder, type) in enumerate(zip(encoders, types)):<EOL><INDENT>spatialData = spatialOutput[i]<EOL>temporalData = temporalOutput[i]<EOL>if type != FieldMetaType.integer and type != FieldMetaType.float:<EOL><INDENT>spatialData = encoder.getScalars(spatialData)[<NUM_LIT:0>]<EOL>temporalData = encoder.getScalars(temporalData)[<NUM_LIT:0>]<EOL><DEDENT>assert isinstance(spatialData, (float, int))<EOL>assert isinstance(temporalData, (float, int))<EOL>output['<STR_LIT>'][i] = spatialData<EOL>output['<STR_LIT>'][i] = temporalData<EOL><DEDENT>

Converts all of the non-numeric fields from spatialOutput and temporalOutput
into their scalar equivalents and records them in the output dictionary.

:param spatialOutput: The results of topDownCompute() for the spatial input.
:param temporalOutput: The results of topDownCompute() for the temporal
  input.
:param output: The main dictionary of outputs passed to compute(). It is
  expected to have keys 'spatialTopDownOut' and 'temporalTopDownOut' that
  are mapped to numpy arrays.


return self._outputValues[outputName]<EOL>

.. note:: These are normal Python lists, rather than numpy arrays. This is
          to support lists with mixed scalars and strings, as in the case of
          records with categorical variables.

:returns: (dict) output values.


if name == "<STR_LIT>":<EOL><INDENT>print ("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>return <NUM_LIT:1><EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>print ("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>return <NUM_LIT:1><EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>if self.encoder is None:<EOL><INDENT>raise Exception("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>return self.encoder.getWidth()<EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>if self.encoder is None:<EOL><INDENT>raise Exception("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>return len(self.encoder.getDescription())<EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>return <NUM_LIT:1><EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>return <NUM_LIT:1><EOL><DEDENT>elif name == "<STR_LIT>":<EOL><INDENT>return self.numCategories<EOL><DEDENT>elif name == '<STR_LIT>' or name == '<STR_LIT>':<EOL><INDENT>if self.encoder is None:<EOL><INDENT>raise Exception("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>return len(self.encoder.getDescription())<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>" % name)<EOL><DEDENT>

Computes the width of dataOut.

Overrides
:meth:`nupic.bindings.regions.PyRegion.PyRegion.getOutputElementCount`.


if parameterName == '<STR_LIT>':<EOL><INDENT>self.topDownMode = parameterValue<EOL><DEDENT>elif parameterName == '<STR_LIT>':<EOL><INDENT>self.predictedField = parameterValue<EOL><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>' + parameterName)<EOL><DEDENT>


return RecordSensorProto<EOL>


self.encoder.write(proto.encoder)<EOL>if self.disabledEncoder is not None:<EOL><INDENT>self.disabledEncoder.write(proto.disabledEncoder)<EOL><DEDENT>proto.topDownMode = int(self.topDownMode)<EOL>proto.verbosity = self.verbosity<EOL>proto.numCategories = self.numCategories<EOL>


instance = cls()<EOL>instance.encoder = MultiEncoder.read(proto.encoder)<EOL>if proto.disabledEncoder is not None:<EOL><INDENT>instance.disabledEncoder = MultiEncoder.read(proto.disabledEncoder)<EOL><DEDENT>instance.topDownMode = bool(proto.topDownMode)<EOL>instance.verbosity = proto.verbosity<EOL>instance.numCategories = proto.numCategories<EOL>return instance<EOL>


return self._sensedValue<EOL>


self._sensedValue = value<EOL>

:param value: will be encoded when this region does a compute.


if logger is None:<EOL><INDENT>logger = logging.getLogger(__name__)<EOL><DEDENT>def retryFilter(e, args, kwargs):<EOL><INDENT>if isinstance(e, (pymysql.InternalError, pymysql.OperationalError)):<EOL><INDENT>if e.args and e.args[<NUM_LIT:0>] in _ALL_RETRIABLE_ERROR_CODES:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>elif isinstance(e, pymysql.Error):<EOL><INDENT>if (e.args and<EOL>inspect.isclass(e.args[<NUM_LIT:0>]) and issubclass(e.args[<NUM_LIT:0>], socket_error)):<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL><DEDENT>retryExceptions = tuple([<EOL>pymysql.InternalError,<EOL>pymysql.OperationalError,<EOL>pymysql.Error,<EOL>])<EOL>return make_retry_decorator(<EOL>timeoutSec=timeoutSec, initialRetryDelaySec=<NUM_LIT:0.1>, maxRetryDelaySec=<NUM_LIT:10>,<EOL>retryExceptions=retryExceptions, retryFilter=retryFilter,<EOL>logger=logger)<EOL>

Return a closure suitable for use as a decorator for
    retrying a pymysql DAO function on certain failures that warrant retries (
    e.g., RDS/MySQL server down temporarily, transaction deadlock, etc.).
    We share this function across multiple scripts (e.g., ClientJobsDAO,
    StreamMgr) for consitent behavior.

    .. note:: Please ensure that the operation being retried is idempotent.

    .. note:: logging must be initialized *before* any loggers are created, else
       there will be no output; see nupic.support.initLogging()

    Usage Example:

    .. code-block:: python

      @retrySQL()
      def jobInfo(self, jobID):
          ...

    :param timeoutSec:       How many seconds from time of initial call to stop retrying
                       (floating point)
    :param logger:           User-supplied logger instance.


def cPrint(self, level, message, *args, **kw):

if level > self.consolePrinterVerbosity:<EOL><INDENT>return<EOL><DEDENT>if len(kw) > <NUM_LIT:1>:<EOL><INDENT>raise KeyError("<STR_LIT>" % str(list(kw.keys())))<EOL><DEDENT>newline = kw.get("<STR_LIT>", True)<EOL>if len(kw) == <NUM_LIT:1> and '<STR_LIT>' not in kw:<EOL><INDENT>raise KeyError("<STR_LIT>" % list(kw.keys())[<NUM_LIT:0>])<EOL><DEDENT>if len(args) == <NUM_LIT:0>:<EOL><INDENT>if newline:<EOL><INDENT>print(message)<EOL><DEDENT>else:<EOL><INDENT>print(message, end='<STR_LIT:U+0020>')<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if newline:<EOL><INDENT>print(message % args)<EOL><DEDENT>else:<EOL><INDENT>print(message % args, end='<STR_LIT:U+0020>')<EOL><DEDENT><DEDENT>

Print a message to the console.

        Prints only if level <= self.consolePrinterVerbosity
        Printing with level 0 is equivalent to using a print statement,
        and should normally be avoided.

        :param level: (int) indicating the urgency of the message with
               lower values meaning more urgent (messages at level 0  are the most
               urgent and are always printed)

        :param message: (string) possibly with format specifiers

        :param args: specifies the values for any format specifiers in message

        :param kw: newline is the only keyword argument. True (default) if a newline
               should be printed


def decorated(self, *args, **kw):<EOL><INDENT>"""<STR_LIT>"""<EOL>if not hasattr(self, '<STR_LIT>'):<EOL><INDENT>self.__dict__['<STR_LIT>'] = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>self._canAddAttributes += <NUM_LIT:1><EOL><DEDENT>assert self._canAddAttributes >= <NUM_LIT:1><EOL>count = self._canAddAttributes<EOL>f(self, *args, **kw)<EOL>if hasattr(self, '<STR_LIT>'):<EOL><INDENT>self._canAddAttributes -= <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>self._canAddAttributes = count - <NUM_LIT:1><EOL><DEDENT>assert self._canAddAttributes >= <NUM_LIT:0><EOL>if self._canAddAttributes == <NUM_LIT:0>:<EOL><INDENT>del self._canAddAttributes<EOL><DEDENT><DEDENT>decorated.__doc__ = f.__doc__<EOL>decorated.__name__ = f.__name__<EOL>return decorated<EOL>

A decorator that maintains the attribute lock state of an object

    It coperates with the LockAttributesMetaclass (see bellow) that replaces
    the __setattr__ method with a custom one that checks the _canAddAttributes
    counter and allows setting new attributes only if _canAddAttributes > 0.

    New attributes can be set only from methods decorated
    with this decorator (should be only __init__ and __setstate__ normally)

    The decorator is reentrant (e.g. if from inside a decorated function another
    decorated function is invoked). Before invoking the target function it
    increments the counter (or sets it to 1). After invoking the target function
    it decrements the counter and if it's 0 it removed the counter.


type(self).__base__.__init__(self, *args, **kw)<EOL>

trivial init method that just calls base class's __init__()

    This method is attached to classes that don't define __init__(). It is needed
    because LockAttributesMetaclass must decorate the __init__() method of
    its target class.


def getLabel(cls, val):<EOL><INDENT>"""<STR_LIT>"""<EOL>return cls.__labels[val]<EOL><DEDENT>def validate(cls, val):<EOL><INDENT>"""<STR_LIT>"""<EOL>return val in cls.__values<EOL><DEDENT>def getValues(cls):<EOL><INDENT>"""<STR_LIT>"""<EOL>return list(cls.__values)<EOL><DEDENT>def getLabels(cls):<EOL><INDENT>"""<STR_LIT>"""<EOL>return list(cls.__labels.values())<EOL><DEDENT>def getValue(cls, label):<EOL><INDENT>"""<STR_LIT>"""<EOL>return cls.__labels[label]<EOL><DEDENT>for arg in list(args)+kwargs.keys():<EOL><INDENT>if type(arg) is not str:<EOL><INDENT>raise TypeError("<STR_LIT>".format(arg))<EOL><DEDENT>if not __isidentifier(arg):<EOL><INDENT>raise ValueError("<STR_LIT>""<STR_LIT>".format(arg))<EOL><DEDENT><DEDENT>kwargs.update(zip(args, args))<EOL>newType = type("<STR_LIT>", (object,), kwargs)<EOL>newType.__labels = dict( (v,k) for k,v in kwargs.iteritems())<EOL>newType.__values = set(newType.__labels.keys())<EOL>newType.getLabel = functools.partial(getLabel, newType)<EOL>newType.validate = functools.partial(validate, newType)<EOL>newType.getValues = functools.partial(getValues, newType)<EOL>newType.getLabels = functools.partial(getLabels, newType)<EOL>newType.getValue = functools.partial(getValue, newType)<EOL>return newType<EOL>

Utility function for creating enumerations in python

Example Usage:
  >> Color = Enum("Red", "Green", "Blue", "Magenta")
  >> print Color.Red
  >> 0
  >> print Color.Green
  >> 1
  >> print Color.Blue
  >> 2
  >> print Color.Magenta
  >> 3
  >> Color.Violet
  >> 'violet'
  >> Color.getLabel(Color.Red)
  >> 'Red'
  >> Color.getLabel(2)
  >> 'Blue'


generatorList = [] <EOL>if len(args) % <NUM_LIT:2> == <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>advanceList = []<EOL>for i in xrange(<NUM_LIT:0>, len(args), <NUM_LIT:2>):<EOL><INDENT>listn = args[i]<EOL>fn = args[i + <NUM_LIT:1>]<EOL>if listn is not None:<EOL><INDENT>generatorList.append(groupby(listn, fn))<EOL>advanceList.append(True) <EOL><DEDENT>else:<EOL><INDENT>generatorList.append(None)<EOL>advanceList.append(False)<EOL><DEDENT><DEDENT>n = len(generatorList)<EOL>nextList = [None] * n<EOL>while True:<EOL><INDENT>for i in xrange(n):<EOL><INDENT>if advanceList[i]:<EOL><INDENT>try:<EOL><INDENT>nextList[i] = generatorList[i].next()<EOL><DEDENT>except StopIteration:<EOL><INDENT>nextList[i] = None<EOL><DEDENT><DEDENT><DEDENT>if all(entry is None for entry in nextList):<EOL><INDENT>break<EOL><DEDENT>minKeyVal = min(nextVal[<NUM_LIT:0>] for nextVal in nextList<EOL>if nextVal is not None)<EOL>retGroups = [minKeyVal]<EOL>for i in xrange(n):<EOL><INDENT>if nextList[i] is not None and nextList[i][<NUM_LIT:0>] == minKeyVal:<EOL><INDENT>retGroups.append(nextList[i][<NUM_LIT:1>])<EOL>advanceList[i] = True<EOL><DEDENT>else:<EOL><INDENT>advanceList[i] = False<EOL>retGroups.append(None)<EOL><DEDENT><DEDENT>yield tuple(retGroups)<EOL><DEDENT>

Like itertools.groupby, with the following additions:

    - Supports multiple sequences. Instead of returning (k, g), each iteration
      returns (k, g0, g1, ...), with one `g` for each input sequence. The value of
      each `g` is either a non-empty iterator or `None`.
    - It treats the value `None` as an empty sequence. So you can make subsequent
      calls to groupby2 on any `g` value.

    .. note:: Read up on groupby here:
           https://docs.python.org/dev/library/itertools.html#itertools.groupby

    :param args: (list) Parameters alternating between sorted lists and their
                        respective key functions. The lists should be sorted with
                        respect to their key function.

    :returns: (tuple) A n + 1 dimensional tuple, where the first element is the
                    key of the iteration, and the other n entries are groups of
                    objects that share this key. Each group corresponds to the an
                    input sequence. `groupby2` is a generator that returns a tuple
                    for every iteration. If an input sequence has no members with
                    the current key, None is returned in place of a generator.


print()<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>" % (self,))<EOL>print("<STR_LIT>" % (datetime.utcnow()))<EOL>print("<STR_LIT>")<EOL>sys.stdout.flush()<EOL>return<EOL>


print()<EOL>print("<STR_LIT>")<EOL>print(msg % args)<EOL>print("<STR_LIT>" % (datetime.utcnow(), self,), file=sys.stdout)<EOL>print("<STR_LIT>")<EOL>sys.stdout.flush()<EOL>return<EOL>


self.__logItems.append(item)<EOL>return<EOL>

Add an item to the log items list for the currently running session.
        Our self.myAssertXXXXXX wrappers add the current items to the msg that is
        passed to the unittest's assertXXXXX methods.  The extra info will show up
        in test results if the test fails.


msg = msgif not self.__logItemselse {"<STR_LIT>":msg, "<STR_LIT>":copy.copy(self.__logItems)}<EOL>msg = str(msg)<EOL>msg = msg.replace('<STR_LIT>', '<STR_LIT:\n>')<EOL>return msg<EOL>

Called by our unittest.TestCase.assertXXXXXX overrides to construct a
        message from the given message plus self.__logItems, if any. If
        self.__logItems is non-empty, returns a dictionary containing the given
        message value as the "msg" property and self.__logItems as the "extra"
        property. If self.__logItems is empy, returns the given msg arg.


def assertEqual(self, first, second, msg=None):

unittest.TestCase.assertEqual(self, first, second, self.__wrapMsg(msg))<EOL>return<EOL>

unittest.TestCase.assertEqual override; adds extra log items to msg


def assertNotEqual(self, first, second, msg=None):

unittest.TestCase.assertNotEqual(self, first, second, self.__wrapMsg(msg))<EOL>return<EOL>

unittest.TestCase.assertNotEqual override; adds extra log items to msg


unittest.TestCase.assertTrue(self, expr, self.__wrapMsg(msg))<EOL>return<EOL>

unittest.TestCase.assertTrue override; adds extra log items to msg


unittest.TestCase.assertFalse(self, expr, self.__wrapMsg(msg))<EOL>return<EOL>

unittest.TestCase.assertFalse override; adds extra log items to msg


return getattr(pytest.mark, tag)<EOL>

A decorator for tagging a test class or test method with the given tag
    string

    tag: test tag string
    comment: reason for the tag; string; optional

    Examples:

    @tagTest("slowTests", "takes a long time to execute")
    class ClusterTests(TestCase):
      def testSwarmWithAggregation(self):
        pass

      def testSwarmWithoutAggregation(self):
        pass

    or

    class MiscTests(TestCase):
      def testOnePlusOne(self):
        pass

      @tagTest("slowTests")
      def testSwarm(self):
        pass


def getNumpyRandomGenerator(seed = None):

if seed is None:<EOL><INDENT>seed = int((time.time()%<NUM_LIT>)*<NUM_LIT:10>)<EOL><DEDENT>print("<STR_LIT>", seed, "<STR_LIT>", end='<STR_LIT:U+0020>')<EOL>callStack = traceback.extract_stack(limit=<NUM_LIT:3>)<EOL>print(callStack[<NUM_LIT:0>][<NUM_LIT:2>], "<STR_LIT>", callStack[<NUM_LIT:0>][<NUM_LIT:1>], "<STR_LIT>", callStack[<NUM_LIT:1>][<NUM_LIT:2>])<EOL>return numpy.random.RandomState(seed)<EOL>

Return a numpy random number generator with the given seed.
If seed is None, set it randomly based on time. Regardless we log
the actual seed and stack trace so that test failures are replicable.


numColumns = sourceSP.getNumColumns()<EOL>numInputs = sourceSP.getNumInputs()<EOL>for i in range(numColumns):<EOL><INDENT>potential = numpy.zeros(numInputs).astype(uintType)<EOL>sourceSP.getPotential(i, potential)<EOL>destSP.setPotential(i, potential)<EOL>perm = numpy.zeros(numInputs).astype(realType)<EOL>sourceSP.getPermanence(i, perm)<EOL>destSP.setPermanence(i, perm)<EOL><DEDENT>

Transfer the permanences from source to dest SP's. This is used in test
routines to counteract some drift between implementations.
We assume the two SP's have identical configurations/parameters.


seed = int((time.time()%<NUM_LIT>)*<NUM_LIT:10>)<EOL>print("<STR_LIT>", seed, "<STR_LIT>", end='<STR_LIT:U+0020>')<EOL>callStack = traceback.extract_stack(limit=<NUM_LIT:3>)<EOL>print(callStack[<NUM_LIT:0>][<NUM_LIT:2>], "<STR_LIT>", callStack[<NUM_LIT:0>][<NUM_LIT:1>], "<STR_LIT>", callStack[<NUM_LIT:1>][<NUM_LIT:2>])<EOL>return seed<EOL>

Generate and log a 32-bit compatible seed value.


Given an instance of a python spatial_pooler return an instance of the CPP
spatial_pooler with identical parameters.


if (imp == "<STR_LIT>"):<EOL><INDENT>spClass = PySpatialPooler<EOL><DEDENT>elif (imp == "<STR_LIT>"):<EOL><INDENT>spClass = CPPSpatialPooler<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>print(params)<EOL>sp = spClass(**params)<EOL>return sp<EOL>

Helper class for creating an instance of the appropriate spatial pooler using
given parameters.

Parameters:
----------------------------
imp:       Either 'py' or 'cpp' for creating the appropriate instance.
params:    A dict for overriding constructor parameters. The keys must
           correspond to contructor parameter names.

Returns the SP object.


Implement this method to specify the Temporal Memory class.


Implement this method to provide the pattern machine.


return {}<EOL>

Override this method to set the default TM params for `self.tm`.


params = self._computeTMParams(overrides)<EOL>class MonitoredTemporalMemory(TemporalMemoryMonitorMixin,<EOL>self.getTMClass()): pass<EOL>self.tm = MonitoredTemporalMemory(**params)<EOL>

Initialize Temporal Memory, and other member variables.

:param overrides: overrides for default Temporal Memory parameters


f = sys._getframe(depth)<EOL>method_name = f.f_code.co_name<EOL>filename = f.f_code.co_filename<EOL>arg_class = None<EOL>args = inspect.getargvalues(f)<EOL>if len(args[<NUM_LIT:0>]) > <NUM_LIT:0>:<EOL><INDENT>arg_name = args[<NUM_LIT:0>][<NUM_LIT:0>] <EOL>arg_class = args[<NUM_LIT:3>][arg_name].__class__.__name__<EOL><DEDENT>return (method_name, filename, arg_class)<EOL>

Utility function to get information about function callers

    The information is the tuple (function/method name, filename, class)
    The class will be None if the caller is just a function and not an object
    method.

    :param depth: (int) how far back in the callstack to go to extract the caller
           info


def title(s=None, additional='<STR_LIT>', stream=sys.stdout):

if s is None:<EOL><INDENT>callable_name, file_name, class_name = getCallerInfo(<NUM_LIT:2>)<EOL>s = callable_name<EOL>if class_name is not None:<EOL><INDENT>s = class_name + '<STR_LIT:.>' + callable_name<EOL><DEDENT><DEDENT>lines = (s + additional).split('<STR_LIT:\n>')<EOL>length = max(len(line) for line in lines)<EOL>print >> stream, '<STR_LIT:->' * length<EOL>print >> stream, s + additional<EOL>print >> stream, '<STR_LIT:->' * length<EOL>

Utility function to display nice titles

    It automatically extracts the name of the function/method it is called from
    and you can add additional text. title() will then print the name
    of the function/method and the additional text surrounded by tow lines
    of dashes. If you don't want the name of the function, you can provide
    alternative text (regardless of the additional text)

    :param s: (string) text to display, uses the function name and arguments by
           default
    :param additional: (string) extra text to display (not needed if s is not
           None)
    :param stream: (stream) the stream to print to. Ny default goes to standard
           output

    Examples:

    .. code-block:: python

      def foo():
        title()

    will display:

    .. code-block:: text

      ---
      foo
      ---

    .. code-block:: python

      def foo():
        title(additional='(), this is cool!!!')

    will display:

    .. code-block:: text

      ----------------------
      foo(), this is cool!!!
      ----------------------

    .. code-block:: python

      def foo():
        title('No function name here!')

    will display:

    .. code-block:: text

      ----------------------
      No function name here!
      ----------------------


<EOL>argspec = inspect.getargspec(f)<EOL>docstring = f.__doc__<EOL>descriptions = {}<EOL>if docstring:<EOL><INDENT>lines = docstring.split('<STR_LIT:\n>')<EOL>i = <NUM_LIT:0><EOL>while i < len(lines):<EOL><INDENT>stripped = lines[i].lstrip()<EOL>if not stripped:<EOL><INDENT>i += <NUM_LIT:1><EOL>continue<EOL><DEDENT>indentLevel = lines[i].index(stripped[<NUM_LIT:0>])<EOL>firstWord = stripped.split()[<NUM_LIT:0>]<EOL>if firstWord.endswith('<STR_LIT::>'):<EOL><INDENT>firstWord = firstWord[:-<NUM_LIT:1>]<EOL><DEDENT>if firstWord in argspec.args:<EOL><INDENT>argName = firstWord<EOL>restOfLine = stripped[len(firstWord)+<NUM_LIT:1>:].strip()<EOL>argLines = [restOfLine]<EOL>i += <NUM_LIT:1><EOL>while i < len(lines):<EOL><INDENT>stripped = lines[i].lstrip()<EOL>if not stripped:<EOL><INDENT>break<EOL><DEDENT>if lines[i].index(stripped[<NUM_LIT:0>]) <= indentLevel:<EOL><INDENT>break<EOL><DEDENT>argLines.append(lines[i].strip())<EOL>i += <NUM_LIT:1><EOL><DEDENT>descriptions[argName] = '<STR_LIT:U+0020>'.join(argLines)<EOL><DEDENT>else:<EOL><INDENT>i += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>args = []<EOL>if argspec.defaults:<EOL><INDENT>defaultCount = len(argspec.defaults)<EOL><DEDENT>else:<EOL><INDENT>defaultCount = <NUM_LIT:0><EOL><DEDENT>nonDefaultArgCount = len(argspec.args) - defaultCount<EOL>for i, argName in enumerate(argspec.args):<EOL><INDENT>if i >= nonDefaultArgCount:<EOL><INDENT>defaultValue = argspec.defaults[i - nonDefaultArgCount]<EOL>args.append((argName, descriptions.get(argName, "<STR_LIT>"), defaultValue))<EOL><DEDENT>else:<EOL><INDENT>args.append((argName, descriptions.get(argName, "<STR_LIT>")))<EOL><DEDENT><DEDENT>return args<EOL>

Get the arguments, default values, and argument descriptions for a function.

Parses the argument descriptions out of the function docstring, using a
format something lke this:

::

  [junk]
  argument_name:     description...
    description...
    description...
  [junk]
  [more arguments]

It will find an argument as long as the exact argument name starts the line.
It will then strip a trailing colon, if present, then strip the rest of the
line and use it to start the description. It will then strip and append any
subsequent lines with a greater indent level than the original argument name.

:param f: (function) to inspect
:returns: (list of tuples) (``argName``, ``argDescription``, ``defaultValue``)
  If an argument has no default value, the tuple is only two elements long (as
  ``None`` cannot be used, since it could be a default value itself).


def initLogging(verbose=False, console='<STR_LIT>', consoleLevel='<STR_LIT>'):

<EOL>global gLoggingInitialized<EOL>if gLoggingInitialized:<EOL><INDENT>if verbose:<EOL><INDENT>print >> sys.stderr, "<STR_LIT>"<EOL><DEDENT>return<EOL><DEDENT>consoleStreamMappings = {<EOL>'<STR_LIT>'  : '<STR_LIT>',<EOL>'<STR_LIT>'  : '<STR_LIT>',<EOL>}<EOL>consoleLogLevels = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>']<EOL>assert console is None or console in consoleStreamMappings.keys(), (<EOL>'<STR_LIT>') % (console,)<EOL>assert consoleLevel in consoleLogLevels, (<EOL>'<STR_LIT>') % (consoleLevel)<EOL>configFilename = '<STR_LIT>'<EOL>configFilePath = resource_filename("<STR_LIT>", configFilename)<EOL>configLogDir = os.environ.get('<STR_LIT>', None)<EOL>if verbose:<EOL><INDENT>print >> sys.stderr, (<EOL>"<STR_LIT>") % (configFilePath)<EOL><DEDENT>replacements = dict()<EOL>def makeKey(name):<EOL><INDENT>"""<STR_LIT>"""<EOL>return "<STR_LIT>" % (name)<EOL><DEDENT>platform = sys.platform.lower()<EOL>if platform.startswith('<STR_LIT>'):<EOL><INDENT>import java.lang<EOL>platform = java.lang.System.getProperty("<STR_LIT>").lower()<EOL>if platform.startswith('<STR_LIT>'):<EOL><INDENT>platform = '<STR_LIT>'<EOL><DEDENT><DEDENT>if platform.startswith('<STR_LIT>'):<EOL><INDENT>replacements[makeKey('<STR_LIT>')] = '<STR_LIT>'<EOL><DEDENT>elif platform.startswith('<STR_LIT>'):<EOL><INDENT>replacements[makeKey('<STR_LIT>')] = '<STR_LIT>'<EOL><DEDENT>elif platform.startswith('<STR_LIT>'):<EOL><INDENT>replacements[makeKey('<STR_LIT>')] = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError("<STR_LIT>" % (<EOL>sys.platform,))<EOL><DEDENT>replacements[makeKey('<STR_LIT>')] = '<STR_LIT>'<EOL>if platform.startswith('<STR_LIT>'):<EOL><INDENT>replacements[makeKey('<STR_LIT>')] = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>replacements[makeKey('<STR_LIT>')] = '<STR_LIT>'<EOL><DEDENT>handlers = list()<EOL>if configLogDir is not None:<EOL><INDENT>logFilePath = _genLoggingFilePath()<EOL>makeDirectoryFromAbsolutePath(os.path.dirname(logFilePath))<EOL>replacements[makeKey('<STR_LIT>')] = repr(logFilePath)<EOL>handlers.append(replacements[makeKey('<STR_LIT>')])<EOL><DEDENT>if console is not None:<EOL><INDENT>handlers.append(consoleStreamMappings[console])<EOL><DEDENT>replacements[makeKey('<STR_LIT>')] = "<STR_LIT:U+002CU+0020>".join(handlers)<EOL>replacements[makeKey('<STR_LIT>')] = consoleLevel<EOL>customConfig = StringIO()<EOL>loggingFileContents = resource_string(__name__, configFilename)<EOL>for lineNum, line in enumerate(loggingFileContents.splitlines()):<EOL><INDENT>if "<STR_LIT>" in line:<EOL><INDENT>for (key, value) in replacements.items():<EOL><INDENT>line = line.replace(key, value)<EOL><DEDENT><DEDENT>if "<STR_LIT>" in line and "<STR_LIT>" not in line:<EOL><INDENT>raise RuntimeError(("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>") % (line, lineNum, configFilePath))<EOL><DEDENT>customConfig.write("<STR_LIT>" % line)<EOL><DEDENT>customConfig.seek(<NUM_LIT:0>)<EOL>if python_version()[:<NUM_LIT:3>] >= '<STR_LIT>':<EOL><INDENT>logging.config.fileConfig(customConfig, disable_existing_loggers=False)<EOL><DEDENT>else:<EOL><INDENT>logging.config.fileConfig(customConfig)<EOL><DEDENT>gLoggingInitialized = True<EOL>

Initilize NuPic logging by reading in from the logging configuration file. The
logging configuration file is named ``nupic-logging.conf`` and is expected to
be in the format defined by the python logging module.

If the environment variable ``NTA_CONF_PATH`` is defined, then the logging
configuration file is expected to be in the ``NTA_CONF_PATH`` directory. If
``NTA_CONF_PATH`` is not defined, then it is found in the 'conf/default'
subdirectory of the NuPic installation directory (typically
~/nupic/current/conf/default)

The logging configuration file can use the environment variable
``NTA_LOG_DIR`` to set the locations of log files. If this variable is not
defined, logging to files will be disabled.

:param console: Defines console output for the default "root" logging
            configuration; this may be one of 'stdout', 'stderr', or None;
            Use None to suppress console logging output
:param consoleLevel:
            Logging-level filter string for console output corresponding to
            logging levels in the logging module; may be one of:
            'DEBUG', 'INFO', 'WARNING', 'ERROR', or 'CRITICAL'.
            E.g.,  a value of'WARNING' suppresses DEBUG and INFO level output
            to console, but allows WARNING, ERROR, and CRITICAL


appName = os.path.splitext(os.path.basename(sys.argv[<NUM_LIT:0>]))[<NUM_LIT:0>] or '<STR_LIT>'<EOL>appLogDir = os.path.abspath(os.path.join(<EOL>os.environ['<STR_LIT>'],<EOL>'<STR_LIT>' % (os.environ['<STR_LIT>'],),<EOL>appName))<EOL>appLogFileName = '<STR_LIT>' % (<EOL>appName, long(time.mktime(time.gmtime())), os.getpid())<EOL>return os.path.join(appLogDir, appLogFileName)<EOL>


seconds = interval.get('<STR_LIT>', <NUM_LIT:0>) * <NUM_LIT><EOL>seconds += interval.get('<STR_LIT>', <NUM_LIT:0>) * <NUM_LIT><EOL>seconds += interval.get('<STR_LIT>', <NUM_LIT:0>)<EOL>seconds += interval.get('<STR_LIT>', <NUM_LIT:0>) * <NUM_LIT><EOL>seconds += interval.get('<STR_LIT>', <NUM_LIT:0>) * <NUM_LIT> * <NUM_LIT><EOL>seconds += interval.get('<STR_LIT>', <NUM_LIT:0>) * <NUM_LIT> * <NUM_LIT> * <NUM_LIT><EOL>seconds += interval.get('<STR_LIT>', <NUM_LIT:0>) * <NUM_LIT:7> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT><EOL>months = interval.get('<STR_LIT>', <NUM_LIT:0>)<EOL>months += <NUM_LIT:12> * interval.get('<STR_LIT>', <NUM_LIT:0>)<EOL>return {'<STR_LIT>': months, '<STR_LIT>': seconds}<EOL>

Return the number of months and seconds from an aggregation dict that
represents a date and time.

Interval is a dict that contain one or more of the following keys: 'years',
'months', 'weeks', 'days', 'hours', 'minutes', seconds', 'milliseconds',
'microseconds'.

For example:

::

  aggregationMicroseconds({'years': 1, 'hours': 4, 'microseconds':42}) ==
      {'months':12, 'seconds':14400.000042}

:param interval: (dict) The aggregation interval representing a date and time
:returns: (dict) number of months and seconds in the interval:
          ``{months': XX, 'seconds': XX}``. The seconds is
          a floating point that can represent resolutions down to a
          microsecond.


<EOL>dividendMonthSec = aggregationToMonthsSeconds(dividend)<EOL>divisorMonthSec = aggregationToMonthsSeconds(divisor)<EOL>if (dividendMonthSec['<STR_LIT>'] != <NUM_LIT:0> and divisorMonthSec['<STR_LIT>'] != <NUM_LIT:0>)or (dividendMonthSec['<STR_LIT>'] != <NUM_LIT:0> and divisorMonthSec['<STR_LIT>'] != <NUM_LIT:0>):<EOL><INDENT>raise RuntimeError("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if dividendMonthSec['<STR_LIT>'] > <NUM_LIT:0>:<EOL><INDENT>return float(dividendMonthSec['<STR_LIT>']) / divisor['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>return float(dividendMonthSec['<STR_LIT>']) / divisorMonthSec['<STR_LIT>']<EOL><DEDENT>

Return the result from dividing two dicts that represent date and time.

Both dividend and divisor are dicts that contain one or more of the following
keys: 'years', 'months', 'weeks', 'days', 'hours', 'minutes', seconds',
'milliseconds', 'microseconds'.

For example:

::

  aggregationDivide({'hours': 4}, {'minutes': 15}) == 16

:param dividend: (dict) The numerator, as a dict representing a date and time
:param divisor: (dict) the denominator, as a dict representing a date and time
:returns: (float) number of times divisor goes into dividend


if cls._properties is None:<EOL><INDENT>cls._readStdConfigFiles()<EOL><DEDENT>envValue = os.environ.get("<STR_LIT>" % (cls.envPropPrefix,<EOL>prop.replace('<STR_LIT:.>', '<STR_LIT:_>')), None)<EOL>if envValue is not None:<EOL><INDENT>return envValue<EOL><DEDENT>return cls._properties[prop]<EOL>

Retrieve the requested property as a string. If property does not exist,
        then KeyError will be raised.

        :param prop: (string) name of the property
        :raises: KeyError
        :returns: (string) property value


value = cls.getInt(prop)<EOL>if value not in (<NUM_LIT:0>, <NUM_LIT:1>):<EOL><INDENT>raise ValueError("<STR_LIT>" % (<EOL>value, prop))<EOL><DEDENT>return bool(value)<EOL>

Retrieve the requested property and return it as a bool. If property
        does not exist, then KeyError will be raised. If the property value is
        neither 0 nor 1, then ValueError will be raised

        :param prop: (string) name of the property
        :raises: KeyError, ValueError
        :returns: (bool) property value


return int(cls.getString(prop))<EOL>

Retrieve the requested property and return it as an int. If property
        does not exist, then KeyError will be raised.

        :param prop: (string) name of the property
        :returns: (int) property value


return float(cls.getString(prop))<EOL>

Retrieve the requested property and return it as a float. If property
        does not exist, then KeyError will be raised.

        :param prop: (string) name of the property
        :returns: (float) property value


@classmethod<EOL><INDENT>def get(cls, prop, default=None):<DEDENT>

try:<EOL><INDENT>return cls.getString(prop)<EOL><DEDENT>except KeyError:<EOL><INDENT>return default<EOL><DEDENT>

Get the value of the given configuration property as string. This
        returns a string which is the property value, or the value of "default" arg.
        If the property is not found, use :meth:`getString` instead.

        .. note:: it's atypical for our configuration properties to be missing - a
         missing configuration property is usually a very serious error. Because
         of this, it's preferable to use one of the :meth:`getString`,
         :meth:`getInt`, :meth:`getFloat`, etc. variants instead of :meth:`get`.
         Those variants will raise KeyError when an expected property is missing.

        :param prop: (string) name of the property
        :param default: default value to return if property does not exist
        :returns: (string) property value, or default if the property does not exist


@classmethod<EOL><INDENT>def set(cls, prop, value):<DEDENT>

if cls._properties is None:<EOL><INDENT>cls._readStdConfigFiles()<EOL><DEDENT>cls._properties[prop] = str(value)<EOL>

Set the value of the given configuration property.

        :param prop: (string) name of the property
        :param value: (object) value to set


if cls._properties is None:<EOL><INDENT>cls._readStdConfigFiles()<EOL><DEDENT>result = dict(cls._properties)<EOL>keys = os.environ.keys()<EOL>replaceKeys = filter(lambda x: x.startswith(cls.envPropPrefix),<EOL>keys)<EOL>for envKey in replaceKeys:<EOL><INDENT>key = envKey[len(cls.envPropPrefix):]<EOL>key = key.replace('<STR_LIT:_>', '<STR_LIT:.>')<EOL>result[key] = os.environ[envKey]<EOL><DEDENT>return result<EOL>

Return a dict containing all of the configuration properties

        :returns: (dict) containing all configuration properties.


@classmethod<EOL><INDENT>def readConfigFile(cls, filename, path=None):<DEDENT>

properties = cls._readConfigFile(filename, path)<EOL>if cls._properties is None:<EOL><INDENT>cls._properties = dict()<EOL><DEDENT>for name in properties:<EOL><INDENT>if '<STR_LIT:value>' in properties[name]:<EOL><INDENT>cls._properties[name] = properties[name]['<STR_LIT:value>']<EOL><DEDENT><DEDENT>

Parse the given XML file and store all properties it describes.

        :param filename: (string) name of XML file to parse (no path)
        :param path: (string) path of the XML file. If None, then use the standard
                      configuration search path.


@classmethod<EOL><INDENT>def _readConfigFile(cls, filename, path=None):<DEDENT>

outputProperties = dict()<EOL>if path is None:<EOL><INDENT>filePath = cls.findConfigFile(filename)<EOL><DEDENT>else:<EOL><INDENT>filePath = os.path.join(path, filename)<EOL><DEDENT>try:<EOL><INDENT>if filePath is not None:<EOL><INDENT>try:<EOL><INDENT>_getLogger().debug("<STR_LIT>", filePath)<EOL>with open(filePath, '<STR_LIT:r>') as inp:<EOL><INDENT>contents = inp.read()<EOL><DEDENT><DEDENT>except Exception:<EOL><INDENT>raise RuntimeError("<STR_LIT>" % filePath)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>contents = resource_string("<STR_LIT>", filename)<EOL><DEDENT>except Exception as resourceException:<EOL><INDENT>if filename in [USER_CONFIG, CUSTOM_CONFIG]:<EOL><INDENT>contents = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>raise resourceException<EOL><DEDENT><DEDENT><DEDENT>elements = ElementTree.XML(contents)<EOL>if elements.tag != '<STR_LIT>':<EOL><INDENT>raise RuntimeError("<STR_LIT>"<EOL>"<STR_LIT>" % (elements.tag))<EOL><DEDENT>propertyElements = elements.findall('<STR_LIT>')<EOL>for propertyItem in propertyElements:<EOL><INDENT>propInfo = dict()<EOL>propertyAttributes = list(propertyItem)<EOL>for propertyAttribute in propertyAttributes:<EOL><INDENT>propInfo[propertyAttribute.tag] = propertyAttribute.text<EOL><DEDENT>name = propInfo.get('<STR_LIT:name>', None)<EOL>if '<STR_LIT:value>' in propInfo and propInfo['<STR_LIT:value>'] is None:<EOL><INDENT>value = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>value = propInfo.get('<STR_LIT:value>', None)<EOL>if value is None:<EOL><INDENT>if '<STR_LIT>' in propInfo:<EOL><INDENT>continue<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError("<STR_LIT>"<EOL>"<STR_LIT>" % (str(propInfo)))<EOL><DEDENT><DEDENT><DEDENT>restOfValue = value<EOL>value = '<STR_LIT>'<EOL>while True:<EOL><INDENT>pos = restOfValue.find('<STR_LIT>')<EOL>if pos == -<NUM_LIT:1>:<EOL><INDENT>value += restOfValue<EOL>break<EOL><DEDENT>value += restOfValue[<NUM_LIT:0>:pos]<EOL>varTailPos = restOfValue.find('<STR_LIT:}>', pos)<EOL>if varTailPos == -<NUM_LIT:1>:<EOL><INDENT>raise RuntimeError("<STR_LIT>"<EOL>"<STR_LIT>" % (restOfValue))<EOL><DEDENT>varname = restOfValue[pos+<NUM_LIT:6>:varTailPos]<EOL>if varname not in os.environ:<EOL><INDENT>raise RuntimeError("<STR_LIT>"<EOL>"<STR_LIT>" % (varname))<EOL><DEDENT>envVarValue = os.environ[varname]<EOL>value += envVarValue<EOL>restOfValue = restOfValue[varTailPos+<NUM_LIT:1>:]<EOL><DEDENT>if name is None:<EOL><INDENT>raise RuntimeError("<STR_LIT>"<EOL>"<STR_LIT>" % (str(propInfo)))<EOL><DEDENT>propInfo['<STR_LIT:value>'] = value<EOL>outputProperties[name] = propInfo<EOL><DEDENT>return outputProperties<EOL><DEDENT>except Exception:<EOL><INDENT>_getLogger().exception("<STR_LIT>",<EOL>filePath)<EOL>raise<EOL><DEDENT>

Parse the given XML file and return a dict describing the file.

        :param filename: (string) name of XML file to parse (no path)
        :param path: (string) path of the XML file. If None, then use the standard
               configuration search path.
        :returns: (dict) with each property as a key and a dict of all the
               property's attributes as value


cls._properties = None<EOL>cls._configPaths = None<EOL>


paths = cls.getConfigPaths()<EOL>for p in paths:<EOL><INDENT>testPath = os.path.join(p, filename)<EOL>if os.path.isfile(testPath):<EOL><INDENT>return os.path.join(p, filename)<EOL><DEDENT><DEDENT>

Search the configuration path (specified via the NTA_CONF_PATH
        environment variable) for the given filename. If found, return the complete
        path to the file.

        :param filename: (string) name of file to locate