signature:
def runModelGivenBaseAndParams ( modelID , jobID , baseDescription , params , predictedField , reportKeys , optimizeKey , jobsDAO , modelCheckpointGUID , logLevel = None , predictionCacheMaxRecords = None ) :
    """This creates an experiment directory with a base . py description file
 created from ' baseDescription ' and a description . py generated from the
 given params dict and then runs the experiment .
 Parameters :
 modelID : ID for this model in the models table
 jobID : ID for this hypersearch job in the jobs table
 baseDescription : Contents of a description . py with the base experiment
 description
 params : Dictionary of specific parameters to override within
 the baseDescriptionFile .
 predictedField : Name of the input field for which this model is being
 optimized
 reportKeys : Which metrics of the experiment to store into the
 results dict of the model ' s database entry
 optimizeKey : Which metric we are optimizing for
 jobsDAO Jobs data access object - the interface to the
 jobs database which has the model ' s table .
 modelCheckpointGUID : A persistent , globally - unique identifier for
 constructing the model checkpoint key
 logLevel : override logging level to this value , if not None
 retval : ( completionReason , completionMsg )"""

implementation:
from nupic . swarming . ModelRunner import OPFModelRunner
# The logger for this method
logger = logging . getLogger ( 'com.numenta.nupic.hypersearch.utils' )
# Create a temp directory for the experiment and the description files
experimentDir = tempfile . mkdtemp ( )
try :
    logger . info ( "Using experiment directory: %s" % ( experimentDir ) )
    # Create the decription . py from the overrides in params
    paramsFilePath = os . path . join ( experimentDir , 'description.py' )
    paramsFile = open ( paramsFilePath , 'wb' )
    paramsFile . write ( _paramsFileHead ( ) )
    items = params . items ( )
    items . sort ( )
    for ( key , value ) in items :
        quotedKey = _quoteAndEscape ( key )
        if isinstance ( value , basestring ) :
            paramsFile . write ( "  %s : '%s',\n" % ( quotedKey , value ) )
        else :
            paramsFile . write ( "  %s : %s,\n" % ( quotedKey , value ) )
    paramsFile . write ( _paramsFileTail ( ) )
    paramsFile . close ( )
    # Write out the base description
    baseParamsFile = open ( os . path . join ( experimentDir , 'base.py' ) , 'wb' )
    baseParamsFile . write ( baseDescription )
    baseParamsFile . close ( )
    # Store the experiment ' s sub - description file into the model table
    # for reference
    fd = open ( paramsFilePath )
    expDescription = fd . read ( )
    fd . close ( )
    jobsDAO . modelSetFields ( modelID , { 'genDescription' : expDescription } )
    # Run the experiment now
    try :
        runner = OPFModelRunner ( modelID = modelID , jobID = jobID , predictedField = predictedField , experimentDir = experimentDir , reportKeyPatterns = reportKeys , optimizeKeyPattern = optimizeKey , jobsDAO = jobsDAO , modelCheckpointGUID = modelCheckpointGUID , logLevel = logLevel , predictionCacheMaxRecords = predictionCacheMaxRecords )
        signal . signal ( signal . SIGINT , runner . handleWarningSignal )
        ( completionReason , completionMsg ) = runner . run ( )
    except InvalidConnectionException :
        raise
    except Exception , e :
        ( completionReason , completionMsg ) = _handleModelRunnerException ( jobID , modelID , jobsDAO , experimentDir , logger , e )
finally : # delete our temporary directory tree
    shutil . rmtree ( experimentDir )
    signal . signal ( signal . SIGINT , signal . default_int_handler )
# Return completion reason and msg
return ( completionReason , completionMsg )


signature:
def rCopy ( d , f = identityConversion , discardNoneKeys = True , deepCopy = True ) :
    """Recursively copies a dict and returns the result .
 Args :
 d : The dict to copy .
 f : A function to apply to values when copying that takes the value and the
 list of keys from the root of the dict to the value and returns a value
 for the new dict .
 discardNoneKeys : If True , discard key - value pairs when f returns None for
 the value .
 deepCopy : If True , all values in returned dict are true copies ( not the
 same object ) .
 Returns :
 A new dict with keys and values from d replaced with the result of f ."""

implementation:
# Optionally deep copy the dict .
if deepCopy :
    d = copy . deepcopy ( d )
newDict = { }
toCopy = [ ( k , v , newDict , ( ) ) for k , v in d . iteritems ( ) ]
while len ( toCopy ) > 0 :
    k , v , d , prevKeys = toCopy . pop ( )
    prevKeys = prevKeys + ( k , )
    if isinstance ( v , dict ) :
        d [ k ] = dict ( )
        toCopy [ 0 : 0 ] = [ ( innerK , innerV , d [ k ] , prevKeys ) for innerK , innerV in v . iteritems ( ) ]
    else : # print k , v , prevKeys
        newV = f ( v , prevKeys )
        if not discardNoneKeys or newV is not None :
            d [ k ] = newV
return newDict


signature:
def rApply ( d , f ) :
    """Recursively applies f to the values in dict d .
 Args :
 d : The dict to recurse over .
 f : A function to apply to values in d that takes the value and a list of
 keys from the root of the dict to the value ."""

implementation:
remainingDicts = [ ( d , ( ) ) ]
while len ( remainingDicts ) > 0 :
    current , prevKeys = remainingDicts . pop ( )
    for k , v in current . iteritems ( ) :
        keys = prevKeys + ( k , )
        if isinstance ( v , dict ) :
            remainingDicts . insert ( 0 , ( v , keys ) )
        else :
            f ( v , keys )


signature:
def clippedObj ( obj , maxElementSize = 64 ) :
    """Return a clipped version of obj suitable for printing , This
 is useful when generating log messages by printing data structures , but
 don ' t want the message to be too long .
 If passed in a dict , list , or namedtuple , each element of the structure ' s
 string representation will be limited to ' maxElementSize ' characters . This
 will return a new object where the string representation of each element
 has been truncated to fit within maxElementSize ."""

implementation:
# Is it a named tuple ?
if hasattr ( obj , '_asdict' ) :
    obj = obj . _asdict ( )
# Printing a dict ?
if isinstance ( obj , dict ) :
    objOut = dict ( )
    for key , val in obj . iteritems ( ) :
        objOut [ key ] = clippedObj ( val )
# Printing a list ?
elif hasattr ( obj , '__iter__' ) :
    objOut = [ ]
    for val in obj :
        objOut . append ( clippedObj ( val ) )
# Some other object
else :
    objOut = str ( obj )
    if len ( objOut ) > maxElementSize :
        objOut = objOut [ 0 : maxElementSize ] + '...'
return objOut


signature:
def validate ( value , ** kwds ) :
    """Validate a python value against json schema :
 validate ( value , schemaPath )
 validate ( value , schemaDict )
 value : python object to validate against the schema
 The json schema may be specified either as a path of the file containing
 the json schema or as a python dictionary using one of the
 following keywords as arguments :
 schemaPath : Path of file containing the json schema object .
 schemaDict : Python dictionary containing the json schema object
 Returns : nothing
 Raises :
 ValidationError when value fails json validation"""

implementation:
assert len ( kwds . keys ( ) ) >= 1
assert 'schemaPath' in kwds or 'schemaDict' in kwds
schemaDict = None
if 'schemaPath' in kwds :
    schemaPath = kwds . pop ( 'schemaPath' )
    schemaDict = loadJsonValueFromFile ( schemaPath )
elif 'schemaDict' in kwds :
    schemaDict = kwds . pop ( 'schemaDict' )
try :
    validictory . validate ( value , schemaDict , ** kwds )
except validictory . ValidationError as e :
    raise ValidationError ( e )


signature:
def loadJsonValueFromFile ( inputFilePath ) :
    """Loads a json value from a file and converts it to the corresponding python
 object .
 inputFilePath :
 Path of the json file ;
 Returns :
 python value that represents the loaded json value"""

implementation:
with open ( inputFilePath ) as fileObj :
    value = json . load ( fileObj )
return value


signature:
def sortedJSONDumpS ( obj ) :
    """Return a JSON representation of obj with sorted keys on any embedded dicts .
 This insures that the same object will always be represented by the same
 string even if it contains dicts ( where the sort order of the keys is
 normally undefined ) ."""

implementation:
itemStrs = [ ]
if isinstance ( obj , dict ) :
    items = obj . items ( )
    items . sort ( )
    for key , value in items :
        itemStrs . append ( '%s: %s' % ( json . dumps ( key ) , sortedJSONDumpS ( value ) ) )
    return '{%s}' % ( ', ' . join ( itemStrs ) )
elif hasattr ( obj , '__iter__' ) :
    for val in obj :
        itemStrs . append ( sortedJSONDumpS ( val ) )
    return '[%s]' % ( ', ' . join ( itemStrs ) )
else :
    return json . dumps ( obj )


signature:
def tick ( self ) :
    """Activity tick handler ; services all activities
 Returns : True if controlling iterator says it ' s okay to keep going ;
 False to stop"""

implementation:
# Run activities whose time has come
for act in self . __activities :
    if not act . iteratorHolder [ 0 ] :
        continue
    try :
        next ( act . iteratorHolder [ 0 ] )
    except StopIteration :
        act . cb ( )
        if act . repeating :
            act . iteratorHolder [ 0 ] = iter ( xrange ( act . period ) )
        else :
            act . iteratorHolder [ 0 ] = None
return True


signature:
def rUpdate ( original , updates ) :
    """Recursively updates the values in original with the values from updates ."""

implementation:
# Keep a list of the sub - dictionaries that need to be updated to avoid having
# to use recursion ( which could fail for dictionaries with a lot of nesting .
dictPairs = [ ( original , updates ) ]
while len ( dictPairs ) > 0 :
    original , updates = dictPairs . pop ( )
    for k , v in updates . iteritems ( ) :
        if k in original and isinstance ( original [ k ] , dict ) and isinstance ( v , dict ) :
            dictPairs . append ( ( original [ k ] , v ) )
        else :
            original [ k ] = v


signature:
def dictDiffAndReport ( da , db ) :
    """Compares two python dictionaries at the top level and report differences ,
 if any , to stdout
 da : first dictionary
 db : second dictionary
 Returns : The same value as returned by dictDiff ( ) for the given args"""

implementation:
differences = dictDiff ( da , db )
if not differences :
    return differences
if differences [ 'inAButNotInB' ] :
    print ">>> inAButNotInB: %s" % differences [ 'inAButNotInB' ]
if differences [ 'inBButNotInA' ] :
    print ">>> inBButNotInA: %s" % differences [ 'inBButNotInA' ]
for key in differences [ 'differentValues' ] :
    print ">>> da[%s] != db[%s]" % ( key , key )
    print "da[%s] = %r" % ( key , da [ key ] )
    print "db[%s] = %r" % ( key , db [ key ] )
return differences


signature:
def dictDiff ( da , db ) :
    """Compares two python dictionaries at the top level and return differences
 da : first dictionary
 db : second dictionary
 Returns : None if dictionaries test equal ; otherwise returns a
 dictionary as follows :
 ' inAButNotInB ' :
 < sequence of keys that are in da but not in db >
 ' inBButNotInA ' :
 < sequence of keys that are in db but not in da >
 ' differentValues ' :
 < sequence of keys whose corresponding values differ
 between da and db >"""

implementation:
different = False
resultDict = dict ( )
resultDict [ 'inAButNotInB' ] = set ( da ) - set ( db )
if resultDict [ 'inAButNotInB' ] :
    different = True
resultDict [ 'inBButNotInA' ] = set ( db ) - set ( da )
if resultDict [ 'inBButNotInA' ] :
    different = True
resultDict [ 'differentValues' ] = [ ]
for key in ( set ( da ) - resultDict [ 'inAButNotInB' ] ) :
    comparisonResult = da [ key ] == db [ key ]
    if isinstance ( comparisonResult , bool ) :
        isEqual = comparisonResult
    else : # This handles numpy arrays ( but only at the top level )
        isEqual = comparisonResult . all ( )
    if not isEqual :
        resultDict [ 'differentValues' ] . append ( key )
        different = True
assert ( ( ( resultDict [ 'inAButNotInB' ] or resultDict [ 'inBButNotInA' ] or resultDict [ 'differentValues' ] ) and different ) or not different )
return resultDict if different else None


signature:
def _seed ( self , seed = - 1 ) :
    """Initialize the random seed"""

implementation:
if seed != - 1 :
    self . random = NupicRandom ( seed )
else :
    self . random = NupicRandom ( )


signature:
def _newRep ( self ) :
    """Generate a new and unique representation . Returns a numpy array
 of shape ( n , ) ."""

