partition
stringclasses
3 values
func_name
stringlengths
1
134
docstring
stringlengths
1
46.9k
path
stringlengths
4
223
original_string
stringlengths
75
104k
code
stringlengths
75
104k
docstring_tokens
listlengths
1
1.97k
repo
stringlengths
7
55
language
stringclasses
1 value
url
stringlengths
87
315
code_tokens
listlengths
19
28.4k
sha
stringlengths
40
40
valid
Configuration.setCustomProperties
Set multiple custom properties and persist them to the custom configuration store. Parameters: ---------------------------------------------------------------- properties: a dict of property name/value pairs to set
src/nupic/swarming/hypersearch/support.py
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 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)
[ "Set", "multiple", "custom", "properties", "and", "persist", "them", "to", "the", "custom", "configuration", "store", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/support.py#L572-L586
[ "def", "setCustomProperties", "(", "cls", ",", "properties", ")", ":", "_getLogger", "(", ")", ".", "info", "(", "\"Setting custom configuration properties=%r; caller=%r\"", ",", "properties", ",", "traceback", ".", "format_stack", "(", ")", ")", "_CustomConfiguration...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
Configuration.clear
Clear all configuration properties from in-memory cache, but do NOT alter the custom configuration file. Used in unit-testing.
src/nupic/swarming/hypersearch/support.py
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 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)
[ "Clear", "all", "configuration", "properties", "from", "in", "-", "memory", "cache", "but", "do", "NOT", "alter", "the", "custom", "configuration", "file", ".", "Used", "in", "unit", "-", "testing", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/support.py#L589-L598
[ "def", "clear", "(", "cls", ")", ":", "# Clear the in-memory settings cache, forcing reload upon subsequent \"get\"", "# request.", "super", "(", "Configuration", ",", "cls", ")", ".", "clear", "(", ")", "# Reset in-memory custom configuration info.", "_CustomConfigurationFileW...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
Configuration.resetCustomConfig
Clear all custom configuration settings and delete the persistent custom configuration store.
src/nupic/swarming/hypersearch/support.py
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 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)
[ "Clear", "all", "custom", "configuration", "settings", "and", "delete", "the", "persistent", "custom", "configuration", "store", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/support.py#L601-L614
[ "def", "resetCustomConfig", "(", "cls", ")", ":", "_getLogger", "(", ")", ".", "info", "(", "\"Resetting all custom configuration properties; \"", "\"caller=%r\"", ",", "traceback", ".", "format_stack", "(", ")", ")", "# Clear the in-memory settings cache, forcing reload up...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_CustomConfigurationFileWrapper.clear
If persistent is True, delete the temporary file Parameters: ---------------------------------------------------------------- persistent: if True, custom configuration file is deleted
src/nupic/swarming/hypersearch/support.py
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 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
[ "If", "persistent", "is", "True", "delete", "the", "temporary", "file" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/support.py#L656-L672
[ "def", "clear", "(", "cls", ",", "persistent", "=", "False", ")", ":", "if", "persistent", ":", "try", ":", "os", ".", "unlink", "(", "cls", ".", "getPath", "(", ")", ")", "except", "OSError", ",", "e", ":", "if", "e", ".", "errno", "!=", "errno"...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_CustomConfigurationFileWrapper.getCustomDict
Returns a dict of all temporary values in custom configuration file
src/nupic/swarming/hypersearch/support.py
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 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
[ "Returns", "a", "dict", "of", "all", "temporary", "values", "in", "custom", "configuration", "file" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/support.py#L675-L690
[ "def", "getCustomDict", "(", "cls", ")", ":", "if", "not", "os", ".", "path", ".", "exists", "(", "cls", ".", "getPath", "(", ")", ")", ":", "return", "dict", "(", ")", "properties", "=", "Configuration", ".", "_readConfigFile", "(", "os", ".", "path...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_CustomConfigurationFileWrapper.edit
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)
src/nupic/swarming/hypersearch/support.py
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 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
[ "Edits", "the", "XML", "configuration", "file", "with", "the", "parameters", "specified", "by", "properties" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/support.py#L693-L772
[ "def", "edit", "(", "cls", ",", "properties", ")", ":", "copyOfProperties", "=", "copy", "(", "properties", ")", "configFilePath", "=", "cls", ".", "getPath", "(", ")", "try", ":", "with", "open", "(", "configFilePath", ",", "'r'", ")", "as", "fp", ":"...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_CustomConfigurationFileWrapper._setPath
Sets the path of the custom configuration file
src/nupic/swarming/hypersearch/support.py
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 _setPath(cls): """ Sets the path of the custom configuration file """ cls._path = os.path.join(os.environ['NTA_DYNAMIC_CONF_DIR'], cls.customFileName)
[ "Sets", "the", "path", "of", "the", "custom", "configuration", "file" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/support.py#L775-L779
[ "def", "_setPath", "(", "cls", ")", ":", "cls", ".", "_path", "=", "os", ".", "path", ".", "join", "(", "os", ".", "environ", "[", "'NTA_DYNAMIC_CONF_DIR'", "]", ",", "cls", ".", "customFileName", ")" ]
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
Particle.getState
Get the particle state as a dict. This is enough information to instantiate this particle on another worker.
src/nupic/swarming/hypersearch/particle.py
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 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)
[ "Get", "the", "particle", "state", "as", "a", "dict", ".", "This", "is", "enough", "information", "to", "instantiate", "this", "particle", "on", "another", "worker", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/particle.py#L258-L268
[ "def", "getState", "(", "self", ")", ":", "varStates", "=", "dict", "(", ")", "for", "varName", ",", "var", "in", "self", ".", "permuteVars", ".", "iteritems", "(", ")", ":", "varStates", "[", "varName", "]", "=", "var", ".", "getState", "(", ")", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
Particle.initStateFrom
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.
src/nupic/swarming/hypersearch/particle.py
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 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)
[ "Init", "all", "of", "our", "variable", "positions", "velocities", "and", "optionally", "the", "best", "result", "and", "best", "position", "from", "the", "given", "particle", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/particle.py#L270-L295
[ "def", "initStateFrom", "(", "self", ",", "particleId", ",", "particleState", ",", "newBest", ")", ":", "# Get the update best position and result?", "if", "newBest", ":", "(", "bestResult", ",", "bestPosition", ")", "=", "self", ".", "_resultsDB", ".", "getPartic...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
Particle.copyVarStatesFrom
Copy specific variables from particleState into this particle. Parameters: -------------------------------------------------------------- particleState: dict produced by a particle's getState() method varNames: which variables to copy
src/nupic/swarming/hypersearch/particle.py
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 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)
[ "Copy", "specific", "variables", "from", "particleState", "into", "this", "particle", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/particle.py#L332-L366
[ "def", "copyVarStatesFrom", "(", "self", ",", "particleState", ",", "varNames", ")", ":", "# 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...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
Particle.getPosition
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
src/nupic/swarming/hypersearch/particle.py
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 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
[ "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", ...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/particle.py#L368-L381
[ "def", "getPosition", "(", "self", ")", ":", "result", "=", "dict", "(", ")", "for", "(", "varName", ",", "value", ")", "in", "self", ".", "permuteVars", ".", "iteritems", "(", ")", ":", "result", "[", "varName", "]", "=", "value", ".", "getPosition"...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
Particle.getPositionFromState
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
src/nupic/swarming/hypersearch/particle.py
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 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
[ "Return", "the", "position", "of", "a", "particle", "given", "its", "state", "dict", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/particle.py#L384-L396
[ "def", "getPositionFromState", "(", "pState", ")", ":", "result", "=", "dict", "(", ")", "for", "(", "varName", ",", "value", ")", "in", "pState", "[", "'varStates'", "]", ".", "iteritems", "(", ")", ":", "result", "[", "varName", "]", "=", "value", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
Particle.agitate
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
src/nupic/swarming/hypersearch/particle.py
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 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()
[ "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", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/particle.py#L398-L410
[ "def", "agitate", "(", "self", ")", ":", "for", "(", "varName", ",", "var", ")", "in", "self", ".", "permuteVars", ".", "iteritems", "(", ")", ":", "var", ".", "agitate", "(", ")", "self", ".", "newPosition", "(", ")" ]
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
Particle.newPosition
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
src/nupic/swarming/hypersearch/particle.py
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 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
[ "Choose", "a", "new", "position", "based", "on", "results", "obtained", "so", "far", "from", "all", "other", "particles", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/particle.py#L412-L465
[ "def", "newPosition", "(", "self", ",", "whichVars", "=", "None", ")", ":", "# TODO: incorporate data from choice variables....", "# TODO: make sure we're calling this when appropriate.", "# Get the global best position for this swarm generation", "globalBestPosition", "=", "None", "...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
DiscreteProposal.propose
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).
src/nupic/math/proposal.py
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 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
[ "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"...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/math/proposal.py#L207-L222
[ "def", "propose", "(", "self", ",", "current", ",", "r", ")", ":", "stay", "=", "(", "r", ".", "uniform", "(", "0", ",", "1", ")", "<", "self", ".", "kernel", ")", "if", "stay", ":", "logKernel", "=", "numpy", ".", "log", "(", "self", ".", "k...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
PoissonProposal.propose
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).
src/nupic/math/proposal.py
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 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
[ "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", ...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/math/proposal.py#L262-L273
[ "def", "propose", "(", "self", ",", "current", ",", "r", ")", ":", "curLambda", "=", "current", "+", "self", ".", "offset", "x", ",", "logProb", "=", "PoissonDistribution", "(", "curLambda", ")", ".", "sample", "(", "r", ")", "logBackward", "=", "Poiss...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
ModelFactory.__getLogger
Get the logger for this object. :returns: (Logger) A Logger object.
src/nupic/frameworks/opf/model_factory.py
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 __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
[ "Get", "the", "logger", "for", "this", "object", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/frameworks/opf/model_factory.py#L46-L53
[ "def", "__getLogger", "(", "cls", ")", ":", "if", "cls", ".", "__logger", "is", "None", ":", "cls", ".", "__logger", "=", "opf_utils", ".", "initLogger", "(", "cls", ")", "return", "cls", ".", "__logger" ]
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
ModelFactory.create
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`
src/nupic/frameworks/opf/model_factory.py
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 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'])
[ "Create", "a", "new", "model", "instance", "given", "a", "description", "dictionary", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/frameworks/opf/model_factory.py#L57-L85
[ "def", "create", "(", "modelConfig", ",", "logLevel", "=", "logging", ".", "ERROR", ")", ":", "logger", "=", "ModelFactory", ".", "__getLogger", "(", ")", "logger", ".", "setLevel", "(", "logLevel", ")", "logger", ".", "debug", "(", "\"ModelFactory returning...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
ModelFactory.loadFromCheckpoint
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.
src/nupic/frameworks/opf/model_factory.py
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 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)
[ "Load", "saved", "model", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/frameworks/opf/model_factory.py#L88-L99
[ "def", "loadFromCheckpoint", "(", "savedModelDir", ",", "newSerialization", "=", "False", ")", ":", "if", "newSerialization", ":", "return", "HTMPredictionModel", ".", "readFromCheckpoint", "(", "savedModelDir", ")", "else", ":", "return", "Model", ".", "load", "(...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory.compute
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.
src/nupic/algorithms/temporal_memory.py
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 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)
[ "Perform", "one", "time", "step", "of", "the", "Temporal", "Memory", "algorithm", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L181-L195
[ "def", "compute", "(", "self", ",", "activeColumns", ",", "learn", "=", "True", ")", ":", "self", ".", "activateCells", "(", "sorted", "(", "activeColumns", ")", ",", "learn", ")", "self", ".", "activateDendrites", "(", "learn", ")" ]
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory.activateCells
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
src/nupic/algorithms/temporal_memory.py
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 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)
[ "Calculate", "the", "active", "cells", "using", "the", "current", "active", "columns", "and", "dendrite", "segments", ".", "Grow", "and", "reinforce", "synapses", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L198-L261
[ "def", "activateCells", "(", "self", ",", "activeColumns", ",", "learn", "=", "True", ")", ":", "prevActiveCells", "=", "self", ".", "activeCells", "prevWinnerCells", "=", "self", ".", "winnerCells", "self", ".", "activeCells", "=", "[", "]", "self", ".", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory.activateDendrites
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
src/nupic/algorithms/temporal_memory.py
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 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
[ "Calculate", "dendrite", "segment", "activity", "using", "the", "current", "active", "cells", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L264-L307
[ "def", "activateDendrites", "(", "self", ",", "learn", "=", "True", ")", ":", "(", "numActiveConnected", ",", "numActivePotential", ")", "=", "self", ".", "connections", ".", "computeActivity", "(", "self", ".", "activeCells", ",", "self", ".", "connectedPerma...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory.reset
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.
src/nupic/algorithms/temporal_memory.py
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 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 = []
[ "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", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L310-L318
[ "def", "reset", "(", "self", ")", ":", "self", ".", "activeCells", "=", "[", "]", "self", ".", "winnerCells", "=", "[", "]", "self", ".", "activeSegments", "=", "[", "]", "self", ".", "matchingSegments", "=", "[", "]" ]
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory.activatePredictedColumn
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.
src/nupic/algorithms/temporal_memory.py
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 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)
[ "Determines", "which", "cells", "in", "a", "predicted", "column", "should", "be", "added", "to", "winner", "cells", "list", "and", "learns", "on", "the", "segments", "that", "correctly", "predicted", "this", "column", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L327-L355
[ "def", "activatePredictedColumn", "(", "self", ",", "column", ",", "columnActiveSegments", ",", "columnMatchingSegments", ",", "prevActiveCells", ",", "prevWinnerCells", ",", "learn", ")", ":", "return", "self", ".", "_activatePredictedColumn", "(", "self", ".", "co...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory.punishPredictedColumn
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``.
src/nupic/algorithms/temporal_memory.py
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 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)
[ "Punishes", "the", "Segments", "that", "incorrectly", "predicted", "a", "column", "to", "be", "active", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L391-L412
[ "def", "punishPredictedColumn", "(", "self", ",", "column", ",", "columnActiveSegments", ",", "columnMatchingSegments", ",", "prevActiveCells", ",", "prevWinnerCells", ")", ":", "self", ".", "_punishPredictedColumn", "(", "self", ".", "connections", ",", "columnMatchi...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory.createSegment
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.
src/nupic/algorithms/temporal_memory.py
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 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)
[ "Create", "a", ":", "class", ":", "~nupic", ".", "algorithms", ".", "connections", ".", "Segment", "on", "the", "specified", "cell", ".", "This", "method", "calls", ":", "meth", ":", "~nupic", ".", "algorithms", ".", "connections", ".", "Connections", ".",...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L415-L431
[ "def", "createSegment", "(", "self", ",", "cell", ")", ":", "return", "self", ".", "_createSegment", "(", "self", ".", "connections", ",", "self", ".", "lastUsedIterationForSegment", ",", "cell", ",", "self", ".", "iteration", ",", "self", ".", "maxSegmentsP...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory._activatePredictedColumn
: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
src/nupic/algorithms/temporal_memory.py
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 _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
[ ":", "param", "connections", ":", "(", "Object", ")", "Connections", "for", "the", "TM", ".", "Gets", "mutated", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L448-L525
[ "def", "_activatePredictedColumn", "(", "cls", ",", "connections", ",", "random", ",", "columnActiveSegments", ",", "prevActiveCells", ",", "prevWinnerCells", ",", "numActivePotentialSynapsesForSegment", ",", "maxNewSynapseCount", ",", "initialPermanence", ",", "permanenceI...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory._burstColumn
: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
src/nupic/algorithms/temporal_memory.py
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 _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
[ ":", "param", "connections", ":", "(", "Object", ")", "Connections", "for", "the", "TM", ".", "Gets", "mutated", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L529-L635
[ "def", "_burstColumn", "(", "cls", ",", "connections", ",", "random", ",", "lastUsedIterationForSegment", ",", "column", ",", "columnMatchingSegments", ",", "prevActiveCells", ",", "prevWinnerCells", ",", "cellsForColumn", ",", "numActivePotentialSynapsesForSegment", ",",...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory._punishPredictedColumn
: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
src/nupic/algorithms/temporal_memory.py
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 _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)
[ ":", "param", "connections", ":", "(", "Object", ")", "Connections", "for", "the", "TM", ".", "Gets", "mutated", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L639-L661
[ "def", "_punishPredictedColumn", "(", "cls", ",", "connections", ",", "columnMatchingSegments", ",", "prevActiveCells", ",", "predictedSegmentDecrement", ")", ":", "if", "predictedSegmentDecrement", ">", "0.0", "and", "columnMatchingSegments", "is", "not", "None", ":", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory._createSegment
Create a segment on the connections, enforcing the maxSegmentsPerCell parameter.
src/nupic/algorithms/temporal_memory.py
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 _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
[ "Create", "a", "segment", "on", "the", "connections", "enforcing", "the", "maxSegmentsPerCell", "parameter", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L665-L692
[ "def", "_createSegment", "(", "cls", ",", "connections", ",", "lastUsedIterationForSegment", ",", "cell", ",", "iteration", ",", "maxSegmentsPerCell", ")", ":", "# Enforce maxSegmentsPerCell.", "while", "connections", ".", "numSegments", "(", "cell", ")", ">=", "max...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory._destroyMinPermanenceSynapses
Destroy nDestroy synapses on the specified segment, but don't destroy synapses to the "excludeCells".
src/nupic/algorithms/temporal_memory.py
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 _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)
[ "Destroy", "nDestroy", "synapses", "on", "the", "specified", "segment", "but", "don", "t", "destroy", "synapses", "to", "the", "excludeCells", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L696-L722
[ "def", "_destroyMinPermanenceSynapses", "(", "cls", ",", "connections", ",", "random", ",", "segment", ",", "nDestroy", ",", "excludeCells", ")", ":", "destroyCandidates", "=", "sorted", "(", "(", "synapse", "for", "synapse", "in", "connections", ".", "synapsesF...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory._leastUsedCell
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.
src/nupic/algorithms/temporal_memory.py
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 _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]
[ "Gets", "the", "cell", "with", "the", "smallest", "number", "of", "segments", ".", "Break", "ties", "randomly", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L726-L755
[ "def", "_leastUsedCell", "(", "cls", ",", "random", ",", "cells", ",", "connections", ")", ":", "leastUsedCells", "=", "[", "]", "minNumSegments", "=", "float", "(", "\"inf\"", ")", "for", "cell", "in", "cells", ":", "numSegments", "=", "connections", ".",...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory._growSynapses
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.
src/nupic/algorithms/temporal_memory.py
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 _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]
[ "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", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L759-L797
[ "def", "_growSynapses", "(", "cls", ",", "connections", ",", "random", ",", "segment", ",", "nDesiredNewSynapes", ",", "prevWinnerCells", ",", "initialPermanence", ",", "maxSynapsesPerSegment", ")", ":", "candidates", "=", "list", "(", "prevWinnerCells", ")", "for...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory._adaptSegment
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
src/nupic/algorithms/temporal_memory.py
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 _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)
[ "Updates", "synapses", "on", "segment", ".", "Strengthens", "active", "synapses", ";", "weakens", "inactive", "synapses", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L801-L837
[ "def", "_adaptSegment", "(", "cls", ",", "connections", ",", "segment", ",", "prevActiveCells", ",", "permanenceIncrement", ",", "permanenceDecrement", ")", ":", "# Destroying a synapse modifies the set that we're iterating through.", "synapsesToDestroy", "=", "[", "]", "fo...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory.columnForCell
Returns the index of the column that a cell belongs to. :param cell: (int) Cell index :returns: (int) Column index
src/nupic/algorithms/temporal_memory.py
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 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)
[ "Returns", "the", "index", "of", "the", "column", "that", "a", "cell", "belongs", "to", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L840-L850
[ "def", "columnForCell", "(", "self", ",", "cell", ")", ":", "self", ".", "_validateCell", "(", "cell", ")", "return", "int", "(", "cell", "/", "self", ".", "cellsPerColumn", ")" ]
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory.cellsForColumn
Returns the indices of cells that belong to a column. :param column: (int) Column index :returns: (list) Cell indices
src/nupic/algorithms/temporal_memory.py
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 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)
[ "Returns", "the", "indices", "of", "cells", "that", "belong", "to", "a", "column", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L853-L865
[ "def", "cellsForColumn", "(", "self", ",", "column", ")", ":", "self", ".", "_validateColumn", "(", "column", ")", "start", "=", "self", ".", "cellsPerColumn", "*", "column", "end", "=", "start", "+", "self", ".", "cellsPerColumn", "return", "range", "(", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory.mapCellsToColumns
Maps cells to the columns they belong to. :param cells: (set) Cells :returns: (dict) Mapping from columns to their cells in `cells`
src/nupic/algorithms/temporal_memory.py
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 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
[ "Maps", "cells", "to", "the", "columns", "they", "belong", "to", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L886-L900
[ "def", "mapCellsToColumns", "(", "self", ",", "cells", ")", ":", "cellsForColumns", "=", "defaultdict", "(", "set", ")", "for", "cell", "in", "cells", ":", "column", "=", "self", ".", "columnForCell", "(", "cell", ")", "cellsForColumns", "[", "column", "]"...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory.getPredictiveCells
Returns the indices of the predictive cells. :returns: (list) Indices of predictive cells.
src/nupic/algorithms/temporal_memory.py
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 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
[ "Returns", "the", "indices", "of", "the", "predictive", "cells", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L912-L924
[ "def", "getPredictiveCells", "(", "self", ")", ":", "previousCell", "=", "None", "predictiveCells", "=", "[", "]", "for", "segment", "in", "self", ".", "activeSegments", ":", "if", "segment", ".", "cell", "!=", "previousCell", ":", "predictiveCells", ".", "a...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory.write
Writes serialized data to proto object. :param proto: (DynamicStructBuilder) Proto object
src/nupic/algorithms/temporal_memory.py
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 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
[ "Writes", "serialized", "data", "to", "proto", "object", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L1134-L1197
[ "def", "write", "(", "self", ",", "proto", ")", ":", "# capnp fails to save a tuple. Let's force columnDimensions to list.", "proto", ".", "columnDimensions", "=", "list", "(", "self", ".", "columnDimensions", ")", "proto", ".", "cellsPerColumn", "=", "self", ".", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
TemporalMemory.read
Reads deserialized data from proto object. :param proto: (DynamicStructBuilder) Proto object :returns: (:class:TemporalMemory) TemporalMemory instance
src/nupic/algorithms/temporal_memory.py
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 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
[ "Reads", "deserialized", "data", "from", "proto", "object", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/temporal_memory.py#L1206-L1277
[ "def", "read", "(", "cls", ",", "proto", ")", ":", "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...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
SequenceMachine.generateFromNumbers
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
src/nupic/data/generators/sequence_machine.py
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 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
[ "Generate", "a", "sequence", "from", "a", "list", "of", "numbers", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/generators/sequence_machine.py#L50-L69
[ "def", "generateFromNumbers", "(", "self", ",", "numbers", ")", ":", "sequence", "=", "[", "]", "for", "number", "in", "numbers", ":", "if", "number", "==", "None", ":", "sequence", ".", "append", "(", "number", ")", "else", ":", "pattern", "=", "self"...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
SequenceMachine.addSpatialNoise
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
src/nupic/data/generators/sequence_machine.py
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 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
[ "Add", "spatial", "noise", "to", "each", "pattern", "in", "the", "sequence", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/generators/sequence_machine.py#L72-L88
[ "def", "addSpatialNoise", "(", "self", ",", "sequence", ",", "amount", ")", ":", "newSequence", "=", "[", "]", "for", "pattern", "in", "sequence", ":", "if", "pattern", "is", "not", "None", ":", "pattern", "=", "self", ".", "patternMachine", ".", "addNoi...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
SequenceMachine.prettyPrintSequence
Pretty print a sequence. @param sequence (list) Sequence @param verbosity (int) Verbosity level @return (string) Pretty-printed text
src/nupic/data/generators/sequence_machine.py
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 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
[ "Pretty", "print", "a", "sequence", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/generators/sequence_machine.py#L91-L113
[ "def", "prettyPrintSequence", "(", "self", ",", "sequence", ",", "verbosity", "=", "1", ")", ":", "text", "=", "\"\"", "for", "i", "in", "xrange", "(", "len", "(", "sequence", ")", ")", ":", "pattern", "=", "sequence", "[", "i", "]", "if", "pattern",...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
ROCCurve
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
src/nupic/math/roc_utils.py
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 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
[ "compute", "Receiver", "operating", "characteristic", "(", "ROC", ")" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/math/roc_utils.py#L43-L136
[ "def", "ROCCurve", "(", "y_true", ",", "y_score", ")", ":", "y_true", "=", "np", ".", "ravel", "(", "y_true", ")", "classes", "=", "np", ".", "unique", "(", "y_true", ")", "# ROC only for binary classification", "if", "classes", ".", "shape", "[", "0", "...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
AreaUnderCurve
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
src/nupic/math/roc_utils.py
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 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
[ "Compute", "Area", "Under", "the", "Curve", "(", "AUC", ")", "using", "the", "trapezoidal", "rule" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/math/roc_utils.py#L140-L183
[ "def", "AreaUnderCurve", "(", "x", ",", "y", ")", ":", "#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 und...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
MonitorMixinBase.mmPrettyPrintTraces
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.
src/nupic/algorithms/monitor_mixin/monitor_mixin_base.py
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 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")
[ "Returns", "pretty", "-", "printed", "table", "of", "traces", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/monitor_mixin/monitor_mixin_base.py#L124-L142
[ "def", "mmPrettyPrintTraces", "(", "traces", ",", "breakOnResets", "=", "None", ")", ":", "assert", "len", "(", "traces", ")", ">", "0", ",", "\"No traces found\"", "table", "=", "PrettyTable", "(", "[", "\"#\"", "]", "+", "[", "trace", ".", "prettyPrintTi...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
MonitorMixinBase.mmPrettyPrintMetrics
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.
src/nupic/algorithms/monitor_mixin/monitor_mixin_base.py
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 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")
[ "Returns", "pretty", "-", "printed", "table", "of", "metrics", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/monitor_mixin/monitor_mixin_base.py#L146-L162
[ "def", "mmPrettyPrintMetrics", "(", "metrics", ",", "sigFigs", "=", "5", ")", ":", "assert", "len", "(", "metrics", ")", ">", "0", ",", "\"No metrics found\"", "table", "=", "PrettyTable", "(", "[", "\"Metric\"", ",", "\"mean\"", ",", "\"standard deviation\"",...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
MonitorMixinBase.mmGetCellTracePlot
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
src/nupic/algorithms/monitor_mixin/monitor_mixin_base.py
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 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
[ "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", ")", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/monitor_mixin/monitor_mixin_base.py#L187-L227
[ "def", "mmGetCellTracePlot", "(", "self", ",", "cellTrace", ",", "cellCount", ",", "activityType", ",", "title", "=", "\"\"", ",", "showReset", "=", "False", ",", "resetShading", "=", "0.25", ")", ":", "plot", "=", "Plot", "(", "self", ",", "title", ")",...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
estimateAnomalyLikelihoods
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
src/nupic/algorithms/anomaly_likelihood.py
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 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)
[ "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", "estima...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/anomaly_likelihood.py#L405-L517
[ "def", "estimateAnomalyLikelihoods", "(", "anomalyScores", ",", "averagingWindow", "=", "10", ",", "skipRecords", "=", "0", ",", "verbosity", "=", "0", ")", ":", "if", "verbosity", ">", "1", ":", "print", "(", "\"In estimateAnomalyLikelihoods.\"", ")", "print", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
updateAnomalyLikelihoods
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.
src/nupic/algorithms/anomaly_likelihood.py
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 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)
[ "Compute", "updated", "probabilities", "for", "anomalyScores", "using", "the", "given", "params", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/anomaly_likelihood.py#L521-L610
[ "def", "updateAnomalyLikelihoods", "(", "anomalyScores", ",", "params", ",", "verbosity", "=", "0", ")", ":", "if", "verbosity", ">", "3", ":", "print", "(", "\"In updateAnomalyLikelihoods.\"", ")", "print", "(", "\"Number of anomaly scores:\"", ",", "len", "(", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_filterLikelihoods
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.
src/nupic/algorithms/anomaly_likelihood.py
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 _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
[ "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", "l...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/anomaly_likelihood.py#L614-L644
[ "def", "_filterLikelihoods", "(", "likelihoods", ",", "redThreshold", "=", "0.99999", ",", "yellowThreshold", "=", "0.999", ")", ":", "redThreshold", "=", "1.0", "-", "redThreshold", "yellowThreshold", "=", "1.0", "-", "yellowThreshold", "# The first value is untouche...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_anomalyScoreMovingAverage
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.
src/nupic/algorithms/anomaly_likelihood.py
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 _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
[ "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",...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/anomaly_likelihood.py#L648-L684
[ "def", "_anomalyScoreMovingAverage", "(", "anomalyScores", ",", "windowSize", "=", "10", ",", "verbosity", "=", "0", ",", ")", ":", "historicalValues", "=", "[", "]", "total", "=", "0.0", "averagedRecordList", "=", "[", "]", "# Aggregated records", "for", "rec...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
estimateNormal
: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``.
src/nupic/algorithms/anomaly_likelihood.py
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 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
[ ":", "param", "sampleData", ":", ":", "type", "sampleData", ":", "Numpy", "array", ".", ":", "param", "performLowerBoundCheck", ":", ":", "type", "performLowerBoundCheck", ":", "bool", ":", "returns", ":", "A", "dict", "containing", "the", "parameters", "of", ...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/anomaly_likelihood.py#L688-L720
[ "def", "estimateNormal", "(", "sampleData", ",", "performLowerBoundCheck", "=", "True", ")", ":", "params", "=", "{", "\"name\"", ":", "\"normal\"", ",", "\"mean\"", ":", "numpy", ".", "mean", "(", "sampleData", ")", ",", "\"variance\"", ":", "numpy", ".", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
tailProbability
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
src/nupic/algorithms/anomaly_likelihood.py
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 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)
[ "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", "...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/anomaly_likelihood.py#L742-L763
[ "def", "tailProbability", "(", "x", ",", "distributionParams", ")", ":", "if", "\"mean\"", "not", "in", "distributionParams", "or", "\"stdev\"", "not", "in", "distributionParams", ":", "raise", "RuntimeError", "(", "\"Insufficient parameters to specify the distribution.\"...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
isValidEstimatorParams
:returns: ``True`` if ``p`` is a valid estimator params as might be returned by ``estimateAnomalyLikelihoods()`` or ``updateAnomalyLikelihoods``, ``False`` otherwise. Just does some basic validation.
src/nupic/algorithms/anomaly_likelihood.py
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 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
[ ":", "returns", ":", "True", "if", "p", "is", "a", "valid", "estimator", "params", "as", "might", "be", "returned", "by", "estimateAnomalyLikelihoods", "()", "or", "updateAnomalyLikelihoods", "False", "otherwise", ".", "Just", "does", "some", "basic", "validatio...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/anomaly_likelihood.py#L767-L784
[ "def", "isValidEstimatorParams", "(", "p", ")", ":", "if", "not", "isinstance", "(", "p", ",", "dict", ")", ":", "return", "False", "if", "\"distribution\"", "not", "in", "p", ":", "return", "False", "if", "\"movingAverage\"", "not", "in", "p", ":", "ret...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
AnomalyLikelihood._calcSkipRecords
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'.
src/nupic/algorithms/anomaly_likelihood.py
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 _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))
[ "Return", "the", "value", "of", "skipRecords", "for", "passing", "to", "estimateAnomalyLikelihoods" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/anomaly_likelihood.py#L241-L258
[ "def", "_calcSkipRecords", "(", "numIngested", ",", "windowSize", ",", "learningPeriod", ")", ":", "numShiftedOut", "=", "max", "(", "0", ",", "numIngested", "-", "windowSize", ")", "return", "min", "(", "numIngested", ",", "max", "(", "0", ",", "learningPer...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
AnomalyLikelihood.read
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
src/nupic/algorithms/anomaly_likelihood.py
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 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
[ "capnp", "deserialization", "method", "for", "the", "anomaly", "likelihood", "object" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/anomaly_likelihood.py#L266-L309
[ "def", "read", "(", "cls", ",", "proto", ")", ":", "# pylint: disable=W0212", "anomalyLikelihood", "=", "object", ".", "__new__", "(", "cls", ")", "anomalyLikelihood", ".", "_iteration", "=", "proto", ".", "iteration", "anomalyLikelihood", ".", "_historicalScores"...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
AnomalyLikelihood.write
capnp serialization method for the anomaly likelihood object :param proto: (Object) capnp proto object specified in nupic.regions.anomaly_likelihood.capnp
src/nupic/algorithms/anomaly_likelihood.py
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 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
[ "capnp", "serialization", "method", "for", "the", "anomaly", "likelihood", "object" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/anomaly_likelihood.py#L312-L354
[ "def", "write", "(", "self", ",", "proto", ")", ":", "proto", ".", "iteration", "=", "self", ".", "_iteration", "pHistScores", "=", "proto", ".", "init", "(", "'historicalScores'", ",", "len", "(", "self", ".", "_historicalScores", ")", ")", "for", "i", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
AnomalyLikelihood.anomalyProbability
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.
src/nupic/algorithms/anomaly_likelihood.py
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 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
[ "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...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/algorithms/anomaly_likelihood.py#L357-L401
[ "def", "anomalyProbability", "(", "self", ",", "value", ",", "anomalyScore", ",", "timestamp", "=", "None", ")", ":", "if", "timestamp", "is", "None", ":", "timestamp", "=", "self", ".", "_iteration", "dataPoint", "=", "(", "timestamp", ",", "value", ",", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
IterationPhaseSpecLearnOnly._getImpl
Creates and returns the _IterationPhase-based instance corresponding to this phase specification model: Model instance
src/nupic/frameworks/opf/opf_task_driver.py
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 = _IterationPhaseLearnOnly(model=model, nIters=self.__nIters) return impl
[ "Creates", "and", "returns", "the", "_IterationPhase", "-", "based", "instance", "corresponding", "to", "this", "phase", "specification" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/frameworks/opf/opf_task_driver.py#L85-L93
[ "def", "_getImpl", "(", "self", ",", "model", ")", ":", "impl", "=", "_IterationPhaseLearnOnly", "(", "model", "=", "model", ",", "nIters", "=", "self", ".", "__nIters", ")", "return", "impl" ]
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
IterationPhaseSpecInferOnly._getImpl
Creates and returns the _IterationPhase-based instance corresponding to this phase specification model: Model instance
src/nupic/frameworks/opf/opf_task_driver.py
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 _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
[ "Creates", "and", "returns", "the", "_IterationPhase", "-", "based", "instance", "corresponding", "to", "this", "phase", "specification" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/frameworks/opf/opf_task_driver.py#L120-L129
[ "def", "_getImpl", "(", "self", ",", "model", ")", ":", "impl", "=", "_IterationPhaseInferOnly", "(", "model", "=", "model", ",", "nIters", "=", "self", ".", "__nIters", ",", "inferenceArgs", "=", "self", ".", "__inferenceArgs", ")", "return", "impl" ]
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
OPFTaskDriver.replaceIterationCycle
Replaces the Iteration Cycle phases :param phaseSpecs: Iteration cycle description consisting of a sequence of IterationPhaseSpecXXXXX elements that are performed in the given order
src/nupic/frameworks/opf/opf_task_driver.py
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 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
[ "Replaces", "the", "Iteration", "Cycle", "phases" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/frameworks/opf/opf_task_driver.py#L247-L262
[ "def", "replaceIterationCycle", "(", "self", ",", "phaseSpecs", ")", ":", "# -----------------------------------------------------------------------", "# Replace our phase manager", "#", "self", ".", "__phaseManager", "=", "_PhaseManager", "(", "model", "=", "self", ".", "_...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
OPFTaskDriver.handleInputRecord
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`
src/nupic/frameworks/opf/opf_task_driver.py
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 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
[ "Processes", "the", "given", "record", "according", "to", "the", "current", "iteration", "cycle", "phase" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/frameworks/opf/opf_task_driver.py#L288-L309
[ "def", "handleInputRecord", "(", "self", ",", "inputRecord", ")", ":", "assert", "inputRecord", ",", "\"Invalid inputRecord: %r\"", "%", "inputRecord", "results", "=", "self", ".", "__phaseManager", ".", "handleInputRecord", "(", "inputRecord", ")", "metrics", "=", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_PhaseManager.__advancePhase
Advance to the next iteration cycle phase
src/nupic/frameworks/opf/opf_task_driver.py
def __advancePhase(self): """ Advance to the next iteration cycle phase """ self.__currentPhase = self.__phaseCycler.next() self.__currentPhase.enterPhase() return
def __advancePhase(self): """ Advance to the next iteration cycle phase """ self.__currentPhase = self.__phaseCycler.next() self.__currentPhase.enterPhase() return
[ "Advance", "to", "the", "next", "iteration", "cycle", "phase" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/frameworks/opf/opf_task_driver.py#L363-L369
[ "def", "__advancePhase", "(", "self", ")", ":", "self", ".", "__currentPhase", "=", "self", ".", "__phaseCycler", ".", "next", "(", ")", "self", ".", "__currentPhase", ".", "enterPhase", "(", ")", "return" ]
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_PhaseManager.handleInputRecord
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
src/nupic/frameworks/opf/opf_task_driver.py
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 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
[ "Processes", "the", "given", "record", "according", "to", "the", "current", "phase" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/frameworks/opf/opf_task_driver.py#L372-L388
[ "def", "handleInputRecord", "(", "self", ",", "inputRecord", ")", ":", "results", "=", "self", ".", "__model", ".", "run", "(", "inputRecord", ")", "shouldContinue", "=", "self", ".", "__currentPhase", ".", "advance", "(", ")", "if", "not", "shouldContinue",...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_IterationPhase.enterPhase
Performs initialization that is necessary upon entry to the phase. Must be called before handleInputRecord() at the beginning of each phase
src/nupic/frameworks/opf/opf_task_driver.py
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 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()
[ "Performs", "initialization", "that", "is", "necessary", "upon", "entry", "to", "the", "phase", ".", "Must", "be", "called", "before", "handleInputRecord", "()", "at", "the", "beginning", "of", "each", "phase" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/frameworks/opf/opf_task_driver.py#L416-L425
[ "def", "enterPhase", "(", "self", ")", ":", "self", ".", "__iter", "=", "iter", "(", "xrange", "(", "self", ".", "__nIters", ")", ")", "# Prime the iterator", "self", ".", "__iter", ".", "next", "(", ")" ]
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_IterationPhase.advance
Advances the iteration; Returns: True if more iterations remain; False if this is the final iteration.
src/nupic/frameworks/opf/opf_task_driver.py
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 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
[ "Advances", "the", "iteration", ";" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/frameworks/opf/opf_task_driver.py#L428-L441
[ "def", "advance", "(", "self", ")", ":", "hasMore", "=", "True", "try", ":", "self", ".", "__iter", ".", "next", "(", ")", "except", "StopIteration", ":", "self", ".", "__iter", "=", "None", "hasMore", "=", "False", "return", "hasMore" ]
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_IterationPhaseLearnOnly.enterPhase
[_IterationPhase method implementation] Performs initialization that is necessary upon entry to the phase. Must be called before handleInputRecord() at the beginning of each phase
src/nupic/frameworks/opf/opf_task_driver.py
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(_IterationPhaseLearnOnly, self).enterPhase() self.__model.enableLearning() self.__model.disableInference() return
[ "[", "_IterationPhase", "method", "implementation", "]", "Performs", "initialization", "that", "is", "necessary", "upon", "entry", "to", "the", "phase", ".", "Must", "be", "called", "before", "handleInputRecord", "()", "at", "the", "beginning", "of", "each", "ph...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/frameworks/opf/opf_task_driver.py#L459-L467
[ "def", "enterPhase", "(", "self", ")", ":", "super", "(", "_IterationPhaseLearnOnly", ",", "self", ")", ".", "enterPhase", "(", ")", "self", ".", "__model", ".", "enableLearning", "(", ")", "self", ".", "__model", ".", "disableInference", "(", ")", "return...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_IterationPhaseInferCommon.enterPhase
[_IterationPhase method implementation] Performs initialization that is necessary upon entry to the phase. Must be called before handleInputRecord() at the beginning of each phase
src/nupic/frameworks/opf/opf_task_driver.py
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 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
[ "[", "_IterationPhase", "method", "implementation", "]", "Performs", "initialization", "that", "is", "necessary", "upon", "entry", "to", "the", "phase", ".", "Must", "be", "called", "before", "handleInputRecord", "()", "at", "the", "beginning", "of", "each", "ph...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/frameworks/opf/opf_task_driver.py#L489-L496
[ "def", "enterPhase", "(", "self", ")", ":", "super", "(", "_IterationPhaseInferCommon", ",", "self", ")", ".", "enterPhase", "(", ")", "self", ".", "_model", ".", "enableInference", "(", "inferenceArgs", "=", "self", ".", "_inferenceArgs", ")", "return" ]
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
PreviousValueModel.write
Serialize via capnp :param proto: capnp PreviousValueModelProto message builder
src/nupic/frameworks/opf/previous_value_model.py
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 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
[ "Serialize", "via", "capnp" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/frameworks/opf/previous_value_model.py#L138-L149
[ "def", "write", "(", "self", ",", "proto", ")", ":", "super", "(", "PreviousValueModel", ",", "self", ")", ".", "writeBaseToProto", "(", "proto", ".", "modelBase", ")", "proto", ".", "fieldNames", "=", "self", ".", "_fieldNames", "proto", ".", "fieldTypes"...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
PreviousValueModel.read
Deserialize via capnp :param proto: capnp PreviousValueModelProto message reader :returns: new instance of PreviousValueModel deserialized from the given proto
src/nupic/frameworks/opf/previous_value_model.py
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 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
[ "Deserialize", "via", "capnp" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/frameworks/opf/previous_value_model.py#L153-L173
[ "def", "read", "(", "cls", ",", "proto", ")", ":", "instance", "=", "object", ".", "__new__", "(", "cls", ")", "super", "(", "PreviousValueModel", ",", "instance", ")", ".", "__init__", "(", "proto", "=", "proto", ".", "modelBase", ")", "instance", "."...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
lscsum
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.
src/nupic/math/logarithms.py
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 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
[ "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", ...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/math/logarithms.py#L28-L67
[ "def", "lscsum", "(", "lx", ",", "epsilon", "=", "None", ")", ":", "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 retur...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
lscsum0
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.
src/nupic/math/logarithms.py
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 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
[ "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", ...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/math/logarithms.py#L70-L106
[ "def", "lscsum0", "(", "lx", ")", ":", "# 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", ")...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
normalize
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.
src/nupic/math/logarithms.py
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 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
[ "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", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/math/logarithms.py#L109-L123
[ "def", "normalize", "(", "lx", ")", ":", "lx", "=", "numpy", ".", "asarray", "(", "lx", ")", "base", "=", "lx", ".", "max", "(", ")", "x", "=", "numpy", ".", "exp", "(", "lx", "-", "base", ")", "result", "=", "x", "/", "x", ".", "sum", "(",...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
nsum0
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.
src/nupic/math/logarithms.py
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 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
[ "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", "lar...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/math/logarithms.py#L126-L141
[ "def", "nsum0", "(", "lx", ")", ":", "lx", "=", "numpy", ".", "asarray", "(", "lx", ")", "base", "=", "lx", ".", "max", "(", ")", "x", "=", "numpy", ".", "exp", "(", "lx", "-", "base", ")", "ssum", "=", "x", ".", "sum", "(", "0", ")", "re...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
logSumExp
returns log(exp(A) + exp(B)). A and B are numpy arrays
src/nupic/math/logarithms.py
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 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
[ "returns", "log", "(", "exp", "(", "A", ")", "+", "exp", "(", "B", "))", ".", "A", "and", "B", "are", "numpy", "arrays" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/math/logarithms.py#L164-L175
[ "def", "logSumExp", "(", "A", ",", "B", ",", "out", "=", "None", ")", ":", "if", "out", "is", "None", ":", "out", "=", "numpy", ".", "zeros", "(", "A", ".", "shape", ")", "indicator1", "=", "A", ">=", "B", "indicator2", "=", "numpy", ".", "logi...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
logDiffExp
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
src/nupic/math/logarithms.py
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 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
[ "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" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/math/logarithms.py#L177-L188
[ "def", "logDiffExp", "(", "A", ",", "B", ",", "out", "=", "None", ")", ":", "if", "out", "is", "None", ":", "out", "=", "numpy", ".", "zeros", "(", "A", ".", "shape", ")", "indicator1", "=", "A", ">=", "B", "assert", "indicator1", ".", "all", "...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_getFieldIndexBySpecial
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
src/nupic/data/record_stream.py
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 _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
[ "Return", "index", "of", "the", "field", "matching", "the", "field", "meta", "special", "value", ".", ":", "param", "fields", ":", "sequence", "of", "nupic", ".", "data", ".", "fieldmeta", ".", "FieldMetaInfo", "objects", "representing", "the", "fields", "of...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/record_stream.py#L32-L44
[ "def", "_getFieldIndexBySpecial", "(", "fields", ",", "special", ")", ":", "for", "i", ",", "field", "in", "enumerate", "(", "fields", ")", ":", "if", "field", ".", "special", "==", "special", ":", "return", "i", "return", "None" ]
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
ModelRecordEncoder.encode
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
src/nupic/data/record_stream.py
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 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
[ "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",...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/record_stream.py#L104-L174
[ "def", "encode", "(", "self", ",", "inputRow", ")", ":", "# Create the return dict", "result", "=", "dict", "(", "zip", "(", "self", ".", "_fieldNames", ",", "inputRow", ")", ")", "# Add in the special fields", "if", "self", ".", "_categoryFieldIndex", "is", "...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
ModelRecordEncoder._computeTimestampRecordIdx
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
src/nupic/data/record_stream.py
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 _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
[ "Give", "the", "timestamp", "of", "a", "record", "(", "a", "datetime", "object", ")", "compute", "the", "record", "s", "timestamp", "index", "-", "this", "is", "the", "timestamp", "divided", "by", "the", "aggregation", "period", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/record_stream.py#L177-L210
[ "def", "_computeTimestampRecordIdx", "(", "self", ",", "recordTS", ")", ":", "if", "self", ".", "_aggregationPeriod", "is", "None", ":", "return", "None", "# Base record index on number of elapsed months if aggregation is in", "# months", "if", "self", ".", "_aggregation...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
RecordStreamIface.getNextRecordDict
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)
src/nupic/data/record_stream.py
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 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)
[ "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", ":" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/record_stream.py#L252-L274
[ "def", "getNextRecordDict", "(", "self", ")", ":", "values", "=", "self", ".", "getNextRecord", "(", ")", "if", "values", "is", "None", ":", "return", "None", "if", "not", "values", ":", "return", "dict", "(", ")", "if", "self", ".", "_modelRecordEncoder...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
RecordStreamIface.getFieldMin
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``.
src/nupic/data/record_stream.py
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 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]
[ "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", "...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/record_stream.py#L361-L377
[ "def", "getFieldMin", "(", "self", ",", "fieldName", ")", ":", "stats", "=", "self", ".", "getStats", "(", ")", "if", "stats", "==", "None", ":", "return", "None", "minValues", "=", "stats", ".", "get", "(", "'min'", ",", "None", ")", "if", "minValue...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
RecordStreamIface.getFieldMax
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``.
src/nupic/data/record_stream.py
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 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]
[ "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", "...
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/record_stream.py#L380-L396
[ "def", "getFieldMax", "(", "self", ",", "fieldName", ")", ":", "stats", "=", "self", ".", "getStats", "(", ")", "if", "stats", "==", "None", ":", "return", "None", "maxValues", "=", "stats", ".", "get", "(", "'max'", ",", "None", ")", "if", "maxValue...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
ExtendedLogger.debug
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)
src/nupic/swarming/hypersearch/extended_logger.py
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 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)
[ "Log", "msg", "%", "args", "with", "severity", "DEBUG", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/extended_logger.py#L47-L56
[ "def", "debug", "(", "self", ",", "msg", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "self", ".", "_baseLogger", ".", "debug", "(", "self", ",", "self", ".", "getExtendedMsg", "(", "msg", ")", ",", "*", "args", ",", "*", "*", "kwargs", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
ExtendedLogger.info
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)
src/nupic/swarming/hypersearch/extended_logger.py
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 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)
[ "Log", "msg", "%", "args", "with", "severity", "INFO", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/extended_logger.py#L59-L68
[ "def", "info", "(", "self", ",", "msg", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "self", ".", "_baseLogger", ".", "info", "(", "self", ",", "self", ".", "getExtendedMsg", "(", "msg", ")", ",", "*", "args", ",", "*", "*", "kwargs", "...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
ExtendedLogger.warning
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)
src/nupic/swarming/hypersearch/extended_logger.py
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 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)
[ "Log", "msg", "%", "args", "with", "severity", "WARNING", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/extended_logger.py#L71-L80
[ "def", "warning", "(", "self", ",", "msg", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "self", ".", "_baseLogger", ".", "warning", "(", "self", ",", "self", ".", "getExtendedMsg", "(", "msg", ")", ",", "*", "args", ",", "*", "*", "kwargs...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
ExtendedLogger.error
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)
src/nupic/swarming/hypersearch/extended_logger.py
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 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)
[ "Log", "msg", "%", "args", "with", "severity", "ERROR", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/extended_logger.py#L85-L94
[ "def", "error", "(", "self", ",", "msg", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "self", ".", "_baseLogger", ".", "error", "(", "self", ",", "self", ".", "getExtendedMsg", "(", "msg", ")", ",", "*", "args", ",", "*", "*", "kwargs", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
ExtendedLogger.critical
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)
src/nupic/swarming/hypersearch/extended_logger.py
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 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)
[ "Log", "msg", "%", "args", "with", "severity", "CRITICAL", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/extended_logger.py#L97-L106
[ "def", "critical", "(", "self", ",", "msg", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "self", ".", "_baseLogger", ".", "critical", "(", "self", ",", "self", ".", "getExtendedMsg", "(", "msg", ")", ",", "*", "args", ",", "*", "*", "kwar...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
ExtendedLogger.log
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)
src/nupic/swarming/hypersearch/extended_logger.py
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 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)
[ "Log", "msg", "%", "args", "with", "the", "integer", "severity", "level", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/swarming/hypersearch/extended_logger.py#L111-L121
[ "def", "log", "(", "self", ",", "level", ",", "msg", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "self", ".", "_baseLogger", ".", "log", "(", "self", ",", "level", ",", "self", ".", "getExtendedMsg", "(", "msg", ")", ",", "*", "args", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
initFilter
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'
src/nupic/data/aggregator.py
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 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)
[ "Initializes", "internal", "filter", "variables", "for", "further", "processing", ".", "Returns", "a", "tuple", "(", "function", "to", "call", "parameters", "for", "the", "filter", "call", ")" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/aggregator.py#L51-L110
[ "def", "initFilter", "(", "input", ",", "filterInfo", "=", "None", ")", ":", "if", "filterInfo", "is", "None", ":", "return", "None", "# Build an array of index/func to call on record[index]", "filterList", "=", "[", "]", "for", "i", ",", "fieldName", "in", "enu...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_filterRecord
Takes a record and returns true if record meets filter criteria, false otherwise
src/nupic/data/aggregator.py
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 _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
[ "Takes", "a", "record", "and", "returns", "true", "if", "record", "meets", "filter", "criteria", "false", "otherwise" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/aggregator.py#L114-L129
[ "def", "_filterRecord", "(", "filterList", ",", "record", ")", ":", "for", "(", "fieldIdx", ",", "fp", ",", "params", ")", "in", "filterList", ":", "x", "=", "dict", "(", ")", "x", "[", "'value'", "]", "=", "record", "[", "fieldIdx", "]", "x", "[",...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_aggr_sum
Returns sum of the elements in the list. Missing items are replaced with the mean value
src/nupic/data/aggregator.py
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_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
[ "Returns", "sum", "of", "the", "elements", "in", "the", "list", ".", "Missing", "items", "are", "replaced", "with", "the", "mean", "value" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/aggregator.py#L153-L168
[ "def", "_aggr_sum", "(", "inList", ")", ":", "aggrMean", "=", "_aggr_mean", "(", "inList", ")", "if", "aggrMean", "==", "None", ":", "return", "None", "aggrSum", "=", "0", "for", "elem", "in", "inList", ":", "if", "elem", "!=", "SENTINEL_VALUE_FOR_MISSING_...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_aggr_mean
Returns mean of non-None elements of the list
src/nupic/data/aggregator.py
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_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
[ "Returns", "mean", "of", "non", "-", "None", "elements", "of", "the", "list" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/aggregator.py#L172-L184
[ "def", "_aggr_mean", "(", "inList", ")", ":", "aggrSum", "=", "0", "nonNone", "=", "0", "for", "elem", "in", "inList", ":", "if", "elem", "!=", "SENTINEL_VALUE_FOR_MISSING_DATA", ":", "aggrSum", "+=", "elem", "nonNone", "+=", "1", "if", "nonNone", "!=", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_aggr_mode
Returns most common value seen in the non-None elements of the list
src/nupic/data/aggregator.py
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_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]
[ "Returns", "most", "common", "value", "seen", "in", "the", "non", "-", "None", "elements", "of", "the", "list" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/aggregator.py#L188-L212
[ "def", "_aggr_mode", "(", "inList", ")", ":", "valueCounts", "=", "dict", "(", ")", "nonNone", "=", "0", "for", "elem", "in", "inList", ":", "if", "elem", "==", "SENTINEL_VALUE_FOR_MISSING_DATA", ":", "continue", "nonNone", "+=", "1", "if", "elem", "in", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
_aggr_weighted_mean
Weighted mean uses params (must be the same size as inList) and makes weighed mean of inList
src/nupic/data/aggregator.py
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 _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
[ "Weighted", "mean", "uses", "params", "(", "must", "be", "the", "same", "size", "as", "inList", ")", "and", "makes", "weighed", "mean", "of", "inList" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/aggregator.py#L216-L230
[ "def", "_aggr_weighted_mean", "(", "inList", ",", "params", ")", ":", "assert", "(", "len", "(", "inList", ")", "==", "len", "(", "params", ")", ")", "# If all weights are 0, then the value is not defined, return None (missing)", "weightsSum", "=", "sum", "(", "para...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
generateDataset
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.
src/nupic/data/aggregator.py
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 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
[ "Generate", "a", "dataset", "of", "aggregated", "values" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/aggregator.py#L720-L834
[ "def", "generateDataset", "(", "aggregationInfo", ",", "inputFilename", ",", "outputFilename", "=", "None", ")", ":", "# Create the input stream", "inputFullPath", "=", "resource_filename", "(", "\"nupic.datafiles\"", ",", "inputFilename", ")", "inputObj", "=", "FileRec...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
getFilename
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)
src/nupic/data/aggregator.py
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 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
[ "Generate", "the", "filename", "for", "aggregated", "dataset" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/aggregator.py#L838-L867
[ "def", "getFilename", "(", "aggregationInfo", ",", "inputFile", ")", ":", "# Find the actual file, with an absolute path", "inputFile", "=", "resource_filename", "(", "\"nupic.datafiles\"", ",", "inputFile", ")", "a", "=", "defaultdict", "(", "lambda", ":", "0", ",", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
Aggregator._getEndTime
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.
src/nupic/data/aggregator.py
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 _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)
[ "Add", "the", "aggregation", "period", "to", "the", "input", "time", "t", "and", "return", "a", "datetime", "object" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/aggregator.py#L426-L444
[ "def", "_getEndTime", "(", "self", ",", "t", ")", ":", "assert", "isinstance", "(", "t", ",", "datetime", ".", "datetime", ")", "if", "self", ".", "_aggTimeDelta", ":", "return", "t", "+", "self", ".", "_aggTimeDelta", "else", ":", "year", "=", "t", ...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
Aggregator._getFuncPtrAndParams
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)
src/nupic/data/aggregator.py
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 _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)
[ "Given", "the", "name", "of", "an", "aggregation", "function", "returns", "the", "function", "pointer", "and", "param", "." ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/aggregator.py#L447-L480
[ "def", "_getFuncPtrAndParams", "(", "self", ",", "funcName", ")", ":", "params", "=", "None", "if", "isinstance", "(", "funcName", ",", "basestring", ")", ":", "if", "funcName", "==", "'sum'", ":", "fp", "=", "_aggr_sum", "elif", "funcName", "==", "'first'...
5922fafffdccc8812e72b3324965ad2f7d4bbdad
valid
Aggregator._createAggregateRecord
Generate the aggregated output record Parameters: ------------------------------------------------------------------------ retval: outputRecord
src/nupic/data/aggregator.py
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
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
[ "Generate", "the", "aggregated", "output", "record" ]
numenta/nupic
python
https://github.com/numenta/nupic/blob/5922fafffdccc8812e72b3324965ad2f7d4bbdad/src/nupic/data/aggregator.py#L483-L505
[ "def", "_createAggregateRecord", "(", "self", ")", ":", "record", "=", "[", "]", "for", "i", ",", "(", "fieldIdx", ",", "aggFP", ",", "paramIdx", ")", "in", "enumerate", "(", "self", ".", "_fields", ")", ":", "if", "aggFP", "is", "None", ":", "# this...
5922fafffdccc8812e72b3324965ad2f7d4bbdad