Search is not available for this dataset
text
stringlengths
75
104k
def _getAdditionalSpecs(temporalImp, kwargs={}): """Build the additional specs in three groups (for the inspector) Use the type of the default argument to set the Spec type, defaulting to 'Byte' for None and complex types Determines the spatial parameters based on the selected implementation. It defaults to TemporalMemory. Determines the temporal parameters based on the temporalImp """ typeNames = {int: 'UInt32', float: 'Real32', str: 'Byte', bool: 'bool', tuple: 'tuple'} def getArgType(arg): t = typeNames.get(type(arg), 'Byte') count = 0 if t == 'Byte' else 1 if t == 'tuple': t = typeNames.get(type(arg[0]), 'Byte') count = len(arg) if t == 'bool': t = 'UInt32' return (t, count) def getConstraints(arg): t = typeNames.get(type(arg), 'Byte') if t == 'Byte': return 'multiple' elif t == 'bool': return 'bool' else: return '' # Build up parameters from temporal memory's constructor TemporalClass = _getTPClass(temporalImp) tArgTuples = _buildArgs(TemporalClass.__init__) temporalSpec = {} for argTuple in tArgTuples: d = dict( description=argTuple[1], accessMode='ReadWrite', dataType=getArgType(argTuple[2])[0], count=getArgType(argTuple[2])[1], constraints=getConstraints(argTuple[2])) temporalSpec[argTuple[0]] = d # Add temporal parameters that weren't handled automatically temporalSpec.update(dict( columnCount=dict( description='Total number of columns.', accessMode='Read', dataType='UInt32', count=1, constraints=''), cellsPerColumn=dict( description='Number of cells per column.', accessMode='Read', dataType='UInt32', count=1, constraints=''), inputWidth=dict( description='Number of inputs to the TM.', accessMode='Read', dataType='UInt32', count=1, constraints=''), predictedSegmentDecrement=dict( description='Predicted segment decrement', accessMode='Read', dataType='Real', count=1, constraints=''), orColumnOutputs=dict( description="""OR together the cell outputs from each column to produce the temporal memory output. When this mode is enabled, the number of cells per column must also be specified and the output size of the region should be set the same as columnCount""", accessMode='Read', dataType='Bool', count=1, constraints='bool'), cellsSavePath=dict( description="""Optional path to file in which large temporal memory cells data structure is to be saved.""", accessMode='ReadWrite', dataType='Byte', count=0, constraints=''), temporalImp=dict( description="""Which temporal memory implementation to use. Set to either 'py' or 'cpp'. The 'cpp' implementation is optimized for speed in C++.""", accessMode='ReadWrite', dataType='Byte', count=0, constraints='enum: py, cpp'), )) # The last group is for parameters that aren't strictly spatial or temporal otherSpec = dict( learningMode=dict( description='True if the node is learning (default True).', accessMode='ReadWrite', dataType='Bool', count=1, defaultValue=True, constraints='bool'), inferenceMode=dict( description='True if the node is inferring (default False).', accessMode='ReadWrite', dataType='Bool', count=1, defaultValue=False, constraints='bool'), computePredictedActiveCellIndices=dict( description='True if active and predicted active indices should be computed', accessMode='Create', dataType='Bool', count=1, defaultValue=False, constraints='bool'), anomalyMode=dict( description='True if an anomaly score is being computed', accessMode='Create', dataType='Bool', count=1, defaultValue=False, constraints='bool'), topDownMode=dict( description='True if the node should do top down compute on the next call ' 'to compute into topDownOut (default False).', accessMode='ReadWrite', dataType='Bool', count=1, defaultValue=False, constraints='bool'), activeOutputCount=dict( description='Number of active elements in bottomUpOut output.', accessMode='Read', dataType='UInt32', count=1, constraints=''), storeDenseOutput=dict( description="""Whether to keep the dense column output (needed for denseOutput parameter).""", accessMode='ReadWrite', dataType='UInt32', count=1, constraints='bool'), logPathOutput=dict( description='Optional name of output log file. If set, every output vector' ' will be logged to this file as a sparse vector.', accessMode='ReadWrite', dataType='Byte', count=0, constraints=''), ) return temporalSpec, otherSpec
def initialize(self): """ Overrides :meth:`~nupic.bindings.regions.PyRegion.initialize`. """ # Allocate appropriate temporal memory object # Retrieve the necessary extra arguments that were handled automatically autoArgs = dict((name, getattr(self, name)) for name in self._temporalArgNames) if self._tfdr is None: tpClass = _getTPClass(self.temporalImp) if self.temporalImp in ['py', 'cpp', 'r', 'tm_py', 'tm_cpp', 'monitored_tm_py',]: self._tfdr = tpClass( numberOfCols=self.columnCount, cellsPerColumn=self.cellsPerColumn, **autoArgs) else: raise RuntimeError("Invalid temporalImp")
def _compute(self, inputs, outputs): """ Run one iteration of TMRegion's compute """ #if self.topDownMode and (not 'topDownIn' in inputs): # raise RuntimeError("The input topDownIn must be linked in if " # "topDownMode is True") if self._tfdr is None: raise RuntimeError("TM has not been initialized") # Conditional compute break self._conditionalBreak() self._iterations += 1 # Get our inputs as numpy array buInputVector = inputs['bottomUpIn'] # Handle reset signal resetSignal = False if 'resetIn' in inputs: assert len(inputs['resetIn']) == 1 if inputs['resetIn'][0] != 0: self._tfdr.reset() self._sequencePos = 0 # Position within the current sequence if self.computePredictedActiveCellIndices: prevPredictedState = self._tfdr.getPredictedState().reshape(-1).astype('float32') if self.anomalyMode: prevPredictedColumns = self._tfdr.topDownCompute().copy().nonzero()[0] # Perform inference and/or learning tpOutput = self._tfdr.compute(buInputVector, self.learningMode, self.inferenceMode) self._sequencePos += 1 # OR'ing together the cells in each column? if self.orColumnOutputs: tpOutput= tpOutput.reshape(self.columnCount, self.cellsPerColumn).max(axis=1) # Direct logging of non-zero TM outputs if self._fpLogTPOutput: output = tpOutput.reshape(-1) outputNZ = tpOutput.nonzero()[0] outStr = " ".join(["%d" % int(token) for token in outputNZ]) print >>self._fpLogTPOutput, output.size, outStr # Write the bottom up out to our node outputs outputs['bottomUpOut'][:] = tpOutput.flat if self.topDownMode: # Top-down compute outputs['topDownOut'][:] = self._tfdr.topDownCompute().copy() # Set output for use with anomaly classification region if in anomalyMode if self.anomalyMode: activeLearnCells = self._tfdr.getLearnActiveStateT() size = activeLearnCells.shape[0] * activeLearnCells.shape[1] outputs['lrnActiveStateT'][:] = activeLearnCells.reshape(size) activeColumns = buInputVector.nonzero()[0] outputs['anomalyScore'][:] = anomaly.computeRawAnomalyScore( activeColumns, prevPredictedColumns) if self.computePredictedActiveCellIndices: # Reshape so we are dealing with 1D arrays activeState = self._tfdr._getActiveState().reshape(-1).astype('float32') activeIndices = numpy.where(activeState != 0)[0] predictedIndices= numpy.where(prevPredictedState != 0)[0] predictedActiveIndices = numpy.intersect1d(activeIndices, predictedIndices) outputs["activeCells"].fill(0) outputs["activeCells"][activeIndices] = 1 outputs["predictedActiveCells"].fill(0) outputs["predictedActiveCells"][predictedActiveIndices] = 1
def getBaseSpec(cls): """ Doesn't include the spatial, temporal and other parameters :returns: (dict) the base Spec for TMRegion. """ spec = dict( description=TMRegion.__doc__, singleNodeOnly=True, inputs=dict( bottomUpIn=dict( description="""The input signal, conceptually organized as an image pyramid data structure, but internally organized as a flattened vector.""", dataType='Real32', count=0, required=True, regionLevel=False, isDefaultInput=True, requireSplitterMap=False), resetIn=dict( description="""Effectively a boolean flag that indicates whether or not the input vector received in this compute cycle represents the first training presentation in a new temporal sequence.""", dataType='Real32', count=1, required=False, regionLevel=True, isDefaultInput=False, requireSplitterMap=False), sequenceIdIn=dict( description="Sequence ID", dataType='UInt64', count=1, required=False, regionLevel=True, isDefaultInput=False, requireSplitterMap=False), ), outputs=dict( bottomUpOut=dict( description="""The output signal generated from the bottom-up inputs from lower levels.""", dataType='Real32', count=0, regionLevel=True, isDefaultOutput=True), topDownOut=dict( description="""The top-down inputsignal, generated from feedback from upper levels""", dataType='Real32', count=0, regionLevel=True, isDefaultOutput=False), activeCells=dict( description="The cells that are active", dataType='Real32', count=0, regionLevel=True, isDefaultOutput=False), predictedActiveCells=dict( description="The cells that are active and predicted", dataType='Real32', count=0, regionLevel=True, isDefaultOutput=False), anomalyScore = dict( description="""The score for how 'anomalous' (i.e. rare) the current sequence is. Higher values are increasingly rare""", dataType='Real32', count=1, regionLevel=True, isDefaultOutput=False), lrnActiveStateT = dict( description="""Active cells during learn phase at time t. This is used for anomaly classification.""", dataType='Real32', count=0, regionLevel=True, isDefaultOutput=False), ), parameters=dict( breakPdb=dict( description='Set to 1 to stop in the pdb debugger on the next compute', dataType='UInt32', count=1, constraints='bool', defaultValue=0, accessMode='ReadWrite'), breakKomodo=dict( description='Set to 1 to stop in the Komodo debugger on the next compute', dataType='UInt32', count=1, constraints='bool', defaultValue=0, accessMode='ReadWrite'), ), commands = {} ) return spec
def getSpec(cls): """ Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.getSpec`. The parameters collection is constructed based on the parameters specified by the various components (spatialSpec, temporalSpec and otherSpec) """ spec = cls.getBaseSpec() t, o = _getAdditionalSpecs(temporalImp=gDefaultTemporalImp) spec['parameters'].update(t) spec['parameters'].update(o) return spec
def getParameter(self, parameterName, index=-1): """ Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.getParameter`. Get the value of a parameter. Most parameters are handled automatically by :class:`~nupic.bindings.regions.PyRegion.PyRegion`'s parameter get mechanism. The ones that need special treatment are explicitly handled here. """ if parameterName in self._temporalArgNames: return getattr(self._tfdr, parameterName) else: return PyRegion.getParameter(self, parameterName, index)
def setParameter(self, parameterName, index, parameterValue): """ Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.setParameter`. """ if parameterName in self._temporalArgNames: setattr(self._tfdr, parameterName, parameterValue) elif parameterName == "logPathOutput": self.logPathOutput = parameterValue # Close any existing log file if self._fpLogTPOutput is not None: self._fpLogTPOutput.close() self._fpLogTPOutput = None # Open a new log file if requested if parameterValue: self._fpLogTPOutput = open(self.logPathOutput, 'w') elif hasattr(self, parameterName): setattr(self, parameterName, parameterValue) else: raise Exception('Unknown parameter: ' + parameterName)
def finishLearning(self): """ Perform an internal optimization step that speeds up inference if we know learning will not be performed anymore. This call may, for example, remove all potential inputs to each column. """ if self._tfdr is None: raise RuntimeError("Temporal memory has not been initialized") if hasattr(self._tfdr, 'finishLearning'): self.resetSequenceStates() self._tfdr.finishLearning()
def writeToProto(self, proto): """ Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.writeToProto`. Write state to proto object. :param proto: TMRegionProto capnproto object """ proto.temporalImp = self.temporalImp proto.columnCount = self.columnCount proto.inputWidth = self.inputWidth proto.cellsPerColumn = self.cellsPerColumn proto.learningMode = self.learningMode proto.inferenceMode = self.inferenceMode proto.anomalyMode = self.anomalyMode proto.topDownMode = self.topDownMode proto.computePredictedActiveCellIndices = ( self.computePredictedActiveCellIndices) proto.orColumnOutputs = self.orColumnOutputs if self.temporalImp == "py": tmProto = proto.init("backtrackingTM") elif self.temporalImp == "cpp": tmProto = proto.init("backtrackingTMCpp") elif self.temporalImp == "tm_py": tmProto = proto.init("temporalMemory") elif self.temporalImp == "tm_cpp": tmProto = proto.init("temporalMemory") else: raise TypeError( "Unsupported temporalImp for capnp serialization: {}".format( self.temporalImp)) self._tfdr.write(tmProto)
def readFromProto(cls, proto): """ Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.readFromProto`. Read state from proto object. :param proto: TMRegionProto capnproto object """ instance = cls(proto.columnCount, proto.inputWidth, proto.cellsPerColumn) instance.temporalImp = proto.temporalImp instance.learningMode = proto.learningMode instance.inferenceMode = proto.inferenceMode instance.anomalyMode = proto.anomalyMode instance.topDownMode = proto.topDownMode instance.computePredictedActiveCellIndices = ( proto.computePredictedActiveCellIndices) instance.orColumnOutputs = proto.orColumnOutputs if instance.temporalImp == "py": tmProto = proto.backtrackingTM elif instance.temporalImp == "cpp": tmProto = proto.backtrackingTMCpp elif instance.temporalImp == "tm_py": tmProto = proto.temporalMemory elif instance.temporalImp == "tm_cpp": tmProto = proto.temporalMemory else: raise TypeError( "Unsupported temporalImp for capnp serialization: {}".format( instance.temporalImp)) instance._tfdr = _getTPClass(proto.temporalImp).read(tmProto) return instance
def getOutputElementCount(self, name): """ Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.getOutputElementCount`. """ if name == 'bottomUpOut': return self.outputWidth elif name == 'topDownOut': return self.columnCount elif name == 'lrnActiveStateT': return self.outputWidth elif name == "activeCells": return self.outputWidth elif name == "predictedActiveCells": return self.outputWidth else: raise Exception("Invalid output name specified")
def computeRawAnomalyScore(activeColumns, prevPredictedColumns): """Computes the raw anomaly score. The raw anomaly score is the fraction of active columns not predicted. :param activeColumns: array of active column indices :param prevPredictedColumns: array of columns indices predicted in prev step :returns: anomaly score 0..1 (float) """ nActiveColumns = len(activeColumns) if nActiveColumns > 0: # Test whether each element of a 1-D array is also present in a second # array. Sum to get the total # of columns that are active and were # predicted. score = numpy.in1d(activeColumns, prevPredictedColumns).sum() # Get the percent of active columns that were NOT predicted, that is # our anomaly score. score = (nActiveColumns - score) / float(nActiveColumns) else: # There are no active columns. score = 0.0 return score
def compute(self, activeColumns, predictedColumns, inputValue=None, timestamp=None): """Compute the anomaly score as the percent of active columns not predicted. :param activeColumns: array of active column indices :param predictedColumns: array of columns indices predicted in this step (used for anomaly in step T+1) :param inputValue: (optional) value of current input to encoders (eg "cat" for category encoder) (used in anomaly-likelihood) :param timestamp: (optional) date timestamp when the sample occured (used in anomaly-likelihood) :returns: the computed anomaly score; float 0..1 """ # Start by computing the raw anomaly score. anomalyScore = computeRawAnomalyScore(activeColumns, predictedColumns) # Compute final anomaly based on selected mode. if self._mode == Anomaly.MODE_PURE: score = anomalyScore elif self._mode == Anomaly.MODE_LIKELIHOOD: if inputValue is None: raise ValueError("Selected anomaly mode 'Anomaly.MODE_LIKELIHOOD' " "requires 'inputValue' as parameter to compute() method. ") probability = self._likelihood.anomalyProbability( inputValue, anomalyScore, timestamp) # low likelihood -> hi anomaly score = 1 - probability elif self._mode == Anomaly.MODE_WEIGHTED: probability = self._likelihood.anomalyProbability( inputValue, anomalyScore, timestamp) score = anomalyScore * (1 - probability) # Last, do moving-average if windowSize was specified. if self._movingAverage is not None: score = self._movingAverage.next(score) # apply binary discretization if required if self._binaryThreshold is not None: if score >= self._binaryThreshold: score = 1.0 else: score = 0.0 return score
def addGraph(self, data, position=111, xlabel=None, ylabel=None): """ Adds a graph to the plot's figure. @param data See matplotlib.Axes.plot documentation. @param position A 3-digit number. The first two digits define a 2D grid where subplots may be added. The final digit specifies the nth grid location for the added subplot @param xlabel text to be displayed on the x-axis @param ylabel text to be displayed on the y-axis """ ax = self._addBase(position, xlabel=xlabel, ylabel=ylabel) ax.plot(data) plt.draw()
def addHistogram(self, data, position=111, xlabel=None, ylabel=None, bins=None): """ Adds a histogram to the plot's figure. @param data See matplotlib.Axes.hist documentation. @param position A 3-digit number. The first two digits define a 2D grid where subplots may be added. The final digit specifies the nth grid location for the added subplot @param xlabel text to be displayed on the x-axis @param ylabel text to be displayed on the y-axis """ ax = self._addBase(position, xlabel=xlabel, ylabel=ylabel) ax.hist(data, bins=bins, color="green", alpha=0.8) plt.draw()
def add2DArray(self, data, position=111, xlabel=None, ylabel=None, cmap=None, aspect="auto", interpolation="nearest", name=None): """ Adds an image to the plot's figure. @param data a 2D array. See matplotlib.Axes.imshow documentation. @param position A 3-digit number. The first two digits define a 2D grid where subplots may be added. The final digit specifies the nth grid location for the added subplot @param xlabel text to be displayed on the x-axis @param ylabel text to be displayed on the y-axis @param cmap color map used in the rendering @param aspect how aspect ratio is handled during resize @param interpolation interpolation method """ if cmap is None: # The default colormodel is an ugly blue-red model. cmap = cm.Greys ax = self._addBase(position, xlabel=xlabel, ylabel=ylabel) ax.imshow(data, cmap=cmap, aspect=aspect, interpolation=interpolation) if self._show: plt.draw() if name is not None: if not os.path.exists("log"): os.mkdir("log") plt.savefig("log/{name}.png".format(name=name), bbox_inches="tight", figsize=(8, 6), dpi=400)
def _addBase(self, position, xlabel=None, ylabel=None): """ Adds a subplot to the plot's figure at specified position. @param position A 3-digit number. The first two digits define a 2D grid where subplots may be added. The final digit specifies the nth grid location for the added subplot @param xlabel text to be displayed on the x-axis @param ylabel text to be displayed on the y-axis @returns (matplotlib.Axes) Axes instance """ ax = self._fig.add_subplot(position) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) return ax
def _generateOverlapping(filename="overlap.csv", numSequences=2, elementsPerSeq=3, numRepeats=10, hub=[0,1], hubOffset=1, resets=False): """ Generate a temporal dataset containing sequences that overlap one or more elements with other sequences. Parameters: ---------------------------------------------------- filename: name of the file to produce, including extension. It will be created in a 'datasets' sub-directory within the directory containing this script. numSequences: how many sequences to generate elementsPerSeq: length of each sequence numRepeats: how many times to repeat each sequence in the output hub: sub-sequence to place within each other sequence hubOffset: where, within each sequence, to place the hub resets: if True, turn on reset at start of each sequence """ # Check for conflicts in arguments assert (hubOffset + len(hub) <= elementsPerSeq) # Create the output file scriptDir = os.path.dirname(__file__) pathname = os.path.join(scriptDir, 'datasets', filename) print "Creating %s..." % (pathname) fields = [('reset', 'int', 'R'), ('field1', 'string', ''), ('field2', 'float', '')] outFile = FileRecordStream(pathname, write=True, fields=fields) # Create the sequences with the hub in the middle sequences = [] nextElemIdx = max(hub)+1 for _ in range(numSequences): seq = [] for j in range(hubOffset): seq.append(nextElemIdx) nextElemIdx += 1 for j in hub: seq.append(j) j = hubOffset + len(hub) while j < elementsPerSeq: seq.append(nextElemIdx) nextElemIdx += 1 j += 1 sequences.append(seq) # Write out the sequences in random order seqIdxs = [] for _ in range(numRepeats): seqIdxs += range(numSequences) random.shuffle(seqIdxs) for seqIdx in seqIdxs: reset = int(resets) seq = sequences[seqIdx] for (x) in seq: outFile.appendRecord([reset, str(x), x]) reset = 0 outFile.close()
def _generateFirstOrder0(): """ Generate the initial, first order, and second order transition probabilities for 'probability0'. For this model, we generate the following set of sequences: .1 .75 0----1-----2 \ \ \ \ .25 \ \-----3 \ \ .9 .5 \--- 4--------- 2 \ \ .5 \---------3 Parameters: ---------------------------------------------------------------------- retval: (initProb, firstOrder, secondOrder, seqLen) initProb: Initial probability for each category. This is a vector of length len(categoryList). firstOrder: A dictionary of the 1st order probabilities. The key is the 1st element of the sequence, the value is the probability of each 2nd element given the first. secondOrder: A dictionary of the 2nd order probabilities. The key is the first 2 elements of the sequence, the value is the probability of each possible 3rd element given the first two. seqLen: Desired length of each sequence. The 1st element will be generated using the initProb, the 2nd element by the firstOrder table, and the 3rd and all successive elements by the secondOrder table. categoryList: list of category names to use Here is an example of some return values when there are 3 categories initProb: [0.7, 0.2, 0.1] firstOrder: {'[0]': [0.3, 0.3, 0.4], '[1]': [0.3, 0.3, 0.4], '[2]': [0.3, 0.3, 0.4]} secondOrder: {'[0,0]': [0.3, 0.3, 0.4], '[0,1]': [0.3, 0.3, 0.4], '[0,2]': [0.3, 0.3, 0.4], '[1,0]': [0.3, 0.3, 0.4], '[1,1]': [0.3, 0.3, 0.4], '[1,2]': [0.3, 0.3, 0.4], '[2,0]': [0.3, 0.3, 0.4], '[2,1]': [0.3, 0.3, 0.4], '[2,2]': [0.3, 0.3, 0.4]} """ # -------------------------------------------------------------------- # Initial probabilities, 'a' and 'e' equally likely numCategories = 5 initProb = numpy.zeros(numCategories) initProb[0] = 1.0 # -------------------------------------------------------------------- # 1st order transitions firstOrder = dict() firstOrder['0'] = numpy.array([0, 0.1, 0, 0, 0.9]) firstOrder['1'] = numpy.array([0, 0, 0.75, 0.25, 0]) firstOrder['2'] = numpy.array([1.0, 0, 0, 0, 0]) firstOrder['3'] = numpy.array([1.0, 0, 0, 0, 0]) firstOrder['4'] = numpy.array([0, 0, 0.5, 0.5, 0]) # -------------------------------------------------------------------- # 2nd order transitions don't apply secondOrder = None # Generate the category list categoryList = ['%d' % x for x in range(5)] return (initProb, firstOrder, secondOrder, 3, categoryList)
def _generateFileFromProb(filename, numRecords, categoryList, initProb, firstOrderProb, secondOrderProb, seqLen, numNoise=0, resetsEvery=None): """ Generate a set of records reflecting a set of probabilities. Parameters: ---------------------------------------------------------------- filename: name of .csv file to generate numRecords: number of records to generate categoryList: list of category names initProb: Initial probability for each category. This is a vector of length len(categoryList). firstOrderProb: A dictionary of the 1st order probabilities. The key is the 1st element of the sequence, the value is the probability of each 2nd element given the first. secondOrderProb: A dictionary of the 2nd order probabilities. The key is the first 2 elements of the sequence, the value is the probability of each possible 3rd element given the first two. If this is None, then the sequences will be first order only. seqLen: Desired length of each sequence. The 1st element will be generated using the initProb, the 2nd element by the firstOrder table, and the 3rd and all successive elements by the secondOrder table. None means infinite length. numNoise: Number of noise elements to place between each sequence. The noise elements are evenly distributed from all categories. resetsEvery: If not None, generate a reset every N records Here is an example of some parameters: categoryList: ['cat1', 'cat2', 'cat3'] initProb: [0.7, 0.2, 0.1] firstOrderProb: {'[0]': [0.3, 0.3, 0.4], '[1]': [0.3, 0.3, 0.4], '[2]': [0.3, 0.3, 0.4]} secondOrderProb: {'[0,0]': [0.3, 0.3, 0.4], '[0,1]': [0.3, 0.3, 0.4], '[0,2]': [0.3, 0.3, 0.4], '[1,0]': [0.3, 0.3, 0.4], '[1,1]': [0.3, 0.3, 0.4], '[1,2]': [0.3, 0.3, 0.4], '[2,0]': [0.3, 0.3, 0.4], '[2,1]': [0.3, 0.3, 0.4], '[2,2]': [0.3, 0.3, 0.4]} """ # Create the file print "Creating %s..." % (filename) fields = [('reset', 'int', 'R'), ('field1', 'string', ''), ('field2', 'float', '')] scriptDir = os.path.dirname(__file__) pathname = os.path.join(scriptDir, 'datasets', filename) outFile = FileRecordStream(pathname, write=True, fields=fields) # -------------------------------------------------------------------- # Convert the probabilitie tables into cumulative probabilities initCumProb = initProb.cumsum() firstOrderCumProb = dict() for (key,value) in firstOrderProb.iteritems(): firstOrderCumProb[key] = value.cumsum() if secondOrderProb is not None: secondOrderCumProb = dict() for (key,value) in secondOrderProb.iteritems(): secondOrderCumProb[key] = value.cumsum() else: secondOrderCumProb = None # -------------------------------------------------------------------- # Write out the sequences elementsInSeq = [] numElementsSinceReset = 0 maxCatIdx = len(categoryList) - 1 for _ in xrange(numRecords): # Generate a reset? if numElementsSinceReset == 0: reset = 1 else: reset = 0 # Pick the next element, based on how are we are into the 2nd order # sequence. rand = numpy.random.rand() # Generate 1st order sequences if secondOrderCumProb is None: if len(elementsInSeq) == 0: catIdx = numpy.searchsorted(initCumProb, rand) elif len(elementsInSeq) >= 1 and \ (seqLen is None or len(elementsInSeq) < seqLen-numNoise): catIdx = numpy.searchsorted(firstOrderCumProb[str(elementsInSeq[-1])], rand) else: # random "noise" catIdx = numpy.random.randint(len(categoryList)) # Generate 2nd order sequences else: if len(elementsInSeq) == 0: catIdx = numpy.searchsorted(initCumProb, rand) elif len(elementsInSeq) == 1: catIdx = numpy.searchsorted(firstOrderCumProb[str(elementsInSeq)], rand) elif (len(elementsInSeq) >=2) and \ (seqLen is None or len(elementsInSeq) < seqLen-numNoise): catIdx = numpy.searchsorted(secondOrderCumProb[str(elementsInSeq[-2:])], rand) else: # random "noise" catIdx = numpy.random.randint(len(categoryList)) # ------------------------------------------------------------------- # Write out the record catIdx = min(maxCatIdx, catIdx) outFile.appendRecord([reset, categoryList[catIdx], catIdx]) #print categoryList[catIdx] # ------------------------------------------------------------ # Increment counters elementsInSeq.append(catIdx) numElementsSinceReset += 1 # Generate another reset? if resetsEvery is not None and numElementsSinceReset == resetsEvery: numElementsSinceReset = 0 elementsInSeq = [] # Start another 2nd order sequence? if seqLen is not None and (len(elementsInSeq) == seqLen+numNoise): elementsInSeq = [] outFile.close()
def getVersion(): """ Get version from local file. """ with open(os.path.join(REPO_DIR, "VERSION"), "r") as versionFile: return versionFile.read().strip()
def nupicBindingsPrereleaseInstalled(): """ Make an attempt to determine if a pre-release version of nupic.bindings is installed already. @return: boolean """ try: nupicDistribution = pkg_resources.get_distribution("nupic.bindings") if pkg_resources.parse_version(nupicDistribution.version).is_prerelease: # A pre-release dev version of nupic.bindings is installed. return True except pkg_resources.DistributionNotFound: pass # Silently ignore. The absence of nupic.bindings will be handled by # setuptools by default return False
def findRequirements(): """ Read the requirements.txt file and parse into requirements for setup's install_requirements option. """ requirementsPath = os.path.join(REPO_DIR, "requirements.txt") requirements = parse_file(requirementsPath) if nupicBindingsPrereleaseInstalled(): # User has a pre-release version of nupic.bindings installed, which is only # possible if the user installed and built nupic.bindings from source and # it is up to the user to decide when to update nupic.bindings. We'll # quietly remove the entry in requirements.txt so as to not conflate the # two. requirements = [req for req in requirements if "nupic.bindings" not in req] return requirements
def _handleDescriptionOption(cmdArgStr, outDir, usageStr, hsVersion, claDescriptionTemplateFile): """ Parses and validates the --description option args and executes the request Parameters: ----------------------------------------------------------------------- cmdArgStr: JSON string compatible with _gExperimentDescriptionSchema outDir: where to place generated experiment files usageStr: program usage string hsVersion: which version of hypersearch permutations file to generate, can be 'v1' or 'v2' claDescriptionTemplateFile: Filename containing the template description retval: nothing """ # convert --description arg from JSON string to dict try: args = json.loads(cmdArgStr) except Exception, e: raise _InvalidCommandArgException( _makeUsageErrorStr( ("JSON arg parsing failed for --description: %s\n" + \ "ARG=<%s>") % (str(e), cmdArgStr), usageStr)) #print "PARSED JSON ARGS=\n%s" % (json.dumps(args, indent=4)) filesDescription = _generateExperiment(args, outDir, hsVersion=hsVersion, claDescriptionTemplateFile = claDescriptionTemplateFile) pprint.pprint(filesDescription) return
def _handleDescriptionFromFileOption(filename, outDir, usageStr, hsVersion, claDescriptionTemplateFile): """ Parses and validates the --descriptionFromFile option and executes the request Parameters: ----------------------------------------------------------------------- filename: File from which we'll extract description JSON outDir: where to place generated experiment files usageStr: program usage string hsVersion: which version of hypersearch permutations file to generate, can be 'v1' or 'v2' claDescriptionTemplateFile: Filename containing the template description retval: nothing """ try: fileHandle = open(filename, 'r') JSONStringFromFile = fileHandle.read().splitlines() JSONStringFromFile = ''.join(JSONStringFromFile) except Exception, e: raise _InvalidCommandArgException( _makeUsageErrorStr( ("File open failed for --descriptionFromFile: %s\n" + \ "ARG=<%s>") % (str(e), filename), usageStr)) _handleDescriptionOption(JSONStringFromFile, outDir, usageStr, hsVersion=hsVersion, claDescriptionTemplateFile = claDescriptionTemplateFile) return
def _isInt(x, precision = 0.0001): """ Return (isInt, intValue) for a given floating point number. Parameters: ---------------------------------------------------------------------- x: floating point number to evaluate precision: desired precision retval: (isInt, intValue) isInt: True if x is close enough to an integer value intValue: x as an integer """ xInt = int(round(x)) return (abs(x - xInt) < precision * x, xInt)
def _indentLines(str, indentLevels = 1, indentFirstLine=True): """ Indent all lines in the given string str: input string indentLevels: number of levels of indentation to apply indentFirstLine: if False, the 1st line will not be indented Returns: The result string with all lines indented """ indent = _ONE_INDENT * indentLevels lines = str.splitlines(True) result = '' if len(lines) > 0 and not indentFirstLine: first = 1 result += lines[0] else: first = 0 for line in lines[first:]: result += indent + line return result
def _generateMetricSpecString(inferenceElement, metric, params=None, field=None, returnLabel=False): """ Generates the string representation of a MetricSpec object, and returns the metric key associated with the metric. Parameters: ----------------------------------------------------------------------- inferenceElement: An InferenceElement value that indicates which part of the inference this metric is computed on metric: The type of the metric being computed (e.g. aae, avg_error) params: A dictionary of parameters for the metric. The keys are the parameter names and the values should be the parameter values (e.g. window=200) field: The name of the field for which this metric is being computed returnLabel: If True, returns the label of the MetricSpec that was generated """ metricSpecArgs = dict(metric=metric, field=field, params=params, inferenceElement=inferenceElement) metricSpecAsString = "MetricSpec(%s)" % \ ', '.join(['%s=%r' % (item[0],item[1]) for item in metricSpecArgs.iteritems()]) if not returnLabel: return metricSpecAsString spec = MetricSpec(**metricSpecArgs) metricLabel = spec.getLabel() return metricSpecAsString, metricLabel
def _generateFileFromTemplates(templateFileNames, outputFilePath, replacementDict): """ Generates a file by applying token replacements to the given template file templateFileName: A list of template file names; these files are assumed to be in the same directory as the running experiment_generator.py script. ExpGenerator will perform the substitution and concanetate the files in the order they are specified outputFilePath: Absolute path of the output file replacementDict: A dictionary of token/replacement pairs """ # Find out where we're running from so we know where to find templates installPath = os.path.dirname(__file__) outputFile = open(outputFilePath, "w") outputLines = [] inputLines = [] firstFile = True for templateFileName in templateFileNames: # Separate lines from each file by two blank lines. if not firstFile: inputLines.extend([os.linesep]*2) firstFile = False inputFilePath = os.path.join(installPath, templateFileName) inputFile = open(inputFilePath) inputLines.extend(inputFile.readlines()) inputFile.close() print "Writing ", len(inputLines), "lines..." for line in inputLines: tempLine = line # Enumerate through each key in replacementDict and replace with value for k, v in replacementDict.iteritems(): if v is None: v = "None" tempLine = re.sub(k, v, tempLine) outputFile.write(tempLine) outputFile.close()
def _generateEncoderChoicesV1(fieldInfo): """ Return a list of possible encoder parameter combinations for the given field and the default aggregation function to use. Each parameter combination is a dict defining the parameters for the encoder. Here is an example return value for the encoderChoicesList: [ None, {'fieldname':'timestamp', 'name': 'timestamp_timeOfDay', 'type':'DateEncoder' 'dayOfWeek': (7,1) }, {'fieldname':'timestamp', 'name': 'timestamp_timeOfDay', 'type':'DateEncoder' 'dayOfWeek': (7,3) }, ], Parameters: -------------------------------------------------- fieldInfo: item from the 'includedFields' section of the description JSON object retval: (encoderChoicesList, aggFunction) encoderChoicesList: a list of encoder choice lists for this field. Most fields will generate just 1 encoder choice list. DateTime fields can generate 2 or more encoder choice lists, one for dayOfWeek, one for timeOfDay, etc. aggFunction: name of aggregation function to use for this field type """ width = 7 fieldName = fieldInfo['fieldName'] fieldType = fieldInfo['fieldType'] encoderChoicesList = [] # Scalar? if fieldType in ['float', 'int']: aggFunction = 'mean' encoders = [None] for n in (13, 50, 150, 500): encoder = dict(type='ScalarSpaceEncoder', name=fieldName, fieldname=fieldName, n=n, w=width, clipInput=True,space="absolute") if 'minValue' in fieldInfo: encoder['minval'] = fieldInfo['minValue'] if 'maxValue' in fieldInfo: encoder['maxval'] = fieldInfo['maxValue'] encoders.append(encoder) encoderChoicesList.append(encoders) # String? elif fieldType == 'string': aggFunction = 'first' encoders = [None] encoder = dict(type='SDRCategoryEncoder', name=fieldName, fieldname=fieldName, n=100, w=width) encoders.append(encoder) encoderChoicesList.append(encoders) # Datetime? elif fieldType == 'datetime': aggFunction = 'first' # First, the time of day representation encoders = [None] for radius in (1, 8): encoder = dict(type='DateEncoder', name='%s_timeOfDay' % (fieldName), fieldname=fieldName, timeOfDay=(width, radius)) encoders.append(encoder) encoderChoicesList.append(encoders) # Now, the day of week representation encoders = [None] for radius in (1, 3): encoder = dict(type='DateEncoder', name='%s_dayOfWeek' % (fieldName), fieldname=fieldName, dayOfWeek=(width, radius)) encoders.append(encoder) encoderChoicesList.append(encoders) else: raise RuntimeError("Unsupported field type '%s'" % (fieldType)) # Return results return (encoderChoicesList, aggFunction)
def _generateEncoderStringsV1(includedFields): """ Generate and return the following encoder related substitution variables: encoderSpecsStr: For the base description file, this string defines the default encoding dicts for each encoder. For example: '__gym_encoder' : { 'fieldname': 'gym', 'n': 13, 'name': 'gym', 'type': 'SDRCategoryEncoder', 'w': 7}, '__address_encoder' : { 'fieldname': 'address', 'n': 13, 'name': 'address', 'type': 'SDRCategoryEncoder', 'w': 7} encoderSchemaStr: For the base description file, this is a list containing a DeferredDictLookup entry for each encoder. For example: [DeferredDictLookup('__gym_encoder'), DeferredDictLookup('__address_encoder'), DeferredDictLookup('__timestamp_timeOfDay_encoder'), DeferredDictLookup('__timestamp_dayOfWeek_encoder'), DeferredDictLookup('__consumption_encoder')], permEncoderChoicesStr: For the permutations file, this defines the possible encoder dicts for each encoder. For example: '__timestamp_dayOfWeek_encoder': [ None, {'fieldname':'timestamp', 'name': 'timestamp_timeOfDay', 'type':'DateEncoder' 'dayOfWeek': (7,1) }, {'fieldname':'timestamp', 'name': 'timestamp_timeOfDay', 'type':'DateEncoder' 'dayOfWeek': (7,3) }, ], '__field_consumption_encoder': [ None, {'fieldname':'consumption', 'name': 'consumption', 'type':'AdaptiveScalarEncoder', 'n': 13, 'w': 7, } ] Parameters: -------------------------------------------------- includedFields: item from the 'includedFields' section of the description JSON object. This is a list of dicts, each dict defining the field name, type, and optional min and max values. retval: (encoderSpecsStr, encoderSchemaStr permEncoderChoicesStr) """ # ------------------------------------------------------------------------ # First accumulate the possible choices for each encoder encoderChoicesList = [] for fieldInfo in includedFields: fieldName = fieldInfo['fieldName'] # Get the list of encoder choices for this field (choicesList, aggFunction) = _generateEncoderChoicesV1(fieldInfo) encoderChoicesList.extend(choicesList) # ------------------------------------------------------------------------ # Generate the string containing the encoder specs and encoder schema. See # the function comments for an example of the encoderSpecsStr and # encoderSchemaStr # encoderSpecsList = [] for encoderChoices in encoderChoicesList: # Use the last choice as the default in the base file because the 1st is # often None encoder = encoderChoices[-1] # Check for bad characters for c in _ILLEGAL_FIELDNAME_CHARACTERS: if encoder['name'].find(c) >= 0: raise _ExpGeneratorException("Illegal character in field: %r (%r)" % ( c, encoder['name'])) encoderSpecsList.append("%s: \n%s%s" % ( _quoteAndEscape(encoder['name']), 2*_ONE_INDENT, pprint.pformat(encoder, indent=2*_INDENT_STEP))) encoderSpecsStr = ',\n '.join(encoderSpecsList) # ------------------------------------------------------------------------ # Generate the string containing the permutation encoder choices. See the # function comments above for an example of the permEncoderChoicesStr permEncoderChoicesList = [] for encoderChoices in encoderChoicesList: permEncoderChoicesList.append("%s: %s," % ( _quoteAndEscape(encoderChoices[-1]['name']), pprint.pformat(encoderChoices, indent=2*_INDENT_STEP))) permEncoderChoicesStr = '\n'.join(permEncoderChoicesList) permEncoderChoicesStr = _indentLines(permEncoderChoicesStr, 1, indentFirstLine=False) # Return results return (encoderSpecsStr, permEncoderChoicesStr)
def _generatePermEncoderStr(options, encoderDict): """ Generate the string that defines the permutations to apply for a given encoder. Parameters: ----------------------------------------------------------------------- options: experiment params encoderDict: the encoder dict, which gets placed into the description.py For example, if the encoderDict contains: 'consumption': { 'clipInput': True, 'fieldname': u'consumption', 'n': 100, 'name': u'consumption', 'type': 'AdaptiveScalarEncoder', 'w': 21}, The return string will contain: "PermuteEncoder(fieldName='consumption', encoderClass='AdaptiveScalarEncoder', w=21, n=PermuteInt(28, 521), clipInput=True)" """ permStr = "" # If it's the encoder for the classifier input, then it's always present so # put it in as a dict in the permutations.py file instead of a # PermuteEncoder(). if encoderDict.get('classifierOnly', False): permStr = "dict(" for key, value in encoderDict.items(): if key == "name": continue if key == 'n' and encoderDict['type'] != 'SDRCategoryEncoder': permStr += "n=PermuteInt(%d, %d), " % (encoderDict["w"] + 7, encoderDict["w"] + 500) else: if issubclass(type(value), basestring): permStr += "%s='%s', " % (key, value) else: permStr += "%s=%s, " % (key, value) permStr += ")" else: # Scalar encoders if encoderDict["type"] in ["ScalarSpaceEncoder", "AdaptiveScalarEncoder", "ScalarEncoder", "LogEncoder"]: permStr = "PermuteEncoder(" for key, value in encoderDict.items(): if key == "fieldname": key = "fieldName" elif key == "type": key = "encoderClass" elif key == "name": continue if key == "n": permStr += "n=PermuteInt(%d, %d), " % (encoderDict["w"] + 1, encoderDict["w"] + 500) elif key == "runDelta": if value and not "space" in encoderDict: permStr += "space=PermuteChoices([%s,%s]), " \ % (_quoteAndEscape("delta"), _quoteAndEscape("absolute")) encoderDict.pop("runDelta") else: if issubclass(type(value), basestring): permStr += "%s='%s', " % (key, value) else: permStr += "%s=%s, " % (key, value) permStr += ")" # Category encoder elif encoderDict["type"] in ["SDRCategoryEncoder"]: permStr = "PermuteEncoder(" for key, value in encoderDict.items(): if key == "fieldname": key = "fieldName" elif key == "type": key = "encoderClass" elif key == "name": continue if issubclass(type(value), basestring): permStr += "%s='%s', " % (key, value) else: permStr += "%s=%s, " % (key, value) permStr += ")" # Datetime encoder elif encoderDict["type"] in ["DateEncoder"]: permStr = "PermuteEncoder(" for key, value in encoderDict.items(): if key == "fieldname": key = "fieldName" elif key == "type": continue elif key == "name": continue if key == "timeOfDay": permStr += "encoderClass='%s.timeOfDay', " % (encoderDict["type"]) permStr += "radius=PermuteFloat(0.5, 12), " permStr += "w=%d, " % (value[0]) elif key == "dayOfWeek": permStr += "encoderClass='%s.dayOfWeek', " % (encoderDict["type"]) permStr += "radius=PermuteFloat(1, 6), " permStr += "w=%d, " % (value[0]) elif key == "weekend": permStr += "encoderClass='%s.weekend', " % (encoderDict["type"]) permStr += "radius=PermuteChoices([1]), " permStr += "w=%d, " % (value) else: if issubclass(type(value), basestring): permStr += "%s='%s', " % (key, value) else: permStr += "%s=%s, " % (key, value) permStr += ")" else: raise RuntimeError("Unsupported encoder type '%s'" % \ (encoderDict["type"])) return permStr
def _generateEncoderStringsV2(includedFields, options): """ Generate and return the following encoder related substitution variables: encoderSpecsStr: For the base description file, this string defines the default encoding dicts for each encoder. For example: __gym_encoder = { 'fieldname': 'gym', 'n': 13, 'name': 'gym', 'type': 'SDRCategoryEncoder', 'w': 7}, __address_encoder = { 'fieldname': 'address', 'n': 13, 'name': 'address', 'type': 'SDRCategoryEncoder', 'w': 7} permEncoderChoicesStr: For the permutations file, this defines the possible encoder dicts for each encoder. For example: '__gym_encoder' : PermuteEncoder('gym', 'SDRCategoryEncoder', w=7, n=100), '__address_encoder' : PermuteEncoder('address', 'SDRCategoryEncoder', w=7, n=100), '__timestamp_dayOfWeek_encoder' : PermuteEncoder('timestamp', 'DateEncoder.timeOfDay', w=7, radius=PermuteChoices([1, 8])), '__consumption_encoder': PermuteEncoder('consumption', 'AdaptiveScalarEncoder', w=7, n=PermuteInt(13, 500, 20), minval=0, maxval=PermuteInt(100, 300, 25)), Parameters: -------------------------------------------------- includedFields: item from the 'includedFields' section of the description JSON object. This is a list of dicts, each dict defining the field name, type, and optional min and max values. retval: (encoderSpecsStr permEncoderChoicesStr) """ width = 21 encoderDictsList = [] # If this is a NontemporalClassification experiment, then the # the "predicted" field (the classification value) should be marked to ONLY # go to the classifier if options['inferenceType'] in ["NontemporalClassification", "NontemporalMultiStep", "TemporalMultiStep", "MultiStep"]: classifierOnlyField = options['inferenceArgs']['predictedField'] else: classifierOnlyField = None # ========================================================================== # For each field, generate the default encoding dict and PermuteEncoder # constructor arguments for fieldInfo in includedFields: fieldName = fieldInfo['fieldName'] fieldType = fieldInfo['fieldType'] # --------- # Scalar? if fieldType in ['float', 'int']: # n=100 is reasonably hardcoded value for n when used by description.py # The swarming will use PermuteEncoder below, where n is variable and # depends on w runDelta = fieldInfo.get("runDelta", False) if runDelta or "space" in fieldInfo: encoderDict = dict(type='ScalarSpaceEncoder', name=fieldName, fieldname=fieldName, n=100, w=width, clipInput=True) if runDelta: encoderDict["runDelta"] = True else: encoderDict = dict(type='AdaptiveScalarEncoder', name=fieldName, fieldname=fieldName, n=100, w=width, clipInput=True) if 'minValue' in fieldInfo: encoderDict['minval'] = fieldInfo['minValue'] if 'maxValue' in fieldInfo: encoderDict['maxval'] = fieldInfo['maxValue'] # If both min and max were specified, use a non-adaptive encoder if ('minValue' in fieldInfo and 'maxValue' in fieldInfo) \ and (encoderDict['type'] == 'AdaptiveScalarEncoder'): encoderDict['type'] = 'ScalarEncoder' # Defaults may have been over-ridden by specifying an encoder type if 'encoderType' in fieldInfo: encoderDict['type'] = fieldInfo['encoderType'] if 'space' in fieldInfo: encoderDict['space'] = fieldInfo['space'] encoderDictsList.append(encoderDict) # --------- # String? elif fieldType == 'string': encoderDict = dict(type='SDRCategoryEncoder', name=fieldName, fieldname=fieldName, n=100+width, w=width) if 'encoderType' in fieldInfo: encoderDict['type'] = fieldInfo['encoderType'] encoderDictsList.append(encoderDict) # --------- # Datetime? elif fieldType == 'datetime': # First, the time of day representation encoderDict = dict(type='DateEncoder', name='%s_timeOfDay' % (fieldName), fieldname=fieldName, timeOfDay=(width, 1)) if 'encoderType' in fieldInfo: encoderDict['type'] = fieldInfo['encoderType'] encoderDictsList.append(encoderDict) # Now, the day of week representation encoderDict = dict(type='DateEncoder', name='%s_dayOfWeek' % (fieldName), fieldname=fieldName, dayOfWeek=(width, 1)) if 'encoderType' in fieldInfo: encoderDict['type'] = fieldInfo['encoderType'] encoderDictsList.append(encoderDict) # Now, the day of week representation encoderDict = dict(type='DateEncoder', name='%s_weekend' % (fieldName), fieldname=fieldName, weekend=(width)) if 'encoderType' in fieldInfo: encoderDict['type'] = fieldInfo['encoderType'] encoderDictsList.append(encoderDict) else: raise RuntimeError("Unsupported field type '%s'" % (fieldType)) # ----------------------------------------------------------------------- # If this was the predicted field, insert another encoder that sends it # to the classifier only if fieldName == classifierOnlyField: clEncoderDict = dict(encoderDict) clEncoderDict['classifierOnly'] = True clEncoderDict['name'] = '_classifierInput' encoderDictsList.append(clEncoderDict) # If the predicted field needs to be excluded, take it out of the encoder # lists if options["inferenceArgs"]["inputPredictedField"] == "no": encoderDictsList.remove(encoderDict) # Remove any encoders not in fixedFields if options.get('fixedFields') is not None: tempList=[] for encoderDict in encoderDictsList: if encoderDict['name'] in options['fixedFields']: tempList.append(encoderDict) encoderDictsList = tempList # ========================================================================== # Now generate the encoderSpecsStr and permEncoderChoicesStr strings from # encoderDictsList and constructorStringList encoderSpecsList = [] permEncoderChoicesList = [] for encoderDict in encoderDictsList: if encoderDict['name'].find('\\') >= 0: raise _ExpGeneratorException("Illegal character in field: '\\'") # Check for bad characters for c in _ILLEGAL_FIELDNAME_CHARACTERS: if encoderDict['name'].find(c) >= 0: raise _ExpGeneratorException("Illegal character %s in field %r" %(c, encoderDict['name'])) constructorStr = _generatePermEncoderStr(options, encoderDict) encoderKey = _quoteAndEscape(encoderDict['name']) encoderSpecsList.append("%s: %s%s" % ( encoderKey, 2*_ONE_INDENT, pprint.pformat(encoderDict, indent=2*_INDENT_STEP))) # Each permEncoderChoicesStr is of the form: # PermuteEncoder('gym', 'SDRCategoryEncoder', # w=7, n=100), permEncoderChoicesList.append("%s: %s," % (encoderKey, constructorStr)) # Join into strings encoderSpecsStr = ',\n '.join(encoderSpecsList) permEncoderChoicesStr = '\n'.join(permEncoderChoicesList) permEncoderChoicesStr = _indentLines(permEncoderChoicesStr, 1, indentFirstLine=True) # Return results return (encoderSpecsStr, permEncoderChoicesStr)
def _handleJAVAParameters(options): """ Handle legacy options (TEMPORARY) """ # Find the correct InferenceType for the Model if 'inferenceType' not in options: prediction = options.get('prediction', {InferenceType.TemporalNextStep: {'optimize':True}}) inferenceType = None for infType, value in prediction.iteritems(): if value['optimize']: inferenceType = infType break if inferenceType == 'temporal': inferenceType = InferenceType.TemporalNextStep if inferenceType != InferenceType.TemporalNextStep: raise _ExpGeneratorException("Unsupported inference type %s" % \ (inferenceType)) options['inferenceType'] = inferenceType # Find the best value for the predicted field if 'predictionField' in options: if 'inferenceArgs' not in options: options['inferenceArgs'] = {'predictedField': options['predictionField']} elif 'predictedField' not in options['inferenceArgs']: options['inferenceArgs']['predictedField'] = options['predictionField']
def _getPropertyValue(schema, propertyName, options): """Checks to see if property is specified in 'options'. If not, reads the default value from the schema""" if propertyName not in options: paramsSchema = schema['properties'][propertyName] if 'default' in paramsSchema: options[propertyName] = paramsSchema['default'] else: options[propertyName] = None
def _getExperimentDescriptionSchema(): """ Returns the experiment description schema. This implementation loads it in from file experimentDescriptionSchema.json. Parameters: -------------------------------------------------------------------------- Returns: returns a dict representing the experiment description schema. """ installPath = os.path.dirname(os.path.abspath(__file__)) schemaFilePath = os.path.join(installPath, "experimentDescriptionSchema.json") return json.loads(open(schemaFilePath, 'r').read())
def _generateExperiment(options, outputDirPath, hsVersion, claDescriptionTemplateFile): """ Executes the --description option, which includes: 1. Perform provider compatibility checks 2. Preprocess the training and testing datasets (filter, join providers) 3. If test dataset omitted, split the training dataset into training and testing datasets. 4. Gather statistics about the training and testing datasets. 5. Generate experiment scripts (description.py, permutaions.py) Parameters: -------------------------------------------------------------------------- options: dictionary that matches the schema defined by the return value of _getExperimentDescriptionSchema(); NOTE: this arg may be modified by this function. outputDirPath: where to place generated files hsVersion: which version of hypersearch permutations file to generate, can be 'v1' or 'v2' claDescriptionTemplateFile: Filename containing the template description Returns: on success, returns a dictionary per _experimentResultsJSONSchema; raises exception on error Assumption1: input train and test files have identical field metadata """ _gExperimentDescriptionSchema = _getExperimentDescriptionSchema() # Validate JSON arg using JSON schema validator try: validictory.validate(options, _gExperimentDescriptionSchema) except Exception, e: raise _InvalidCommandArgException( ("JSON arg validation failed for option --description: " + \ "%s\nOPTION ARG=%s") % (str(e), pprint.pformat(options))) # Validate the streamDef streamSchema = json.load(resource_stream(jsonschema.__name__, 'stream_def.json')) try: validictory.validate(options['streamDef'], streamSchema) except Exception, e: raise _InvalidCommandArgException( ("JSON arg validation failed for streamDef " + \ "%s\nOPTION ARG=%s") % (str(e), json.dumps(options))) # ----------------------------------------------------------------------- # Handle legacy parameters from JAVA API server # TODO: remove this! _handleJAVAParameters(options) # ----------------------------------------------------------------------- # Get default values for propertyName in _gExperimentDescriptionSchema['properties']: _getPropertyValue(_gExperimentDescriptionSchema, propertyName, options) if options['inferenceArgs'] is not None: infArgs = _gExperimentDescriptionSchema['properties']['inferenceArgs'] for schema in infArgs['type']: if isinstance(schema, dict): for propertyName in schema['properties']: _getPropertyValue(schema, propertyName, options['inferenceArgs']) if options['anomalyParams'] is not None: anomalyArgs = _gExperimentDescriptionSchema['properties']['anomalyParams'] for schema in anomalyArgs['type']: if isinstance(schema, dict): for propertyName in schema['properties']: _getPropertyValue(schema, propertyName, options['anomalyParams']) # If the user specified nonTemporalClassification, make sure prediction # steps is 0 predictionSteps = options['inferenceArgs'].get('predictionSteps', None) if options['inferenceType'] == InferenceType.NontemporalClassification: if predictionSteps is not None and predictionSteps != [0]: raise RuntimeError("When NontemporalClassification is used, prediction" " steps must be [0]") # ------------------------------------------------------------------------- # If the user asked for 0 steps of prediction, then make this a spatial # classification experiment if predictionSteps == [0] \ and options['inferenceType'] in ['NontemporalMultiStep', 'TemporalMultiStep', 'MultiStep']: options['inferenceType'] = InferenceType.NontemporalClassification # If NontemporalClassification was chosen as the inferenceType, then the # predicted field can NOT be used as an input if options["inferenceType"] == InferenceType.NontemporalClassification: if options["inferenceArgs"]["inputPredictedField"] == "yes" \ or options["inferenceArgs"]["inputPredictedField"] == "auto": raise RuntimeError("When the inference type is NontemporalClassification" " inputPredictedField must be set to 'no'") options["inferenceArgs"]["inputPredictedField"] = "no" # ----------------------------------------------------------------------- # Process the swarmSize setting, if provided swarmSize = options['swarmSize'] if swarmSize is None: if options["inferenceArgs"]["inputPredictedField"] is None: options["inferenceArgs"]["inputPredictedField"] = "auto" elif swarmSize == 'small': if options['minParticlesPerSwarm'] is None: options['minParticlesPerSwarm'] = 3 if options['iterationCount'] is None: options['iterationCount'] = 100 if options['maxModels'] is None: options['maxModels'] = 1 if options["inferenceArgs"]["inputPredictedField"] is None: options["inferenceArgs"]["inputPredictedField"] = "yes" elif swarmSize == 'medium': if options['minParticlesPerSwarm'] is None: options['minParticlesPerSwarm'] = 5 if options['iterationCount'] is None: options['iterationCount'] = 4000 if options['maxModels'] is None: options['maxModels'] = 200 if options["inferenceArgs"]["inputPredictedField"] is None: options["inferenceArgs"]["inputPredictedField"] = "auto" elif swarmSize == 'large': if options['minParticlesPerSwarm'] is None: options['minParticlesPerSwarm'] = 15 #options['killUselessSwarms'] = False #options['minFieldContribution'] = -1000 #options['maxFieldBranching'] = 10 #options['tryAll3FieldCombinations'] = True options['tryAll3FieldCombinationsWTimestamps'] = True if options["inferenceArgs"]["inputPredictedField"] is None: options["inferenceArgs"]["inputPredictedField"] = "auto" else: raise RuntimeError("Unsupported swarm size: %s" % (swarmSize)) # ----------------------------------------------------------------------- # Get token replacements tokenReplacements = dict() #-------------------------------------------------------------------------- # Generate the encoder related substitution strings includedFields = options['includedFields'] if hsVersion == 'v1': (encoderSpecsStr, permEncoderChoicesStr) = \ _generateEncoderStringsV1(includedFields) elif hsVersion in ['v2', 'ensemble']: (encoderSpecsStr, permEncoderChoicesStr) = \ _generateEncoderStringsV2(includedFields, options) else: raise RuntimeError("Unsupported hsVersion of %s" % (hsVersion)) #-------------------------------------------------------------------------- # Generate the string containing the sensor auto-reset dict. if options['resetPeriod'] is not None: sensorAutoResetStr = pprint.pformat(options['resetPeriod'], indent=2*_INDENT_STEP) else: sensorAutoResetStr = 'None' #-------------------------------------------------------------------------- # Generate the string containing the aggregation settings. aggregationPeriod = { 'days': 0, 'hours': 0, 'microseconds': 0, 'milliseconds': 0, 'minutes': 0, 'months': 0, 'seconds': 0, 'weeks': 0, 'years': 0, } # Honor any overrides provided in the stream definition aggFunctionsDict = {} if 'aggregation' in options['streamDef']: for key in aggregationPeriod.keys(): if key in options['streamDef']['aggregation']: aggregationPeriod[key] = options['streamDef']['aggregation'][key] if 'fields' in options['streamDef']['aggregation']: for (fieldName, func) in options['streamDef']['aggregation']['fields']: aggFunctionsDict[fieldName] = str(func) # Do we have any aggregation at all? hasAggregation = False for v in aggregationPeriod.values(): if v != 0: hasAggregation = True break # Convert the aggFunctionsDict to a list aggFunctionList = aggFunctionsDict.items() aggregationInfo = dict(aggregationPeriod) aggregationInfo['fields'] = aggFunctionList # Form the aggregation strings aggregationInfoStr = "%s" % (pprint.pformat(aggregationInfo, indent=2*_INDENT_STEP)) # ----------------------------------------------------------------------- # Generate the string defining the dataset. This is basically the # streamDef, but referencing the aggregation we already pulled out into the # config dict (which enables permuting over it) datasetSpec = options['streamDef'] if 'aggregation' in datasetSpec: datasetSpec.pop('aggregation') if hasAggregation: datasetSpec['aggregation'] = '$SUBSTITUTE' datasetSpecStr = pprint.pformat(datasetSpec, indent=2*_INDENT_STEP) datasetSpecStr = datasetSpecStr.replace( "'$SUBSTITUTE'", "config['aggregationInfo']") datasetSpecStr = _indentLines(datasetSpecStr, 2, indentFirstLine=False) # ----------------------------------------------------------------------- # Was computeInterval specified with Multistep prediction? If so, this swarm # should permute over different aggregations computeInterval = options['computeInterval'] if computeInterval is not None \ and options['inferenceType'] in ['NontemporalMultiStep', 'TemporalMultiStep', 'MultiStep']: # Compute the predictAheadTime based on the minAggregation (specified in # the stream definition) and the number of prediction steps predictionSteps = options['inferenceArgs'].get('predictionSteps', [1]) if len(predictionSteps) > 1: raise _InvalidCommandArgException("Invalid predictionSteps: %s. " \ "When computeInterval is specified, there can only be one " \ "stepSize in predictionSteps." % predictionSteps) if max(aggregationInfo.values()) == 0: raise _InvalidCommandArgException("Missing or nil stream aggregation: " "When computeInterval is specified, then the stream aggregation " "interval must be non-zero.") # Compute the predictAheadTime numSteps = predictionSteps[0] predictAheadTime = dict(aggregationPeriod) for key in predictAheadTime.iterkeys(): predictAheadTime[key] *= numSteps predictAheadTimeStr = pprint.pformat(predictAheadTime, indent=2*_INDENT_STEP) # This tells us to plug in a wildcard string for the prediction steps that # we use in other parts of the description file (metrics, inferenceArgs, # etc.) options['dynamicPredictionSteps'] = True else: options['dynamicPredictionSteps'] = False predictAheadTimeStr = "None" # ----------------------------------------------------------------------- # Save environment-common token substitutions tokenReplacements['\$EXP_GENERATOR_PROGRAM_PATH'] = \ _quoteAndEscape(os.path.abspath(__file__)) # If the "uber" metric 'MultiStep' was specified, then plug in TemporalMultiStep # by default inferenceType = options['inferenceType'] if inferenceType == 'MultiStep': inferenceType = InferenceType.TemporalMultiStep tokenReplacements['\$INFERENCE_TYPE'] = "'%s'" % inferenceType # Nontemporal classificaion uses only encoder and classifier if inferenceType == InferenceType.NontemporalClassification: tokenReplacements['\$SP_ENABLE'] = "False" tokenReplacements['\$TP_ENABLE'] = "False" else: tokenReplacements['\$SP_ENABLE'] = "True" tokenReplacements['\$TP_ENABLE'] = "True" tokenReplacements['\$CLA_CLASSIFIER_IMPL'] = "" tokenReplacements['\$ANOMALY_PARAMS'] = pprint.pformat( options['anomalyParams'], indent=2*_INDENT_STEP) tokenReplacements['\$ENCODER_SPECS'] = encoderSpecsStr tokenReplacements['\$SENSOR_AUTO_RESET'] = sensorAutoResetStr tokenReplacements['\$AGGREGATION_INFO'] = aggregationInfoStr tokenReplacements['\$DATASET_SPEC'] = datasetSpecStr if options['iterationCount'] is None: options['iterationCount'] = -1 tokenReplacements['\$ITERATION_COUNT'] \ = str(options['iterationCount']) tokenReplacements['\$SP_POOL_PCT'] \ = str(options['spCoincInputPoolPct']) tokenReplacements['\$HS_MIN_PARTICLES'] \ = str(options['minParticlesPerSwarm']) tokenReplacements['\$SP_PERM_CONNECTED'] \ = str(options['spSynPermConnected']) tokenReplacements['\$FIELD_PERMUTATION_LIMIT'] \ = str(options['fieldPermutationLimit']) tokenReplacements['\$PERM_ENCODER_CHOICES'] \ = permEncoderChoicesStr predictionSteps = options['inferenceArgs'].get('predictionSteps', [1]) predictionStepsStr = ','.join([str(x) for x in predictionSteps]) tokenReplacements['\$PREDICTION_STEPS'] = "'%s'" % (predictionStepsStr) tokenReplacements['\$PREDICT_AHEAD_TIME'] = predictAheadTimeStr # Option permuting over SP synapse decrement value tokenReplacements['\$PERM_SP_CHOICES'] = "" if options['spPermuteDecrement'] \ and options['inferenceType'] != 'NontemporalClassification': tokenReplacements['\$PERM_SP_CHOICES'] = \ _ONE_INDENT +"'synPermInactiveDec': PermuteFloat(0.0003, 0.1),\n" # The TM permutation parameters are not required for non-temporal networks if options['inferenceType'] in ['NontemporalMultiStep', 'NontemporalClassification']: tokenReplacements['\$PERM_TP_CHOICES'] = "" else: tokenReplacements['\$PERM_TP_CHOICES'] = \ " 'activationThreshold': PermuteInt(12, 16),\n" \ + " 'minThreshold': PermuteInt(9, 12),\n" \ + " 'pamLength': PermuteInt(1, 5),\n" # If the inference type is just the generic 'MultiStep', then permute over # temporal/nonTemporal multistep if options['inferenceType'] == 'MultiStep': tokenReplacements['\$PERM_INFERENCE_TYPE_CHOICES'] = \ " 'inferenceType': PermuteChoices(['NontemporalMultiStep', " \ + "'TemporalMultiStep'])," else: tokenReplacements['\$PERM_INFERENCE_TYPE_CHOICES'] = "" # The Classifier permutation parameters are only required for # Multi-step inference types if options['inferenceType'] in ['NontemporalMultiStep', 'TemporalMultiStep', 'MultiStep', 'TemporalAnomaly', 'NontemporalClassification']: tokenReplacements['\$PERM_CL_CHOICES'] = \ " 'alpha': PermuteFloat(0.0001, 0.1),\n" else: tokenReplacements['\$PERM_CL_CHOICES'] = "" # The Permutations alwaysIncludePredictedField setting. # * When the experiment description has 'inputPredictedField' set to 'no', we # simply do not put in an encoder for the predicted field. # * When 'inputPredictedField' is set to 'auto', we include an encoder for the # predicted field and swarming tries it out just like all the other fields. # * When 'inputPredictedField' is set to 'yes', we include this setting in # the permutations file which informs swarming to always use the # predicted field (the first swarm will be the predicted field only) tokenReplacements['\$PERM_ALWAYS_INCLUDE_PREDICTED_FIELD'] = \ "inputPredictedField = '%s'" % \ (options["inferenceArgs"]["inputPredictedField"]) # The Permutations minFieldContribution setting if options.get('minFieldContribution', None) is not None: tokenReplacements['\$PERM_MIN_FIELD_CONTRIBUTION'] = \ "minFieldContribution = %d" % (options['minFieldContribution']) else: tokenReplacements['\$PERM_MIN_FIELD_CONTRIBUTION'] = "" # The Permutations killUselessSwarms setting if options.get('killUselessSwarms', None) is not None: tokenReplacements['\$PERM_KILL_USELESS_SWARMS'] = \ "killUselessSwarms = %r" % (options['killUselessSwarms']) else: tokenReplacements['\$PERM_KILL_USELESS_SWARMS'] = "" # The Permutations maxFieldBranching setting if options.get('maxFieldBranching', None) is not None: tokenReplacements['\$PERM_MAX_FIELD_BRANCHING'] = \ "maxFieldBranching = %r" % (options['maxFieldBranching']) else: tokenReplacements['\$PERM_MAX_FIELD_BRANCHING'] = "" # The Permutations tryAll3FieldCombinations setting if options.get('tryAll3FieldCombinations', None) is not None: tokenReplacements['\$PERM_TRY_ALL_3_FIELD_COMBINATIONS'] = \ "tryAll3FieldCombinations = %r" % (options['tryAll3FieldCombinations']) else: tokenReplacements['\$PERM_TRY_ALL_3_FIELD_COMBINATIONS'] = "" # The Permutations tryAll3FieldCombinationsWTimestamps setting if options.get('tryAll3FieldCombinationsWTimestamps', None) is not None: tokenReplacements['\$PERM_TRY_ALL_3_FIELD_COMBINATIONS_W_TIMESTAMPS'] = \ "tryAll3FieldCombinationsWTimestamps = %r" % \ (options['tryAll3FieldCombinationsWTimestamps']) else: tokenReplacements['\$PERM_TRY_ALL_3_FIELD_COMBINATIONS_W_TIMESTAMPS'] = "" # The Permutations fieldFields setting if options.get('fixedFields', None) is not None: tokenReplacements['\$PERM_FIXED_FIELDS'] = \ "fixedFields = %r" % (options['fixedFields']) else: tokenReplacements['\$PERM_FIXED_FIELDS'] = "" # The Permutations fastSwarmModelParams setting if options.get('fastSwarmModelParams', None) is not None: tokenReplacements['\$PERM_FAST_SWARM_MODEL_PARAMS'] = \ "fastSwarmModelParams = %r" % (options['fastSwarmModelParams']) else: tokenReplacements['\$PERM_FAST_SWARM_MODEL_PARAMS'] = "" # The Permutations maxModels setting if options.get('maxModels', None) is not None: tokenReplacements['\$PERM_MAX_MODELS'] = \ "maxModels = %r" % (options['maxModels']) else: tokenReplacements['\$PERM_MAX_MODELS'] = "" # -------------------------------------------------------------------------- # The Aggregation choices have to be determined when we are permuting over # aggregations. if options['dynamicPredictionSteps']: debugAgg = True # First, we need to error check to insure that computeInterval is an integer # multiple of minAggregation (aggregationPeriod) quotient = aggregationDivide(computeInterval, aggregationPeriod) (isInt, multiple) = _isInt(quotient) if not isInt or multiple < 1: raise _InvalidCommandArgException("Invalid computeInterval: %s. " "computeInterval must be an integer multiple of the stream " "aggregation (%s)." % (computeInterval, aggregationPeriod)) # The valid aggregation choices are governed by the following constraint, # 1.) (minAggregation * N) * M = predictAheadTime # (minAggregation * N) * M = maxPredictionSteps * minAggregation # N * M = maxPredictionSteps # # 2.) computeInterval = K * aggregation # computeInterval = K * (minAggregation * N) # # where: aggregation = minAggregation * N # K, M and N are integers >= 1 # N = aggregation / minAggregation # M = predictionSteps, for a particular aggregation # K = number of predictions within each compute interval # # Let's build up a a list of the possible N's that satisfy the # N * M = maxPredictionSteps constraint mTimesN = float(predictionSteps[0]) possibleNs = [] for n in xrange(1, int(mTimesN)+1): m = mTimesN / n mInt = int(round(m)) if mInt < 1: break if abs(m - mInt) > 0.0001 * m: continue possibleNs.append(n) if debugAgg: print "All integer factors of %d are: %s" % (mTimesN, possibleNs) # Now go through and throw out any N's that don't satisfy the constraint: # computeInterval = K * (minAggregation * N) aggChoices = [] for n in possibleNs: # Compute minAggregation * N agg = dict(aggregationPeriod) for key in agg.iterkeys(): agg[key] *= n # Make sure computeInterval is an integer multiple of the aggregation # period quotient = aggregationDivide(computeInterval, agg) #print computeInterval, agg #print quotient #import sys; sys.exit() (isInt, multiple) = _isInt(quotient) if not isInt or multiple < 1: continue aggChoices.append(agg) # Only eveluate up to 5 different aggregations aggChoices = aggChoices[-5:] if debugAgg: print "Aggregation choices that will be evaluted during swarming:" for agg in aggChoices: print " ==>", agg print tokenReplacements['\$PERM_AGGREGATION_CHOICES'] = ( "PermuteChoices(%s)" % ( pprint.pformat(aggChoices, indent=2*_INDENT_STEP))) else: tokenReplacements['\$PERM_AGGREGATION_CHOICES'] = aggregationInfoStr # Generate the inferenceArgs replacement tokens _generateInferenceArgs(options, tokenReplacements) # Generate the metric replacement tokens _generateMetricsSubstitutions(options, tokenReplacements) # ----------------------------------------------------------------------- # Generate Control dictionary environment = options['environment'] if environment == OpfEnvironment.Nupic: tokenReplacements['\$ENVIRONMENT'] = "'%s'"%OpfEnvironment.Nupic controlTemplate = "nupicEnvironmentTemplate.tpl" elif environment == OpfEnvironment.Experiment: tokenReplacements['\$ENVIRONMENT'] = "'%s'"%OpfEnvironment.Experiment controlTemplate = "opfExperimentTemplate.tpl" else: raise _InvalidCommandArgException("Invalid environment type %s"% environment) # ----------------------------------------------------------------------- if outputDirPath is None: outputDirPath = tempfile.mkdtemp() if not os.path.exists(outputDirPath): os.makedirs(outputDirPath) print "Generating experiment files in directory: %s..." % (outputDirPath) descriptionPyPath = os.path.join(outputDirPath, "description.py") _generateFileFromTemplates([claDescriptionTemplateFile, controlTemplate], descriptionPyPath, tokenReplacements) permutationsPyPath = os.path.join(outputDirPath, "permutations.py") if hsVersion == 'v1': _generateFileFromTemplates(['permutationsTemplateV1.tpl'],permutationsPyPath, tokenReplacements) elif hsVersion == 'ensemble': _generateFileFromTemplates(['permutationsTemplateEnsemble.tpl'],permutationsPyPath, tokenReplacements) elif hsVersion == 'v2': _generateFileFromTemplates(['permutationsTemplateV2.tpl'],permutationsPyPath, tokenReplacements) else: raise(ValueError("This permutation version is not supported yet: %s" % hsVersion)) print "done."
def _generateMetricsSubstitutions(options, tokenReplacements): """Generate the token substitution for metrics related fields. This includes: \$METRICS \$LOGGED_METRICS \$PERM_OPTIMIZE_SETTING """ # ----------------------------------------------------------------------- # options['loggedMetrics'] = [".*"] # ----------------------------------------------------------------------- # Generate the required metrics metricList, optimizeMetricLabel = _generateMetricSpecs(options) metricListString = ",\n".join(metricList) metricListString = _indentLines(metricListString, 2, indentFirstLine=False) permOptimizeSettingStr = 'minimize = "%s"' % optimizeMetricLabel # ----------------------------------------------------------------------- # Specify which metrics should be logged loggedMetricsListAsStr = "[%s]" % (", ".join(["'%s'"% ptrn for ptrn in options['loggedMetrics']])) tokenReplacements['\$LOGGED_METRICS'] \ = loggedMetricsListAsStr tokenReplacements['\$METRICS'] = metricListString tokenReplacements['\$PERM_OPTIMIZE_SETTING'] \ = permOptimizeSettingStr
def _generateMetricSpecs(options): """ Generates the Metrics for a given InferenceType Parameters: ------------------------------------------------------------------------- options: ExpGenerator options retval: (metricsList, optimizeMetricLabel) metricsList: list of metric string names optimizeMetricLabel: Name of the metric which to optimize over """ inferenceType = options['inferenceType'] inferenceArgs = options['inferenceArgs'] predictionSteps = inferenceArgs['predictionSteps'] metricWindow = options['metricWindow'] if metricWindow is None: metricWindow = int(Configuration.get("nupic.opf.metricWindow")) metricSpecStrings = [] optimizeMetricLabel = "" # ----------------------------------------------------------------------- # Generate the metrics specified by the expGenerator paramters metricSpecStrings.extend(_generateExtraMetricSpecs(options)) # ----------------------------------------------------------------------- optimizeMetricSpec = None # If using a dynamically computed prediction steps (i.e. when swarming # over aggregation is requested), then we will plug in the variable # predictionSteps in place of the statically provided predictionSteps # from the JSON description. if options['dynamicPredictionSteps']: assert len(predictionSteps) == 1 predictionSteps = ['$REPLACE_ME'] # ----------------------------------------------------------------------- # Metrics for temporal prediction if inferenceType in (InferenceType.TemporalNextStep, InferenceType.TemporalAnomaly, InferenceType.TemporalMultiStep, InferenceType.NontemporalMultiStep, InferenceType.NontemporalClassification, 'MultiStep'): predictedFieldName, predictedFieldType = _getPredictedField(options) isCategory = _isCategory(predictedFieldType) metricNames = ('avg_err',) if isCategory else ('aae', 'altMAPE') trivialErrorMetric = 'avg_err' if isCategory else 'altMAPE' oneGramErrorMetric = 'avg_err' if isCategory else 'altMAPE' movingAverageBaselineName = 'moving_mode' if isCategory else 'moving_mean' # Multi-step metrics for metricName in metricNames: metricSpec, metricLabel = \ _generateMetricSpecString(field=predictedFieldName, inferenceElement=InferenceElement.multiStepBestPredictions, metric='multiStep', params={'errorMetric': metricName, 'window':metricWindow, 'steps': predictionSteps}, returnLabel=True) metricSpecStrings.append(metricSpec) # If the custom error metric was specified, add that if options["customErrorMetric"] is not None : metricParams = dict(options["customErrorMetric"]) metricParams['errorMetric'] = 'custom_error_metric' metricParams['steps'] = predictionSteps # If errorWindow is not specified, make it equal to the default window if not "errorWindow" in metricParams: metricParams["errorWindow"] = metricWindow metricSpec, metricLabel =_generateMetricSpecString(field=predictedFieldName, inferenceElement=InferenceElement.multiStepPredictions, metric="multiStep", params=metricParams, returnLabel=True) metricSpecStrings.append(metricSpec) # If this is the first specified step size, optimize for it. Be sure to # escape special characters since this is a regular expression optimizeMetricSpec = metricSpec metricLabel = metricLabel.replace('[', '\\[') metricLabel = metricLabel.replace(']', '\\]') optimizeMetricLabel = metricLabel if options["customErrorMetric"] is not None : optimizeMetricLabel = ".*custom_error_metric.*" # Add in the trivial metrics if options["runBaselines"] \ and inferenceType != InferenceType.NontemporalClassification: for steps in predictionSteps: metricSpecStrings.append( _generateMetricSpecString(field=predictedFieldName, inferenceElement=InferenceElement.prediction, metric="trivial", params={'window':metricWindow, "errorMetric":trivialErrorMetric, 'steps': steps}) ) ##Add in the One-Gram baseline error metric #metricSpecStrings.append( # _generateMetricSpecString(field=predictedFieldName, # inferenceElement=InferenceElement.encodings, # metric="two_gram", # params={'window':metricWindow, # "errorMetric":oneGramErrorMetric, # 'predictionField':predictedFieldName, # 'steps': steps}) # ) # #Include the baseline moving mean/mode metric if isCategory: metricSpecStrings.append( _generateMetricSpecString(field=predictedFieldName, inferenceElement=InferenceElement.prediction, metric=movingAverageBaselineName, params={'window':metricWindow ,"errorMetric":"avg_err", "mode_window":200, "steps": steps}) ) else : metricSpecStrings.append( _generateMetricSpecString(field=predictedFieldName, inferenceElement=InferenceElement.prediction, metric=movingAverageBaselineName, params={'window':metricWindow ,"errorMetric":"altMAPE", "mean_window":200, "steps": steps}) ) # ----------------------------------------------------------------------- # Metrics for classification elif inferenceType in (InferenceType.TemporalClassification): metricName = 'avg_err' trivialErrorMetric = 'avg_err' oneGramErrorMetric = 'avg_err' movingAverageBaselineName = 'moving_mode' optimizeMetricSpec, optimizeMetricLabel = \ _generateMetricSpecString(inferenceElement=InferenceElement.classification, metric=metricName, params={'window':metricWindow}, returnLabel=True) metricSpecStrings.append(optimizeMetricSpec) if options["runBaselines"]: # If temporal, generate the trivial predictor metric if inferenceType == InferenceType.TemporalClassification: metricSpecStrings.append( _generateMetricSpecString(inferenceElement=InferenceElement.classification, metric="trivial", params={'window':metricWindow, "errorMetric":trivialErrorMetric}) ) metricSpecStrings.append( _generateMetricSpecString(inferenceElement=InferenceElement.classification, metric="two_gram", params={'window':metricWindow, "errorMetric":oneGramErrorMetric}) ) metricSpecStrings.append( _generateMetricSpecString(inferenceElement=InferenceElement.classification, metric=movingAverageBaselineName, params={'window':metricWindow ,"errorMetric":"avg_err", "mode_window":200}) ) # Custom Error Metric if not options["customErrorMetric"] == None : #If errorWindow is not specified, make it equal to the default window if not "errorWindow" in options["customErrorMetric"]: options["customErrorMetric"]["errorWindow"] = metricWindow optimizeMetricSpec = _generateMetricSpecString( inferenceElement=InferenceElement.classification, metric="custom", params=options["customErrorMetric"]) optimizeMetricLabel = ".*custom_error_metric.*" metricSpecStrings.append(optimizeMetricSpec) # ----------------------------------------------------------------------- # If plug in the predictionSteps variable for any dynamically generated # prediction steps if options['dynamicPredictionSteps']: for i in range(len(metricSpecStrings)): metricSpecStrings[i] = metricSpecStrings[i].replace( "'$REPLACE_ME'", "predictionSteps") optimizeMetricLabel = optimizeMetricLabel.replace( "'$REPLACE_ME'", ".*") return metricSpecStrings, optimizeMetricLabel
def _generateExtraMetricSpecs(options): """Generates the non-default metrics specified by the expGenerator params """ _metricSpecSchema = {'properties': {}} results = [] for metric in options['metrics']: for propertyName in _metricSpecSchema['properties'].keys(): _getPropertyValue(_metricSpecSchema, propertyName, metric) specString, label = _generateMetricSpecString( field=metric['field'], metric=metric['metric'], params=metric['params'], inferenceElement=\ metric['inferenceElement'], returnLabel=True) if metric['logged']: options['loggedMetrics'].append(label) results.append(specString) return results
def _getPredictedField(options): """ Gets the predicted field and it's datatype from the options dictionary Returns: (predictedFieldName, predictedFieldType) """ if not options['inferenceArgs'] or \ not options['inferenceArgs']['predictedField']: return None, None predictedField = options['inferenceArgs']['predictedField'] predictedFieldInfo = None includedFields = options['includedFields'] for info in includedFields: if info['fieldName'] == predictedField: predictedFieldInfo = info break if predictedFieldInfo is None: raise ValueError( "Predicted field '%s' does not exist in included fields." % predictedField ) predictedFieldType = predictedFieldInfo['fieldType'] return predictedField, predictedFieldType
def _generateInferenceArgs(options, tokenReplacements): """ Generates the token substitutions related to the predicted field and the supplemental arguments for prediction """ inferenceType = options['inferenceType'] optionInferenceArgs = options.get('inferenceArgs', None) resultInferenceArgs = {} predictedField = _getPredictedField(options)[0] if inferenceType in (InferenceType.TemporalNextStep, InferenceType.TemporalAnomaly): assert predictedField, "Inference Type '%s' needs a predictedField "\ "specified in the inferenceArgs dictionary"\ % inferenceType if optionInferenceArgs: # If we will be using a dynamically created predictionSteps, plug in that # variable name in place of the constant scalar value if options['dynamicPredictionSteps']: altOptionInferenceArgs = copy.deepcopy(optionInferenceArgs) altOptionInferenceArgs['predictionSteps'] = '$REPLACE_ME' resultInferenceArgs = pprint.pformat(altOptionInferenceArgs) resultInferenceArgs = resultInferenceArgs.replace("'$REPLACE_ME'", '[predictionSteps]') else: resultInferenceArgs = pprint.pformat(optionInferenceArgs) tokenReplacements['\$INFERENCE_ARGS'] = resultInferenceArgs tokenReplacements['\$PREDICTION_FIELD'] = predictedField
def expGenerator(args): """ Parses, validates, and executes command-line options; On success: Performs requested operation and exits program normally On Error: Dumps exception/error info in JSON format to stdout and exits the program with non-zero status. """ # ----------------------------------------------------------------- # Parse command line options # parser = OptionParser() parser.set_usage("%prog [options] --description='{json object with args}'\n" + \ "%prog [options] --descriptionFromFile='{filename}'\n" + \ "%prog [options] --showSchema") parser.add_option("--description", dest = "description", help = "Tells ExpGenerator to generate an experiment description.py and " \ "permutations.py file using the given JSON formatted experiment "\ "description string.") parser.add_option("--descriptionFromFile", dest = 'descriptionFromFile', help = "Tells ExpGenerator to open the given filename and use it's " \ "contents as the JSON formatted experiment description.") parser.add_option("--claDescriptionTemplateFile", dest = 'claDescriptionTemplateFile', default = 'claDescriptionTemplate.tpl', help = "The file containing the template description file for " \ " ExpGenerator [default: %default]") parser.add_option("--showSchema", action="store_true", dest="showSchema", help="Prints the JSON schemas for the --description arg.") parser.add_option("--version", dest = 'version', default='v2', help = "Generate the permutations file for this version of hypersearch." " Possible choices are 'v1' and 'v2' [default: %default].") parser.add_option("--outDir", dest = "outDir", default=None, help = "Where to generate experiment. If not specified, " \ "then a temp directory will be created" ) (options, remainingArgs) = parser.parse_args(args) #print("OPTIONS=%s" % (str(options))) # ----------------------------------------------------------------- # Check for unprocessed args # if len(remainingArgs) > 0: raise _InvalidCommandArgException( _makeUsageErrorStr("Unexpected command-line args: <%s>" % \ (' '.join(remainingArgs),), parser.get_usage())) # ----------------------------------------------------------------- # Check for use of mutually-exclusive options # activeOptions = filter(lambda x: getattr(options, x) != None, ('description', 'showSchema')) if len(activeOptions) > 1: raise _InvalidCommandArgException( _makeUsageErrorStr(("The specified command options are " + \ "mutually-exclusive: %s") % (activeOptions,), parser.get_usage())) # ----------------------------------------------------------------- # Process requests # if options.showSchema: _handleShowSchemaOption() elif options.description: _handleDescriptionOption(options.description, options.outDir, parser.get_usage(), hsVersion=options.version, claDescriptionTemplateFile = options.claDescriptionTemplateFile) elif options.descriptionFromFile: _handleDescriptionFromFileOption(options.descriptionFromFile, options.outDir, parser.get_usage(), hsVersion=options.version, claDescriptionTemplateFile = options.claDescriptionTemplateFile) else: raise _InvalidCommandArgException( _makeUsageErrorStr("Error in validating command options. No option " "provided:\n", parser.get_usage()))
def parseTimestamp(s): """ Parses a textual datetime format and return a Python datetime object. The supported format is: ``yyyy-mm-dd h:m:s.ms`` The time component is optional. - hours are 00..23 (no AM/PM) - minutes are 00..59 - seconds are 00..59 - micro-seconds are 000000..999999 :param s: (string) input time text :return: (datetime.datetime) """ s = s.strip() for pattern in DATETIME_FORMATS: try: return datetime.datetime.strptime(s, pattern) except ValueError: pass raise ValueError('The provided timestamp %s is malformed. The supported ' 'formats are: [%s]' % (s, ', '.join(DATETIME_FORMATS)))
def parseBool(s): """ String to boolean :param s: (string) :return: (bool) """ l = s.lower() if l in ("true", "t", "1"): return True if l in ("false", "f", "0"): return False raise Exception("Unable to convert string '%s' to a boolean value" % s)
def escape(s): """ Escape commas, tabs, newlines and dashes in a string Commas are encoded as tabs. :param s: (string) to escape :returns: (string) escaped string """ if s is None: return '' assert isinstance(s, basestring), \ "expected %s but got %s; value=%s" % (basestring, type(s), s) s = s.replace('\\', '\\\\') s = s.replace('\n', '\\n') s = s.replace('\t', '\\t') s = s.replace(',', '\t') return s
def unescape(s): """ Unescapes a string that may contain commas, tabs, newlines and dashes Commas are decoded from tabs. :param s: (string) to unescape :returns: (string) unescaped string """ assert isinstance(s, basestring) s = s.replace('\t', ',') s = s.replace('\\,', ',') s = s.replace('\\n', '\n') s = s.replace('\\\\', '\\') return s
def parseSdr(s): """ Parses a string containing only 0's and 1's and return a Python list object. :param s: (string) string to parse :returns: (list) SDR out """ assert isinstance(s, basestring) sdr = [int(c) for c in s if c in ("0", "1")] if len(sdr) != len(s): raise ValueError("The provided string %s is malformed. The string should " "have only 0's and 1's.") return sdr
def parseStringList(s): """ Parse a string of space-separated numbers, returning a Python list. :param s: (string) to parse :returns: (list) binary SDR """ assert isinstance(s, basestring) return [int(i) for i in s.split()]
def coordinatesFromIndex(index, dimensions): """ Translate an index into coordinates, using the given coordinate system. Similar to ``numpy.unravel_index``. :param index: (int) The index of the point. The coordinates are expressed as a single index by using the dimensions as a mixed radix definition. For example, in dimensions 42x10, the point [1, 4] is index 1*420 + 4*10 = 460. :param dimensions (list of ints) The coordinate system. :returns: (list) of coordinates of length ``len(dimensions)``. """ coordinates = [0] * len(dimensions) shifted = index for i in xrange(len(dimensions) - 1, 0, -1): coordinates[i] = shifted % dimensions[i] shifted = shifted / dimensions[i] coordinates[0] = shifted return coordinates
def indexFromCoordinates(coordinates, dimensions): """ Translate coordinates into an index, using the given coordinate system. Similar to ``numpy.ravel_multi_index``. :param coordinates: (list of ints) A list of coordinates of length ``dimensions.size()``. :param dimensions: (list of ints) The coordinate system. :returns: (int) The index of the point. The coordinates are expressed as a single index by using the dimensions as a mixed radix definition. For example, in dimensions 42x10, the point [1, 4] is index 1*420 + 4*10 = 460. """ index = 0 for i, dimension in enumerate(dimensions): index *= dimension index += coordinates[i] return index
def neighborhood(centerIndex, radius, dimensions): """ Get the points in the neighborhood of a point. A point's neighborhood is the n-dimensional hypercube with sides ranging [center - radius, center + radius], inclusive. For example, if there are two dimensions and the radius is 3, the neighborhood is 6x6. Neighborhoods are truncated when they are near an edge. This is designed to be fast. In C++ it's fastest to iterate through neighbors one by one, calculating them on-demand rather than creating a list of them. But in Python it's faster to build up the whole list in batch via a few calls to C code rather than calculating them on-demand with lots of calls to Python code. :param centerIndex: (int) The index of the point. The coordinates are expressed as a single index by using the dimensions as a mixed radix definition. For example, in dimensions 42x10, the point [1, 4] is index 1*420 + 4*10 = 460. :param radius: (int) The radius of this neighborhood about the ``centerIndex``. :param dimensions: (indexable sequence) The dimensions of the world outside this neighborhood. :returns: (numpy array) The points in the neighborhood, including ``centerIndex``. """ centerPosition = coordinatesFromIndex(centerIndex, dimensions) intervals = [] for i, dimension in enumerate(dimensions): left = max(0, centerPosition[i] - radius) right = min(dimension - 1, centerPosition[i] + radius) intervals.append(xrange(left, right + 1)) coords = numpy.array(list(itertools.product(*intervals))) return numpy.ravel_multi_index(coords.T, dimensions)
def encodeIntoArray(self, inputData, output): """ See `nupic.encoders.base.Encoder` for more information. @param inputData (tuple) Contains coordinate (numpy.array, N-dimensional integer coordinate) and radius (int) @param output (numpy.array) Stores encoded SDR in this numpy array """ (coordinate, radius) = inputData assert isinstance(radius, int), ("Expected integer radius, got: {} ({})" .format(radius, type(radius))) neighbors = self._neighbors(coordinate, radius) winners = self._topWCoordinates(neighbors, self.w) bitFn = lambda coordinate: self._bitForCoordinate(coordinate, self.n) indices = numpy.array([bitFn(w) for w in winners]) output[:] = 0 output[indices] = 1
def _neighbors(coordinate, radius): """ Returns coordinates around given coordinate, within given radius. Includes given coordinate. @param coordinate (numpy.array) N-dimensional integer coordinate @param radius (int) Radius around `coordinate` @return (numpy.array) List of coordinates """ ranges = (xrange(n-radius, n+radius+1) for n in coordinate.tolist()) return numpy.array(list(itertools.product(*ranges)))
def _topWCoordinates(cls, coordinates, w): """ Returns the top W coordinates by order. @param coordinates (numpy.array) A 2D numpy array, where each element is a coordinate @param w (int) Number of top coordinates to return @return (numpy.array) A subset of `coordinates`, containing only the top ones by order """ orders = numpy.array([cls._orderForCoordinate(c) for c in coordinates.tolist()]) indices = numpy.argsort(orders)[-w:] return coordinates[indices]
def _hashCoordinate(coordinate): """Hash a coordinate to a 64 bit integer.""" coordinateStr = ",".join(str(v) for v in coordinate) # Compute the hash and convert to 64 bit int. hash = int(int(hashlib.md5(coordinateStr).hexdigest(), 16) % (2 ** 64)) return hash
def _orderForCoordinate(cls, coordinate): """ Returns the order for a coordinate. @param coordinate (numpy.array) Coordinate @return (float) A value in the interval [0, 1), representing the order of the coordinate """ seed = cls._hashCoordinate(coordinate) rng = Random(seed) return rng.getReal64()
def _bitForCoordinate(cls, coordinate, n): """ Maps the coordinate to a bit in the SDR. @param coordinate (numpy.array) Coordinate @param n (int) The number of available bits in the SDR @return (int) The index to a bit in the SDR """ seed = cls._hashCoordinate(coordinate) rng = Random(seed) return rng.getUInt32(n)
def binSearch(arr, val): """ Function for running binary search on a sorted list. :param arr: (list) a sorted list of integers to search :param val: (int) a integer to search for in the sorted array :returns: (int) the index of the element if it is found and -1 otherwise. """ i = bisect_left(arr, val) if i != len(arr) and arr[i] == val: return i return -1
def createSegment(self, cell): """ Adds a new segment on a cell. :param cell: (int) Cell index :returns: (int) New segment index """ cellData = self._cells[cell] if len(self._freeFlatIdxs) > 0: flatIdx = self._freeFlatIdxs.pop() else: flatIdx = self._nextFlatIdx self._segmentForFlatIdx.append(None) self._nextFlatIdx += 1 ordinal = self._nextSegmentOrdinal self._nextSegmentOrdinal += 1 segment = Segment(cell, flatIdx, ordinal) cellData._segments.append(segment) self._segmentForFlatIdx[flatIdx] = segment return segment
def destroySegment(self, segment): """ Destroys a segment. :param segment: (:class:`Segment`) representing the segment to be destroyed. """ # Remove the synapses from all data structures outside this Segment. for synapse in segment._synapses: self._removeSynapseFromPresynapticMap(synapse) self._numSynapses -= len(segment._synapses) # Remove the segment from the cell's list. segments = self._cells[segment.cell]._segments i = segments.index(segment) del segments[i] # Free the flatIdx and remove the final reference so the Segment can be # garbage-collected. self._freeFlatIdxs.append(segment.flatIdx) self._segmentForFlatIdx[segment.flatIdx] = None
def createSynapse(self, segment, presynapticCell, permanence): """ Creates a new synapse on a segment. :param segment: (:class:`Segment`) Segment object for synapse to be synapsed to. :param presynapticCell: (int) Source cell index. :param permanence: (float) Initial permanence of synapse. :returns: (:class:`Synapse`) created synapse """ idx = len(segment._synapses) synapse = Synapse(segment, presynapticCell, permanence, self._nextSynapseOrdinal) self._nextSynapseOrdinal += 1 segment._synapses.add(synapse) self._synapsesForPresynapticCell[presynapticCell].add(synapse) self._numSynapses += 1 return synapse
def destroySynapse(self, synapse): """ Destroys a synapse. :param synapse: (:class:`Synapse`) synapse to destroy """ self._numSynapses -= 1 self._removeSynapseFromPresynapticMap(synapse) synapse.segment._synapses.remove(synapse)
def computeActivity(self, activePresynapticCells, connectedPermanence): """ Compute each segment's number of active synapses for a given input. In the returned lists, a segment's active synapse count is stored at index ``segment.flatIdx``. :param activePresynapticCells: (iter) Active cells. :param connectedPermanence: (float) Permanence threshold for a synapse to be considered connected :returns: (tuple) (``numActiveConnectedSynapsesForSegment`` [list], ``numActivePotentialSynapsesForSegment`` [list]) """ numActiveConnectedSynapsesForSegment = [0] * self._nextFlatIdx numActivePotentialSynapsesForSegment = [0] * self._nextFlatIdx threshold = connectedPermanence - EPSILON for cell in activePresynapticCells: for synapse in self._synapsesForPresynapticCell[cell]: flatIdx = synapse.segment.flatIdx numActivePotentialSynapsesForSegment[flatIdx] += 1 if synapse.permanence > threshold: numActiveConnectedSynapsesForSegment[flatIdx] += 1 return (numActiveConnectedSynapsesForSegment, numActivePotentialSynapsesForSegment)
def numSegments(self, cell=None): """ Returns the number of segments. :param cell: (int) Optional parameter to get the number of segments on a cell. :returns: (int) Number of segments on all cells if cell is not specified, or on a specific specified cell """ if cell is not None: return len(self._cells[cell]._segments) return self._nextFlatIdx - len(self._freeFlatIdxs)
def segmentPositionSortKey(self, segment): """ Return a numeric key for sorting this segment. This can be used with the python built-in ``sorted()`` function. :param segment: (:class:`Segment`) within this :class:`Connections` instance. :returns: (float) A numeric key for sorting. """ return segment.cell + (segment._ordinal / float(self._nextSegmentOrdinal))
def write(self, proto): """ Writes serialized data to proto object. :param proto: (DynamicStructBuilder) Proto object """ protoCells = proto.init('cells', self.numCells) for i in xrange(self.numCells): segments = self._cells[i]._segments protoSegments = protoCells[i].init('segments', len(segments)) for j, segment in enumerate(segments): synapses = segment._synapses protoSynapses = protoSegments[j].init('synapses', len(synapses)) for k, synapse in enumerate(sorted(synapses, key=lambda s: s._ordinal)): protoSynapses[k].presynapticCell = synapse.presynapticCell protoSynapses[k].permanence = synapse.permanence
def read(cls, proto): """ Reads deserialized data from proto object :param proto: (DynamicStructBuilder) Proto object :returns: (:class:`Connections`) instance """ #pylint: disable=W0212 protoCells = proto.cells connections = cls(len(protoCells)) for cellIdx, protoCell in enumerate(protoCells): protoCell = protoCells[cellIdx] protoSegments = protoCell.segments connections._cells[cellIdx] = CellData() segments = connections._cells[cellIdx]._segments for segmentIdx, protoSegment in enumerate(protoSegments): segment = Segment(cellIdx, connections._nextFlatIdx, connections._nextSegmentOrdinal) segments.append(segment) connections._segmentForFlatIdx.append(segment) connections._nextFlatIdx += 1 connections._nextSegmentOrdinal += 1 synapses = segment._synapses protoSynapses = protoSegment.synapses for synapseIdx, protoSynapse in enumerate(protoSynapses): presynapticCell = protoSynapse.presynapticCell synapse = Synapse(segment, presynapticCell, protoSynapse.permanence, ordinal=connections._nextSynapseOrdinal) connections._nextSynapseOrdinal += 1 synapses.add(synapse) connections._synapsesForPresynapticCell[presynapticCell].add(synapse) connections._numSynapses += 1 #pylint: enable=W0212 return connections
def getString(cls, prop): """ Retrieve the requested property as a string. If property does not exist, then KeyError will be raised. :param prop: (string) name of the property :raises: KeyError :returns: (string) property value """ if cls._properties is None: cls._readStdConfigFiles() # Allow configuration properties to be overridden via environment variables envValue = os.environ.get("%s%s" % (cls.envPropPrefix, prop.replace('.', '_')), None) if envValue is not None: return envValue return cls._properties[prop]
def getBool(cls, prop): """ Retrieve the requested property and return it as a bool. If property does not exist, then KeyError will be raised. If the property value is neither 0 nor 1, then ValueError will be raised :param prop: (string) name of the property :raises: KeyError, ValueError :returns: (bool) property value """ value = cls.getInt(prop) if value not in (0, 1): raise ValueError("Expected 0 or 1, but got %r in config property %s" % ( value, prop)) return bool(value)
def set(cls, prop, value): """ Set the value of the given configuration property. :param prop: (string) name of the property :param value: (object) value to set """ if cls._properties is None: cls._readStdConfigFiles() cls._properties[prop] = str(value)
def dict(cls): """ Return a dict containing all of the configuration properties :returns: (dict) containing all configuration properties. """ if cls._properties is None: cls._readStdConfigFiles() # Make a copy so we can update any current values obtained from environment # variables result = dict(cls._properties) keys = os.environ.keys() replaceKeys = filter(lambda x: x.startswith(cls.envPropPrefix), keys) for envKey in replaceKeys: key = envKey[len(cls.envPropPrefix):] key = key.replace('_', '.') result[key] = os.environ[envKey] return result
def readConfigFile(cls, filename, path=None): """ Parse the given XML file and store all properties it describes. :param filename: (string) name of XML file to parse (no path) :param path: (string) path of the XML file. If None, then use the standard configuration search path. """ properties = cls._readConfigFile(filename, path) # Create properties dict if necessary if cls._properties is None: cls._properties = dict() for name in properties: if 'value' in properties[name]: cls._properties[name] = properties[name]['value']
def findConfigFile(cls, filename): """ Search the configuration path (specified via the NTA_CONF_PATH environment variable) for the given filename. If found, return the complete path to the file. :param filename: (string) name of file to locate """ paths = cls.getConfigPaths() for p in paths: testPath = os.path.join(p, filename) if os.path.isfile(testPath): return os.path.join(p, filename)
def getConfigPaths(cls): """ Return the list of paths to search for configuration files. :returns: (list) of paths """ configPaths = [] if cls._configPaths is not None: return cls._configPaths else: if 'NTA_CONF_PATH' in os.environ: configVar = os.environ['NTA_CONF_PATH'] # Return as a list of paths configPaths = configVar.split(os.pathsep) return configPaths
def addNoise(input, noise=0.1, doForeground=True, doBackground=True): """ Add noise to the given input. Parameters: ----------------------------------------------- input: the input to add noise to noise: how much noise to add doForeground: If true, turn off some of the 1 bits in the input doBackground: If true, turn on some of the 0 bits in the input """ if doForeground and doBackground: return numpy.abs(input - (numpy.random.random(input.shape) < noise)) else: if doForeground: return numpy.logical_and(input, numpy.random.random(input.shape) > noise) if doBackground: return numpy.logical_or(input, numpy.random.random(input.shape) < noise) return input
def generateCoincMatrix(nCoinc=10, length=500, activity=50): """ Generate a coincidence matrix. This is used to generate random inputs to the temporal learner and to compare the predicted output against. It generates a matrix of nCoinc rows, each row has length 'length' and has a total of 'activity' bits on. Parameters: ----------------------------------------------- nCoinc: the number of rows to generate length: the length of each row activity: the number of ones to put into each row. """ coincMatrix0 = SM32(int(nCoinc), int(length)) theOnes = numpy.array([1.0] * activity, dtype=numpy.float32) for rowIdx in xrange(nCoinc): coinc = numpy.array(random.sample(xrange(length), activity), dtype=numpy.uint32) coinc.sort() coincMatrix0.setRowFromSparse(rowIdx, coinc, theOnes) # This is the right code to use, it's faster, but it derails the unit # testing of the pooling for now. coincMatrix = SM32(int(nCoinc), int(length)) coincMatrix.initializeWithFixedNNZR(activity) return coincMatrix0
def generateVectors(numVectors=100, length=500, activity=50): """ Generate a list of random sparse distributed vectors. This is used to generate training vectors to the spatial or temporal learner and to compare the predicted output against. It generates a list of 'numVectors' elements, each element has length 'length' and has a total of 'activity' bits on. Parameters: ----------------------------------------------- numVectors: the number of vectors to generate length: the length of each row activity: the number of ones to put into each row. """ vectors = [] coinc = numpy.zeros(length, dtype='int32') indexList = range(length) for i in xrange(numVectors): coinc[:] = 0 coinc[random.sample(indexList, activity)] = 1 vectors.append(coinc.copy()) return vectors
def generateSimpleSequences(nCoinc=10, seqLength=[5,6,7], nSeq=100): """ Generate a set of simple sequences. The elements of the sequences will be integers from 0 to 'nCoinc'-1. The length of each sequence will be randomly chosen from the 'seqLength' list. Parameters: ----------------------------------------------- nCoinc: the number of elements available to use in the sequences seqLength: a list of possible sequence lengths. The length of each sequence will be randomly chosen from here. nSeq: The number of sequences to generate retval: a list of sequences. Each sequence is itself a list containing the coincidence indices for that sequence. """ coincList = range(nCoinc) seqList = [] for i in xrange(nSeq): if max(seqLength) <= nCoinc: seqList.append(random.sample(coincList, random.choice(seqLength))) else: len = random.choice(seqLength) seq = [] for x in xrange(len): seq.append(random.choice(coincList)) seqList.append(seq) return seqList
def generateHubSequences(nCoinc=10, hubs = [2,6], seqLength=[5,6,7], nSeq=100): """ Generate a set of hub sequences. These are sequences which contain a hub element in the middle. The elements of the sequences will be integers from 0 to 'nCoinc'-1. The hub elements will only appear in the middle of each sequence. The length of each sequence will be randomly chosen from the 'seqLength' list. Parameters: ----------------------------------------------- nCoinc: the number of elements available to use in the sequences hubs: which of the elements will be used as hubs. seqLength: a list of possible sequence lengths. The length of each sequence will be randomly chosen from here. nSeq: The number of sequences to generate retval: a list of sequences. Each sequence is itself a list containing the coincidence indices for that sequence. """ coincList = range(nCoinc) for hub in hubs: coincList.remove(hub) seqList = [] for i in xrange(nSeq): length = random.choice(seqLength)-1 seq = random.sample(coincList,length) seq.insert(length//2, random.choice(hubs)) seqList.append(seq) return seqList
def generateSimpleCoincMatrix(nCoinc=10, length=500, activity=50): """ Generate a non overlapping coincidence matrix. This is used to generate random inputs to the temporal learner and to compare the predicted output against. It generates a matrix of nCoinc rows, each row has length 'length' and has a total of 'activity' bits on. Parameters: ----------------------------------------------- nCoinc: the number of rows to generate length: the length of each row activity: the number of ones to put into each row. """ assert nCoinc*activity<=length, "can't generate non-overlapping coincidences" coincMatrix = SM32(0, length) coinc = numpy.zeros(length, dtype='int32') for i in xrange(nCoinc): coinc[:] = 0 coinc[i*activity:(i+1)*activity] = 1 coincMatrix.addRow(coinc) return coincMatrix
def generateSequences(nPatterns=10, patternLen=500, patternActivity=50, hubs=[2,6], seqLength=[5,6,7], nSimpleSequences=50, nHubSequences=50): """ Generate a set of simple and hub sequences. A simple sequence contains a randomly chosen set of elements from 0 to 'nCoinc-1'. A hub sequence always contains a hub element in the middle of it. Parameters: ----------------------------------------------- nPatterns: the number of patterns to use in the sequences. patternLen: The number of elements in each pattern patternActivity: The number of elements that should be active in each pattern hubs: which of the elements will be used as hubs. seqLength: a list of possible sequence lengths. The length of each sequence will be randomly chosen from here. nSimpleSequences: The number of simple sequences to generate nHubSequences: The number of hub sequences to generate retval: (seqList, patterns) seqList: a list of sequences. Each sequence is itself a list containing the input pattern indices for that sequence. patterns: the input patterns used in the seqList. """ # Create the input patterns patterns = generateCoincMatrix(nCoinc=nPatterns, length=patternLen, activity=patternActivity) # Create the raw sequences seqList = generateSimpleSequences(nCoinc=nPatterns, seqLength=seqLength, nSeq=nSimpleSequences) + \ generateHubSequences(nCoinc=nPatterns, hubs=hubs, seqLength=seqLength, nSeq=nHubSequences) # Return results return (seqList, patterns)
def generateL2Sequences(nL1Patterns=10, l1Hubs=[2,6], l1SeqLength=[5,6,7], nL1SimpleSequences=50, nL1HubSequences=50, l1Pooling=4, perfectStability=False, spHysteresisFactor=1.0, patternLen=500, patternActivity=50): """ Generate the simulated output from a spatial pooler that's sitting on top of another spatial pooler / temporal memory pair. The average on-time of the outputs from the simulated TM is given by the l1Pooling argument. In this routine, L1 refers to the first spatial and temporal memory and L2 refers to the spatial pooler above that. Parameters: ----------------------------------------------- nL1Patterns: the number of patterns to use in the L1 sequences. l1Hubs: which of the elements will be used as hubs. l1SeqLength: a list of possible sequence lengths. The length of each sequence will be randomly chosen from here. nL1SimpleSequences: The number of simple sequences to generate for L1 nL1HubSequences: The number of hub sequences to generate for L1 l1Pooling: The number of time steps to pool over in the L1 temporal pooler perfectStability: If true, then the input patterns represented by the sequences generated will have perfect stability over l1Pooling time steps. This is the best case ideal input to a TM. In actual situations, with an actual SP providing input, the stability will always be less than this. spHystereisFactor: The hysteresisFactor to use in the L2 spatial pooler. Only used when perfectStability is False patternLen: The number of elements in each pattern output by L2 patternActivity: The number of elements that should be active in each pattern @retval: (seqList, patterns) seqList: a list of sequences output from L2. Each sequence is itself a list containing the input pattern indices for that sequence. patterns: the input patterns used in the L2 seqList. """ # First, generate the L1 sequences l1SeqList = generateSimpleSequences(nCoinc=nL1Patterns, seqLength=l1SeqLength, nSeq=nL1SimpleSequences) + \ generateHubSequences(nCoinc=nL1Patterns, hubs=l1Hubs, seqLength=l1SeqLength, nSeq=nL1HubSequences) # Generate the L2 SP output from those spOutput = generateSlowSPOutput(seqListBelow = l1SeqList, poolingTimeBelow=l1Pooling, outputWidth=patternLen, activity=patternActivity, perfectStability=perfectStability, spHysteresisFactor=spHysteresisFactor) # Map the spOutput patterns into indices into a pattern matrix which we # generate now. outSeq = None outSeqList = [] outPatterns = SM32(0, patternLen) for pattern in spOutput: # If we have a reset vector start a new sequence if pattern.sum() == 0: if outSeq is not None: outSeqList.append(outSeq) outSeq = [] continue # See if this vector matches a pattern we've already seen before patternIdx = None if outPatterns.nRows() > 0: # Find most matching 1's. matches = outPatterns.rightVecSumAtNZ(pattern) outCoinc = matches.argmax().astype('uint32') # See if its number of 1's is the same in the pattern and in the # coincidence row. If so, it is an exact match numOnes = pattern.sum() if matches[outCoinc] == numOnes \ and outPatterns.getRow(int(outCoinc)).sum() == numOnes: patternIdx = outCoinc # If no match, add this pattern to our matrix if patternIdx is None: outPatterns.addRow(pattern) patternIdx = outPatterns.nRows() - 1 # Store the pattern index into the sequence outSeq.append(patternIdx) # Put in last finished sequence if outSeq is not None: outSeqList.append(outSeq) # Return with the seqList and patterns matrix return (outSeqList, outPatterns)
def vectorsFromSeqList(seqList, patternMatrix): """ Convert a list of sequences of pattern indices, and a pattern lookup table into a an array of patterns Parameters: ----------------------------------------------- seq: the sequence, given as indices into the patternMatrix patternMatrix: a SparseMatrix contaning the possible patterns used in the sequence. """ totalLen = 0 for seq in seqList: totalLen += len(seq) vectors = numpy.zeros((totalLen, patternMatrix.shape[1]), dtype='bool') vecOffset = 0 for seq in seqList: seq = numpy.array(seq, dtype='uint32') for idx,coinc in enumerate(seq): vectors[vecOffset] = patternMatrix.getRow(int(coinc)) vecOffset += 1 return vectors
def sameTMParams(tp1, tp2): """Given two TM instances, see if any parameters are different.""" result = True for param in ["numberOfCols", "cellsPerColumn", "initialPerm", "connectedPerm", "minThreshold", "newSynapseCount", "permanenceInc", "permanenceDec", "permanenceMax", "globalDecay", "activationThreshold", "doPooling", "segUpdateValidDuration", "burnIn", "pamLength", "maxAge"]: if getattr(tp1, param) != getattr(tp2,param): print param,"is different" print getattr(tp1, param), "vs", getattr(tp2,param) result = False return result
def sameSynapse(syn, synapses): """Given a synapse and a list of synapses, check whether this synapse exist in the list. A synapse is represented as [col, cell, permanence]. A synapse matches if col and cell are identical and the permanence value is within 0.001.""" for s in synapses: if (s[0]==syn[0]) and (s[1]==syn[1]) and (abs(s[2]-syn[2]) <= 0.001): return True return False
def sameSegment(seg1, seg2): """Return True if seg1 and seg2 are identical, ignoring order of synapses""" result = True # check sequence segment, total activations etc. In case any are floats, # check that they are within 0.001. for field in [1, 2, 3, 4, 5, 6]: if abs(seg1[0][field] - seg2[0][field]) > 0.001: result = False # Compare number of synapses if len(seg1[1:]) != len(seg2[1:]): result = False # Now compare synapses, ignoring order of synapses for syn in seg2[1:]: if syn[2] <= 0: print "A synapse with zero permanence encountered" result = False if result == True: for syn in seg1[1:]: if syn[2] <= 0: print "A synapse with zero permanence encountered" result = False res = sameSynapse(syn, seg2[1:]) if res == False: result = False return result
def tmDiff(tm1, tm2, verbosity = 0, relaxSegmentTests =True): """ 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. """ # First check basic parameters. If we fail here, don't continue if sameTMParams(tm1, tm2) == False: print "Two TM's have different parameters" return False result = True # Compare states at t first, they usually diverge before the structure of the # cells starts diverging if (tm1.activeState['t'] != tm2.activeState['t']).any(): print 'Active states diverge', numpy.where(tm1.activeState['t'] != tm2.activeState['t']) result = False if (tm1.predictedState['t'] - tm2.predictedState['t']).any(): print 'Predicted states diverge', numpy.where(tm1.predictedState['t'] != tm2.predictedState['t']) result = False # TODO: check confidence at T (confT) # Now check some high level learned parameters. if tm1.getNumSegments() != tm2.getNumSegments(): print "Number of segments are different", tm1.getNumSegments(), tm2.getNumSegments() result = False if tm1.getNumSynapses() != tm2.getNumSynapses(): print "Number of synapses are different", tm1.getNumSynapses(), tm2.getNumSynapses() tm1.printCells() tm2.printCells() result = False # Check that each cell has the same number of segments and synapses for c in xrange(tm1.numberOfCols): for i in xrange(tm2.cellsPerColumn): if tm1.getNumSegmentsInCell(c, i) != tm2.getNumSegmentsInCell(c, i): print "Num segments different in cell:",c,i, print tm1.getNumSegmentsInCell(c, i), tm2.getNumSegmentsInCell(c, i) result = False # If the above tests pass, then check each segment and report differences # Note that segments in tm1 can be in a different order than tm2. Here we # make sure that, for each segment in tm1, there is an identical segment # in tm2. if result == True and not relaxSegmentTests: for c in xrange(tm1.numberOfCols): for i in xrange(tm2.cellsPerColumn): nSegs = tm1.getNumSegmentsInCell(c, i) for segIdx in xrange(nSegs): tm1seg = tm1.getSegmentOnCell(c, i, segIdx) # Loop through all segments in tm2seg and see if any of them match tm1seg res = False for tm2segIdx in xrange(nSegs): tm2seg = tm2.getSegmentOnCell(c, i, tm2segIdx) if sameSegment(tm1seg, tm2seg) == True: res = True break if res == False: print "\nSegments are different for cell:",c,i if verbosity >= 1: print "C++" tm1.printCell(c, i) print "Py" tm2.printCell(c, i) result = False if result == True and (verbosity > 1): print "TM's match" return result
def tmDiff2(tm1, tm2, verbosity = 0, relaxSegmentTests =True, checkLearn = True, checkStates = True): """ 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 """ # First check basic parameters. If we fail here, don't continue if sameTMParams(tm1, tm2) == False: print "Two TM's have different parameters" return False tm1Label = "<tm_1 (%s)>" % tm1.__class__.__name__ tm2Label = "<tm_2 (%s)>" % tm2.__class__.__name__ result = True if checkStates: # Compare states at t first, they usually diverge before the structure of the # cells starts diverging if (tm1.infActiveState['t'] != tm2.infActiveState['t']).any(): print 'Active states diverged', numpy.where(tm1.infActiveState['t'] != tm2.infActiveState['t']) result = False if (tm1.infPredictedState['t'] - tm2.infPredictedState['t']).any(): print 'Predicted states diverged', numpy.where(tm1.infPredictedState['t'] != tm2.infPredictedState['t']) result = False if checkLearn and (tm1.lrnActiveState['t'] - tm2.lrnActiveState['t']).any(): print 'lrnActiveState[t] diverged', numpy.where(tm1.lrnActiveState['t'] != tm2.lrnActiveState['t']) result = False if checkLearn and (tm1.lrnPredictedState['t'] - tm2.lrnPredictedState['t']).any(): print 'lrnPredictedState[t] diverged', numpy.where(tm1.lrnPredictedState['t'] != tm2.lrnPredictedState['t']) result = False if checkLearn and abs(tm1.getAvgLearnedSeqLength() - tm2.getAvgLearnedSeqLength()) > 0.01: print "Average learned sequence lengths differ: ", print tm1.getAvgLearnedSeqLength(), " vs ", tm2.getAvgLearnedSeqLength() result = False # TODO: check confidence at T (confT) # Now check some high level learned parameters. if tm1.getNumSegments() != tm2.getNumSegments(): print "Number of segments are different", tm1.getNumSegments(), tm2.getNumSegments() result = False if tm1.getNumSynapses() != tm2.getNumSynapses(): print "Number of synapses are different", tm1.getNumSynapses(), tm2.getNumSynapses() if verbosity >= 3: print "%s: " % tm1Label, tm1.printCells() print "\n%s : " % tm2Label, tm2.printCells() #result = False # Check that each cell has the same number of segments and synapses for c in xrange(tm1.numberOfCols): for i in xrange(tm2.cellsPerColumn): if tm1.getNumSegmentsInCell(c, i) != tm2.getNumSegmentsInCell(c, i): print "Num segments different in cell:",c,i, print tm1.getNumSegmentsInCell(c, i), tm2.getNumSegmentsInCell(c, i) result = False # If the above tests pass, then check each segment and report differences # Note that segments in tm1 can be in a different order than tm2. Here we # make sure that, for each segment in tm1, there is an identical segment # in tm2. if result == True and not relaxSegmentTests and checkLearn: for c in xrange(tm1.numberOfCols): for i in xrange(tm2.cellsPerColumn): nSegs = tm1.getNumSegmentsInCell(c, i) for segIdx in xrange(nSegs): tm1seg = tm1.getSegmentOnCell(c, i, segIdx) # Loop through all segments in tm2seg and see if any of them match tm1seg res = False for tm2segIdx in xrange(nSegs): tm2seg = tm2.getSegmentOnCell(c, i, tm2segIdx) if sameSegment(tm1seg, tm2seg) == True: res = True break if res == False: print "\nSegments are different for cell:",c,i result = False if verbosity >= 0: print "%s : " % tm1Label, tm1.printCell(c, i) print "\n%s : " % tm2Label, tm2.printCell(c, i) if result == True and (verbosity > 1): print "TM's match" return result
def spDiff(SP1,SP2): """ 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 """ if(len(SP1._masterConnectedM)!=len(SP2._masterConnectedM)): print "Connected synapse matrices are different sizes" return False if(len(SP1._masterPotentialM)!=len(SP2._masterPotentialM)): print "Potential synapse matrices are different sizes" return False if(len(SP1._masterPermanenceM)!=len(SP2._masterPermanenceM)): print "Permanence matrices are different sizes" return False #iterate over cells for i in range(0,len(SP1._masterConnectedM)): #grab the Coincidence Matrices and compare them connected1 = SP1._masterConnectedM[i] connected2 = SP2._masterConnectedM[i] if(connected1!=connected2): print "Connected Matrices for cell %d different" % (i) return False #grab permanence Matrices and compare them permanences1 = SP1._masterPermanenceM[i]; permanences2 = SP2._masterPermanenceM[i]; if(permanences1!=permanences2): print "Permanence Matrices for cell %d different" % (i) return False #grab the potential connection Matrices and compare them potential1 = SP1._masterPotentialM[i]; potential2 = SP2._masterPotentialM[i]; if(potential1!=potential2): print "Potential Matrices for cell %d different" % (i) return False #Check firing boosts if(not numpy.array_equal(SP1._firingBoostFactors,SP2._firingBoostFactors)): print "Firing boost factors are different between spatial poolers" return False #Check duty cycles after inhibiton if(not numpy.array_equal(SP1._dutyCycleAfterInh,SP2._dutyCycleAfterInh)): print "Duty cycles after inhibition are different between spatial poolers" return False #Check duty cycles before inhibition if(not numpy.array_equal(SP1._dutyCycleBeforeInh,SP2._dutyCycleBeforeInh)): print "Duty cycles before inhibition are different between spatial poolers" return False print("Spatial Poolers are equivalent") return True
def removeSeqStarts(vectors, resets, numSteps=1): """ 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. """ # Do nothing if numSteps is 0 if numSteps == 0: return vectors resetIndices = resets.nonzero()[0] removeRows = resetIndices for i in range(numSteps-1): removeRows = numpy.hstack((removeRows, resetIndices+i+1)) return numpy.delete(vectors, removeRows, axis=0)
def _accumulateFrequencyCounts(values, freqCounts=None): """ 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 """ # How big does our freqCounts vector need to be? values = numpy.array(values) numEntries = values.max() + 1 if freqCounts is not None: numEntries = max(numEntries, freqCounts.size) # Where do we accumulate the results? if freqCounts is not None: if freqCounts.size != numEntries: newCounts = numpy.zeros(numEntries, dtype='int32') newCounts[0:freqCounts.size] = freqCounts else: newCounts = freqCounts else: newCounts = numpy.zeros(numEntries, dtype='int32') # Accumulate the new values for v in values: newCounts[v] += 1 return newCounts
def _listOfOnTimesInVec(vector): """ 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]) """ # init counters durations = [] numOnTimes = 0 totalOnTime = 0 # Find where the nonzeros are nonzeros = numpy.array(vector).nonzero()[0] # Nothing to do if vector is empty if len(nonzeros) == 0: return (0, 0, []) # Special case of only 1 on bit if len(nonzeros) == 1: return (1, 1, [1]) # Count the consecutive non-zeros prev = nonzeros[0] onTime = 1 endIdx = nonzeros[-1] for idx in nonzeros[1:]: if idx != prev+1: totalOnTime += onTime numOnTimes += 1 durations.append(onTime) onTime = 1 else: onTime += 1 prev = idx # Add in the last one totalOnTime += onTime numOnTimes += 1 durations.append(onTime) return (totalOnTime, numOnTimes, durations)
def _fillInOnTimes(vector, durations): """ 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 """ # Find where the nonzeros are nonzeros = numpy.array(vector).nonzero()[0] # Nothing to do if vector is empty if len(nonzeros) == 0: return # Special case of only 1 on bit if len(nonzeros) == 1: durations[nonzeros[0]] = 1 return # Count the consecutive non-zeros prev = nonzeros[0] onTime = 1 onStartIdx = prev endIdx = nonzeros[-1] for idx in nonzeros[1:]: if idx != prev+1: # Fill in the durations durations[onStartIdx:onStartIdx+onTime] = range(1,onTime+1) onTime = 1 onStartIdx = idx else: onTime += 1 prev = idx # Fill in the last one durations[onStartIdx:onStartIdx+onTime] = range(1,onTime+1)
def averageOnTimePerTimestep(vectors, numSamples=None): """ 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) """ # Special case given a 1 dimensional vector: it represents a single column if vectors.ndim == 1: vectors.shape = (-1,1) numTimeSteps = len(vectors) numElements = len(vectors[0]) # How many samples will we look at? if numSamples is not None: import pdb; pdb.set_trace() # Test this.... countOn = numpy.random.randint(0, numElements, numSamples) vectors = vectors[:, countOn] # Fill in each non-zero of vectors with the on-time that that output was # on for. durations = numpy.zeros(vectors.shape, dtype='int32') for col in xrange(vectors.shape[1]): _fillInOnTimes(vectors[:,col], durations[:,col]) # Compute the average on time for each time step sums = vectors.sum(axis=1) sums.clip(min=1, max=numpy.inf, out=sums) avgDurations = durations.sum(axis=1, dtype='float64') / sums avgOnTime = avgDurations.sum() / (avgDurations > 0).sum() # Generate the frequency counts for each duration freqCounts = _accumulateFrequencyCounts(avgDurations) return (avgOnTime, freqCounts)
def averageOnTime(vectors, numSamples=None): """ 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) """ # Special case given a 1 dimensional vector: it represents a single column if vectors.ndim == 1: vectors.shape = (-1,1) numTimeSteps = len(vectors) numElements = len(vectors[0]) # How many samples will we look at? if numSamples is None: numSamples = numElements countOn = range(numElements) else: countOn = numpy.random.randint(0, numElements, numSamples) # Compute the on-times and accumulate the frequency counts of each on-time # encountered sumOfLengths = 0.0 onTimeFreqCounts = None n = 0 for i in countOn: (onTime, segments, durations) = _listOfOnTimesInVec(vectors[:,i]) if onTime != 0.0: sumOfLengths += onTime n += segments onTimeFreqCounts = _accumulateFrequencyCounts(durations, onTimeFreqCounts) # Return the average on time of each element that was on. if n > 0: return (sumOfLengths/n, onTimeFreqCounts) else: return (0.0, onTimeFreqCounts)
def plotOutputsOverTime(vectors, buVectors=None, title='On-times'): """ 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. """ # Produce the plot import pylab pylab.ion() pylab.figure() imData = vectors.transpose() if buVectors is not None: assert(buVectors.shape == vectors.shape) imData = imData.copy() imData[buVectors.transpose().astype('bool')] = 2 pylab.imshow(imData, aspect='auto', cmap=pylab.cm.gray_r, interpolation='nearest') pylab.title(title)
def plotHistogram(freqCounts, title='On-Times Histogram', xLabel='On-Time'): """ 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 """ import pylab pylab.ion() pylab.figure() pylab.bar(numpy.arange(len(freqCounts)) - 0.5, freqCounts) pylab.title(title) pylab.xlabel(xLabel)
def populationStability(vectors, numSamples=None): """ 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 """ # ---------------------------------------------------------------------- # Calculate the stability numVectors = len(vectors) if numSamples is None: numSamples = numVectors-1 countOn = range(numVectors-1) else: countOn = numpy.random.randint(0, numVectors-1, numSamples) sigmap = 0.0 for i in countOn: match = checkMatch(vectors[i], vectors[i+1], sparse=False) # Ignore reset vectors (all 0's) if match[1] != 0: sigmap += float(match[0])/match[1] return sigmap / numSamples
def percentOutputsStableOverNTimeSteps(vectors, numSamples=None): """ 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. """ # ---------------------------------------------------------------------- # Calculate the stability totalSamples = len(vectors) windowSize = numSamples # Process each window numWindows = 0 pctStable = 0 for wStart in range(0, totalSamples-windowSize+1): # Count how many elements are active for the entire time data = vectors[wStart:wStart+windowSize] outputSums = data.sum(axis=0) stableOutputs = (outputSums == windowSize).sum() # Accumulated samplePctStable = float(stableOutputs) / data[0].sum() print samplePctStable pctStable += samplePctStable numWindows += 1 # Return percent average over all possible windows return float(pctStable) / numWindows