signature:
def modelSetCompleted ( self , modelID , completionReason , completionMsg , cpuTime = 0 , useConnectionID = True ) :
    """Mark a model as completed , with the given completionReason and
 completionMsg . This will fail if the model does not currently belong to this
 client ( connection _ id doesn ' t match ) .
 Parameters :
 modelID : model ID of model to modify
 completionReason : completionReason string
 completionMsg : completionMsg string
 cpuTime : amount of CPU time spent on this model
 useConnectionID : True if the connection id of the calling function
 must be the same as the connection that created the
 job . Set to True for hypersearch workers , which use
 this mechanism for orphaned model detection ."""

implementation:
if completionMsg is None :
    completionMsg = ''
query = 'UPDATE %s SET status=%%s, ' '            completion_reason=%%s, ' '            completion_msg=%%s, ' '            end_time=UTC_TIMESTAMP(), ' '            cpu_time=%%s, ' '            _eng_last_update_time=UTC_TIMESTAMP(), ' '            update_counter=update_counter+1 ' '        WHERE model_id=%%s' % ( self . modelsTableName , )
sqlParams = [ self . STATUS_COMPLETED , completionReason , completionMsg , cpuTime , modelID ]
if useConnectionID :
    query += " AND _eng_worker_conn_id=%s"
    sqlParams . append ( self . _connectionID )
with ConnectionFactory . get ( ) as conn :
    numRowsAffected = conn . cursor . execute ( query , sqlParams )
if numRowsAffected != 1 :
    raise InvalidConnectionException ( ( "Tried to set modelID=%r using connectionID=%r, but this model " "belongs to some other worker or modelID not found; " "numRowsAffected=%r" ) % ( modelID , self . _connectionID , numRowsAffected ) )


signature:
def modelAdoptNextOrphan ( self , jobId , maxUpdateInterval ) :
    """Look through the models table for an orphaned model , which is a model
 that is not completed yet , whose _ eng _ last _ update _ time is more than
 maxUpdateInterval seconds ago .
 If one is found , change its _ eng _ worker _ conn _ id to the current worker ' s
 and return the model id .
 Parameters :
 retval : modelId of the model we adopted , or None if none found"""

implementation:
@ g_retrySQL
def findCandidateModelWithRetries ( ) :
    modelID = None
    with ConnectionFactory . get ( ) as conn : # TODO : may need a table index on job _ id / status for speed
        query = 'SELECT model_id FROM %s ' '   WHERE  status=%%s ' '          AND job_id=%%s ' '          AND TIMESTAMPDIFF(SECOND, ' '                            _eng_last_update_time, ' '                            UTC_TIMESTAMP()) > %%s ' '   LIMIT 1 ' % ( self . modelsTableName , )
        sqlParams = [ self . STATUS_RUNNING , jobId , maxUpdateInterval ]
        numRows = conn . cursor . execute ( query , sqlParams )
        rows = conn . cursor . fetchall ( )
    assert numRows <= 1 , "Unexpected numRows: %r" % numRows
    if numRows == 1 :
        ( modelID , ) = rows [ 0 ]
    return modelID
@ g_retrySQL
def adoptModelWithRetries ( modelID ) :
    adopted = False
    with ConnectionFactory . get ( ) as conn :
        query = 'UPDATE %s SET _eng_worker_conn_id=%%s, ' '            _eng_last_update_time=UTC_TIMESTAMP() ' '        WHERE model_id=%%s ' '              AND status=%%s' '              AND TIMESTAMPDIFF(SECOND, ' '                                _eng_last_update_time, ' '                                UTC_TIMESTAMP()) > %%s ' '        LIMIT 1 ' % ( self . modelsTableName , )
        sqlParams = [ self . _connectionID , modelID , self . STATUS_RUNNING , maxUpdateInterval ]
        numRowsAffected = conn . cursor . execute ( query , sqlParams )
        assert numRowsAffected <= 1 , 'Unexpected numRowsAffected=%r' % ( numRowsAffected , )
        if numRowsAffected == 1 :
            adopted = True
        else : # Discern between transient failure during update and someone else
        # claiming this model
            ( status , connectionID ) = self . _getOneMatchingRowNoRetries ( self . _models , conn , { 'model_id' : modelID } , [ 'status' , '_eng_worker_conn_id' ] )
            adopted = ( status == self . STATUS_RUNNING and connectionID == self . _connectionID )
    return adopted
adoptedModelID = None
while True :
    modelID = findCandidateModelWithRetries ( )
    if modelID is None :
        break
    if adoptModelWithRetries ( modelID ) :
        adoptedModelID = modelID
        break
return adoptedModelID


signature:
def profileSP ( spClass , spDim , nRuns ) :
    """profiling performance of SpatialPooler ( SP )
 using the python cProfile module and ordered by cumulative time ,
 see how to run on command - line above .
 @ param spClass implementation of SP ( cpp , py , . . )
 @ param spDim number of columns in SP ( in 1D , for 2D see colDim in code )
 @ param nRuns number of calls of the profiled code ( epochs )"""

implementation:
# you can change dimensionality here , eg to 2D
inDim = [ 10000 , 1 , 1 ]
colDim = [ spDim , 1 , 1 ]
# create SP instance to measure
# changing the params here affects the performance
sp = spClass ( inputDimensions = inDim , columnDimensions = colDim , potentialRadius = 3 , potentialPct = 0.5 , globalInhibition = False , localAreaDensity = - 1.0 , numActiveColumnsPerInhArea = 3 , stimulusThreshold = 1 , synPermInactiveDec = 0.01 , synPermActiveInc = 0.1 , synPermConnected = 0.10 , minPctOverlapDutyCycle = 0.1 , dutyCyclePeriod = 10 , boostStrength = 10.0 , seed = 42 , spVerbosity = 0 )
# generate input data
dataDim = inDim
dataDim . append ( nRuns )
data = numpy . random . randint ( 0 , 2 , dataDim ) . astype ( 'float32' )
for i in xrange ( nRuns ) : # new data every time , this is the worst case performance
# real performance would be better , as the input data would not be completely random
    d = data [ : , : , : , i ]
    activeArray = numpy . zeros ( colDim )
    # the actual function to profile !
    sp . compute ( d , True , activeArray )


signature:
def getSpec ( cls ) :
    """Overrides : meth : ` nupic . bindings . regions . PyRegion . PyRegion . getSpec ` ."""

implementation:
ns = dict ( description = KNNClassifierRegion . __doc__ , singleNodeOnly = True , inputs = dict ( categoryIn = dict ( description = 'Vector of zero or more category indices for this input' 'sample. -1 implies no category.' , dataType = 'Real32' , count = 0 , required = True , regionLevel = True , isDefaultInput = False , requireSplitterMap = False ) , bottomUpIn = dict ( description = 'Belief values over children\'s groups' , dataType = 'Real32' , count = 0 , required = True , regionLevel = False , isDefaultInput = True , requireSplitterMap = False ) , partitionIn = dict ( description = 'Partition ID of the input sample' , dataType = 'Real32' , count = 0 , required = True , regionLevel = True , isDefaultInput = False , requireSplitterMap = False ) , auxDataIn = dict ( description = 'Auxiliary data from the sensor' , dataType = 'Real32' , count = 0 , required = False , regionLevel = True , isDefaultInput = False , requireSplitterMap = False ) ) , outputs = dict ( categoriesOut = dict ( description = 'A vector representing, for each category ' 'index, the likelihood that the input to the node belongs ' 'to that category based on the number of neighbors of ' 'that category that are among the nearest K.' , dataType = 'Real32' , count = 0 , regionLevel = True , isDefaultOutput = True ) , bestPrototypeIndices = dict ( description = 'A vector that lists, in descending order of ' 'the match, the positions of the prototypes ' 'that best match the input pattern.' , dataType = 'Real32' , count = 0 , regionLevel = True , isDefaultOutput = False ) , categoryProbabilitiesOut = dict ( description = 'A vector representing, for each category ' 'index, the probability that the input to the node belongs ' 'to that category based on the distance to the nearest ' 'neighbor of each category.' , dataType = 'Real32' , count = 0 , regionLevel = True , isDefaultOutput = True ) , ) , parameters = dict ( learningMode = dict ( description = 'Boolean (0/1) indicating whether or not a region ' 'is in learning mode.' , dataType = 'UInt32' , count = 1 , constraints = 'bool' , defaultValue = 1 , accessMode = 'ReadWrite' ) , inferenceMode = dict ( description = 'Boolean (0/1) indicating whether or not a region ' 'is in inference mode.' , dataType = 'UInt32' , count = 1 , constraints = 'bool' , defaultValue = 0 , accessMode = 'ReadWrite' ) , acceptanceProbability = dict ( description = 'During learning, inputs are learned with ' 'probability equal to this parameter. ' 'If set to 1.0, the default, ' 'all inputs will be considered ' '(subject to other tests).' , dataType = 'Real32' , count = 1 , constraints = '' , defaultValue = 1.0 , # accessMode = ' Create ' ) ,
accessMode = 'ReadWrite' ) , # and Create too
confusion = dict ( description = 'Confusion matrix accumulated during inference. ' 'Reset with reset(). This is available to Python ' 'client code only.' , dataType = 'Handle' , count = 2 , constraints = '' , defaultValue = None , accessMode = 'Read' ) , activeOutputCount = dict ( description = 'The number of active elements in the ' '"categoriesOut" output.' , dataType = 'UInt32' , count = 1 , constraints = '' , defaultValue = 0 , accessMode = 'Read' ) , categoryCount = dict ( description = 'An integer indicating the number of ' 'categories that have been learned' , dataType = 'UInt32' , count = 1 , constraints = '' , defaultValue = None , accessMode = 'Read' ) , patternCount = dict ( description = 'Number of patterns learned by the classifier.' , dataType = 'UInt32' , count = 1 , constraints = '' , defaultValue = None , accessMode = 'Read' ) , patternMatrix = dict ( description = 'The actual patterns learned by the classifier, ' 'returned as a matrix.' , dataType = 'Handle' , count = 1 , constraints = '' , defaultValue = None , accessMode = 'Read' ) , k = dict ( description = 'The number of nearest neighbors to use ' 'during inference.' , dataType = 'UInt32' , count = 1 , constraints = '' , defaultValue = 1 , accessMode = 'Create' ) , maxCategoryCount = dict ( description = 'The maximal number of categories the ' 'classifier will distinguish between.' , dataType = 'UInt32' , count = 1 , constraints = '' , defaultValue = 2 , accessMode = 'Create' ) , distanceNorm = dict ( description = 'The norm to use for a distance metric (i.e., ' 'the "p" in Lp-norm)' , dataType = 'Real32' , count = 1 , constraints = '' , defaultValue = 2.0 , accessMode = 'ReadWrite' ) , # accessMode = ' Create ' ) ,
distanceMethod = dict ( description = 'Method used to compute distances between inputs and' 'prototypes. Possible options are norm, rawOverlap, ' 'pctOverlapOfLarger, and pctOverlapOfProto' , dataType = "Byte" , count = 0 , constraints = 'enum: norm, rawOverlap, pctOverlapOfLarger, ' 'pctOverlapOfProto, pctOverlapOfInput' , defaultValue = 'norm' , accessMode = 'ReadWrite' ) , outputProbabilitiesByDist = dict ( description = 'If True, categoryProbabilitiesOut is the probability of ' 'each category based on the distance to the nearest neighbor of ' 'each category. If False, categoryProbabilitiesOut is the ' 'percentage of neighbors among the top K that are of each category.' , dataType = 'UInt32' , count = 1 , constraints = 'bool' , defaultValue = 0 , accessMode = 'Create' ) , distThreshold = dict ( description = 'Distance Threshold.  If a pattern that ' 'is less than distThreshold apart from ' 'the input pattern already exists in the ' 'KNN memory, then the input pattern is ' 'not added to KNN memory.' , dataType = 'Real32' , count = 1 , constraints = '' , defaultValue = 0.0 , accessMode = 'ReadWrite' ) , inputThresh = dict ( description = 'Input binarization threshold, used if ' '"doBinarization" is True.' , dataType = 'Real32' , count = 1 , constraints = '' , defaultValue = 0.5 , accessMode = 'Create' ) , doBinarization = dict ( description = 'Whether or not to binarize the input vectors.' , dataType = 'UInt32' , count = 1 , constraints = 'bool' , defaultValue = 0 , accessMode = 'Create' ) , useSparseMemory = dict ( description = 'A boolean flag that determines whether or ' 'not the KNNClassifier will use sparse Memory' , dataType = 'UInt32' , count = 1 , constraints = '' , defaultValue = 1 , accessMode = 'Create' ) , minSparsity = dict ( description = "If useSparseMemory is set, only vectors with sparsity" " >= minSparsity will be stored during learning. A value" " of 0.0 implies all vectors will be stored. A value of" " 0.1 implies only vectors with at least 10% sparsity" " will be stored" , dataType = 'Real32' , count = 1 , constraints = '' , defaultValue = 0.0 , accessMode = 'ReadWrite' ) , sparseThreshold = dict ( description = 'If sparse memory is used, input variables ' 'whose absolute value is less than this ' 'threshold  will be stored as zero' , dataType = 'Real32' , count = 1 , constraints = '' , defaultValue = 0.0 , accessMode = 'Create' ) , relativeThreshold = dict ( description = 'Whether to multiply sparseThreshold by max value ' ' in input' , dataType = 'UInt32' , count = 1 , constraints = 'bool' , defaultValue = 0 , accessMode = 'Create' ) , winnerCount = dict ( description = 'Only this many elements of the input are ' 'stored. All elements are stored if 0.' , dataType = 'UInt32' , count = 1 , constraints = '' , defaultValue = 0 , accessMode = 'Create' ) , doSphering = dict ( description = 'A boolean indicating whether or not data should' 'be "sphered" (i.e. each dimension should be normalized such' 'that its mean and variance are zero and one, respectively.) This' ' sphering normalization would be performed after all training ' 'samples had been received but before inference was performed. ' 'The dimension-specific normalization constants would then ' ' be applied to all future incoming vectors prior to performing ' ' conventional NN inference.' , dataType = 'UInt32' , count = 1 , constraints = 'bool' , defaultValue = 0 , accessMode = 'Create' ) , SVDSampleCount = dict ( description = 'If not 0, carries out SVD transformation after ' 'that many samples have been seen.' , dataType = 'UInt32' , count = 1 , constraints = '' , defaultValue = 0 , accessMode = 'Create' ) , SVDDimCount = dict ( description = 'Number of dimensions to keep after SVD if greater ' 'than 0. If set to -1 it is considered unspecified. ' 'If set to 0 it is consider "adaptive" and the number ' 'is chosen automatically.' , dataType = 'Int32' , count = 1 , constraints = '' , defaultValue = - 1 , accessMode = 'Create' ) , fractionOfMax = dict ( description = 'The smallest singular value which is retained ' 'as a fraction of the largest singular value. This is ' 'used only if SVDDimCount==0 ("adaptive").' , dataType = 'UInt32' , count = 1 , constraints = '' , defaultValue = 0 , accessMode = 'Create' ) , useAuxiliary = dict ( description = 'Whether or not the classifier should use auxiliary ' 'input data.' , dataType = 'UInt32' , count = 1 , constraints = 'bool' , defaultValue = 0 , accessMode = 'Create' ) , justUseAuxiliary = dict ( description = 'Whether or not the classifier should ONLUY use the ' 'auxiliary input data.' , dataType = 'UInt32' , count = 1 , constraints = 'bool' , defaultValue = 0 , accessMode = 'Create' ) , verbosity = dict ( description = 'An integer that controls the verbosity level, ' '0 means no verbose output, increasing integers ' 'provide more verbosity.' , dataType = 'UInt32' , count = 1 , constraints = '' , defaultValue = 0 , accessMode = 'ReadWrite' ) , keepAllDistances = dict ( description = 'Whether to store all the protoScores in an array, ' 'rather than just the ones for the last inference. ' 'When this parameter is changed from True to False, ' 'all the scores are discarded except for the most ' 'recent one.' , dataType = 'UInt32' , count = 1 , constraints = 'bool' , defaultValue = None , accessMode = 'ReadWrite' ) , replaceDuplicates = dict ( description = 'A boolean flag that determines whether or' 'not the KNNClassifier should replace duplicates' 'during learning. This should be on when online' 'learning.' , dataType = 'UInt32' , count = 1 , constraints = 'bool' , defaultValue = None , accessMode = 'ReadWrite' ) , cellsPerCol = dict ( description = 'If >= 1, we assume the input is organized into columns, ' 'in the same manner as the temporal memory AND ' 'whenever we store a new prototype, we only store the ' 'start cell (first cell) in any column which is bursting.' 'colum ' , dataType = 'UInt32' , count = 1 , constraints = '' , defaultValue = 0 , accessMode = 'Create' ) , maxStoredPatterns = dict ( description = 'Limits the maximum number of the training patterns ' 'stored. When KNN learns in a fixed capacity mode, ' 'the unused patterns are deleted once the number ' 'of stored patterns is greater than maxStoredPatterns' 'columns. [-1 is no limit] ' , dataType = 'Int32' , count = 1 , constraints = '' , defaultValue = - 1 , accessMode = 'Create' ) , ) , commands = dict ( ) )
return ns


signature:
def _initEphemerals ( self ) :
    """Initialize attributes that are not saved with the checkpoint ."""

implementation:
self . _firstComputeCall = True
self . _accuracy = None
self . _protoScores = None
self . _categoryDistances = None
self . _knn = knn_classifier . KNNClassifier ( ** self . knnParams )
for x in ( '_partitions' , '_useAuxiliary' , '_doSphering' , '_scanInfo' , '_protoScores' ) :
    if not hasattr ( self , x ) :
        setattr ( self , x , None )


signature:
def getParameter ( self , name , index = - 1 ) :
    """Overrides : meth : ` nupic . bindings . regions . PyRegion . PyRegion . getParameter ` ."""

implementation:
if name == "patternCount" :
    return self . _knn . _numPatterns
elif name == "patternMatrix" :
    return self . _getPatternMatrix ( )
elif name == "k" :
    return self . _knn . k
elif name == "distanceNorm" :
    return self . _knn . distanceNorm
elif name == "distanceMethod" :
    return self . _knn . distanceMethod
elif name == "distThreshold" :
    return self . _knn . distThreshold
elif name == "inputThresh" :
    return self . _knn . binarizationThreshold
elif name == "doBinarization" :
    return self . _knn . doBinarization
elif name == "useSparseMemory" :
    return self . _knn . useSparseMemory
elif name == "sparseThreshold" :
    return self . _knn . sparseThreshold
elif name == "winnerCount" :
    return self . _knn . numWinners
elif name == "relativeThreshold" :
    return self . _knn . relativeThreshold
elif name == "SVDSampleCount" :
    v = self . _knn . numSVDSamples
    return v if v is not None else 0
elif name == "SVDDimCount" :
    v = self . _knn . numSVDDims
    return v if v is not None else 0
elif name == "fractionOfMax" :
    v = self . _knn . fractionOfMax
    return v if v is not None else 0
elif name == "useAuxiliary" :
    return self . _useAuxiliary
elif name == "justUseAuxiliary" :
    return self . _justUseAuxiliary
elif name == "doSphering" :
    return self . _doSphering
elif name == "cellsPerCol" :
    return self . _knn . cellsPerCol
elif name == "maxStoredPatterns" :
    return self . maxStoredPatterns
elif name == 'categoryRecencyList' :
    return self . _knn . _categoryRecencyList
else : # If any spec parameter name is the same as an attribute , this call
# will get it automatically , e . g . self . learningMode
    return PyRegion . getParameter ( self , name , index )


signature:
def setParameter ( self , name , index , value ) :
    """Overrides : meth : ` nupic . bindings . regions . PyRegion . PyRegion . setParameter ` ."""

implementation:
if name == "learningMode" :
    self . learningMode = bool ( int ( value ) )
    self . _epoch = 0
elif name == "inferenceMode" :
    self . _epoch = 0
    if int ( value ) and not self . inferenceMode :
        self . _finishLearning ( )
    self . inferenceMode = bool ( int ( value ) )
elif name == "distanceNorm" :
    self . _knn . distanceNorm = value
elif name == "distanceMethod" :
    self . _knn . distanceMethod = value
elif name == "keepAllDistances" :
    self . keepAllDistances = bool ( value )
    if not self . keepAllDistances : # Discard all distances except the latest
        if self . _protoScores is not None and self . _protoScores . shape [ 0 ] > 1 :
            self . _protoScores = self . _protoScores [ - 1 , : ]
        if self . _protoScores is not None :
            self . _protoScoreCount = 1
        else :
            self . _protoScoreCount = 0
elif name == "verbosity" :
    self . verbosity = value
    self . _knn . verbosity = value
else :
    return PyRegion . setParameter ( self , name , index , value )


signature:
def enableTap ( self , tapPath ) :
    """Begin writing output tap files .
 : param tapPath : ( string ) base name of the output tap files to write ."""

implementation:
self . _tapFileIn = open ( tapPath + '.in' , 'w' )
self . _tapFileOut = open ( tapPath + '.out' , 'w' )


signature:
def disableTap ( self ) :
    """Disable writing of output tap files ."""

implementation:
if self . _tapFileIn is not None :
    self . _tapFileIn . close ( )
    self . _tapFileIn = None
if self . _tapFileOut is not None :
    self . _tapFileOut . close ( )
    self . _tapFileOut = None


signature:
def handleLogInput ( self , inputs ) :
    """Write inputs to output tap file .
 : param inputs : ( iter ) some inputs ."""

implementation:
if self . _tapFileIn is not None :
    for input in inputs :
        for k in range ( len ( input ) ) :
            print >> self . _tapFileIn , input [ k ] ,
        print >> self . _tapFileIn


signature:
def handleLogOutput ( self , output ) :
    """Write outputs to output tap file .
 : param outputs : ( iter ) some outputs ."""

implementation:
# raise Exception ( ' MULTI - LINE DUMMY \ nMULTI - LINE DUMMY ' )
if self . _tapFileOut is not None :
    for k in range ( len ( output ) ) :
        print >> self . _tapFileOut , output [ k ] ,
    print >> self . _tapFileOut


signature:
def _storeSample ( self , inputVector , trueCatIndex , partition = 0 ) :
    """Store a training sample and associated category label"""

implementation:
# If this is the first sample , then allocate a numpy array
# of the appropriate size in which to store all samples .
if self . _samples is None :
    self . _samples = numpy . zeros ( ( 0 , len ( inputVector ) ) , dtype = RealNumpyDType )
    assert self . _labels is None
    self . _labels = [ ]
# Add the sample vector and category lable
self . _samples = numpy . concatenate ( ( self . _samples , numpy . atleast_2d ( inputVector ) ) , axis = 0 )
self . _labels += [ trueCatIndex ]
# Add the partition ID
if self . _partitions is None :
    self . _partitions = [ ]
if partition is None :
    partition = 0
self . _partitions += [ partition ]


signature:
def compute ( self , inputs , outputs ) :
    """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"""

implementation:
# raise Exception ( ' MULTI - LINE DUMMY \ nMULTI - LINE DUMMY ' )
# For backward compatibility
if self . _useAuxiliary is None :
    self . _useAuxiliary = False
# If the first time being called , then print potential warning messsages
if self . _firstComputeCall :
    self . _firstComputeCall = False
    if self . _useAuxiliary : # print " \ n Auxiliary input stream from Image Sensor enabled . "
        if self . _justUseAuxiliary == True :
            print "  Warning: You have chosen to ignore the image data and instead just use the auxiliary data stream."
# Format inputs
# childInputs = [ x . wvector ( 0 ) for x in inputs [ " bottomUpIn " ] ]
# inputVector = numpy . concatenate ( [ x . array ( ) for x in childInputs ] )
inputVector = inputs [ 'bottomUpIn' ]
# Look for auxiliary input
if self . _useAuxiliary == True : # auxVector = inputs [ ' auxDataIn ' ] [ 0 ] . wvector ( 0 ) . array ( )
    auxVector = inputs [ 'auxDataIn' ]
    if auxVector . dtype != numpy . float32 :
        raise RuntimeError , "KNNClassifierRegion expects numpy.float32 for the auxiliary data vector"
    if self . _justUseAuxiliary == True : # inputVector = inputs [ ' auxDataIn ' ] [ 0 ] . wvector ( 0 ) . array ( )
        inputVector = inputs [ 'auxDataIn' ]
    else : # inputVector = numpy . concatenate ( [ inputVector , inputs [ ' auxDataIn ' ] [ 0 ] . wvector ( 0 ) . array ( ) ] )
        inputVector = numpy . concatenate ( [ inputVector , inputs [ 'auxDataIn' ] ] )
# Logging
# self . handleLogInput ( childInputs )
self . handleLogInput ( [ inputVector ] )
# Read the category .
assert "categoryIn" in inputs , "No linked category input."
categories = inputs [ 'categoryIn' ]
# Read the partition ID .
if "partitionIn" in inputs :
    assert len ( inputs [ "partitionIn" ] ) == 1 , "Must have exactly one link to partition input."
    partInput = inputs [ 'partitionIn' ]
    assert len ( partInput ) == 1 , "Partition input element count must be exactly 1."
    partition = int ( partInput [ 0 ] )
else :
    partition = None
# Inference ( can be done simultaneously with learning )
if self . inferenceMode :
    categoriesOut = outputs [ 'categoriesOut' ]
    probabilitiesOut = outputs [ 'categoryProbabilitiesOut' ]
    # If we are sphering , then apply normalization
    if self . _doSphering :
        inputVector = ( inputVector + self . _normOffset ) * self . _normScale
    nPrototypes = 0
    if "bestPrototypeIndices" in outputs : # bestPrototypeIndicesOut = outputs [ " bestPrototypeIndices " ] . wvector ( )
        bestPrototypeIndicesOut = outputs [ "bestPrototypeIndices" ]
        nPrototypes = len ( bestPrototypeIndicesOut )
    winner , inference , protoScores , categoryDistances = self . _knn . infer ( inputVector , partitionId = partition )
    if not self . keepAllDistances :
        self . _protoScores = protoScores
    else : # Keep all prototype scores in an array
        if self . _protoScores is None :
            self . _protoScores = numpy . zeros ( ( 1 , protoScores . shape [ 0 ] ) , protoScores . dtype )
            self . _protoScores [ 0 , : ] = protoScores
            # . reshape ( 1 , protoScores . shape [ 0 ] )
            self . _protoScoreCount = 1
        else :
            if self . _protoScoreCount == self . _protoScores . shape [ 0 ] : # Double the size of the array
                newProtoScores = numpy . zeros ( ( self . _protoScores . shape [ 0 ] * 2 , self . _protoScores . shape [ 1 ] ) , self . _protoScores . dtype )
                newProtoScores [ : self . _protoScores . shape [ 0 ] , : ] = self . _protoScores
                self . _protoScores = newProtoScores
            # Store the new prototype score
            self . _protoScores [ self . _protoScoreCount , : ] = protoScores
            self . _protoScoreCount += 1
    self . _categoryDistances = categoryDistances
    # Compute the probability of each category
    if self . outputProbabilitiesByDist :
        scores = 1.0 - self . _categoryDistances
    else :
        scores = inference
    # Probability is simply the scores / scores . sum ( )
    total = scores . sum ( )
    if total == 0 :
        numScores = len ( scores )
        probabilities = numpy . ones ( numScores ) / numScores
    else :
        probabilities = scores / total
    # Fill the output vectors with our results
    nout = min ( len ( categoriesOut ) , len ( inference ) )
    categoriesOut . fill ( 0 )
    categoriesOut [ 0 : nout ] = inference [ 0 : nout ]
    probabilitiesOut . fill ( 0 )
    probabilitiesOut [ 0 : nout ] = probabilities [ 0 : nout ]
    if self . verbosity >= 1 :
        print "KNNRegion: categoriesOut: " , categoriesOut [ 0 : nout ]
        print "KNNRegion: probabilitiesOut: " , probabilitiesOut [ 0 : nout ]
    if self . _scanInfo is not None :
        self . _scanResults = [ tuple ( inference [ : nout ] ) ]
    # Update the stored confusion matrix .
    for category in categories :
        if category >= 0 :
            dims = max ( int ( category ) + 1 , len ( inference ) )
            oldDims = len ( self . confusion )
            if oldDims < dims :
                confusion = numpy . zeros ( ( dims , dims ) )
                confusion [ 0 : oldDims , 0 : oldDims ] = self . confusion
                self . confusion = confusion
            self . confusion [ inference . argmax ( ) , int ( category ) ] += 1
    # Calculate the best prototype indices
    if nPrototypes > 1 :
        bestPrototypeIndicesOut . fill ( 0 )
        if categoryDistances is not None :
            indices = categoryDistances . argsort ( )
            nout = min ( len ( indices ) , nPrototypes )
            bestPrototypeIndicesOut [ 0 : nout ] = indices [ 0 : nout ]
    elif nPrototypes == 1 :
        if ( categoryDistances is not None ) and len ( categoryDistances ) :
            bestPrototypeIndicesOut [ 0 ] = categoryDistances . argmin ( )
        else :
            bestPrototypeIndicesOut [ 0 ] = 0
    # Logging
    self . handleLogOutput ( inference )
# Learning mode
if self . learningMode :
    if ( self . acceptanceProbability < 1.0 ) and ( self . _rgen . getReal64 ( ) > self . acceptanceProbability ) :
        pass
    else : # Accept the input
        for category in categories :
            if category >= 0 : # category values of - 1 are to be skipped ( they are non - categories )
                if self . _doSphering : # If we are sphering , then we can ' t provide the data to the KNN
                # library until we have computed per - dimension normalization
                # constants . So instead , we ' ll just store each training sample .
                    self . _storeSample ( inputVector , category , partition )
                else : # Pass the raw training sample directly to the KNN library .
                    self . _knn . learn ( inputVector , category , partition )
self . _epoch += 1


signature:
def _finishLearning ( self ) :
    """Does nothing . Kept here for API compatibility"""

implementation:
if self . _doSphering :
    self . _finishSphering ( )
self . _knn . finishLearning ( )
# Compute leave - one - out validation accuracy if
# we actually received non - trivial partition info
self . _accuracy = None


signature:
def _finishSphering ( self ) :
    """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 ."""

implementation:
# If we are sphering our data , we need to compute the
# per - dimension normalization constants
# First normalize the means ( to zero )
self . _normOffset = self . _samples . mean ( axis = 0 ) * - 1.0
self . _samples += self . _normOffset
# Now normalize the variances ( to one ) . However , we need to be
# careful because the variance could conceivably be zero for one
# or more dimensions .
variance = self . _samples . var ( axis = 0 )
variance [ numpy . where ( variance == 0.0 ) ] = 1.0
self . _normScale = 1.0 / numpy . sqrt ( variance )
self . _samples *= self . _normScale
# Now feed each " sphered " sample into the SVM library
for sampleIndex in range ( len ( self . _labels ) ) :
    self . _knn . learn ( self . _samples [ sampleIndex ] , self . _labels [ sampleIndex ] , self . _partitions [ sampleIndex ] )


signature:
def getOutputElementCount ( self , name ) :
    """Overrides : meth : ` nupic . bindings . regions . PyRegion . PyRegion . getOutputElementCount ` ."""

implementation:
if name == 'categoriesOut' :
    return self . maxCategoryCount
elif name == 'categoryProbabilitiesOut' :
    return self . maxCategoryCount
elif name == 'bestPrototypeIndices' :
    return self . _bestPrototypeIndexCount if self . _bestPrototypeIndexCount else 0
else :
    raise Exception ( 'Unknown output: ' + name )


signature:
def generateStats ( filename , statsInfo , maxSamples = None , filters = [ ] , cache = True ) :
    """Generate requested statistics for a dataset and cache to a file .
 If filename is None , then don ' t cache to a file"""

implementation:
# Sanity checking
if not isinstance ( statsInfo , dict ) :
    raise RuntimeError ( "statsInfo must be a dict -- " "found '%s' instead" % type ( statsInfo ) )
filename = resource_filename ( "nupic.datafiles" , filename )
if cache :
    statsFilename = getStatsFilename ( filename , statsInfo , filters )
    # Use cached stats if found AND if it has the right data
    if os . path . exists ( statsFilename ) :
        try :
            r = pickle . load ( open ( statsFilename , "rb" ) )
        except : # Ok to ignore errors - - we will just re - generate the file
            print "Warning: unable to load stats for %s -- " "will regenerate" % filename
            r = dict ( )
        requestedKeys = set ( [ s for s in statsInfo ] )
        availableKeys = set ( r . keys ( ) )
        unavailableKeys = requestedKeys . difference ( availableKeys )
        if len ( unavailableKeys ) == 0 :
            return r
        else :
            print "generateStats: re-generating stats file %s because " "keys %s are not available" % ( filename , str ( unavailableKeys ) )
            os . remove ( filename )
print "Generating statistics for file '%s' with filters '%s'" % ( filename , filters )
sensor = RecordSensor ( )
sensor . dataSource = FileRecordStream ( filename )
sensor . preEncodingFilters = filters
# Convert collector description to collector object
stats = [ ]
for field in statsInfo : # field = key from statsInfo
    if statsInfo [ field ] == "number" : # This wants a field name e . g . consumption and the field type as the value
        statsInfo [ field ] = NumberStatsCollector ( )
    elif statsInfo [ field ] == "category" :
        statsInfo [ field ] = CategoryStatsCollector ( )
    else :
        raise RuntimeError ( "Unknown stats type '%s' for field '%s'" % ( statsInfo [ field ] , field ) )
# Now collect the stats
if maxSamples is None :
    maxSamples = 500000
for i in xrange ( maxSamples ) :
    try :
        record = sensor . getNextRecord ( )
    except StopIteration :
        break
    for ( name , collector ) in statsInfo . items ( ) :
        collector . add ( record [ name ] )
del sensor
# Assemble the results and return
r = dict ( )
for ( field , collector ) in statsInfo . items ( ) :
    stats = collector . getStats ( )
    if field not in r :
        r [ field ] = stats
    else :
        r [ field ] . update ( stats )
if cache :
    f = open ( statsFilename , "wb" )
    pickle . dump ( r , f )
    f . close ( )
    # caller may need to know name of cached file
    r [ "_filename" ] = statsFilename
return r


signature:
def getScalarMetricWithTimeOfDayAnomalyParams ( metricData , minVal = None , maxVal = None , minResolution = None , tmImplementation = "cpp" ) :
    """Return a dict that can be used to create an anomaly model via
 : meth : ` nupic . frameworks . opf . model _ factory . ModelFactory . create ` .
 Example :
 . . code - block : : python
 from nupic . frameworks . opf . model _ factory import ModelFactory
 from nupic . frameworks . opf . common _ models . cluster _ params import (
 getScalarMetricWithTimeOfDayAnomalyParams )
 params = getScalarMetricWithTimeOfDayAnomalyParams (
 metricData = [ 0 ] ,
 tmImplementation = " cpp " ,
 minVal = 0.0,
 maxVal = 100.0)
 model = ModelFactory . create ( modelConfig = params [ " modelConfig " ] )
 model . enableLearning ( )
 model . enableInference ( params [ " inferenceArgs " ] )
 : param metricData : numpy array of metric data . Used to calculate ` ` minVal ` `
 and ` ` maxVal ` ` if either is unspecified
 : param minVal : minimum value of metric . Used to set up encoders . If ` ` None ` `
 will be derived from ` ` metricData ` ` .
 : param maxVal : maximum value of metric . Used to set up input encoders . If
 ` ` None ` ` will be derived from ` ` metricData ` `
 : param minResolution : minimum resolution of metric . Used to set up
 encoders . If ` ` None ` ` , will use default value of ` ` 0.001 ` ` .
 : param tmImplementation : ( string ) specifying type of temporal memory
 implementation . Valid strings : ` ` [ " cpp " , " tm _ cpp " ] ` `
 : returns : ( dict ) containing ` ` modelConfig ` ` and ` ` inferenceArgs ` ` top - level
 properties . The value of the ` ` modelConfig ` ` property is for passing to
 : meth : ` ~ nupic . frameworks . opf . model _ factory . ModelFactory . create ` method as
 the ` ` modelConfig ` ` parameter . The ` ` inferenceArgs ` ` property is for passing
 to the resulting model ' s
 : meth : ` ~ nupic . frameworks . opf . model . Model . enableInference ` method as the
 ` ` inferenceArgs ` ` parameter .
 . . note : : The timestamp field corresponds to input ` ` c0 ` ` ; the predicted
 field corresponds to input ` ` c1 ` ` ."""

implementation:
# Default values
if minResolution is None :
    minResolution = 0.001
# Compute min and / or max from the data if not specified
if minVal is None or maxVal is None :
    compMinVal , compMaxVal = _rangeGen ( metricData )
    if minVal is None :
        minVal = compMinVal
    if maxVal is None :
        maxVal = compMaxVal
# Handle the corner case where the incoming min and max are the same
if minVal == maxVal :
    maxVal = minVal + 1
# Load model parameters and update encoder params
if ( tmImplementation is "cpp" ) :
    paramFileRelativePath = os . path . join ( "anomaly_params_random_encoder" , "best_single_metric_anomaly_params_cpp.json" )
elif ( tmImplementation is "tm_cpp" ) :
    paramFileRelativePath = os . path . join ( "anomaly_params_random_encoder" , "best_single_metric_anomaly_params_tm_cpp.json" )
else :
    raise ValueError ( "Invalid string for tmImplementation. Try cpp or tm_cpp" )
with resource_stream ( __name__ , paramFileRelativePath ) as infile :
    paramSet = json . load ( infile )
_fixupRandomEncoderParams ( paramSet , minVal , maxVal , minResolution )
return paramSet


signature:
def _rangeGen ( data , std = 1 ) :
    """Return reasonable min / max values to use given the data ."""

implementation:
dataStd = np . std ( data )
if dataStd == 0 :
    dataStd = 1
minval = np . min ( data ) - std * dataStd
maxval = np . max ( data ) + std * dataStd
return minval , maxval


signature:
def _fixupRandomEncoderParams ( params , minVal , maxVal , minResolution ) :
    """Given model params , figure out the correct parameters for the
 RandomDistributed encoder . Modifies params in place ."""

implementation:
encodersDict = ( params [ "modelConfig" ] [ "modelParams" ] [ "sensorParams" ] [ "encoders" ] )
for encoder in encodersDict . itervalues ( ) :
    if encoder is not None :
        if encoder [ "type" ] == "RandomDistributedScalarEncoder" :
            resolution = max ( minResolution , ( maxVal - minVal ) / encoder . pop ( "numBuckets" ) )
            encodersDict [ "c1" ] [ "resolution" ] = resolution


signature:
def read ( cls , proto ) :
    """Intercepts TemporalMemory deserialization request in order to initialize
 ` TemporalMemoryMonitorMixin ` state
 @ param proto ( DynamicStructBuilder ) Proto object
 @ return ( TemporalMemory ) TemporalMemory shim instance"""

implementation:
tm = super ( TemporalMemoryMonitorMixin , cls ) . read ( proto )
# initialize ` TemporalMemoryMonitorMixin ` attributes
tm . mmName = None
tm . _mmTraces = None
tm . _mmData = None
tm . mmClearHistory ( )
tm . _mmResetActive = True
return tm


signature:
def read ( cls , proto ) :
    """Intercepts TemporalMemory deserialization request in order to initialize
 ` self . infActiveState `
 @ param proto ( DynamicStructBuilder ) Proto object
 @ return ( TemporalMemory ) TemporalMemory shim instance"""

implementation:
tm = super ( TMShimMixin , cls ) . read ( proto )
tm . infActiveState = { "t" : None }
return tm


signature:
def topDownCompute ( self , topDownIn = None ) :
    """( From ` backtracking _ tm . py ` )
 Top - down compute - generate expected input given output of the TM
 @ param topDownIn top down input from the level above us
 @ returns best estimate of the TM input that would have generated bottomUpOut ."""

implementation:
output = numpy . zeros ( self . numberOfColumns ( ) )
columns = [ self . columnForCell ( idx ) for idx in self . getPredictiveCells ( ) ]
output [ columns ] = 1
return output


implementation:
tm = super ( MonitoredTMShim , cls ) . read ( proto )
tm . infActiveState = { "t" : None }
return tm


signature:
def compute ( self , bottomUpInput , enableLearn , computeInfOutput = None ) :
    """( From ` backtracking _ tm . py ` )
 Handle one compute , possibly learning .
 @ param bottomUpInput The bottom - up input , typically from a spatial pooler
 @ param enableLearn If true , perform learning
 @ param computeInfOutput If None , default behavior is to disable the inference
 output when enableLearn is on .
 If true , compute the inference output
 If false , do not compute the inference output"""

implementation:
super ( MonitoredTMShim , self ) . compute ( set ( bottomUpInput . nonzero ( ) [ 0 ] ) , learn = enableLearn )
numberOfCells = self . numberOfCells ( )
activeState = numpy . zeros ( numberOfCells )
activeState [ self . getActiveCells ( ) ] = 1
self . infActiveState [ "t" ] = activeState
output = numpy . zeros ( numberOfCells )
output [ self . getPredictiveCells ( ) + self . getActiveCells ( ) ] = 1
return output


signature:
def pickByDistribution ( distribution , r = None ) :
    """Pick a value according to the provided distribution .
 Example :
 pickByDistribution ( [ . 2 , . 1 ] )
 Returns 0 two thirds of the time and 1 one third of the time .
 : param distribution : Probability distribution . Need not be normalized .
 : param r : Instance of random . Random . Uses the system instance if one is
 not provided ."""

implementation:
if r is None :
    r = random
x = r . uniform ( 0 , sum ( distribution ) )
for i , d in enumerate ( distribution ) :
    if x <= d :
        return i
    x -= d


signature:
def Indicator ( pos , size , dtype ) :
    """Returns an array of length size and type dtype that is everywhere 0,
 except in the index in pos .
 : param pos : ( int ) specifies the position of the one entry that will be set .
 : param size : ( int ) The total size of the array to be returned .
 : param dtype : The element type ( compatible with NumPy array ( ) )
 of the array to be returned .
 : returns : ( list ) of length ` ` size ` ` and element type ` ` dtype ` ` ."""

implementation:
x = numpy . zeros ( size , dtype = dtype )
x [ pos ] = 1
return x


signature:
def MultiArgMax ( x ) :
    """Get tuple ( actually a generator ) of indices where the max value of
 array x occurs . Requires that x have a max ( ) method , as x . max ( )
 ( in the case of NumPy ) is much faster than max ( x ) .
 For a simpler , faster argmax when there is only a single maximum entry ,
 or when knowing only the first index where the maximum occurs ,
 call argmax ( ) on a NumPy array .
 : param x : Any sequence that has a max ( ) method .
 : returns : Generator with the indices where the max value occurs ."""

implementation:
m = x . max ( )
return ( i for i , v in enumerate ( x ) if v == m )


signature:
def Any ( sequence ) :
    """Tests much faster ( 30 % ) than bool ( sum ( bool ( x ) for x in sequence ) ) .
 : returns : ( bool ) true if any element of the sequence satisfies True .
 : param sequence : Any sequence whose elements can be evaluated as booleans ."""

implementation: return bool ( reduce ( lambda x , y : x or y , sequence , False ) )


signature:
def All ( sequence ) :
    """: param sequence : Any sequence whose elements can be evaluated as booleans .
 : returns : true if all elements of the sequence satisfy True and x ."""

implementation: return bool ( reduce ( lambda x , y : x and y , sequence , True ) )


signature:
def MultiIndicator ( pos , size , dtype ) :
    """Returns an array of length size and type dtype that is everywhere 0,
 except in the indices listed in sequence pos .
 : param pos : A single integer or sequence of integers that specify
 the position of ones to be set .
 : param size : The total size of the array to be returned .
 : param dtype : The element type ( compatible with NumPy array ( ) )
 of the array to be returned .
 : returns : An array of length size and element type dtype ."""

implementation:
x = numpy . zeros ( size , dtype = dtype )
if hasattr ( pos , '__iter__' ) :
    for i in pos :
        x [ i ] = 1
else :
    x [ pos ] = 1
return x


signature:
def Distribution ( pos , size , counts , dtype ) :
    """Returns an array of length size and type dtype that is everywhere 0,
 except in the indices listed in sequence pos . The non - zero indices
 contain a normalized distribution based on the counts .
 : param pos : A single integer or sequence of integers that specify
 the position of ones to be set .
 : param size : The total size of the array to be returned .
 : param counts : The number of times we have observed each index .
 : param dtype : The element type ( compatible with NumPy array ( ) )
 of the array to be returned .
 : returns : An array of length size and element type dtype ."""

implementation:
x = numpy . zeros ( size , dtype = dtype )
if hasattr ( pos , '__iter__' ) : # calculate normalization constant
    total = 0
    for i in pos :
        total += counts [ i ]
    total = float ( total )
    # set included positions to normalized probability
    for i in pos :
        x [ i ] = counts [ i ] / total
# If we don ' t have a set of positions , assume there ' s only one position
else :
    x [ pos ] = 1
return x


signature:
def grow ( self , rows , cols ) :
    """Grows the histogram to have rows rows and cols columns .
 Must not have been initialized before , or already have the same
 number of columns .
 If rows is smaller than the current number of rows ,
 does not shrink .
 Also updates the sizes of the row and column sums .
 : param rows : Integer number of rows .
 : param cols : Integer number of columns ."""

implementation:
if not self . hist_ :
    self . hist_ = SparseMatrix ( rows , cols )
    self . rowSums_ = numpy . zeros ( rows , dtype = dtype )
    self . colSums_ = numpy . zeros ( cols , dtype = dtype )
    self . hack_ = None