implementation:
maxAttempts = 1000
for _ in xrange ( maxAttempts ) :
    foundUnique = True
    population = numpy . arange ( self . n , dtype = numpy . uint32 )
    choices = numpy . arange ( self . w , dtype = numpy . uint32 )
    oneBits = sorted ( self . random . sample ( population , choices ) )
    sdr = numpy . zeros ( self . n , dtype = 'uint8' )
    sdr [ oneBits ] = 1
    for i in xrange ( self . ncategories ) :
        if ( sdr == self . sdrs [ i ] ) . all ( ) :
            foundUnique = False
            break
    if foundUnique :
        break ;
if not foundUnique :
    raise RuntimeError ( "Error, could not find unique pattern %d after " "%d attempts" % ( self . ncategories , maxAttempts ) )
return sdr


signature:
def getScalars ( self , input ) :
    """See method description in base . py"""

implementation:
if input == SENTINEL_VALUE_FOR_MISSING_DATA :
    return numpy . array ( [ 0 ] )
index = self . categoryToIndex . get ( input , None )
if index is None :
    if self . _learningEnabled :
        self . _addCategory ( input )
        index = self . ncategories - 1
    else : # if not found , we encode category 0
        index = 0
return numpy . array ( [ index ] )


signature:
def decode ( self , encoded , parentFieldName = '' ) :
    """See the function description in base . py"""

implementation:
assert ( encoded [ 0 : self . n ] <= 1.0 ) . all ( )
resultString = ""
resultRanges = [ ]
overlaps = ( self . sdrs * encoded [ 0 : self . n ] ) . sum ( axis = 1 )
if self . verbosity >= 2 :
    print "Overlaps for decoding:"
    for i in xrange ( 0 , self . ncategories ) :
        print "%d %s" % ( overlaps [ i ] , self . categories [ i ] )
matchingCategories = ( overlaps > self . thresholdOverlap ) . nonzero ( ) [ 0 ]
for index in matchingCategories :
    if resultString != "" :
        resultString += " "
    resultString += str ( self . categories [ index ] )
    resultRanges . append ( [ int ( index ) , int ( index ) ] )
if parentFieldName != '' :
    fieldName = "%s.%s" % ( parentFieldName , self . name )
else :
    fieldName = self . name
return ( { fieldName : ( resultRanges , resultString ) } , [ fieldName ] )


signature:
def _getTopDownMapping ( self ) :
    """Return the interal _ topDownMappingM matrix used for handling the
 bucketInfo ( ) and topDownCompute ( ) methods . This is a matrix , one row per
 category ( bucket ) where each row contains the encoded output for that
 category ."""

implementation:
# Do we need to build up our reverse mapping table ?
if self . _topDownMappingM is None : # Each row represents an encoded output pattern
    self . _topDownMappingM = SM32 ( self . ncategories , self . n )
    outputSpace = numpy . zeros ( self . n , dtype = GetNTAReal ( ) )
    for i in xrange ( self . ncategories ) :
        self . encodeIntoArray ( self . categories [ i ] , outputSpace )
        self . _topDownMappingM . setRowFromDense ( i , outputSpace )
return self . _topDownMappingM


signature:
def getBucketInfo ( self , buckets ) :
    """See the function description in base . py"""

implementation:
if self . ncategories == 0 :
    return 0
topDownMappingM = self . _getTopDownMapping ( )
categoryIndex = buckets [ 0 ]
category = self . categories [ categoryIndex ]
encoding = topDownMappingM . getRow ( categoryIndex )
return [ EncoderResult ( value = category , scalar = categoryIndex , encoding = encoding ) ]


signature:
def topDownCompute ( self , encoded ) :
    """See the function description in base . py"""

implementation:
if self . ncategories == 0 :
    return 0
topDownMappingM = self . _getTopDownMapping ( )
categoryIndex = topDownMappingM . rightVecProd ( encoded ) . argmax ( )
category = self . categories [ categoryIndex ]
encoding = topDownMappingM . getRow ( categoryIndex )
return EncoderResult ( value = category , scalar = categoryIndex , encoding = encoding )


signature:
def getScalarNames ( self , parentFieldName = '' ) :
    """See method description in base . py"""

implementation:
names = [ ]
# This forms a name which is the concatenation of the parentFieldName
# passed in and the encoder ' s own name .
def _formFieldName ( encoder ) :
    if parentFieldName == '' :
        return encoder . name
    else :
        return '%s.%s' % ( parentFieldName , encoder . name )
# Get the scalar values for each sub - field
if self . seasonEncoder is not None :
    names . append ( _formFieldName ( self . seasonEncoder ) )
if self . dayOfWeekEncoder is not None :
    names . append ( _formFieldName ( self . dayOfWeekEncoder ) )
if self . customDaysEncoder is not None :
    names . append ( _formFieldName ( self . customDaysEncoder ) )
if self . weekendEncoder is not None :
    names . append ( _formFieldName ( self . weekendEncoder ) )
if self . holidayEncoder is not None :
    names . append ( _formFieldName ( self . holidayEncoder ) )
if self . timeOfDayEncoder is not None :
    names . append ( _formFieldName ( self . timeOfDayEncoder ) )
return names


signature:
def getEncodedValues ( self , input ) :
    """See method description in base . py"""

implementation:
if input == SENTINEL_VALUE_FOR_MISSING_DATA :
    return numpy . array ( [ None ] )
assert isinstance ( input , datetime . datetime )
values = [ ]
# Get the scalar values for each sub - field
timetuple = input . timetuple ( )
timeOfDay = timetuple . tm_hour + float ( timetuple . tm_min ) / 60.0
if self . seasonEncoder is not None :
    dayOfYear = timetuple . tm_yday
    # input . timetuple ( ) computes the day of year 1 based , so convert to 0 based
    values . append ( dayOfYear - 1 )
if self . dayOfWeekEncoder is not None :
    dayOfWeek = timetuple . tm_wday + timeOfDay / 24.0
    values . append ( dayOfWeek )
if self . weekendEncoder is not None : # saturday , sunday or friday evening
    if timetuple . tm_wday == 6 or timetuple . tm_wday == 5 or ( timetuple . tm_wday == 4 and timeOfDay > 18 ) :
        weekend = 1
    else :
        weekend = 0
    values . append ( weekend )
if self . customDaysEncoder is not None :
    if timetuple . tm_wday in self . customDays :
        customDay = 1
    else :
        customDay = 0
    values . append ( customDay )
if self . holidayEncoder is not None : # A " continuous " binary value . = 1 on the holiday itself and smooth ramp
# 0 - > 1 on the day before the holiday and 1 - > 0 on the day after the holiday .
# Currently the only holiday we know about is December 25
# holidays is a list of holidays that occur on a fixed date every year
    if len ( self . holidays ) == 0 :
        holidays = [ ( 12 , 25 ) ]
    else :
        holidays = self . holidays
    val = 0
    for h in holidays : # hdate is midnight on the holiday
        if len ( h ) == 3 :
            hdate = datetime . datetime ( h [ 0 ] , h [ 1 ] , h [ 2 ] , 0 , 0 , 0 )
        else :
            hdate = datetime . datetime ( timetuple . tm_year , h [ 0 ] , h [ 1 ] , 0 , 0 , 0 )
        if input > hdate :
            diff = input - hdate
            if diff . days == 0 : # return 1 on the holiday itself
                val = 1
                break
            elif diff . days == 1 : # ramp smoothly from 1 - > 0 on the next day
                val = 1.0 - ( float ( diff . seconds ) / 86400 )
                break
        else :
            diff = hdate - input
            if diff . days == 0 : # ramp smoothly from 0 - > 1 on the previous day
                val = 1.0 - ( float ( diff . seconds ) / 86400 )
    values . append ( val )
if self . timeOfDayEncoder is not None :
    values . append ( timeOfDay )
return values


signature:
def getBucketIndices ( self , input ) :
    """See method description in base . py"""

implementation:
if input == SENTINEL_VALUE_FOR_MISSING_DATA : # Encoder each sub - field
    return [ None ] * len ( self . encoders )
else :
    assert isinstance ( input , datetime . datetime )
    # Get the scalar values for each sub - field
    scalars = self . getScalars ( input )
    # Encoder each sub - field
    result = [ ]
    for i in xrange ( len ( self . encoders ) ) :
        ( name , encoder , offset ) = self . encoders [ i ]
        result . extend ( encoder . getBucketIndices ( scalars [ i ] ) )
    return result


signature:
def encodeIntoArray ( self , input , output ) :
    """See method description in base . py"""

implementation:
if input == SENTINEL_VALUE_FOR_MISSING_DATA :
    output [ 0 : ] = 0
else :
    if not isinstance ( input , datetime . datetime ) :
        raise ValueError ( "Input is type %s, expected datetime. Value: %s" % ( type ( input ) , str ( input ) ) )
    # Get the scalar values for each sub - field
    scalars = self . getScalars ( input )
    # Encoder each sub - field
    for i in xrange ( len ( self . encoders ) ) :
        ( name , encoder , offset ) = self . encoders [ i ]
        encoder . encodeIntoArray ( scalars [ i ] , output [ offset : ] )


signature:
def getSpec ( cls ) :
    """Return the Spec for IdentityRegion ."""

implementation:
spec = { "description" : IdentityRegion . __doc__ , "singleNodeOnly" : True , "inputs" : { "in" : { "description" : "The input vector." , "dataType" : "Real32" , "count" : 0 , "required" : True , "regionLevel" : False , "isDefaultInput" : True , "requireSplitterMap" : False } , } , "outputs" : { "out" : { "description" : "A copy of the input vector." , "dataType" : "Real32" , "count" : 0 , "regionLevel" : True , "isDefaultOutput" : True } , } , "parameters" : { "dataWidth" : { "description" : "Size of inputs" , "accessMode" : "Read" , "dataType" : "UInt32" , "count" : 1 , "constraints" : "" } , } , }
return spec


signature:
def _setRandomEncoderResolution ( minResolution = 0.001 ) :
    """Given model params , figure out the correct resolution for the
 RandomDistributed encoder . Modifies params in place ."""

implementation:
encoder = ( model_params . MODEL_PARAMS [ "modelParams" ] [ "sensorParams" ] [ "encoders" ] [ "value" ] )
if encoder [ "type" ] == "RandomDistributedScalarEncoder" :
    rangePadding = abs ( _INPUT_MAX - _INPUT_MIN ) * 0.2
    minValue = _INPUT_MIN - rangePadding
    maxValue = _INPUT_MAX + rangePadding
    resolution = max ( minResolution , ( maxValue - minValue ) / encoder . pop ( "numBuckets" ) )
    encoder [ "resolution" ] = resolution


signature:
def addLabel ( self , start , end , labelName ) :
    """Add the label labelName to each record with record ROWID in range from
 start to end , noninclusive of end .
 This will recalculate all points from end to the last record stored in the
 internal cache of this classifier ."""

implementation:
if len ( self . saved_states ) == 0 :
    raise HTMPredictionModelInvalidRangeError ( "Invalid supplied range for 'addLabel'. " "Model has no saved records." )
startID = self . saved_states [ 0 ] . ROWID
clippedStart = max ( 0 , start - startID )
clippedEnd = max ( 0 , min ( len ( self . saved_states ) , end - startID ) )
if clippedEnd <= clippedStart :
    raise HTMPredictionModelInvalidRangeError ( "Invalid supplied range for 'addLabel'." , debugInfo = { 'requestRange' : { 'startRecordID' : start , 'endRecordID' : end } , 'clippedRequestRange' : { 'startRecordID' : clippedStart , 'endRecordID' : clippedEnd } , 'validRange' : { 'startRecordID' : startID , 'endRecordID' : self . saved_states [ len ( self . saved_states ) - 1 ] . ROWID } , 'numRecordsStored' : len ( self . saved_states ) } )
# Add label to range [ clippedStart , clippedEnd )
for state in self . saved_states [ clippedStart : clippedEnd ] :
    if labelName not in state . anomalyLabel :
        state . anomalyLabel . append ( labelName )
        state . setByUser = True
        self . _addRecordToKNN ( state )
assert len ( self . saved_categories ) > 0
# Recompute [ end , . . . )
for state in self . saved_states [ clippedEnd : ] :
    self . _updateState ( state )


signature:
def removeLabels ( self , start = None , end = None , labelFilter = None ) :
    """Remove labels from each record with record ROWID in range from
 start to end , noninclusive of end . Removes all records if labelFilter is
 None , otherwise only removes the labels eqaul to labelFilter .
 This will recalculate all points from end to the last record stored in the
 internal cache of this classifier ."""

implementation:
if len ( self . saved_states ) == 0 :
    raise HTMPredictionModelInvalidRangeError ( "Invalid supplied range for " "'removeLabels'. Model has no saved records." )
startID = self . saved_states [ 0 ] . ROWID
clippedStart = 0 if start is None else max ( 0 , start - startID )
clippedEnd = len ( self . saved_states ) if end is None else max ( 0 , min ( len ( self . saved_states ) , end - startID ) )
if clippedEnd <= clippedStart :
    raise HTMPredictionModelInvalidRangeError ( "Invalid supplied range for " "'removeLabels'." , debugInfo = { 'requestRange' : { 'startRecordID' : start , 'endRecordID' : end } , 'clippedRequestRange' : { 'startRecordID' : clippedStart , 'endRecordID' : clippedEnd } , 'validRange' : { 'startRecordID' : startID , 'endRecordID' : self . saved_states [ len ( self . saved_states ) - 1 ] . ROWID } , 'numRecordsStored' : len ( self . saved_states ) } )
# Remove records within the cache
recordsToDelete = [ ]
for state in self . saved_states [ clippedStart : clippedEnd ] :
    if labelFilter is not None :
        if labelFilter in state . anomalyLabel :
            state . anomalyLabel . remove ( labelFilter )
    else :
        state . anomalyLabel = [ ]
    state . setByUser = False
    recordsToDelete . append ( state )
self . _deleteRecordsFromKNN ( recordsToDelete )
# Remove records not in cache
self . _deleteRangeFromKNN ( start , end )
# Recompute [ clippedEnd , . . . )
for state in self . saved_states [ clippedEnd : ] :
    self . _updateState ( state )
return { 'status' : 'success' }


signature:
def _addRecordToKNN ( self , record ) :
    """This method will add the record to the KNN classifier ."""

implementation:
classifier = self . htm_prediction_model . _getAnomalyClassifier ( )
knn = classifier . getSelf ( ) . _knn
prototype_idx = classifier . getSelf ( ) . getParameter ( 'categoryRecencyList' )
category = self . _labelListToCategoryNumber ( record . anomalyLabel )
# If record is already in the classifier , overwrite its labeling
if record . ROWID in prototype_idx :
    knn . prototypeSetCategory ( record . ROWID , category )
    return
# Learn this pattern in the knn
pattern = self . _getStateAnomalyVector ( record )
rowID = record . ROWID
knn . learn ( pattern , category , rowID = rowID )


signature:
def _deleteRecordsFromKNN ( self , recordsToDelete ) :
    """This method will remove the given records from the classifier .
 parameters
 recordsToDelete - list of records to delete from the classififier"""

implementation:
classifier = self . htm_prediction_model . _getAnomalyClassifier ( )
knn = classifier . getSelf ( ) . _knn
prototype_idx = classifier . getSelf ( ) . getParameter ( 'categoryRecencyList' )
idsToDelete = [ r . ROWID for r in recordsToDelete if not r . setByUser and r . ROWID in prototype_idx ]
nProtos = knn . _numPatterns
knn . removeIds ( idsToDelete )
assert knn . _numPatterns == nProtos - len ( idsToDelete )


signature:
def _deleteRangeFromKNN ( self , start = 0 , end = None ) :
    """This method will remove any stored records within the range from start to
 end . Noninclusive of end .
 parameters
 start - integer representing the ROWID of the start of the deletion range ,
 end - integer representing the ROWID of the end of the deletion range ,
 if None , it will default to end ."""

implementation:
classifier = self . htm_prediction_model . _getAnomalyClassifier ( )
knn = classifier . getSelf ( ) . _knn
prototype_idx = numpy . array ( classifier . getSelf ( ) . getParameter ( 'categoryRecencyList' ) )
if end is None :
    end = prototype_idx . max ( ) + 1
idsIdxToDelete = numpy . logical_and ( prototype_idx >= start , prototype_idx < end )
idsToDelete = prototype_idx [ idsIdxToDelete ]
nProtos = knn . _numPatterns
knn . removeIds ( idsToDelete . tolist ( ) )
assert knn . _numPatterns == nProtos - len ( idsToDelete )


signature:
def _recomputeRecordFromKNN ( self , record ) :
    """return the classified labeling of record"""

implementation:
inputs = { "categoryIn" : [ None ] , "bottomUpIn" : self . _getStateAnomalyVector ( record ) , }
outputs = { "categoriesOut" : numpy . zeros ( ( 1 , ) ) , "bestPrototypeIndices" : numpy . zeros ( ( 1 , ) ) , "categoryProbabilitiesOut" : numpy . zeros ( ( 1 , ) ) }
# Run inference only to capture state before learning
classifier = self . htm_prediction_model . _getAnomalyClassifier ( )
knn = classifier . getSelf ( ) . _knn
# Only use points before record to classify and after the wait period .
classifier_indexes = numpy . array ( classifier . getSelf ( ) . getParameter ( 'categoryRecencyList' ) )
valid_idx = numpy . where ( ( classifier_indexes >= self . _autoDetectWaitRecords ) & ( classifier_indexes < record . ROWID ) ) [ 0 ] . tolist ( )
if len ( valid_idx ) == 0 :
    return None
classifier . setParameter ( 'inferenceMode' , True )
classifier . setParameter ( 'learningMode' , False )
classifier . getSelf ( ) . compute ( inputs , outputs )
classifier . setParameter ( 'learningMode' , True )
classifier_distances = classifier . getSelf ( ) . getLatestDistances ( )
valid_distances = classifier_distances [ valid_idx ]
if valid_distances . min ( ) <= self . _classificationMaxDist :
    classifier_indexes_prev = classifier_indexes [ valid_idx ]
    rowID = classifier_indexes_prev [ valid_distances . argmin ( ) ]
    indexID = numpy . where ( classifier_indexes == rowID ) [ 0 ] [ 0 ]
    category = classifier . getSelf ( ) . getCategoryList ( ) [ indexID ]
    return category
return None


signature:
def _constructClassificationRecord ( self ) :
    """Construct a _ HTMClassificationRecord based on the current state of the
 htm _ prediction _ model of this classifier .
 * * * This will look into the internals of the model and may depend on the
 SP , TM , and KNNClassifier * * *"""

implementation:
model = self . htm_prediction_model
sp = model . _getSPRegion ( )
tm = model . _getTPRegion ( )
tpImp = tm . getSelf ( ) . _tfdr
# Count the number of unpredicted columns
activeColumns = sp . getOutputData ( "bottomUpOut" ) . nonzero ( ) [ 0 ]
score = numpy . in1d ( activeColumns , self . _prevPredictedColumns ) . sum ( )
score = ( self . _activeColumnCount - score ) / float ( self . _activeColumnCount )
spSize = sp . getParameter ( 'activeOutputCount' )
tpSize = tm . getParameter ( 'cellsPerColumn' ) * tm . getParameter ( 'columnCount' )
classificationVector = numpy . array ( [ ] )
if self . _vectorType == 'tpc' : # Classification Vector : [ - - - TM Cells - - - ]
    classificationVector = numpy . zeros ( tpSize )
    activeCellMatrix = tpImp . getLearnActiveStateT ( ) . reshape ( tpSize , 1 )
    activeCellIdx = numpy . where ( activeCellMatrix > 0 ) [ 0 ]
    if activeCellIdx . shape [ 0 ] > 0 :
        classificationVector [ numpy . array ( activeCellIdx , dtype = numpy . uint16 ) ] = 1
elif self . _vectorType == 'sp_tpe' : # Classification Vecotr : [ - - - SP - - - | - - - ( TM - SP ) - - - - ]
    classificationVector = numpy . zeros ( spSize + spSize )
    if activeColumns . shape [ 0 ] > 0 :
        classificationVector [ activeColumns ] = 1.0
    errorColumns = numpy . setdiff1d ( self . _prevPredictedColumns , activeColumns )
    if errorColumns . shape [ 0 ] > 0 :
        errorColumnIndexes = ( numpy . array ( errorColumns , dtype = numpy . uint16 ) + spSize )
        classificationVector [ errorColumnIndexes ] = 1.0
else :
    raise TypeError ( "Classification vector type must be either 'tpc' or" " 'sp_tpe', current value is %s" % ( self . _vectorType ) )
# Store the state for next time step
numPredictedCols = len ( self . _prevPredictedColumns )
predictedColumns = tm . getOutputData ( "topDownOut" ) . nonzero ( ) [ 0 ]
self . _prevPredictedColumns = copy . deepcopy ( predictedColumns )
if self . _anomalyVectorLength is None :
    self . _anomalyVectorLength = len ( classificationVector )
result = _CLAClassificationRecord ( ROWID = int ( model . getParameter ( '__numRunCalls' ) - 1 ) , # _ _ numRunCalls called
# at beginning of model . run
anomalyScore = score , anomalyVector = classificationVector . nonzero ( ) [ 0 ] . tolist ( ) , anomalyLabel = [ ] )
return result


signature:
def compute ( self ) :
    """Run an iteration of this anomaly classifier"""

implementation:
result = self . _constructClassificationRecord ( )
# Classify this point after waiting the classification delay
if result . ROWID >= self . _autoDetectWaitRecords :
    self . _updateState ( result )
# Save new classification record and keep history as moving window
self . saved_states . append ( result )
if len ( self . saved_states ) > self . _history_length :
    self . saved_states . pop ( 0 )
return result


signature:
def setAutoDetectWaitRecords ( self , waitRecords ) :
    """Sets the autoDetectWaitRecords ."""

implementation:
if not isinstance ( waitRecords , int ) :
    raise HTMPredictionModelInvalidArgument ( "Invalid argument type \'%s\'. WaitRecord " "must be a number." % ( type ( waitRecords ) ) )
if len ( self . saved_states ) > 0 and waitRecords < self . saved_states [ 0 ] . ROWID :
    raise HTMPredictionModelInvalidArgument ( "Invalid value. autoDetectWaitRecord value " "must be valid record within output stream. Current minimum ROWID in " "output stream is %d." % ( self . saved_states [ 0 ] . ROWID ) )
self . _autoDetectWaitRecords = waitRecords
# Update all the states in the classifier ' s cache
for state in self . saved_states :
    self . _updateState ( state )


signature:
def setAutoDetectThreshold ( self , threshold ) :
    """Sets the autoDetectThreshold .
 TODO : Ensure previously classified points outside of classifier are valid ."""

implementation:
if not ( isinstance ( threshold , float ) or isinstance ( threshold , int ) ) :
    raise HTMPredictionModelInvalidArgument ( "Invalid argument type \'%s\'. threshold " "must be a number." % ( type ( threshold ) ) )
self . _autoDetectThreshold = threshold
# Update all the states in the classifier ' s cache
for state in self . saved_states :
    self . _updateState ( state )


signature:
def _getAdditionalSpecs ( spatialImp , kwargs = { } ) :
    """Build the additional specs in three groups ( for the inspector )
 Use the type of the default argument to set the Spec type , defaulting
 to ' Byte ' for None and complex types
 Determines the spatial parameters based on the selected implementation .
 It defaults to SpatialPooler ."""

implementation:
typeNames = { int : 'UInt32' , float : 'Real32' , str : 'Byte' , bool : 'bool' , tuple : 'tuple' }
def getArgType ( arg ) :
    t = typeNames . get ( type ( arg ) , 'Byte' )
    count = 0 if t == 'Byte' else 1
    if t == 'tuple' :
        t = typeNames . get ( type ( arg [ 0 ] ) , 'Byte' )
        count = len ( arg )
    if t == 'bool' :
        t = 'UInt32'
    return ( t , count )
def getConstraints ( arg ) :
    t = typeNames . get ( type ( arg ) , 'Byte' )
    if t == 'Byte' :
        return 'multiple'
    elif t == 'bool' :
        return 'bool'
    else :
        return ''
# Get arguments from spatial pooler constructors , figure out types of
# variables and populate spatialSpec .
SpatialClass = getSPClass ( spatialImp )
sArgTuples = _buildArgs ( SpatialClass . __init__ )
spatialSpec = { }
for argTuple in sArgTuples :
    d = dict ( description = argTuple [ 1 ] , accessMode = 'ReadWrite' , dataType = getArgType ( argTuple [ 2 ] ) [ 0 ] , count = getArgType ( argTuple [ 2 ] ) [ 1 ] , constraints = getConstraints ( argTuple [ 2 ] ) )
    spatialSpec [ argTuple [ 0 ] ] = d
