def tmDiff2(tm1, tm2, verbosity = <NUM_LIT:0>, relaxSegmentTests =True,<EOL>checkLearn = True, checkStates = True):

<EOL>if sameTMParams(tm1, tm2) == False:<EOL><INDENT>print("<STR_LIT>")<EOL>return False<EOL><DEDENT>tm1Label = "<STR_LIT>" % tm1.__class__.__name__<EOL>tm2Label = "<STR_LIT>" % tm2.__class__.__name__<EOL>result = True<EOL>if checkStates:<EOL><INDENT>if (tm1.infActiveState['<STR_LIT:t>'] != tm2.infActiveState['<STR_LIT:t>']).any():<EOL><INDENT>print('<STR_LIT>', numpy.where(tm1.infActiveState['<STR_LIT:t>'] != tm2.infActiveState['<STR_LIT:t>']))<EOL>result = False<EOL><DEDENT>if (tm1.infPredictedState['<STR_LIT:t>'] - tm2.infPredictedState['<STR_LIT:t>']).any():<EOL><INDENT>print('<STR_LIT>', numpy.where(tm1.infPredictedState['<STR_LIT:t>'] != tm2.infPredictedState['<STR_LIT:t>']))<EOL>result = False<EOL><DEDENT>if checkLearn and (tm1.lrnActiveState['<STR_LIT:t>'] - tm2.lrnActiveState['<STR_LIT:t>']).any():<EOL><INDENT>print('<STR_LIT>', numpy.where(tm1.lrnActiveState['<STR_LIT:t>'] != tm2.lrnActiveState['<STR_LIT:t>']))<EOL>result = False<EOL><DEDENT>if checkLearn and (tm1.lrnPredictedState['<STR_LIT:t>'] - tm2.lrnPredictedState['<STR_LIT:t>']).any():<EOL><INDENT>print('<STR_LIT>', numpy.where(tm1.lrnPredictedState['<STR_LIT:t>'] != tm2.lrnPredictedState['<STR_LIT:t>']))<EOL>result = False<EOL><DEDENT>if checkLearn and abs(tm1.getAvgLearnedSeqLength() - tm2.getAvgLearnedSeqLength()) > <NUM_LIT>:<EOL><INDENT>print("<STR_LIT>", end='<STR_LIT:U+0020>')<EOL>print(tm1.getAvgLearnedSeqLength(), "<STR_LIT>", tm2.getAvgLearnedSeqLength())<EOL>result = False<EOL><DEDENT><DEDENT>if tm1.getNumSegments() != tm2.getNumSegments():<EOL><INDENT>print("<STR_LIT>", tm1.getNumSegments(), tm2.getNumSegments())<EOL>result = False<EOL><DEDENT>if tm1.getNumSynapses() != tm2.getNumSynapses():<EOL><INDENT>print("<STR_LIT>", tm1.getNumSynapses(), tm2.getNumSynapses())<EOL>if verbosity >= <NUM_LIT:3>:<EOL><INDENT>print("<STR_LIT>" % tm1Label, end='<STR_LIT:U+0020>')<EOL>tm1.printCells()<EOL>print("<STR_LIT>" % tm2Label, end='<STR_LIT:U+0020>')<EOL>tm2.printCells()<EOL><DEDENT><DEDENT>for c in range(tm1.numberOfCols):<EOL><INDENT>for i in range(tm2.cellsPerColumn):<EOL><INDENT>if tm1.getNumSegmentsInCell(c, i) != tm2.getNumSegmentsInCell(c, i):<EOL><INDENT>print("<STR_LIT>",c,i, end='<STR_LIT:U+0020>')<EOL>print(tm1.getNumSegmentsInCell(c, i), tm2.getNumSegmentsInCell(c, i))<EOL>result = False<EOL><DEDENT><DEDENT><DEDENT>if result == True and not relaxSegmentTests and checkLearn:<EOL><INDENT>for c in range(tm1.numberOfCols):<EOL><INDENT>for i in range(tm2.cellsPerColumn):<EOL><INDENT>nSegs = tm1.getNumSegmentsInCell(c, i)<EOL>for segIdx in range(nSegs):<EOL><INDENT>tm1seg = tm1.getSegmentOnCell(c, i, segIdx)<EOL>res = False<EOL>for tm2segIdx in range(nSegs):<EOL><INDENT>tm2seg = tm2.getSegmentOnCell(c, i, tm2segIdx)<EOL>if sameSegment(tm1seg, tm2seg) == True:<EOL><INDENT>res = True<EOL>break<EOL><DEDENT><DEDENT>if res == False:<EOL><INDENT>print("<STR_LIT>",c,i)<EOL>result = False<EOL>if verbosity >= <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>" % tm1Label, end='<STR_LIT:U+0020>')<EOL>tm1.printCell(c, i)<EOL>print("<STR_LIT>" % tm2Label, end='<STR_LIT:U+0020>')<EOL>tm2.printCell(c, i)<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>if result == True and (verbosity > <NUM_LIT:1>):<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>return result<EOL>

Given two TM instances, list the difference between them and returns False
if there is a difference. This function checks the major parameters. If this
passes (and checkLearn is true) it checks the number of segments on each cell.
If this passes, checks each synapse on each segment.
When comparing C++ and Py, the segments are usually in different orders in the
cells. tmDiff ignores segment order when comparing TM's.

If checkLearn is True, will check learn states as well as all the segments

If checkStates is True, will check the various state arrays


if(len(SP1._masterConnectedM)!=len(SP2._masterConnectedM)):<EOL><INDENT>print("<STR_LIT>")<EOL>return False<EOL><DEDENT>if(len(SP1._masterPotentialM)!=len(SP2._masterPotentialM)):<EOL><INDENT>print("<STR_LIT>")<EOL>return False<EOL><DEDENT>if(len(SP1._masterPermanenceM)!=len(SP2._masterPermanenceM)):<EOL><INDENT>print("<STR_LIT>")<EOL>return False<EOL><DEDENT>for i in range(<NUM_LIT:0>,len(SP1._masterConnectedM)):<EOL><INDENT>connected1 = SP1._masterConnectedM[i]<EOL>connected2 = SP2._masterConnectedM[i]<EOL>if(connected1!=connected2):<EOL><INDENT>print("<STR_LIT>"  % (i))<EOL>return False<EOL><DEDENT>permanences1 = SP1._masterPermanenceM[i];<EOL>permanences2 = SP2._masterPermanenceM[i];<EOL>if(permanences1!=permanences2):<EOL><INDENT>print("<STR_LIT>" % (i))<EOL>return False<EOL><DEDENT>potential1 = SP1._masterPotentialM[i];<EOL>potential2 = SP2._masterPotentialM[i];<EOL>if(potential1!=potential2):<EOL><INDENT>print("<STR_LIT>" % (i))<EOL>return False<EOL><DEDENT><DEDENT>if(not numpy.array_equal(SP1._firingBoostFactors,SP2._firingBoostFactors)):<EOL><INDENT>print("<STR_LIT>")<EOL>return False<EOL><DEDENT>if(not numpy.array_equal(SP1._dutyCycleAfterInh,SP2._dutyCycleAfterInh)):<EOL><INDENT>print("<STR_LIT>")<EOL>return False<EOL><DEDENT>if(not numpy.array_equal(SP1._dutyCycleBeforeInh,SP2._dutyCycleBeforeInh)):<EOL><INDENT>print("<STR_LIT>")<EOL>return False<EOL><DEDENT>print("<STR_LIT>")<EOL>return True<EOL>

Function that compares two spatial pooler instances. Compares the
static variables between the two poolers to make sure that they are equivalent.

Parameters
-----------------------------------------
SP1 first spatial pooler to be compared

SP2 second spatial pooler to be compared

To establish equality, this function does the following:

1.Compares the connected synapse matrices for each coincidence

2.Compare the potential synapse matrices for each coincidence

3.Compare the permanence matrices for each coincidence

4.Compare the firing boosts between the two poolers.

5.Compare the duty cycles before and after inhibition for both poolers


def removeSeqStarts(vectors, resets, numSteps=<NUM_LIT:1>):

<EOL>if numSteps == <NUM_LIT:0>:<EOL><INDENT>return vectors<EOL><DEDENT>resetIndices = resets.nonzero()[<NUM_LIT:0>]<EOL>removeRows = resetIndices<EOL>for i in range(numSteps-<NUM_LIT:1>):<EOL><INDENT>removeRows = numpy.hstack((removeRows, resetIndices+i+<NUM_LIT:1>))<EOL><DEDENT>return numpy.delete(vectors, removeRows, axis=<NUM_LIT:0>)<EOL>

Convert a list of sequences of pattern indices, and a pattern lookup table
    into a an array of patterns

Parameters:
-----------------------------------------------
vectors:          the data vectors. Row 0 contains the outputs from time
                  step 0, row 1 from time step 1, etc.
resets:           the reset signal. This is a vector of booleans
                  the same length as the number of rows in 'vectors'. It
                  has a 1 where a sequence started and a 0 otherwise. The
                  first 'numSteps' rows of 'vectors' of each sequence will
                  not be included in the return result.
numSteps          Number of samples to remove from the start of each sequence

retval:           copy of vectors, with the first 'numSteps' samples at the
                  start of each sequence removed.


<EOL>values = numpy.array(values)<EOL>numEntries = values.max() + <NUM_LIT:1><EOL>if freqCounts is not None:<EOL><INDENT>numEntries = max(numEntries, freqCounts.size)<EOL><DEDENT>if freqCounts is not None:<EOL><INDENT>if freqCounts.size != numEntries:<EOL><INDENT>newCounts = numpy.zeros(numEntries, dtype='<STR_LIT>')<EOL>newCounts[<NUM_LIT:0>:freqCounts.size] = freqCounts<EOL><DEDENT>else:<EOL><INDENT>newCounts = freqCounts<EOL><DEDENT><DEDENT>else:<EOL><INDENT>newCounts = numpy.zeros(numEntries, dtype='<STR_LIT>')<EOL><DEDENT>for v in values:<EOL><INDENT>newCounts[v] += <NUM_LIT:1><EOL><DEDENT>return newCounts<EOL>

Accumulate a list of values 'values' into the frequency counts 'freqCounts',
and return the updated frequency counts

For example, if values contained the following: [1,1,3,5,1,3,5], and the initial
freqCounts was None, then the return value would be:
[0,3,0,2,0,2]
which corresponds to how many of each value we saw in the input, i.e. there
were 0 0's, 3 1's, 0 2's, 2 3's, 0 4's, and 2 5's.

If freqCounts is not None, the values will be added to the existing counts and
the length of the frequency Counts will be automatically extended as necessary

Parameters:
-----------------------------------------------
values:         The values to accumulate into the frequency counts
freqCounts:     Accumulated frequency counts so far, or none


<EOL>durations = []<EOL>numOnTimes   = <NUM_LIT:0><EOL>totalOnTime = <NUM_LIT:0><EOL>nonzeros = numpy.array(vector).nonzero()[<NUM_LIT:0>]<EOL>if len(nonzeros) == <NUM_LIT:0>:<EOL><INDENT>return (<NUM_LIT:0>, <NUM_LIT:0>, [])<EOL><DEDENT>if len(nonzeros) == <NUM_LIT:1>:<EOL><INDENT>return (<NUM_LIT:1>, <NUM_LIT:1>, [<NUM_LIT:1>])<EOL><DEDENT>prev = nonzeros[<NUM_LIT:0>]<EOL>onTime = <NUM_LIT:1><EOL>endIdx = nonzeros[-<NUM_LIT:1>]<EOL>for idx in nonzeros[<NUM_LIT:1>:]:<EOL><INDENT>if idx != prev+<NUM_LIT:1>:<EOL><INDENT>totalOnTime += onTime<EOL>numOnTimes  += <NUM_LIT:1><EOL>durations.append(onTime)<EOL>onTime       = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>onTime += <NUM_LIT:1><EOL><DEDENT>prev = idx<EOL><DEDENT>totalOnTime += onTime<EOL>numOnTimes  += <NUM_LIT:1><EOL>durations.append(onTime)<EOL>return (totalOnTime, numOnTimes, durations)<EOL>

Returns 3 things for a vector:
  * the total on time
  * the number of runs
  * a list of the durations of each run.

Parameters:
-----------------------------------------------
input stream: 11100000001100000000011111100000
return value: (11, 3, [3, 2, 6])


<EOL>nonzeros = numpy.array(vector).nonzero()[<NUM_LIT:0>]<EOL>if len(nonzeros) == <NUM_LIT:0>:<EOL><INDENT>return<EOL><DEDENT>if len(nonzeros) == <NUM_LIT:1>:<EOL><INDENT>durations[nonzeros[<NUM_LIT:0>]] = <NUM_LIT:1><EOL>return<EOL><DEDENT>prev = nonzeros[<NUM_LIT:0>]<EOL>onTime = <NUM_LIT:1><EOL>onStartIdx = prev<EOL>endIdx = nonzeros[-<NUM_LIT:1>]<EOL>for idx in nonzeros[<NUM_LIT:1>:]:<EOL><INDENT>if idx != prev+<NUM_LIT:1>:<EOL><INDENT>durations[onStartIdx:onStartIdx+onTime] = list(range(<NUM_LIT:1>,onTime+<NUM_LIT:1>))<EOL>onTime       = <NUM_LIT:1><EOL>onStartIdx = idx<EOL><DEDENT>else:<EOL><INDENT>onTime += <NUM_LIT:1><EOL><DEDENT>prev = idx<EOL><DEDENT>durations[onStartIdx:onStartIdx+onTime] = list(range(<NUM_LIT:1>,onTime+<NUM_LIT:1>))<EOL>