else :
    oldRows = self . hist_ . nRows ( )
    oldCols = self . hist_ . nCols ( )
    nextRows = max ( oldRows , rows )
    nextCols = max ( oldCols , cols )
    if ( oldRows < nextRows ) or ( oldCols < nextCols ) :
        self . hist_ . resize ( nextRows , nextCols )
        if oldRows < nextRows :
            oldSums = self . rowSums_
            self . rowSums_ = numpy . zeros ( nextRows , dtype = dtype )
            self . rowSums_ [ 0 : len ( oldSums ) ] = oldSums
            self . hack_ = None
        if oldCols < nextCols :
            oldSums = self . colSums_
            self . colSums_ = numpy . zeros ( nextCols , dtype = dtype )
            self . colSums_ [ 0 : len ( oldSums ) ] = oldSums
            self . hack_ = None


signature:
def updateRow ( self , row , distribution ) :
    """Add distribution to row row .
 Distribution should be an array of probabilities or counts .
 : param row : Integer index of the row to add to .
 May be larger than the current number of rows , in which case
 the histogram grows .
 : param distribution : Array of length equal to the number of columns ."""

implementation:
self . grow ( row + 1 , len ( distribution ) )
self . hist_ . axby ( row , 1 , 1 , distribution )
self . rowSums_ [ row ] += distribution . sum ( )
self . colSums_ += distribution
self . hack_ = None


signature:
def inferRowCompat ( self , distribution ) :
    """Equivalent to the category inference of zeta1 . TopLevel .
 Computes the max _ prod ( maximum component of a component - wise multiply )
 between the rows of the histogram and the incoming distribution .
 May be slow if the result of clean _ outcpd ( ) is not valid .
 : param distribution : Array of length equal to the number of columns .
 : returns : array of length equal to the number of rows ."""

implementation:
if self . hack_ is None :
    self . clean_outcpd ( )
return self . hack_ . vecMaxProd ( distribution )


signature:
def clean_outcpd ( self ) :
    """Hack to act like clean _ outcpd on zeta1 . TopLevelNode .
 Take the max element in each to column , set it to 1 , and set all the
 other elements to 0.
 Only called by inferRowMaxProd ( ) and only needed if an updateRow ( )
 has been called since the last clean _ outcpd ( ) ."""

implementation:
m = self . hist_ . toDense ( )
for j in xrange ( m . shape [ 1 ] ) : # For each column .
    cmax = m [ : , j ] . max ( )
    if cmax :
        m [ : , j ] = numpy . array ( m [ : , j ] == cmax , dtype = dtype )
self . hack_ = SparseMatrix ( 0 , self . hist_ . nCols ( ) )
for i in xrange ( m . shape [ 0 ] ) :
    self . hack_ . addRow ( m [ i , : ] )


signature:
def importAndRunFunction ( path , moduleName , funcName , ** keywords ) :
    """Run a named function specified by a filesystem path , module name
 and function name .
 Returns the value returned by the imported function .
 Use this when access is needed to code that has
 not been added to a package accessible from the ordinary Python
 path . Encapsulates the multiple lines usually needed to
 safely manipulate and restore the Python path .
 Parameters
 path : filesystem path
 Path to the directory where the desired module is stored .
 This will be used to temporarily augment the Python path .
 moduleName : basestring
 Name of the module , without trailing extension , where the desired
 function is stored . This module should be in the directory specified
 with path .
 funcName : basestring
 Name of the function to import and call .
 keywords :
 Keyword arguments to be passed to the imported function ."""

implementation:
import sys
originalPath = sys . path
try :
    augmentedPath = [ path ] + sys . path
    sys . path = augmentedPath
    func = getattr ( __import__ ( moduleName , fromlist = [ funcName ] ) , funcName )
    sys . path = originalPath
except : # Restore the original path in case of an exception .
    sys . path = originalPath
    raise
return func ( ** keywords )


signature:
def transferCoincidences ( network , fromElementName , toElementName ) :
    """Gets the coincidence matrix from one element and sets it on
 another element
 ( using locked handles , a la nupic . bindings . research . lockHandle ) .
 TODO : Generalize to more node types , parameter name pairs , etc .
 Does not work across processes ."""

implementation:
coincidenceHandle = getLockedHandle ( runtimeElement = network . getElement ( fromElementName ) , # TODO : Re - purpose for use with nodes other than PMXClassifierNode .
expression = "self._cd._W" )
network . getElement ( toElementName ) . setParameter ( "coincidencesAbove" , coincidenceHandle )


signature:
def compute ( slidingWindow , total , newVal , windowSize ) :
    """Routine for computing a moving average .
 @ param slidingWindow a list of previous values to use in computation that
 will be modified and returned
 @ param total the sum of the values in slidingWindow to be used in the
 calculation of the moving average
 @ param newVal a new number compute the new windowed average
 @ param windowSize how many values to use in the moving window
 @ returns an updated windowed average , the modified input slidingWindow list ,
 and the new total sum of the sliding window"""

implementation:
if len ( slidingWindow ) == windowSize :
    total -= slidingWindow . pop ( 0 )
slidingWindow . append ( newVal )
total += newVal
return float ( total ) / len ( slidingWindow ) , slidingWindow , total


signature:
def next ( self , newValue ) :
    """Instance method wrapper around compute ."""

implementation:
newAverage , self . slidingWindow , self . total = self . compute ( self . slidingWindow , self . total , newValue , self . windowSize )
return newAverage


signature:
def getModule ( metricSpec ) :
    """Factory method to return an appropriate : class : ` MetricsIface ` module .
 - ` ` rmse ` ` : : class : ` MetricRMSE `
 - ` ` nrmse ` ` : : class : ` MetricNRMSE `
 - ` ` aae ` ` : : class : ` MetricAAE `
 - ` ` acc ` ` : : class : ` MetricAccuracy `
 - ` ` avg _ err ` ` : : class : ` MetricAveError `
 - ` ` trivial ` ` : : class : ` MetricTrivial `
 - ` ` two _ gram ` ` : : class : ` MetricTwoGram `
 - ` ` moving _ mean ` ` : : class : ` MetricMovingMean `
 - ` ` moving _ mode ` ` : : class : ` MetricMovingMode `
 - ` ` neg _ auc ` ` : : class : ` MetricNegAUC `
 - ` ` custom _ error _ metric ` ` : : class : ` CustomErrorMetric `
 - ` ` multiStep ` ` : : class : ` MetricMultiStep `
 - ` ` ms _ aae ` ` : : class : ` MetricMultiStepAAE `
 - ` ` ms _ avg _ err ` ` : : class : ` MetricMultiStepAveError `
 - ` ` passThruPrediction ` ` : : class : ` MetricPassThruPrediction `
 - ` ` altMAPE ` ` : : class : ` MetricAltMAPE `
 - ` ` MAPE ` ` : : class : ` MetricMAPE `
 - ` ` multi ` ` : : class : ` MetricMulti `
 - ` ` negativeLogLikelihood ` ` : : class : ` MetricNegativeLogLikelihood `
 : param metricSpec : ( : class : ` MetricSpec ` ) metric to find module for .
 ` ` metricSpec . metric ` ` must be in the list above .
 : returns : ( : class : ` AggregateMetric ` ) an appropriate metric module"""

implementation:
metricName = metricSpec . metric
if metricName == 'rmse' :
    return MetricRMSE ( metricSpec )
if metricName == 'nrmse' :
    return MetricNRMSE ( metricSpec )
elif metricName == 'aae' :
    return MetricAAE ( metricSpec )
elif metricName == 'acc' :
    return MetricAccuracy ( metricSpec )
elif metricName == 'avg_err' :
    return MetricAveError ( metricSpec )
elif metricName == 'trivial' :
    return MetricTrivial ( metricSpec )
elif metricName == 'two_gram' :
    return MetricTwoGram ( metricSpec )
elif metricName == 'moving_mean' :
    return MetricMovingMean ( metricSpec )
elif metricName == 'moving_mode' :
    return MetricMovingMode ( metricSpec )
elif metricName == 'neg_auc' :
    return MetricNegAUC ( metricSpec )
elif metricName == 'custom_error_metric' :
    return CustomErrorMetric ( metricSpec )
elif metricName == 'multiStep' :
    return MetricMultiStep ( metricSpec )
elif metricName == 'multiStepProbability' :
    return MetricMultiStepProbability ( metricSpec )
elif metricName == 'ms_aae' :
    return MetricMultiStepAAE ( metricSpec )
elif metricName == 'ms_avg_err' :
    return MetricMultiStepAveError ( metricSpec )
elif metricName == 'passThruPrediction' :
    return MetricPassThruPrediction ( metricSpec )
elif metricName == 'altMAPE' :
    return MetricAltMAPE ( metricSpec )
elif metricName == 'MAPE' :
    return MetricMAPE ( metricSpec )
elif metricName == 'multi' :
    return MetricMulti ( metricSpec )
elif metricName == 'negativeLogLikelihood' :
    return MetricNegativeLogLikelihood ( metricSpec )
else :
    raise Exception ( "Unsupported metric type: %s" % metricName )


signature:
def getLabel ( self , inferenceType = None ) :
    """Helper method that generates a unique label for a : class : ` MetricSpec ` /
 : class : ` ~ nupic . frameworks . opf . opf _ utils . InferenceType ` pair . The label is
 formatted as follows :
 < predictionKind > : < metric type > : ( paramName = value ) * : field = < fieldname >
 For example :
 classification : aae : paramA = 10.2 : paramB = 20 : window = 100 : field = pounds
 : returns : ( string ) label for inference type"""

implementation:
result = [ ]
if inferenceType is not None :
    result . append ( InferenceType . getLabel ( inferenceType ) )
result . append ( self . inferenceElement )
result . append ( self . metric )
params = self . params
if params is not None :
    sortedParams = params . keys ( )
    sortedParams . sort ( )
    for param in sortedParams : # Don ' t include the customFuncSource - it is too long an unwieldy
        if param in ( 'customFuncSource' , 'customFuncDef' , 'customExpr' ) :
            continue
        value = params [ param ]
        if isinstance ( value , str ) :
            result . extend ( [ "%s='%s'" % ( param , value ) ] )
        else :
            result . extend ( [ "%s=%s" % ( param , value ) ] )
if self . field :
    result . append ( "field=%s" % ( self . field ) )
return self . _LABEL_SEPARATOR . join ( result )


signature:
def getInferenceTypeFromLabel ( cls , label ) :
    """Extracts the PredictionKind ( temporal vs . nontemporal ) from the given
 metric label .
 : param label : ( string ) for a metric spec generated by
 : meth : ` getMetricLabel `
 : returns : ( : class : ` ~ nupic . frameworks . opf . opf _ utils . InferenceType ` )"""

implementation:
infType , _ , _ = label . partition ( cls . _LABEL_SEPARATOR )
if not InferenceType . validate ( infType ) :
    return None
return infType


signature:
def _getShiftedGroundTruth ( self , groundTruth ) :
    """Utility function that saves the passed in groundTruth into a local
 history buffer , and returns the groundTruth from self . _ predictionSteps ago ,
 where self . _ predictionSteps is defined by the ' steps ' parameter .
 This can be called from the beginning of a derived class ' s addInstance ( )
 before it passes groundTruth and prediction onto accumulate ( ) ."""

implementation:
# Save this ground truth into our input history
self . _groundTruthHistory . append ( groundTruth )
# This is only supported when _ predictionSteps has one item in it
assert ( len ( self . _predictionSteps ) == 1 )
# Return the one from N steps ago
if len ( self . _groundTruthHistory ) > self . _predictionSteps [ 0 ] :
    return self . _groundTruthHistory . popleft ( )
else :
    if hasattr ( groundTruth , '__iter__' ) :
        return [ None ] * len ( groundTruth )
    else :
        return None


signature:
def addInstance ( self , groundTruth , prediction , record = None , result = None ) :
    """Compute and store metric value"""

implementation: self . value = self . avg ( prediction )


signature:
def mostLikely ( self , pred ) :
    """Helper function to return a scalar value representing the most
 likely outcome given a probability distribution"""

implementation:
if len ( pred ) == 1 :
    return pred . keys ( ) [ 0 ]
mostLikelyOutcome = None
maxProbability = 0
for prediction , probability in pred . items ( ) :
    if probability > maxProbability :
        mostLikelyOutcome = prediction
        maxProbability = probability
return mostLikelyOutcome


signature:
def expValue ( self , pred ) :
    """Helper function to return a scalar value representing the expected
 value of a probability distribution"""

implementation:
if len ( pred ) == 1 :
    return pred . keys ( ) [ 0 ]
return sum ( [ x * p for x , p in pred . items ( ) ] )


signature:
def encode ( self , inputData ) :
    """Convenience wrapper for : meth : ` . encodeIntoArray ` .
 This may be less efficient because it allocates a new numpy array every
 call .
 : param inputData : input data to be encoded
 : return : a numpy array with the encoded representation of inputData"""

implementation:
output = numpy . zeros ( ( self . getWidth ( ) , ) , dtype = defaultDtype )
self . encodeIntoArray ( inputData , output )
return output


signature:
def getScalarNames ( self , parentFieldName = '' ) :
    """Return the field names for each of the scalar values returned by
 getScalars .
 : param parentFieldName : The name of the encoder which is our parent . This
 name is prefixed to each of the field names within this encoder to
 form the keys of the dict ( ) in the retval .
 : return : array of field names"""

implementation:
names = [ ]
if self . encoders is not None :
    for ( name , encoder , offset ) in self . encoders :
        subNames = encoder . getScalarNames ( parentFieldName = name )
        if parentFieldName != '' :
            subNames = [ '%s.%s' % ( parentFieldName , name ) for name in subNames ]
        names . extend ( subNames )
else :
    if parentFieldName != '' :
        names . append ( parentFieldName )
    else :
        names . append ( self . name )
return names


signature:
def getDecoderOutputFieldTypes ( self ) :
    """Returns a sequence of field types corresponding to the elements in the
 decoded output field array . The types are defined by
 : class : ` ~ nupic . data . field _ meta . FieldMetaType ` .
 : return : list of : class : ` ~ nupic . data . field _ meta . FieldMetaType ` objects"""

implementation:
if hasattr ( self , '_flattenedFieldTypeList' ) and self . _flattenedFieldTypeList is not None :
    return self . _flattenedFieldTypeList
fieldTypes = [ ]
# NOTE : we take care of the composites , but leaf encoders must override
# this method and return a list of one field _ meta . FieldMetaType . XXXX
# element corresponding to the encoder ' s decoder output field type
for ( name , encoder , offset ) in self . encoders :
    subTypes = encoder . getDecoderOutputFieldTypes ( )
    fieldTypes . extend ( subTypes )
self . _flattenedFieldTypeList = fieldTypes
return fieldTypes


signature:
def _getInputValue ( self , obj , fieldName ) :
    """Gets the value of a given field from the input record"""

implementation:
if isinstance ( obj , dict ) :
    if not fieldName in obj :
        knownFields = ", " . join ( key for key in obj . keys ( ) if not key . startswith ( "_" ) )
        raise ValueError ( "Unknown field name '%s' in input record. Known fields are '%s'.\n" "This could be because input headers are mislabeled, or because " "input data rows do not contain a value for '%s'." % ( fieldName , knownFields , fieldName ) )
    return obj [ fieldName ]
else :
    return getattr ( obj , fieldName )


signature:
def getEncoderList ( self ) :
    """: return : a reference to each sub - encoder in this encoder . They are
 returned in the same order as they are for : meth : ` . getScalarNames `
 and : meth : ` . getScalars ` ."""

implementation:
if hasattr ( self , '_flattenedEncoderList' ) and self . _flattenedEncoderList is not None :
    return self . _flattenedEncoderList
encoders = [ ]
if self . encoders is not None :
    for ( name , encoder , offset ) in self . encoders :
        subEncoders = encoder . getEncoderList ( )
        encoders . extend ( subEncoders )
else :
    encoders . append ( self )
self . _flattenedEncoderList = encoders
return encoders


signature:
def getScalars ( self , inputData ) :
    """Returns a numpy array containing the sub - field scalar value ( s ) for
 each sub - field of the ` ` inputData ` ` . To get the associated field names for
 each of the scalar values , call : meth : ` . getScalarNames ( ) ` .
 For a simple scalar encoder , the scalar value is simply the input unmodified .
 For category encoders , it is the scalar representing the category string
 that is passed in . For the datetime encoder , the scalar value is the
 the number of seconds since epoch .
 The intent of the scalar representation of a sub - field is to provide a
 baseline for measuring error differences . You can compare the scalar value
 of the inputData with the scalar value returned from : meth : ` . topDownCompute `
 on a top - down representation to evaluate prediction accuracy , for example .
 : param inputData : The data from the source . This is typically an object with
 members
 : return : array of scalar values"""

implementation:
retVals = numpy . array ( [ ] )
if self . encoders is not None :
    for ( name , encoder , offset ) in self . encoders :
        values = encoder . getScalars ( self . _getInputValue ( inputData , name ) )
        retVals = numpy . hstack ( ( retVals , values ) )
else :
    retVals = numpy . hstack ( ( retVals , inputData ) )
return retVals


signature:
def getEncodedValues ( self , inputData ) :
    """Returns the input in the same format as is returned by
 : meth : ` . topDownCompute ` . For most encoder types , this is the same as the
 input data . For instance , for scalar and category types , this corresponds to
 the numeric and string values , respectively , from the inputs . For datetime
 encoders , this returns the list of scalars for each of the sub - fields
 ( timeOfDay , dayOfWeek , etc . )
 This method is essentially the same as : meth : ` . getScalars ` except that it
 returns strings .
 : param inputData : The input data in the format it is received from the data
 source
 : return : A list of values , in the same format and in the same order as they
 are returned by : meth : ` . topDownCompute ` ."""

implementation:
retVals = [ ]
if self . encoders is not None :
    for name , encoders , offset in self . encoders :
        values = encoders . getEncodedValues ( self . _getInputValue ( inputData , name ) )
        if _isSequence ( values ) :
            retVals . extend ( values )
        else :
            retVals . append ( values )
else :
    if _isSequence ( inputData ) :
        retVals . extend ( inputData )
    else :
        retVals . append ( inputData )
return tuple ( retVals )


signature:
def getBucketIndices ( self , inputData ) :
    """Returns an array containing the sub - field bucket indices for each sub - field
 of the inputData . To get the associated field names for each of the buckets ,
 call : meth : ` . getScalarNames ` .
 : param inputData : The data from the source . This is typically an object with
 members .
 : return : array of bucket indices"""

implementation:
retVals = [ ]
if self . encoders is not None :
    for ( name , encoder , offset ) in self . encoders :
        values = encoder . getBucketIndices ( self . _getInputValue ( inputData , name ) )
        retVals . extend ( values )
else :
    assert False , "Should be implemented in base classes that are not " "containers for other encoders"
return retVals


signature:
def scalarsToStr ( self , scalarValues , scalarNames = None ) :
    """Return a pretty print string representing the return values from
 : meth : ` . getScalars ` and : meth : ` . getScalarNames ` .
 : param scalarValues : input values to encode to string
 : param scalarNames : optional input of scalar names to convert . If None , gets
 scalar names from : meth : ` . getScalarNames `
 : return : string representation of scalar values"""

implementation:
if scalarNames is None :
    scalarNames = self . getScalarNames ( )
desc = ''
for ( name , value ) in zip ( scalarNames , scalarValues ) :
    if len ( desc ) > 0 :
        desc += ", %s:%.2f" % ( name , value )
    else :
        desc += "%s:%.2f" % ( name , value )
return desc


signature:
def getFieldDescription ( self , fieldName ) :
    """Return the offset and length of a given field within the encoded output .
 : param fieldName : Name of the field
 : return : tuple ( ` ` offset ` ` , ` ` width ` ` ) of the field within the encoded output"""

implementation:
# Find which field it ' s in
description = self . getDescription ( ) + [ ( "end" , self . getWidth ( ) ) ]
for i in xrange ( len ( description ) ) :
    ( name , offset ) = description [ i ]
    if ( name == fieldName ) :
        break
if i >= len ( description ) - 1 :
    raise RuntimeError ( "Field name %s not found in this encoder" % fieldName )
# Return the offset and width
return ( offset , description [ i + 1 ] [ 1 ] - offset )


signature:
def encodedBitDescription ( self , bitOffset , formatted = False ) :
    """Return a description of the given bit in the encoded output .
 This will include the field name and the offset within the field .
 : param bitOffset : Offset of the bit to get the description of
 : param formatted : If True , the bitOffset is w . r . t . formatted output ,
 which includes separators
 : return : tuple ( ` ` fieldName ` ` , ` ` offsetWithinField ` ` )"""

implementation:
# Find which field it ' s in
( prevFieldName , prevFieldOffset ) = ( None , None )
description = self . getDescription ( )
for i in xrange ( len ( description ) ) :
    ( name , offset ) = description [ i ]
    if formatted :
        offset = offset + i
        if bitOffset == offset - 1 :
            prevFieldName = "separator"
            prevFieldOffset = bitOffset
            break
    if bitOffset < offset :
        break
    ( prevFieldName , prevFieldOffset ) = ( name , offset )
# Return the field name and offset within the field
# return ( fieldName , bitOffset - fieldOffset )
width = self . getDisplayWidth ( ) if formatted else self . getWidth ( )
if prevFieldOffset is None or bitOffset > self . getWidth ( ) :
    raise IndexError ( "Bit is outside of allowable range: [0 - %d]" % width )
return ( prevFieldName , bitOffset - prevFieldOffset )


signature:
def pprintHeader ( self , prefix = "" ) :
    """Pretty - print a header that labels the sub - fields of the encoded
 output . This can be used in conjuction with : meth : ` . pprint ` .
 : param prefix : printed before the header if specified"""

implementation:
print prefix ,
description = self . getDescription ( ) + [ ( "end" , self . getWidth ( ) ) ]
for i in xrange ( len ( description ) - 1 ) :
    name = description [ i ] [ 0 ]
    width = description [ i + 1 ] [ 1 ] - description [ i ] [ 1 ]
    formatStr = "%%-%ds |" % width
    if len ( name ) > width :
        pname = name [ 0 : width ]
    else :
        pname = name
    print formatStr % pname ,
print
print prefix , "-" * ( self . getWidth ( ) + ( len ( description ) - 1 ) * 3 - 1 )


signature:
def pprint ( self , output , prefix = "" ) :
    """Pretty - print the encoded output using ascii art .
 : param output : to print
 : param prefix : printed before the header if specified"""

implementation:
print prefix ,
description = self . getDescription ( ) + [ ( "end" , self . getWidth ( ) ) ]
for i in xrange ( len ( description ) - 1 ) :
    offset = description [ i ] [ 1 ]
    nextoffset = description [ i + 1 ] [ 1 ]
    print "%s |" % bitsToString ( output [ offset : nextoffset ] ) ,
print


signature:
def decode ( self , encoded , parentFieldName = '' ) :
    """Takes an encoded output and does its best to work backwards and generate
 the input that would have generated it .
 In cases where the encoded output contains more ON bits than an input
 would have generated , this routine will return one or more ranges of inputs
 which , if their encoded outputs were ORed together , would produce the
 target output . This behavior makes this method suitable for doing things
 like generating a description of a learned coincidence in the SP , which
 in many cases might be a union of one or more inputs .
 If instead , you want to figure the * most likely * single input scalar value
 that would have generated a specific encoded output , use the
 : meth : ` . topDownCompute ` method .
 If you want to pretty print the return value from this method , use the
 : meth : ` . decodedToStr ` method .
 : param encoded : The encoded output that you want decode
 : param parentFieldName : The name of the encoder which is our parent . This name
 is prefixed to each of the field names within this encoder to form the
 keys of the dict ( ) in the retval .
 : return : tuple ( ` ` fieldsDict ` ` , ` ` fieldOrder ` ` )
 ` ` fieldsDict ` ` is a dict ( ) where the keys represent field names
 ( only 1 if this is a simple encoder , > 1 if this is a multi
 or date encoder ) and the values are the result of decoding each
 field . If there are no bits in encoded that would have been
 generated by a field , it won ' t be present in the dict . The
 key of each entry in the dict is formed by joining the passed in
 parentFieldName with the child encoder name using a ' . ' .
 Each ' value ' in ` ` fieldsDict ` ` consists of ( ranges , desc ) , where
 ranges is a list of one or more ( minVal , maxVal ) ranges of
 input that would generate bits in the encoded output and ' desc '
 is a pretty print description of the ranges . For encoders like
 the category encoder , the ' desc ' will contain the category
 names that correspond to the scalar values included in the
 ranges .
 ` ` fieldOrder ` ` is a list of the keys from ` ` fieldsDict ` ` , in the
 same order as the fields appear in the encoded output .
 TODO : when we switch to Python 2.7 or 3 . x , use OrderedDict
 Example retvals for a scalar encoder :
 . . code - block : : python
 { ' amount ' : ( [ [ 1,3 ] , [ 7,10 ] ] , ' 1-3 , 7-10 ' ) }
 { ' amount ' : ( [ [ 2.5,2.5 ] ] , ' 2.5 ' ) }
 Example retval for a category encoder :
 . . code - block : : python
 { ' country ' : ( [ [ 1,1 ] , [ 5,6 ] ] , ' US , GB , ES ' ) }
 Example retval for a multi encoder :
 . . code - block : : python
 { ' amount ' : ( [ [ 2.5,2.5 ] ] , ' 2.5 ' ) ,
 ' country ' : ( [ [ 1,1 ] , [ 5,6 ] ] , ' US , GB , ES ' ) }"""

implementation:
fieldsDict = dict ( )
fieldsOrder = [ ]
# What is the effective parent name ?
if parentFieldName == '' :
    parentName = self . name
else :
    parentName = "%s.%s" % ( parentFieldName , self . name )
if self . encoders is not None : # Merge decodings of all child encoders together
    for i in xrange ( len ( self . encoders ) ) : # Get the encoder and the encoded output
        ( name , encoder , offset ) = self . encoders [ i ]
        if i < len ( self . encoders ) - 1 :
            nextOffset = self . encoders [ i + 1 ] [ 2 ]
        else :
            nextOffset = self . width
        fieldOutput = encoded [ offset : nextOffset ]
        ( subFieldsDict , subFieldsOrder ) = encoder . decode ( fieldOutput , parentFieldName = parentName )
        fieldsDict . update ( subFieldsDict )
        fieldsOrder . extend ( subFieldsOrder )
return ( fieldsDict , fieldsOrder )


signature:
def decodedToStr ( self , decodeResults ) :
    """Return a pretty print string representing the return value from
 : meth : ` . decode ` ."""

implementation:
( fieldsDict , fieldsOrder ) = decodeResults
desc = ''
for fieldName in fieldsOrder :
    ( ranges , rangesStr ) = fieldsDict [ fieldName ]
    if len ( desc ) > 0 :
        desc += ", %s:" % ( fieldName )
    else :
        desc += "%s:" % ( fieldName )
    desc += "[%s]" % ( rangesStr )
return desc


signature:
def getBucketInfo ( self , buckets ) :
    """Returns a list of : class : ` . EncoderResult ` namedtuples describing the inputs
 for each sub - field that correspond to the bucket indices passed in
 ` ` buckets ` ` . To get the associated field names for each of the values , call
 : meth : ` . getScalarNames ` .
 : param buckets : The list of bucket indices , one for each sub - field encoder .
 These bucket indices for example may have been retrieved
 from the : meth : ` . getBucketIndices ` call .
 : return : A list of : class : ` . EncoderResult ` ."""

implementation:
# Fall back topdown compute
if self . encoders is None :
    raise RuntimeError ( "Must be implemented in sub-class" )
# Concatenate the results from bucketInfo on each child encoder
retVals = [ ]
bucketOffset = 0
for i in xrange ( len ( self . encoders ) ) :
    ( name , encoder , offset ) = self . encoders [ i ]
    if encoder . encoders is not None :
        nextBucketOffset = bucketOffset + len ( encoder . encoders )
    else :
        nextBucketOffset = bucketOffset + 1
    bucketIndices = buckets [ bucketOffset : nextBucketOffset ]
    values = encoder . getBucketInfo ( bucketIndices )
    retVals . extend ( values )
    bucketOffset = nextBucketOffset
return retVals


signature:
def topDownCompute ( self , encoded ) :
    """Returns a list of : class : ` . EncoderResult ` namedtuples describing the
 top - down best guess inputs for each sub - field given the encoded output .
 These are the values which are most likely to generate the given encoded
 output . To get the associated field names for each of the values , call
 : meth : ` . getScalarNames ` .
 : param encoded : The encoded output . Typically received from the topDown
 outputs from the spatial pooler just above us .
 : return : A list of : class : ` . EncoderResult `"""

implementation:
# Fallback topdown compute
if self . encoders is None :
    raise RuntimeError ( "Must be implemented in sub-class" )
# Concatenate the results from topDownCompute on each child encoder
retVals = [ ]
for i in xrange ( len ( self . encoders ) ) :
    ( name , encoder , offset ) = self . encoders [ i ]
    if i < len ( self . encoders ) - 1 :
        nextOffset = self . encoders [ i + 1 ] [ 2 ]
    else :
        nextOffset = self . width
    fieldOutput = encoded [ offset : nextOffset ]
    values = encoder . topDownCompute ( fieldOutput )
    if _isSequence ( values ) :
        retVals . extend ( values )
    else :
        retVals . append ( values )
return retVals


signature:
def closenessScores ( self , expValues , actValues , fractional = True ) :
    """Compute closeness scores between the expected scalar value ( s ) and actual
 scalar value ( s ) . The expected scalar values are typically those obtained
 from the : meth : ` . getScalars ` method . The actual scalar values are typically
 those returned from : meth : ` . topDownCompute ` .
 This method returns one closeness score for each value in expValues ( or
 actValues which must be the same length ) . The closeness score ranges from
 0 to 1.0 , 1.0 being a perfect match and 0 being the worst possible match .
 If this encoder is a simple , single field encoder , then it will expect
 just 1 item in each of the ` ` expValues ` ` and ` ` actValues ` ` arrays .
 Multi - encoders will expect 1 item per sub - encoder .
 Each encoder type can define it ' s own metric for closeness . For example ,
 a category encoder may return either 1 or 0 , if the scalar matches exactly
 or not . A scalar encoder might return a percentage match , etc .
 : param expValues : Array of expected scalar values , typically obtained from
 : meth : ` . getScalars `
 : param actValues : Array of actual values , typically obtained from
 : meth : ` . topDownCompute `
 : return : Array of closeness scores , one per item in expValues ( or
 actValues ) ."""

implementation:
# Fallback closenss is a percentage match
if self . encoders is None :
    err = abs ( expValues [ 0 ] - actValues [ 0 ] )
    if fractional :
        denom = max ( expValues [ 0 ] , actValues [ 0 ] )
        if denom == 0 :
            denom = 1.0
        closeness = 1.0 - float ( err ) / denom
        if closeness < 0 :
            closeness = 0
    else :
        closeness = err
    return numpy . array ( [ closeness ] )
# Concatenate the results from closeness scores on each child encoder
scalarIdx = 0
retVals = numpy . array ( [ ] )
for ( name , encoder , offset ) in self . encoders :
    values = encoder . closenessScores ( expValues [ scalarIdx : ] , actValues [ scalarIdx : ] , fractional = fractional )
    scalarIdx += len ( values )
    retVals = numpy . hstack ( ( retVals , values ) )
return retVals


signature:
def POST ( self , name ) :
    """/ models / { name }
 schema :
 " modelParams " : dict containing model parameters
 " predictedFieldName " : str
 returns :
 { " success " : name }"""

implementation:
global g_models
data = json . loads ( web . data ( ) )
modelParams = data [ "modelParams" ]
predictedFieldName = data [ "predictedFieldName" ]
if name in g_models . keys ( ) :
    raise web . badrequest ( "Model with name <%s> already exists" % name )
model = ModelFactory . create ( modelParams )
model . enableInference ( { 'predictedField' : predictedFieldName } )
g_models [ name ] = model
return json . dumps ( { "success" : name } )


signature:
def POST ( self , name ) :
    """/ models / { name } / run
 schema :
 predictedFieldName : value
 timestamp : % m / % d / % y % H : % M
 NOTE : predictedFieldName MUST be the same name specified when
 creating the model .
 returns :
 " predictionNumber " : < number of record > ,
 " anomalyScore " : anomalyScore"""

implementation:
global g_models
data = json . loads ( web . data ( ) )
data [ "timestamp" ] = datetime . datetime . strptime ( data [ "timestamp" ] , "%m/%d/%y %H:%M" )
if name not in g_models . keys ( ) :
    raise web . notfound ( "Model with name <%s> does not exist." % name )
modelResult = g_models [ name ] . run ( data )
predictionNumber = modelResult . predictionNumber
anomalyScore = modelResult . inferences [ "anomalyScore" ]
return json . dumps ( { "predictionNumber" : predictionNumber , "anomalyScore" : anomalyScore } )


signature:
def analyzeOverlaps ( activeCoincsFile , encodingsFile , dataset ) :
    '''Mirror Image Visualization : Shows the encoding space juxtaposed against the
 coincidence space . The encoding space is the bottom - up sensory encoding and
 the coincidence space depicts the corresponding activation of coincidences in
 the SP . Hence , the mirror image visualization is a visual depiction of the
 mapping of SP cells to the input representations .
 Note :
 * The files spBUOut and sensorBUOut are assumed to be in the output format
 used for LPF experiment outputs .
 * BU outputs for some sample datasets are provided . Specify the name of the
 dataset as an option while running this script .'''

implementation:
lines = activeCoincsFile . readlines ( )
inputs = encodingsFile . readlines ( )
w = len ( inputs [ 0 ] . split ( ' ' ) ) - 1
patterns = set ( [ ] )
encodings = set ( [ ] )
coincs = [ ]
# The set of all coincidences that have won at least once
reUsedCoincs = [ ]
firstLine = inputs [ 0 ] . split ( ' ' )
size = int ( firstLine . pop ( 0 ) )
spOutput = np . zeros ( ( len ( lines ) , 40 ) )
inputBits = np . zeros ( ( len ( lines ) , w ) )
print 'Total n:' , size
print 'Total number of records in the file:' , len ( lines ) , '\n'
print 'w:' , w
count = 0
for x in xrange ( len ( lines ) ) :
    inputSpace = [ ]
    # Encoded representation for each input
    spBUout = [ int ( z ) for z in lines [ x ] . split ( ' ' ) ]
    spBUout . pop ( 0 )
    # The first element of each row of spBUOut is the size of the SP
    temp = set ( spBUout )
    spOutput [ x ] = spBUout
    input = [ int ( z ) for z in inputs [ x ] . split ( ' ' ) ]
    input . pop ( 0 )
    # The first element of each row of sensorBUout is the size of the encoding space
    tempInput = set ( input )
    inputBits [ x ] = input
    # Creating the encoding space
    for m in xrange ( size ) :
        if m in tempInput :
            inputSpace . append ( m )
        else :
            inputSpace . append ( '|' )
    # A non - active bit
    repeatedBits = tempInput . intersection ( encodings )
    # Storing the bits that have been previously active
    reUsed = temp . intersection ( patterns )
    # Checking if any of the active cells have been previously active
    # Dividing the coincidences into two difference categories .
    if len ( reUsed ) == 0 :
        coincs . append ( ( count , temp , repeatedBits , inputSpace , tempInput ) )
        # Pattern no , active cells , repeated bits , encoding ( full ) , encoding ( summary )
    else :
        reUsedCoincs . append ( ( count , temp , repeatedBits , inputSpace , tempInput ) )
    patterns = patterns . union ( temp )
    # Adding the active cells to the set of coincs that have been active at least once
    encodings = encodings . union ( tempInput )
    count += 1