# Add special parameters that weren ' t handled automatically
# Spatial parameters only !
spatialSpec . update ( dict ( columnCount = dict ( description = 'Total number of columns (coincidences).' , accessMode = 'Read' , dataType = 'UInt32' , count = 1 , constraints = '' ) , inputWidth = dict ( description = 'Size of inputs to the SP.' , accessMode = 'Read' , dataType = 'UInt32' , count = 1 , constraints = '' ) , spInputNonZeros = dict ( description = 'The indices of the non-zero inputs to the spatial pooler' , accessMode = 'Read' , dataType = 'UInt32' , count = 0 , constraints = '' ) , spOutputNonZeros = dict ( description = 'The indices of the non-zero outputs from the spatial pooler' , accessMode = 'Read' , dataType = 'UInt32' , count = 0 , constraints = '' ) , spOverlapDistribution = dict ( description = """The overlaps between the active output coincidences
      and the input. The overlap amounts for each coincidence are sorted
      from highest to lowest. """ , accessMode = 'Read' , dataType = 'Real32' , count = 0 , constraints = '' ) , sparseCoincidenceMatrix = dict ( description = 'The coincidences, as a SparseMatrix' , accessMode = 'Read' , dataType = 'Byte' , count = 0 , constraints = '' ) , denseOutput = dict ( description = 'Score for each coincidence.' , accessMode = 'Read' , dataType = 'Real32' , count = 0 , constraints = '' ) , spLearningStatsStr = dict ( description = """String representation of dictionary containing a number
                     of statistics related to learning.""" , accessMode = 'Read' , dataType = 'Byte' , count = 0 , constraints = 'handle' ) , spatialImp = dict ( description = """Which spatial pooler implementation to use. Set to either
                      'py', or 'cpp'. The 'cpp' implementation is optimized for
                      speed in C++.""" , accessMode = 'ReadWrite' , dataType = 'Byte' , count = 0 , constraints = 'enum: py, cpp' ) , ) )
# The last group is for parameters that aren ' t specific to spatial pooler
otherSpec = dict ( learningMode = dict ( description = '1 if the node is learning (default 1).' , accessMode = 'ReadWrite' , dataType = 'UInt32' , count = 1 , constraints = 'bool' ) , inferenceMode = dict ( description = '1 if the node is inferring (default 0).' , accessMode = 'ReadWrite' , dataType = 'UInt32' , count = 1 , constraints = 'bool' ) , anomalyMode = dict ( description = '1 if an anomaly score is being computed' , accessMode = 'ReadWrite' , dataType = 'UInt32' , count = 1 , constraints = 'bool' ) , topDownMode = dict ( description = '1 if the node should do top down compute on the next call ' 'to compute into topDownOut (default 0).' , accessMode = 'ReadWrite' , dataType = 'UInt32' , count = 1 , constraints = 'bool' ) , activeOutputCount = dict ( description = 'Number of active elements in bottomUpOut output.' , accessMode = 'Read' , dataType = 'UInt32' , count = 1 , constraints = '' ) , logPathInput = dict ( description = 'Optional name of input log file. If set, every input vector' ' will be logged to this file.' , accessMode = 'ReadWrite' , dataType = 'Byte' , count = 0 , constraints = '' ) , logPathOutput = dict ( description = 'Optional name of output log file. If set, every output vector' ' will be logged to this file.' , accessMode = 'ReadWrite' , dataType = 'Byte' , count = 0 , constraints = '' ) , logPathOutputDense = dict ( description = 'Optional name of output log file. If set, every output vector' ' will be logged to this file as a dense vector.' , accessMode = 'ReadWrite' , dataType = 'Byte' , count = 0 , constraints = '' ) , )
return spatialSpec , otherSpec


signature:
def _initializeEphemeralMembers ( self ) :
    """Initialize all ephemeral data members , and give the derived class the
 opportunity to do the same by invoking the virtual member _ initEphemerals ( ) ,
 which is intended to be overridden .
 NOTE : this is used by both _ _ init _ _ and _ _ setstate _ _ code paths ."""

implementation:
for attrName in self . _getEphemeralMembersBase ( ) :
    if attrName != "_loaded" :
        if hasattr ( self , attrName ) :
            if self . _loaded : # print self . _ _ class _ _ . _ _ name _ _ , " contains base class member ' % s ' " \
            # " after loading . " % attrName
            # TODO : Re - enable warning or turn into error in a future release .
                pass
            else :
                print self . __class__ . __name__ , "contains base class member '%s'" % attrName
if not self . _loaded :
    for attrName in self . _getEphemeralMembersBase ( ) :
        if attrName != "_loaded" : # if hasattr ( self , attrName ) :
        # import pdb ; pdb . set _ trace ( )
            assert not hasattr ( self , attrName )
        else :
            assert hasattr ( self , attrName )
# Profiling information
self . _profileObj = None
self . _iterations = 0
# Let derived class initialize ephemerals
self . _initEphemerals ( )
self . _checkEphemeralMembers ( )


signature:
def initialize ( self ) :
    """Overrides : meth : ` ~ nupic . bindings . regions . PyRegion . PyRegion . initialize ` ."""

implementation:
# Zero out the spatial output in case it is requested
self . _spatialPoolerOutput = numpy . zeros ( self . columnCount , dtype = GetNTAReal ( ) )
# Zero out the rfInput in case it is requested
self . _spatialPoolerInput = numpy . zeros ( ( 1 , self . inputWidth ) , dtype = GetNTAReal ( ) )
# Allocate the spatial pooler
self . _allocateSpatialFDR ( None )


signature:
def _allocateSpatialFDR ( self , rfInput ) :
    """Allocate the spatial pooler instance ."""

implementation:
if self . _sfdr :
    return
# Retrieve the necessary extra arguments that were handled automatically
autoArgs = dict ( ( name , getattr ( self , name ) ) for name in self . _spatialArgNames )
# Instantiate the spatial pooler class .
if ( ( self . SpatialClass == CPPSpatialPooler ) or ( self . SpatialClass == PYSpatialPooler ) ) :
    autoArgs [ 'columnDimensions' ] = [ self . columnCount ]
    autoArgs [ 'inputDimensions' ] = [ self . inputWidth ]
    autoArgs [ 'potentialRadius' ] = self . inputWidth
    self . _sfdr = self . SpatialClass ( ** autoArgs )


signature:
def compute ( self , inputs , outputs ) :
    """Run one iteration , profiling it if requested .
 : param inputs : ( dict ) mapping region input names to numpy . array values
 : param outputs : ( dict ) mapping region output names to numpy . arrays that
 should be populated with output values by this method"""

implementation:
# Uncomment this to find out who is generating divide by 0 , or other numpy warnings
# numpy . seterr ( divide = ' raise ' , invalid = ' raise ' , over = ' raise ' )
# Modify this line to turn on profiling for a given node . The results file
# ( ' hotshot . stats ' ) will be sensed and printed out by the vision framework ' s
# RunInference . py script at the end of inference .
# Also uncomment the hotshot import at the top of this file .
if False and self . learningMode and self . _iterations > 0 and self . _iterations <= 10 :
    import hotshot
    if self . _iterations == 10 :
        print "\n  Collecting and sorting internal node profiling stats generated by hotshot..."
        stats = hotshot . stats . load ( "hotshot.stats" )
        stats . strip_dirs ( )
        stats . sort_stats ( 'time' , 'calls' )
        stats . print_stats ( )
    # The guts of the compute are contained in the _ compute ( ) call so that we
    # can profile it if requested .
    if self . _profileObj is None :
        print "\n  Preparing to capture profile using hotshot..."
        if os . path . exists ( 'hotshot.stats' ) : # There is an old hotshot stats profile left over , remove it .
            os . remove ( 'hotshot.stats' )
        self . _profileObj = hotshot . Profile ( "hotshot.stats" , 1 , 1 )
        # filename , lineevents , linetimings
    self . _profileObj . runcall ( self . _compute , * [ inputs , outputs ] )
else :
    self . _compute ( inputs , outputs )


signature:
def _compute ( self , inputs , outputs ) :
    """Run one iteration of SPRegion ' s compute"""

implementation:
# if self . topDownMode and ( not ' topDownIn ' in inputs ) :
# raise RuntimeError ( " The input topDownIn must be linked in if "
# " topDownMode is True " )
if self . _sfdr is None :
    raise RuntimeError ( "Spatial pooler has not been initialized" )
if not self . topDownMode : # BOTTOM - UP compute
    self . _iterations += 1
    # Get our inputs into numpy arrays
    buInputVector = inputs [ 'bottomUpIn' ]
    resetSignal = False
    if 'resetIn' in inputs :
        assert len ( inputs [ 'resetIn' ] ) == 1
        resetSignal = inputs [ 'resetIn' ] [ 0 ] != 0
    # Perform inference and / or learning
    rfOutput = self . _doBottomUpCompute ( rfInput = buInputVector . reshape ( ( 1 , buInputVector . size ) ) , resetSignal = resetSignal )
    outputs [ 'bottomUpOut' ] [ : ] = rfOutput . flat
else : # TOP - DOWN inference
    topDownIn = inputs . get ( 'topDownIn' , None )
    spatialTopDownOut , temporalTopDownOut = self . _doTopDownInfer ( topDownIn )
    outputs [ 'spatialTopDownOut' ] [ : ] = spatialTopDownOut
    if temporalTopDownOut is not None :
        outputs [ 'temporalTopDownOut' ] [ : ] = temporalTopDownOut
# OBSOLETE
outputs [ 'anomalyScore' ] [ : ] = 0


signature:
def _doBottomUpCompute ( self , rfInput , resetSignal ) :
    """Do one iteration of inference and / or learning and return the result
 Parameters :
 rfInput : Input vector . Shape is : ( 1 , inputVectorLen ) .
 resetSignal : True if reset is asserted"""

implementation:
# Conditional compute break
self . _conditionalBreak ( )
# Save the rfInput for the spInputNonZeros parameter
self . _spatialPoolerInput = rfInput . reshape ( - 1 )
assert ( rfInput . shape [ 0 ] == 1 )
# Run inference using the spatial pooler . We learn on the coincidences only
# if we are in learning mode and trainingStep is set appropriately .
# Run SFDR bottom - up compute and cache output in self . _ spatialPoolerOutput
inputVector = numpy . array ( rfInput [ 0 ] ) . astype ( 'uint32' )
outputVector = numpy . zeros ( self . _sfdr . getNumColumns ( ) ) . astype ( 'uint32' )
self . _sfdr . compute ( inputVector , self . learningMode , outputVector )
self . _spatialPoolerOutput [ : ] = outputVector [ : ]
# Direct logging of SP outputs if requested
if self . _fpLogSP :
    output = self . _spatialPoolerOutput . reshape ( - 1 )
    outputNZ = output . nonzero ( ) [ 0 ]
    outStr = " " . join ( [ "%d" % int ( token ) for token in outputNZ ] )
    print >> self . _fpLogSP , output . size , outStr
# Direct logging of SP inputs
if self . _fpLogSPInput :
    output = rfInput . reshape ( - 1 )
    outputNZ = output . nonzero ( ) [ 0 ]
    outStr = " " . join ( [ "%d" % int ( token ) for token in outputNZ ] )
    print >> self . _fpLogSPInput , output . size , outStr
return self . _spatialPoolerOutput


signature:
def getBaseSpec ( cls ) :
    """Doesn ' t include the spatial , temporal and other parameters
 : returns : ( dict ) The base Spec for SPRegion ."""

implementation:
spec = dict ( description = SPRegion . __doc__ , singleNodeOnly = True , inputs = dict ( bottomUpIn = dict ( description = """The input vector.""" , dataType = 'Real32' , count = 0 , required = True , regionLevel = False , isDefaultInput = True , requireSplitterMap = False ) , resetIn = dict ( description = """A boolean flag that indicates whether
                         or not the input vector received in this compute cycle
                         represents the start of a new temporal sequence.""" , dataType = 'Real32' , count = 1 , required = False , regionLevel = True , isDefaultInput = False , requireSplitterMap = False ) , topDownIn = dict ( description = """The top-down input signal, generated from
                        feedback from upper levels""" , dataType = 'Real32' , count = 0 , required = False , regionLevel = True , isDefaultInput = False , requireSplitterMap = False ) , sequenceIdIn = dict ( description = "Sequence ID" , dataType = 'UInt64' , count = 1 , required = False , regionLevel = True , isDefaultInput = False , requireSplitterMap = False ) , ) , outputs = dict ( bottomUpOut = dict ( description = """The output signal generated from the bottom-up inputs
                          from lower levels.""" , dataType = 'Real32' , count = 0 , regionLevel = True , isDefaultOutput = True ) , topDownOut = dict ( description = """The top-down output signal, generated from
                        feedback from upper levels""" , dataType = 'Real32' , count = 0 , regionLevel = True , isDefaultOutput = False ) , spatialTopDownOut = dict ( description = """The top-down output, generated only from the current
                         SP output. This can be used to evaluate how well the
                         SP is representing the inputs independent of the TM.""" , dataType = 'Real32' , count = 0 , regionLevel = True , isDefaultOutput = False ) , temporalTopDownOut = dict ( description = """The top-down output, generated only from the current
                         TM output feedback down through the SP.""" , dataType = 'Real32' , count = 0 , regionLevel = True , isDefaultOutput = False ) , anomalyScore = dict ( description = """The score for how 'anomalous' (i.e. rare) this spatial
                        input pattern is. Higher values are increasingly rare""" , dataType = 'Real32' , count = 1 , regionLevel = True , isDefaultOutput = False ) , ) , parameters = dict ( breakPdb = dict ( description = 'Set to 1 to stop in the pdb debugger on the next compute' , dataType = 'UInt32' , count = 1 , constraints = 'bool' , defaultValue = 0 , accessMode = 'ReadWrite' ) , breakKomodo = dict ( description = 'Set to 1 to stop in the Komodo debugger on the next compute' , dataType = 'UInt32' , count = 1 , constraints = 'bool' , defaultValue = 0 , accessMode = 'ReadWrite' ) , ) , )