Helper function used by averageOnTimePerTimestep. 'durations' is a vector
which must be the same len as vector. For each "on" in vector, it fills in
the corresponding element of duration with the duration of that "on" signal
up until that time

Parameters:
-----------------------------------------------
vector:     vector of output values over time
durations:  vector same length as 'vector', initialized to 0's.
            This is filled in with the durations of each 'on" signal.

Example:
vector:     11100000001100000000011111100000
durations:  12300000001200000000012345600000


<EOL>if vectors.ndim == <NUM_LIT:1>:<EOL><INDENT>vectors.shape = (-<NUM_LIT:1>,<NUM_LIT:1>)<EOL><DEDENT>numTimeSteps = len(vectors)<EOL>numElements  = len(vectors[<NUM_LIT:0>])<EOL>if numSamples is not None:<EOL><INDENT>import pdb; pdb.set_trace()   <EOL>countOn    = numpy.random.randint(<NUM_LIT:0>, numElements, numSamples)<EOL>vectors = vectors[:, countOn]<EOL><DEDENT>durations = numpy.zeros(vectors.shape, dtype='<STR_LIT>')<EOL>for col in range(vectors.shape[<NUM_LIT:1>]):<EOL><INDENT>_fillInOnTimes(vectors[:,col], durations[:,col])<EOL><DEDENT>sums = vectors.sum(axis=<NUM_LIT:1>)<EOL>sums.clip(min=<NUM_LIT:1>, max=numpy.inf, out=sums)<EOL>avgDurations = durations.sum(axis=<NUM_LIT:1>, dtype='<STR_LIT>') / sums<EOL>avgOnTime = avgDurations.sum() / (avgDurations > <NUM_LIT:0>).sum()<EOL>freqCounts = _accumulateFrequencyCounts(avgDurations)<EOL>return (avgOnTime, freqCounts)<EOL>

Computes the average on-time of the outputs that are on at each time step, and
then averages this over all time steps.

This metric is resiliant to the number of outputs that are on at each time
step. That is, if time step 0 has many more outputs on than time step 100, it
won't skew the results. This is particularly useful when measuring the
average on-time of things like the temporal memory output where you might
have many columns bursting at the start of a sequence - you don't want those
start of sequence bursts to over-influence the calculated average on-time.

Parameters:
-----------------------------------------------
vectors:          the vectors for which the onTime is calculated. Row 0
                  contains the outputs from time step 0, row 1 from time step
                  1, etc.
numSamples:       the number of elements for which on-time is calculated.
                  If not specified, then all elements are looked at.

Returns  (scalar average on-time over all time steps,
          list containing frequency counts of each encountered on-time)


<EOL>if vectors.ndim == <NUM_LIT:1>:<EOL><INDENT>vectors.shape = (-<NUM_LIT:1>,<NUM_LIT:1>)<EOL><DEDENT>numTimeSteps = len(vectors)<EOL>numElements  = len(vectors[<NUM_LIT:0>])<EOL>if numSamples is None:<EOL><INDENT>numSamples = numElements<EOL>countOn    = list(range(numElements))<EOL><DEDENT>else:<EOL><INDENT>countOn    = numpy.random.randint(<NUM_LIT:0>, numElements, numSamples)<EOL><DEDENT>sumOfLengths = <NUM_LIT:0.0><EOL>onTimeFreqCounts = None<EOL>n = <NUM_LIT:0><EOL>for i in countOn:<EOL><INDENT>(onTime, segments, durations) = _listOfOnTimesInVec(vectors[:,i])<EOL>if onTime != <NUM_LIT:0.0>:<EOL><INDENT>sumOfLengths += onTime<EOL>n += segments<EOL>onTimeFreqCounts = _accumulateFrequencyCounts(durations, onTimeFreqCounts)<EOL><DEDENT><DEDENT>if n > <NUM_LIT:0>:<EOL><INDENT>return (sumOfLengths/n, onTimeFreqCounts)<EOL><DEDENT>else:<EOL><INDENT>return (<NUM_LIT:0.0>, onTimeFreqCounts)<EOL><DEDENT>

Returns the average on-time, averaged over all on-time runs.

Parameters:
-----------------------------------------------
vectors:          the vectors for which the onTime is calculated. Row 0
                  contains the outputs from time step 0, row 1 from time step
                  1, etc.
numSamples:       the number of elements for which on-time is calculated.
                  If not specified, then all elements are looked at.

Returns:    (scalar average on-time of all outputs,
             list containing frequency counts of each encountered on-time)


def plotOutputsOverTime(vectors, buVectors=None, title='<STR_LIT>'):

<EOL>import pylab<EOL>pylab.ion()<EOL>pylab.figure()<EOL>imData = vectors.transpose()<EOL>if buVectors is not None:<EOL><INDENT>assert(buVectors.shape == vectors.shape)<EOL>imData = imData.copy()<EOL>imData[buVectors.transpose().astype('<STR_LIT:bool>')] = <NUM_LIT:2><EOL><DEDENT>pylab.imshow(imData, aspect='<STR_LIT>', cmap=pylab.cm.gray_r,<EOL>interpolation='<STR_LIT>')<EOL>pylab.title(title)<EOL>

Generate a figure that shows each output over time. Time goes left to right,
and each output is plotted on a different line, allowing you to see the overlap
in the outputs, when they turn on/off, etc.

Parameters:
------------------------------------------------------------
vectors:            the vectors to plot
buVectors:          These are normally specified when plotting the pooling
                    outputs of the temporal memory over time. The 'buVectors'
                    are the sequence outputs and the 'vectors' are the
                    pooling outputs. The buVector (sequence) outputs will be drawn
                    in a darker color than the vector (pooling) outputs to
                    distinguish where the cell is outputting due to pooling vs.
                    sequence memory.
title:              title for the plot
avgOnTime:          The average on-time measurement. If not supplied,
                    then it will be calculated from the passed in vectors.


def plotHistogram(freqCounts, title='<STR_LIT>', xLabel='<STR_LIT>'):

import pylab<EOL>pylab.ion()<EOL>pylab.figure()<EOL>pylab.bar(numpy.arange(len(freqCounts)) - <NUM_LIT:0.5>, freqCounts)<EOL>pylab.title(title)<EOL>pylab.xlabel(xLabel)<EOL>

This is usually used to display a histogram of the on-times encountered
in a particular output.

The freqCounts is a vector containg the frequency counts of each on-time
(starting at an on-time of 0 and going to an on-time = len(freqCounts)-1)

The freqCounts are typically generated from the averageOnTimePerTimestep
or averageOnTime methods of this module.

Parameters:
-----------------------------------------------
freqCounts:       The frequency counts to plot
title:            Title of the plot


<EOL>numVectors = len(vectors)<EOL>if numSamples is None:<EOL><INDENT>numSamples = numVectors-<NUM_LIT:1><EOL>countOn = list(range(numVectors-<NUM_LIT:1>))<EOL><DEDENT>else:<EOL><INDENT>countOn = numpy.random.randint(<NUM_LIT:0>, numVectors-<NUM_LIT:1>, numSamples)<EOL><DEDENT>sigmap = <NUM_LIT:0.0><EOL>for i in countOn:<EOL><INDENT>match = checkMatch(vectors[i], vectors[i+<NUM_LIT:1>], sparse=False)<EOL>if match[<NUM_LIT:1>] != <NUM_LIT:0>:<EOL><INDENT>sigmap += float(match[<NUM_LIT:0>])/match[<NUM_LIT:1>]<EOL><DEDENT><DEDENT>return sigmap / numSamples<EOL>

Returns the stability for the population averaged over multiple time steps

Parameters:
-----------------------------------------------
vectors:          the vectors for which the stability is calculated
numSamples        the number of time steps where stability is counted

At each time step, count the fraction of the active elements which are stable
from the previous step
Average all the fraction


<EOL>totalSamples = len(vectors)<EOL>windowSize = numSamples<EOL>numWindows = <NUM_LIT:0><EOL>pctStable = <NUM_LIT:0><EOL>for wStart in range(<NUM_LIT:0>, totalSamples-windowSize+<NUM_LIT:1>):<EOL><INDENT>data = vectors[wStart:wStart+windowSize]<EOL>outputSums = data.sum(axis=<NUM_LIT:0>)<EOL>stableOutputs = (outputSums == windowSize).sum()<EOL>samplePctStable = float(stableOutputs) / data[<NUM_LIT:0>].sum()<EOL>print(samplePctStable)<EOL>pctStable += samplePctStable<EOL>numWindows += <NUM_LIT:1><EOL><DEDENT>return float(pctStable) / numWindows<EOL>

Returns the percent of the outputs that remain completely stable over
N time steps.

Parameters:
-----------------------------------------------
vectors:        the vectors for which the stability is calculated
numSamples:     the number of time steps where stability is counted

For each window of numSamples, count how many outputs are active during
the entire window.


def computeSaturationLevels(outputs, outputsShape, sparseForm=False):

<EOL>if not sparseForm:<EOL><INDENT>outputs = outputs.reshape(outputsShape)<EOL>spOut = SM32(outputs)<EOL><DEDENT>else:<EOL><INDENT>if len(outputs) > <NUM_LIT:0>:<EOL><INDENT>assert (outputs.max() < outputsShape[<NUM_LIT:0>] * outputsShape[<NUM_LIT:1>])<EOL><DEDENT>spOut = SM32(<NUM_LIT:1>, outputsShape[<NUM_LIT:0>] * outputsShape[<NUM_LIT:1>])<EOL>spOut.setRowFromSparse(<NUM_LIT:0>, outputs, [<NUM_LIT:1>]*len(outputs))<EOL>spOut.reshape(outputsShape[<NUM_LIT:0>], outputsShape[<NUM_LIT:1>])<EOL><DEDENT>regionSize = <NUM_LIT:15><EOL>rows = range(regionSize+<NUM_LIT:1>, outputsShape[<NUM_LIT:0>]+<NUM_LIT:1>, regionSize)<EOL>cols = range(regionSize+<NUM_LIT:1>, outputsShape[<NUM_LIT:1>]+<NUM_LIT:1>, regionSize)<EOL>regionSums = spOut.nNonZerosPerBox(rows, cols)<EOL>(locations, values) = regionSums.tolist()<EOL>values /= float(regionSize * regionSize)<EOL>sat = list(values)<EOL>innerSat = []<EOL>locationSet = set(locations)<EOL>for (location, value) in zip(locations, values):<EOL><INDENT>(row, col) = location<EOL>if (row-<NUM_LIT:1>,col) in locationSet and (row, col-<NUM_LIT:1>) in locationSetand (row+<NUM_LIT:1>, col) in locationSet and (row, col+<NUM_LIT:1>) in locationSet:<EOL><INDENT>innerSat.append(value)<EOL><DEDENT><DEDENT>return (sat, innerSat)<EOL>

Compute the saturation for a continuous level. This breaks the level into
multiple regions and computes the saturation level for each region.

Parameters:
--------------------------------------------
outputs:      output of the level. If sparseForm is True, this is a list of
                the non-zeros. If sparseForm is False, it is the dense
                representation
outputsShape: The shape of the outputs of the level (height, width)
retval:       (sat, innerSat):
                sat: list of the saturation levels of each non-empty
                  region of the level (each 0 -> 1.0)
                innerSat: list of the saturation level of each non-empty region
                     that is not near an edge (each 0 -> 1.0)


def checkMatch(input, prediction, sparse=True, verbosity=<NUM_LIT:0>):

if sparse:<EOL><INDENT>activeElementsInInput = set(input)<EOL>activeElementsInPrediction = set(prediction)<EOL><DEDENT>else:<EOL><INDENT>activeElementsInInput = set(input.nonzero()[<NUM_LIT:0>])<EOL>activeElementsInPrediction = set(prediction.nonzero()[<NUM_LIT:0>])<EOL><DEDENT>totalActiveInPrediction = len(activeElementsInPrediction)<EOL>totalActiveInInput     = len(activeElementsInInput)<EOL>foundInInput = len(activeElementsInPrediction.intersection(activeElementsInInput))<EOL>missingFromInput = len(activeElementsInPrediction.difference(activeElementsInInput))<EOL>missingFromPrediction = len(activeElementsInInput.difference(activeElementsInPrediction))<EOL>if verbosity >= <NUM_LIT:1>:<EOL><INDENT>print("<STR_LIT>", foundInInput, "<STR_LIT>", totalActiveInPrediction, end='<STR_LIT:U+0020>')<EOL>print("<STR_LIT>", missingFromInput, "<STR_LIT>",totalActiveInPrediction, end='<STR_LIT:U+0020>')<EOL>print("<STR_LIT>", missingFromPrediction, "<STR_LIT>",totalActiveInInput)<EOL><DEDENT>return (foundInInput, totalActiveInInput, missingFromInput,<EOL>totalActiveInPrediction)<EOL>

Compares the actual input with the predicted input and returns results

Parameters:
-----------------------------------------------
input:          The actual input
prediction:     the predicted input
verbosity:        If > 0, print debugging messages
sparse:         If true, they are in sparse form (list of
                   active indices)

retval         (foundInInput, totalActiveInInput, missingFromInput,
                          totalActiveInPrediction)
  foundInInput:       The number of predicted active elements that were
                      found in the actual input
  totalActiveInInput: The total number of active elements in the input.
  missingFromInput:   The number of predicted active elements that were not
                      found in the actual input
  totalActiveInPrediction:  The total number of active elements in the prediction


def predictionExtent(inputs, resets, outputs, minOverlapPct=<NUM_LIT>):

