diff --git a/src/carver/htm/__init__.py b/src/carver/htm/__init__.py index c60e685..1697945 100644 --- a/src/carver/htm/__init__.py +++ b/src/carver/htm/__init__.py @@ -23,8 +23,15 @@ EXTENSION_NEXT_STEP_PENALTY = config.getboolean('extensions', 'next_step_penalty') class HTM(object): + + dimensions = 2 #hard-coded for now + def __init__(self, cellsPerColumn=None): self.inhibitionRadius = config.getint('init', 'inhibitionRadius') + + impliedSparsity = float(config.getint('constants','desiredLocalActivity'))/(self.inhibitionRadius**self.dimensions) + self.impliedSparsity = min(impliedSparsity, 1.0) + if cellsPerColumn: self.cellsPerColumn = cellsPerColumn else: @@ -97,36 +104,35 @@ def inputLength(self): return len(self._inputCells[0]) def imagineNext(self): - 'project down estimates for next time step to the input cells' + 'project down estimates for next time step to the input cells, and step through' self._imagineStimulate(self.columns) - return self._imagineOverride(self._inputCells) - + self._imagineOverride(self._inputCells) + self.__executeOne(False) + @classmethod - def _imagineStimulate(cls, columns): - 'testable step one of imagineNext' + def stimulateFromColumns(cls, columns, columnFilter): for col in columns: - if col.predicting: - down_scale = len(col.synapsesConnected) - activityPerSynapse = float(1) / down_scale + if columnFilter(col): + permanences = map(lambda syn: syn.permanence, col.synapsesConnected) + down_scale = float(sum(permanences)) for synapse in col.synapsesConnected: - synapse.input.stimulate(activityPerSynapse) + synapse.input.stimulate(synapse.permanence / down_scale) + + @classmethod + def _imagineStimulate(cls, columns): + 'testable step one of imagineNext' + cls.stimulateFromColumns(columns, lambda col: col.predictingNext) @classmethod def _imagineOverride(cls, inputCells): 'testable step two of imagineNext' - #flatten cell matrix - allInputs = [] + cls.normalize_input_stimulation(inputCells) + for row in inputCells: for cell in row: - allInputs.append(cell) - - maxStim = max(map(lambda inCell: inCell.stimulation, allInputs)) - if maxStim: - for inCell in allInputs: - inCell.stimulation /= maxStim - inCell.override() + cell.override() def executeOnce(self, data, learning=True, postTick=None): ''' @@ -248,8 +254,22 @@ def average_receptive_field_size(self): radii = [] for c in self.columns: for syn in c.synapsesConnected: - radii.append(((c.x-syn.input.x)**2 + (c.y-syn.input.y)**2)**0.5) + radius = ((c.x-syn.input.x)**2 + (c.y-syn.input.y)**2)**0.5 + if radius!=0: + radii.append(radius) return sum(radii)/len(radii) + + @classmethod + def _max_input_stimulation(cls, inputCells): + return max(cell.stimulation for row in inputCells for cell in row) + + @classmethod + def normalize_input_stimulation(cls, inputCells): + maxStim = cls._max_input_stimulation(inputCells) + if maxStim: + for row in inputCells: + for cell in row: + cell.stimulation /= maxStim class UpdateSegments(DictDefault): diff --git a/src/carver/htm/cell.py b/src/carver/htm/cell.py index 55b7d17..6110cd4 100644 --- a/src/carver/htm/cell.py +++ b/src/carver/htm/cell.py @@ -144,15 +144,25 @@ def __ne__(self, other): return not (self == other) def findSegmentWasActive(self, nextStep=True): - 'prefer distal, return hits from segments connected to other cells that were active' + ''' + prefer distal (comes for free, all cells have only distal segments) + prefer segments where learning cell triggered + return hits from segments connected to other cells that were active + ''' if nextStep: segments = self.segmentsNear else: segments = self.segments + activeSeg = None + for seg in segments: if seg.wasActive: - return seg + activeSeg = seg + if seg.wasActiveFromLearningCells: + return seg + + return activeSeg def bestMatchingSegment(self, nextStep): ''' @@ -167,7 +177,12 @@ def bestMatchingSegment(self, nextStep): ''' bestSegment = None bestSegmentSynapseCount = MIN_THRESHOLD-1 - for seg in filter(lambda seg: seg.nextStep == nextStep, self.segments): + + segments = self.segments + if nextStep: + segments = self.segmentsNear + + for seg in segments: synapseCount = len(seg.old_firing_synapses(requireConnection=False)) if synapseCount > bestSegmentSynapseCount: bestSegmentSynapseCount = synapseCount diff --git a/src/carver/htm/column.py b/src/carver/htm/column.py index a240d6d..0c0f3e4 100644 --- a/src/carver/htm/column.py +++ b/src/carver/htm/column.py @@ -127,16 +127,20 @@ def neighbor_duty_cycle_max(self): ''' return max((c.dutyCycleActive for c in self.neighbors)) + def __adjustKthNeighborForSparsity(self, numNeighbors): + return max(int(self.htm.impliedSparsity*numNeighbors), 1) + def kth_neighbor(self, k): ''' Numenta docs: Given the list of columns, return the kth highest overlap value ''' allNeighbors = sorted(self.neighbors, reverse=True, key=lambda col: col.overlap) - index = min(k-1,len(allNeighbors)-1) + sparserK = self.__adjustKthNeighborForSparsity(len(allNeighbors)) + index = min(k-1, sparserK-1) return allNeighbors[index] - @property + @property def predicting(self): for cell in self.cells: if cell.predicting: @@ -151,6 +155,18 @@ def predictedNext(self): return True return False + @property + def predictingNext(self): + ''' + is this column expected to get excited next? + @requires: cells' active state is already set + ''' + for cell in self.cells: + for seg in cell.segmentsNear: + if seg.active: + return True + return False + def __hash__(self): return self.x * self.y * hash(self.htm) diff --git a/src/carver/htm/input.py b/src/carver/htm/input.py index 9598861..1ce207b 100644 --- a/src/carver/htm/input.py +++ b/src/carver/htm/input.py @@ -3,6 +3,8 @@ @author: Jason Carver ''' +from carver.htm.synapse import SYNAPSES_PER_SEGMENT +from carver.htm.segment import FRACTION_SEGMENT_ACTIVATION_THRESHOLD class InputCell(object): ''' @@ -49,8 +51,12 @@ def stimulate(self, amount): def override(self): self.overrideInput = True - #TODO move magic number 0.8 into .properties file, or make htm understand float - self.stimulationPast = 1 if self.stimulation > 0.8 else 0 - self.resetStimulation() + + if self.stimulation >= FRACTION_SEGMENT_ACTIVATION_THRESHOLD: + self.stimulationPast = 1 + else: + self.stimulationPast = 0 + + self.resetStimulation() diff --git a/src/carver/htm/io/image_builder.py b/src/carver/htm/io/image_builder.py index 4f743dc..4002380 100644 --- a/src/carver/htm/io/image_builder.py +++ b/src/carver/htm/io/image_builder.py @@ -5,6 +5,10 @@ ''' from PIL import Image +from carver.htm import HTM +from carver.htm.synapse import SYNAPSES_PER_SEGMENT +from carver.htm.segment import FRACTION_SEGMENT_ACTIVATION_THRESHOLD +from carver.htm.ui.excite_history import ExciteHistory class ImageBuilder(object): ''' @@ -35,37 +39,101 @@ def setData(self, data): def show(self): return self.img.show() -class ColumnDisplay(object): - def __init__(self, htm): - width = len(htm._column_grid) - length = len(htm._column_grid[0]) - self.imageBuilder = ImageBuilder((width, length), self.colStateColor) - self.imageBuilder.setData(htm.columns) +class HTMDisplayBase(object): def show(self): return self.imageBuilder.show() - + @classmethod def showNow(cls, htm): cls(htm).show() + +class ActivityOverTimeDisplay(HTMDisplayBase): + def __init__(self, columnStates): + img = ImageBuilder([len(columnStates[0]), len(columnStates)], self.stateToRGB) + img.setData2D(columnStates) + img.rotate90() + self.imageBuilder = img + + def show(self): + print """*************** Graph History ************** +Y-Axis: All cells in the network, with the 4 cells per column grouped together +X-Axis: Time +Colors: +\tblack: no activity +\tgray: predicting +\twhite: active +""" + HTMDisplayBase.show(self) + + @classmethod + def stateToRGB(cls, state): + if state == ExciteHistory.ACTIVE: + return (255, 255, 255) + elif state == ExciteHistory.PREDICTING: + return (127, 127, 127) + elif state == ExciteHistory.INACTIVE: + return (0, 0, 0) + else: + return (255, 0, 0) #unknown cell/column state + +class ColumnDisplay(HTMDisplayBase): + def __init__(self, htm): + width = len(htm._column_grid) + length = len(htm._column_grid[0]) + self.imageBuilder = ImageBuilder((width, length), self.colStateColor) + self.imageBuilder.setData(htm.columns) @classmethod def colStateColor(cls, column): - if column.active: + if column.active and column.predictedNext: #correct return (0,255,0) + elif column.active: #false negative + return (255,0,0) + elif column.predictedNext: #false positive + return (180,0,180) #purple else: return (0,0,0) +class InputReflectionOverlayDisplay(HTMDisplayBase): + 'show the input cells, and the column activation pushed back onto the input space' + def __init__(self, htm): + self.imageBuilder = ImageBuilder((htm.inputWidth, htm.inputLength), self.inputOverlay) + + HTM.stimulateFromColumns(htm.columns, lambda col: col.active) + HTM.normalize_input_stimulation(htm._inputCells) + + data = [(cell, cell.stimulation) for row in htm._inputCells for cell in row] + + for row in htm._inputCells: + for cell in row: + cell.resetStimulation() + + self.imageBuilder.setData(data) + + @classmethod + def inputOverlay(cls, cellInfo): + (cell, percentStimulated) = cellInfo + + triggered = percentStimulated >= FRACTION_SEGMENT_ACTIVATION_THRESHOLD + + if cell.wasActive and triggered: #correct + return (0,int(percentStimulated*255),0) + elif cell.wasActive: #false negative + return (255-int(percentStimulated*255),0,0) + elif triggered: #false positive + return (int(percentStimulated*180),0,int(percentStimulated*180)) #purple + else: + gray = int(percentStimulated*255) + return (gray,gray,gray) + -class InputCellsDisplay(object): +class InputCellsDisplay(HTMDisplayBase): def __init__(self, htm): - self.imageBuilder = ImageBuilder((htm.width, htm.length), self.cellActiveBW) + self.imageBuilder = ImageBuilder((htm.inputWidth, htm.inputLength), self.cellActiveBW) data = [cell for row in htm._inputCells for cell in row] self.imageBuilder.setData(data) - def show(self): - return self.imageBuilder.show() - @classmethod def cellActiveBW(cls, cell): 'A black and white representation of whether a cell is active' diff --git a/src/carver/htm/segment.py b/src/carver/htm/segment.py index 67bd9c8..5a599bb 100644 --- a/src/carver/htm/segment.py +++ b/src/carver/htm/segment.py @@ -46,6 +46,18 @@ def old_firing_synapses(self, requireConnection=True): ''' return filter(lambda synapse: synapse.was_firing(requireConnection=requireConnection), self.synapses) + + @classmethod + def _is_firing_filter(cls, requireConnection=True): + return lambda synapse: synapse.is_firing(requireConnection=requireConnection) + + def synapses_firing(self, requireConnection=True): + ''' + @param requireConnection: only include synapse if the synapse is connected + @return an iterable of firing synapses + ''' + return filter(self._is_firing_filter(requireConnection), + self.synapses) def increase_permanences(self, byAmount): 'increase permanence on all synapses' @@ -72,7 +84,7 @@ def active(self): Timing note: a synapse is considered active if the cell it came from was active in the previous step ''' - filterFunc = lambda synapse: synapse.is_firing(requireConnection=True) + filterFunc = self._is_firing_filter(requireConnection=True) return self._areSynapsesAboveThreshold(filterFunc) @property @@ -121,6 +133,7 @@ def round_out_synapses(self, htm): missingSynapses = MAX_NEW_SYNAPSES - len(synapses) if missingSynapses > 0: lastLearningCells = filter(lambda cell: cell.wasLearning, htm.cells) - for _ in xrange(missingSynapses): - cell = random.choice(lastLearningCells) - self.create_synapse(cell) + if len(lastLearningCells): + for _ in xrange(missingSynapses): + cell = random.choice(lastLearningCells) + self.create_synapse(cell) diff --git a/src/carver/htm/synapse.py b/src/carver/htm/synapse.py index 61b5c46..baf8e6f 100644 --- a/src/carver/htm/synapse.py +++ b/src/carver/htm/synapse.py @@ -37,7 +37,7 @@ def was_firing(self, requireConnection=True): if False: return True even if synapse is not connected ''' return self.input.wasActive and (self.connected or not requireConnection) - + def is_firing(self, requireConnection=True): ''' Is the input firing? @@ -54,7 +54,7 @@ def firing_at(self, timeDelta, requireConnection=True): else: raise NotImplementedError - def wasInputLearning(self): + def wasInputLearning(self): if not hasattr(input, 'learning'): return False else: diff --git a/src/carver/tests/recognition_temporal.py b/src/carver/tests/recognition_temporal.py new file mode 100644 index 0000000..0339e19 --- /dev/null +++ b/src/carver/tests/recognition_temporal.py @@ -0,0 +1,213 @@ +''' +Created on Dec 7, 2010 + +@author: Jason Carver +''' +import unittest +from carver.htm import HTM +from carver.htm.ui.excite_history import ExciteHistory +from carver.htm.io.objectrecognize import ObjectRecognize +from carver.htm.io.translate_input import TranslateInput +from PIL import Image +from carver.htm.io.image_builder import ImageBuilder, InputCellsDisplay,\ + ColumnDisplay, InputReflectionOverlayDisplay, ActivityOverTimeDisplay +from carver.htm.synapse import SYNAPSES_PER_SEGMENT +from carver.htm.segment import FRACTION_SEGMENT_ACTIVATION_THRESHOLD + +class TestRecognitionTemporal(unittest.TestCase): + + + def setUp(self): + self.left_block = [ + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,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,0,0,0,0,0,0,0,0,0,0], + [1,1,1,1,0,0,0,0,0,0,0,0,0,0], + [1,1,1,1,0,0,0,0,0,0,0,0,0,0], + [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,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + ] + self.right_block = [ + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [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], + [0,0,0,0,0,0,0,0,0,0,1,1,1,1], + [0,0,0,0,0,0,0,0,0,0,1,1,1,1], + [0,0,0,0,0,0,0,0,0,0,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,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + ] + self.top_block = [ + [0,0,0,0,0,1,1,1,1,0,0,0,0,0], + [0,0,0,0,0,1,1,1,1,0,0,0,0,0], + [0,0,0,0,0,1,1,1,1,0,0,0,0,0], + [0,0,0,0,0,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,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + ] + self.bottom_block = [ + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,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,0,0,0,0,0], + [0,0,0,0,0,1,1,1,1,0,0,0,0,0], + [0,0,0,0,0,1,1,1,1,0,0,0,0,0], + [0,0,0,0,0,1,1,1,1,0,0,0,0,0], + ] + + def tearDown(self): + pass + + def testToyTemporalSetup(self): + data = [ + [1,0], + [0,0], + ] + h = HTM(cellsPerColumn=3) + h.initialize_input(data) + + rowflip = TranslateInput(data, shift=(1,0)) + colflip = TranslateInput(data, shift=(0,1)) + + history = ExciteHistory() + h.execute(rowflip.dataGenerator(), ticks=20, postTick=history.update) + h.execute(colflip.dataGenerator(), ticks=20, postTick=history.update) + h.execute(rowflip.dataGenerator(), ticks=20, postTick=history.update) + h.execute(colflip.dataGenerator(), ticks=20, postTick=history.update) + + def printCellActive(htm): + print "cell activity:" + for i, col in enumerate(htm.columns): + print ( + "column %s:" % i, + ' '.join(map(lambda c: '1' if c.active else '0', col.cells)) + ) + + h.execute(rowflip.dataGenerator(), ticks=6, learning=True, + #postTick=printCellActive + ) + + for _ in xrange(4): + h.imagineNext() +# InputReflectionOverlayDisplay.showNow(h) + + #check that the right cell is active + active = filter(lambda i:i.wasActive, [i for row in h._inputCells for i in row]) + + ActivityOverTimeDisplay.showNow(history.data) + + #InputCellsDisplay.showNow(h) + + #imagine next step + h._imagineStimulate(h.columns) + print "stimulation:" + for row in h._inputCells: + print ' '.join(map(lambda cell: '%.2f' % cell.stimulation, row)) + + self.assertEqual(1, len(active)) + self.assertEqual(0, active[0].x+active[0].y) + + self.assertGreater(len(filter(lambda c: c.active, h.cells)), 0) + self.assertLess(len(filter(lambda c: c.active, h.cells)), 3) + + def testTemporalImagination(self): + h = HTM(cellsPerColumn=3) + h.initialize_input(self.left_block, +# compressionFactor=2 + ) + + history = ExciteHistory() + + #show a whole left->right pass, 5 times + steps = 14*10 + + for _ in xrange(2): + goright = TranslateInput(self.left_block, shift=(0,1)) + h.execute(goright.dataGenerator(), ticks=steps-1, postTick=history.update) + + #show a whole right->left pass, 5 times + goleft = TranslateInput(self.right_block, shift=(0,-1)) + h.execute(goleft.dataGenerator(), ticks=steps-1, postTick=history.update) + + #show a whole top->bottom pass, 5 times + godown = TranslateInput(self.top_block, shift=(1,0)) + h.execute(godown.dataGenerator(), ticks=steps-1, postTick=history.update) + + #show a whole bottom->top pass, 5 times + goup = TranslateInput(self.bottom_block, shift=(-1,0)) + h.execute(goup.dataGenerator(), ticks=steps-1, postTick=history.update) + + #TODO: test imagination automatically + + #go one whole loop to give it a chance to figure out what's going on + h.execute(goright.dataGenerator(), ticks=14*10-1, postTick=history.update) + + #do three steps of block starting left and moving right + h.execute(dataGenerator=goright.dataGenerator(), ticks=4, + learning=False, postTick=InputReflectionOverlayDisplay.showNow) + #the displays here should be mostly green and black, but a bit of purple and red is ok + + InputCellsDisplay.showNow(h) + + #imagine 9 more steps +# for _ in xrange(9): +# h.imagineNext() +# InputReflectionOverlayDisplay.showNow(h) +# history.update(h) + + InputCellsDisplay.showNow(h) + + ActivityOverTimeDisplay.showNow(history.data) + + #downstream the last step to project back into the input, + #see if you have a block that is on the right side + h._imagineStimulate(h.columns) + allStimulation = [cell.stimulation for row in h._inputCells for cell in row] + maxStim = max(allStimulation) + + white = (255,255,255) + black = (0,0,0) + percentSynapsesForActivation = FRACTION_SEGMENT_ACTIVATION_THRESHOLD + def stimToRGB(stim): + percentStimulated = stim/maxStim if maxStim else 0 + triggered = percentStimulated >= percentSynapsesForActivation + red = 0 + green = int(percentStimulated*255) + blue = 255 if triggered else 0 + return (red, green, blue) + + img = ImageBuilder([h.inputWidth, h.inputLength], stimToRGB) + img.setData(allStimulation) + img.show() + +if __name__ == "__main__": + #import sys;sys.argv = ['', 'Test.testInit'] + unittest.main() diff --git a/src/htm_main.py b/src/htm_main.py index 7c9fe0e..7fb1795 100644 --- a/src/htm_main.py +++ b/src/htm_main.py @@ -7,7 +7,7 @@ from carver.htm import HTM from carver.htm.ui.excite_history import ExciteHistory from copy import deepcopy -from carver.htm.io.image_builder import ImageBuilder +from carver.htm.io.image_builder import ImageBuilder, ActivityOverTimeDisplay def flipDataGenerator(htm): @@ -41,35 +41,13 @@ def flipDataGenerator(htm): #track htm's data history with history = ExciteHistory() - htm.execute(flipDataGenerator(htm), ticks=180, postTick=history.update) - - print """*************** Graph History ************** -Y-Axis: All cells in the network, with the 4 cells per column grouped together -X-Axis: Time -Colors: -\tblack: no activity -\tgray: predicting -\twhite: active + htm.execute(flipDataGenerator(htm), ticks=50, postTick=history.update) + ## Show image with history grouped by cell + ActivityOverTimeDisplay.showNow(history.data) + + print """ Notice that the network settles down very quickly at the left, but not completely. You will typically see artifacts around 100 steps in (about halfway across the image). -At each time step, the input data is flipping bits. So you will see some cells alternating at every time step, some cells that are active either way, and some cells that are never active. +At each time step, the input data is flipping bits. So you will see some cells alternating at every time step, some cells that are active either way, and some cells that are never active. """ - - ## Show image with history grouped by cell - columnStates = history.data - - def stateToRGB(state): - if state == ExciteHistory.ACTIVE: - return (255, 255, 255) - elif state == ExciteHistory.PREDICTING: - return (127, 127, 127) - elif state == ExciteHistory.INACTIVE: - return (0, 0, 0) - else: - return (255, 0, 0) #unknown cell/column state - - img = ImageBuilder([len(columnStates[0]), len(columnStates)], stateToRGB) - img.setData2D(columnStates) - img.rotate90() - img.show() diff --git a/src/mock.py b/src/mock.py index 7c20056..f5efa64 100644 --- a/src/mock.py +++ b/src/mock.py @@ -1,465 +1,465 @@ -# -# (c) Dave Kirby 2001 - 2005 -# mock@thedeveloperscoach.com -# -# Original call interceptor and call assertion code by Phil Dawes (pdawes@users.sourceforge.net) -# Call interceptor code enhanced by Bruce Cropley (cropleyb@yahoo.com.au) -# -# This Python module and associated files are released under the FreeBSD -# license. Essentially, you can do what you like with it except pretend you wrote -# it yourself. -# -# -# Copyright (c) 2005, Dave Kirby -# -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# -# * Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# -# * Neither the name of this library nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# -# mock@thedeveloperscoach.com - - -""" -Mock object library for Python. Mock objects can be used when unit testing -to remove a dependency on another production class. They are typically used -when the dependency would either pull in lots of other classes, or -significantly slow down the execution of the test. -They are also used to create exceptional conditions that cannot otherwise -be easily triggered in the class under test. -""" - -__version__ = "0.1.0" - -# Added in Python 2.1 -import inspect -import re - -class MockInterfaceError(Exception): - pass - -class Mock: - """ - The Mock class emulates any other class for testing purposes. - All method calls are stored for later examination. - """ - - def __init__(self, returnValues=None, realClass=None): - """ - The Mock class constructor takes a dictionary of method names and - the values they return. Methods that are not in the returnValues - dictionary will return None. - You may also supply a class whose interface is being mocked. - All calls will be checked to see if they appear in the original - interface. Any calls to methods not appearing in the real class - will raise a MockInterfaceError. Any calls that would fail due to - non-matching parameter lists will also raise a MockInterfaceError. - Both of these help to prevent the Mock class getting out of sync - with the class it is Mocking. - """ - self.mockCalledMethods = {} - self.mockAllCalledMethods = [] - self.mockReturnValues = returnValues or {} - self.mockExpectations = {} - self.realClassMethods = None - if realClass: - self.realClassMethods = dict(inspect.getmembers(realClass, inspect.isroutine)) - for retMethod in self.mockReturnValues.keys(): - if not self.realClassMethods.has_key(retMethod): - raise MockInterfaceError("Return value supplied for method '%s' that was not in the original class" % retMethod) - self._setupSubclassMethodInterceptors() - - def _setupSubclassMethodInterceptors(self): - methods = inspect.getmembers(self.__class__,inspect.isroutine) - baseMethods = dict(inspect.getmembers(Mock, inspect.ismethod)) - for m in methods: - name = m[0] - # Don't record calls to methods of Mock base class. - if not name in baseMethods: - self.__dict__[name] = MockCallable(name, self, handcrafted=True) - - def __getattr__(self, name): - return MockCallable(name, self) - - def mockAddReturnValues(self, **methodReturnValues ): - self.mockReturnValues.update(methodReturnValues) - - def mockSetExpectation(self, name, testFn, after=0, until=0): - self.mockExpectations.setdefault(name, []).append((testFn,after,until)) - - def _checkInterfaceCall(self, name, callParams, callKwParams): - """ - Check that a call to a method of the given name to the original - class with the given parameters would not fail. If it would fail, - raise a MockInterfaceError. - Based on the Python 2.3.3 Reference Manual section 5.3.4: Calls. - """ - if self.realClassMethods == None: - return - if not self.realClassMethods.has_key(name): - raise MockInterfaceError("Calling mock method '%s' that was not found in the original class" % name) - - func = self.realClassMethods[name] - try: - args, varargs, varkw, defaults = inspect.getargspec(func) - except TypeError: - # func is not a Python function. It is probably a builtin, - # such as __repr__ or __coerce__. TODO: Checking? - # For now assume params are OK. - return - - # callParams doesn't include self; args does include self. - numPosCallParams = 1 + len(callParams) - - if numPosCallParams > len(args) and not varargs: - raise MockInterfaceError("Original %s() takes at most %s arguments (%s given)" % - (name, len(args), numPosCallParams)) - - # Get the number of positional arguments that appear in the call, - # also check for duplicate parameters and unknown parameters - numPosSeen = _getNumPosSeenAndCheck(numPosCallParams, callKwParams, args, varkw) - - lenArgsNoDefaults = len(args) - len(defaults or []) - if numPosSeen < lenArgsNoDefaults: - raise MockInterfaceError("Original %s() takes at least %s arguments (%s given)" % (name, lenArgsNoDefaults, numPosSeen)) - - def mockGetAllCalls(self): - """ - Return a list of MockCall objects, - representing all the methods in the order they were called. - """ - return self.mockAllCalledMethods - getAllCalls = mockGetAllCalls # deprecated - kept for backward compatibility - - def mockGetNamedCalls(self, methodName): - """ - Return a list of MockCall objects, - representing all the calls to the named method in the order they were called. - """ - return self.mockCalledMethods.get(methodName, []) - getNamedCalls = mockGetNamedCalls # deprecated - kept for backward compatibility - - def mockCheckCall(self, index, name, *args, **kwargs): - '''test that the index-th call had the specified name and parameters''' - call = self.mockAllCalledMethods[index] - assert name == call.getName(), "%r != %r" % (name, call.getName()) - call.checkArgs(*args, **kwargs) - - -def _getNumPosSeenAndCheck(numPosCallParams, callKwParams, args, varkw): - """ - Positional arguments can appear as call parameters either named as - a named (keyword) parameter, or just as a value to be matched by - position. Count the positional arguments that are given by either - keyword or position, and check for duplicate specifications. - Also check for arguments specified by keyword that do not appear - in the method's parameter list. - """ - posSeen = {} - for arg in args[:numPosCallParams]: - posSeen[arg] = True - for kwp in callKwParams: - if posSeen.has_key(kwp): - raise MockInterfaceError("%s appears as both a positional and named parameter." % kwp) - if kwp in args: - posSeen[kwp] = True - elif not varkw: - raise MockInterfaceError("Original method does not have a parameter '%s'" % kwp) - return len(posSeen) - -class MockCall: - """ - MockCall records the name and parameters of a call to an instance - of a Mock class. Instances of MockCall are created by the Mock class, - but can be inspected later as part of the test. - """ - def __init__(self, name, params, kwparams ): - self.name = name - self.params = params - self.kwparams = kwparams - - def checkArgs(self, *args, **kwargs): - assert args == self.params, "%r != %r" % (args, self.params) - assert kwargs == self.kwparams, "%r != %r" % (kwargs, self.kwparams) - - def getParam( self, n ): - if isinstance(n, int): - return self.params[n] - elif isinstance(n, str): - return self.kwparams[n] - else: - raise IndexError, 'illegal index type for getParam' - - def getNumParams(self): - return len(self.params) - - def getNumKwParams(self): - return len(self.kwparams) - - def getName(self): - return self.name - - #pretty-print the method call - def __str__(self): - s = self.name + "(" - sep = '' - for p in self.params: - s = s + sep + repr(p) - sep = ', ' - items = self.kwparams.items() - items.sort() - for k,v in items: - s = s + sep + k + '=' + repr(v) - sep = ', ' - s = s + ')' - return s - def __repr__(self): - return self.__str__() - -class MockCallable: - """ - Intercepts the call and records it, then delegates to either the mock's - dictionary of mock return values that was passed in to the constructor, - or a handcrafted method of a Mock subclass. - """ - def __init__(self, name, mock, handcrafted=False): - self.name = name - self.mock = mock - self.handcrafted = handcrafted - - def __call__(self, *params, **kwparams): - self.mock._checkInterfaceCall(self.name, params, kwparams) - thisCall = self.recordCall(params,kwparams) - self.checkExpectations(thisCall, params, kwparams) - return self.makeCall(params, kwparams) - - def recordCall(self, params, kwparams): - """ - Record the MockCall in an ordered list of all calls, and an ordered - list of calls for that method name. - """ - thisCall = MockCall(self.name, params, kwparams) - calls = self.mock.mockCalledMethods.setdefault(self.name, []) - calls.append(thisCall) - self.mock.mockAllCalledMethods.append(thisCall) - return thisCall - - def makeCall(self, params, kwparams): - if self.handcrafted: - allPosParams = (self.mock,) + params - func = _findFunc(self.mock.__class__, self.name) - if not func: - raise NotImplementedError - return func(*allPosParams, **kwparams) - else: - returnVal = self.mock.mockReturnValues.get(self.name) - if isinstance(returnVal, ReturnValuesBase): - returnVal = returnVal.next() - return returnVal - - def checkExpectations(self, thisCall, params, kwparams): - if self.name in self.mock.mockExpectations: - callsMade = len(self.mock.mockCalledMethods[self.name]) - for (expectation, after, until) in self.mock.mockExpectations[self.name]: - if callsMade > after and (until==0 or callsMade < until): - assert expectation(self.mock, thisCall, len(self.mock.mockAllCalledMethods)-1), 'Expectation failed: '+str(thisCall) - - -def _findFunc(cl, name): - """ Depth first search for a method with a given name. """ - if cl.__dict__.has_key(name): - return cl.__dict__[name] - for base in cl.__bases__: - func = _findFunc(base, name) - if func: - return func - return None - - - -class ReturnValuesBase: - def next(self): - try: - return self.iter.next() - except StopIteration: - raise AssertionError("No more return values") - def __iter__(self): - return self - -class ReturnValues(ReturnValuesBase): - def __init__(self, *values): - self.iter = iter(values) - - -class ReturnIterator(ReturnValuesBase): - def __init__(self, iterator): - self.iter = iter(iterator) - - -def expectParams(*params, **keywords): - '''check that the callObj is called with specified params and keywords - ''' - def fn(mockObj, callObj, idx): - return callObj.params == params and callObj.kwparams == keywords - return fn - - -def expectAfter(*methods): - '''check that the function is only called after all the functions in 'methods' - ''' - def fn(mockObj, callObj, idx): - calledMethods = [method.getName() for method in mockObj.mockGetAllCalls()] - #skip last entry, since that is the current call - calledMethods = calledMethods[:-1] - for method in methods: - if method not in calledMethods: - return False - return True - return fn - -def expectException(exception, *args, **kwargs): - ''' raise an exception when the method is called - ''' - def fn(mockObj, callObj, idx): - raise exception(*args, **kwargs) - return fn - - -def expectParam(paramIdx, cond): - '''check that the callObj is called with parameter specified by paramIdx (a position index or keyword) - fulfills the condition specified by cond. - cond is a function that takes a single argument, the value to test. - ''' - def fn(mockObj, callObj, idx): - param = callObj.getParam(paramIdx) - return cond(param) - return fn - -def EQ(value): - def testFn(param): - return param == value - return testFn - -def NE(value): - def testFn(param): - return param != value - return testFn - -def GT(value): - def testFn(param): - return param > value - return testFn - -def LT(value): - def testFn(param): - return param < value - return testFn - -def GE(value): - def testFn(param): - return param >= value - return testFn - -def LE(value): - def testFn(param): - return param <= value - return testFn - -def AND(*condlist): - def testFn(param): - for cond in condlist: - if not cond(param): - return False - return True - return testFn - -def OR(*condlist): - def testFn(param): - for cond in condlist: - if cond(param): - return True - return False - return testFn - -def NOT(cond): - def testFn(param): - return not cond(param) - return testFn - -def MATCHES(regex, *args, **kwargs): - compiled_regex = re.compile(regex, *args, **kwargs) - def testFn(param): - return compiled_regex.match(param) != None - return testFn - -def SEQ(*sequence): - iterator = iter(sequence) - def testFn(param): - try: - cond = iterator.next() - except StopIteration: - raise AssertionError('SEQ exhausted') - return cond(param) - return testFn - -def IS(instance): - def testFn(param): - return param is instance - return testFn - -def ISINSTANCE(class_): - def testFn(param): - return isinstance(param, class_) - return testFn - -def ISSUBCLASS(class_): - def testFn(param): - return issubclass(param, class_) - return testFn - -def CONTAINS(val): - def testFn(param): - return val in param - return testFn - -def IN(container): - def testFn(param): - return param in container - return testFn - -def HASATTR(attr): - def testFn(param): - return hasattr(param, attr) - return testFn - -def HASMETHOD(method): - def testFn(param): - return hasattr(param, method) and callable(getattr(param, method)) - return testFn - -CALLABLE = callable - - - +# +# (c) Dave Kirby 2001 - 2005 +# mock@thedeveloperscoach.com +# +# Original call interceptor and call assertion code by Phil Dawes (pdawes@users.sourceforge.net) +# Call interceptor code enhanced by Bruce Cropley (cropleyb@yahoo.com.au) +# +# This Python module and associated files are released under the FreeBSD +# license. Essentially, you can do what you like with it except pretend you wrote +# it yourself. +# +# +# Copyright (c) 2005, Dave Kirby +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# * Neither the name of this library nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# mock@thedeveloperscoach.com + + +""" +Mock object library for Python. Mock objects can be used when unit testing +to remove a dependency on another production class. They are typically used +when the dependency would either pull in lots of other classes, or +significantly slow down the execution of the test. +They are also used to create exceptional conditions that cannot otherwise +be easily triggered in the class under test. +""" + +__version__ = "0.1.0" + +# Added in Python 2.1 +import inspect +import re + +class MockInterfaceError(Exception): + pass + +class Mock: + """ + The Mock class emulates any other class for testing purposes. + All method calls are stored for later examination. + """ + + def __init__(self, returnValues=None, realClass=None): + """ + The Mock class constructor takes a dictionary of method names and + the values they return. Methods that are not in the returnValues + dictionary will return None. + You may also supply a class whose interface is being mocked. + All calls will be checked to see if they appear in the original + interface. Any calls to methods not appearing in the real class + will raise a MockInterfaceError. Any calls that would fail due to + non-matching parameter lists will also raise a MockInterfaceError. + Both of these help to prevent the Mock class getting out of sync + with the class it is Mocking. + """ + self.mockCalledMethods = {} + self.mockAllCalledMethods = [] + self.mockReturnValues = returnValues or {} + self.mockExpectations = {} + self.realClassMethods = None + if realClass: + self.realClassMethods = dict(inspect.getmembers(realClass, inspect.isroutine)) + for retMethod in self.mockReturnValues.keys(): + if not self.realClassMethods.has_key(retMethod): + raise MockInterfaceError("Return value supplied for method '%s' that was not in the original class" % retMethod) + self._setupSubclassMethodInterceptors() + + def _setupSubclassMethodInterceptors(self): + methods = inspect.getmembers(self.__class__,inspect.isroutine) + baseMethods = dict(inspect.getmembers(Mock, inspect.ismethod)) + for m in methods: + name = m[0] + # Don't record calls to methods of Mock base class. + if not name in baseMethods: + self.__dict__[name] = MockCallable(name, self, handcrafted=True) + + def __getattr__(self, name): + return MockCallable(name, self) + + def mockAddReturnValues(self, **methodReturnValues ): + self.mockReturnValues.update(methodReturnValues) + + def mockSetExpectation(self, name, testFn, after=0, until=0): + self.mockExpectations.setdefault(name, []).append((testFn,after,until)) + + def _checkInterfaceCall(self, name, callParams, callKwParams): + """ + Check that a call to a method of the given name to the original + class with the given parameters would not fail. If it would fail, + raise a MockInterfaceError. + Based on the Python 2.3.3 Reference Manual section 5.3.4: Calls. + """ + if self.realClassMethods == None: + return + if not self.realClassMethods.has_key(name): + raise MockInterfaceError("Calling mock method '%s' that was not found in the original class" % name) + + func = self.realClassMethods[name] + try: + args, varargs, varkw, defaults = inspect.getargspec(func) + except TypeError: + # func is not a Python function. It is probably a builtin, + # such as __repr__ or __coerce__. TODO: Checking? + # For now assume params are OK. + return + + # callParams doesn't include self; args does include self. + numPosCallParams = 1 + len(callParams) + + if numPosCallParams > len(args) and not varargs: + raise MockInterfaceError("Original %s() takes at most %s arguments (%s given)" % + (name, len(args), numPosCallParams)) + + # Get the number of positional arguments that appear in the call, + # also check for duplicate parameters and unknown parameters + numPosSeen = _getNumPosSeenAndCheck(numPosCallParams, callKwParams, args, varkw) + + lenArgsNoDefaults = len(args) - len(defaults or []) + if numPosSeen < lenArgsNoDefaults: + raise MockInterfaceError("Original %s() takes at least %s arguments (%s given)" % (name, lenArgsNoDefaults, numPosSeen)) + + def mockGetAllCalls(self): + """ + Return a list of MockCall objects, + representing all the methods in the order they were called. + """ + return self.mockAllCalledMethods + getAllCalls = mockGetAllCalls # deprecated - kept for backward compatibility + + def mockGetNamedCalls(self, methodName): + """ + Return a list of MockCall objects, + representing all the calls to the named method in the order they were called. + """ + return self.mockCalledMethods.get(methodName, []) + getNamedCalls = mockGetNamedCalls # deprecated - kept for backward compatibility + + def mockCheckCall(self, index, name, *args, **kwargs): + '''test that the index-th call had the specified name and parameters''' + call = self.mockAllCalledMethods[index] + assert name == call.getName(), "%r != %r" % (name, call.getName()) + call.checkArgs(*args, **kwargs) + + +def _getNumPosSeenAndCheck(numPosCallParams, callKwParams, args, varkw): + """ + Positional arguments can appear as call parameters either named as + a named (keyword) parameter, or just as a value to be matched by + position. Count the positional arguments that are given by either + keyword or position, and check for duplicate specifications. + Also check for arguments specified by keyword that do not appear + in the method's parameter list. + """ + posSeen = {} + for arg in args[:numPosCallParams]: + posSeen[arg] = True + for kwp in callKwParams: + if posSeen.has_key(kwp): + raise MockInterfaceError("%s appears as both a positional and named parameter." % kwp) + if kwp in args: + posSeen[kwp] = True + elif not varkw: + raise MockInterfaceError("Original method does not have a parameter '%s'" % kwp) + return len(posSeen) + +class MockCall: + """ + MockCall records the name and parameters of a call to an instance + of a Mock class. Instances of MockCall are created by the Mock class, + but can be inspected later as part of the test. + """ + def __init__(self, name, params, kwparams ): + self.name = name + self.params = params + self.kwparams = kwparams + + def checkArgs(self, *args, **kwargs): + assert args == self.params, "%r != %r" % (args, self.params) + assert kwargs == self.kwparams, "%r != %r" % (kwargs, self.kwparams) + + def getParam( self, n ): + if isinstance(n, int): + return self.params[n] + elif isinstance(n, str): + return self.kwparams[n] + else: + raise IndexError, 'illegal index type for getParam' + + def getNumParams(self): + return len(self.params) + + def getNumKwParams(self): + return len(self.kwparams) + + def getName(self): + return self.name + + #pretty-print the method call + def __str__(self): + s = self.name + "(" + sep = '' + for p in self.params: + s = s + sep + repr(p) + sep = ', ' + items = self.kwparams.items() + items.sort() + for k,v in items: + s = s + sep + k + '=' + repr(v) + sep = ', ' + s = s + ')' + return s + def __repr__(self): + return self.__str__() + +class MockCallable: + """ + Intercepts the call and records it, then delegates to either the mock's + dictionary of mock return values that was passed in to the constructor, + or a handcrafted method of a Mock subclass. + """ + def __init__(self, name, mock, handcrafted=False): + self.name = name + self.mock = mock + self.handcrafted = handcrafted + + def __call__(self, *params, **kwparams): + self.mock._checkInterfaceCall(self.name, params, kwparams) + thisCall = self.recordCall(params,kwparams) + self.checkExpectations(thisCall, params, kwparams) + return self.makeCall(params, kwparams) + + def recordCall(self, params, kwparams): + """ + Record the MockCall in an ordered list of all calls, and an ordered + list of calls for that method name. + """ + thisCall = MockCall(self.name, params, kwparams) + calls = self.mock.mockCalledMethods.setdefault(self.name, []) + calls.append(thisCall) + self.mock.mockAllCalledMethods.append(thisCall) + return thisCall + + def makeCall(self, params, kwparams): + if self.handcrafted: + allPosParams = (self.mock,) + params + func = _findFunc(self.mock.__class__, self.name) + if not func: + raise NotImplementedError + return func(*allPosParams, **kwparams) + else: + returnVal = self.mock.mockReturnValues.get(self.name) + if isinstance(returnVal, ReturnValuesBase): + returnVal = returnVal.next() + return returnVal + + def checkExpectations(self, thisCall, params, kwparams): + if self.name in self.mock.mockExpectations: + callsMade = len(self.mock.mockCalledMethods[self.name]) + for (expectation, after, until) in self.mock.mockExpectations[self.name]: + if callsMade > after and (until==0 or callsMade < until): + assert expectation(self.mock, thisCall, len(self.mock.mockAllCalledMethods)-1), 'Expectation failed: '+str(thisCall) + + +def _findFunc(cl, name): + """ Depth first search for a method with a given name. """ + if cl.__dict__.has_key(name): + return cl.__dict__[name] + for base in cl.__bases__: + func = _findFunc(base, name) + if func: + return func + return None + + + +class ReturnValuesBase: + def next(self): + try: + return self.iter.next() + except StopIteration: + raise AssertionError("No more return values") + def __iter__(self): + return self + +class ReturnValues(ReturnValuesBase): + def __init__(self, *values): + self.iter = iter(values) + + +class ReturnIterator(ReturnValuesBase): + def __init__(self, iterator): + self.iter = iter(iterator) + + +def expectParams(*params, **keywords): + '''check that the callObj is called with specified params and keywords + ''' + def fn(mockObj, callObj, idx): + return callObj.params == params and callObj.kwparams == keywords + return fn + + +def expectAfter(*methods): + '''check that the function is only called after all the functions in 'methods' + ''' + def fn(mockObj, callObj, idx): + calledMethods = [method.getName() for method in mockObj.mockGetAllCalls()] + #skip last entry, since that is the current call + calledMethods = calledMethods[:-1] + for method in methods: + if method not in calledMethods: + return False + return True + return fn + +def expectException(exception, *args, **kwargs): + ''' raise an exception when the method is called + ''' + def fn(mockObj, callObj, idx): + raise exception(*args, **kwargs) + return fn + + +def expectParam(paramIdx, cond): + '''check that the callObj is called with parameter specified by paramIdx (a position index or keyword) + fulfills the condition specified by cond. + cond is a function that takes a single argument, the value to test. + ''' + def fn(mockObj, callObj, idx): + param = callObj.getParam(paramIdx) + return cond(param) + return fn + +def EQ(value): + def testFn(param): + return param == value + return testFn + +def NE(value): + def testFn(param): + return param != value + return testFn + +def GT(value): + def testFn(param): + return param > value + return testFn + +def LT(value): + def testFn(param): + return param < value + return testFn + +def GE(value): + def testFn(param): + return param >= value + return testFn + +def LE(value): + def testFn(param): + return param <= value + return testFn + +def AND(*condlist): + def testFn(param): + for cond in condlist: + if not cond(param): + return False + return True + return testFn + +def OR(*condlist): + def testFn(param): + for cond in condlist: + if cond(param): + return True + return False + return testFn + +def NOT(cond): + def testFn(param): + return not cond(param) + return testFn + +def MATCHES(regex, *args, **kwargs): + compiled_regex = re.compile(regex, *args, **kwargs) + def testFn(param): + return compiled_regex.match(param) != None + return testFn + +def SEQ(*sequence): + iterator = iter(sequence) + def testFn(param): + try: + cond = iterator.next() + except StopIteration: + raise AssertionError('SEQ exhausted') + return cond(param) + return testFn + +def IS(instance): + def testFn(param): + return param is instance + return testFn + +def ISINSTANCE(class_): + def testFn(param): + return isinstance(param, class_) + return testFn + +def ISSUBCLASS(class_): + def testFn(param): + return issubclass(param, class_) + return testFn + +def CONTAINS(val): + def testFn(param): + return val in param + return testFn + +def IN(container): + def testFn(param): + return param in container + return testFn + +def HASATTR(attr): + def testFn(param): + return hasattr(param, attr) + return testFn + +def HASMETHOD(method): + def testFn(param): + return hasattr(param, method) and callable(getattr(param, method)) + return testFn + +CALLABLE = callable + + + diff --git a/src/numenta/htm.py b/src/numenta/htm.py index b9f6281..62c3a99 100644 --- a/src/numenta/htm.py +++ b/src/numenta/htm.py @@ -106,7 +106,7 @@ def _temporal_phase1(htm, learning, updateSegments): learningCellChosen = False for cell in col.cells: if cell.predicted: - seg = cell.findSegmentWasActive(nextStep=True) + seg = cell.findSegmentWasActive(nextStep=False) #distal dendrite segments = sequence memory if seg and seg.distal: @@ -124,7 +124,7 @@ def _temporal_phase1(htm, learning, updateSegments): #Learning Phase 1, p41 if learning and not learningCellChosen: - cell, seg = col.bestCell(nextStep=True) + cell, seg = col.bestCell(nextStep=False) cell.learning = True if seg is None: