def createRegion(self): """ Create the CLA Region and some default simple data. """ self.datas = [] self.datas.append(self.getData1()) # self.datas.append(self.getData2()) # self.datas.append(self.getData3()) # self.datas.append(self.getData4()) imgs = [] # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/A.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/B.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/C.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/D.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/E.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/F.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/G.bmp")) datas = [] for img in imgs: pix = img.load() imgData = [] for r in xrange(img.size[0]): row = [] for c in xrange(img.size[1]): p = pix[c, r] if p == 0: p = 1 elif p == 255: p = 0 row.append(p) imgData.append(row) datas.append(imgData) self.datas.append(datas) self.dNum = 0 self.data = list(self.datas[0][0]) self.time = 0 self.region = Region(self.data)
def __checkCreateRegion(self): """ Create the Region using the currently set UI parameters. The region is only created new if this is first run or if the UI parameters have changed since Region was last created. """ colGridSize = (self.colXSpin.GetValue(), self.colYSpin.GetValue()) pctInputPerCol = self.inputPerColSpin.GetValue() / 100.0 pctMinOverlap = self.minOverlapSpin.GetValue() / 100.0 localityRadius = self.localityRadSpin.GetValue() pctLocalActivity = self.desireLocalSpin.GetValue() / 100.0 cellsPerCol = self.cellsPerColSpin.GetValue() segActiveThreshold = self.thresholdSpin.GetValue() newSynapseCount = self.newSynSpin.GetValue() rebuild = False if not self.region: rebuild = self.onButton.GetValue() elif self.region.pctInputPerCol!=pctInputPerCol or \ self.region.pctMinOverlap!=pctMinOverlap or \ self.region.localityRadius!=localityRadius or \ self.region.pctLocalActivity!=pctLocalActivity or \ self.region.cellsPerCol!=cellsPerCol or \ self.region.segActiveThreshold!=segActiveThreshold or \ self.region.newSynapseCount!=newSynapseCount or \ len(self.region.columnGrid)!=colGridSize[0] or \ len(self.region.columnGrid[0])!=colGridSize[1] or \ self.region.inputWidth!=self.inputSize[0] or \ self.region.inputHeight!=self.inputSize[1]: rebuild = True if rebuild: self.statusValText.SetLabel("Initializing...") self.region = Region(self.inputSize, colGridSize, pctInputPerCol, \ pctMinOverlap, localityRadius, pctLocalActivity, \ cellsPerCol, segActiveThreshold, newSynapseCount) self.statusValText.SetLabel("Active") #Recreate the region visualizer if previously open if self.regionFrame: self.regionFrame.Close() self.regionFrame = RegionFrame(self.region) self.regionFrame.Show()
def createHierarchy(self, depth=1, inputSize=10): region = [] for level in range(0, depth): regionInputSize = int(abs(inputSize / 2**level)) regionOutputSize = int(abs(inputSize / 2**(level + 1))) print regionInputSize print regionOutputSize self.regions.append( Region([regionInputSize, regionInputSize], [regionOutputSize, regionOutputSize], pctInputPerCol=0.15, pctMinOverlap=0.0, localityRadius=regionOutputSize, pctLocalActivity=0.0, cellsPerCol=1, segActiveThreshold=1, newSynapseCount=25))
def createRegion(self): """ Create the CLA Region and some default simple data. """ self.datas = [] self.datas.append(self.getData1()) # self.datas.append(self.getData2()) # self.datas.append(self.getData3()) # self.datas.append(self.getData4()) imgs = [] # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/A.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/B.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/C.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/D.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/E.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/F.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/G.bmp")) datas = [] for img in imgs: pix = img.load() imgData = [] for r in xrange(img.size[0]): row = [] for c in xrange(img.size[1]): p = pix[c,r] if p==0: p = 1 elif p==255: p = 0 row.append(p) imgData.append(row) datas.append(imgData) self.datas.append(datas) self.dNum = 0 self.data = list(self.datas[0][0]) self.time = 0 self.region = Region(self.data)
class RegionParamsPanel(wx.Panel): """ wx Panel that displays and allows editing of key parameters used to create an HTM Region. """ def __init__(self, parent, id, inputSize): """ Create a new RegionParamsPanel with the standard wx parent and id plus the initial size of the input that is to be fed into the HTM Region. @param id: the wx panel id, but also used as the numerical id for the Region. @param inputSize: tuple (x,y) specifying the size of the input data for the Region. """ super(RegionParamsPanel, self).__init__(parent, id) self.predictedCols = None self.activeCols = None self.regionFrame = None self.inputSize = inputSize self.regionID = id self.region = None self._nextRegionParams = None #set UI controls self.staticBox = wx.StaticBox(self, -1, "Region") inputBox = wx.StaticBoxSizer(self.staticBox, orient=wx.VERTICAL) self.onButton = wx.CheckBox(self, 0, "Region On") self.onButton.SetToolTipString("Enable this Region for input processing (Region is created fresh if parameters have changed since last on).") self.onButton.Bind(wx.EVT_CHECKBOX, self.regionOnRun) hSizerLearn = wx.BoxSizer(wx.HORIZONTAL) self.spatialButton = wx.CheckBox(self, 0, "Spatial Learning") self.temporalButton = wx.CheckBox(self, 0, "Temporal Learning") hSizerLearn.Add(self.spatialButton, 1, \ wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM | wx.RIGHT, 5), hSizerLearn.Add(self.temporalButton, 1, \ wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM | wx.LEFT, 5) self.viewButton = wx.Button(self, 0, "Visualize") self.viewButton.Bind(wx.EVT_BUTTON, self.regionViewRun) self.viewButton.Disable() #disable until the Region is created hbox = wx.BoxSizer(wx.HORIZONTAL) panel = wx.Panel(self) fgs = wx.FlexGridSizer(7, 4, 5, 5) #rows, cols, vgap, hgap inputPerColText = wx.StaticText(panel, label="Input per Column (%)") minOverlapText = wx.StaticText(panel, label="Min Overlap (%)") localityRadText = wx.StaticText(panel, label="Locality Radius") desireLocalText = wx.StaticText(panel, label="Local Activity (%)") cellsPerColText = wx.StaticText(panel, label="Cells per Column") thresholdText = wx.StaticText(panel, label="Segment Threshold") newSynText = wx.StaticText(panel, label="New Synapse Count") colXText = wx.StaticText(panel, label="# Columns (X Dir)") colYText = wx.StaticText(panel, label="# Columns (Y Dir)") inhiRadText = wx.StaticText(panel, label="Inhibition Radius") predAccText = wx.StaticText(panel, label="Prediction Accuracy") activeAccText = wx.StaticText(panel, label="Activation Accuracy") statusText = wx.StaticText(panel, label="Region Status") blankText = wx.StaticText(panel, label="") inputPerColText.SetToolTipString("Percent of input bits within locality radius each Column has potential (proximal) synapses for.") minOverlapText.SetToolTipString("Minimum percent of column's proximal synapses that must be active for the column to be considered by the spatial pooler.") localityRadText.SetToolTipString("Furthest number of Columns away to allow distal synapse connections (0 means no restriction).") desireLocalText.SetToolTipString("Approximate percent of Columns within locality radius to be winners after inhibition.") cellsPerColText.SetToolTipString("Number of (temporal context) cells to use for each Column.") thresholdText.SetToolTipString("Minimum number of active synapses to activate a segment.") newSynText.SetToolTipString("Number of 'new' synapses to add to a segment if none activated during learning.") colXText.SetToolTipString("Number of Columns in the X direction (width) in this Region.") colYText.SetToolTipString("Number of Columns in the Y direction (height) in this Region.") inhiRadText.SetToolTipString("Radius, in Columns, of how far inhibition will take effect per Column (most recent time step).") predAccText.SetToolTipString("Correctly predicted active columns out of total sequence-segment predicted columns (most recent time step).") activeAccText.SetToolTipString("Correctly predicated active columns out of total active columns (most recent time step).") statusText.SetToolTipString("Current state of the Region.") self.inputPerColSpin = wx.SpinCtrl(panel, size=(70,-1), style=wx.SP_ARROW_KEYS) self.inputPerColSpin.SetValue(20) self.minOverlapSpin = wx.SpinCtrl(panel, size=(70,-1), style=wx.SP_ARROW_KEYS) self.minOverlapSpin.SetValue(10) self.localityRadSpin = wx.SpinCtrl(panel, size=(70,-1), style=wx.SP_ARROW_KEYS) self.localityRadSpin.SetValue(5) self.desireLocalSpin = wx.SpinCtrl(panel, size=(70,-1), style=wx.SP_ARROW_KEYS) self.desireLocalSpin.SetValue(20) self.cellsPerColSpin = wx.SpinCtrl(panel, size=(70,-1), style=wx.SP_ARROW_KEYS) self.cellsPerColSpin.SetRange(1,4) self.cellsPerColSpin.SetValue(1) self.thresholdSpin = wx.SpinCtrl(panel, size=(70,-1), style=wx.SP_ARROW_KEYS) self.thresholdSpin.SetRange(1, 100) self.thresholdSpin.SetValue(3) self.newSynSpin = wx.SpinCtrl(panel, size=(70,-1), style=wx.SP_ARROW_KEYS) self.newSynSpin.SetRange(1, 100) self.newSynSpin.SetValue(5) self.colXSpin = wx.SpinCtrl(panel, size=(70,-1), style=wx.SP_ARROW_KEYS) self.colXSpin.SetRange(4, inputSize[0]) self.colXSpin.SetValue(inputSize[0]/2) self.colYSpin = wx.SpinCtrl(panel, size=(70,-1), style=wx.SP_ARROW_KEYS) self.colYSpin.SetRange(4, inputSize[1]) self.colYSpin.SetValue(inputSize[1]/2) self.inhiRadValText = wx.StaticText(panel, label="?") self.predAccValText = wx.StaticText(panel, label="0%") self.activeAccValText = wx.StaticText(panel, label="0%") self.statusValText = wx.StaticText(panel, label="Not Built") blankValText = wx.StaticText(panel, label="") fgs.AddMany([(inputPerColText, 0, wx.ALIGN_CENTER_VERTICAL), (self.inputPerColSpin, 0, wx.ALIGN_CENTER_VERTICAL), (minOverlapText, 0, wx.ALIGN_CENTER_VERTICAL), (self.minOverlapSpin, 0, wx.ALIGN_CENTER_VERTICAL), (localityRadText, 0, wx.ALIGN_CENTER_VERTICAL), (self.localityRadSpin, 0, wx.ALIGN_CENTER_VERTICAL), (desireLocalText, 0, wx.ALIGN_CENTER_VERTICAL), (self.desireLocalSpin, 0, wx.ALIGN_CENTER_VERTICAL), (thresholdText, 0, wx.ALIGN_CENTER_VERTICAL), (self.thresholdSpin, 0, wx.ALIGN_CENTER_VERTICAL), (newSynText, 0, wx.ALIGN_CENTER_VERTICAL), (self.newSynSpin, 0, wx.ALIGN_CENTER_VERTICAL), (colXText, 0, wx.ALIGN_CENTER_VERTICAL), (self.colXSpin, 0, wx.ALIGN_CENTER_VERTICAL), (cellsPerColText, 0, wx.ALIGN_CENTER_VERTICAL), (self.cellsPerColSpin, 0, wx.ALIGN_CENTER_VERTICAL), (colYText, 0, wx.ALIGN_CENTER_VERTICAL), (self.colYSpin, 0, wx.ALIGN_CENTER_VERTICAL), (blankText, 0, wx.ALIGN_CENTER_VERTICAL), (blankValText, 0, wx.ALIGN_CENTER_VERTICAL), (inhiRadText, 0, wx.ALIGN_CENTER_VERTICAL), (self.inhiRadValText, 0, wx.ALIGN_CENTER_VERTICAL), (predAccText, 0, wx.ALIGN_CENTER_VERTICAL), (self.predAccValText, 0, wx.ALIGN_CENTER_VERTICAL), (statusText, 0, wx.ALIGN_CENTER_VERTICAL), (self.statusValText, 0, wx.ALIGN_CENTER_VERTICAL), (activeAccText, 0, wx.ALIGN_CENTER_VERTICAL), (self.activeAccValText, 0, wx.ALIGN_CENTER_VERTICAL)]) #fgs.AddGrowableCol(1, 1) hbox.Add(fgs, proportion=1, flag=wx.ALL|wx.EXPAND, border=5) panel.SetSizer(hbox) inputBox.Add(self.onButton, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM, border=5) inputBox.Add(hSizerLearn, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, border=5) inputBox.Add(panel, 0)#, wx.ALL, border=5) inputBox.Add(self.viewButton, 0, wx.ALIGN_CENTER_HORIZONTAL) vSizer = wx.BoxSizer(wx.VERTICAL) vSizer.Add(inputBox, 0, wx.ALIGN_LEFT | wx.ALL, border=0) self.__refreshInputSize() self.SetSizer(vSizer) self.SetAutoLayout(1) def __refreshInputSize(self): """ This method should be called when the region input size has changed. If it does we need to generate a new label for the main UI grouping box for this panel (which displays input size), as well as reset the boundaries for the column dimension spinners (which are bound by input size). """ sizeStr = str(self.inputSize[0])+"x"+str(self.inputSize[1]) rid = self.regionID src = "Region "+str(rid-1) if rid==1: src = "Video Source" self.staticBox.SetLabel("Region "+str(rid)+" (Input="+sizeStr+" from "+src+")") self.colXSpin.SetRange(4, self.inputSize[0]) self.colYSpin.SetRange(4, self.inputSize[1]) def setNextRegionParams(self, nextRegionParams): """ Assign a reference to the next (higher) hierarchical Region's Parameter panel. This is needed to allow communicating Region states between levels. If there is no higher Region, the nextRegionParams may be None. """ self._nextRegionParams = nextRegionParams if not self.onButton.GetValue(): self._nextRegionParams.onButton.Disable() def enableNextRegion(self, enable=True): """ Tell the next hierarchical region panel to enable or disable its "Region On" button. This method is recursive so telling the next region will then tell its next region such that changes take effect for all higher-up Regions. If any higher Regions were turned on, if enable is False they will first turn their Regions off before disabling the on button. """ if self._nextRegionParams: if not enable: self._nextRegionParams.onButton.SetValue(False) self._nextRegionParams.onButton.Disable() self._nextRegionParams.regionOnRun() else: regionOn = self.onButton.GetValue() self._nextRegionParams.inputSize = (self.colXSpin.GetValue(), self.colYSpin.GetValue()) self._nextRegionParams.__refreshInputSize() self._nextRegionParams.onButton.Enable(regionOn) self._nextRegionParams.enableNextRegion(enable) def regionOnRun(self, evt=None): """ User clicked button to enable/disable Region. If turned on, the Region will be created new if this is the first time or if the Region parameters have changed since last create. Otherwise the Region is just re-enabled to run without creating fresh. The UI for parameters is only enabled if the Region is off as most parameters cannot change once the Region has been created. """ isOn = self.onButton.GetValue() #if region previously created, rebuild if params changed self.__checkCreateRegion() if not isOn: self.statusValText.SetLabel("Inactive") #enable next hierarchical Region's onButton only if this one is on self.enableNextRegion(isOn) self.inputPerColSpin.Enable(not isOn) self.minOverlapSpin.Enable(not isOn) self.localityRadSpin.Enable(not isOn) self.desireLocalSpin.Enable(not isOn) self.cellsPerColSpin.Enable(not isOn) self.thresholdSpin.Enable(not isOn) self.newSynSpin.Enable(not isOn) self.colXSpin.Enable(not isOn) self.colYSpin.Enable(not isOn) self.viewButton.Enable(self.region!=None) def regionViewRun(self, evt=None): """ User clicked button to launch Region Visualization view. """ if not self.regionFrame: self.regionFrame = RegionFrame(self.region) self.regionFrame.SetTitle("Visualizer for Region "+str(self.regionID)) self.regionFrame.Show() def __checkCreateRegion(self): """ Create the Region using the currently set UI parameters. The region is only created new if this is first run or if the UI parameters have changed since Region was last created. """ colGridSize = (self.colXSpin.GetValue(), self.colYSpin.GetValue()) pctInputPerCol = self.inputPerColSpin.GetValue() / 100.0 pctMinOverlap = self.minOverlapSpin.GetValue() / 100.0 localityRadius = self.localityRadSpin.GetValue() pctLocalActivity = self.desireLocalSpin.GetValue() / 100.0 cellsPerCol = self.cellsPerColSpin.GetValue() segActiveThreshold = self.thresholdSpin.GetValue() newSynapseCount = self.newSynSpin.GetValue() rebuild = False if not self.region: rebuild = self.onButton.GetValue() elif self.region.pctInputPerCol!=pctInputPerCol or \ self.region.pctMinOverlap!=pctMinOverlap or \ self.region.localityRadius!=localityRadius or \ self.region.pctLocalActivity!=pctLocalActivity or \ self.region.cellsPerCol!=cellsPerCol or \ self.region.segActiveThreshold!=segActiveThreshold or \ self.region.newSynapseCount!=newSynapseCount or \ len(self.region.columnGrid)!=colGridSize[0] or \ len(self.region.columnGrid[0])!=colGridSize[1] or \ self.region.inputWidth!=self.inputSize[0] or \ self.region.inputHeight!=self.inputSize[1]: rebuild = True if rebuild: self.statusValText.SetLabel("Initializing...") self.region = Region(self.inputSize, colGridSize, pctInputPerCol, \ pctMinOverlap, localityRadius, pctLocalActivity, \ cellsPerCol, segActiveThreshold, newSynapseCount) self.statusValText.SetLabel("Active") #Recreate the region visualizer if previously open if self.regionFrame: self.regionFrame.Close() self.regionFrame = RegionFrame(self.region) self.regionFrame.Show() def runRegionOnce(self, inputData): """ If this Region is enabled to run, then run the Region for one time step using the last processed video frame (or last processed output from the previous Region in the hierarchy). Set the Region to learn based on checkbox enablement in the UI. @param inputData: current bit-matrix input to this Region. @return the output bit-matrix of the Region (or None if Region disabled). """ if not self.onButton.GetValue(): #Region disabled, return None return None #build new Region if first time Run if not self.region: self.__checkCreateRegion() self.statusValText.SetLabel("Active") #Update inputs, learning states, and run the Region self.region.updateInput(inputData) self.region.temporalLearning = self.temporalButton.GetValue() self.region.spatialLearning = self.spatialButton.GetValue() self.region.runOnce() self.inhiRadValText.SetLabel(SIGF.format(self.region.inhibitionRadius)) #refresh RegionFrame visualization if open if self.regionFrame: self.regionFrame.draw() nx = len(self.region.columnGrid) ny = len(self.region.columnGrid[0]) if self.activeCols==None or self.activeCols.shape!=(nx,ny): self.predictedCols = numpy.zeros((nx,ny), dtype=numpy.uint8) self.activeCols = numpy.zeros((nx,ny), dtype=numpy.uint8) #want to know % active columns that were correctly predicted for col in self.region.columns: if col.isActive: self.activeCols[col.cx][col.cy] = 1 else: self.activeCols[col.cx][col.cy] = 0 #compare active columns now, to predicted columns from t-1 a = self.activeCols p = self.predictedCols pctA = 0.0 pctP = 0.0 if numpy.sum(p)>0: pctP = (1.0*numpy.sum(a*p)) / numpy.sum(p) if numpy.sum(a)>0: pctA = (1.0*numpy.sum(a*p)) / numpy.sum(a) self.predAccValText.SetLabel(SIGF.format(pctP*100.0)+"%") self.activeAccValText.SetLabel(SIGF.format(pctA*100.0)+"%") #save the current prediction to compare to next time step for col in self.region.columns: self.predictedCols[col.cx][col.cy] = 0 for cell in col.cells: if cell.isPredicting: for seg in cell.segments: if seg.isSequence: self.predictedCols[col.cx][col.cy] = 1 break return self.region.getOutput()
class RegionTestWindow(wx.Panel): """ wxPython Panel for user to control and visualize a Simple Test CLA Region. """ def __init__(self, parent): super(RegionTestWindow, self).__init__(parent) self.createRegion() #add buttons on top control bar self.hSizer = wx.BoxSizer(wx.HORIZONTAL) self.runButton = wx.Button(self, -1, "Run 1 Time Step") self.runButton.Bind(wx.EVT_BUTTON, self.runRegionOnce) self.hSizer.Add(self.runButton, 1, wx.EXPAND|wx.ALL, border=5 ) self.run10Button = wx.Button(self, -1, "Run 10 Time Steps") self.run10Button.Bind(wx.EVT_BUTTON, self.runRegionTen) self.hSizer.Add(self.run10Button, 1, wx.EXPAND|wx.ALL, border=5 ) #add RegionCanvas where state will be painted self.canvas = RegionCanvas(self, self.region) # Use some sizers to see layout options self.vSizer = wx.BoxSizer(wx.VERTICAL) self.vSizer.Add(self.hSizer, 0, wx.ALIGN_RIGHT) self.vSizer.Add(self.canvas, 1, wx.EXPAND) #Layout sizers self.SetSizer(self.vSizer) self.SetAutoLayout(1) self.vSizer.Fit(self) def getData1(self): datas = [] datas.append([ [1,0,0,0,0,0,0,0,0], [0,1,0,0,0,0,0,0,0], [0,0,1,0,0,0,0,0,0], [0,0,0,1,0,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,0,0,0,0,0], # [0,0,0,0,0,0,0,0,0], # [0,0,0,0,0,0,0,0,0], # [0,0,0,0,0,0,0,0,0], ]) datas.append([ [0,0,0,0,0,0,0,0,1], [0,0,0,0,0,0,0,1,0], [0,0,0,0,0,0,1,0,0], [0,0,0,0,0,1,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,0,0,0,0,0], # [0,0,0,0,0,0,0,0,0], # [0,0,0,0,0,0,0,0,0], # [0,0,0,0,0,0,0,0,0], ]) datas.append([ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,0,1,0,0,0], # [0,0,0,0,0,0,1,0,0], # [0,0,0,0,0,0,0,1,0], # [0,0,0,0,0,0,0,0,1], ]) datas.append([ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,1,0,0,0,0,0], # [0,0,1,0,0,0,0,0,0], # [0,1,0,0,0,0,0,0,0], # [1,0,0,0,0,0,0,0,0], ]) return datas def getData2(self): datas = [] datas.append([ [1,0,0,0,0,0,0,0,0], [0,1,0,0,0,0,0,0,0], [0,0,1,0,0,0,0,0,0], [0,0,0,1,0,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], ]) datas.append([ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,1,0,0,0,0,0], [0,0,1,0,0,0,0,0,0], [0,1,0,0,0,0,0,0,0], [1,0,0,0,0,0,0,0,0], ]) datas.append([ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,0,1,0,0,0], [0,0,0,0,0,0,1,0,0], [0,0,0,0,0,0,0,1,0], [0,0,0,0,0,0,0,0,1], ]) datas.append([ [0,0,0,0,0,0,0,0,1], [0,0,0,0,0,0,0,1,0], [0,0,0,0,0,0,1,0,0], [0,0,0,0,0,1,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], ]) return datas def getData3(self): """ doc """ datas = [] datas.append([ [0,0,0,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], ]) datas.append([ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,1,1,1,1,1], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], ]) datas.append([ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0], ]) datas.append([ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [1,1,1,1,1,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], ]) return datas def getData4(self): """ doc """ datas = [] datas.append([ [0,0,0,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], ]) datas.append([ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [1,1,1,1,1,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], ]) datas.append([ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,1,0,0,0,0], ]) datas.append([ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,1,1,1,1,1], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], ]) return datas def createRegion(self): """ Create the CLA Region and some default simple data. """ self.datas = [] self.datas.append(self.getData1()) # self.datas.append(self.getData2()) # self.datas.append(self.getData3()) # self.datas.append(self.getData4()) imgs = [] # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/A.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/B.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/C.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/D.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/E.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/F.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/G.bmp")) datas = [] for img in imgs: pix = img.load() imgData = [] for r in xrange(img.size[0]): row = [] for c in xrange(img.size[1]): p = pix[c,r] if p==0: p = 1 elif p==255: p = 0 row.append(p) imgData.append(row) datas.append(imgData) self.datas.append(datas) self.dNum = 0 self.data = list(self.datas[0][0]) self.time = 0 self.region = Region(self.data) def runRegionOnce(self, event): self.runRegion() def runRegionTen(self, event): self.runRegion(timeSteps=10) def runRegion(self, timeSteps=1): """ Run the Region for the specified number of time steps. """ for t in xrange(timeSteps): self.region.runOnce() #draw state of Region after run completes self.canvas.syncInputData(self.data) self.canvas.draw() #now update the data to its next frame self.time += 1 # if self.time % len(self.datas[self.dNum]) == 0: # self.dNum = random.randint(0,len(self.datas)-1) # dN = self.dNum dN = 0 newData = list(self.datas[dN][self.time % len(self.datas[dN])]) for i in xrange(len(self.data)): self.data[i] = list(newData[i]) def shiftRowsVertically(self): """ Shift all data rows down in a rotating manner. """ firstRow = self.data[0] for i in xrange(len(self.data)-1): self.data[i] = self.data[i+1] self.data[-1] = firstRow
class RegionTestWindow(wx.Panel): """ wxPython Panel for user to control and visualize a Simple Test CLA Region. """ def __init__(self, parent): super(RegionTestWindow, self).__init__(parent) self.createRegion() #add buttons on top control bar self.hSizer = wx.BoxSizer(wx.HORIZONTAL) self.runButton = wx.Button(self, -1, "Run 1 Time Step") self.runButton.Bind(wx.EVT_BUTTON, self.runRegionOnce) self.hSizer.Add(self.runButton, 1, wx.EXPAND | wx.ALL, border=5) self.run10Button = wx.Button(self, -1, "Run 10 Time Steps") self.run10Button.Bind(wx.EVT_BUTTON, self.runRegionTen) self.hSizer.Add(self.run10Button, 1, wx.EXPAND | wx.ALL, border=5) #add RegionCanvas where state will be painted self.canvas = RegionCanvas(self, self.region) # Use some sizers to see layout options self.vSizer = wx.BoxSizer(wx.VERTICAL) self.vSizer.Add(self.hSizer, 0, wx.ALIGN_RIGHT) self.vSizer.Add(self.canvas, 1, wx.EXPAND) #Layout sizers self.SetSizer(self.vSizer) self.SetAutoLayout(1) self.vSizer.Fit(self) def getData1(self): datas = [] datas.append([ [1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], # [0,0,0,0,0,0,0,0,0], # [0,0,0,0,0,0,0,0,0], # [0,0,0,0,0,0,0,0,0], ]) datas.append([ [0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], # [0,0,0,0,0,0,0,0,0], # [0,0,0,0,0,0,0,0,0], # [0,0,0,0,0,0,0,0,0], ]) datas.append([ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0], # [0,0,0,0,0,0,1,0,0], # [0,0,0,0,0,0,0,1,0], # [0,0,0,0,0,0,0,0,1], ]) datas.append([ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], # [0,0,1,0,0,0,0,0,0], # [0,1,0,0,0,0,0,0,0], # [1,0,0,0,0,0,0,0,0], ]) return datas def getData2(self): datas = [] datas.append([ [1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], ]) datas.append([ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0], ]) datas.append([ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1], ]) datas.append([ [0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], ]) return datas def getData3(self): """ doc """ datas = [] datas.append([ [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], ]) datas.append([ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], ]) datas.append([ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], ]) datas.append([ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], ]) return datas def getData4(self): """ doc """ datas = [] datas.append([ [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], ]) datas.append([ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], ]) datas.append([ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], ]) datas.append([ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], ]) return datas def createRegion(self): """ Create the CLA Region and some default simple data. """ self.datas = [] self.datas.append(self.getData1()) # self.datas.append(self.getData2()) # self.datas.append(self.getData3()) # self.datas.append(self.getData4()) imgs = [] # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/A.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/B.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/C.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/D.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/E.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/F.bmp")) # imgs.append(Image.open("C:/apps/numenta/cla/bitmaps/G.bmp")) datas = [] for img in imgs: pix = img.load() imgData = [] for r in xrange(img.size[0]): row = [] for c in xrange(img.size[1]): p = pix[c, r] if p == 0: p = 1 elif p == 255: p = 0 row.append(p) imgData.append(row) datas.append(imgData) self.datas.append(datas) self.dNum = 0 self.data = list(self.datas[0][0]) self.time = 0 self.region = Region(self.data) def runRegionOnce(self, event): self.runRegion() def runRegionTen(self, event): self.runRegion(timeSteps=10) def runRegion(self, timeSteps=1): """ Run the Region for the specified number of time steps. """ for t in xrange(timeSteps): self.region.runOnce() #draw state of Region after run completes self.canvas.syncInputData(self.data) self.canvas.draw() #now update the data to its next frame self.time += 1 # if self.time % len(self.datas[self.dNum]) == 0: # self.dNum = random.randint(0,len(self.datas)-1) # dN = self.dNum dN = 0 newData = list(self.datas[dN][self.time % len(self.datas[dN])]) for i in xrange(len(self.data)): self.data[i] = list(newData[i]) def shiftRowsVertically(self): """ Shift all data rows down in a rotating manner. """ firstRow = self.data[0] for i in xrange(len(self.data) - 1): self.data[i] = self.data[i + 1] self.data[-1] = firstRow