return spec


signature:
def getSpec ( cls ) :
    """Overrides : meth : ` ~ nupic . bindings . regions . PyRegion . PyRegion . getSpec ` .
 The parameters collection is constructed based on the parameters specified
 by the various components ( spatialSpec , temporalSpec and otherSpec )"""

implementation:
spec = cls . getBaseSpec ( )
s , o = _getAdditionalSpecs ( spatialImp = getDefaultSPImp ( ) )
spec [ 'parameters' ] . update ( s )
spec [ 'parameters' ] . update ( o )
return spec


signature:
def getParameter ( self , parameterName , index = - 1 ) :
    """Overrides : meth : ` ~ nupic . bindings . regions . PyRegion . PyRegion . getParameter ` .
 Most parameters are handled automatically by PyRegion ' s parameter get
 mechanism . The ones that need special treatment are explicitly handled here ."""

implementation:
if parameterName == 'activeOutputCount' :
    return self . columnCount
elif parameterName == 'spatialPoolerInput' :
    return list ( self . _spatialPoolerInput . reshape ( - 1 ) )
elif parameterName == 'spatialPoolerOutput' :
    return list ( self . _spatialPoolerOutput )
elif parameterName == 'spNumActiveOutputs' :
    return len ( self . _spatialPoolerOutput . nonzero ( ) [ 0 ] )
elif parameterName == 'spOutputNonZeros' :
    return [ len ( self . _spatialPoolerOutput ) ] + list ( self . _spatialPoolerOutput . nonzero ( ) [ 0 ] )
elif parameterName == 'spInputNonZeros' :
    import pdb ;
    pdb . set_trace ( )
    return [ len ( self . _spatialPoolerInput ) ] + list ( self . _spatialPoolerInput . nonzero ( ) [ 0 ] )
elif parameterName == 'spLearningStatsStr' :
    try :
        return str ( self . _sfdr . getLearningStats ( ) )
    except :
        return str ( dict ( ) )
else :
    return PyRegion . getParameter ( self , parameterName , index )


signature:
def setParameter ( self , parameterName , index , parameterValue ) :
    """Overrides : meth : ` ~ nupic . bindings . regions . PyRegion . PyRegion . setParameter ` .
 Set the value of a Spec parameter . Most parameters are handled
 automatically by PyRegion ' s parameter set mechanism . The ones that need
 special treatment are explicitly handled here ."""

implementation:
if parameterName in self . _spatialArgNames :
    setattr ( self . _sfdr , parameterName , parameterValue )
elif parameterName == "logPathInput" :
    self . logPathInput = parameterValue
    # Close any existing log file
    if self . _fpLogSPInput :
        self . _fpLogSPInput . close ( )
        self . _fpLogSPInput = None
    # Open a new log file
    if parameterValue :
        self . _fpLogSPInput = open ( self . logPathInput , 'w' )
elif parameterName == "logPathOutput" :
    self . logPathOutput = parameterValue
    # Close any existing log file
    if self . _fpLogSP :
        self . _fpLogSP . close ( )
        self . _fpLogSP = None
    # Open a new log file
    if parameterValue :
        self . _fpLogSP = open ( self . logPathOutput , 'w' )
elif parameterName == "logPathOutputDense" :
    self . logPathOutputDense = parameterValue
    # Close any existing log file
    if self . _fpLogSPDense :
        self . _fpLogSPDense . close ( )
        self . _fpLogSPDense = None
    # Open a new log file
    if parameterValue :
        self . _fpLogSPDense = open ( self . logPathOutputDense , 'w' )
elif hasattr ( self , parameterName ) :
    setattr ( self , parameterName , parameterValue )
else :
    raise Exception ( 'Unknown parameter: ' + parameterName )


signature:
def writeToProto ( self , proto ) :
    """Overrides : meth : ` ~ nupic . bindings . regions . PyRegion . PyRegion . writeToProto ` .
 Write state to proto object .
 : param proto : SPRegionProto capnproto object"""

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


signature:
def readFromProto ( cls , proto ) :
    """Overrides : meth : ` ~ nupic . bindings . regions . PyRegion . PyRegion . readFromProto ` .
 Read state from proto object .
 : param proto : SPRegionProto capnproto object"""

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


signature:
def _initEphemerals ( self ) :
    """Initialize all ephemerals used by derived classes ."""

implementation:
if hasattr ( self , '_sfdr' ) and self . _sfdr :
    self . _spatialPoolerOutput = numpy . zeros ( self . columnCount , dtype = GetNTAReal ( ) )
else :
    self . _spatialPoolerOutput = None
# Will be filled in initInNetwork
# Direct logging support ( faster than node watch )
self . _fpLogSPInput = None
self . _fpLogSP = None
self . _fpLogSPDense = None
self . logPathInput = ""
self . logPathOutput = ""
self . logPathOutputDense = ""


signature:
def getParameterArrayCount ( self , name , index ) :
    """Overrides : meth : ` ~ nupic . bindings . regions . PyRegion . PyRegion . getParameterArrayCount ` .
 TODO : as a temporary hack , getParameterArrayCount checks to see if there ' s a
 variable , private or not , with that name . If so , it returns the value of the
 variable ."""

implementation:
p = self . getParameter ( name )
if ( not hasattr ( p , '__len__' ) ) :
    raise Exception ( "Attempt to access parameter '%s' as an array but it is not an array" % name )
return len ( p )


signature:
def getParameterArray ( self , name , index , a ) :
    """Overrides : meth : ` ~ nupic . bindings . regions . PyRegion . PyRegion . getParameterArray ` .
 TODO : as a temporary hack , getParameterArray checks to see if there ' s a
 variable , private or not , with that name . If so , it returns the value of the
 variable ."""

implementation:
p = self . getParameter ( name )
if ( not hasattr ( p , '__len__' ) ) :
    raise Exception ( "Attempt to access parameter '%s' as an array but it is not an array" % name )
if len ( p ) > 0 :
    a [ : ] = p [ : ]


signature:
def _cacheSequenceInfoType ( self ) :
    """Figure out whether reset , sequenceId ,
 both or neither are present in the data .
 Compute once instead of every time .
 Taken from filesource . py"""

implementation:
hasReset = self . resetFieldName is not None
hasSequenceId = self . sequenceIdFieldName is not None
if hasReset and not hasSequenceId :
    self . _sequenceInfoType = self . SEQUENCEINFO_RESET_ONLY
    self . _prevSequenceId = 0
elif not hasReset and hasSequenceId :
    self . _sequenceInfoType = self . SEQUENCEINFO_SEQUENCEID_ONLY
    self . _prevSequenceId = None
elif hasReset and hasSequenceId :
    self . _sequenceInfoType = self . SEQUENCEINFO_BOTH
else :
    self . _sequenceInfoType = self . SEQUENCEINFO_NONE


signature:
def _getTPClass ( temporalImp ) :
    """Return the class corresponding to the given temporalImp string"""

implementation:
if temporalImp == 'py' :
    return backtracking_tm . BacktrackingTM
elif temporalImp == 'cpp' :
    return backtracking_tm_cpp . BacktrackingTMCPP
elif temporalImp == 'tm_py' :
    return backtracking_tm_shim . TMShim
elif temporalImp == 'tm_cpp' :
    return backtracking_tm_shim . TMCPPShim
elif temporalImp == 'monitored_tm_py' :
    return backtracking_tm_shim . MonitoredTMShim
else :
    raise RuntimeError ( "Invalid temporalImp '%s'. Legal values are: 'py', " "'cpp', 'tm_py', 'monitored_tm_py'" % ( temporalImp ) )


signature:
def _buildArgs ( f , self = None , kwargs = { } ) :
    """Get the default arguments from the function and assign as instance vars .
 Return a list of 3 - tuples with ( name , description , defaultValue ) for each
 argument to the function .
 Assigns all arguments to the function as instance variables of TMRegion .
 If the argument was not provided , uses the default value .
 Pops any values from kwargs that go to the function ."""

implementation:
# Get the name , description , and default value for each argument
argTuples = getArgumentDescriptions ( f )
argTuples = argTuples [ 1 : ]
# Remove ' self '
# Get the names of the parameters to our own constructor and remove them
# Check for _ originial _ init first , because if LockAttributesMixin is used ,
# _ _ init _ _ ' s signature will be just ( self , * args , * * kw ) , but
# _ original _ init is created with the original signature
# init = getattr ( self , ' _ original _ init ' , self . _ _ init _ _ )
init = TMRegion . __init__
ourArgNames = [ t [ 0 ] for t in getArgumentDescriptions ( init ) ]
# Also remove a few other names that aren ' t in our constructor but are
# computed automatically ( e . g . numberOfCols for the TM )
ourArgNames += [ 'numberOfCols' , # TM
]
for argTuple in argTuples [ : ] :
    if argTuple [ 0 ] in ourArgNames :
        argTuples . remove ( argTuple )
# Build the dictionary of arguments
if self :
    for argTuple in argTuples :
        argName = argTuple [ 0 ]
        if argName in kwargs : # Argument was provided
            argValue = kwargs . pop ( argName )
        else : # Argument was not provided ; use the default value if there is one , and
        # raise an exception otherwise
            if len ( argTuple ) == 2 : # No default value
                raise TypeError ( "Must provide '%s'" % argName )
            argValue = argTuple [ 2 ]
        # Set as an instance variable if ' self ' was passed in
        setattr ( self , argName , argValue )
return argTuples


signature:
def _getAdditionalSpecs ( temporalImp , kwargs = { } ) :
    """Build the additional specs in three groups ( for the inspector )
 Use the type of the default argument to set the Spec type , defaulting
 to ' Byte ' for None and complex types
 Determines the spatial parameters based on the selected implementation .
 It defaults to TemporalMemory .
 Determines the temporal parameters based on the temporalImp"""

implementation:
typeNames = { int : 'UInt32' , float : 'Real32' , str : 'Byte' , bool : 'bool' , tuple : 'tuple' }
def getArgType ( arg ) :
    t = typeNames . get ( type ( arg ) , 'Byte' )
    count = 0 if t == 'Byte' else 1
    if t == 'tuple' :
        t = typeNames . get ( type ( arg [ 0 ] ) , 'Byte' )
        count = len ( arg )
    if t == 'bool' :
        t = 'UInt32'
    return ( t , count )
def getConstraints ( arg ) :
    t = typeNames . get ( type ( arg ) , 'Byte' )
    if t == 'Byte' :
        return 'multiple'
    elif t == 'bool' :
        return 'bool'
    else :
        return ''
# Build up parameters from temporal memory ' s constructor
TemporalClass = _getTPClass ( temporalImp )
tArgTuples = _buildArgs ( TemporalClass . __init__ )
temporalSpec = { }
for argTuple in tArgTuples :
    d = dict ( description = argTuple [ 1 ] , accessMode = 'ReadWrite' , dataType = getArgType ( argTuple [ 2 ] ) [ 0 ] , count = getArgType ( argTuple [ 2 ] ) [ 1 ] , constraints = getConstraints ( argTuple [ 2 ] ) )
    temporalSpec [ argTuple [ 0 ] ] = d
