def testSP():
  """ Run a SP test
  """

  elemSize = 400
  numSet = 42

  addNear = True
  numRecords = 2

  wantPlot = True

  poolPct = 0.5
  itr = 1
  doLearn = True

  while numRecords < 3:

    # Setup a SP
    sp = SpatialPooler(
           columnDimensions=(2048, 1),
           inputDimensions=(1, elemSize),
           potentialRadius=elemSize/2,
           numActiveColumnsPerInhArea=40,
           spVerbosity=0,
           stimulusThreshold=0,
           seed=1,
           potentialPct=poolPct,
           globalInhibition=True
           )

    # Generate inputs using rand()
    inputs = generateRandomInput(numRecords, elemSize, numSet)
    if addNear:
      # Append similar entries (distance of 1)
      appendInputWithNSimilarValues(inputs, 42)

    inputSize = len(inputs)
    print 'Num random records = %d, inputs to process %d' % (numRecords, inputSize)

    # Run a number of iterations, with learning on or off,
    # retrieve results from the last iteration only
    outputs = np.zeros((inputSize,2048))

    numIter = 1
    if doLearn:
      numIter = itr

    for iter in xrange(numIter):
      for i in xrange(inputSize):
        time.sleep(0.001)
        if iter == numIter - 1:
          # TODO: See https://github.com/numenta/nupic/issues/2072
          sp.compute(inputs[i], learn=doLearn, activeArray=outputs[i])
          #print outputs[i].sum(), outputs[i]
        else:
          # TODO: See https://github.com/numenta/nupic/issues/2072
          output = np.zeros(2048)
          sp.compute(inputs[i], learn=doLearn, activeArray=output)

    # Build a plot from the generated input and output and display it
    distribMatrix = generatePlot(outputs, inputs)

    # If we don't want a plot, just continue
    if wantPlot:
      plt.imshow(distribMatrix, origin='lower', interpolation = "nearest")
      plt.ylabel('SP (2048/40) distance in %')
      plt.xlabel('Input (400/42) distance in %')

      title = 'SP distribution'
      if doLearn:
        title += ', leaning ON'
      else:
        title +=  ', learning OFF'

      title += ', inputs = %d' % len(inputs)
      title += ', iterations = %d' % numIter
      title += ', poolPct =%f' % poolPct

      plt.suptitle(title, fontsize=12)
      plt.show()
      #plt.savefig(os.path.join('~/Desktop/ExperimentResults/videos5', '%s' % numRecords))
      #plt.clf()

    numRecords += 1

  return
def testSP ( ) : elemSize = 400 numSet = 42 addNear = True numRecords = 2 wantPlot = True poolPct = 0.5 itr = 1 doLearn = True while numRecords < 3 : sp = SpatialPooler ( columnDimensions = ( 2048 , 1 ) , inputDimensions = ( 1 , elemSize ) , potentialRadius = elemSize / 2 , numActiveColumnsPerInhArea = 40 , spVerbosity = 0 , stimulusThreshold = 0 , seed = 1 , potentialPct = poolPct , globalInhibition = True ) inputs = generateRandomInput ( numRecords , elemSize , numSet ) if addNear : appendInputWithNSimilarValues ( inputs , 42 ) inputSize = len ( inputs ) print 'Num random records = %d, inputs to process %d' % ( numRecords , inputSize ) outputs = np . zeros ( ( inputSize , 2048 ) ) numIter = 1 if doLearn : numIter = itr for iter in xrange ( numIter ) : for i in xrange ( inputSize ) : time . sleep ( 0.001 ) if iter == numIter - 1 : sp . compute ( inputs [ i ] , learn = doLearn , activeArray = outputs [ i ] ) else : output = np . zeros ( 2048 ) sp . compute ( inputs [ i ] , learn = doLearn , activeArray = output ) distribMatrix = generatePlot ( outputs , inputs ) if wantPlot : plt . imshow ( distribMatrix , origin = 'lower' , interpolation = "nearest" ) plt . ylabel ( 'SP (2048/40) distance in %' ) plt . xlabel ( 'Input (400/42) distance in %' ) title = 'SP distribution' if doLearn : title += ', leaning ON' else : title += ', learning OFF' title += ', inputs = %d' % len ( inputs ) title += ', iterations = %d' % numIter title += ', poolPct =%f' % poolPct plt . suptitle ( title , fontsize = 12 ) plt . show ( ) numRecords += 1 return
Run a SP test