<EOL>predCounts = None<EOL>predTotal = <NUM_LIT:0><EOL>nSamples = len(outputs)<EOL>predTotalNotLimited = <NUM_LIT:0><EOL>nSamplesNotLimited = <NUM_LIT:0><EOL>nCols = len(inputs[<NUM_LIT:0>])<EOL>nCellsPerCol = len(outputs[<NUM_LIT:0>]) // nCols<EOL>for idx in range(nSamples):<EOL><INDENT>activeCols = outputs[idx].reshape(nCols, nCellsPerCol).max(axis=<NUM_LIT:1>)<EOL>steps = <NUM_LIT:0><EOL>while (idx+steps+<NUM_LIT:1> < nSamples) and (resets[idx+steps+<NUM_LIT:1>] == <NUM_LIT:0>):<EOL><INDENT>overlap = numpy.logical_and(inputs[idx+steps+<NUM_LIT:1>], activeCols)<EOL>overlapPct = <NUM_LIT> * float(overlap.sum()) / inputs[idx+steps+<NUM_LIT:1>].sum()<EOL>if overlapPct >= minOverlapPct:<EOL><INDENT>steps += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT>predCounts = _accumulateFrequencyCounts([steps], predCounts)<EOL>predTotal += steps<EOL>if resets[idx] or((idx+steps+<NUM_LIT:1> < nSamples) and (not resets[idx+steps+<NUM_LIT:1>])):<EOL><INDENT>predTotalNotLimited += steps<EOL>nSamplesNotLimited += <NUM_LIT:1><EOL><DEDENT><DEDENT>return (float(predTotal) / nSamples,<EOL>float(predTotalNotLimited) / nSamplesNotLimited,<EOL>predCounts)<EOL>

Computes the predictive ability of a temporal memory (TM). This routine returns
a value which is the average number of time steps of prediction provided
by the TM. It accepts as input the inputs, outputs, and resets provided to
the TM as well as a 'minOverlapPct' used to evalulate whether or not a
prediction is a good enough match to the actual input.

The 'outputs' are the pooling outputs of the TM. This routine treats each output
as a "manifold" that includes the active columns that should be present in the
next N inputs. It then looks at each successive input and sees if it's active
columns are within the manifold. For each output sample, it computes how
many time steps it can go forward on the input before the input overlap with
the manifold is less then 'minOverlapPct'. It returns the average number of
time steps calculated for each output.

Parameters:
-----------------------------------------------
inputs:          The inputs to the TM. Row 0 contains the inputs from time
                 step 0, row 1 from time step 1, etc.
resets:          The reset input to the TM. Element 0 contains the reset from
                 time step 0, element 1 from time step 1, etc.
outputs:         The pooling outputs from the TM. Row 0 contains the outputs
                 from time step 0, row 1 from time step 1, etc.
minOverlapPct:   How much each input's columns must overlap with the pooling
                 output's columns to be considered a valid prediction.

retval:          (Average number of time steps of prediction over all output
                   samples,
                  Average number of time steps of prediction when we aren't
                   cut short by the end of the sequence,
                  List containing frequency counts of each encountered
                   prediction time)


from nupic.math.cross import cross<EOL>shape = spaceShape<EOL>if shape[<NUM_LIT:0>] == <NUM_LIT:1> and shape[<NUM_LIT:1>] == <NUM_LIT:1>:<EOL><INDENT>centerOffsets = [(<NUM_LIT:0>,<NUM_LIT:0>)]<EOL><DEDENT>else:<EOL><INDENT>xMin = -<NUM_LIT:1> * (shape[<NUM_LIT:1>] // <NUM_LIT:2>)<EOL>xMax = xMin + shape[<NUM_LIT:1>] - <NUM_LIT:1><EOL>xPositions = list(range(stepSize * xMin, stepSize * xMax + <NUM_LIT:1>, stepSize))<EOL>yMin = -<NUM_LIT:1> * (shape[<NUM_LIT:0>] // <NUM_LIT:2>)<EOL>yMax = yMin + shape[<NUM_LIT:0>] - <NUM_LIT:1><EOL>yPositions = list(range(stepSize * yMin, stepSize * yMax + <NUM_LIT:1>, stepSize))<EOL>centerOffsets = list(cross(yPositions, xPositions))<EOL><DEDENT>numCenterOffsets = len(centerOffsets)<EOL>print("<STR_LIT>", centerOffsets)<EOL>shape = spreadShape<EOL>if shape[<NUM_LIT:0>] == <NUM_LIT:1> and shape[<NUM_LIT:1>] == <NUM_LIT:1>:<EOL><INDENT>spreadOffsets = [(<NUM_LIT:0>,<NUM_LIT:0>)]<EOL><DEDENT>else:<EOL><INDENT>xMin = -<NUM_LIT:1> * (shape[<NUM_LIT:1>] // <NUM_LIT:2>)<EOL>xMax = xMin + shape[<NUM_LIT:1>] - <NUM_LIT:1><EOL>xPositions = list(range(stepSize * xMin, stepSize * xMax + <NUM_LIT:1>, stepSize))<EOL>yMin = -<NUM_LIT:1> * (shape[<NUM_LIT:0>] // <NUM_LIT:2>)<EOL>yMax = yMin + shape[<NUM_LIT:0>] - <NUM_LIT:1><EOL>yPositions = list(range(stepSize * yMin, stepSize * yMax + <NUM_LIT:1>, stepSize))<EOL>spreadOffsets = list(cross(yPositions, xPositions))<EOL>spreadOffsets.remove((<NUM_LIT:0>,<NUM_LIT:0>))<EOL>spreadOffsets.insert(<NUM_LIT:0>, (<NUM_LIT:0>,<NUM_LIT:0>))<EOL><DEDENT>numSpreadOffsets = len(spreadOffsets)<EOL>print("<STR_LIT>", spreadOffsets)<EOL>return centerOffsets, spreadOffsets<EOL>

Generates centre offsets and spread offsets for block-mode based training
regimes - star, cross, block.

  Parameters:
  -----------------------------------------------
  spaceShape:   The (height, width) of the 2-D space to explore. This
                sets the number of center-points.
  spreadShape:  The shape (height, width) of the area around each center-point
                to explore.
  stepSize:     The step size. How big each step is, in pixels. This controls
                *both* the spacing of the center-points within the block and the
                points we explore around each center-point
  retval:       (centreOffsets, spreadOffsets)


def makeCloneMap(columnsShape, outputCloningWidth, outputCloningHeight=-<NUM_LIT:1>):

if outputCloningHeight < <NUM_LIT:0>:<EOL><INDENT>outputCloningHeight = outputCloningWidth<EOL><DEDENT>columnsHeight, columnsWidth = columnsShape<EOL>numDistinctMasters = outputCloningWidth * outputCloningHeight<EOL>a = numpy.empty((columnsHeight, columnsWidth), '<STR_LIT>')<EOL>for row in range(columnsHeight):<EOL><INDENT>for col in range(columnsWidth):<EOL><INDENT>a[row, col] = (col % outputCloningWidth) +(row % outputCloningHeight) * outputCloningWidth<EOL><DEDENT><DEDENT>return a, numDistinctMasters<EOL>


def numpyStr(array, format='<STR_LIT>', includeIndices=False, includeZeros=True):

shape = array.shape<EOL>assert (len(shape) <= <NUM_LIT:2>)<EOL>items = ['<STR_LIT:[>']<EOL>if len(shape) == <NUM_LIT:1>:<EOL><INDENT>if includeIndices:<EOL><INDENT>format = '<STR_LIT>' + format<EOL>if includeZeros:<EOL><INDENT>rowItems = [format % (c,x) for (c,x) in enumerate(array)]<EOL><DEDENT>else:<EOL><INDENT>rowItems = [format % (c,x) for (c,x) in enumerate(array) if x != <NUM_LIT:0>]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>rowItems = [format % (x) for x in array]<EOL><DEDENT>items.extend(rowItems)<EOL><DEDENT>else:<EOL><INDENT>(rows, cols) = shape<EOL>if includeIndices:<EOL><INDENT>format = '<STR_LIT>' + format<EOL><DEDENT>for r in range(rows):<EOL><INDENT>if includeIndices:<EOL><INDENT>rowItems = [format % (r,c,x) for c,x in enumerate(array[r])]<EOL><DEDENT>else:<EOL><INDENT>rowItems = [format % (x) for x in array[r]]<EOL><DEDENT>if r > <NUM_LIT:0>:<EOL><INDENT>items.append('<STR_LIT>')<EOL><DEDENT>items.append('<STR_LIT:[>')<EOL>items.extend(rowItems)<EOL>if r < rows-<NUM_LIT:1>:<EOL><INDENT>items.append('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>items.append('<STR_LIT:]>')<EOL><DEDENT><DEDENT><DEDENT>items.append('<STR_LIT:]>')<EOL>return '<STR_LIT:U+0020>'.join(items)<EOL>

Pretty print a numpy matrix using the given format string for each
    value. Return the string representation

    Parameters:
    ------------------------------------------------------------
    array:    The numpy array to print. This can be either a 1D vector or 2D matrix
    format:   The format string to use for each value
    includeIndices: If true, include [row,col] label for each value
    includeZeros:   Can only be set to False if includeIndices is on.
                    If True, include 0 values in the print-out
                    If False, exclude 0 values from the print-out.


tm = super(TemporalMemoryMonitorMixin, cls).read(proto)<EOL>tm.mmName = None<EOL>tm._mmTraces = None<EOL>tm._mmData = None<EOL>tm.mmClearHistory()<EOL>tm._mmResetActive = True<EOL>return tm<EOL>

Intercepts TemporalMemory deserialization request in order to initialize
`TemporalMemoryMonitorMixin` state

@param proto (DynamicStructBuilder) Proto object

@return (TemporalMemory) TemporalMemory shim instance


Translate parameters and initialize member variables specific to `backtracking_tm.py`.


tm = super(TMShimMixin, cls).read(proto)<EOL>tm.infActiveState = {"<STR_LIT:t>": None}<EOL>return tm<EOL>

Intercepts TemporalMemory deserialization request in order to initialize
`self.infActiveState`

@param proto (DynamicStructBuilder) Proto object

@return (TemporalMemory) TemporalMemory shim instance


def compute(self, bottomUpInput, enableLearn, computeInfOutput=None):

super(TMShimMixin, self).compute(set(bottomUpInput.nonzero()[<NUM_LIT:0>]),<EOL>learn=enableLearn)<EOL>numberOfCells = self.numberOfCells()<EOL>activeState = numpy.zeros(numberOfCells)<EOL>activeState[self.getActiveCells()] = <NUM_LIT:1><EOL>self.infActiveState["<STR_LIT:t>"] = activeState<EOL>output = numpy.zeros(numberOfCells)<EOL>output[self.getPredictiveCells()] = <NUM_LIT:1><EOL>output[self.getActiveCells()] = <NUM_LIT:1><EOL>return output<EOL>

(From `backtracking_tm.py`)
Handle one compute, possibly learning.

@param bottomUpInput     The bottom-up input, typically from a spatial pooler
@param enableLearn       If true, perform learning
@param computeInfOutput  If None, default behavior is to disable the inference
                         output when enableLearn is on.
                         If true, compute the inference output
                         If false, do not compute the inference output


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

(From `backtracking_tm.py`)
Top-down compute - generate expected input given output of the TM

@param topDownIn top down input from the level above us

@returns best estimate of the TM input that would have generated bottomUpOut.


tm = super(MonitoredTMShim, cls).read(proto)<EOL>tm.infActiveState = {"<STR_LIT:t>": None}<EOL>return tm<EOL>


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


return "<STR_LIT>" + "<STR_LIT:U+0020>".join(fmt % x for x in array_) + "<STR_LIT>"<EOL>

Return a string with pretty-print of a numpy array using the given format
    for each element


def compute(self, recordNum, patternNZ, classification, learn, infer):