overlap = { }
overlapVal = 0
seen = [ ]
seen = ( printOverlaps ( coincs , coincs , seen ) )
print len ( seen ) , 'sets of 40 cells'
seen = printOverlaps ( reUsedCoincs , coincs , seen )
Summ = [ ]
for z in coincs :
    c = 0
    for y in reUsedCoincs :
        c += len ( z [ 1 ] . intersection ( y [ 1 ] ) )
    Summ . append ( c )
print 'Sum: ' , Summ
for m in xrange ( 3 ) :
    displayLimit = min ( 51 , len ( spOutput [ m * 200 : ] ) )
    if displayLimit > 0 :
        drawFile ( dataset , np . zeros ( [ len ( inputBits [ : ( m + 1 ) * displayLimit ] ) , len ( inputBits [ : ( m + 1 ) * displayLimit ] ) ] ) , inputBits [ : ( m + 1 ) * displayLimit ] , spOutput [ : ( m + 1 ) * displayLimit ] , w , m + 1 )
    else :
        print 'No more records to display'
pyl . show ( )


signature:
def drawFile ( dataset , matrix , patterns , cells , w , fnum ) :
    '''The similarity of two patterns in the bit - encoding space is displayed alongside
 their similarity in the sp - coinc space .'''

implementation:
score = 0
count = 0
assert len ( patterns ) == len ( cells )
for p in xrange ( len ( patterns ) - 1 ) :
    matrix [ p + 1 : , p ] = [ len ( set ( patterns [ p ] ) . intersection ( set ( q ) ) ) * 100 / w for q in patterns [ p + 1 : ] ]
    matrix [ p , p + 1 : ] = [ len ( set ( cells [ p ] ) . intersection ( set ( r ) ) ) * 5 / 2 for r in cells [ p + 1 : ] ]
    score += sum ( abs ( np . array ( matrix [ p + 1 : , p ] ) - np . array ( matrix [ p , p + 1 : ] ) ) )
    count += len ( matrix [ p + 1 : , p ] )
print 'Score' , score / count
fig = pyl . figure ( figsize = ( 10 , 10 ) , num = fnum )
pyl . matshow ( matrix , fignum = fnum )
pyl . colorbar ( )
pyl . title ( 'Coincidence Space' , verticalalignment = 'top' , fontsize = 12 )
pyl . xlabel ( 'The Mirror Image Visualization for ' + dataset , fontsize = 17 )
pyl . ylabel ( 'Encoding space' , fontsize = 12 )


signature:
def printOverlaps ( comparedTo , coincs , seen ) :
    """Compare the results and return True if success , False if failure
 Parameters :
 coincs : Which cells are we comparing ?
 comparedTo : The set of 40 cells we being compared to ( they have no overlap with seen )
 seen : Which of the cells we are comparing to have already been encountered .
 This helps glue together the unique and reused coincs"""

implementation:
inputOverlap = 0
cellOverlap = 0
for y in comparedTo :
    closestInputs = [ ]
    closestCells = [ ]
    if len ( seen ) > 0 :
        inputOverlap = max ( [ len ( seen [ m ] [ 1 ] . intersection ( y [ 4 ] ) ) for m in xrange ( len ( seen ) ) ] )
        cellOverlap = max ( [ len ( seen [ m ] [ 0 ] . intersection ( y [ 1 ] ) ) for m in xrange ( len ( seen ) ) ] )
        for m in xrange ( len ( seen ) ) :
            if len ( seen [ m ] [ 1 ] . intersection ( y [ 4 ] ) ) == inputOverlap :
                closestInputs . append ( seen [ m ] [ 2 ] )
            if len ( seen [ m ] [ 0 ] . intersection ( y [ 1 ] ) ) == cellOverlap :
                closestCells . append ( seen [ m ] [ 2 ] )
    seen . append ( ( y [ 1 ] , y [ 4 ] , y [ 0 ] ) )
    print 'Pattern' , y [ 0 ] + 1 , ':' , ' ' . join ( str ( len ( z [ 1 ] . intersection ( y [ 1 ] ) ) ) . rjust ( 2 ) for z in coincs ) , 'input overlap:' , inputOverlap , ';' , len ( closestInputs ) , 'closest encodings:' , ',' . join ( str ( m + 1 ) for m in closestInputs ) . ljust ( 15 ) , 'cell overlap:' , cellOverlap , ';' , len ( closestCells ) , 'closest set(s):' , ',' . join ( str ( m + 1 ) for m in closestCells )
return seen


signature:
def createInput ( self ) :
    """create a random input vector"""

implementation:
print "-" * 70 + "Creating a random input vector" + "-" * 70
# clear the inputArray to zero before creating a new input vector
self . inputArray [ 0 : ] = 0
for i in range ( self . inputSize ) : # randrange returns 0 or 1
    self . inputArray [ i ] = random . randrange ( 2 )


signature:
def run ( self ) :
    """Run the spatial pooler with the input vector"""

implementation:
print "-" * 80 + "Computing the SDR" + "-" * 80
# activeArray [ column ] = 1 if column is active after spatial pooling
self . sp . compute ( self . inputArray , True , self . activeArray )
print self . activeArray . nonzero ( )


signature:
def addNoise ( self , noiseLevel ) :
    """Flip the value of 10 % of input bits ( add noise )
 : param noiseLevel : The percentage of total input bits that should be flipped"""

implementation:
for _ in range ( int ( noiseLevel * self . inputSize ) ) : # 0.1 * self . inputSize represents 10 % of the total input bits
# random . random ( ) returns a float between 0 and 1
    randomPosition = int ( random . random ( ) * self . inputSize )
    # Flipping the bit at the randomly picked position
    if self . inputArray [ randomPosition ] == 1 :
        self . inputArray [ randomPosition ] = 0
    else :
        self . inputArray [ randomPosition ] = 1


signature:
def _labeledInput ( activeInputs , cellsPerCol = 32 ) :
    """Print the list of [ column , cellIdx ] indices for each of the active
 cells in activeInputs ."""

implementation:
if cellsPerCol == 0 :
    cellsPerCol = 1
cols = activeInputs . size / cellsPerCol
activeInputs = activeInputs . reshape ( cols , cellsPerCol )
( cols , cellIdxs ) = activeInputs . nonzero ( )
if len ( cols ) == 0 :
    return "NONE"
items = [ "(%d): " % ( len ( cols ) ) ]
prevCol = - 1
for ( col , cellIdx ) in zip ( cols , cellIdxs ) :
    if col != prevCol :
        if prevCol != - 1 :
            items . append ( "] " )
        items . append ( "Col %d: [" % col )
        prevCol = col
    items . append ( "%d," % cellIdx )
items . append ( "]" )
return " " . join ( items )


signature:
def clear ( self ) :
    """Clears the state of the KNNClassifier ."""

implementation:
self . _Memory = None
self . _numPatterns = 0
self . _M = None
self . _categoryList = [ ]
self . _partitionIdList = [ ]
self . _partitionIdMap = { }
self . _finishedLearning = False
self . _iterationIdx = - 1
# Fixed capacity KNN
if self . maxStoredPatterns > 0 :
    assert self . useSparseMemory , ( "Fixed capacity KNN is implemented only " "in the sparse memory mode" )
    self . fixedCapacity = True
    self . _categoryRecencyList = [ ]
else :
    self . fixedCapacity = False
# Cached value of the store prototype sizes
self . _protoSizes = None
# Used by PCA
self . _s = None
self . _vt = None
self . _nc = None
self . _mean = None
# Used by Network Builder
self . _specificIndexTraining = False
self . _nextTrainingIndices = None


signature:
def prototypeSetCategory ( self , idToCategorize , newCategory ) :
    """Allows ids to be assigned a category and subsequently enables users to use :
 - : meth : ` ~ . KNNClassifier . KNNClassifier . removeCategory `
 - : meth : ` ~ . KNNClassifier . KNNClassifier . closestTrainingPattern `
 - : meth : ` ~ . KNNClassifier . KNNClassifier . closestOtherTrainingPattern `"""

implementation:
if idToCategorize not in self . _categoryRecencyList :
    return
recordIndex = self . _categoryRecencyList . index ( idToCategorize )
self . _categoryList [ recordIndex ] = newCategory


signature:
def removeIds ( self , idsToRemove ) :
    """There are two caveats . First , this is a potentially slow operation . Second ,
 pattern indices will shift if patterns before them are removed .
 : param idsToRemove : A list of row indices to remove ."""

implementation:
# Form a list of all categories to remove
rowsToRemove = [ k for k , rowID in enumerate ( self . _categoryRecencyList ) if rowID in idsToRemove ]
# Remove rows from the classifier
self . _removeRows ( rowsToRemove )


signature:
def removeCategory ( self , categoryToRemove ) :
    """There are two caveats . First , this is a potentially slow operation . Second ,
 pattern indices will shift if patterns before them are removed .
 : param categoryToRemove : Category label to remove"""

implementation:
removedRows = 0
if self . _Memory is None :
    return removedRows
# The internal category indices are stored in float
# format , so we should compare with a float
catToRemove = float ( categoryToRemove )
# Form a list of all categories to remove
rowsToRemove = [ k for k , catID in enumerate ( self . _categoryList ) if catID == catToRemove ]
# Remove rows from the classifier
self . _removeRows ( rowsToRemove )
assert catToRemove not in self . _categoryList


signature:
def _removeRows ( self , rowsToRemove ) :
    """A list of row indices to remove . There are two caveats . First , this is
 a potentially slow operation . Second , pattern indices will shift if
 patterns before them are removed ."""

implementation:
# Form a numpy array of row indices to be removed
removalArray = numpy . array ( rowsToRemove )
# Remove categories
self . _categoryList = numpy . delete ( numpy . array ( self . _categoryList ) , removalArray ) . tolist ( )
if self . fixedCapacity :
    self . _categoryRecencyList = numpy . delete ( numpy . array ( self . _categoryRecencyList ) , removalArray ) . tolist ( )
# Remove the partition ID , if any for these rows and rebuild the id map .
for row in reversed ( rowsToRemove ) : # Go backwards
# Remove these patterns from partitionList
    self . _partitionIdList . pop ( row )
self . _rebuildPartitionIdMap ( self . _partitionIdList )
# Remove actual patterns
if self . useSparseMemory : # Delete backwards
    for rowIndex in rowsToRemove [ : : - 1 ] :
        self . _Memory . deleteRow ( rowIndex )
else :
    self . _M = numpy . delete ( self . _M , removalArray , 0 )
numRemoved = len ( rowsToRemove )
# Sanity checks
numRowsExpected = self . _numPatterns - numRemoved
if self . useSparseMemory :
    if self . _Memory is not None :
        assert self . _Memory . nRows ( ) == numRowsExpected
else :
    assert self . _M . shape [ 0 ] == numRowsExpected
assert len ( self . _categoryList ) == numRowsExpected
self . _numPatterns -= numRemoved
return numRemoved