# Add temporal parameters that weren ' t handled automatically
temporalSpec . update ( dict ( columnCount = dict ( description = 'Total number of columns.' , accessMode = 'Read' , dataType = 'UInt32' , count = 1 , constraints = '' ) , cellsPerColumn = dict ( description = 'Number of cells per column.' , accessMode = 'Read' , dataType = 'UInt32' , count = 1 , constraints = '' ) , inputWidth = dict ( description = 'Number of inputs to the TM.' , accessMode = 'Read' , dataType = 'UInt32' , count = 1 , constraints = '' ) , predictedSegmentDecrement = dict ( description = 'Predicted segment decrement' , accessMode = 'Read' , dataType = 'Real' , count = 1 , constraints = '' ) , orColumnOutputs = dict ( description = """OR together the cell outputs from each column to produce
      the temporal memory output. When this mode is enabled, the number of
      cells per column must also be specified and the output size of the region
      should be set the same as columnCount""" , accessMode = 'Read' , dataType = 'Bool' , count = 1 , constraints = 'bool' ) , cellsSavePath = dict ( description = """Optional path to file in which large temporal memory cells
                     data structure is to be saved.""" , accessMode = 'ReadWrite' , dataType = 'Byte' , count = 0 , constraints = '' ) , temporalImp = dict ( description = """Which temporal memory implementation to use. Set to either
       'py' or 'cpp'. The 'cpp' implementation is optimized for speed in C++.""" , accessMode = 'ReadWrite' , dataType = 'Byte' , count = 0 , constraints = 'enum: py, cpp' ) , ) )
# The last group is for parameters that aren ' t strictly spatial or temporal
otherSpec = dict ( learningMode = dict ( description = 'True if the node is learning (default True).' , accessMode = 'ReadWrite' , dataType = 'Bool' , count = 1 , defaultValue = True , constraints = 'bool' ) , inferenceMode = dict ( description = 'True if the node is inferring (default False).' , accessMode = 'ReadWrite' , dataType = 'Bool' , count = 1 , defaultValue = False , constraints = 'bool' ) , computePredictedActiveCellIndices = dict ( description = 'True if active and predicted active indices should be computed' , accessMode = 'Create' , dataType = 'Bool' , count = 1 , defaultValue = False , constraints = 'bool' ) , anomalyMode = dict ( description = 'True if an anomaly score is being computed' , accessMode = 'Create' , dataType = 'Bool' , count = 1 , defaultValue = False , constraints = 'bool' ) , topDownMode = dict ( description = 'True if the node should do top down compute on the next call ' 'to compute into topDownOut (default False).' , accessMode = 'ReadWrite' , dataType = 'Bool' , count = 1 , defaultValue = False , constraints = 'bool' ) , activeOutputCount = dict ( description = 'Number of active elements in bottomUpOut output.' , accessMode = 'Read' , dataType = 'UInt32' , count = 1 , constraints = '' ) , storeDenseOutput = dict ( description = """Whether to keep the dense column output (needed for
                     denseOutput parameter).""" , accessMode = 'ReadWrite' , dataType = 'UInt32' , count = 1 , constraints = 'bool' ) , logPathOutput = dict ( description = 'Optional name of output log file. If set, every output vector' ' will be logged to this file as a sparse vector.' , accessMode = 'ReadWrite' , dataType = 'Byte' , count = 0 , constraints = '' ) , )
return temporalSpec , otherSpec


signature:
def initialize ( self ) :
    """Overrides : meth : ` ~ nupic . bindings . regions . PyRegion . initialize ` ."""

implementation:
# Allocate appropriate temporal memory object
# Retrieve the necessary extra arguments that were handled automatically
autoArgs = dict ( ( name , getattr ( self , name ) ) for name in self . _temporalArgNames )
if self . _tfdr is None :
    tpClass = _getTPClass ( self . temporalImp )
    if self . temporalImp in [ 'py' , 'cpp' , 'r' , 'tm_py' , 'tm_cpp' , 'monitored_tm_py' , ] :
        self . _tfdr = tpClass ( numberOfCols = self . columnCount , cellsPerColumn = self . cellsPerColumn , ** autoArgs )
    else :
        raise RuntimeError ( "Invalid temporalImp" )


signature:
def _compute ( self , inputs , outputs ) :
    """Run one iteration of TMRegion ' s compute"""

implementation:
# if self . topDownMode and ( not ' topDownIn ' in inputs ) :
# raise RuntimeError ( " The input topDownIn must be linked in if "
# " topDownMode is True " )
if self . _tfdr is None :
    raise RuntimeError ( "TM has not been initialized" )
# Conditional compute break
self . _conditionalBreak ( )
self . _iterations += 1
# Get our inputs as numpy array
buInputVector = inputs [ 'bottomUpIn' ]
# Handle reset signal
resetSignal = False
if 'resetIn' in inputs :
    assert len ( inputs [ 'resetIn' ] ) == 1
    if inputs [ 'resetIn' ] [ 0 ] != 0 :
        self . _tfdr . reset ( )
        self . _sequencePos = 0
# Position within the current sequence
if self . computePredictedActiveCellIndices :
    prevPredictedState = self . _tfdr . getPredictedState ( ) . reshape ( - 1 ) . astype ( 'float32' )
if self . anomalyMode :
    prevPredictedColumns = self . _tfdr . topDownCompute ( ) . copy ( ) . nonzero ( ) [ 0 ]
# Perform inference and / or learning
tpOutput = self . _tfdr . compute ( buInputVector , self . learningMode , self . inferenceMode )
self . _sequencePos += 1
# OR ' ing together the cells in each column ?
if self . orColumnOutputs :
    tpOutput = tpOutput . reshape ( self . columnCount , self . cellsPerColumn ) . max ( axis = 1 )
# Direct logging of non - zero TM outputs
if self . _fpLogTPOutput :
    output = tpOutput . reshape ( - 1 )
    outputNZ = tpOutput . nonzero ( ) [ 0 ]
    outStr = " " . join ( [ "%d" % int ( token ) for token in outputNZ ] )
    print >> self . _fpLogTPOutput , output . size , outStr
# Write the bottom up out to our node outputs
outputs [ 'bottomUpOut' ] [ : ] = tpOutput . flat
if self . topDownMode : # Top - down compute
    outputs [ 'topDownOut' ] [ : ] = self . _tfdr . topDownCompute ( ) . copy ( )
# Set output for use with anomaly classification region if in anomalyMode
if self . anomalyMode :
    activeLearnCells = self . _tfdr . getLearnActiveStateT ( )
    size = activeLearnCells . shape [ 0 ] * activeLearnCells . shape [ 1 ]
    outputs [ 'lrnActiveStateT' ] [ : ] = activeLearnCells . reshape ( size )
    activeColumns = buInputVector . nonzero ( ) [ 0 ]
    outputs [ 'anomalyScore' ] [ : ] = anomaly . computeRawAnomalyScore ( activeColumns , prevPredictedColumns )
if self . computePredictedActiveCellIndices : # Reshape so we are dealing with 1D arrays
    activeState = self . _tfdr . _getActiveState ( ) . reshape ( - 1 ) . astype ( 'float32' )
    activeIndices = numpy . where ( activeState != 0 ) [ 0 ]
    predictedIndices = numpy . where ( prevPredictedState != 0 ) [ 0 ]
    predictedActiveIndices = numpy . intersect1d ( activeIndices , predictedIndices )
    outputs [ "activeCells" ] . fill ( 0 )
    outputs [ "activeCells" ] [ activeIndices ] = 1
    outputs [ "predictedActiveCells" ] . fill ( 0 )
    outputs [ "predictedActiveCells" ] [ predictedActiveIndices ] = 1


signature:
def getBaseSpec ( cls ) :
    """Doesn ' t include the spatial , temporal and other parameters
 : returns : ( dict ) the base Spec for TMRegion ."""

implementation:
spec = dict ( description = TMRegion . __doc__ , singleNodeOnly = True , inputs = dict ( bottomUpIn = dict ( description = """The input signal, conceptually organized as an
                         image pyramid data structure, but internally
                         organized as a flattened vector.""" , dataType = 'Real32' , count = 0 , required = True , regionLevel = False , isDefaultInput = True , requireSplitterMap = False ) , resetIn = dict ( description = """Effectively a boolean flag that indicates whether
                         or not the input vector received in this compute cycle
                         represents the first training presentation in a
                         new temporal sequence.""" , dataType = 'Real32' , count = 1 , required = False , regionLevel = True , isDefaultInput = False , requireSplitterMap = False ) , sequenceIdIn = dict ( description = "Sequence ID" , dataType = 'UInt64' , count = 1 , required = False , regionLevel = True , isDefaultInput = False , requireSplitterMap = False ) , ) , outputs = dict ( bottomUpOut = dict ( description = """The output signal generated from the bottom-up inputs
                          from lower levels.""" , dataType = 'Real32' , count = 0 , regionLevel = True , isDefaultOutput = True ) , topDownOut = dict ( description = """The top-down inputsignal, generated from
                        feedback from upper levels""" , dataType = 'Real32' , count = 0 , regionLevel = True , isDefaultOutput = False ) , activeCells = dict ( description = "The cells that are active" , dataType = 'Real32' , count = 0 , regionLevel = True , isDefaultOutput = False ) , predictedActiveCells = dict ( description = "The cells that are active and predicted" , dataType = 'Real32' , count = 0 , regionLevel = True , isDefaultOutput = False ) , anomalyScore = dict ( description = """The score for how 'anomalous' (i.e. rare) the current
                        sequence is. Higher values are increasingly rare""" , dataType = 'Real32' , count = 1 , regionLevel = True , isDefaultOutput = False ) , lrnActiveStateT = dict ( description = """Active cells during learn phase at time t.  This is
                        used for anomaly classification.""" , dataType = 'Real32' , count = 0 , regionLevel = True , isDefaultOutput = False ) , ) , parameters = dict ( breakPdb = dict ( description = 'Set to 1 to stop in the pdb debugger on the next compute' , dataType = 'UInt32' , count = 1 , constraints = 'bool' , defaultValue = 0 , accessMode = 'ReadWrite' ) , breakKomodo = dict ( description = 'Set to 1 to stop in the Komodo debugger on the next compute' , dataType = 'UInt32' , count = 1 , constraints = 'bool' , defaultValue = 0 , accessMode = 'ReadWrite' ) , ) , commands = { } )
return spec


implementation:
spec = cls . getBaseSpec ( )
t , o = _getAdditionalSpecs ( temporalImp = gDefaultTemporalImp )
spec [ 'parameters' ] . update ( t )
spec [ 'parameters' ] . update ( o )
return spec


signature:
def getParameter ( self , parameterName , index = - 1 ) :
    """Overrides : meth : ` ~ nupic . bindings . regions . PyRegion . PyRegion . getParameter ` .
 Get the value of a parameter . Most parameters are handled automatically by
 : class : ` ~ nupic . bindings . regions . PyRegion . PyRegion ` ' s parameter get mechanism . The
 ones that need special treatment are explicitly handled here ."""

implementation:
if parameterName in self . _temporalArgNames :
    return getattr ( self . _tfdr , parameterName )
else :
    return PyRegion . getParameter ( self , parameterName , index )


signature:
def setParameter ( self , parameterName , index , parameterValue ) :
    """Overrides : meth : ` ~ nupic . bindings . regions . PyRegion . PyRegion . setParameter ` ."""

implementation:
if parameterName in self . _temporalArgNames :
    setattr ( self . _tfdr , parameterName , parameterValue )
elif parameterName == "logPathOutput" :
    self . logPathOutput = parameterValue
    # Close any existing log file
    if self . _fpLogTPOutput is not None :
        self . _fpLogTPOutput . close ( )
        self . _fpLogTPOutput = None
    # Open a new log file if requested
    if parameterValue :
        self . _fpLogTPOutput = open ( self . logPathOutput , 'w' )
elif hasattr ( self , parameterName ) :
    setattr ( self , parameterName , parameterValue )
else :
    raise Exception ( 'Unknown parameter: ' + parameterName )


signature:
def finishLearning ( self ) :
    """Perform an internal optimization step that speeds up inference if we know
 learning will not be performed anymore . This call may , for example , remove
 all potential inputs to each column ."""

implementation:
if self . _tfdr is None :
    raise RuntimeError ( "Temporal memory has not been initialized" )
if hasattr ( self . _tfdr , 'finishLearning' ) :
    self . resetSequenceStates ( )
    self . _tfdr . finishLearning ( )


signature:
def writeToProto ( self , proto ) :
    """Overrides : meth : ` ~ nupic . bindings . regions . PyRegion . PyRegion . writeToProto ` .
 Write state to proto object .
 : param proto : TMRegionProto capnproto object"""