if self.verbosity >= <NUM_LIT:1>:<EOL><INDENT>print("<STR_LIT>", learn)<EOL>print("<STR_LIT>", recordNum)<EOL>print("<STR_LIT>" % len(patternNZ), patternNZ)<EOL>print("<STR_LIT>", classification)<EOL><DEDENT>if len(self._patternNZHistory) > <NUM_LIT:0>:<EOL><INDENT>if recordNum < self._patternNZHistory[-<NUM_LIT:1>][<NUM_LIT:0>]:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>if len(self._patternNZHistory) == <NUM_LIT:0> orrecordNum > self._patternNZHistory[-<NUM_LIT:1>][<NUM_LIT:0>]:<EOL><INDENT>self._patternNZHistory.append((recordNum, patternNZ))<EOL><DEDENT>retval = {}<EOL>if max(patternNZ) > self._maxInputIdx:<EOL><INDENT>newMaxInputIdx = max(patternNZ)<EOL>for nSteps in self.steps:<EOL><INDENT>self._weightMatrix[nSteps] = numpy.concatenate((<EOL>self._weightMatrix[nSteps],<EOL>numpy.zeros(shape=(newMaxInputIdx-self._maxInputIdx,<EOL>self._maxBucketIdx+<NUM_LIT:1>))), axis=<NUM_LIT:0>)<EOL><DEDENT>self._maxInputIdx = int(newMaxInputIdx)<EOL><DEDENT>if classification is not None:<EOL><INDENT>if type(classification["<STR_LIT>"]) is not list:<EOL><INDENT>bucketIdxList = [classification["<STR_LIT>"]]<EOL>actValueList = [classification["<STR_LIT>"]]<EOL>numCategory = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>bucketIdxList = classification["<STR_LIT>"]<EOL>actValueList = classification["<STR_LIT>"]<EOL>numCategory = len(classification["<STR_LIT>"])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if learn:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>actValueList = None<EOL>bucketIdxList = None<EOL><DEDENT>if infer:<EOL><INDENT>retval = self.infer(patternNZ, actValueList)<EOL><DEDENT>if learn and classification["<STR_LIT>"] is not None:<EOL><INDENT>for categoryI in range(numCategory):<EOL><INDENT>bucketIdx = bucketIdxList[categoryI]<EOL>actValue = actValueList[categoryI]<EOL>if bucketIdx > self._maxBucketIdx:<EOL><INDENT>for nSteps in self.steps:<EOL><INDENT>self._weightMatrix[nSteps] = numpy.concatenate((<EOL>self._weightMatrix[nSteps],<EOL>numpy.zeros(shape=(self._maxInputIdx+<NUM_LIT:1>,<EOL>bucketIdx-self._maxBucketIdx))), axis=<NUM_LIT:1>)<EOL><DEDENT>self._maxBucketIdx = int(bucketIdx)<EOL><DEDENT>while self._maxBucketIdx > len(self._actualValues) - <NUM_LIT:1>:<EOL><INDENT>self._actualValues.append(None)<EOL><DEDENT>if self._actualValues[bucketIdx] is None:<EOL><INDENT>self._actualValues[bucketIdx] = actValue<EOL><DEDENT>else:<EOL><INDENT>if (isinstance(actValue, int) or<EOL>isinstance(actValue, float) or<EOL>isinstance(actValue, int)):<EOL><INDENT>self._actualValues[bucketIdx] = ((<NUM_LIT:1.0> - self.actValueAlpha)<EOL>* self._actualValues[bucketIdx]<EOL>+ self.actValueAlpha * actValue)<EOL><DEDENT>else:<EOL><INDENT>self._actualValues[bucketIdx] = actValue<EOL><DEDENT><DEDENT><DEDENT>for (learnRecordNum, learnPatternNZ) in self._patternNZHistory:<EOL><INDENT>error = self._calculateError(recordNum, bucketIdxList)<EOL>nSteps = recordNum - learnRecordNum<EOL>if nSteps in self.steps:<EOL><INDENT>for bit in learnPatternNZ:<EOL><INDENT>self._weightMatrix[nSteps][bit, :] += self.alpha * error[nSteps]<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if infer and self.verbosity >= <NUM_LIT:1>:<EOL><INDENT>print("<STR_LIT>")<EOL>print("<STR_LIT>", retval["<STR_LIT>"])<EOL>for (nSteps, votes) in list(retval.items()):<EOL><INDENT>if nSteps == "<STR_LIT>":<EOL><INDENT>continue<EOL><DEDENT>print("<STR_LIT>" % (nSteps), _pFormatArray(votes))<EOL>bestBucketIdx = votes.argmax()<EOL>print(("<STR_LIT>"<EOL>"<STR_LIT>" % (bestBucketIdx,<EOL>retval["<STR_LIT>"][bestBucketIdx])))<EOL><DEDENT>print()<EOL><DEDENT>return retval<EOL>

Process one input sample.

This method is called by outer loop code outside the nupic-engine. We
use this instead of the nupic engine compute() because our inputs and
outputs aren't fixed size vectors of reals.

:param recordNum: Record number of this input pattern. Record numbers
  normally increase sequentially by 1 each time unless there are missing
  records in the dataset. Knowing this information insures that we don't get
  confused by missing records.

:param patternNZ: List of the active indices from the output below. When the
  input is from TemporalMemory, this list should be the indices of the
  active cells.

:param classification: Dict of the classification information where:

  - bucketIdx: list of indices of the encoder bucket
  - actValue: list of actual values going into the encoder

  Classification could be None for inference mode.
:param learn: (bool) if true, learn this sample
:param infer: (bool) if true, perform inference

:return:    Dict containing inference results, there is one entry for each
            step in self.steps, where the key is the number of steps, and
            the value is an array containing the relative likelihood for
            each bucketIdx starting from bucketIdx 0.

            There is also an entry containing the average actual value to
            use for each bucket. The key is 'actualValues'.

            for example:

            .. code-block:: python

               {1 :             [0.1, 0.3, 0.2, 0.7],
                 4 :             [0.2, 0.4, 0.3, 0.5],
                 'actualValues': [1.5, 3,5, 5,5, 7.6],
               }


def infer(self, patternNZ, actValueList):

<EOL>if self.steps[<NUM_LIT:0>] == <NUM_LIT:0> or actValueList is None:<EOL><INDENT>defaultValue = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>defaultValue = actValueList[<NUM_LIT:0>]<EOL><DEDENT>actValues = [x if x is not None else defaultValue<EOL>for x in self._actualValues]<EOL>retval = {"<STR_LIT>": actValues}<EOL>for nSteps in self.steps:<EOL><INDENT>predictDist = self.inferSingleStep(patternNZ, self._weightMatrix[nSteps])<EOL>retval[nSteps] = predictDist<EOL><DEDENT>return retval<EOL>

Return the inference value from one input sample. The actual
learning happens in compute().

:param patternNZ: list of the active indices from the output below
:param classification: dict of the classification information:
                bucketIdx: index of the encoder bucket
                actValue:  actual value going into the encoder

:return:    dict containing inference results, one entry for each step in
            self.steps. The key is the number of steps, the value is an
            array containing the relative likelihood for each bucketIdx
            starting from bucketIdx 0.

            for example:

            .. code-block:: python

               {'actualValues': [0.0, 1.0, 2.0, 3.0]
                 1 : [0.1, 0.3, 0.2, 0.7]
                 4 : [0.2, 0.4, 0.3, 0.5]}


def inferSingleStep(self, patternNZ, weightMatrix):

outputActivation = weightMatrix[patternNZ].sum(axis=<NUM_LIT:0>)<EOL>outputActivation = outputActivation - numpy.max(outputActivation)<EOL>expOutputActivation = numpy.exp(outputActivation)<EOL>predictDist = expOutputActivation / numpy.sum(expOutputActivation)<EOL>return predictDist<EOL>

Perform inference for a single step. Given an SDR input and a weight
matrix, return a predicted distribution.

:param patternNZ: list of the active indices from the output below
:param weightMatrix: numpy array of the weight matrix
:return: numpy array of the predicted class label distribution


def _calculateError(self, recordNum, bucketIdxList):

error = dict()<EOL>targetDist = numpy.zeros(self._maxBucketIdx + <NUM_LIT:1>)<EOL>numCategories = len(bucketIdxList)<EOL>for bucketIdx in bucketIdxList:<EOL><INDENT>targetDist[bucketIdx] = <NUM_LIT:1.0>/numCategories<EOL><DEDENT>for (learnRecordNum, learnPatternNZ) in self._patternNZHistory:<EOL><INDENT>nSteps = recordNum - learnRecordNum<EOL>if nSteps in self.steps:<EOL><INDENT>predictDist = self.inferSingleStep(learnPatternNZ,<EOL>self._weightMatrix[nSteps])<EOL>error[nSteps] = targetDist - predictDist<EOL><DEDENT><DEDENT>return error<EOL>

Calculate error signal

:param bucketIdxList: list of encoder buckets

:return: dict containing error. The key is the number of steps
         The value is a numpy array of error at the output layer


return super(_SparseMatrixCorticalColumnAdapter, self).getRow(columnIndex)<EOL>

Wraps getRow() such that instances may be indexed by columnIndex.


return super(_SparseMatrixCorticalColumnAdapter, self).replaceSparseRow(<EOL>columnIndex, bitmap<EOL>)<EOL>


return super(_SparseMatrixCorticalColumnAdapter, self).setRowFromDense(<EOL>columnIndex, vector<EOL>)<EOL>


return self._columnDimensions<EOL>

:returns: (iter) the dimensions of the columns in the region


return self._inputDimensions<EOL>

:returns: (iter) the dimensions of the input vector


return self._numColumns<EOL>

:returns: (int) the total number of columns


return self._numInputs<EOL>

:returns: (int) the total number of inputs.


return self._potentialRadius<EOL>


self._potentialRadius = potentialRadius<EOL>

:param potentialRadius: (float) value to set


return self._potentialPct<EOL>


self._potentialPct = potentialPct<EOL>

:param potentialPct: (float) value to set


return self._globalInhibition<EOL>

:returns: (bool) whether global inhibition is enabled.


self._globalInhibition = globalInhibition<EOL>

:param globalInhibition: (bool) value to set.


return self._numActiveColumnsPerInhArea<EOL>

:returns: (float) the number of active columns per inhibition area. Returns
          a value less than 0 if parameter is unused.


assert(numActiveColumnsPerInhArea > <NUM_LIT:0>)<EOL>self._numActiveColumnsPerInhArea = numActiveColumnsPerInhArea<EOL>self._localAreaDensity = <NUM_LIT:0><EOL>

Sets the number of active columns per inhibition area. Invalidates the
``localAreaDensity`` parameter

:param numActiveColumnsPerInhArea: (float) value to set


return self._localAreaDensity<EOL>

:returns: (float) the local area density. Returns a value less than 0 if
          parameter is unused.


assert(localAreaDensity > <NUM_LIT:0> and localAreaDensity <= <NUM_LIT:1>)<EOL>self._localAreaDensity = localAreaDensity<EOL>self._numActiveColumnsPerInhArea = <NUM_LIT:0><EOL>

Sets the local area density. Invalidates the 'numActiveColumnsPerInhArea'
parameter

:param localAreaDensity: (float) value to set


return self._stimulusThreshold<EOL>


self._stimulusThreshold = stimulusThreshold<EOL>

:param stimulusThreshold: (float) value to set.


return self._inhibitionRadius<EOL>


self._inhibitionRadius = inhibitionRadius<EOL>

:param inhibitionRadius: (int) value to set


return self._dutyCyclePeriod<EOL>


self._dutyCyclePeriod = dutyCyclePeriod<EOL>

:param dutyCyclePeriod: (int) value to set.


return self._boostStrength<EOL>

:returns: (float) the maximum boost value used.


self._boostStrength = boostStrength<EOL>

Sets the maximum boost value.
:param boostStrength: (float) value to set


return self._iterationNum<EOL>


self._iterationNum = iterationNum<EOL>


return self._iterationLearnNum<EOL>

:returns: (int) The number of iterations that have been learned.


self._iterationLearnNum = iterationLearnNum<EOL>

:param iterationLearnNum: (int) value to set


return self._spVerbosity<EOL>

:returns: (int) the verbosity level, larger is more verbose.


self._spVerbosity = spVerbosity<EOL>

:param spVerbosity: (int) value to set, larger is more verbose.


return self._updatePeriod<EOL>

:returns: (int) The period at which active duty cycles are updated.


self._updatePeriod = updatePeriod<EOL>

:param updatePeriod: (int) The period at which active duty cycles are
       updated.


return self._synPermTrimThreshold<EOL>

Sparsity is enforced by trimming out all permanence values below this value.

:returns: (float) the permanence trim threshold


self._synPermTrimThreshold = synPermTrimThreshold<EOL>

Sparsity is enforced by trimming out all permanence values below this value.

:param synPermTrimThreshold: (float) the permanence trim threshold


return self._synPermActiveInc<EOL>

:returns: (float) the permanence increment amount for active synapses inputs


self._synPermActiveInc = synPermActiveInc<EOL>

Sets the permanence increment amount for active synapses.

:param synPermActiveInc: (float) value to set.


return self._synPermInactiveDec<EOL>

:returns: (float) the permanence decrement amount for inactive synapses.


self._synPermInactiveDec = synPermInactiveDec<EOL>

Sets the permanence decrement amount for inactive synapses.

:param synPermInactiveDec: (float) value to set.


return self._synPermBelowStimulusInc<EOL>

:returns: (float) the permanence increment amount for columns that have not
          been recently active.


self._synPermBelowStimulusInc = synPermBelowStimulusInc<EOL>

Sets the permanence increment amount for columns that have not been
recently active.

:param synPermBelowStimulusInc: (float) value to set.


return self._synPermConnected<EOL>

:returns: (float) the permanence amount that qualifies a synapse as being
          connected.


self._synPermConnected = synPermConnected<EOL>

Sets the permanence amount that qualifies a synapse as being
connected.

:param synPermConnected: (float) value to set.


return self._minPctOverlapDutyCycles<EOL>

:returns: (float) the minimum tolerated overlaps, given as percent of
          neighbors overlap score


self._minPctOverlapDutyCycles = minPctOverlapDutyCycles<EOL>

Sets the minimum tolerated activity duty cycle, given as percent of
neighbors' activity duty cycle.

:param minPctOverlapDutyCycles: (float) value to set.


boostFactors[:] = self._boostFactors[:]<EOL>

Gets the boost factors for all columns. Input list will be overwritten.

:param boostFactors: (list) size must match number of columns.


self._boostFactors[:] = boostFactors[:]<EOL>

Sets the boost factors for all columns. ``boostFactors`` size must match
the number of columns.

:param boostFactors: (iter) value to set.


overlapDutyCycles[:] = self._overlapDutyCycles[:]<EOL>

Gets the overlap duty cycles for all columns. ``overlapDutyCycles``
size must match the number of columns.

:param overlapDutyCycles: (list) will be overwritten.


self._overlapDutyCycles[:] = overlapDutyCycles<EOL>

Sets the overlap duty cycles for all columns. ``overlapDutyCycles``
size must match the number of columns.

:param overlapDutyCycles: (list) value to set.


activeDutyCycles[:] = self._activeDutyCycles[:]<EOL>

Gets the activity duty cycles for all columns. Input list will be
overwritten.

:param activeDutyCycles: (list) size must match number of columns.


self._activeDutyCycles[:] = activeDutyCycles<EOL>

Sets the activity duty cycles for all columns. ``activeDutyCycles`` size
must match the number of columns.

:param activeDutyCycles: (list) value to set.


minOverlapDutyCycles[:] = self._minOverlapDutyCycles[:]<EOL>

:returns: (list) the minimum overlap duty cycles for all columns.
          ``minOverlapDutyCycles`` size must match the number of columns.


self._minOverlapDutyCycles[:] = minOverlapDutyCycles[:]<EOL>

Sets the minimum overlap duty cycles for all columns.
``minOverlapDutyCycles`` size must match the number of columns.

:param minOverlapDutyCycles: (iter) value to set.


def getPotential(self, columnIndex, potential):

assert(columnIndex < self._numColumns)<EOL>potential[:] = self._potentialPools[columnIndex]<EOL>

:param columnIndex: (int) column index to get potential for.
:param potential: (list) will be overwritten with column potentials. Must
       match the number of inputs.


def setPotential(self, columnIndex, potential):

assert(columnIndex < self._numColumns)<EOL>potentialSparse = numpy.where(potential > <NUM_LIT:0>)[<NUM_LIT:0>]<EOL>if len(potentialSparse) < self._stimulusThreshold:<EOL><INDENT>raise Exception("<STR_LIT>" +<EOL>"<STR_LIT>" +<EOL>"<STR_LIT>")<EOL><DEDENT>self._potentialPools.replace(columnIndex, potentialSparse)<EOL>

Sets the potential mapping for a given column. ``potential`` size must match
the number of inputs, and must be greater than ``stimulusThreshold``.

:param columnIndex: (int) column index to set potential for.
:param potential: (list) value to set.


def getPermanence(self, columnIndex, permanence):

assert(columnIndex < self._numColumns)<EOL>permanence[:] = self._permanences[columnIndex]<EOL>

Returns the permanence values for a given column. ``permanence`` size
must match the number of inputs.

:param columnIndex: (int) column index to get permanence for.
:param permanence: (list) will be overwritten with permanences.


def setPermanence(self, columnIndex, permanence):

assert(columnIndex < self._numColumns)<EOL>self._updatePermanencesForColumn(permanence, columnIndex, raisePerm=False)<EOL>

Sets the permanence values for a given column. ``permanence`` size must
match the number of inputs.

:param columnIndex: (int) column index to set permanence for.
:param permanence: (list) value to set.


def getConnectedSynapses(self, columnIndex, connectedSynapses):

assert(columnIndex < self._numColumns)<EOL>connectedSynapses[:] = self._connectedSynapses[columnIndex]<EOL>

:param connectedSynapses: (list) will be overwritten
:returns: (iter) the connected synapses for a given column.
          ``connectedSynapses`` size must match the number of inputs


connectedCounts[:] = self._connectedCounts[:]<EOL>

:param connectedCounts: (list) will be overwritten
:returns: (int) the number of connected synapses for all columns.
          ``connectedCounts`` size must match the number of columns.


return self._overlaps<EOL>

:returns: (iter) the overlap score for each column.


return self._boostedOverlaps<EOL>

:returns: (list) the boosted overlap score for each column.


def compute(self, inputVector, learn, activeArray):

if not isinstance(inputVector, numpy.ndarray):<EOL><INDENT>raise TypeError("<STR_LIT>" %<EOL>str(type(inputVector)))<EOL><DEDENT>if inputVector.size != self._numInputs:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>" % (<EOL>inputVector.size, self._numInputs))<EOL><DEDENT>self._updateBookeepingVars(learn)<EOL>inputVector = numpy.array(inputVector, dtype=realDType)<EOL>inputVector.reshape(-<NUM_LIT:1>)<EOL>self._overlaps = self._calculateOverlap(inputVector)<EOL>if learn:<EOL><INDENT>self._boostedOverlaps = self._boostFactors * self._overlaps<EOL><DEDENT>else:<EOL><INDENT>self._boostedOverlaps = self._overlaps<EOL><DEDENT>activeColumns = self._inhibitColumns(self._boostedOverlaps)<EOL>if learn:<EOL><INDENT>self._adaptSynapses(inputVector, activeColumns)<EOL>self._updateDutyCycles(self._overlaps, activeColumns)<EOL>self._bumpUpWeakColumns()<EOL>self._updateBoostFactors()<EOL>if self._isUpdateRound():<EOL><INDENT>self._updateInhibitionRadius()<EOL>self._updateMinDutyCycles()<EOL><DEDENT><DEDENT>activeArray.fill(<NUM_LIT:0>)<EOL>activeArray[activeColumns] = <NUM_LIT:1><EOL>

This is the primary public method of the SpatialPooler class. This
function takes a input vector and outputs the indices of the active columns.
If 'learn' is set to True, this method also updates the permanences of the
columns.

:param inputVector: A numpy array of 0's and 1's that comprises the input
    to the spatial pooler. The array will be treated as a one dimensional
    array, therefore the dimensions of the array do not have to match the
    exact dimensions specified in the class constructor. In fact, even a
    list would suffice. The number of input bits in the vector must,
    however, match the number of bits specified by the call to the
    constructor. Therefore there must be a '0' or '1' in the array for
    every input bit.
:param learn: A boolean value indicating whether learning should be
    performed. Learning entails updating the  permanence values of the
    synapses, and hence modifying the 'state' of the model. Setting
    learning to 'off' freezes the SP and has many uses. For example, you
    might want to feed in various inputs and examine the resulting SDR's.
:param activeArray: An array whose size is equal to the number of columns.
    Before the function returns this array will be populated with 1's at
    the indices of the active columns, and 0's everywhere else.


neverLearned = numpy.where(self._activeDutyCycles == <NUM_LIT:0>)[<NUM_LIT:0>]<EOL>activeArray[neverLearned] = <NUM_LIT:0><EOL>

Removes the set of columns who have never been active from the set of
active columns selected in the inhibition round. Such columns cannot
represent learned pattern and are therefore meaningless if only inference
is required. This should not be done when using a random, unlearned SP
since you would end up with no active columns.

:param activeArray: An array whose size is equal to the number of columns.
    Any columns marked as active with an activeDutyCycle of 0 have
    never been activated before and therefore are not active due to
    learning. Any of these (unlearned) columns will be disabled (set to 0).


if self._globalInhibition or self._inhibitionRadius > self._numInputs:<EOL><INDENT>self._updateMinDutyCyclesGlobal()<EOL><DEDENT>else:<EOL><INDENT>self._updateMinDutyCyclesLocal()<EOL><DEDENT>

Updates the minimum duty cycles defining normal activity for a column. A
column with activity duty cycle below this minimum threshold is boosted.


self._minOverlapDutyCycles.fill(<EOL>self._minPctOverlapDutyCycles * self._overlapDutyCycles.max()<EOL>)<EOL>

Updates the minimum duty cycles in a global fashion. Sets the minimum duty
cycles for the overlap all columns to be a percent of the maximum in the
region, specified by minPctOverlapDutyCycle. Functionality it is equivalent
to _updateMinDutyCyclesLocal, but this function exploits the globality of
the computation to perform it in a straightforward, and efficient manner.


for column in range(self._numColumns):<EOL><INDENT>neighborhood = self._getColumnNeighborhood(column)<EOL>maxActiveDuty = self._activeDutyCycles[neighborhood].max()<EOL>maxOverlapDuty = self._overlapDutyCycles[neighborhood].max()<EOL>self._minOverlapDutyCycles[column] = (maxOverlapDuty *<EOL>self._minPctOverlapDutyCycles)<EOL><DEDENT>

Updates the minimum duty cycles. The minimum duty cycles are determined
locally. Each column's minimum duty cycles are set to be a percent of the
maximum duty cycles in the column's neighborhood. Unlike
_updateMinDutyCyclesGlobal, here the values can be quite different for
different columns.


def _updateDutyCycles(self, overlaps, activeColumns):

overlapArray = numpy.zeros(self._numColumns, dtype=realDType)<EOL>activeArray = numpy.zeros(self._numColumns, dtype=realDType)<EOL>overlapArray[overlaps > <NUM_LIT:0>] = <NUM_LIT:1><EOL>activeArray[activeColumns] = <NUM_LIT:1><EOL>period = self._dutyCyclePeriod<EOL>if (period > self._iterationNum):<EOL><INDENT>period = self._iterationNum<EOL><DEDENT>self._overlapDutyCycles = self._updateDutyCyclesHelper(<EOL>self._overlapDutyCycles,<EOL>overlapArray,<EOL>period<EOL>)<EOL>self._activeDutyCycles = self._updateDutyCyclesHelper(<EOL>self._activeDutyCycles,<EOL>activeArray,<EOL>period<EOL>)<EOL>

Updates the duty cycles for each column. The OVERLAP duty cycle is a moving
average of the number of inputs which overlapped with the each column. The
ACTIVITY duty cycles is a moving average of the frequency of activation for
each column.

Parameters:
----------------------------
:param overlaps:
                An array containing the overlap score for each column.
                The overlap score for a column is defined as the number
                of synapses in a "connected state" (connected synapses)
                that are connected to input bits which are turned on.
:param activeColumns:
                An array containing the indices of the active columns,
                the sparse set of columns which survived inhibition


if self._globalInhibition:<EOL><INDENT>self._inhibitionRadius = int(self._columnDimensions.max())<EOL>return<EOL><DEDENT>avgConnectedSpan = numpy.average(<EOL>[self._avgConnectedSpanForColumnND(i)<EOL>for i in range(self._numColumns)]<EOL>)<EOL>columnsPerInput = self._avgColumnsPerInput()<EOL>diameter = avgConnectedSpan * columnsPerInput<EOL>radius = (diameter - <NUM_LIT:1>) / <NUM_LIT><EOL>radius = max(<NUM_LIT:1.0>, radius)<EOL>self._inhibitionRadius = int(radius + <NUM_LIT:0.5>)<EOL>

Update the inhibition radius. The inhibition radius is a measure of the
square (or hypersquare) of columns that each a column is "connected to"
on average. Since columns are are not connected to each other directly, we
determine this quantity by first figuring out how many *inputs* a column is
connected to, and then multiplying it by the total number of columns that
exist for each input. For multiple dimension the aforementioned
calculations are averaged over all dimensions of inputs and columns. This
value is meaningless if global inhibition is enabled.


<EOL>numDim = max(self._columnDimensions.size, self._inputDimensions.size)<EOL>colDim = numpy.ones(numDim)<EOL>colDim[:self._columnDimensions.size] = self._columnDimensions<EOL>inputDim = numpy.ones(numDim)<EOL>inputDim[:self._inputDimensions.size] = self._inputDimensions<EOL>columnsPerInput = colDim.astype(realDType) / inputDim<EOL>return numpy.average(columnsPerInput)<EOL>

The average number of columns per input, taking into account the topology
of the inputs and columns. This value is used to calculate the inhibition
radius. This function supports an arbitrary number of dimensions. If the
number of column dimensions does not match the number of input dimensions,
we treat the missing, or phantom dimensions as 'ones'.


assert(self._inputDimensions.size == <NUM_LIT:1>)<EOL>connected = self._connectedSynapses[columnIndex].nonzero()[<NUM_LIT:0>]<EOL>if connected.size == <NUM_LIT:0>:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>return max(connected) - min(connected) + <NUM_LIT:1><EOL><DEDENT>

The range of connected synapses for column. This is used to
calculate the inhibition radius. This variation of the function only
supports a 1 dimensional column topology.

Parameters:
----------------------------
:param columnIndex:   The index identifying a column in the permanence,
                      potential and connectivity matrices


assert(self._inputDimensions.size == <NUM_LIT:2>)<EOL>connected = self._connectedSynapses[columnIndex]<EOL>(rows, cols) = connected.reshape(self._inputDimensions).nonzero()<EOL>if  rows.size == <NUM_LIT:0> and cols.size == <NUM_LIT:0>:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>rowSpan = rows.max() - rows.min() + <NUM_LIT:1><EOL>colSpan = cols.max() - cols.min() + <NUM_LIT:1><EOL>return numpy.average([rowSpan, colSpan])<EOL>

The range of connectedSynapses per column, averaged for each dimension.
This value is used to calculate the inhibition radius. This variation of
the  function only supports a 2 dimensional column topology.

Parameters:
----------------------------
:param columnIndex:   The index identifying a column in the permanence,
                      potential and connectivity matrices


dimensions = self._inputDimensions<EOL>connected = self._connectedSynapses[columnIndex].nonzero()[<NUM_LIT:0>]<EOL>if connected.size == <NUM_LIT:0>:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>maxCoord = numpy.empty(self._inputDimensions.size)<EOL>minCoord = numpy.empty(self._inputDimensions.size)<EOL>maxCoord.fill(-<NUM_LIT:1>)<EOL>minCoord.fill(max(self._inputDimensions))<EOL>for i in connected:<EOL><INDENT>maxCoord = numpy.maximum(maxCoord, numpy.unravel_index(i, dimensions))<EOL>minCoord = numpy.minimum(minCoord, numpy.unravel_index(i, dimensions))<EOL><DEDENT>return numpy.average(maxCoord - minCoord + <NUM_LIT:1>)<EOL>

The range of connectedSynapses per column, averaged for each dimension.
This value is used to calculate the inhibition radius. This variation of
the function supports arbitrary column dimensions.

Parameters:
----------------------------
:param index:   The index identifying a column in the permanence, potential
                and connectivity matrices.


def _adaptSynapses(self, inputVector, activeColumns):

