Search is not available for this dataset
text
stringlengths
75
104k
def setCustomProperties(cls, properties): """ Set multiple custom properties and persist them to the custom configuration store. Parameters: ---------------------------------------------------------------- properties: a dict of property name/value pairs to set """ _getLogger().info("Setting custom configuration properties=%r; caller=%r", properties, traceback.format_stack()) _CustomConfigurationFileWrapper.edit(properties) for propertyName, value in properties.iteritems(): cls.set(propertyName, value)
def clear(cls): """ Clear all configuration properties from in-memory cache, but do NOT alter the custom configuration file. Used in unit-testing. """ # Clear the in-memory settings cache, forcing reload upon subsequent "get" # request. super(Configuration, cls).clear() # Reset in-memory custom configuration info. _CustomConfigurationFileWrapper.clear(persistent=False)
def resetCustomConfig(cls): """ Clear all custom configuration settings and delete the persistent custom configuration store. """ _getLogger().info("Resetting all custom configuration properties; " "caller=%r", traceback.format_stack()) # Clear the in-memory settings cache, forcing reload upon subsequent "get" # request. super(Configuration, cls).clear() # Delete the persistent custom configuration store and reset in-memory # custom configuration info _CustomConfigurationFileWrapper.clear(persistent=True)
def clear(cls, persistent=False): """ If persistent is True, delete the temporary file Parameters: ---------------------------------------------------------------- persistent: if True, custom configuration file is deleted """ if persistent: try: os.unlink(cls.getPath()) except OSError, e: if e.errno != errno.ENOENT: _getLogger().exception("Error %s while trying to remove dynamic " \ "configuration file: %s", e.errno, cls.getPath()) raise cls._path = None
def getCustomDict(cls): """ Returns a dict of all temporary values in custom configuration file """ if not os.path.exists(cls.getPath()): return dict() properties = Configuration._readConfigFile(os.path.basename( cls.getPath()), os.path.dirname(cls.getPath())) values = dict() for propName in properties: if 'value' in properties[propName]: values[propName] = properties[propName]['value'] return values
def edit(cls, properties): """ Edits the XML configuration file with the parameters specified by properties Parameters: ---------------------------------------------------------------- properties: dict of settings to be applied to the custom configuration store (key is property name, value is value) """ copyOfProperties = copy(properties) configFilePath = cls.getPath() try: with open(configFilePath, 'r') as fp: contents = fp.read() except IOError, e: if e.errno != errno.ENOENT: _getLogger().exception("Error %s reading custom configuration store " "from %s, while editing properties %s.", e.errno, configFilePath, properties) raise contents = '<configuration/>' try: elements = ElementTree.XML(contents) ElementTree.tostring(elements) except Exception, e: # Raising error as RuntimeError with custom message since ElementTree # exceptions aren't clear. msg = "File contents of custom configuration is corrupt. File " \ "location: %s; Contents: '%s'. Original Error (%s): %s." % \ (configFilePath, contents, type(e), e) _getLogger().exception(msg) raise RuntimeError(msg), None, sys.exc_info()[2] if elements.tag != 'configuration': e = "Expected top-level element to be 'configuration' but got '%s'" % \ (elements.tag) _getLogger().error(e) raise RuntimeError(e) # Apply new properties to matching settings in the custom config store; # pop matching properties from our copy of the properties dict for propertyItem in elements.findall('./property'): propInfo = dict((attr.tag, attr.text) for attr in propertyItem) name = propInfo['name'] if name in copyOfProperties: foundValues = propertyItem.findall('./value') if len(foundValues) > 0: foundValues[0].text = str(copyOfProperties.pop(name)) if not copyOfProperties: break else: e = "Property %s missing value tag." % (name,) _getLogger().error(e) raise RuntimeError(e) # Add unmatched remaining properties to custom config store for propertyName, value in copyOfProperties.iteritems(): newProp = ElementTree.Element('property') nameTag = ElementTree.Element('name') nameTag.text = propertyName newProp.append(nameTag) valueTag = ElementTree.Element('value') valueTag.text = str(value) newProp.append(valueTag) elements.append(newProp) try: makeDirectoryFromAbsolutePath(os.path.dirname(configFilePath)) with open(configFilePath, 'w') as fp: fp.write(ElementTree.tostring(elements)) except Exception, e: _getLogger().exception("Error while saving custom configuration " "properties %s in %s.", properties, configFilePath) raise
def _setPath(cls): """ Sets the path of the custom configuration file """ cls._path = os.path.join(os.environ['NTA_DYNAMIC_CONF_DIR'], cls.customFileName)
def getState(self): """Get the particle state as a dict. This is enough information to instantiate this particle on another worker.""" varStates = dict() for varName, var in self.permuteVars.iteritems(): varStates[varName] = var.getState() return dict(id=self.particleId, genIdx=self.genIdx, swarmId=self.swarmId, varStates=varStates)
def initStateFrom(self, particleId, particleState, newBest): """Init all of our variable positions, velocities, and optionally the best result and best position from the given particle. If newBest is true, we get the best result and position for this new generation from the resultsDB, This is used when evoloving a particle because the bestResult and position as stored in was the best AT THE TIME THAT PARTICLE STARTED TO RUN and does not include the best since that particle completed. """ # Get the update best position and result? if newBest: (bestResult, bestPosition) = self._resultsDB.getParticleBest(particleId) else: bestResult = bestPosition = None # Replace with the position and velocity of each variable from # saved state varStates = particleState['varStates'] for varName in varStates.keys(): varState = copy.deepcopy(varStates[varName]) if newBest: varState['bestResult'] = bestResult if bestPosition is not None: varState['bestPosition'] = bestPosition[varName] self.permuteVars[varName].setState(varState)
def copyVarStatesFrom(self, particleState, varNames): """Copy specific variables from particleState into this particle. Parameters: -------------------------------------------------------------- particleState: dict produced by a particle's getState() method varNames: which variables to copy """ # Set this to false if you don't want the variable to move anymore # after we set the state allowedToMove = True for varName in particleState['varStates']: if varName in varNames: # If this particle doesn't include this field, don't copy it if varName not in self.permuteVars: continue # Set the best position to the copied position state = copy.deepcopy(particleState['varStates'][varName]) state['_position'] = state['position'] state['bestPosition'] = state['position'] if not allowedToMove: state['velocity'] = 0 # Set the state now self.permuteVars[varName].setState(state) if allowedToMove: # Let the particle move in both directions from the best position # it found previously and set it's initial velocity to a known # fraction of the total distance. self.permuteVars[varName].resetVelocity(self._rng)
def getPosition(self): """Return the position of this particle. This returns a dict() of key value pairs where each key is the name of the flattened permutation variable and the value is its chosen value. Parameters: -------------------------------------------------------------- retval: dict() of flattened permutation choices """ result = dict() for (varName, value) in self.permuteVars.iteritems(): result[varName] = value.getPosition() return result
def getPositionFromState(pState): """Return the position of a particle given its state dict. Parameters: -------------------------------------------------------------- retval: dict() of particle position, keys are the variable names, values are their positions """ result = dict() for (varName, value) in pState['varStates'].iteritems(): result[varName] = value['position'] return result
def agitate(self): """Agitate this particle so that it is likely to go to a new position. Every time agitate is called, the particle is jiggled an even greater amount. Parameters: -------------------------------------------------------------- retval: None """ for (varName, var) in self.permuteVars.iteritems(): var.agitate() self.newPosition()
def newPosition(self, whichVars=None): # TODO: incorporate data from choice variables.... # TODO: make sure we're calling this when appropriate. """Choose a new position based on results obtained so far from all other particles. Parameters: -------------------------------------------------------------- whichVars: If not None, only move these variables retval: new position """ # Get the global best position for this swarm generation globalBestPosition = None # If speculative particles are enabled, use the global best considering # even particles in the current generation. This gives better results # but does not provide repeatable results because it depends on # worker timing if self._hsObj._speculativeParticles: genIdx = self.genIdx else: genIdx = self.genIdx - 1 if genIdx >= 0: (bestModelId, _) = self._resultsDB.bestModelIdAndErrScore(self.swarmId, genIdx) if bestModelId is not None: (particleState, _, _, _, _) = self._resultsDB.getParticleInfo( bestModelId) globalBestPosition = Particle.getPositionFromState(particleState) # Update each variable for (varName, var) in self.permuteVars.iteritems(): if whichVars is not None and varName not in whichVars: continue if globalBestPosition is None: var.newPosition(None, self._rng) else: var.newPosition(globalBestPosition[varName], self._rng) # get the new position position = self.getPosition() # Log the new position if self.logger.getEffectiveLevel() <= logging.DEBUG: msg = StringIO.StringIO() print >> msg, "New particle position: \n%s" % (pprint.pformat(position, indent=4)) print >> msg, "Particle variables:" for (varName, var) in self.permuteVars.iteritems(): print >> msg, " %s: %s" % (varName, str(var)) self.logger.debug(msg.getvalue()) msg.close() return position
def propose(self, current, r): """Generates a random sample from the discrete probability distribution and returns its value, the log of the probability of sampling that value and the log of the probability of sampling the current value (passed in). """ stay = (r.uniform(0, 1) < self.kernel) if stay: logKernel = numpy.log(self.kernel) return current, logKernel, logKernel else: # Choose uniformly, not according to the pmf. curIndex = self.keyMap[current] ri = r.randint(0, self.nKeys-1) logKernel = numpy.log(1.0 - self.kernel) lp = logKernel + self.logp if ri < curIndex: return self.keys[ri], lp, lp else: return self.keys[ri+1], lp, lp
def propose(self, current, r): """Generates a random sample from the Poisson probability distribution with with location and scale parameter equal to the current value (passed in). Returns the value of the random sample, the log of the probability of sampling that value, and the log of the probability of sampling the current value if the roles of the new sample and the current sample were reversed (the log of the backward proposal probability). """ curLambda = current + self.offset x, logProb = PoissonDistribution(curLambda).sample(r) logBackward = PoissonDistribution(x+self.offset).logDensity(current) return x, logProb, logBackward
def __getLogger(cls): """ Get the logger for this object. :returns: (Logger) A Logger object. """ if cls.__logger is None: cls.__logger = opf_utils.initLogger(cls) return cls.__logger
def create(modelConfig, logLevel=logging.ERROR): """ Create a new model instance, given a description dictionary. :param modelConfig: (dict) A dictionary describing the current model, `described here <../../quick-start/example-model-params.html>`_. :param logLevel: (int) The level of logging output that should be generated :raises Exception: Unsupported model type :returns: :class:`nupic.frameworks.opf.model.Model` """ logger = ModelFactory.__getLogger() logger.setLevel(logLevel) logger.debug("ModelFactory returning Model from dict: %s", modelConfig) modelClass = None if modelConfig['model'] == "HTMPrediction": modelClass = HTMPredictionModel elif modelConfig['model'] == "TwoGram": modelClass = TwoGramModel elif modelConfig['model'] == "PreviousValue": modelClass = PreviousValueModel else: raise Exception("ModelFactory received unsupported Model type: %s" % \ modelConfig['model']) return modelClass(**modelConfig['modelParams'])
def loadFromCheckpoint(savedModelDir, newSerialization=False): """ Load saved model. :param savedModelDir: (string) Directory of where the experiment is to be or was saved :returns: (:class:`nupic.frameworks.opf.model.Model`) The loaded model instance. """ if newSerialization: return HTMPredictionModel.readFromCheckpoint(savedModelDir) else: return Model.load(savedModelDir)
def compute(self, activeColumns, learn=True): """ Perform one time step of the Temporal Memory algorithm. This method calls :meth:`activateCells`, then calls :meth:`activateDendrites`. Using :class:`TemporalMemory` via its :meth:`compute` method ensures that you'll always be able to call :meth:`getPredictiveCells` to get predictions for the next time step. :param activeColumns: (iter) Indices of active columns. :param learn: (bool) Whether or not learning is enabled. """ self.activateCells(sorted(activeColumns), learn) self.activateDendrites(learn)
def activateCells(self, activeColumns, learn=True): """ Calculate the active cells, using the current active columns and dendrite segments. Grow and reinforce synapses. :param activeColumns: (iter) A sorted list of active column indices. :param learn: (bool) If true, reinforce / punish / grow synapses. **Pseudocode:** :: for each column if column is active and has active distal dendrite segments call activatePredictedColumn if column is active and doesn't have active distal dendrite segments call burstColumn if column is inactive and has matching distal dendrite segments call punishPredictedColumn """ prevActiveCells = self.activeCells prevWinnerCells = self.winnerCells self.activeCells = [] self.winnerCells = [] segToCol = lambda segment: int(segment.cell / self.cellsPerColumn) identity = lambda x: x for columnData in groupby2(activeColumns, identity, self.activeSegments, segToCol, self.matchingSegments, segToCol): (column, activeColumns, columnActiveSegments, columnMatchingSegments) = columnData if activeColumns is not None: if columnActiveSegments is not None: cellsToAdd = self.activatePredictedColumn(column, columnActiveSegments, columnMatchingSegments, prevActiveCells, prevWinnerCells, learn) self.activeCells += cellsToAdd self.winnerCells += cellsToAdd else: (cellsToAdd, winnerCell) = self.burstColumn(column, columnMatchingSegments, prevActiveCells, prevWinnerCells, learn) self.activeCells += cellsToAdd self.winnerCells.append(winnerCell) else: if learn: self.punishPredictedColumn(column, columnActiveSegments, columnMatchingSegments, prevActiveCells, prevWinnerCells)
def activateDendrites(self, learn=True): """ Calculate dendrite segment activity, using the current active cells. :param learn: (bool) If true, segment activations will be recorded. This information is used during segment cleanup. **Pseudocode:** :: for each distal dendrite segment with activity >= activationThreshold mark the segment as active for each distal dendrite segment with unconnected activity >= minThreshold mark the segment as matching """ (numActiveConnected, numActivePotential) = self.connections.computeActivity( self.activeCells, self.connectedPermanence) activeSegments = ( self.connections.segmentForFlatIdx(i) for i in xrange(len(numActiveConnected)) if numActiveConnected[i] >= self.activationThreshold ) matchingSegments = ( self.connections.segmentForFlatIdx(i) for i in xrange(len(numActivePotential)) if numActivePotential[i] >= self.minThreshold ) self.activeSegments = sorted(activeSegments, key=self.connections.segmentPositionSortKey) self.matchingSegments = sorted(matchingSegments, key=self.connections.segmentPositionSortKey) self.numActiveConnectedSynapsesForSegment = numActiveConnected self.numActivePotentialSynapsesForSegment = numActivePotential if learn: for segment in self.activeSegments: self.lastUsedIterationForSegment[segment.flatIdx] = self.iteration self.iteration += 1
def reset(self): """ Indicates the start of a new sequence. Clears any predictions and makes sure synapses don't grow to the currently active cells in the next time step. """ self.activeCells = [] self.winnerCells = [] self.activeSegments = [] self.matchingSegments = []
def activatePredictedColumn(self, column, columnActiveSegments, columnMatchingSegments, prevActiveCells, prevWinnerCells, learn): """ Determines which cells in a predicted column should be added to winner cells list, and learns on the segments that correctly predicted this column. :param column: (int) Index of bursting column. :param columnActiveSegments: (iter) Active segments in this column. :param columnMatchingSegments: (iter) Matching segments in this column. :param prevActiveCells: (list) Active cells in ``t-1``. :param prevWinnerCells: (list) Winner cells in ``t-1``. :param learn: (bool) If true, grow and reinforce synapses. :returns: (list) A list of predicted cells that will be added to active cells and winner cells. """ return self._activatePredictedColumn( self.connections, self._random, columnActiveSegments, prevActiveCells, prevWinnerCells, self.numActivePotentialSynapsesForSegment, self.maxNewSynapseCount, self.initialPermanence, self.permanenceIncrement, self.permanenceDecrement, self.maxSynapsesPerSegment, learn)
def punishPredictedColumn(self, column, columnActiveSegments, columnMatchingSegments, prevActiveCells, prevWinnerCells): """ Punishes the Segments that incorrectly predicted a column to be active. :param column: (int) Index of bursting column. :param columnActiveSegments: (iter) Active segments for this column, or None if there aren't any. :param columnMatchingSegments: (iter) Matching segments for this column, or None if there aren't any. :param prevActiveCells: (list) Active cells in ``t-1``. :param prevWinnerCells: (list) Winner cells in ``t-1``. """ self._punishPredictedColumn( self.connections, columnMatchingSegments, prevActiveCells, self.predictedSegmentDecrement)
def createSegment(self, cell): """ Create a :class:`~nupic.algorithms.connections.Segment` on the specified cell. This method calls :meth:`~nupic.algorithms.connections.Connections.createSegment` on the underlying :class:`~nupic.algorithms.connections.Connections`, and it does some extra bookkeeping. Unit tests should call this method, and not :meth:`~nupic.algorithms.connections.Connections.createSegment`. :param cell: (int) Index of cell to create a segment on. :returns: (:class:`~nupic.algorithms.connections.Segment`) The created segment. """ return self._createSegment( self.connections, self.lastUsedIterationForSegment, cell, self.iteration, self.maxSegmentsPerCell)
def _activatePredictedColumn(cls, connections, random, columnActiveSegments, prevActiveCells, prevWinnerCells, numActivePotentialSynapsesForSegment, maxNewSynapseCount, initialPermanence, permanenceIncrement, permanenceDecrement, maxSynapsesPerSegment, learn): """ :param connections: (Object) Connections for the TM. Gets mutated. :param random: (Object) Random number generator. Gets mutated. :param columnActiveSegments: (iter) Active segments in this column. :param prevActiveCells: (list) Active cells in `t-1`. :param prevWinnerCells: (list) Winner cells in `t-1`. :param numActivePotentialSynapsesForSegment: (list) Number of active potential synapses per segment, indexed by the segment's flatIdx. :param maxNewSynapseCount: (int) The maximum number of synapses added to a segment during learning :param initialPermanence: (float) Initial permanence of a new synapse. @permanenceIncrement (float) Amount by which permanences of synapses are incremented during learning. @permanenceDecrement (float) Amount by which permanences of synapses are decremented during learning. :param maxSynapsesPerSegment: (int) The maximum number of synapses per segment. :param learn: (bool) If true, grow and reinforce synapses. :returns: cellsToAdd (list) A list of predicted cells that will be added to active cells and winner cells. Pseudocode: for each cell in the column that has an active distal dendrite segment mark the cell as active mark the cell as a winner cell (learning) for each active distal dendrite segment strengthen active synapses weaken inactive synapses grow synapses to previous winner cells """ cellsToAdd = [] previousCell = None for segment in columnActiveSegments: if segment.cell != previousCell: cellsToAdd.append(segment.cell) previousCell = segment.cell if learn: cls._adaptSegment(connections, segment, prevActiveCells, permanenceIncrement, permanenceDecrement) active = numActivePotentialSynapsesForSegment[segment.flatIdx] nGrowDesired = maxNewSynapseCount - active if nGrowDesired > 0: cls._growSynapses(connections, random, segment, nGrowDesired, prevWinnerCells, initialPermanence, maxSynapsesPerSegment) return cellsToAdd
def _burstColumn(cls, connections, random, lastUsedIterationForSegment, column, columnMatchingSegments, prevActiveCells, prevWinnerCells, cellsForColumn, numActivePotentialSynapsesForSegment, iteration, maxNewSynapseCount, initialPermanence, permanenceIncrement, permanenceDecrement, maxSegmentsPerCell, maxSynapsesPerSegment, learn): """ :param connections: (Object) Connections for the TM. Gets mutated. :param random: (Object) Random number generator. Gets mutated. :param lastUsedIterationForSegment: (list) Last used iteration for each segment, indexed by the segment's flatIdx. Gets mutated. :param column: (int) Index of bursting column. :param columnMatchingSegments: (iter) Matching segments in this column. :param prevActiveCells: (list) Active cells in `t-1`. :param prevWinnerCells: (list) Winner cells in `t-1`. :param cellsForColumn: (sequence) Range of cell indices on which to operate. :param numActivePotentialSynapsesForSegment: (list) Number of active potential synapses per segment, indexed by the segment's flatIdx. :param iteration: (int) The current timestep. :param maxNewSynapseCount: (int) The maximum number of synapses added to a segment during learning. :param initialPermanence: (float) Initial permanence of a new synapse. :param permanenceIncrement: (float) Amount by which permanences of synapses are incremented during learning. :param permanenceDecrement: (float) Amount by which permanences of synapses are decremented during learning. :param maxSegmentsPerCell: (int) The maximum number of segments per cell. :param maxSynapsesPerSegment: (int) The maximum number of synapses per segment. :param learn: (bool) Whether or not learning is enabled. :returns: (tuple) Contains: `cells` (iter), `winnerCell` (int), Pseudocode: mark all cells as active if there are any matching distal dendrite segments find the most active matching segment mark its cell as a winner cell (learning) grow and reinforce synapses to previous winner cells else find the cell with the least segments, mark it as a winner cell (learning) (optimization) if there are prev winner cells add a segment to this winner cell grow synapses to previous winner cells """ if columnMatchingSegments is not None: numActive = lambda s: numActivePotentialSynapsesForSegment[s.flatIdx] bestMatchingSegment = max(columnMatchingSegments, key=numActive) winnerCell = bestMatchingSegment.cell if learn: cls._adaptSegment(connections, bestMatchingSegment, prevActiveCells, permanenceIncrement, permanenceDecrement) nGrowDesired = maxNewSynapseCount - numActive(bestMatchingSegment) if nGrowDesired > 0: cls._growSynapses(connections, random, bestMatchingSegment, nGrowDesired, prevWinnerCells, initialPermanence, maxSynapsesPerSegment) else: winnerCell = cls._leastUsedCell(random, cellsForColumn, connections) if learn: nGrowExact = min(maxNewSynapseCount, len(prevWinnerCells)) if nGrowExact > 0: segment = cls._createSegment(connections, lastUsedIterationForSegment, winnerCell, iteration, maxSegmentsPerCell) cls._growSynapses(connections, random, segment, nGrowExact, prevWinnerCells, initialPermanence, maxSynapsesPerSegment) return cellsForColumn, winnerCell
def _punishPredictedColumn(cls, connections, columnMatchingSegments, prevActiveCells, predictedSegmentDecrement): """ :param connections: (Object) Connections for the TM. Gets mutated. :param columnMatchingSegments: (iter) Matching segments for this column. :param prevActiveCells: (list) Active cells in `t-1`. :param predictedSegmentDecrement: (float) Amount by which segments are punished for incorrect predictions. Pseudocode: for each matching segment in the column weaken active synapses """ if predictedSegmentDecrement > 0.0 and columnMatchingSegments is not None: for segment in columnMatchingSegments: cls._adaptSegment(connections, segment, prevActiveCells, -predictedSegmentDecrement, 0.0)
def _createSegment(cls, connections, lastUsedIterationForSegment, cell, iteration, maxSegmentsPerCell): """ Create a segment on the connections, enforcing the maxSegmentsPerCell parameter. """ # Enforce maxSegmentsPerCell. while connections.numSegments(cell) >= maxSegmentsPerCell: leastRecentlyUsedSegment = min( connections.segmentsForCell(cell), key=lambda segment : lastUsedIterationForSegment[segment.flatIdx]) connections.destroySegment(leastRecentlyUsedSegment) # Create the segment. segment = connections.createSegment(cell) # Do TM-specific bookkeeping for the segment. if segment.flatIdx == len(lastUsedIterationForSegment): lastUsedIterationForSegment.append(iteration) elif segment.flatIdx < len(lastUsedIterationForSegment): # A flatIdx was recycled. lastUsedIterationForSegment[segment.flatIdx] = iteration else: raise AssertionError( "All segments should be created with the TM createSegment method.") return segment
def _destroyMinPermanenceSynapses(cls, connections, random, segment, nDestroy, excludeCells): """ Destroy nDestroy synapses on the specified segment, but don't destroy synapses to the "excludeCells". """ destroyCandidates = sorted( (synapse for synapse in connections.synapsesForSegment(segment) if synapse.presynapticCell not in excludeCells), key=lambda s: s._ordinal ) for _ in xrange(nDestroy): if len(destroyCandidates) == 0: break minSynapse = None minPermanence = float("inf") for synapse in destroyCandidates: if synapse.permanence < minPermanence - EPSILON: minSynapse = synapse minPermanence = synapse.permanence connections.destroySynapse(minSynapse) destroyCandidates.remove(minSynapse)
def _leastUsedCell(cls, random, cells, connections): """ Gets the cell with the smallest number of segments. Break ties randomly. :param random: (Object) Random number generator. Gets mutated. :param cells: (list) Indices of cells. :param connections: (Object) Connections instance for the TM. :returns: (int) Cell index. """ leastUsedCells = [] minNumSegments = float("inf") for cell in cells: numSegments = connections.numSegments(cell) if numSegments < minNumSegments: minNumSegments = numSegments leastUsedCells = [] if numSegments == minNumSegments: leastUsedCells.append(cell) i = random.getUInt32(len(leastUsedCells)) return leastUsedCells[i]
def _growSynapses(cls, connections, random, segment, nDesiredNewSynapes, prevWinnerCells, initialPermanence, maxSynapsesPerSegment): """ Creates nDesiredNewSynapes synapses on the segment passed in if possible, choosing random cells from the previous winner cells that are not already on the segment. :param connections: (Object) Connections instance for the tm :param random: (Object) TM object used to generate random numbers :param segment: (int) Segment to grow synapses on. :param nDesiredNewSynapes: (int) Desired number of synapses to grow :param prevWinnerCells: (list) Winner cells in `t-1` :param initialPermanence: (float) Initial permanence of a new synapse. """ candidates = list(prevWinnerCells) for synapse in connections.synapsesForSegment(segment): i = binSearch(candidates, synapse.presynapticCell) if i != -1: del candidates[i] nActual = min(nDesiredNewSynapes, len(candidates)) # Check if we're going to surpass the maximum number of synapses. overrun = connections.numSynapses(segment) + nActual - maxSynapsesPerSegment if overrun > 0: cls._destroyMinPermanenceSynapses(connections, random, segment, overrun, prevWinnerCells) # Recalculate in case we weren't able to destroy as many synapses as needed. nActual = min(nActual, maxSynapsesPerSegment - connections.numSynapses(segment)) for _ in range(nActual): i = random.getUInt32(len(candidates)) connections.createSynapse(segment, candidates[i], initialPermanence) del candidates[i]
def _adaptSegment(cls, connections, segment, prevActiveCells, permanenceIncrement, permanenceDecrement): """ Updates synapses on segment. Strengthens active synapses; weakens inactive synapses. :param connections: (Object) Connections instance for the tm :param segment: (int) Segment to adapt :param prevActiveCells: (list) Active cells in `t-1` :param permanenceIncrement: (float) Amount to increment active synapses :param permanenceDecrement: (float) Amount to decrement inactive synapses """ # Destroying a synapse modifies the set that we're iterating through. synapsesToDestroy = [] for synapse in connections.synapsesForSegment(segment): permanence = synapse.permanence if binSearch(prevActiveCells, synapse.presynapticCell) != -1: permanence += permanenceIncrement else: permanence -= permanenceDecrement # Keep permanence within min/max bounds permanence = max(0.0, min(1.0, permanence)) if permanence < EPSILON: synapsesToDestroy.append(synapse) else: connections.updateSynapsePermanence(synapse, permanence) for synapse in synapsesToDestroy: connections.destroySynapse(synapse) if connections.numSynapses(segment) == 0: connections.destroySegment(segment)
def columnForCell(self, cell): """ Returns the index of the column that a cell belongs to. :param cell: (int) Cell index :returns: (int) Column index """ self._validateCell(cell) return int(cell / self.cellsPerColumn)
def cellsForColumn(self, column): """ Returns the indices of cells that belong to a column. :param column: (int) Column index :returns: (list) Cell indices """ self._validateColumn(column) start = self.cellsPerColumn * column end = start + self.cellsPerColumn return range(start, end)
def mapCellsToColumns(self, cells): """ Maps cells to the columns they belong to. :param cells: (set) Cells :returns: (dict) Mapping from columns to their cells in `cells` """ cellsForColumns = defaultdict(set) for cell in cells: column = self.columnForCell(cell) cellsForColumns[column].add(cell) return cellsForColumns
def getPredictiveCells(self): """ Returns the indices of the predictive cells. :returns: (list) Indices of predictive cells. """ previousCell = None predictiveCells = [] for segment in self.activeSegments: if segment.cell != previousCell: predictiveCells.append(segment.cell) previousCell = segment.cell return predictiveCells
def write(self, proto): """ Writes serialized data to proto object. :param proto: (DynamicStructBuilder) Proto object """ # capnp fails to save a tuple. Let's force columnDimensions to list. proto.columnDimensions = list(self.columnDimensions) proto.cellsPerColumn = self.cellsPerColumn proto.activationThreshold = self.activationThreshold proto.initialPermanence = round(self.initialPermanence, EPSILON_ROUND) proto.connectedPermanence = round(self.connectedPermanence, EPSILON_ROUND) proto.minThreshold = self.minThreshold proto.maxNewSynapseCount = self.maxNewSynapseCount proto.permanenceIncrement = round(self.permanenceIncrement, EPSILON_ROUND) proto.permanenceDecrement = round(self.permanenceDecrement, EPSILON_ROUND) proto.predictedSegmentDecrement = self.predictedSegmentDecrement proto.maxSegmentsPerCell = self.maxSegmentsPerCell proto.maxSynapsesPerSegment = self.maxSynapsesPerSegment self.connections.write(proto.connections) self._random.write(proto.random) proto.activeCells = list(self.activeCells) proto.winnerCells = list(self.winnerCells) protoActiveSegments = proto.init("activeSegments", len(self.activeSegments)) for i, segment in enumerate(self.activeSegments): protoActiveSegments[i].cell = segment.cell idx = self.connections.segmentsForCell(segment.cell).index(segment) protoActiveSegments[i].idxOnCell = idx protoMatchingSegments = proto.init("matchingSegments", len(self.matchingSegments)) for i, segment in enumerate(self.matchingSegments): protoMatchingSegments[i].cell = segment.cell idx = self.connections.segmentsForCell(segment.cell).index(segment) protoMatchingSegments[i].idxOnCell = idx protoNumActivePotential = proto.init( "numActivePotentialSynapsesForSegment", len(self.numActivePotentialSynapsesForSegment)) for i, numActivePotentialSynapses in enumerate( self.numActivePotentialSynapsesForSegment): segment = self.connections.segmentForFlatIdx(i) if segment is not None: protoNumActivePotential[i].cell = segment.cell idx = self.connections.segmentsForCell(segment.cell).index(segment) protoNumActivePotential[i].idxOnCell = idx protoNumActivePotential[i].number = numActivePotentialSynapses proto.iteration = self.iteration protoLastUsedIteration = proto.init( "lastUsedIterationForSegment", len(self.numActivePotentialSynapsesForSegment)) for i, lastUsed in enumerate(self.lastUsedIterationForSegment): segment = self.connections.segmentForFlatIdx(i) if segment is not None: protoLastUsedIteration[i].cell = segment.cell idx = self.connections.segmentsForCell(segment.cell).index(segment) protoLastUsedIteration[i].idxOnCell = idx protoLastUsedIteration[i].number = lastUsed
def read(cls, proto): """ Reads deserialized data from proto object. :param proto: (DynamicStructBuilder) Proto object :returns: (:class:TemporalMemory) TemporalMemory instance """ tm = object.__new__(cls) # capnp fails to save a tuple, so proto.columnDimensions was forced to # serialize as a list. We prefer a tuple, however, because columnDimensions # should be regarded as immutable. tm.columnDimensions = tuple(proto.columnDimensions) tm.cellsPerColumn = int(proto.cellsPerColumn) tm.activationThreshold = int(proto.activationThreshold) tm.initialPermanence = round(proto.initialPermanence, EPSILON_ROUND) tm.connectedPermanence = round(proto.connectedPermanence, EPSILON_ROUND) tm.minThreshold = int(proto.minThreshold) tm.maxNewSynapseCount = int(proto.maxNewSynapseCount) tm.permanenceIncrement = round(proto.permanenceIncrement, EPSILON_ROUND) tm.permanenceDecrement = round(proto.permanenceDecrement, EPSILON_ROUND) tm.predictedSegmentDecrement = round(proto.predictedSegmentDecrement, EPSILON_ROUND) tm.maxSegmentsPerCell = int(proto.maxSegmentsPerCell) tm.maxSynapsesPerSegment = int(proto.maxSynapsesPerSegment) tm.connections = Connections.read(proto.connections) #pylint: disable=W0212 tm._random = Random() tm._random.read(proto.random) #pylint: enable=W0212 tm.activeCells = [int(x) for x in proto.activeCells] tm.winnerCells = [int(x) for x in proto.winnerCells] flatListLength = tm.connections.segmentFlatListLength() tm.numActiveConnectedSynapsesForSegment = [0] * flatListLength tm.numActivePotentialSynapsesForSegment = [0] * flatListLength tm.lastUsedIterationForSegment = [0] * flatListLength tm.activeSegments = [] tm.matchingSegments = [] for protoSegment in proto.activeSegments: tm.activeSegments.append( tm.connections.getSegment(protoSegment.cell, protoSegment.idxOnCell)) for protoSegment in proto.matchingSegments: tm.matchingSegments.append( tm.connections.getSegment(protoSegment.cell, protoSegment.idxOnCell)) for protoSegment in proto.numActivePotentialSynapsesForSegment: segment = tm.connections.getSegment(protoSegment.cell, protoSegment.idxOnCell) tm.numActivePotentialSynapsesForSegment[segment.flatIdx] = ( int(protoSegment.number)) tm.iteration = long(proto.iteration) for protoSegment in proto.lastUsedIterationForSegment: segment = tm.connections.getSegment(protoSegment.cell, protoSegment.idxOnCell) tm.lastUsedIterationForSegment[segment.flatIdx] = ( long(protoSegment.number)) return tm
def generateFromNumbers(self, numbers): """ Generate a sequence from a list of numbers. Note: Any `None` in the list of numbers is considered a reset. @param numbers (list) List of numbers @return (list) Generated sequence """ sequence = [] for number in numbers: if number == None: sequence.append(number) else: pattern = self.patternMachine.get(number) sequence.append(pattern) return sequence
def addSpatialNoise(self, sequence, amount): """ Add spatial noise to each pattern in the sequence. @param sequence (list) Sequence @param amount (float) Amount of spatial noise @return (list) Sequence with spatial noise """ newSequence = [] for pattern in sequence: if pattern is not None: pattern = self.patternMachine.addNoise(pattern, amount) newSequence.append(pattern) return newSequence
def prettyPrintSequence(self, sequence, verbosity=1): """ Pretty print a sequence. @param sequence (list) Sequence @param verbosity (int) Verbosity level @return (string) Pretty-printed text """ text = "" for i in xrange(len(sequence)): pattern = sequence[i] if pattern == None: text += "<reset>" if i < len(sequence) - 1: text += "\n" else: text += self.patternMachine.prettyPrintPattern(pattern, verbosity=verbosity) return text
def ROCCurve(y_true, y_score): """compute Receiver operating characteristic (ROC) Note: this implementation is restricted to the binary classification task. Parameters ---------- y_true : array, shape = [n_samples] true binary labels y_score : array, shape = [n_samples] target scores, can either be probability estimates of the positive class, confidence values, or binary decisions. Returns ------- fpr : array, shape = [>2] False Positive Rates tpr : array, shape = [>2] True Positive Rates thresholds : array, shape = [>2] Thresholds on y_score used to compute fpr and tpr Examples -------- >>> import numpy as np >>> from sklearn import metrics >>> y = np.array([1, 1, 2, 2]) >>> scores = np.array([0.1, 0.4, 0.35, 0.8]) >>> fpr, tpr, thresholds = metrics.roc_curve(y, scores) >>> fpr array([ 0. , 0.5, 0.5, 1. ]) References ---------- http://en.wikipedia.org/wiki/Receiver_operating_characteristic """ y_true = np.ravel(y_true) classes = np.unique(y_true) # ROC only for binary classification if classes.shape[0] != 2: raise ValueError("ROC is defined for binary classification only") y_score = np.ravel(y_score) n_pos = float(np.sum(y_true == classes[1])) # nb of true positive n_neg = float(np.sum(y_true == classes[0])) # nb of true negative thresholds = np.unique(y_score) neg_value, pos_value = classes[0], classes[1] tpr = np.empty(thresholds.size, dtype=np.float) # True positive rate fpr = np.empty(thresholds.size, dtype=np.float) # False positive rate # Build tpr/fpr vector current_pos_count = current_neg_count = sum_pos = sum_neg = idx = 0 signal = np.c_[y_score, y_true] sorted_signal = signal[signal[:, 0].argsort(), :][::-1] last_score = sorted_signal[0][0] for score, value in sorted_signal: if score == last_score: if value == pos_value: current_pos_count += 1 else: current_neg_count += 1 else: tpr[idx] = (sum_pos + current_pos_count) / n_pos fpr[idx] = (sum_neg + current_neg_count) / n_neg sum_pos += current_pos_count sum_neg += current_neg_count current_pos_count = 1 if value == pos_value else 0 current_neg_count = 1 if value == neg_value else 0 idx += 1 last_score = score else: tpr[-1] = (sum_pos + current_pos_count) / n_pos fpr[-1] = (sum_neg + current_neg_count) / n_neg # hard decisions, add (0,0) if fpr.shape[0] == 2: fpr = np.array([0.0, fpr[0], fpr[1]]) tpr = np.array([0.0, tpr[0], tpr[1]]) # trivial decisions, add (0,0) and (1,1) elif fpr.shape[0] == 1: fpr = np.array([0.0, fpr[0], 1.0]) tpr = np.array([0.0, tpr[0], 1.0]) return fpr, tpr, thresholds
def AreaUnderCurve(x, y): """Compute Area Under the Curve (AUC) using the trapezoidal rule Parameters ---------- x : array, shape = [n] x coordinates y : array, shape = [n] y coordinates Returns ------- auc : float Examples -------- >>> import numpy as np >>> from sklearn import metrics >>> y = np.array([1, 1, 2, 2]) >>> pred = np.array([0.1, 0.4, 0.35, 0.8]) >>> fpr, tpr, thresholds = metrics.roc_curve(y, pred) >>> metrics.auc(fpr, tpr) 0.75 """ #x, y = check_arrays(x, y) if x.shape[0] != y.shape[0]: raise ValueError('x and y should have the same shape' ' to compute area under curve,' ' but x.shape = %s and y.shape = %s.' % (x.shape, y.shape)) if x.shape[0] < 2: raise ValueError('At least 2 points are needed to compute' ' area under curve, but x.shape = %s' % x.shape) # reorder the data points according to the x axis order = np.argsort(x) x = x[order] y = y[order] h = np.diff(x) area = np.sum(h * (y[1:] + y[:-1])) / 2.0 return area
def mmPrettyPrintTraces(traces, breakOnResets=None): """ Returns pretty-printed table of traces. @param traces (list) Traces to print in table @param breakOnResets (BoolsTrace) Trace of resets to break table on @return (string) Pretty-printed table of traces. """ assert len(traces) > 0, "No traces found" table = PrettyTable(["#"] + [trace.prettyPrintTitle() for trace in traces]) for i in xrange(len(traces[0].data)): if breakOnResets and breakOnResets.data[i]: table.add_row(["<reset>"] * (len(traces) + 1)) table.add_row([i] + [trace.prettyPrintDatum(trace.data[i]) for trace in traces]) return table.get_string().encode("utf-8")
def mmPrettyPrintMetrics(metrics, sigFigs=5): """ Returns pretty-printed table of metrics. @param metrics (list) Traces to print in table @param sigFigs (int) Number of significant figures to print @return (string) Pretty-printed table of metrics. """ assert len(metrics) > 0, "No metrics found" table = PrettyTable(["Metric", "mean", "standard deviation", "min", "max", "sum", ]) for metric in metrics: table.add_row([metric.prettyPrintTitle()] + metric.getStats()) return table.get_string().encode("utf-8")
def mmGetCellTracePlot(self, cellTrace, cellCount, activityType, title="", showReset=False, resetShading=0.25): """ Returns plot of the cell activity. Note that if many timesteps of activities are input, matplotlib's image interpolation may omit activities (columns in the image). @param cellTrace (list) a temporally ordered list of sets of cell activities @param cellCount (int) number of cells in the space being rendered @param activityType (string) type of cell activity being displayed @param title (string) an optional title for the figure @param showReset (bool) if true, the first set of cell activities after a reset will have a grayscale background @param resetShading (float) applicable if showReset is true, specifies the intensity of the reset background with 0.0 being white and 1.0 being black @return (Plot) plot """ plot = Plot(self, title) resetTrace = self.mmGetTraceResets().data data = numpy.zeros((cellCount, 1)) for i in xrange(len(cellTrace)): # Set up a "background" vector that is shaded or blank if showReset and resetTrace[i]: activity = numpy.ones((cellCount, 1)) * resetShading else: activity = numpy.zeros((cellCount, 1)) activeIndices = cellTrace[i] activity[list(activeIndices)] = 1 data = numpy.concatenate((data, activity), 1) plot.add2DArray(data, xlabel="Time", ylabel=activityType, name=title) return plot
def estimateAnomalyLikelihoods(anomalyScores, averagingWindow=10, skipRecords=0, verbosity=0): """ Given a series of anomaly scores, compute the likelihood for each score. This function should be called once on a bunch of historical anomaly scores for an initial estimate of the distribution. It should be called again every so often (say every 50 records) to update the estimate. :param anomalyScores: a list of records. Each record is a list with the following three elements: [timestamp, value, score] Example:: [datetime.datetime(2013, 8, 10, 23, 0), 6.0, 1.0] For best results, the list should be between 1000 and 10,000 records :param averagingWindow: integer number of records to average over :param skipRecords: integer specifying number of records to skip when estimating distributions. If skip records are >= len(anomalyScores), a very broad distribution is returned that makes everything pretty likely. :param verbosity: integer controlling extent of printouts for debugging 0 = none 1 = occasional information 2 = print every record :returns: 3-tuple consisting of: - likelihoods numpy array of likelihoods, one for each aggregated point - avgRecordList list of averaged input records - params a small JSON dict that contains the state of the estimator """ if verbosity > 1: print("In estimateAnomalyLikelihoods.") print("Number of anomaly scores:", len(anomalyScores)) print("Skip records=", skipRecords) print("First 20:", anomalyScores[0:min(20, len(anomalyScores))]) if len(anomalyScores) == 0: raise ValueError("Must have at least one anomalyScore") # Compute averaged anomaly scores aggRecordList, historicalValues, total = _anomalyScoreMovingAverage( anomalyScores, windowSize = averagingWindow, verbosity = verbosity) s = [r[2] for r in aggRecordList] dataValues = numpy.array(s) # Estimate the distribution of anomaly scores based on aggregated records if len(aggRecordList) <= skipRecords: distributionParams = nullDistribution(verbosity = verbosity) else: distributionParams = estimateNormal(dataValues[skipRecords:]) # HACK ALERT! The HTMPredictionModel currently does not handle constant # metric values very well (time of day encoder changes sometimes lead to # unstable SDR's even though the metric is constant). Until this is # resolved, we explicitly detect and handle completely flat metric values by # reporting them as not anomalous. s = [r[1] for r in aggRecordList] # Only do this if the values are numeric if all([isinstance(r[1], numbers.Number) for r in aggRecordList]): metricValues = numpy.array(s) metricDistribution = estimateNormal(metricValues[skipRecords:], performLowerBoundCheck=False) if metricDistribution["variance"] < 1.5e-5: distributionParams = nullDistribution(verbosity = verbosity) # Estimate likelihoods based on this distribution likelihoods = numpy.array(dataValues, dtype=float) for i, s in enumerate(dataValues): likelihoods[i] = tailProbability(s, distributionParams) # Filter likelihood values filteredLikelihoods = numpy.array( _filterLikelihoods(likelihoods) ) params = { "distribution": distributionParams, "movingAverage": { "historicalValues": historicalValues, "total": total, "windowSize": averagingWindow, }, "historicalLikelihoods": list(likelihoods[-min(averagingWindow, len(likelihoods)):]), } if verbosity > 1: print("Discovered params=") print(params) print("Number of likelihoods:", len(likelihoods)) print("First 20 likelihoods:", ( filteredLikelihoods[0:min(20, len(filteredLikelihoods))] )) print("leaving estimateAnomalyLikelihoods") return (filteredLikelihoods, aggRecordList, params)
def updateAnomalyLikelihoods(anomalyScores, params, verbosity=0): """ Compute updated probabilities for anomalyScores using the given params. :param anomalyScores: a list of records. Each record is a list with the following three elements: [timestamp, value, score] Example:: [datetime.datetime(2013, 8, 10, 23, 0), 6.0, 1.0] :param params: the JSON dict returned by estimateAnomalyLikelihoods :param verbosity: integer controlling extent of printouts for debugging :type verbosity: int :returns: 3-tuple consisting of: - likelihoods numpy array of likelihoods, one for each aggregated point - avgRecordList list of averaged input records - params an updated JSON object containing the state of this metric. """ if verbosity > 3: print("In updateAnomalyLikelihoods.") print("Number of anomaly scores:", len(anomalyScores)) print("First 20:", anomalyScores[0:min(20, len(anomalyScores))]) print("Params:", params) if len(anomalyScores) == 0: raise ValueError("Must have at least one anomalyScore") if not isValidEstimatorParams(params): raise ValueError("'params' is not a valid params structure") # For backward compatibility. if "historicalLikelihoods" not in params: params["historicalLikelihoods"] = [1.0] # Compute moving averages of these new scores using the previous values # as well as likelihood for these scores using the old estimator historicalValues = params["movingAverage"]["historicalValues"] total = params["movingAverage"]["total"] windowSize = params["movingAverage"]["windowSize"] aggRecordList = numpy.zeros(len(anomalyScores), dtype=float) likelihoods = numpy.zeros(len(anomalyScores), dtype=float) for i, v in enumerate(anomalyScores): newAverage, historicalValues, total = ( MovingAverage.compute(historicalValues, total, v[2], windowSize) ) aggRecordList[i] = newAverage likelihoods[i] = tailProbability(newAverage, params["distribution"]) # Filter the likelihood values. First we prepend the historical likelihoods # to the current set. Then we filter the values. We peel off the likelihoods # to return and the last windowSize values to store for later. likelihoods2 = params["historicalLikelihoods"] + list(likelihoods) filteredLikelihoods = _filterLikelihoods(likelihoods2) likelihoods[:] = filteredLikelihoods[-len(likelihoods):] historicalLikelihoods = likelihoods2[-min(windowSize, len(likelihoods2)):] # Update the estimator newParams = { "distribution": params["distribution"], "movingAverage": { "historicalValues": historicalValues, "total": total, "windowSize": windowSize, }, "historicalLikelihoods": historicalLikelihoods, } assert len(newParams["historicalLikelihoods"]) <= windowSize if verbosity > 3: print("Number of likelihoods:", len(likelihoods)) print("First 20 likelihoods:", likelihoods[0:min(20, len(likelihoods))]) print("Leaving updateAnomalyLikelihoods.") return (likelihoods, aggRecordList, newParams)
def _filterLikelihoods(likelihoods, redThreshold=0.99999, yellowThreshold=0.999): """ Filter the list of raw (pre-filtered) likelihoods so that we only preserve sharp increases in likelihood. 'likelihoods' can be a numpy array of floats or a list of floats. :returns: A new list of floats likelihoods containing the filtered values. """ redThreshold = 1.0 - redThreshold yellowThreshold = 1.0 - yellowThreshold # The first value is untouched filteredLikelihoods = [likelihoods[0]] for i, v in enumerate(likelihoods[1:]): if v <= redThreshold: # Value is in the redzone if likelihoods[i] > redThreshold: # Previous value is not in redzone, so leave as-is filteredLikelihoods.append(v) else: filteredLikelihoods.append(yellowThreshold) else: # Value is below the redzone, so leave as-is filteredLikelihoods.append(v) return filteredLikelihoods
def _anomalyScoreMovingAverage(anomalyScores, windowSize=10, verbosity=0, ): """ Given a list of anomaly scores return a list of averaged records. anomalyScores is assumed to be a list of records of the form: [datetime.datetime(2013, 8, 10, 23, 0), 6.0, 1.0] Each record in the returned list list contains: [datetime, value, averagedScore] *Note:* we only average the anomaly score. """ historicalValues = [] total = 0.0 averagedRecordList = [] # Aggregated records for record in anomalyScores: # Skip (but log) records without correct number of entries if not isinstance(record, (list, tuple)) or len(record) != 3: if verbosity >= 1: print("Malformed record:", record) continue avg, historicalValues, total = ( MovingAverage.compute(historicalValues, total, record[2], windowSize) ) averagedRecordList.append( [record[0], record[1], avg] ) if verbosity > 2: print("Aggregating input record:", record) print("Result:", [record[0], record[1], avg]) return averagedRecordList, historicalValues, total
def estimateNormal(sampleData, performLowerBoundCheck=True): """ :param sampleData: :type sampleData: Numpy array. :param performLowerBoundCheck: :type performLowerBoundCheck: bool :returns: A dict containing the parameters of a normal distribution based on the ``sampleData``. """ params = { "name": "normal", "mean": numpy.mean(sampleData), "variance": numpy.var(sampleData), } if performLowerBoundCheck: # Handle edge case of almost no deviations and super low anomaly scores. We # find that such low anomaly means can happen, but then the slightest blip # of anomaly score can cause the likelihood to jump up to red. if params["mean"] < 0.03: params["mean"] = 0.03 # Catch all for super low variance to handle numerical precision issues if params["variance"] < 0.0003: params["variance"] = 0.0003 # Compute standard deviation if params["variance"] > 0: params["stdev"] = math.sqrt(params["variance"]) else: params["stdev"] = 0 return params
def tailProbability(x, distributionParams): """ Given the normal distribution specified by the mean and standard deviation in distributionParams, return the probability of getting samples further from the mean. For values above the mean, this is the probability of getting samples > x and for values below the mean, the probability of getting samples < x. This is the Q-function: the tail probability of the normal distribution. :param distributionParams: dict with 'mean' and 'stdev' of the distribution """ if "mean" not in distributionParams or "stdev" not in distributionParams: raise RuntimeError("Insufficient parameters to specify the distribution.") if x < distributionParams["mean"]: # Gaussian is symmetrical around mean, so flip to get the tail probability xp = 2 * distributionParams["mean"] - x return tailProbability(xp, distributionParams) # Calculate the Q function with the complementary error function, explained # here: http://www.gaussianwaves.com/2012/07/q-function-and-error-functions z = (x - distributionParams["mean"]) / distributionParams["stdev"] return 0.5 * math.erfc(z/1.4142)
def isValidEstimatorParams(p): """ :returns: ``True`` if ``p`` is a valid estimator params as might be returned by ``estimateAnomalyLikelihoods()`` or ``updateAnomalyLikelihoods``, ``False`` otherwise. Just does some basic validation. """ if not isinstance(p, dict): return False if "distribution" not in p: return False if "movingAverage" not in p: return False dist = p["distribution"] if not ("mean" in dist and "name" in dist and "variance" in dist and "stdev" in dist): return False return True
def _calcSkipRecords(numIngested, windowSize, learningPeriod): """Return the value of skipRecords for passing to estimateAnomalyLikelihoods If `windowSize` is very large (bigger than the amount of data) then this could just return `learningPeriod`. But when some values have fallen out of the historical sliding window of anomaly records, then we have to take those into account as well so we return the `learningPeriod` minus the number shifted out. :param numIngested - (int) number of data points that have been added to the sliding window of historical data points. :param windowSize - (int) size of sliding window of historical data points. :param learningPeriod - (int) the number of iterations required for the algorithm to learn the basic patterns in the dataset and for the anomaly score to 'settle down'. """ numShiftedOut = max(0, numIngested - windowSize) return min(numIngested, max(0, learningPeriod - numShiftedOut))
def read(cls, proto): """ capnp deserialization method for the anomaly likelihood object :param proto: (Object) capnp proto object specified in nupic.regions.anomaly_likelihood.capnp :returns: (Object) the deserialized AnomalyLikelihood object """ # pylint: disable=W0212 anomalyLikelihood = object.__new__(cls) anomalyLikelihood._iteration = proto.iteration anomalyLikelihood._historicalScores = collections.deque( maxlen=proto.historicWindowSize) for i, score in enumerate(proto.historicalScores): anomalyLikelihood._historicalScores.append((i, score.value, score.anomalyScore)) if proto.distribution.name: # is "" when there is no distribution. anomalyLikelihood._distribution = dict() anomalyLikelihood._distribution['distribution'] = dict() anomalyLikelihood._distribution['distribution']["name"] = proto.distribution.name anomalyLikelihood._distribution['distribution']["mean"] = proto.distribution.mean anomalyLikelihood._distribution['distribution']["variance"] = proto.distribution.variance anomalyLikelihood._distribution['distribution']["stdev"] = proto.distribution.stdev anomalyLikelihood._distribution["movingAverage"] = {} anomalyLikelihood._distribution["movingAverage"]["windowSize"] = proto.distribution.movingAverage.windowSize anomalyLikelihood._distribution["movingAverage"]["historicalValues"] = [] for value in proto.distribution.movingAverage.historicalValues: anomalyLikelihood._distribution["movingAverage"]["historicalValues"].append(value) anomalyLikelihood._distribution["movingAverage"]["total"] = proto.distribution.movingAverage.total anomalyLikelihood._distribution["historicalLikelihoods"] = [] for likelihood in proto.distribution.historicalLikelihoods: anomalyLikelihood._distribution["historicalLikelihoods"].append(likelihood) else: anomalyLikelihood._distribution = None anomalyLikelihood._probationaryPeriod = proto.probationaryPeriod anomalyLikelihood._learningPeriod = proto.learningPeriod anomalyLikelihood._reestimationPeriod = proto.reestimationPeriod # pylint: enable=W0212 return anomalyLikelihood
def write(self, proto): """ capnp serialization method for the anomaly likelihood object :param proto: (Object) capnp proto object specified in nupic.regions.anomaly_likelihood.capnp """ proto.iteration = self._iteration pHistScores = proto.init('historicalScores', len(self._historicalScores)) for i, score in enumerate(list(self._historicalScores)): _, value, anomalyScore = score record = pHistScores[i] record.value = float(value) record.anomalyScore = float(anomalyScore) if self._distribution: proto.distribution.name = self._distribution["distribution"]["name"] proto.distribution.mean = float(self._distribution["distribution"]["mean"]) proto.distribution.variance = float(self._distribution["distribution"]["variance"]) proto.distribution.stdev = float(self._distribution["distribution"]["stdev"]) proto.distribution.movingAverage.windowSize = float(self._distribution["movingAverage"]["windowSize"]) historicalValues = self._distribution["movingAverage"]["historicalValues"] pHistValues = proto.distribution.movingAverage.init( "historicalValues", len(historicalValues)) for i, value in enumerate(historicalValues): pHistValues[i] = float(value) #proto.distribution.movingAverage.historicalValues = self._distribution["movingAverage"]["historicalValues"] proto.distribution.movingAverage.total = float(self._distribution["movingAverage"]["total"]) historicalLikelihoods = self._distribution["historicalLikelihoods"] pHistLikelihoods = proto.distribution.init("historicalLikelihoods", len(historicalLikelihoods)) for i, likelihood in enumerate(historicalLikelihoods): pHistLikelihoods[i] = float(likelihood) proto.probationaryPeriod = self._probationaryPeriod proto.learningPeriod = self._learningPeriod proto.reestimationPeriod = self._reestimationPeriod proto.historicWindowSize = self._historicalScores.maxlen
def anomalyProbability(self, value, anomalyScore, timestamp=None): """ Compute the probability that the current value plus anomaly score represents an anomaly given the historical distribution of anomaly scores. The closer the number is to 1, the higher the chance it is an anomaly. :param value: the current metric ("raw") input value, eg. "orange", or '21.2' (deg. Celsius), ... :param anomalyScore: the current anomaly score :param timestamp: [optional] timestamp of the ocurrence, default (None) results in using iteration step. :returns: the anomalyLikelihood for this record. """ if timestamp is None: timestamp = self._iteration dataPoint = (timestamp, value, anomalyScore) # We ignore the first probationaryPeriod data points if self._iteration < self._probationaryPeriod: likelihood = 0.5 else: # On a rolling basis we re-estimate the distribution if ( (self._distribution is None) or (self._iteration % self._reestimationPeriod == 0) ): numSkipRecords = self._calcSkipRecords( numIngested=self._iteration, windowSize=self._historicalScores.maxlen, learningPeriod=self._learningPeriod) _, _, self._distribution = estimateAnomalyLikelihoods( self._historicalScores, skipRecords=numSkipRecords) likelihoods, _, self._distribution = updateAnomalyLikelihoods( [dataPoint], self._distribution) likelihood = 1.0 - likelihoods[0] # Before we exit update historical scores and iteration self._historicalScores.append(dataPoint) self._iteration += 1 return likelihood
def _getImpl(self, model): """ Creates and returns the _IterationPhase-based instance corresponding to this phase specification model: Model instance """ impl = _IterationPhaseLearnOnly(model=model, nIters=self.__nIters) return impl
def _getImpl(self, model): """ Creates and returns the _IterationPhase-based instance corresponding to this phase specification model: Model instance """ impl = _IterationPhaseInferOnly(model=model, nIters=self.__nIters, inferenceArgs=self.__inferenceArgs) return impl
def replaceIterationCycle(self, phaseSpecs): """ Replaces the Iteration Cycle phases :param phaseSpecs: Iteration cycle description consisting of a sequence of IterationPhaseSpecXXXXX elements that are performed in the given order """ # ----------------------------------------------------------------------- # Replace our phase manager # self.__phaseManager = _PhaseManager( model=self.__model, phaseSpecs=phaseSpecs) return
def handleInputRecord(self, inputRecord): """ Processes the given record according to the current iteration cycle phase :param inputRecord: (object) record expected to be returned from :meth:`nupic.data.record_stream.RecordStreamIface.getNextRecord`. :returns: :class:`nupic.frameworks.opf.opf_utils.ModelResult` """ assert inputRecord, "Invalid inputRecord: %r" % inputRecord results = self.__phaseManager.handleInputRecord(inputRecord) metrics = self.__metricsMgr.update(results) # Execute task-postIter callbacks for cb in self.__userCallbacks['postIter']: cb(self.__model) results.metrics = metrics # Return the input and predictions for this record return results
def __advancePhase(self): """ Advance to the next iteration cycle phase """ self.__currentPhase = self.__phaseCycler.next() self.__currentPhase.enterPhase() return
def handleInputRecord(self, inputRecord): """ Processes the given record according to the current phase inputRecord: record object formatted according to nupic.data.FileSource.getNext() result format. Returns: An opf_utils.ModelResult object with the inputs and inferences after the current record is processed by the model """ results = self.__model.run(inputRecord) shouldContinue = self.__currentPhase.advance() if not shouldContinue: self.__advancePhase() return results
def enterPhase(self): """ Performs initialization that is necessary upon entry to the phase. Must be called before handleInputRecord() at the beginning of each phase """ self.__iter = iter(xrange(self.__nIters)) # Prime the iterator self.__iter.next()
def advance(self): """ Advances the iteration; Returns: True if more iterations remain; False if this is the final iteration. """ hasMore = True try: self.__iter.next() except StopIteration: self.__iter = None hasMore = False return hasMore
def enterPhase(self): """ [_IterationPhase method implementation] Performs initialization that is necessary upon entry to the phase. Must be called before handleInputRecord() at the beginning of each phase """ super(_IterationPhaseLearnOnly, self).enterPhase() self.__model.enableLearning() self.__model.disableInference() return
def enterPhase(self): """ [_IterationPhase method implementation] Performs initialization that is necessary upon entry to the phase. Must be called before handleInputRecord() at the beginning of each phase """ super(_IterationPhaseInferCommon, self).enterPhase() self._model.enableInference(inferenceArgs=self._inferenceArgs) return
def write(self, proto): """ Serialize via capnp :param proto: capnp PreviousValueModelProto message builder """ super(PreviousValueModel, self).writeBaseToProto(proto.modelBase) proto.fieldNames = self._fieldNames proto.fieldTypes = self._fieldTypes if self._predictedField: proto.predictedField = self._predictedField proto.predictionSteps = self._predictionSteps
def read(cls, proto): """Deserialize via capnp :param proto: capnp PreviousValueModelProto message reader :returns: new instance of PreviousValueModel deserialized from the given proto """ instance = object.__new__(cls) super(PreviousValueModel, instance).__init__(proto=proto.modelBase) instance._logger = opf_utils.initLogger(instance) if len(proto.predictedField): instance._predictedField = proto.predictedField else: instance._predictedField = None instance._fieldNames = list(proto.fieldNames) instance._fieldTypes = list(proto.fieldTypes) instance._predictionSteps = list(proto.predictionSteps) return instance
def lscsum(lx, epsilon=None): """ Accepts log-values as input, exponentiates them, computes the sum, then converts the sum back to log-space and returns the result. Handles underflow by rescaling so that the largest values is exactly 1.0. """ lx = numpy.asarray(lx) base = lx.max() # If the input is the log of 0's, catch this condition before we generate # an exception, and return the log(0) if numpy.isinf(base): return base # If the user specified an epsilon and we are below it, return epsilon if (epsilon is not None) and (base < epsilon): return epsilon x = numpy.exp(lx - base) ssum = x.sum() result = numpy.log(ssum) + base # try: # conventional = numpy.log(numpy.exp(lx).sum()) # if not similar(result, conventional): # if numpy.isinf(conventional).any() and not numpy.isinf(result).any(): # # print "Scaled log sum avoided underflow or overflow." # pass # else: # import sys # print >>sys.stderr, "Warning: scaled log sum did not match." # print >>sys.stderr, "Scaled log result:" # print >>sys.stderr, result # print >>sys.stderr, "Conventional result:" # print >>sys.stderr, conventional # except FloatingPointError, e: # # print "Scaled log sum avoided underflow or overflow." # pass return result
def lscsum0(lx): """ Accepts log-values as input, exponentiates them, sums down the rows (first dimension), then converts the sum back to log-space and returns the result. Handles underflow by rescaling so that the largest values is exactly 1.0. """ # rows = lx.shape[0] # columns = numpy.prod(lx.shape[1:]) # lx = lx.reshape(rows, columns) # bases = lx.max(1).reshape(rows, 1) # bases = lx.max(0).reshape((1,) + lx.shape[1:]) lx = numpy.asarray(lx) bases = lx.max(0) # Don't need to reshape in the case of 0. x = numpy.exp(lx - bases) ssum = x.sum(0) result = numpy.log(ssum) + bases try: conventional = numpy.log(numpy.exp(lx).sum(0)) if not similar(result, conventional): if numpy.isinf(conventional).any() and not numpy.isinf(result).any(): # print "Scaled log sum down axis 0 avoided underflow or overflow." pass else: import sys print >>sys.stderr, "Warning: scaled log sum down axis 0 did not match." print >>sys.stderr, "Scaled log result:" print >>sys.stderr, result print >>sys.stderr, "Conventional result:" print >>sys.stderr, conventional except FloatingPointError, e: # print "Scaled log sum down axis 0 avoided underflow or overflow." pass return result
def normalize(lx): """ Accepts log-values as input, exponentiates them, normalizes and returns the result. Handles underflow by rescaling so that the largest values is exactly 1.0. """ lx = numpy.asarray(lx) base = lx.max() x = numpy.exp(lx - base) result = x / x.sum() conventional = (numpy.exp(lx) / numpy.exp(lx).sum()) assert similar(result, conventional) return result
def nsum0(lx): """ Accepts log-values as input, exponentiates them, sums down the rows (first dimension), normalizes and returns the result. Handles underflow by rescaling so that the largest values is exactly 1.0. """ lx = numpy.asarray(lx) base = lx.max() x = numpy.exp(lx - base) ssum = x.sum(0) result = ssum / ssum.sum() conventional = (numpy.exp(lx).sum(0) / numpy.exp(lx).sum()) assert similar(result, conventional) return result
def logSumExp(A, B, out=None): """ returns log(exp(A) + exp(B)). A and B are numpy arrays""" if out is None: out = numpy.zeros(A.shape) indicator1 = A >= B indicator2 = numpy.logical_not(indicator1) out[indicator1] = A[indicator1] + numpy.log1p(numpy.exp(B[indicator1]-A[indicator1])) out[indicator2] = B[indicator2] + numpy.log1p(numpy.exp(A[indicator2]-B[indicator2])) return out
def logDiffExp(A, B, out=None): """ returns log(exp(A) - exp(B)). A and B are numpy arrays. values in A should be greater than or equal to corresponding values in B""" if out is None: out = numpy.zeros(A.shape) indicator1 = A >= B assert indicator1.all(), "Values in the first array should be greater than the values in the second" out[indicator1] = A[indicator1] + numpy.log(1 - numpy.exp(B[indicator1]-A[indicator1])) return out
def _getFieldIndexBySpecial(fields, special): """ Return index of the field matching the field meta special value. :param fields: sequence of nupic.data.fieldmeta.FieldMetaInfo objects representing the fields of a stream :param special: one of the special field attribute values from nupic.data.fieldmeta.FieldMetaSpecial :returns: first zero-based index of the field tagged with the target field meta special attribute; None if no such field """ for i, field in enumerate(fields): if field.special == special: return i return None
def encode(self, inputRow): """Encodes the given input row as a dict, with the keys being the field names. This also adds in some meta fields: '_category': The value from the category field (if any) '_reset': True if the reset field was True (if any) '_sequenceId': the value from the sequenceId field (if any) :param inputRow: sequence of values corresponding to a single input metric data row :rtype: dict """ # Create the return dict result = dict(zip(self._fieldNames, inputRow)) # Add in the special fields if self._categoryFieldIndex is not None: # category value can be an int or a list if isinstance(inputRow[self._categoryFieldIndex], int): result['_category'] = [inputRow[self._categoryFieldIndex]] else: result['_category'] = (inputRow[self._categoryFieldIndex] if inputRow[self._categoryFieldIndex] else [None]) else: result['_category'] = [None] if self._resetFieldIndex is not None: result['_reset'] = int(bool(inputRow[self._resetFieldIndex])) else: result['_reset'] = 0 if self._learningFieldIndex is not None: result['_learning'] = int(bool(inputRow[self._learningFieldIndex])) result['_timestampRecordIdx'] = None if self._timestampFieldIndex is not None: result['_timestamp'] = inputRow[self._timestampFieldIndex] # Compute the record index based on timestamp result['_timestampRecordIdx'] = self._computeTimestampRecordIdx( inputRow[self._timestampFieldIndex]) else: result['_timestamp'] = None # ----------------------------------------------------------------------- # Figure out the sequence ID hasReset = self._resetFieldIndex is not None hasSequenceId = self._sequenceFieldIndex is not None if hasReset and not hasSequenceId: # Reset only if result['_reset']: self._sequenceId += 1 sequenceId = self._sequenceId elif not hasReset and hasSequenceId: sequenceId = inputRow[self._sequenceFieldIndex] result['_reset'] = int(sequenceId != self._sequenceId) self._sequenceId = sequenceId elif hasReset and hasSequenceId: sequenceId = inputRow[self._sequenceFieldIndex] else: sequenceId = 0 if sequenceId is not None: result['_sequenceId'] = hash(sequenceId) else: result['_sequenceId'] = None return result
def _computeTimestampRecordIdx(self, recordTS): """ Give the timestamp of a record (a datetime object), compute the record's timestamp index - this is the timestamp divided by the aggregation period. Parameters: ------------------------------------------------------------------------ recordTS: datetime instance retval: record timestamp index, or None if no aggregation period """ if self._aggregationPeriod is None: return None # Base record index on number of elapsed months if aggregation is in # months if self._aggregationPeriod['months'] > 0: assert self._aggregationPeriod['seconds'] == 0 result = int( (recordTS.year * 12 + (recordTS.month-1)) / self._aggregationPeriod['months']) # Base record index on elapsed seconds elif self._aggregationPeriod['seconds'] > 0: delta = recordTS - datetime.datetime(year=1, month=1, day=1) deltaSecs = delta.days * 24 * 60 * 60 \ + delta.seconds \ + delta.microseconds / 1000000.0 result = int(deltaSecs / self._aggregationPeriod['seconds']) else: result = None return result
def getNextRecordDict(self): """Returns next available data record from the storage as a dict, with the keys being the field names. This also adds in some meta fields: - ``_category``: The value from the category field (if any) - ``_reset``: True if the reset field was True (if any) - ``_sequenceId``: the value from the sequenceId field (if any) """ values = self.getNextRecord() if values is None: return None if not values: return dict() if self._modelRecordEncoder is None: self._modelRecordEncoder = ModelRecordEncoder( fields=self.getFields(), aggregationPeriod=self.getAggregationMonthsAndSeconds()) return self._modelRecordEncoder.encode(values)
def getFieldMin(self, fieldName): """ If underlying implementation does not support min/max stats collection, or if a field type does not support min/max (non scalars), the return value will be None. :param fieldName: (string) name of field to get min :returns: current minimum value for the field ``fieldName``. """ stats = self.getStats() if stats == None: return None minValues = stats.get('min', None) if minValues == None: return None index = self.getFieldNames().index(fieldName) return minValues[index]
def getFieldMax(self, fieldName): """ If underlying implementation does not support min/max stats collection, or if a field type does not support min/max (non scalars), the return value will be None. :param fieldName: (string) name of field to get max :returns: current maximum value for the field ``fieldName``. """ stats = self.getStats() if stats == None: return None maxValues = stats.get('max', None) if maxValues == None: return None index = self.getFieldNames().index(fieldName) return maxValues[index]
def debug(self, msg, *args, **kwargs): """ Log 'msg % args' with severity 'DEBUG'. To pass exception information, use the keyword argument exc_info with a true value, e.g. logger.debug("Houston, we have a %s", "thorny problem", exc_info=1) """ self._baseLogger.debug(self, self.getExtendedMsg(msg), *args, **kwargs)
def info(self, msg, *args, **kwargs): """ Log 'msg % args' with severity 'INFO'. To pass exception information, use the keyword argument exc_info with a true value, e.g. logger.info("Houston, we have a %s", "interesting problem", exc_info=1) """ self._baseLogger.info(self, self.getExtendedMsg(msg), *args, **kwargs)
def warning(self, msg, *args, **kwargs): """ Log 'msg % args' with severity 'WARNING'. To pass exception information, use the keyword argument exc_info with a true value, e.g. logger.warning("Houston, we have a %s", "bit of a problem", exc_info=1) """ self._baseLogger.warning(self, self.getExtendedMsg(msg), *args, **kwargs)
def error(self, msg, *args, **kwargs): """ Log 'msg % args' with severity 'ERROR'. To pass exception information, use the keyword argument exc_info with a true value, e.g. logger.error("Houston, we have a %s", "major problem", exc_info=1) """ self._baseLogger.error(self, self.getExtendedMsg(msg), *args, **kwargs)
def critical(self, msg, *args, **kwargs): """ Log 'msg % args' with severity 'CRITICAL'. To pass exception information, use the keyword argument exc_info with a true value, e.g. logger.critical("Houston, we have a %s", "major disaster", exc_info=1) """ self._baseLogger.critical(self, self.getExtendedMsg(msg), *args, **kwargs)
def log(self, level, msg, *args, **kwargs): """ Log 'msg % args' with the integer severity 'level'. To pass exception information, use the keyword argument exc_info with a true value, e.g. logger.log(level, "We have a %s", "mysterious problem", exc_info=1) """ self._baseLogger.log(self, level, self.getExtendedMsg(msg), *args, **kwargs)
def initFilter(input, filterInfo = None): """ Initializes internal filter variables for further processing. Returns a tuple (function to call,parameters for the filter call) The filterInfo is a dict. Here is an example structure: {fieldName: {'min': x, 'max': y, 'type': 'category', # or 'number' 'acceptValues': ['foo', 'bar'], } } This returns the following: (filterFunc, ((fieldIdx, fieldFilterFunc, filterDict), ...) Where fieldIdx is the index of the field within each record fieldFilterFunc returns True if the value is "OK" (within min, max or part of acceptValues) fieldDict is a dict containing 'type', 'min', max', 'acceptValues' """ if filterInfo is None: return None # Build an array of index/func to call on record[index] filterList = [] for i, fieldName in enumerate(input.getFieldNames()): fieldFilter = filterInfo.get(fieldName, None) if fieldFilter == None: continue var = dict() var['acceptValues'] = None min = fieldFilter.get('min', None) max = fieldFilter.get('max', None) var['min'] = min var['max'] = max if fieldFilter['type'] == 'category': var['acceptValues'] = fieldFilter['acceptValues'] fp = lambda x: (x['value'] != SENTINEL_VALUE_FOR_MISSING_DATA and \ x['value'] in x['acceptValues']) elif fieldFilter['type'] == 'number': if min != None and max != None: fp = lambda x: (x['value'] != SENTINEL_VALUE_FOR_MISSING_DATA and \ x['value'] >= x['min'] and x['value'] <= x['max']) elif min != None: fp = lambda x: (x['value'] != SENTINEL_VALUE_FOR_MISSING_DATA and \ x['value'] >= x['min']) else: fp = lambda x: (x['value'] != SENTINEL_VALUE_FOR_MISSING_DATA and \ x['value'] <= x['max']) filterList.append((i, fp, var)) return (_filterRecord, filterList)
def _filterRecord(filterList, record): """ Takes a record and returns true if record meets filter criteria, false otherwise """ for (fieldIdx, fp, params) in filterList: x = dict() x['value'] = record[fieldIdx] x['acceptValues'] = params['acceptValues'] x['min'] = params['min'] x['max'] = params['max'] if not fp(x): return False # None of the field filters triggered, accept the record as a good one return True
def _aggr_sum(inList): """ Returns sum of the elements in the list. Missing items are replaced with the mean value """ aggrMean = _aggr_mean(inList) if aggrMean == None: return None aggrSum = 0 for elem in inList: if elem != SENTINEL_VALUE_FOR_MISSING_DATA: aggrSum += elem else: aggrSum += aggrMean return aggrSum
def _aggr_mean(inList): """ Returns mean of non-None elements of the list """ aggrSum = 0 nonNone = 0 for elem in inList: if elem != SENTINEL_VALUE_FOR_MISSING_DATA: aggrSum += elem nonNone += 1 if nonNone != 0: return aggrSum / nonNone else: return None
def _aggr_mode(inList): """ Returns most common value seen in the non-None elements of the list """ valueCounts = dict() nonNone = 0 for elem in inList: if elem == SENTINEL_VALUE_FOR_MISSING_DATA: continue nonNone += 1 if elem in valueCounts: valueCounts[elem] += 1 else: valueCounts[elem] = 1 # Get the most common one if nonNone == 0: return None # Sort by counts sortedCounts = valueCounts.items() sortedCounts.sort(cmp=lambda x,y: x[1] - y[1], reverse=True) return sortedCounts[0][0]
def _aggr_weighted_mean(inList, params): """ Weighted mean uses params (must be the same size as inList) and makes weighed mean of inList""" assert(len(inList) == len(params)) # If all weights are 0, then the value is not defined, return None (missing) weightsSum = sum(params) if weightsSum == 0: return None weightedMean = 0 for i, elem in enumerate(inList): weightedMean += elem * params[i] return weightedMean / weightsSum
def generateDataset(aggregationInfo, inputFilename, outputFilename=None): """Generate a dataset of aggregated values Parameters: ---------------------------------------------------------------------------- aggregationInfo: a dictionary that contains the following entries - fields: a list of pairs. Each pair is a field name and an aggregation function (e.g. sum). The function will be used to aggregate multiple values during the aggregation period. aggregation period: 0 or more of unit=value fields; allowed units are: [years months] | [weeks days hours minutes seconds milliseconds microseconds] NOTE: years and months are mutually-exclusive with the other units. See getEndTime() and _aggregate() for more details. Example1: years=1, months=6, Example2: hours=1, minutes=30, If none of the period fields are specified or if all that are specified have values of 0, then aggregation will be suppressed, and the given inputFile parameter value will be returned. inputFilename: filename of the input dataset within examples/prediction/data outputFilename: name for the output file. If not given, a name will be generated based on the input filename and the aggregation params retval: Name of the generated output file. This will be the same as the input file name if no aggregation needed to be performed If the input file contained a time field, sequence id field or reset field that were not specified in aggregationInfo fields, those fields will be added automatically with the following rules: 1. The order will be R, S, T, rest of the fields 2. The aggregation function for all will be to pick the first: lambda x: x[0] Returns: the path of the aggregated data file if aggregation was performed (in the same directory as the given input file); if aggregation did not need to be performed, then the given inputFile argument value is returned. """ # Create the input stream inputFullPath = resource_filename("nupic.datafiles", inputFilename) inputObj = FileRecordStream(inputFullPath) # Instantiate the aggregator aggregator = Aggregator(aggregationInfo=aggregationInfo, inputFields=inputObj.getFields()) # Is it a null aggregation? If so, just return the input file unmodified if aggregator.isNullAggregation(): return inputFullPath # ------------------------------------------------------------------------ # If we were not given an output filename, create one based on the # aggregation settings if outputFilename is None: outputFilename = 'agg_%s' % \ os.path.splitext(os.path.basename(inputFullPath))[0] timePeriods = 'years months weeks days '\ 'hours minutes seconds milliseconds microseconds' for k in timePeriods.split(): if aggregationInfo.get(k, 0) > 0: outputFilename += '_%s_%d' % (k, aggregationInfo[k]) outputFilename += '.csv' outputFilename = os.path.join(os.path.dirname(inputFullPath), outputFilename) # ------------------------------------------------------------------------ # If some other process already started creating this file, simply # wait for it to finish and return without doing anything lockFilePath = outputFilename + '.please_wait' if os.path.isfile(outputFilename) or \ os.path.isfile(lockFilePath): while os.path.isfile(lockFilePath): print 'Waiting for %s to be fully written by another process' % \ lockFilePath time.sleep(1) return outputFilename # Create the lock file lockFD = open(lockFilePath, 'w') # ------------------------------------------------------------------------- # Create the output stream outputObj = FileRecordStream(streamID=outputFilename, write=True, fields=inputObj.getFields()) # ------------------------------------------------------------------------- # Write all aggregated records to the output while True: inRecord = inputObj.getNextRecord() (aggRecord, aggBookmark) = aggregator.next(inRecord, None) if aggRecord is None and inRecord is None: break if aggRecord is not None: outputObj.appendRecord(aggRecord) return outputFilename
def getFilename(aggregationInfo, inputFile): """Generate the filename for aggregated dataset The filename is based on the input filename and the aggregation period. Returns the inputFile if no aggregation required (aggregation info has all 0's) """ # Find the actual file, with an absolute path inputFile = resource_filename("nupic.datafiles", inputFile) a = defaultdict(lambda: 0, aggregationInfo) outputDir = os.path.dirname(inputFile) outputFile = 'agg_%s' % os.path.splitext(os.path.basename(inputFile))[0] noAggregation = True timePeriods = 'years months weeks days '\ 'hours minutes seconds milliseconds microseconds' for k in timePeriods.split(): if a[k] > 0: noAggregation = False outputFile += '_%s_%d' % (k, a[k]) if noAggregation: return inputFile outputFile += '.csv' outputFile = os.path.join(outputDir, outputFile) return outputFile
def _getEndTime(self, t): """Add the aggregation period to the input time t and return a datetime object Years and months are handled as aspecial case due to leap years and months with different number of dates. They can't be converted to a strict timedelta because a period of 3 months will have different durations actually. The solution is to just add the years and months fields directly to the current time. Other periods are converted to timedelta and just added to current time. """ assert isinstance(t, datetime.datetime) if self._aggTimeDelta: return t + self._aggTimeDelta else: year = t.year + self._aggYears + (t.month - 1 + self._aggMonths) / 12 month = (t.month - 1 + self._aggMonths) % 12 + 1 return t.replace(year=year, month=month)
def _getFuncPtrAndParams(self, funcName): """ Given the name of an aggregation function, returns the function pointer and param. Parameters: ------------------------------------------------------------------------ funcName: a string (name of function) or funcPtr retval: (funcPtr, param) """ params = None if isinstance(funcName, basestring): if funcName == 'sum': fp = _aggr_sum elif funcName == 'first': fp = _aggr_first elif funcName == 'last': fp = _aggr_last elif funcName == 'mean': fp = _aggr_mean elif funcName == 'max': fp = max elif funcName == 'min': fp = min elif funcName == 'mode': fp = _aggr_mode elif funcName.startswith('wmean:'): fp = _aggr_weighted_mean paramsName = funcName[6:] params = [f[0] for f in self._inputFields].index(paramsName) else: fp = funcName return (fp, params)
def _createAggregateRecord(self): """ Generate the aggregated output record Parameters: ------------------------------------------------------------------------ retval: outputRecord """ record = [] for i, (fieldIdx, aggFP, paramIdx) in enumerate(self._fields): if aggFP is None: # this field is not supposed to be aggregated. continue values = self._slice[i] refIndex = None if paramIdx is not None: record.append(aggFP(values, self._slice[paramIdx])) else: record.append(aggFP(values)) return record