implementation:
proto . temporalImp = self . temporalImp
proto . columnCount = self . columnCount
proto . inputWidth = self . inputWidth
proto . cellsPerColumn = self . cellsPerColumn
proto . learningMode = self . learningMode
proto . inferenceMode = self . inferenceMode
proto . anomalyMode = self . anomalyMode
proto . topDownMode = self . topDownMode
proto . computePredictedActiveCellIndices = ( self . computePredictedActiveCellIndices )
proto . orColumnOutputs = self . orColumnOutputs
if self . temporalImp == "py" :
    tmProto = proto . init ( "backtrackingTM" )
elif self . temporalImp == "cpp" :
    tmProto = proto . init ( "backtrackingTMCpp" )
elif self . temporalImp == "tm_py" :
    tmProto = proto . init ( "temporalMemory" )
elif self . temporalImp == "tm_cpp" :
    tmProto = proto . init ( "temporalMemory" )
else :
    raise TypeError ( "Unsupported temporalImp for capnp serialization: {}" . format ( self . temporalImp ) )
self . _tfdr . write ( tmProto )


signature:
def readFromProto ( cls , proto ) :
    """Overrides : meth : ` ~ nupic . bindings . regions . PyRegion . PyRegion . readFromProto ` .
 Read state from proto object .
 : param proto : TMRegionProto capnproto object"""

implementation:
instance = cls ( proto . columnCount , proto . inputWidth , proto . cellsPerColumn )
instance . temporalImp = proto . temporalImp
instance . learningMode = proto . learningMode
instance . inferenceMode = proto . inferenceMode
instance . anomalyMode = proto . anomalyMode
instance . topDownMode = proto . topDownMode
instance . computePredictedActiveCellIndices = ( proto . computePredictedActiveCellIndices )
instance . orColumnOutputs = proto . orColumnOutputs
if instance . temporalImp == "py" :
    tmProto = proto . backtrackingTM
elif instance . temporalImp == "cpp" :
    tmProto = proto . backtrackingTMCpp
elif instance . temporalImp == "tm_py" :
    tmProto = proto . temporalMemory
elif instance . temporalImp == "tm_cpp" :
    tmProto = proto . temporalMemory
else :
    raise TypeError ( "Unsupported temporalImp for capnp serialization: {}" . format ( instance . temporalImp ) )
instance . _tfdr = _getTPClass ( proto . temporalImp ) . read ( tmProto )
return instance


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

implementation:
if name == 'bottomUpOut' :
    return self . outputWidth
elif name == 'topDownOut' :
    return self . columnCount
elif name == 'lrnActiveStateT' :
    return self . outputWidth
elif name == "activeCells" :
    return self . outputWidth
elif name == "predictedActiveCells" :
    return self . outputWidth
else :
    raise Exception ( "Invalid output name specified" )


signature:
def computeRawAnomalyScore ( activeColumns , prevPredictedColumns ) :
    """Computes the raw anomaly score .
 The raw anomaly score is the fraction of active columns not predicted .
 : param activeColumns : array of active column indices
 : param prevPredictedColumns : array of columns indices predicted in prev step
 : returns : anomaly score 0 . . 1 ( float )"""

implementation:
nActiveColumns = len ( activeColumns )
if nActiveColumns > 0 : # Test whether each element of a 1 - D array is also present in a second
# array . Sum to get the total # of columns that are active and were
# predicted .
    score = numpy . in1d ( activeColumns , prevPredictedColumns ) . sum ( )
    # Get the percent of active columns that were NOT predicted , that is
    # our anomaly score .
    score = ( nActiveColumns - score ) / float ( nActiveColumns )
else : # There are no active columns .
    score = 0.0
return score


signature:
def compute ( self , activeColumns , predictedColumns , inputValue = None , timestamp = None ) :
    """Compute the anomaly score as the percent of active columns not predicted .
 : param activeColumns : array of active column indices
 : param predictedColumns : array of columns indices predicted in this step
 ( used for anomaly in step T + 1)
 : param inputValue : ( optional ) value of current input to encoders
 ( eg " cat " for category encoder )
 ( used in anomaly - likelihood )
 : param timestamp : ( optional ) date timestamp when the sample occured
 ( used in anomaly - likelihood )
 : returns : the computed anomaly score ; float 0 . . 1"""

implementation:
# Start by computing the raw anomaly score .
anomalyScore = computeRawAnomalyScore ( activeColumns , predictedColumns )
# Compute final anomaly based on selected mode .
if self . _mode == Anomaly . MODE_PURE :
    score = anomalyScore
elif self . _mode == Anomaly . MODE_LIKELIHOOD :
    if inputValue is None :
        raise ValueError ( "Selected anomaly mode 'Anomaly.MODE_LIKELIHOOD' " "requires 'inputValue' as parameter to compute() method. " )
    probability = self . _likelihood . anomalyProbability ( inputValue , anomalyScore , timestamp )
    # low likelihood - > hi anomaly
    score = 1 - probability
elif self . _mode == Anomaly . MODE_WEIGHTED :
    probability = self . _likelihood . anomalyProbability ( inputValue , anomalyScore , timestamp )
    score = anomalyScore * ( 1 - probability )
# Last , do moving - average if windowSize was specified .
if self . _movingAverage is not None :
    score = self . _movingAverage . next ( score )
# apply binary discretization if required
if self . _binaryThreshold is not None :
    if score >= self . _binaryThreshold :
        score = 1.0
    else :
        score = 0.0
return score


signature:
def addGraph ( self , data , position = 111 , xlabel = None , ylabel = None ) :
    """Adds a graph to the plot ' s figure .
 @ param data See matplotlib . Axes . plot documentation .
 @ param position A 3 - digit number . The first two digits define a 2D grid
 where subplots may be added . The final digit specifies the nth grid
 location for the added subplot
 @ param xlabel text to be displayed on the x - axis
 @ param ylabel text to be displayed on the y - axis"""

implementation:
ax = self . _addBase ( position , xlabel = xlabel , ylabel = ylabel )
ax . plot ( data )
plt . draw ( )


signature:
def addHistogram ( self , data , position = 111 , xlabel = None , ylabel = None , bins = None ) :
    """Adds a histogram to the plot ' s figure .
 @ param data See matplotlib . Axes . hist documentation .
 @ param position A 3 - digit number . The first two digits define a 2D grid
 where subplots may be added . The final digit specifies the nth grid
 location for the added subplot
 @ param xlabel text to be displayed on the x - axis
 @ param ylabel text to be displayed on the y - axis"""

implementation:
ax = self . _addBase ( position , xlabel = xlabel , ylabel = ylabel )
ax . hist ( data , bins = bins , color = "green" , alpha = 0.8 )
plt . draw ( )


signature:
def add2DArray ( self , data , position = 111 , xlabel = None , ylabel = None , cmap = None , aspect = "auto" , interpolation = "nearest" , name = None ) :
    """Adds an image to the plot ' s figure .
 @ param data a 2D array . See matplotlib . Axes . imshow documentation .
 @ param position A 3 - digit number . The first two digits define a 2D grid
 where subplots may be added . The final digit specifies the nth grid
 location for the added subplot
 @ param xlabel text to be displayed on the x - axis
 @ param ylabel text to be displayed on the y - axis
 @ param cmap color map used in the rendering
 @ param aspect how aspect ratio is handled during resize
 @ param interpolation interpolation method"""

implementation:
if cmap is None : # The default colormodel is an ugly blue - red model .
    cmap = cm . Greys
ax = self . _addBase ( position , xlabel = xlabel , ylabel = ylabel )
ax . imshow ( data , cmap = cmap , aspect = aspect , interpolation = interpolation )
if self . _show :
    plt . draw ( )
if name is not None :
    if not os . path . exists ( "log" ) :
        os . mkdir ( "log" )
    plt . savefig ( "log/{name}.png" . format ( name = name ) , bbox_inches = "tight" , figsize = ( 8 , 6 ) , dpi = 400 )


signature:
def _addBase ( self , position , xlabel = None , ylabel = None ) :
    """Adds a subplot to the plot ' s figure at specified position .
 @ param position A 3 - digit number . The first two digits define a 2D grid
 where subplots may be added . The final digit specifies the nth grid
 location for the added subplot
 @ param xlabel text to be displayed on the x - axis
 @ param ylabel text to be displayed on the y - axis
 @ returns ( matplotlib . Axes ) Axes instance"""

implementation:
ax = self . _fig . add_subplot ( position )
ax . set_xlabel ( xlabel )
ax . set_ylabel ( ylabel )
return ax


signature:
def _generateOverlapping ( filename = "overlap.csv" , numSequences = 2 , elementsPerSeq = 3 , numRepeats = 10 , hub = [ 0 , 1 ] , hubOffset = 1 , resets = False ) :
    """Generate a temporal dataset containing sequences that overlap one or more
 elements with other sequences .
 Parameters :
 filename : name of the file to produce , including extension . It will
 be created in a ' datasets ' sub - directory within the
 directory containing this script .
 numSequences : how many sequences to generate
 elementsPerSeq : length of each sequence
 numRepeats : how many times to repeat each sequence in the output
 hub : sub - sequence to place within each other sequence
 hubOffset : where , within each sequence , to place the hub
 resets : if True , turn on reset at start of each sequence"""

implementation:
# Check for conflicts in arguments
assert ( hubOffset + len ( hub ) <= elementsPerSeq )
# Create the output file
scriptDir = os . path . dirname ( __file__ )
pathname = os . path . join ( scriptDir , 'datasets' , filename )
print "Creating %s..." % ( pathname )
fields = [ ( 'reset' , 'int' , 'R' ) , ( 'field1' , 'string' , '' ) , ( 'field2' , 'float' , '' ) ]
outFile = FileRecordStream ( pathname , write = True , fields = fields )
# Create the sequences with the hub in the middle
sequences = [ ]
nextElemIdx = max ( hub ) + 1
for _ in range ( numSequences ) :
    seq = [ ]
    for j in range ( hubOffset ) :
        seq . append ( nextElemIdx )
        nextElemIdx += 1
    for j in hub :
        seq . append ( j )
    j = hubOffset + len ( hub )
    while j < elementsPerSeq :
        seq . append ( nextElemIdx )
        nextElemIdx += 1
        j += 1
    sequences . append ( seq )
# Write out the sequences in random order
seqIdxs = [ ]
for _ in range ( numRepeats ) :
    seqIdxs += range ( numSequences )
random . shuffle ( seqIdxs )
for seqIdx in seqIdxs :
    reset = int ( resets )
    seq = sequences [ seqIdx ]
    for ( x ) in seq :
        outFile . appendRecord ( [ reset , str ( x ) , x ] )
        reset = 0
outFile . close ( )


signature:
def _generateFirstOrder0 ( ) :
    """Generate the initial , first order , and second order transition
 probabilities for ' probability0 ' . For this model , we generate the following
 set of sequences :
 .1 . 75
 0 - - - - 1 - - - - - 2
 \ \ .25
 \ .9 .5
 \--- 4--------- 2
 Parameters :
 retval : ( initProb , firstOrder , secondOrder , seqLen )
 initProb : Initial probability for each category . This is a vector
 of length len ( categoryList ) .
 firstOrder : A dictionary of the 1st order probabilities . The key
 is the 1st element of the sequence , the value is
 the probability of each 2nd element given the first .
 secondOrder : A dictionary of the 2nd order probabilities . The key
 is the first 2 elements of the sequence , the value is
 the probability of each possible 3rd element given the
 first two .
 seqLen : Desired length of each sequence . The 1st element will
 be generated using the initProb , the 2nd element by the
 firstOrder table , and the 3rd and all successive
 elements by the secondOrder table .
 categoryList : list of category names to use
 Here is an example of some return values when there are 3 categories
 initProb : [ 0.7 , 0.2 , 0.1]
 firstOrder : { ' [ 0 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 1 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 2 ] ' : [ 0.3 , 0.3 , 0.4 ] }
 secondOrder : { ' [ 0,0 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 0,1 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 0,2 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 1,0 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 1,1 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 1,2 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 2,0 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 2,1 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 2,2 ] ' : [ 0.3 , 0.3 , 0.4 ] }"""

implementation:
# Initial probabilities , ' a ' and ' e ' equally likely
numCategories = 5
initProb = numpy . zeros ( numCategories )
initProb [ 0 ] = 1.0
# 1st order transitions
firstOrder = dict ( )
firstOrder [ '0' ] = numpy . array ( [ 0 , 0.1 , 0 , 0 , 0.9 ] )
firstOrder [ '1' ] = numpy . array ( [ 0 , 0 , 0.75 , 0.25 , 0 ] )
firstOrder [ '2' ] = numpy . array ( [ 1.0 , 0 , 0 , 0 , 0 ] )
firstOrder [ '3' ] = numpy . array ( [ 1.0 , 0 , 0 , 0 , 0 ] )
firstOrder [ '4' ] = numpy . array ( [ 0 , 0 , 0.5 , 0.5 , 0 ] )
# 2nd order transitions don ' t apply
secondOrder = None
# Generate the category list
categoryList = [ '%d' % x for x in range ( 5 ) ]
return ( initProb , firstOrder , secondOrder , 3 , categoryList )