inputIndices = numpy.where(inputVector > <NUM_LIT:0>)[<NUM_LIT:0>]<EOL>permChanges = numpy.zeros(self._numInputs, dtype=realDType)<EOL>permChanges.fill(-<NUM_LIT:1> * self._synPermInactiveDec)<EOL>permChanges[inputIndices] = self._synPermActiveInc<EOL>for columnIndex in activeColumns:<EOL><INDENT>perm = self._permanences[columnIndex]<EOL>maskPotential = numpy.where(self._potentialPools[columnIndex] > <NUM_LIT:0>)[<NUM_LIT:0>]<EOL>perm[maskPotential] += permChanges[maskPotential]<EOL>self._updatePermanencesForColumn(perm, columnIndex, raisePerm=True)<EOL><DEDENT>

The primary method in charge of learning. Adapts the permanence values of
the synapses based on the input vector, and the chosen columns after
inhibition round. Permanence values are increased for synapses connected to
input bits that are turned on, and decreased for synapses connected to
inputs bits that are turned off.

Parameters:
----------------------------
:param inputVector:
                A numpy array of 0's and 1's that comprises the input to
                the spatial pooler. There exists an entry in the array
                for every input bit.
:param activeColumns:
                An array containing the indices of the columns that
                survived inhibition.


weakColumns = numpy.where(self._overlapDutyCycles<EOL>< self._minOverlapDutyCycles)[<NUM_LIT:0>]<EOL>for columnIndex in weakColumns:<EOL><INDENT>perm = self._permanences[columnIndex].astype(realDType)<EOL>maskPotential = numpy.where(self._potentialPools[columnIndex] > <NUM_LIT:0>)[<NUM_LIT:0>]<EOL>perm[maskPotential] += self._synPermBelowStimulusInc<EOL>self._updatePermanencesForColumn(perm, columnIndex, raisePerm=False)<EOL><DEDENT>

This method increases the permanence values of synapses of columns whose
activity level has been too low. Such columns are identified by having an
overlap duty cycle that drops too much below those of their peers. The
permanence values for such columns are increased.


def _raisePermanenceToThreshold(self, perm, mask):

if len(mask) < self._stimulusThreshold:<EOL><INDENT>raise Exception("<STR_LIT>" +<EOL>"<STR_LIT>" +<EOL>"<STR_LIT>")<EOL><DEDENT>numpy.clip(perm, self._synPermMin, self._synPermMax, out=perm)<EOL>while True:<EOL><INDENT>numConnected = numpy.nonzero(<EOL>perm > self._synPermConnected - PERMANENCE_EPSILON)[<NUM_LIT:0>].size<EOL>if numConnected >= self._stimulusThreshold:<EOL><INDENT>return<EOL><DEDENT>perm[mask] += self._synPermBelowStimulusInc<EOL><DEDENT>

This method ensures that each column has enough connections to input bits
to allow it to become active. Since a column must have at least
'self._stimulusThreshold' overlaps in order to be considered during the
inhibition phase, columns without such minimal number of connections, even
if all the input bits they are connected to turn on, have no chance of
obtaining the minimum threshold. For such columns, the permanence values
are increased until the minimum number of connections are formed.

Parameters:
----------------------------
:param perm:    An array of permanence values for a column. The array is
                "dense", i.e. it contains an entry for each input bit, even
                if the permanence value is 0.
:param mask:    the indices of the columns whose permanences need to be
                raised.


def _updatePermanencesForColumn(self, perm, columnIndex, raisePerm=True):

maskPotential = numpy.where(self._potentialPools[columnIndex] > <NUM_LIT:0>)[<NUM_LIT:0>]<EOL>if raisePerm:<EOL><INDENT>self._raisePermanenceToThreshold(perm, maskPotential)<EOL><DEDENT>perm[perm < self._synPermTrimThreshold] = <NUM_LIT:0><EOL>numpy.clip(perm, self._synPermMin, self._synPermMax, out=perm)<EOL>newConnected = numpy.where(perm >=<EOL>self._synPermConnected - PERMANENCE_EPSILON)[<NUM_LIT:0>]<EOL>self._permanences.update(columnIndex, perm)<EOL>self._connectedSynapses.replace(columnIndex, newConnected)<EOL>self._connectedCounts[columnIndex] = newConnected.size<EOL>

This method updates the permanence matrix with a column's new permanence
values. The column is identified by its index, which reflects the row in
the matrix, and the permanence is given in 'dense' form, i.e. a full
array containing all the zeros as well as the non-zero values. It is in
charge of implementing 'clipping' - ensuring that the permanence values are
always between 0 and 1 - and 'trimming' - enforcing sparsity by zeroing out
all permanence values below '_synPermTrimThreshold'. It also maintains
the consistency between 'self._permanences' (the matrix storing the
permanence values), 'self._connectedSynapses', (the matrix storing the bits
each column is connected to), and 'self._connectedCounts' (an array storing
the number of input bits each column is connected to). Every method wishing
to modify the permanence matrix should do so through this method.

Parameters:
----------------------------
:param perm:    An array of permanence values for a column. The array is
                "dense", i.e. it contains an entry for each input bit, even
                if the permanence value is 0.
:param index:   The index identifying a column in the permanence, potential
                and connectivity matrices
:param raisePerm: A boolean value indicating whether the permanence values
                should be raised until a minimum number are synapses are in
                a connected state. Should be set to 'false' when a direct
                assignment is required.


p = self._synPermConnected + (<EOL>self._synPermMax - self._synPermConnected)*self._random.getReal64()<EOL>p = int(p*<NUM_LIT>) / <NUM_LIT><EOL>return p<EOL>

Returns a randomly generated permanence value for a synapses that is
initialized in a connected state. The basic idea here is to initialize
permanence values very close to synPermConnected so that a small number of
learning steps could make it disconnected or connected.

Note: experimentation was done a long time ago on the best way to initialize
permanence values, but the history for this particular scheme has been lost.


p = self._synPermConnected * self._random.getReal64()<EOL>p = int(p*<NUM_LIT>) / <NUM_LIT><EOL>return p<EOL>

Returns a randomly generated permanence value for a synapses that is to be
initialized in a non-connected state.


def _initPermanence(self, potential, connectedPct):

<EOL>perm = numpy.zeros(self._numInputs, dtype=realDType)<EOL>for i in range(self._numInputs):<EOL><INDENT>if (potential[i] < <NUM_LIT:1>):<EOL><INDENT>continue<EOL><DEDENT>if (self._random.getReal64() <= connectedPct):<EOL><INDENT>perm[i] = self._initPermConnected()<EOL><DEDENT>else:<EOL><INDENT>perm[i] = self._initPermNonConnected()<EOL><DEDENT><DEDENT>perm[perm < self._synPermTrimThreshold] = <NUM_LIT:0><EOL>return perm<EOL>

Initializes the permanences of a column. The method
returns a 1-D array the size of the input, where each entry in the
array represents the initial permanence value between the input bit
at the particular index in the array, and the column represented by
the 'index' parameter.

Parameters:
----------------------------
:param potential: A numpy array specifying the potential pool of the column.
                Permanence values will only be generated for input bits
                corresponding to indices for which the mask value is 1.
:param connectedPct: A value between 0 or 1 governing the chance, for each
                     permanence, that the initial permanence value will
                     be a value that is considered connected.


columnCoords = numpy.unravel_index(index, self._columnDimensions)<EOL>columnCoords = numpy.array(columnCoords, dtype=realDType)<EOL>ratios = columnCoords / self._columnDimensions<EOL>inputCoords = self._inputDimensions * ratios<EOL>inputCoords += <NUM_LIT:0.5> * self._inputDimensions / self._columnDimensions<EOL>inputCoords = inputCoords.astype(int)<EOL>inputIndex = numpy.ravel_multi_index(inputCoords, self._inputDimensions)<EOL>return inputIndex<EOL>

Maps a column to its respective input index, keeping to the topology of
the region. It takes the index of the column as an argument and determines
what is the index of the flattened input vector that is to be the center of
the column's potential pool. It distributes the columns over the inputs
uniformly. The return value is an integer representing the index of the
input bit. Examples of the expected output of this method:
* If the topology is one dimensional, and the column index is 0, this
  method will return the input index 0. If the column index is 1, and there
  are 3 columns over 7 inputs, this method will return the input index 3.
* If the topology is two dimensional, with column dimensions [3, 5] and
  input dimensions [7, 11], and the column index is 3, the method
  returns input index 8.

Parameters:
----------------------------
:param index:   The index identifying a column in the permanence, potential
                and connectivity matrices.
:param wrapAround: A boolean value indicating that boundaries should be
                ignored.


centerInput = self._mapColumn(index)<EOL>columnInputs = self._getInputNeighborhood(centerInput).astype(uintType)<EOL>numPotential = int(columnInputs.size * self._potentialPct + <NUM_LIT:0.5>)<EOL>selectedInputs = numpy.empty(numPotential, dtype=uintType)<EOL>self._random.sample(columnInputs, selectedInputs)<EOL>potential = numpy.zeros(self._numInputs, dtype=uintType)<EOL>potential[selectedInputs] = <NUM_LIT:1><EOL>return potential<EOL>

Maps a column to its input bits. This method encapsulates the topology of
the region. It takes the index of the column as an argument and determines
what are the indices of the input vector that are located within the
column's potential pool. The return value is a list containing the indices
of the input bits. The current implementation of the base class only
supports a 1 dimensional topology of columns with a 1 dimensional topology
of inputs. To extend this class to support 2-D topology you will need to
override this method. Examples of the expected output of this method:
* If the potentialRadius is greater than or equal to the largest input
  dimension then each column connects to all of the inputs.
* If the topology is one dimensional, the input space is divided up evenly
  among the columns and each column is centered over its share of the
  inputs.  If the potentialRadius is 5, then each column connects to the
  input it is centered above as well as the 5 inputs to the left of that
  input and the five inputs to the right of that input, wrapping around if
  wrapAround=True.
* If the topology is two dimensional, the input space is again divided up
  evenly among the columns and each column is centered above its share of
  the inputs.  If the potentialRadius is 5, the column connects to a square
  that has 11 inputs on a side and is centered on the input that the column
  is centered above.

Parameters:
----------------------------
:param index:   The index identifying a column in the permanence, potential
                and connectivity matrices.


@staticmethod<EOL><INDENT>def _updateDutyCyclesHelper(dutyCycles, newInput, period):<DEDENT>

assert(period >= <NUM_LIT:1>)<EOL>return (dutyCycles * (period -<NUM_LIT:1.0>) + newInput) / period<EOL>

Updates a duty cycle estimate with a new value. This is a helper
function that is used to update several duty cycle variables in
the Column class, such as: overlapDutyCucle, activeDutyCycle,
minPctDutyCycleBeforeInh, minPctDutyCycleAfterInh, etc. returns
the updated duty cycle. Duty cycles are updated according to the following
formula:

              (period - 1)*dutyCycle + newValue
  dutyCycle := ----------------------------------
                          period

Parameters:
----------------------------
:param dutyCycles: An array containing one or more duty cycle values that need
                to be updated
:param newInput: A new numerical value used to update the duty cycle
:param period:  The period of the duty cycle


if self._globalInhibition:<EOL><INDENT>self._updateBoostFactorsGlobal()<EOL><DEDENT>else:<EOL><INDENT>self._updateBoostFactorsLocal()<EOL><DEDENT>

Update the boost factors for all columns. The boost factors are used to
increase the overlap of inactive columns to improve their chances of
becoming active, and hence encourage participation of more columns in the
learning process. The boosting function is a curve defined as:
boostFactors = exp[ - boostStrength * (dutyCycle - targetDensity)]
Intuitively this means that columns that have been active at the target
activation level have a boost factor of 1, meaning their overlap is not
boosted. Columns whose active duty cycle drops too much below that of their
neighbors are boosted depending on how infrequently they have been active.
Columns that has been active more than the target activation level have
a boost factor below 1, meaning their overlap is suppressed

The boostFactor depends on the activeDutyCycle via an exponential function:

        boostFactor
            ^
            |
            |\
            | \
      1  _  |  \
            |    _
            |      _ _
            |          _ _ _ _
            +--------------------> activeDutyCycle
               |
          targetDensity


<EOL>if (self._localAreaDensity > <NUM_LIT:0>):<EOL><INDENT>targetDensity = self._localAreaDensity<EOL><DEDENT>else:<EOL><INDENT>inhibitionArea = ((<NUM_LIT:2> * self._inhibitionRadius + <NUM_LIT:1>)<EOL>** self._columnDimensions.size)<EOL>inhibitionArea = min(self._numColumns, inhibitionArea)<EOL>targetDensity = float(self._numActiveColumnsPerInhArea) / inhibitionArea<EOL>targetDensity = min(targetDensity, <NUM_LIT:0.5>)<EOL><DEDENT>self._boostFactors = numpy.exp(<EOL>(targetDensity - self._activeDutyCycles) * self._boostStrength)<EOL>

Update boost factors when global inhibition is used


<EOL>targetDensity = numpy.zeros(self._numColumns, dtype=realDType)<EOL>for i in range(self._numColumns):<EOL><INDENT>maskNeighbors = self._getColumnNeighborhood(i)<EOL>targetDensity[i] = numpy.mean(self._activeDutyCycles[maskNeighbors])<EOL><DEDENT>self._boostFactors = numpy.exp(<EOL>(targetDensity - self._activeDutyCycles) * self._boostStrength)<EOL>

Update boost factors when local inhibition is used


self._iterationNum += <NUM_LIT:1><EOL>if learn:<EOL><INDENT>self._iterationLearnNum += <NUM_LIT:1><EOL><DEDENT>

Updates counter instance variables each round.

Parameters:
----------------------------
:param learn:   a boolean value indicating whether learning should be
                performed. Learning entails updating the  permanence
                values of the synapses, and hence modifying the 'state'
                of the model. setting learning to 'off' might be useful
                for indicating separate training vs. testing sets.


overlaps = numpy.zeros(self._numColumns, dtype=realDType)<EOL>self._connectedSynapses.rightVecSumAtNZ_fast(inputVector.astype(realDType),<EOL>overlaps)<EOL>return overlaps<EOL>

This function determines each column's overlap with the current input
vector. The overlap of a column is the number of synapses for that column
that are connected (permanence value is greater than '_synPermConnected')
to input bits which are turned on. The implementation takes advantage of
the SparseBinaryMatrix class to perform this calculation efficiently.

Parameters:
----------------------------
:param inputVector: a numpy array of 0's and 1's that comprises the input to
                the spatial pooler.


<EOL>if (self._localAreaDensity > <NUM_LIT:0>):<EOL><INDENT>density = self._localAreaDensity<EOL><DEDENT>else:<EOL><INDENT>inhibitionArea = ((<NUM_LIT:2>*self._inhibitionRadius + <NUM_LIT:1>)<EOL>** self._columnDimensions.size)<EOL>inhibitionArea = min(self._numColumns, inhibitionArea)<EOL>density = float(self._numActiveColumnsPerInhArea) / inhibitionArea<EOL>density = min(density, <NUM_LIT:0.5>)<EOL><DEDENT>if self._globalInhibition orself._inhibitionRadius > max(self._columnDimensions):<EOL><INDENT>return self._inhibitColumnsGlobal(overlaps, density)<EOL><DEDENT>else:<EOL><INDENT>return self._inhibitColumnsLocal(overlaps, density)<EOL><DEDENT>

Performs inhibition. This method calculates the necessary values needed to
actually perform inhibition and then delegates the task of picking the
active columns to helper functions.

Parameters:
----------------------------
:param overlaps: an array containing the overlap score for each  column.
                The overlap score for a column is defined as the number
                of synapses in a "connected state" (connected synapses)
                that are connected to input bits which are turned on.


def _inhibitColumnsGlobal(self, overlaps, density):

<EOL>numActive = int(density * self._numColumns)<EOL>sortedWinnerIndices = numpy.argsort(overlaps, kind='<STR_LIT>')<EOL>start = len(sortedWinnerIndices) - numActive<EOL>while start < len(sortedWinnerIndices):<EOL><INDENT>i = sortedWinnerIndices[start]<EOL>if overlaps[i] >= self._stimulusThreshold:<EOL><INDENT>break<EOL><DEDENT>else:<EOL><INDENT>start += <NUM_LIT:1><EOL><DEDENT><DEDENT>return sortedWinnerIndices[start:][::-<NUM_LIT:1>]<EOL>

Perform global inhibition. Performing global inhibition entails picking the
top 'numActive' columns with the highest overlap score in the entire
region. At most half of the columns in a local neighborhood are allowed to
be active. Columns with an overlap score below the 'stimulusThreshold' are
always inhibited.

:param overlaps: an array containing the overlap score for each  column.
                The overlap score for a column is defined as the number
                of synapses in a "connected state" (connected synapses)
                that are connected to input bits which are turned on.
:param density: The fraction of columns to survive inhibition.
@return list with indices of the winning columns


def _inhibitColumnsLocal(self, overlaps, density):

activeArray = numpy.zeros(self._numColumns, dtype="<STR_LIT:bool>")<EOL>for column, overlap in enumerate(overlaps):<EOL><INDENT>if overlap >= self._stimulusThreshold:<EOL><INDENT>neighborhood = self._getColumnNeighborhood(column)<EOL>neighborhoodOverlaps = overlaps[neighborhood]<EOL>numBigger = numpy.count_nonzero(neighborhoodOverlaps > overlap)<EOL>ties = numpy.where(neighborhoodOverlaps == overlap)<EOL>tiedNeighbors = neighborhood[ties]<EOL>numTiesLost = numpy.count_nonzero(activeArray[tiedNeighbors])<EOL>numActive = int(<NUM_LIT:0.5> + density * len(neighborhood))<EOL>if numBigger + numTiesLost < numActive:<EOL><INDENT>activeArray[column] = True<EOL><DEDENT><DEDENT><DEDENT>return activeArray.nonzero()[<NUM_LIT:0>]<EOL>

Performs local inhibition. Local inhibition is performed on a column by
column basis. Each column observes the overlaps of its neighbors and is
selected if its overlap score is within the top 'numActive' in its local
neighborhood. At most half of the columns in a local neighborhood are
allowed to be active. Columns with an overlap score below the
'stimulusThreshold' are always inhibited.

:param overlaps: an array containing the overlap score for each  column.
                The overlap score for a column is defined as the number
                of synapses in a "connected state" (connected synapses)
                that are connected to input bits which are turned on.
:param density: The fraction of columns to survive inhibition. This
                value is only an intended target. Since the surviving
                columns are picked in a local fashion, the exact fraction
                of surviving columns is likely to vary.
@return list with indices of the winning columns


return (self._iterationNum % self._updatePeriod) == <NUM_LIT:0><EOL>

returns true if enough rounds have passed to warrant updates of
duty cycles


Gets a neighborhood of columns.

Simply calls topology.neighborhood or topology.wrappingNeighborhood

A subclass can insert different topology behavior by overriding this method.

:param centerColumn (int)
The center of the neighborhood.

@returns (1D numpy array of integers)
The columns in the neighborhood.


Gets a neighborhood of inputs.

Simply calls topology.wrappingNeighborhood or topology.neighborhood.

A subclass can insert different topology behavior by overriding this method.

:param centerInput (int)
The center of the neighborhood.

@returns (1D numpy array of integers)
The inputs in the neighborhood.


if seed != -<NUM_LIT:1>:<EOL><INDENT>self._random = NupicRandom(seed)<EOL><DEDENT>else:<EOL><INDENT>self._random = NupicRandom()<EOL><DEDENT>


<EOL>if state['<STR_LIT>'] < <NUM_LIT:2>:<EOL><INDENT>state['<STR_LIT>'] = True<EOL><DEDENT>if state['<STR_LIT>'] < <NUM_LIT:3>:<EOL><INDENT>state['<STR_LIT>'] = numpy.zeros(self._numColumns, dtype=realDType)<EOL>state['<STR_LIT>'] = numpy.zeros(self._numColumns, dtype=realDType)<EOL><DEDENT>state['<STR_LIT>'] = VERSION<EOL>self.__dict__.update(state)<EOL>

Initialize class properties from stored values.


print("<STR_LIT>")<EOL>print("<STR_LIT>", self.getNumInputs())<EOL>print("<STR_LIT>", self.getNumColumns())<EOL>print("<STR_LIT>", self._columnDimensions)<EOL>print("<STR_LIT>", self.getNumActiveColumnsPerInhArea())<EOL>print("<STR_LIT>", self.getPotentialPct())<EOL>print("<STR_LIT>", self.getGlobalInhibition())<EOL>print("<STR_LIT>", self.getLocalAreaDensity())<EOL>print("<STR_LIT>", self.getStimulusThreshold())<EOL>print("<STR_LIT>", self.getSynPermActiveInc())<EOL>print("<STR_LIT>", self.getSynPermInactiveDec())<EOL>print("<STR_LIT>", self.getSynPermConnected())<EOL>print("<STR_LIT>", self.getMinPctOverlapDutyCycles())<EOL>print("<STR_LIT>", self.getDutyCyclePeriod())<EOL>print("<STR_LIT>", self.getBoostStrength())<EOL>print("<STR_LIT>", self.getSpVerbosity())<EOL>print("<STR_LIT>", self._version)<EOL>


import inspect<EOL>import copy<EOL>callingFrame = inspect.stack()[<NUM_LIT:1>][<NUM_LIT:0>]<EOL>argNames, _, _, frameLocalVarDict = inspect.getargvalues(callingFrame)<EOL>argNames.remove("<STR_LIT>")<EOL>args = copy.copy(frameLocalVarDict)<EOL>for varName in frameLocalVarDict:<EOL><INDENT>if varName not in argNames:<EOL><INDENT>args.pop(varName)<EOL><DEDENT><DEDENT>return args<EOL>

Returns args dictionary from the calling method


<EOL>super(BacktrackingTMCPP, self).write(proto.baseTM)<EOL>self.cells4.write(proto.cells4)<EOL>proto.makeCells4Ephemeral = self.makeCells4Ephemeral<EOL>proto.seed = self.seed<EOL>proto.checkSynapseConsistency = self.checkSynapseConsistency<EOL>proto.initArgs = json.dumps(self._initArgsDict)<EOL>

Populate serialization proto instance.

        :param proto: (BacktrackingTMCppProto) the proto instance to populate


<EOL>obj = BacktrackingTM.read(proto.baseTM)<EOL>obj.__class__ = cls<EOL>newCells4 = Cells4.read(proto.cells4)<EOL>print(newCells4)<EOL>obj.cells4 = newCells4<EOL>obj.makeCells4Ephemeral = proto.makeCells4Ephemeral<EOL>obj.seed = proto.seed<EOL>obj.checkSynapseConsistency = proto.checkSynapseConsistency<EOL>obj._initArgsDict = json.loads(proto.initArgs)<EOL>obj._initArgsDict["<STR_LIT>"] = str(obj._initArgsDict["<STR_LIT>"])<EOL>obj.allocateStatesInCPP = False<EOL>obj.retrieveLearningStates = False<EOL>obj._setStatePointers()<EOL>return obj<EOL>

Deserialize from proto instance.

        :param proto: (BacktrackingTMCppProto) the proto instance to read from


super(BacktrackingTMCPP, self).__setstate__(state)<EOL>if self.makeCells4Ephemeral:<EOL><INDENT>self._initCells4()<EOL><DEDENT>

Set the state of ourself from a serialized state.


e = BacktrackingTM._getEphemeralMembers(self)<EOL>if self.makeCells4Ephemeral:<EOL><INDENT>e.extend(['<STR_LIT>'])<EOL><DEDENT>return e<EOL>

List of our member variables that we don't need to be saved


BacktrackingTM._initEphemerals(self)<EOL>self.allocateStatesInCPP = False<EOL>self.retrieveLearningStates = False<EOL>if self.makeCells4Ephemeral:<EOL><INDENT>self._initCells4()<EOL><DEDENT>

Initialize all ephemeral members after being restored to a pickled state.


Save Cells4 state to a file. File can be loaded with :meth:`loadFromFile`.


self._setStatePointers()<EOL>self.cells4.loadFromFile(filePath)<EOL>

Load Cells4 state from a file saved with :meth:`saveToFile`.


try:<EOL><INDENT>return super(BacktrackingTMCPP, self).__getattr__(name)<EOL><DEDENT>except AttributeError:<EOL><INDENT>raise AttributeError("<STR_LIT>" % name)<EOL><DEDENT>

Patch __getattr__ so that we can catch the first access to 'cells' and load.

This function is only called when we try to access an attribute that doesn't
exist.  We purposely make sure that "self.cells" doesn't exist after
unpickling so that we'll hit this, then we can load it on the first access.

If this is called at any other time, it will raise an AttributeError.
That's because:
- If 'name' is "cells", after the first call, self._realCells won't exist
  so we'll get an implicit AttributeError.
- If 'name' isn't "cells", I'd expect our super wouldn't have __getattr__,
  so we'll raise our own Attribute error.  If the super did get __getattr__,
  we'll just return what it gives us.


def compute(self, bottomUpInput, enableLearn, enableInference=None):