signature:
def _generateFileFromProb ( filename , numRecords , categoryList , initProb , firstOrderProb , secondOrderProb , seqLen , numNoise = 0 , resetsEvery = None ) :
    """Generate a set of records reflecting a set of probabilities .
 Parameters :
 filename : name of . csv file to generate
 numRecords : number of records to generate
 categoryList : list of category names
 initProb : Initial probability for each category . This is a vector
 of length len ( categoryList ) .
 firstOrderProb : A dictionary of the 1st order probabilities . The key
 is the 1st element of the sequence , the value is
 the probability of each 2nd element given the first .
 secondOrderProb : A dictionary of the 2nd order probabilities . The key
 is the first 2 elements of the sequence , the value is
 the probability of each possible 3rd element given the
 first two . If this is None , then the sequences will be
 first order only .
 seqLen : Desired length of each sequence . The 1st element will
 be generated using the initProb , the 2nd element by the
 firstOrder table , and the 3rd and all successive
 elements by the secondOrder table . None means infinite
 length .
 numNoise : Number of noise elements to place between each
 sequence . The noise elements are evenly distributed from
 all categories .
 resetsEvery : If not None , generate a reset every N records
 Here is an example of some parameters :
 categoryList : [ ' cat1 ' , ' cat2 ' , ' cat3 ' ]
 initProb : [ 0.7 , 0.2 , 0.1]
 firstOrderProb : { ' [ 0 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 1 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 2 ] ' : [ 0.3 , 0.3 , 0.4 ] }
 secondOrderProb : { ' [ 0,0 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 0,1 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 0,2 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 1,0 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 1,1 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 1,2 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 2,0 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 2,1 ] ' : [ 0.3 , 0.3 , 0.4 ] ,
 ' [ 2,2 ] ' : [ 0.3 , 0.3 , 0.4 ] }"""

implementation:
# Create the file
print "Creating %s..." % ( filename )
fields = [ ( 'reset' , 'int' , 'R' ) , ( 'field1' , 'string' , '' ) , ( 'field2' , 'float' , '' ) ]
scriptDir = os . path . dirname ( __file__ )
pathname = os . path . join ( scriptDir , 'datasets' , filename )
outFile = FileRecordStream ( pathname , write = True , fields = fields )
# Convert the probabilitie tables into cumulative probabilities
initCumProb = initProb . cumsum ( )
firstOrderCumProb = dict ( )
for ( key , value ) in firstOrderProb . iteritems ( ) :
    firstOrderCumProb [ key ] = value . cumsum ( )
if secondOrderProb is not None :
    secondOrderCumProb = dict ( )
    for ( key , value ) in secondOrderProb . iteritems ( ) :
        secondOrderCumProb [ key ] = value . cumsum ( )
else :
    secondOrderCumProb = None
# Write out the sequences
elementsInSeq = [ ]
numElementsSinceReset = 0
maxCatIdx = len ( categoryList ) - 1
for _ in xrange ( numRecords ) : # Generate a reset ?
    if numElementsSinceReset == 0 :
        reset = 1
    else :
        reset = 0
    # Pick the next element , based on how are we are into the 2nd order
    # sequence .
    rand = numpy . random . rand ( )
    # Generate 1st order sequences
    if secondOrderCumProb is None :
        if len ( elementsInSeq ) == 0 :
            catIdx = numpy . searchsorted ( initCumProb , rand )
        elif len ( elementsInSeq ) >= 1 and ( seqLen is None or len ( elementsInSeq ) < seqLen - numNoise ) :
            catIdx = numpy . searchsorted ( firstOrderCumProb [ str ( elementsInSeq [ - 1 ] ) ] , rand )
        else : # random " noise "
            catIdx = numpy . random . randint ( len ( categoryList ) )
    # Generate 2nd order sequences
    else :
        if len ( elementsInSeq ) == 0 :
            catIdx = numpy . searchsorted ( initCumProb , rand )
        elif len ( elementsInSeq ) == 1 :
            catIdx = numpy . searchsorted ( firstOrderCumProb [ str ( elementsInSeq ) ] , rand )
        elif ( len ( elementsInSeq ) >= 2 ) and ( seqLen is None or len ( elementsInSeq ) < seqLen - numNoise ) :
            catIdx = numpy . searchsorted ( secondOrderCumProb [ str ( elementsInSeq [ - 2 : ] ) ] , rand )
        else : # random " noise "
            catIdx = numpy . random . randint ( len ( categoryList ) )
    # Write out the record
    catIdx = min ( maxCatIdx , catIdx )
    outFile . appendRecord ( [ reset , categoryList [ catIdx ] , catIdx ] )
    # print categoryList [ catIdx ]
    # Increment counters
    elementsInSeq . append ( catIdx )
    numElementsSinceReset += 1
    # Generate another reset ?
    if resetsEvery is not None and numElementsSinceReset == resetsEvery :
        numElementsSinceReset = 0
        elementsInSeq = [ ]
    # Start another 2nd order sequence ?
    if seqLen is not None and ( len ( elementsInSeq ) == seqLen + numNoise ) :
        elementsInSeq = [ ]
outFile . close ( )


signature:
def getVersion ( ) :
    """Get version from local file ."""

implementation:
with open ( os . path . join ( REPO_DIR , "VERSION" ) , "r" ) as versionFile :
    return versionFile . read ( ) . strip ( )


signature:
def nupicBindingsPrereleaseInstalled ( ) :
    """Make an attempt to determine if a pre - release version of nupic . bindings is
 installed already .
 @ return : boolean"""

implementation:
try :
    nupicDistribution = pkg_resources . get_distribution ( "nupic.bindings" )
    if pkg_resources . parse_version ( nupicDistribution . version ) . is_prerelease : # A pre - release dev version of nupic . bindings is installed .
        return True
except pkg_resources . DistributionNotFound :
    pass
# Silently ignore . The absence of nupic . bindings will be handled by
# setuptools by default
return False


signature:
def findRequirements ( ) :
    """Read the requirements . txt file and parse into requirements for setup ' s
 install _ requirements option ."""

implementation:
requirementsPath = os . path . join ( REPO_DIR , "requirements.txt" )
requirements = parse_file ( requirementsPath )
if nupicBindingsPrereleaseInstalled ( ) : # User has a pre - release version of nupic . bindings installed , which is only
# possible if the user installed and built nupic . bindings from source and
# it is up to the user to decide when to update nupic . bindings . We ' ll
# quietly remove the entry in requirements . txt so as to not conflate the
# two .
    requirements = [ req for req in requirements if "nupic.bindings" not in req ]
return requirements


signature:
def _handleDescriptionOption ( cmdArgStr , outDir , usageStr , hsVersion , claDescriptionTemplateFile ) :
    """Parses and validates the - - description option args and executes the
 request
 Parameters :
 cmdArgStr : JSON string compatible with _ gExperimentDescriptionSchema
 outDir : where to place generated experiment files
 usageStr : program usage string
 hsVersion : which version of hypersearch permutations file to generate , can
 be ' v1 ' or ' v2'
 claDescriptionTemplateFile : Filename containing the template description
 retval : nothing"""

implementation:
# convert - - description arg from JSON string to dict
try :
    args = json . loads ( cmdArgStr )
except Exception , e :
    raise _InvalidCommandArgException ( _makeUsageErrorStr ( ( "JSON arg parsing failed for --description: %s\n" + "ARG=<%s>" ) % ( str ( e ) , cmdArgStr ) , usageStr ) )
# print " PARSED JSON ARGS = \ n % s " % ( json . dumps ( args , indent = 4 ) )
filesDescription = _generateExperiment ( args , outDir , hsVersion = hsVersion , claDescriptionTemplateFile = claDescriptionTemplateFile )
pprint . pprint ( filesDescription )
return


signature:
def _handleDescriptionFromFileOption ( filename , outDir , usageStr , hsVersion , claDescriptionTemplateFile ) :
    """Parses and validates the - - descriptionFromFile option and executes the
 request
 Parameters :
 filename : File from which we ' ll extract description JSON
 outDir : where to place generated experiment files
 usageStr : program usage string
 hsVersion : which version of hypersearch permutations file to generate , can
 be ' v1 ' or ' v2'
 claDescriptionTemplateFile : Filename containing the template description
 retval : nothing"""

implementation:
try :
    fileHandle = open ( filename , 'r' )
    JSONStringFromFile = fileHandle . read ( ) . splitlines ( )
    JSONStringFromFile = '' . join ( JSONStringFromFile )
except Exception , e :
    raise _InvalidCommandArgException ( _makeUsageErrorStr ( ( "File open failed for --descriptionFromFile: %s\n" + "ARG=<%s>" ) % ( str ( e ) , filename ) , usageStr ) )
_handleDescriptionOption ( JSONStringFromFile , outDir , usageStr , hsVersion = hsVersion , claDescriptionTemplateFile = claDescriptionTemplateFile )
return


signature:
def _isInt ( x , precision = 0.0001 ) :
    """Return ( isInt , intValue ) for a given floating point number .
 Parameters :
 x : floating point number to evaluate
 precision : desired precision
 retval : ( isInt , intValue )
 isInt : True if x is close enough to an integer value
 intValue : x as an integer"""

implementation:
xInt = int ( round ( x ) )
return ( abs ( x - xInt ) < precision * x , xInt )


signature:
def _indentLines ( str , indentLevels = 1 , indentFirstLine = True ) :
    """Indent all lines in the given string
 str : input string
 indentLevels : number of levels of indentation to apply
 indentFirstLine : if False , the 1st line will not be indented
 Returns : The result string with all lines indented"""

implementation:
indent = _ONE_INDENT * indentLevels
lines = str . splitlines ( True )
result = ''
if len ( lines ) > 0 and not indentFirstLine :
    first = 1
    result += lines [ 0 ]
else :
    first = 0
for line in lines [ first : ] :
    result += indent + line
return result


signature:
def _generateMetricSpecString ( inferenceElement , metric , params = None , field = None , returnLabel = False ) :
    """Generates the string representation of a MetricSpec object , and returns
 the metric key associated with the metric .
 Parameters :
 inferenceElement :
 An InferenceElement value that indicates which part of the inference this
 metric is computed on
 metric :
 The type of the metric being computed ( e . g . aae , avg _ error )
 params :
 A dictionary of parameters for the metric . The keys are the parameter names
 and the values should be the parameter values ( e . g . window = 200)
 field :
 The name of the field for which this metric is being computed
 returnLabel :
 If True , returns the label of the MetricSpec that was generated"""

implementation:
metricSpecArgs = dict ( metric = metric , field = field , params = params , inferenceElement = inferenceElement )
metricSpecAsString = "MetricSpec(%s)" % ', ' . join ( [ '%s=%r' % ( item [ 0 ] , item [ 1 ] ) for item in metricSpecArgs . iteritems ( ) ] )
if not returnLabel :
    return metricSpecAsString
spec = MetricSpec ( ** metricSpecArgs )
metricLabel = spec . getLabel ( )
return metricSpecAsString , metricLabel


signature:
def _generateFileFromTemplates ( templateFileNames , outputFilePath , replacementDict ) :
    """Generates a file by applying token replacements to the given template
 file
 templateFileName :
 A list of template file names ; these files are assumed to be in
 the same directory as the running experiment _ generator . py script .
 ExpGenerator will perform the substitution and concanetate
 the files in the order they are specified
 outputFilePath : Absolute path of the output file
 replacementDict :
 A dictionary of token / replacement pairs"""

implementation:
# Find out where we ' re running from so we know where to find templates
installPath = os . path . dirname ( __file__ )
outputFile = open ( outputFilePath , "w" )
outputLines = [ ]
inputLines = [ ]
firstFile = True
for templateFileName in templateFileNames : # Separate lines from each file by two blank lines .
    if not firstFile :
        inputLines . extend ( [ os . linesep ] * 2 )
    firstFile = False
    inputFilePath = os . path . join ( installPath , templateFileName )
    inputFile = open ( inputFilePath )
    inputLines . extend ( inputFile . readlines ( ) )
    inputFile . close ( )
print "Writing " , len ( inputLines ) , "lines..."
for line in inputLines :
    tempLine = line
    # Enumerate through each key in replacementDict and replace with value
    for k , v in replacementDict . iteritems ( ) :
        if v is None :
            v = "None"
        tempLine = re . sub ( k , v , tempLine )
    outputFile . write ( tempLine )
outputFile . close ( )