<EOL>assert (bottomUpInput.dtype == numpy.dtype('<STR_LIT>')) or(bottomUpInput.dtype == numpy.dtype('<STR_LIT>')) or(bottomUpInput.dtype == numpy.dtype('<STR_LIT>'))<EOL>self.iterationIdx = self.iterationIdx + <NUM_LIT:1><EOL>if enableInference is None:<EOL><INDENT>if enableLearn:<EOL><INDENT>enableInference = False<EOL><DEDENT>else:<EOL><INDENT>enableInference = True<EOL><DEDENT><DEDENT>self._setStatePointers()<EOL>y = self.cells4.compute(bottomUpInput, enableInference, enableLearn)<EOL>self.currentOutput = y.reshape((self.numberOfCols, self.cellsPerColumn))<EOL>self.avgLearnedSeqLength = self.cells4.getAvgLearnedSeqLength()<EOL>self._copyAllocatedStates()<EOL>if self.collectStats:<EOL><INDENT>activeColumns = bottomUpInput.nonzero()[<NUM_LIT:0>]<EOL>if enableInference:<EOL><INDENT>predictedState = self.infPredictedState['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>predictedState = self.lrnPredictedState['<STR_LIT>']<EOL><DEDENT>self._updateStatsInferEnd(self._internalStats,<EOL>activeColumns,<EOL>predictedState,<EOL>self.colConfidence['<STR_LIT>'])<EOL><DEDENT>output = self._computeOutput()<EOL>self.printComputeEnd(output, learn=enableLearn)<EOL>self.resetCalled = False<EOL>return output<EOL>


self._setStatePointers()<EOL>self.cells4.inferPhase2()<EOL>self._copyAllocatedStates()<EOL>

This calls phase 2 of inference (used in multistep prediction).


<EOL>if self.verbosity > <NUM_LIT:1> or self.retrieveLearningStates:<EOL><INDENT>(activeT, activeT1, predT, predT1) = self.cells4.getLearnStates()<EOL>self.lrnActiveState['<STR_LIT>'] = activeT1.reshape((self.numberOfCols, self.cellsPerColumn))<EOL>self.lrnActiveState['<STR_LIT:t>'] = activeT.reshape((self.numberOfCols, self.cellsPerColumn))<EOL>self.lrnPredictedState['<STR_LIT>'] = predT1.reshape((self.numberOfCols, self.cellsPerColumn))<EOL>self.lrnPredictedState['<STR_LIT:t>'] = predT.reshape((self.numberOfCols, self.cellsPerColumn))<EOL><DEDENT>if self.allocateStatesInCPP:<EOL><INDENT>assert False<EOL>(activeT, activeT1, predT, predT1, colConfidenceT, colConfidenceT1, confidenceT,<EOL>confidenceT1) = self.cells4.getStates()<EOL>self.cellConfidence['<STR_LIT:t>'] = confidenceT.reshape((self.numberOfCols, self.cellsPerColumn))<EOL>self.cellConfidence['<STR_LIT>'] = confidenceT1.reshape((self.numberOfCols, self.cellsPerColumn))<EOL>self.colConfidence['<STR_LIT:t>'] = colConfidenceT.reshape(self.numberOfCols)<EOL>self.colConfidence['<STR_LIT>'] = colConfidenceT1.reshape(self.numberOfCols)<EOL>self.infActiveState['<STR_LIT>'] = activeT1.reshape((self.numberOfCols, self.cellsPerColumn))<EOL>self.infActiveState['<STR_LIT:t>'] = activeT.reshape((self.numberOfCols, self.cellsPerColumn))<EOL>self.infPredictedState['<STR_LIT>'] = predT1.reshape((self.numberOfCols, self.cellsPerColumn))<EOL>self.infPredictedState['<STR_LIT:t>'] = predT.reshape((self.numberOfCols, self.cellsPerColumn))<EOL><DEDENT>

If state is allocated in CPP, copy over the data into our numpy arrays.


If we are having CPP use numpy-allocated buffers, set these buffer
        pointers. This is a relatively fast operation and, for safety, should be
        done before every call to the cells4 compute methods.  This protects us
        in situations where code can cause Python or numpy to create copies.


if self.verbosity >= <NUM_LIT:3>:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>self._setStatePointers()<EOL>self.cells4.reset()<EOL>BacktrackingTM.reset(self)<EOL>


<EOL>self.trimSegments(minPermanence=<NUM_LIT>)<EOL>


def trimSegments(self, minPermanence=None, minNumSyns=None):

<EOL>if minPermanence is None:<EOL><INDENT>minPermanence = <NUM_LIT:0.0><EOL><DEDENT>if minNumSyns is None:<EOL><INDENT>minNumSyns = <NUM_LIT:0><EOL><DEDENT>if self.verbosity >= <NUM_LIT:5>:<EOL><INDENT>print("<STR_LIT>")<EOL>self.printCells(predictedOnly=False)<EOL><DEDENT>return self.cells4.trimSegments(minPermanence=minPermanence, minNumSyns=minNumSyns)<EOL>


<EOL>assert False<EOL>print("<STR_LIT>", len(self.segmentUpdates))<EOL>for key, updateList in self.segmentUpdates.items():<EOL><INDENT>c,i = key[<NUM_LIT:0>],key[<NUM_LIT:1>]<EOL>print(c,i,updateList)<EOL><DEDENT>


def _slowIsSegmentActive(self, seg, timeStep):

numSyn = seg.size()<EOL>numActiveSyns = <NUM_LIT:0><EOL>for synIdx in range(numSyn):<EOL><INDENT>if seg.getPermanence(synIdx) < self.connectedPerm:<EOL><INDENT>continue<EOL><DEDENT>sc, si = self.getColCellIdx(seg.getSrcCellIdx(synIdx))<EOL>if self.infActiveState[timeStep][sc, si]:<EOL><INDENT>numActiveSyns += <NUM_LIT:1><EOL>if numActiveSyns >= self.activationThreshold:<EOL><INDENT>return True<EOL><DEDENT><DEDENT><DEDENT>return numActiveSyns >= self.activationThreshold<EOL>

A segment is active if it has >= activationThreshold connected
synapses that are active due to infActiveState.


def printCell(self, c, i, onlyActiveSegments=False):

nSegs = self.cells4.nSegmentsOnCell(c,i)<EOL>if nSegs > <NUM_LIT:0>:<EOL><INDENT>segList = self.cells4.getNonEmptySegList(c,i)<EOL>gidx = c * self.cellsPerColumn + i<EOL>print("<STR_LIT>", c, "<STR_LIT>", i, "<STR_LIT>"%(gidx),"<STR_LIT::>", nSegs, "<STR_LIT>")<EOL>for k,segIdx in enumerate(segList):<EOL><INDENT>seg = self.cells4.getSegment(c, i, segIdx)<EOL>isActive = self._slowIsSegmentActive(seg, '<STR_LIT:t>')<EOL>if onlyActiveSegments and not isActive:<EOL><INDENT>continue<EOL><DEDENT>isActiveStr = "<STR_LIT:*>" if isActive else "<STR_LIT:U+0020>"<EOL>print("<STR_LIT>" % (isActiveStr, segIdx), end='<STR_LIT:U+0020>')<EOL>print(seg.size(), end='<STR_LIT:U+0020>')<EOL>print(seg.isSequenceSegment(), "<STR_LIT>" % (seg.dutyCycle(<EOL>self.cells4.getNLrnIterations(), False, True)), end='<STR_LIT:U+0020>')<EOL>print("<STR_LIT>" % (seg.getPositiveActivations(),<EOL>seg.getTotalActivations()), end='<STR_LIT:U+0020>')<EOL>print("<STR_LIT>" % (self.cells4.getNLrnIterations()<EOL>- seg.getLastActiveIteration()), end='<STR_LIT:U+0020>')<EOL>numSyn = seg.size()<EOL>for s in range(numSyn):<EOL><INDENT>sc, si = self.getColCellIdx(seg.getSrcCellIdx(s))<EOL>print("<STR_LIT>"%(sc, si, seg.getPermanence(s)), end='<STR_LIT:U+0020>')<EOL><DEDENT>print()<EOL><DEDENT><DEDENT>


return self.cells4.getAvgLearnedSeqLength()<EOL>


c = idx//self.cellsPerColumn<EOL>i = idx - c*self.cellsPerColumn<EOL>return c,i<EOL>

Get column and cell within column from a global cell index.
The global index is ``idx = colIdx * nCellsPerCol() + cellIdxInCol``

:param idx: (int) global cell index
:returns: (tuple) (colIdx, cellIdxInCol)


def getSegmentOnCell(self, c, i, segIdx):

segList = self.cells4.getNonEmptySegList(c,i)<EOL>seg = self.cells4.getSegment(c, i, segList[segIdx])<EOL>numSyn = seg.size()<EOL>assert numSyn != <NUM_LIT:0><EOL>result = []<EOL>result.append([int(segIdx), bool(seg.isSequenceSegment()),<EOL>seg.getPositiveActivations(),<EOL>seg.getTotalActivations(), seg.getLastActiveIteration(),<EOL>seg.getLastPosDutyCycle(),<EOL>seg.getLastPosDutyCycleIteration()])<EOL>for s in range(numSyn):<EOL><INDENT>sc, si = self.getColCellIdx(seg.getSrcCellIdx(s))<EOL>result.append([int(sc), int(si), seg.getPermanence(s)])<EOL><DEDENT>return result<EOL>


return self.cells4.nSegments()<EOL>


return self.cells4.nSynapses()<EOL>


return self.cells4.nSegmentsOnCell(c,i)<EOL>


def getSegmentInfo(self, collectActiveData = False):

<EOL>assert collectActiveData == False<EOL>nSegments, nSynapses = self.getNumSegments(), self.cells4.nSynapses()<EOL>distSegSizes, distNSegsPerCell = {}, {}<EOL>nActiveSegs, nActiveSynapses = <NUM_LIT:0>, <NUM_LIT:0><EOL>distPermValues = {}   <EOL>numAgeBuckets = <NUM_LIT:20><EOL>distAges = []<EOL>ageBucketSize = int((self.iterationIdx+<NUM_LIT:20>) / <NUM_LIT:20>)<EOL>for i in range(numAgeBuckets):<EOL><INDENT>distAges.append(['<STR_LIT>' % (i*ageBucketSize, (i+<NUM_LIT:1>)*ageBucketSize-<NUM_LIT:1>), <NUM_LIT:0>])<EOL><DEDENT>for c in range(self.numberOfCols):<EOL><INDENT>for i in range(self.cellsPerColumn):<EOL><INDENT>nSegmentsThisCell = self.getNumSegmentsInCell(c,i)<EOL>if nSegmentsThisCell > <NUM_LIT:0>:<EOL><INDENT>if nSegmentsThisCell in distNSegsPerCell:<EOL><INDENT>distNSegsPerCell[nSegmentsThisCell] += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>distNSegsPerCell[nSegmentsThisCell] = <NUM_LIT:1><EOL><DEDENT>segList = self.cells4.getNonEmptySegList(c,i)<EOL>for segIdx in range(nSegmentsThisCell):<EOL><INDENT>seg = self.getSegmentOnCell(c, i, segIdx)<EOL>nSynapsesThisSeg = len(seg) - <NUM_LIT:1><EOL>if nSynapsesThisSeg > <NUM_LIT:0>:<EOL><INDENT>if nSynapsesThisSeg in distSegSizes:<EOL><INDENT>distSegSizes[nSynapsesThisSeg] += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>distSegSizes[nSynapsesThisSeg] = <NUM_LIT:1><EOL><DEDENT>for syn in seg[<NUM_LIT:1>:]:<EOL><INDENT>p = int(syn[<NUM_LIT:2>]*<NUM_LIT:10>)<EOL>if p in distPermValues:<EOL><INDENT>distPermValues[p] += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>distPermValues[p] = <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>segObj = self.cells4.getSegment(c, i, segList[segIdx])<EOL>age = self.iterationIdx - segObj.getLastActiveIteration()<EOL>ageBucket = int(age/ageBucketSize)<EOL>distAges[ageBucket][<NUM_LIT:1>] += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT><DEDENT>return (nSegments, nSynapses, nActiveSegs, nActiveSynapses,distSegSizes, distNSegsPerCell, distPermValues, distAges)<EOL>


nActiveColumns = len(activeColumns)<EOL>if nActiveColumns > <NUM_LIT:0>:<EOL><INDENT>score = numpy.in1d(activeColumns, prevPredictedColumns).sum()<EOL>score = (nActiveColumns - score) / float(nActiveColumns)<EOL><DEDENT>else:<EOL><INDENT>score = <NUM_LIT:0.0><EOL><DEDENT>return score<EOL>

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)


def compute(self, activeColumns, predictedColumns,<EOL>inputValue=None, timestamp=None):

<EOL>anomalyScore = computeRawAnomalyScore(activeColumns, predictedColumns)<EOL>if self._mode == Anomaly.MODE_PURE:<EOL><INDENT>score = anomalyScore<EOL><DEDENT>elif self._mode == Anomaly.MODE_LIKELIHOOD:<EOL><INDENT>if inputValue is None:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>probability = self._likelihood.anomalyProbability(<EOL>inputValue, anomalyScore, timestamp)<EOL>score = <NUM_LIT:1> - probability<EOL><DEDENT>elif self._mode == Anomaly.MODE_WEIGHTED:<EOL><INDENT>probability = self._likelihood.anomalyProbability(<EOL>inputValue, anomalyScore, timestamp)<EOL>score = anomalyScore * (<NUM_LIT:1> - probability)<EOL><DEDENT>if self._movingAverage is not None:<EOL><INDENT>score = self._movingAverage.next(score)<EOL><DEDENT>if self._binaryThreshold is not None:<EOL><INDENT>if score >= self._binaryThreshold:<EOL><INDENT>score = <NUM_LIT:1.0><EOL><DEDENT>else:<EOL><INDENT>score = <NUM_LIT:0.0><EOL><DEDENT><DEDENT>return score<EOL>

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


self.__dict__.update(state)<EOL>if not hasattr(self, '<STR_LIT>'):<EOL><INDENT>self._mode = Anomaly.MODE_PURE<EOL><DEDENT>if not hasattr(self, '<STR_LIT>'):<EOL><INDENT>self._movingAverage = None<EOL><DEDENT>if not hasattr(self, '<STR_LIT>'):<EOL><INDENT>self._binaryThreshold = None<EOL><DEDENT>


impl = kwargs.pop('<STR_LIT>', None)<EOL>if impl is None:<EOL><INDENT>impl = Configuration.get('<STR_LIT>')<EOL><DEDENT>if impl == '<STR_LIT>':<EOL><INDENT>return SDRClassifier(*args, **kwargs)<EOL><DEDENT>elif impl == '<STR_LIT>':<EOL><INDENT>return FastSDRClassifier(*args, **kwargs)<EOL><DEDENT>elif impl == '<STR_LIT>':<EOL><INDENT>return SDRClassifierDiff(*args, **kwargs)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>' % impl)<EOL><DEDENT>

Create a SDR classifier factory.
The implementation of the SDR Classifier can be specified with
the "implementation" keyword argument.

The SDRClassifierFactory uses the implementation as specified in
 `Default NuPIC Configuration <default-config.html>`_.


impl = proto.implementation<EOL>if impl == '<STR_LIT>':<EOL><INDENT>return SDRClassifier.read(proto.sdrClassifier)<EOL><DEDENT>elif impl == '<STR_LIT>':<EOL><INDENT>return FastSDRClassifier.read(proto.sdrClassifier)<EOL><DEDENT>elif impl == '<STR_LIT>':<EOL><INDENT>return SDRClassifierDiff.read(proto.sdrClassifier)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>' % impl)<EOL><DEDENT>

:param proto: SDRClassifierRegionProto capnproto object


return []<EOL>

List of our member variables that we don't need to be saved.