Search is not available for this dataset
text
stringlengths
75
104k
def _finalize(self): """Run final activities after a model has run. These include recording and logging the final score""" self._logger.info( "Finished: modelID=%r; %r records processed. Performing final activities", self._modelID, self._currentRecordIndex + 1) # ========================================================================= # Dump the experiment metrics at the end of the task # ========================================================================= self._updateModelDBResults() # ========================================================================= # Check if the current model is the best. Create a milestone if necessary # If the model has been killed, it is not a candidate for "best model", # and its output cache should be destroyed # ========================================================================= if not self._isKilled: self.__updateJobResults() else: self.__deleteOutputCache(self._modelID) # ========================================================================= # Close output stream, if necessary # ========================================================================= if self._predictionLogger: self._predictionLogger.close() # ========================================================================= # Close input stream, if necessary # ========================================================================= if self._inputSource: self._inputSource.close()
def __createModelCheckpoint(self): """ Create a checkpoint from the current model, and store it in a dir named after checkpoint GUID, and finally store the GUID in the Models DB """ if self._model is None or self._modelCheckpointGUID is None: return # Create an output store, if one doesn't exist already if self._predictionLogger is None: self._createPredictionLogger() predictions = StringIO.StringIO() self._predictionLogger.checkpoint( checkpointSink=predictions, maxRows=int(Configuration.get('nupic.model.checkpoint.maxPredictionRows'))) self._model.save(os.path.join(self._experimentDir, str(self._modelCheckpointGUID))) self._jobsDAO.modelSetFields(modelID, {'modelCheckpointId':str(self._modelCheckpointGUID)}, ignoreUnchanged=True) self._logger.info("Checkpointed Hypersearch Model: modelID: %r, " "checkpointID: %r", self._modelID, checkpointID) return
def __deleteModelCheckpoint(self, modelID): """ Delete the stored checkpoint for the specified modelID. This function is called if the current model is now the best model, making the old model's checkpoint obsolete Parameters: ----------------------------------------------------------------------- modelID: The modelID for the checkpoint to delete. This is NOT the unique checkpointID """ checkpointID = \ self._jobsDAO.modelsGetFields(modelID, ['modelCheckpointId'])[0] if checkpointID is None: return try: shutil.rmtree(os.path.join(self._experimentDir, str(self._modelCheckpointGUID))) except: self._logger.warn("Failed to delete model checkpoint %s. "\ "Assuming that another worker has already deleted it", checkpointID) return self._jobsDAO.modelSetFields(modelID, {'modelCheckpointId':None}, ignoreUnchanged=True) return
def _createPredictionLogger(self): """ Creates the model's PredictionLogger object, which is an interface to write model results to a permanent storage location """ # Write results to a file self._predictionLogger = BasicPredictionLogger( fields=self._model.getFieldInfo(), experimentDir=self._experimentDir, label = "hypersearch-worker", inferenceType=self._model.getInferenceType()) if self.__loggedMetricPatterns: metricLabels = self.__metricMgr.getMetricLabels() loggedMetrics = matchPatterns(self.__loggedMetricPatterns, metricLabels) self._predictionLogger.setLoggedMetrics(loggedMetrics)
def __getOptimizedMetricLabel(self): """ Get the label for the metric being optimized. This function also caches the label in the instance variable self._optimizedMetricLabel Parameters: ----------------------------------------------------------------------- metricLabels: A sequence of all the labels being computed for this model Returns: The label for the metric being optmized over """ matchingKeys = matchPatterns([self._optimizeKeyPattern], self._getMetricLabels()) if len(matchingKeys) == 0: raise Exception("None of the generated metrics match the specified " "optimization pattern: %s. Available metrics are %s" % \ (self._optimizeKeyPattern, self._getMetricLabels())) elif len(matchingKeys) > 1: raise Exception("The specified optimization pattern '%s' matches more " "than one metric: %s" % (self._optimizeKeyPattern, matchingKeys)) return matchingKeys[0]
def _getFieldStats(self): """ Method which returns a dictionary of field statistics received from the input source. Returns: fieldStats: dict of dicts where the first level is the field name and the second level is the statistic. ie. fieldStats['pounds']['min'] """ fieldStats = dict() fieldNames = self._inputSource.getFieldNames() for field in fieldNames: curStats = dict() curStats['min'] = self._inputSource.getFieldMin(field) curStats['max'] = self._inputSource.getFieldMax(field) fieldStats[field] = curStats return fieldStats
def _updateModelDBResults(self): """ Retrieves the current results and updates the model's record in the Model database. """ # ----------------------------------------------------------------------- # Get metrics metrics = self._getMetrics() # ----------------------------------------------------------------------- # Extract report metrics that match the requested report REs reportDict = dict([(k,metrics[k]) for k in self._reportMetricLabels]) # ----------------------------------------------------------------------- # Extract the report item that matches the optimize key RE # TODO cache optimizedMetricLabel sooner metrics = self._getMetrics() optimizeDict = dict() if self._optimizeKeyPattern is not None: optimizeDict[self._optimizedMetricLabel] = \ metrics[self._optimizedMetricLabel] # ----------------------------------------------------------------------- # Update model results results = json.dumps((metrics , optimizeDict)) self._jobsDAO.modelUpdateResults(self._modelID, results=results, metricValue=optimizeDict.values()[0], numRecords=(self._currentRecordIndex + 1)) self._logger.debug( "Model Results: modelID=%s; numRecords=%s; results=%s" % \ (self._modelID, self._currentRecordIndex + 1, results)) return
def __updateJobResultsPeriodic(self): """ Periodic check to see if this is the best model. This should only have an effect if this is the *first* model to report its progress """ if self._isBestModelStored and not self._isBestModel: return while True: jobResultsStr = self._jobsDAO.jobGetFields(self._jobID, ['results'])[0] if jobResultsStr is None: jobResults = {} else: self._isBestModelStored = True if not self._isBestModel: return jobResults = json.loads(jobResultsStr) bestModel = jobResults.get('bestModel', None) bestMetric = jobResults.get('bestValue', None) isSaved = jobResults.get('saved', False) # If there is a best model, and it is not the same as the current model # we should wait till we have processed all of our records to see if # we are the the best if (bestModel is not None) and (self._modelID != bestModel): self._isBestModel = False return # Make sure prediction output stream is ready before we present our model # as "bestModel"; sometimes this takes a long time, so update the model's # timestamp to help avoid getting orphaned self.__flushPredictionCache() self._jobsDAO.modelUpdateTimestamp(self._modelID) metrics = self._getMetrics() jobResults['bestModel'] = self._modelID jobResults['bestValue'] = metrics[self._optimizedMetricLabel] jobResults['metrics'] = metrics jobResults['saved'] = False newResults = json.dumps(jobResults) isUpdated = self._jobsDAO.jobSetFieldIfEqual(self._jobID, fieldName='results', curValue=jobResultsStr, newValue=newResults) if isUpdated or (not isUpdated and newResults==jobResultsStr): self._isBestModel = True break
def __checkIfBestCompletedModel(self): """ Reads the current "best model" for the job and returns whether or not the current model is better than the "best model" stored for the job Returns: (isBetter, storedBest, origResultsStr) isBetter: True if the current model is better than the stored "best model" storedResults: A dict of the currently stored results in the jobs table record origResultsStr: The json-encoded string that currently resides in the "results" field of the jobs record (used to create atomicity) """ jobResultsStr = self._jobsDAO.jobGetFields(self._jobID, ['results'])[0] if jobResultsStr is None: jobResults = {} else: jobResults = json.loads(jobResultsStr) isSaved = jobResults.get('saved', False) bestMetric = jobResults.get('bestValue', None) currentMetric = self._getMetrics()[self._optimizedMetricLabel] self._isBestModel = (not isSaved) \ or (currentMetric < bestMetric) return self._isBestModel, jobResults, jobResultsStr
def __updateJobResults(self): """" Check if this is the best model If so: 1) Write it's checkpoint 2) Record this model as the best 3) Delete the previous best's output cache Otherwise: 1) Delete our output cache """ isSaved = False while True: self._isBestModel, jobResults, jobResultsStr = \ self.__checkIfBestCompletedModel() # ----------------------------------------------------------------------- # If the current model is the best: # 1) Save the model's predictions # 2) Checkpoint the model state # 3) Update the results for the job if self._isBestModel: # Save the current model and its results if not isSaved: self.__flushPredictionCache() self._jobsDAO.modelUpdateTimestamp(self._modelID) self.__createModelCheckpoint() self._jobsDAO.modelUpdateTimestamp(self._modelID) isSaved = True # Now record the model as the best for the job prevBest = jobResults.get('bestModel', None) prevWasSaved = jobResults.get('saved', False) # If the current model is the best, it shouldn't already be checkpointed if prevBest == self._modelID: assert not prevWasSaved metrics = self._getMetrics() jobResults['bestModel'] = self._modelID jobResults['bestValue'] = metrics[self._optimizedMetricLabel] jobResults['metrics'] = metrics jobResults['saved'] = True isUpdated = self._jobsDAO.jobSetFieldIfEqual(self._jobID, fieldName='results', curValue=jobResultsStr, newValue=json.dumps(jobResults)) if isUpdated: if prevWasSaved: self.__deleteOutputCache(prevBest) self._jobsDAO.modelUpdateTimestamp(self._modelID) self.__deleteModelCheckpoint(prevBest) self._jobsDAO.modelUpdateTimestamp(self._modelID) self._logger.info("Model %d chosen as best model", self._modelID) break # ----------------------------------------------------------------------- # If the current model is not the best, delete its outputs else: # NOTE: we update model timestamp around these occasionally-lengthy # operations to help prevent the model from becoming orphaned self.__deleteOutputCache(self._modelID) self._jobsDAO.modelUpdateTimestamp(self._modelID) self.__deleteModelCheckpoint(self._modelID) self._jobsDAO.modelUpdateTimestamp(self._modelID) break
def _writePrediction(self, result): """ Writes the results of one iteration of a model. The results are written to this ModelRunner's in-memory cache unless this model is the "best model" for the job. If this model is the "best model", the predictions are written out to a permanent store via a prediction output stream instance Parameters: ----------------------------------------------------------------------- result: A opf_utils.ModelResult object, which contains the input and output for this iteration """ self.__predictionCache.append(result) if self._isBestModel: self.__flushPredictionCache()
def __flushPredictionCache(self): """ Writes the contents of this model's in-memory prediction cache to a permanent store via the prediction output stream instance """ if not self.__predictionCache: return # Create an output store, if one doesn't exist already if self._predictionLogger is None: self._createPredictionLogger() startTime = time.time() self._predictionLogger.writeRecords(self.__predictionCache, progressCB=self.__writeRecordsCallback) self._logger.info("Flushed prediction cache; numrows=%s; elapsed=%s sec.", len(self.__predictionCache), time.time() - startTime) self.__predictionCache.clear()
def __deleteOutputCache(self, modelID): """ Delete's the output cache associated with the given modelID. This actually clears up the resources associated with the cache, rather than deleting al the records in the cache Parameters: ----------------------------------------------------------------------- modelID: The id of the model whose output cache is being deleted """ # If this is our output, we should close the connection if modelID == self._modelID and self._predictionLogger is not None: self._predictionLogger.close() del self.__predictionCache self._predictionLogger = None self.__predictionCache = None
def _initPeriodicActivities(self): """ Creates and returns a PeriodicActivityMgr instance initialized with our periodic activities Parameters: ------------------------------------------------------------------------- retval: a PeriodicActivityMgr instance """ # Activity to update the metrics for this model # in the models table updateModelDBResults = PeriodicActivityRequest(repeating=True, period=100, cb=self._updateModelDBResults) updateJobResults = PeriodicActivityRequest(repeating=True, period=100, cb=self.__updateJobResultsPeriodic) checkCancelation = PeriodicActivityRequest(repeating=True, period=50, cb=self.__checkCancelation) checkMaturity = PeriodicActivityRequest(repeating=True, period=10, cb=self.__checkMaturity) # Do an initial update of the job record after 2 iterations to make # sure that it is populated with something without having to wait too long updateJobResultsFirst = PeriodicActivityRequest(repeating=False, period=2, cb=self.__updateJobResultsPeriodic) periodicActivities = [updateModelDBResults, updateJobResultsFirst, updateJobResults, checkCancelation] if self._isMaturityEnabled: periodicActivities.append(checkMaturity) return PeriodicActivityMgr(requestedActivities=periodicActivities)
def __checkCancelation(self): """ Check if the cancelation flag has been set for this model in the Model DB""" # Update a hadoop job counter at least once every 600 seconds so it doesn't # think our map task is dead print >>sys.stderr, "reporter:counter:HypersearchWorker,numRecords,50" # See if the job got cancelled jobCancel = self._jobsDAO.jobGetFields(self._jobID, ['cancel'])[0] if jobCancel: self._cmpReason = ClientJobsDAO.CMPL_REASON_KILLED self._isCanceled = True self._logger.info("Model %s canceled because Job %s was stopped.", self._modelID, self._jobID) else: stopReason = self._jobsDAO.modelsGetFields(self._modelID, ['engStop'])[0] if stopReason is None: pass elif stopReason == ClientJobsDAO.STOP_REASON_KILLED: self._cmpReason = ClientJobsDAO.CMPL_REASON_KILLED self._isKilled = True self._logger.info("Model %s canceled because it was killed by hypersearch", self._modelID) elif stopReason == ClientJobsDAO.STOP_REASON_STOPPED: self._cmpReason = ClientJobsDAO.CMPL_REASON_STOPPED self._isCanceled = True self._logger.info("Model %s stopped because hypersearch ended", self._modelID) else: raise RuntimeError ("Unexpected stop reason encountered: %s" % (stopReason))
def __checkMaturity(self): """ Save the current metric value and see if the model's performance has 'leveled off.' We do this by looking at some number of previous number of recordings """ if self._currentRecordIndex+1 < self._MIN_RECORDS_TO_BE_BEST: return # If we are already mature, don't need to check anything if self._isMature: return metric = self._getMetrics()[self._optimizedMetricLabel] self._metricRegression.addPoint(x=self._currentRecordIndex, y=metric) # Perform a linear regression to see if the error is leveled off #pctChange = self._metricRegression.getPctChange() #if pctChange is not None and abs(pctChange ) <= self._MATURITY_MAX_CHANGE: pctChange, absPctChange = self._metricRegression.getPctChanges() if pctChange is not None and absPctChange <= self._MATURITY_MAX_CHANGE: self._jobsDAO.modelSetFields(self._modelID, {'engMatured':True}) # TODO: Don't stop if we are currently the best model. Also, if we # are still running after maturity, we have to periodically check to # see if we are still the best model. As soon we lose to some other # model, then we should stop at that point. self._cmpReason = ClientJobsDAO.CMPL_REASON_STOPPED self._isMature = True self._logger.info("Model %d has matured (pctChange=%s, n=%d). \n"\ "Scores = %s\n"\ "Stopping execution",self._modelID, pctChange, self._MATURITY_NUM_POINTS, self._metricRegression._window)
def __setAsOrphaned(self): """ Sets the current model as orphaned. This is called when the scheduler is about to kill the process to reallocate the worker to a different process. """ cmplReason = ClientJobsDAO.CMPL_REASON_ORPHAN cmplMessage = "Killed by Scheduler" self._jobsDAO.modelSetCompleted(self._modelID, cmplReason, cmplMessage)
def readStateFromDB(self): """Set our state to that obtained from the engWorkerState field of the job record. Parameters: --------------------------------------------------------------------- stateJSON: JSON encoded state from job record """ self._priorStateJSON = self._hsObj._cjDAO.jobGetFields(self._hsObj._jobID, ['engWorkerState'])[0] # Init if no prior state yet if self._priorStateJSON is None: swarms = dict() # Fast Swarm, first and only sprint has one swarm for each field # in fixedFields if self._hsObj._fixedFields is not None: print self._hsObj._fixedFields encoderSet = [] for field in self._hsObj._fixedFields: if field =='_classifierInput': continue encoderName = self.getEncoderKeyFromName(field) assert encoderName in self._hsObj._encoderNames, "The field '%s' " \ " specified in the fixedFields list is not present in this " \ " model." % (field) encoderSet.append(encoderName) encoderSet.sort() swarms['.'.join(encoderSet)] = { 'status': 'active', 'bestModelId': None, 'bestErrScore': None, 'sprintIdx': 0, } # Temporal prediction search, first sprint has N swarms of 1 field each, # the predicted field may or may not be that one field. elif self._hsObj._searchType == HsSearchType.temporal: for encoderName in self._hsObj._encoderNames: swarms[encoderName] = { 'status': 'active', 'bestModelId': None, 'bestErrScore': None, 'sprintIdx': 0, } # Classification prediction search, first sprint has N swarms of 1 field # each where this field can NOT be the predicted field. elif self._hsObj._searchType == HsSearchType.classification: for encoderName in self._hsObj._encoderNames: if encoderName == self._hsObj._predictedFieldEncoder: continue swarms[encoderName] = { 'status': 'active', 'bestModelId': None, 'bestErrScore': None, 'sprintIdx': 0, } # Legacy temporal. This is either a model that uses reconstruction or # an older multi-step model that doesn't have a separate # 'classifierOnly' encoder for the predicted field. Here, the predicted # field must ALWAYS be present and the first sprint tries the predicted # field only elif self._hsObj._searchType == HsSearchType.legacyTemporal: swarms[self._hsObj._predictedFieldEncoder] = { 'status': 'active', 'bestModelId': None, 'bestErrScore': None, 'sprintIdx': 0, } else: raise RuntimeError("Unsupported search type: %s" % \ (self._hsObj._searchType)) # Initialize the state. self._state = dict( # The last time the state was updated by a worker. lastUpdateTime = time.time(), # Set from within setSwarmState() if we detect that the sprint we just # completed did worse than a prior sprint. This stores the index of # the last good sprint. lastGoodSprint = None, # Set from within setSwarmState() if lastGoodSprint is True and all # sprints have completed. searchOver = False, # This is a summary of the active swarms - this information can also # be obtained from the swarms entry that follows, but is summarized here # for easier reference when viewing the state as presented by # log messages and prints of the hsState data structure (by # permutations_runner). activeSwarms = swarms.keys(), # All the swarms that have been created so far. swarms = swarms, # All the sprints that have completed or are in progress. sprints = [{'status': 'active', 'bestModelId': None, 'bestErrScore': None}], # The list of encoders we have "blacklisted" because they # performed so poorly. blackListedEncoders = [], ) # This will do nothing if the value of engWorkerState is not still None. self._hsObj._cjDAO.jobSetFieldIfEqual( self._hsObj._jobID, 'engWorkerState', json.dumps(self._state), None) self._priorStateJSON = self._hsObj._cjDAO.jobGetFields( self._hsObj._jobID, ['engWorkerState'])[0] assert (self._priorStateJSON is not None) # Read state from the database self._state = json.loads(self._priorStateJSON) self._dirty = False
def writeStateToDB(self): """Update the state in the job record with our local changes (if any). If we don't have the latest state in our priorStateJSON, then re-load in the latest state and return False. If we were successful writing out our changes, return True Parameters: --------------------------------------------------------------------- retval: True if we were successful writing out our changes False if our priorState is not the latest that was in the DB. In this case, we will re-load our state from the DB """ # If no changes, do nothing if not self._dirty: return True # Set the update time self._state['lastUpdateTime'] = time.time() newStateJSON = json.dumps(self._state) success = self._hsObj._cjDAO.jobSetFieldIfEqual(self._hsObj._jobID, 'engWorkerState', str(newStateJSON), str(self._priorStateJSON)) if success: self.logger.debug("Success changing hsState to: \n%s " % \ (pprint.pformat(self._state, indent=4))) self._priorStateJSON = newStateJSON # If no success, read in the current state from the DB else: self.logger.debug("Failed to change hsState to: \n%s " % \ (pprint.pformat(self._state, indent=4))) self._priorStateJSON = self._hsObj._cjDAO.jobGetFields(self._hsObj._jobID, ['engWorkerState'])[0] self._state = json.loads(self._priorStateJSON) self.logger.info("New hsState has been set by some other worker to: " " \n%s" % (pprint.pformat(self._state, indent=4))) return success
def getFieldContributions(self): """Return the field contributions statistics. Parameters: --------------------------------------------------------------------- retval: Dictionary where the keys are the field names and the values are how much each field contributed to the best score. """ #in the fast swarm, there is only 1 sprint and field contributions are #not defined if self._hsObj._fixedFields is not None: return dict(), dict() # Get the predicted field encoder name predictedEncoderName = self._hsObj._predictedFieldEncoder # ----------------------------------------------------------------------- # Collect all the single field scores fieldScores = [] for swarmId, info in self._state['swarms'].iteritems(): encodersUsed = swarmId.split('.') if len(encodersUsed) != 1: continue field = self.getEncoderNameFromKey(encodersUsed[0]) bestScore = info['bestErrScore'] # If the bestScore is None, this swarm hasn't completed yet (this could # happen if we're exiting because of maxModels), so look up the best # score so far if bestScore is None: (_modelId, bestScore) = \ self._hsObj._resultsDB.bestModelIdAndErrScore(swarmId) fieldScores.append((bestScore, field)) # ----------------------------------------------------------------------- # If we only have 1 field that was tried in the first sprint, then use that # as the base and get the contributions from the fields in the next sprint. if self._hsObj._searchType == HsSearchType.legacyTemporal: assert(len(fieldScores)==1) (baseErrScore, baseField) = fieldScores[0] for swarmId, info in self._state['swarms'].iteritems(): encodersUsed = swarmId.split('.') if len(encodersUsed) != 2: continue fields = [self.getEncoderNameFromKey(name) for name in encodersUsed] fields.remove(baseField) fieldScores.append((info['bestErrScore'], fields[0])) # The first sprint tried a bunch of fields, pick the worst performing one # (within the top self._hsObj._maxBranching ones) as the base else: fieldScores.sort(reverse=True) # If maxBranching was specified, pick the worst performing field within # the top maxBranching+1 fields as our base, which will give that field # a contribution of 0. if self._hsObj._maxBranching > 0 \ and len(fieldScores) > self._hsObj._maxBranching: baseErrScore = fieldScores[-self._hsObj._maxBranching-1][0] else: baseErrScore = fieldScores[0][0] # ----------------------------------------------------------------------- # Prepare and return the fieldContributions dict pctFieldContributionsDict = dict() absFieldContributionsDict = dict() # If we have no base score, can't compute field contributions. This can # happen when we exit early due to maxModels or being cancelled if baseErrScore is not None: # If the base error score is 0, we can't compute a percent difference # off of it, so move it to a very small float if abs(baseErrScore) < 0.00001: baseErrScore = 0.00001 for (errScore, field) in fieldScores: if errScore is not None: pctBetter = (baseErrScore - errScore) * 100.0 / baseErrScore else: pctBetter = 0.0 errScore = baseErrScore # for absFieldContribution pctFieldContributionsDict[field] = pctBetter absFieldContributionsDict[field] = baseErrScore - errScore self.logger.debug("FieldContributions: %s" % (pctFieldContributionsDict)) return pctFieldContributionsDict, absFieldContributionsDict
def getAllSwarms(self, sprintIdx): """Return the list of all swarms in the given sprint. Parameters: --------------------------------------------------------------------- retval: list of active swarm Ids in the given sprint """ swarmIds = [] for swarmId, info in self._state['swarms'].iteritems(): if info['sprintIdx'] == sprintIdx: swarmIds.append(swarmId) return swarmIds
def getCompletedSwarms(self): """Return the list of all completed swarms. Parameters: --------------------------------------------------------------------- retval: list of active swarm Ids """ swarmIds = [] for swarmId, info in self._state['swarms'].iteritems(): if info['status'] == 'completed': swarmIds.append(swarmId) return swarmIds
def getCompletingSwarms(self): """Return the list of all completing swarms. Parameters: --------------------------------------------------------------------- retval: list of active swarm Ids """ swarmIds = [] for swarmId, info in self._state['swarms'].iteritems(): if info['status'] == 'completing': swarmIds.append(swarmId) return swarmIds
def bestModelInSprint(self, sprintIdx): """Return the best model ID and it's errScore from the given sprint, which may still be in progress. This returns the best score from all models in the sprint which have matured so far. Parameters: --------------------------------------------------------------------- retval: (modelId, errScore) """ # Get all the swarms in this sprint swarms = self.getAllSwarms(sprintIdx) # Get the best model and score from each swarm bestModelId = None bestErrScore = numpy.inf for swarmId in swarms: (modelId, errScore) = self._hsObj._resultsDB.bestModelIdAndErrScore(swarmId) if errScore < bestErrScore: bestModelId = modelId bestErrScore = errScore return (bestModelId, bestErrScore)
def setSwarmState(self, swarmId, newStatus): """Change the given swarm's state to 'newState'. If 'newState' is 'completed', then bestModelId and bestErrScore must be provided. Parameters: --------------------------------------------------------------------- swarmId: swarm Id newStatus: new status, either 'active', 'completing', 'completed', or 'killed' """ assert (newStatus in ['active', 'completing', 'completed', 'killed']) # Set the swarm status swarmInfo = self._state['swarms'][swarmId] if swarmInfo['status'] == newStatus: return # If some other worker noticed it as completed, setting it to completing # is obviously old information.... if swarmInfo['status'] == 'completed' and newStatus == 'completing': return self._dirty = True swarmInfo['status'] = newStatus if newStatus == 'completed': (modelId, errScore) = self._hsObj._resultsDB.bestModelIdAndErrScore(swarmId) swarmInfo['bestModelId'] = modelId swarmInfo['bestErrScore'] = errScore # If no longer active, remove it from the activeSwarms entry if newStatus != 'active' and swarmId in self._state['activeSwarms']: self._state['activeSwarms'].remove(swarmId) # If new status is 'killed', kill off any running particles in that swarm if newStatus=='killed': self._hsObj.killSwarmParticles(swarmId) # In case speculative particles are enabled, make sure we generate a new # swarm at this time if all of the swarms in the current sprint have # completed. This will insure that we don't mark the sprint as completed # before we've created all the possible swarms. sprintIdx = swarmInfo['sprintIdx'] self.isSprintActive(sprintIdx) # Update the sprint status. Check all the swarms that belong to this sprint. # If they are all completed, the sprint is completed. sprintInfo = self._state['sprints'][sprintIdx] statusCounts = dict(active=0, completing=0, completed=0, killed=0) bestModelIds = [] bestErrScores = [] for info in self._state['swarms'].itervalues(): if info['sprintIdx'] != sprintIdx: continue statusCounts[info['status']] += 1 if info['status'] == 'completed': bestModelIds.append(info['bestModelId']) bestErrScores.append(info['bestErrScore']) if statusCounts['active'] > 0: sprintStatus = 'active' elif statusCounts['completing'] > 0: sprintStatus = 'completing' else: sprintStatus = 'completed' sprintInfo['status'] = sprintStatus # If the sprint is complete, get the best model from all of its swarms and # store that as the sprint best if sprintStatus == 'completed': if len(bestErrScores) > 0: whichIdx = numpy.array(bestErrScores).argmin() sprintInfo['bestModelId'] = bestModelIds[whichIdx] sprintInfo['bestErrScore'] = bestErrScores[whichIdx] else: # This sprint was empty, most likely because all particles were # killed. Give it a huge error score sprintInfo['bestModelId'] = 0 sprintInfo['bestErrScore'] = numpy.inf # See if our best err score got NO BETTER as compared to a previous # sprint. If so, stop exploring subsequent sprints (lastGoodSprint # is no longer None). bestPrior = numpy.inf for idx in range(sprintIdx): if self._state['sprints'][idx]['status'] == 'completed': (_, errScore) = self.bestModelInCompletedSprint(idx) if errScore is None: errScore = numpy.inf else: errScore = numpy.inf if errScore < bestPrior: bestPrior = errScore if sprintInfo['bestErrScore'] >= bestPrior: self._state['lastGoodSprint'] = sprintIdx-1 # If ALL sprints up to the last good one are done, the search is now over if self._state['lastGoodSprint'] is not None \ and not self.anyGoodSprintsActive(): self._state['searchOver'] = True
def anyGoodSprintsActive(self): """Return True if there are any more good sprints still being explored. A 'good' sprint is one that is earlier than where we detected an increase in error from sprint to subsequent sprint. """ if self._state['lastGoodSprint'] is not None: goodSprints = self._state['sprints'][0:self._state['lastGoodSprint']+1] else: goodSprints = self._state['sprints'] for sprint in goodSprints: if sprint['status'] == 'active': anyActiveSprints = True break else: anyActiveSprints = False return anyActiveSprints
def isSprintCompleted(self, sprintIdx): """Return True if the given sprint has completed.""" numExistingSprints = len(self._state['sprints']) if sprintIdx >= numExistingSprints: return False return (self._state['sprints'][sprintIdx]['status'] == 'completed')
def killUselessSwarms(self): """See if we can kill off some speculative swarms. If an earlier sprint has finally completed, we can now tell which fields should *really* be present in the sprints we've already started due to speculation, and kill off the swarms that should not have been included. """ # Get number of existing sprints numExistingSprints = len(self._state['sprints']) # Should we bother killing useless swarms? if self._hsObj._searchType == HsSearchType.legacyTemporal: if numExistingSprints <= 2: return else: if numExistingSprints <= 1: return # Form completedSwarms as a list of tuples, each tuple contains: # (swarmName, swarmState, swarmBestErrScore) # ex. completedSwarms: # [('a', {...}, 1.4), # ('b', {...}, 2.0), # ('c', {...}, 3.0)] completedSwarms = self.getCompletedSwarms() completedSwarms = [(swarm, self._state["swarms"][swarm], self._state["swarms"][swarm]["bestErrScore"]) \ for swarm in completedSwarms] # Form the completedMatrix. Each row corresponds to a sprint. Each row # contains the list of swarm tuples that belong to that sprint, sorted # by best score. Each swarm tuple contains (swarmName, swarmState, # swarmBestErrScore). # ex. completedMatrix: # [(('a', {...}, 1.4), ('b', {...}, 2.0), ('c', {...}, 3.0)), # (('a.b', {...}, 3.0), ('b.c', {...}, 4.0))] completedMatrix = [[] for i in range(numExistingSprints)] for swarm in completedSwarms: completedMatrix[swarm[1]["sprintIdx"]].append(swarm) for sprint in completedMatrix: sprint.sort(key=itemgetter(2)) # Form activeSwarms as a list of tuples, each tuple contains: # (swarmName, swarmState, swarmBestErrScore) # Include all activeSwarms and completingSwarms # ex. activeSwarms: # [('d', {...}, 1.4), # ('e', {...}, 2.0), # ('f', {...}, 3.0)] activeSwarms = self.getActiveSwarms() # Append the completing swarms activeSwarms.extend(self.getCompletingSwarms()) activeSwarms = [(swarm, self._state["swarms"][swarm], self._state["swarms"][swarm]["bestErrScore"]) \ for swarm in activeSwarms] # Form the activeMatrix. Each row corresponds to a sprint. Each row # contains the list of swarm tuples that belong to that sprint, sorted # by best score. Each swarm tuple contains (swarmName, swarmState, # swarmBestErrScore) # ex. activeMatrix: # [(('d', {...}, 1.4), ('e', {...}, 2.0), ('f', {...}, 3.0)), # (('d.e', {...}, 3.0), ('e.f', {...}, 4.0))] activeMatrix = [[] for i in range(numExistingSprints)] for swarm in activeSwarms: activeMatrix[swarm[1]["sprintIdx"]].append(swarm) for sprint in activeMatrix: sprint.sort(key=itemgetter(2)) # Figure out which active swarms to kill toKill = [] for i in range(1, numExistingSprints): for swarm in activeMatrix[i]: curSwarmEncoders = swarm[0].split(".") # If previous sprint is complete, get the best swarm and kill all active # sprints that are not supersets if(len(activeMatrix[i-1])==0): # If we are trying all possible 3 field combinations, don't kill any # off in sprint 2 if i==2 and (self._hsObj._tryAll3FieldCombinations or \ self._hsObj._tryAll3FieldCombinationsWTimestamps): pass else: bestInPrevious = completedMatrix[i-1][0] bestEncoders = bestInPrevious[0].split('.') for encoder in bestEncoders: if not encoder in curSwarmEncoders: toKill.append(swarm) # if there are more than two completed encoders sets that are complete and # are worse than at least one active swarm in the previous sprint. Remove # any combinations that have any pair of them since they cannot have the best encoder. #elif(len(completedMatrix[i-1])>1): # for completedSwarm in completedMatrix[i-1]: # activeMatrix[i-1][0][2]<completed # Mark the bad swarms as killed if len(toKill) > 0: print "ParseMe: Killing encoders:" + str(toKill) for swarm in toKill: self.setSwarmState(swarm[0], "killed") return
def isSprintActive(self, sprintIdx): """If the given sprint exists and is active, return active=True. If the sprint does not exist yet, this call will create it (and return active=True). If it already exists, but is completing or complete, return active=False. If sprintIdx is past the end of the possible sprints, return active=False, noMoreSprints=True IMPORTANT: When speculative particles are enabled, this call has some special processing to handle speculative sprints: * When creating a new speculative sprint (creating sprint N before sprint N-1 has completed), it initially only puts in only ONE swarm into the sprint. * Every time it is asked if sprint N is active, it also checks to see if it is time to add another swarm to the sprint, and adds a new swarm if appropriate before returning active=True * We decide it is time to add a new swarm to a speculative sprint when ALL of the currently active swarms in the sprint have all the workers they need (number of running (not mature) particles is _minParticlesPerSwarm). This means that we have capacity to run additional particles in a new swarm. It is expected that the sprints will be checked IN ORDER from 0 on up. (It is an error not to) The caller should always try to allocate from the first active sprint it finds. If it can't, then it can call this again to find/create the next active sprint. Parameters: --------------------------------------------------------------------- retval: (active, noMoreSprints) active: True if the given sprint is active noMoreSprints: True if there are no more sprints possible """ while True: numExistingSprints = len(self._state['sprints']) # If this sprint already exists, see if it is active if sprintIdx <= numExistingSprints-1: # With speculation off, it's simple, just return whether or not the # asked for sprint has active status if not self._hsObj._speculativeParticles: active = (self._state['sprints'][sprintIdx]['status'] == 'active') return (active, False) # With speculation on, if the sprint is still marked active, we also # need to see if it's time to add a new swarm to it. else: active = (self._state['sprints'][sprintIdx]['status'] == 'active') if not active: return (active, False) # See if all of the existing swarms are at capacity (have all the # workers they need): activeSwarmIds = self.getActiveSwarms(sprintIdx) swarmSizes = [self._hsObj._resultsDB.getParticleInfos(swarmId, matured=False)[0] for swarmId in activeSwarmIds] notFullSwarms = [len(swarm) for swarm in swarmSizes \ if len(swarm) < self._hsObj._minParticlesPerSwarm] # If some swarms have room return that the swarm is active. if len(notFullSwarms) > 0: return (True, False) # If the existing swarms are at capacity, we will fall through to the # logic below which tries to add a new swarm to the sprint. # Stop creating new sprints? if self._state['lastGoodSprint'] is not None: return (False, True) # if fixedFields is set, we are running a fast swarm and only run sprint0 if self._hsObj._fixedFields is not None: return (False, True) # ---------------------------------------------------------------------- # Get the best model (if there is one) from the prior sprint. That gives # us the base encoder set for the next sprint. For sprint zero make sure # it does not take the last sprintidx because of wrapping. if sprintIdx > 0 \ and self._state['sprints'][sprintIdx-1]['status'] == 'completed': (bestModelId, _) = self.bestModelInCompletedSprint(sprintIdx-1) (particleState, _, _, _, _) = self._hsObj._resultsDB.getParticleInfo( bestModelId) bestSwarmId = particleState['swarmId'] baseEncoderSets = [bestSwarmId.split('.')] # If there is no best model yet, then use all encoder sets from the prior # sprint that were not killed else: bestSwarmId = None particleState = None # Build up more combinations, using ALL of the sets in the current # sprint. baseEncoderSets = [] for swarmId in self.getNonKilledSwarms(sprintIdx-1): baseEncoderSets.append(swarmId.split('.')) # ---------------------------------------------------------------------- # Which encoders should we add to the current base set? encoderAddSet = [] # If we have constraints on how many fields we carry forward into # subsequent sprints (either nupic.hypersearch.max.field.branching or # nupic.hypersearch.min.field.contribution was set), then be more # picky about which fields we add in. limitFields = False if self._hsObj._maxBranching > 0 \ or self._hsObj._minFieldContribution >= 0: if self._hsObj._searchType == HsSearchType.temporal or \ self._hsObj._searchType == HsSearchType.classification: if sprintIdx >= 1: limitFields = True baseSprintIdx = 0 elif self._hsObj._searchType == HsSearchType.legacyTemporal: if sprintIdx >= 2: limitFields = True baseSprintIdx = 1 else: raise RuntimeError("Unimplemented search type %s" % \ (self._hsObj._searchType)) # Only add top _maxBranching encoders to the swarms? if limitFields: # Get field contributions to filter added fields pctFieldContributions, absFieldContributions = \ self.getFieldContributions() toRemove = [] self.logger.debug("FieldContributions min: %s" % \ (self._hsObj._minFieldContribution)) for fieldname in pctFieldContributions: if pctFieldContributions[fieldname] < self._hsObj._minFieldContribution: self.logger.debug("FieldContributions removing: %s" % (fieldname)) toRemove.append(self.getEncoderKeyFromName(fieldname)) else: self.logger.debug("FieldContributions keeping: %s" % (fieldname)) # Grab the top maxBranching base sprint swarms. swarms = self._state["swarms"] sprintSwarms = [(swarm, swarms[swarm]["bestErrScore"]) \ for swarm in swarms if swarms[swarm]["sprintIdx"] == baseSprintIdx] sprintSwarms = sorted(sprintSwarms, key=itemgetter(1)) if self._hsObj._maxBranching > 0: sprintSwarms = sprintSwarms[0:self._hsObj._maxBranching] # Create encoder set to generate further swarms. for swarm in sprintSwarms: swarmEncoders = swarm[0].split(".") for encoder in swarmEncoders: if not encoder in encoderAddSet: encoderAddSet.append(encoder) encoderAddSet = [encoder for encoder in encoderAddSet \ if not str(encoder) in toRemove] # If no limit on the branching or min contribution, simply use all of the # encoders. else: encoderAddSet = self._hsObj._encoderNames # ----------------------------------------------------------------------- # Build up the new encoder combinations for the next sprint. newSwarmIds = set() # See if the caller wants to try more extensive field combinations with # 3 fields. if (self._hsObj._searchType == HsSearchType.temporal \ or self._hsObj._searchType == HsSearchType.legacyTemporal) \ and sprintIdx == 2 \ and (self._hsObj._tryAll3FieldCombinations or \ self._hsObj._tryAll3FieldCombinationsWTimestamps): if self._hsObj._tryAll3FieldCombinations: newEncoders = set(self._hsObj._encoderNames) if self._hsObj._predictedFieldEncoder in newEncoders: newEncoders.remove(self._hsObj._predictedFieldEncoder) else: # Just make sure the timestamp encoders are part of the mix newEncoders = set(encoderAddSet) if self._hsObj._predictedFieldEncoder in newEncoders: newEncoders.remove(self._hsObj._predictedFieldEncoder) for encoder in self._hsObj._encoderNames: if encoder.endswith('_timeOfDay') or encoder.endswith('_weekend') \ or encoder.endswith('_dayOfWeek'): newEncoders.add(encoder) allCombos = list(itertools.combinations(newEncoders, 2)) for combo in allCombos: newSet = list(combo) newSet.append(self._hsObj._predictedFieldEncoder) newSet.sort() newSwarmId = '.'.join(newSet) if newSwarmId not in self._state['swarms']: newSwarmIds.add(newSwarmId) # If a speculative sprint, only add the first encoder, if not add # all of them. if (len(self.getActiveSwarms(sprintIdx-1)) > 0): break # Else, we only build up by adding 1 new encoder to the best combination(s) # we've seen from the prior sprint else: for baseEncoderSet in baseEncoderSets: for encoder in encoderAddSet: if encoder not in self._state['blackListedEncoders'] \ and encoder not in baseEncoderSet: newSet = list(baseEncoderSet) newSet.append(encoder) newSet.sort() newSwarmId = '.'.join(newSet) if newSwarmId not in self._state['swarms']: newSwarmIds.add(newSwarmId) # If a speculative sprint, only add the first encoder, if not add # all of them. if (len(self.getActiveSwarms(sprintIdx-1)) > 0): break # ---------------------------------------------------------------------- # Sort the new swarm Ids newSwarmIds = sorted(newSwarmIds) # If no more swarms can be found for this sprint... if len(newSwarmIds) == 0: # if sprint is not an empty sprint return that it is active but do not # add anything to it. if len(self.getAllSwarms(sprintIdx)) > 0: return (True, False) # If this is an empty sprint and we couldn't find any new swarms to # add (only bad fields are remaining), the search is over else: return (False, True) # Add this sprint and the swarms that are in it to our state self._dirty = True # Add in the new sprint if necessary if len(self._state["sprints"]) == sprintIdx: self._state['sprints'].append({'status': 'active', 'bestModelId': None, 'bestErrScore': None}) # Add in the new swarm(s) to the sprint for swarmId in newSwarmIds: self._state['swarms'][swarmId] = {'status': 'active', 'bestModelId': None, 'bestErrScore': None, 'sprintIdx': sprintIdx} # Update the list of active swarms self._state['activeSwarms'] = self.getActiveSwarms() # Try to set new state success = self.writeStateToDB() # Return result if successful if success: return (True, False)
def addEncoder(self, name, encoder): """ Adds one encoder. :param name: (string) name of encoder, should be unique :param encoder: (:class:`.Encoder`) the encoder to add """ self.encoders.append((name, encoder, self.width)) for d in encoder.getDescription(): self.description.append((d[0], d[1] + self.width)) self.width += encoder.getWidth()
def invariant(self): """Verify the validity of the node spec object The type of each sub-object is verified and then the validity of each node spec item is verified by calling it invariant() method. It also makes sure that there is at most one default input and one default output. """ # Verify the description and singleNodeOnly attributes assert isinstance(self.description, str) assert isinstance(self.singleNodeOnly, bool) # Make sure that all items dicts are really dicts assert isinstance(self.inputs, dict) assert isinstance(self.outputs, dict) assert isinstance(self.parameters, dict) assert isinstance(self.commands, dict) # Verify all item dicts hasDefaultInput = False for k, v in self.inputs.items(): assert isinstance(k, str) assert isinstance(v, InputSpec) v.invariant() if v.isDefaultInput: assert not hasDefaultInput hasDefaultInput = True hasDefaultOutput = False for k, v in self.outputs.items(): assert isinstance(k, str) assert isinstance(v, OutputSpec) v.invariant() if v.isDefaultOutput: assert not hasDefaultOutput hasDefaultOutput = True for k, v in self.parameters.items(): assert isinstance(k, str) assert isinstance(v, ParameterSpec) v.invariant() for k, v in self.commands.items(): assert isinstance(k, str) assert isinstance(v, CommandSpec) v.invariant()
def toDict(self): """Convert the information of the node spec to a plain dict of basic types The description and singleNodeOnly attributes are placed directly in the result dicts. The inputs, outputs, parameters and commands dicts contain Spec item objects (InputSpec, OutputSpec, etc). Each such object is converted also to a plain dict using the internal items2dict() function (see bellow). """ def items2dict(items): """Convert a dict of node spec items to a plain dict Each node spec item object will be converted to a dict of its attributes. The entire items dict will become a dict of dicts (same keys). """ d = {} for k, v in items.items(): d[k] = v.__dict__ return d self.invariant() return dict(description=self.description, singleNodeOnly=self.singleNodeOnly, inputs=items2dict(self.inputs), outputs=items2dict(self.outputs), parameters=items2dict(self.parameters), commands=items2dict(self.commands))
def updateResultsForJob(self, forceUpdate=True): """ Chooses the best model for a given job. Parameters ----------------------------------------------------------------------- forceUpdate: (True/False). If True, the update will ignore all the restrictions on the minimum time to update and the minimum number of records to update. This should typically only be set to true if the model has completed running """ updateInterval = time.time() - self._lastUpdateAttemptTime if updateInterval < self._MIN_UPDATE_INTERVAL and not forceUpdate: return self.logger.info("Attempting model selection for jobID=%d: time=%f"\ " lastUpdate=%f"%(self._jobID, time.time(), self._lastUpdateAttemptTime)) timestampUpdated = self._cjDB.jobUpdateSelectionSweep(self._jobID, self._MIN_UPDATE_INTERVAL) if not timestampUpdated: self.logger.info("Unable to update selection sweep timestamp: jobID=%d" \ " updateTime=%f"%(self._jobID, self._lastUpdateAttemptTime)) if not forceUpdate: return self._lastUpdateAttemptTime = time.time() self.logger.info("Succesfully updated selection sweep timestamp jobid=%d updateTime=%f"\ %(self._jobID, self._lastUpdateAttemptTime)) minUpdateRecords = self._MIN_UPDATE_THRESHOLD jobResults = self._getJobResults() if forceUpdate or jobResults is None: minUpdateRecords = 0 candidateIDs, bestMetric = self._cjDB.modelsGetCandidates(self._jobID, minUpdateRecords) self.logger.info("Candidate models=%s, metric=%s, jobID=%s"\ %(candidateIDs, bestMetric, self._jobID)) if len(candidateIDs) == 0: return self._jobUpdateCandidate(candidateIDs[0], bestMetric, results=jobResults)
def createEncoder(): """Create the encoder instance for our test and return it.""" consumption_encoder = ScalarEncoder(21, 0.0, 100.0, n=50, name="consumption", clipInput=True) time_encoder = DateEncoder(timeOfDay=(21, 9.5), name="timestamp_timeOfDay") encoder = MultiEncoder() encoder.addEncoder("consumption", consumption_encoder) encoder.addEncoder("timestamp", time_encoder) return encoder
def createNetwork(dataSource): """Create the Network instance. The network has a sensor region reading data from `dataSource` and passing the encoded representation to an SPRegion. The SPRegion output is passed to a TMRegion. :param dataSource: a RecordStream instance to get data from :returns: a Network instance ready to run """ network = Network() # Our input is sensor data from the gym file. The RecordSensor region # allows us to specify a file record stream as the input source via the # dataSource attribute. network.addRegion("sensor", "py.RecordSensor", json.dumps({"verbosity": _VERBOSITY})) sensor = network.regions["sensor"].getSelf() # The RecordSensor needs to know how to encode the input values sensor.encoder = createEncoder() # Specify the dataSource as a file record stream instance sensor.dataSource = dataSource # Create the spatial pooler region SP_PARAMS["inputWidth"] = sensor.encoder.getWidth() network.addRegion("spatialPoolerRegion", "py.SPRegion", json.dumps(SP_PARAMS)) # Link the SP region to the sensor input network.link("sensor", "spatialPoolerRegion", "UniformLink", "") network.link("sensor", "spatialPoolerRegion", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") network.link("spatialPoolerRegion", "sensor", "UniformLink", "", srcOutput="spatialTopDownOut", destInput="spatialTopDownIn") network.link("spatialPoolerRegion", "sensor", "UniformLink", "", srcOutput="temporalTopDownOut", destInput="temporalTopDownIn") # Add the TMRegion on top of the SPRegion network.addRegion("temporalPoolerRegion", "py.TMRegion", json.dumps(TM_PARAMS)) network.link("spatialPoolerRegion", "temporalPoolerRegion", "UniformLink", "") network.link("temporalPoolerRegion", "spatialPoolerRegion", "UniformLink", "", srcOutput="topDownOut", destInput="topDownIn") # Add the AnomalyLikelihoodRegion on top of the TMRegion network.addRegion("anomalyLikelihoodRegion", "py.AnomalyLikelihoodRegion", json.dumps({})) network.link("temporalPoolerRegion", "anomalyLikelihoodRegion", "UniformLink", "", srcOutput="anomalyScore", destInput="rawAnomalyScore") network.link("sensor", "anomalyLikelihoodRegion", "UniformLink", "", srcOutput="sourceOut", destInput="metricValue") spatialPoolerRegion = network.regions["spatialPoolerRegion"] # Make sure learning is enabled spatialPoolerRegion.setParameter("learningMode", True) # We want temporal anomalies so disable anomalyMode in the SP. This mode is # used for computing anomalies in a non-temporal model. spatialPoolerRegion.setParameter("anomalyMode", False) temporalPoolerRegion = network.regions["temporalPoolerRegion"] # Enable topDownMode to get the predicted columns output temporalPoolerRegion.setParameter("topDownMode", True) # Make sure learning is enabled (this is the default) temporalPoolerRegion.setParameter("learningMode", True) # Enable inference mode so we get predictions temporalPoolerRegion.setParameter("inferenceMode", True) # Enable anomalyMode to compute the anomaly score to be passed to the anomaly # likelihood region. temporalPoolerRegion.setParameter("anomalyMode", True) return network
def runNetwork(network, writer): """Run the network and write output to writer. :param network: a Network instance to run :param writer: a csv.writer instance to write output to """ sensorRegion = network.regions["sensor"] spatialPoolerRegion = network.regions["spatialPoolerRegion"] temporalPoolerRegion = network.regions["temporalPoolerRegion"] anomalyLikelihoodRegion = network.regions["anomalyLikelihoodRegion"] prevPredictedColumns = [] for i in xrange(_NUM_RECORDS): # Run the network for a single iteration network.run(1) # Write out the anomaly likelihood along with the record number and consumption # value. consumption = sensorRegion.getOutputData("sourceOut")[0] anomalyScore = temporalPoolerRegion.getOutputData("anomalyScore")[0] anomalyLikelihood = anomalyLikelihoodRegion.getOutputData("anomalyLikelihood")[0] writer.writerow((i, consumption, anomalyScore, anomalyLikelihood))
def __validateExperimentControl(self, control): """ Validates control dictionary for the experiment context""" # Validate task list taskList = control.get('tasks', None) if taskList is not None: taskLabelsList = [] for task in taskList: validateOpfJsonValue(task, "opfTaskSchema.json") validateOpfJsonValue(task['taskControl'], "opfTaskControlSchema.json") taskLabel = task['taskLabel'] assert isinstance(taskLabel, types.StringTypes), \ "taskLabel type: %r" % type(taskLabel) assert len(taskLabel) > 0, "empty string taskLabel not is allowed" taskLabelsList.append(taskLabel.lower()) taskLabelDuplicates = filter(lambda x: taskLabelsList.count(x) > 1, taskLabelsList) assert len(taskLabelDuplicates) == 0, \ "Duplcate task labels are not allowed: %s" % taskLabelDuplicates return
def normalizeStreamSource(self, stream): """ TODO: document :param stream: """ # The stream source in the task might be in several formats, so we need # to make sure it gets converted into an absolute path: source = stream['source'][len(FILE_SCHEME):] # If the source is already an absolute path, we won't use pkg_resources, # we'll just trust that the path exists. If not, it's a user problem. if os.path.isabs(source): sourcePath = source else: # First we'll check to see if this path exists within the nupic.datafiles # package resource. sourcePath = resource_filename("nupic.datafiles", source) if not os.path.exists(sourcePath): # If this path doesn't exist as a package resource, the last option will # be to treat it as a relative path to the current working directory. sourcePath = os.path.join(os.getcwd(), source) stream['source'] = FILE_SCHEME + sourcePath
def normalizeStreamSources(self): """ TODO: document """ task = dict(self.__control) if 'dataset' in task: for stream in task['dataset']['streams']: self.normalizeStreamSource(stream) else: for subtask in task['tasks']: for stream in subtask['dataset']['streams']: self.normalizeStreamSource(stream)
def convertNupicEnvToOPF(self): """ TODO: document """ # We need to create a task structure, most of which is taken verbatim # from the Nupic control dict task = dict(self.__control) task.pop('environment') inferenceArgs = task.pop('inferenceArgs') task['taskLabel'] = 'DefaultTask' # Create the iterationCycle element that will be placed inside the # taskControl. iterationCount = task.get('iterationCount', -1) iterationCountInferOnly = task.pop('iterationCountInferOnly', 0) if iterationCountInferOnly == -1: iterationCycle = [IterationPhaseSpecInferOnly(1000, inferenceArgs=inferenceArgs)] elif iterationCountInferOnly > 0: assert iterationCount > 0, "When iterationCountInferOnly is specified, "\ "iterationCount must also be specified and not be -1" iterationCycle = [IterationPhaseSpecLearnAndInfer(iterationCount -iterationCountInferOnly, inferenceArgs=inferenceArgs), IterationPhaseSpecInferOnly(iterationCountInferOnly, inferenceArgs=inferenceArgs)] else: iterationCycle = [IterationPhaseSpecLearnAndInfer(1000, inferenceArgs=inferenceArgs)] taskControl = dict(metrics = task.pop('metrics'), loggedMetrics = task.pop('loggedMetrics'), iterationCycle = iterationCycle) task['taskControl'] = taskControl # Create the new control self.__control = dict(environment = OpfEnvironment.Nupic, tasks = [task])
def createNetwork(dataSource): """Create the Network instance. The network has a sensor region reading data from `dataSource` and passing the encoded representation to an Identity Region. :param dataSource: a RecordStream instance to get data from :returns: a Network instance ready to run """ network = Network() # Our input is sensor data from the gym file. The RecordSensor region # allows us to specify a file record stream as the input source via the # dataSource attribute. network.addRegion("sensor", "py.RecordSensor", json.dumps({"verbosity": _VERBOSITY})) sensor = network.regions["sensor"].getSelf() # The RecordSensor needs to know how to encode the input values sensor.encoder = createEncoder() # Specify the dataSource as a file record stream instance sensor.dataSource = dataSource # CUSTOM REGION # Add path to custom region to PYTHONPATH # NOTE: Before using a custom region, please modify your PYTHONPATH # export PYTHONPATH="<path to custom region module>:$PYTHONPATH" # In this demo, we have modified it using sys.path.append since we need it to # have an effect on this program. sys.path.append(os.path.dirname(os.path.abspath(__file__))) from custom_region.identity_region import IdentityRegion # Add custom region class to the network Network.registerRegion(IdentityRegion) # Create a custom region network.addRegion("identityRegion", "py.IdentityRegion", json.dumps({ "dataWidth": sensor.encoder.getWidth(), })) # Link the Identity region to the sensor output network.link("sensor", "identityRegion", "UniformLink", "") network.initialize() return network
def runNetwork(network, writer): """Run the network and write output to writer. :param network: a Network instance to run :param writer: a csv.writer instance to write output to """ identityRegion = network.regions["identityRegion"] for i in xrange(_NUM_RECORDS): # Run the network for a single iteration network.run(1) # Write out the record number and encoding encoding = identityRegion.getOutputData("out") writer.writerow((i, encoding))
def _appendReportKeys(keys, prefix, results): """ Generate a set of possible report keys for an experiment's results. A report key is a string of key names separated by colons, each key being one level deeper into the experiment results dict. For example, 'key1:key2'. This routine is called recursively to build keys that are multiple levels deep from the results dict. Parameters: ----------------------------------------------------------- keys: Set of report keys accumulated so far prefix: prefix formed so far, this is the colon separated list of key names that led up to the dict passed in results results: dictionary of results at this level. """ allKeys = results.keys() allKeys.sort() for key in allKeys: if hasattr(results[key], 'keys'): _appendReportKeys(keys, "%s%s:" % (prefix, key), results[key]) else: keys.add("%s%s" % (prefix, key))
def _matchReportKeys(reportKeyREs=[], allReportKeys=[]): """ Extract all items from the 'allKeys' list whose key matches one of the regular expressions passed in 'reportKeys'. Parameters: ---------------------------------------------------------------------------- reportKeyREs: List of regular expressions allReportKeys: List of all keys retval: list of keys from allReportKeys that match the regular expressions in 'reportKeyREs' If an invalid regular expression was included in 'reportKeys', then BadKeyError() is raised """ matchingReportKeys = [] # Extract the report items of interest for keyRE in reportKeyREs: # Find all keys that match this regular expression matchObj = re.compile(keyRE) found = False for keyName in allReportKeys: match = matchObj.match(keyName) if match and match.end() == len(keyName): matchingReportKeys.append(keyName) found = True if not found: raise _BadKeyError(keyRE) return matchingReportKeys
def _getReportItem(itemName, results): """ Get a specific item by name out of the results dict. The format of itemName is a string of dictionary keys separated by colons, each key being one level deeper into the results dict. For example, 'key1:key2' would fetch results['key1']['key2']. If itemName is not found in results, then None is returned """ subKeys = itemName.split(':') subResults = results for subKey in subKeys: subResults = subResults[subKey] return subResults
def filterResults(allResults, reportKeys, optimizeKey=None): """ Given the complete set of results generated by an experiment (passed in 'results'), filter out and return only the ones the caller wants, as specified through 'reportKeys' and 'optimizeKey'. A report key is a string of key names separated by colons, each key being one level deeper into the experiment results dict. For example, 'key1:key2'. Parameters: ------------------------------------------------------------------------- results: dict of all results generated by an experiment reportKeys: list of items from the results dict to include in the report. These can be regular expressions. optimizeKey: Which report item, if any, we will be optimizing for. This can also be a regular expression, but is an error if it matches more than one key from the experiment's results. retval: (reportDict, optimizeDict) reportDict: a dictionary of the metrics named by desiredReportKeys optimizeDict: A dictionary containing 1 item: the full name and value of the metric identified by the optimizeKey """ # Init return values optimizeDict = dict() # Get all available report key names for this experiment allReportKeys = set() _appendReportKeys(keys=allReportKeys, prefix='', results=allResults) #---------------------------------------------------------------------------- # Extract the report items that match the regular expressions passed in reportKeys matchingKeys = _matchReportKeys(reportKeys, allReportKeys) # Extract the values of the desired items reportDict = dict() for keyName in matchingKeys: value = _getReportItem(keyName, allResults) reportDict[keyName] = value # ------------------------------------------------------------------------- # Extract the report item that matches the regular expression passed in # optimizeKey if optimizeKey is not None: matchingKeys = _matchReportKeys([optimizeKey], allReportKeys) if len(matchingKeys) == 0: raise _BadKeyError(optimizeKey) elif len(matchingKeys) > 1: raise _BadOptimizeKeyError(optimizeKey, matchingKeys) optimizeKeyFullName = matchingKeys[0] # Get the value of the optimize metric value = _getReportItem(optimizeKeyFullName, allResults) optimizeDict[optimizeKeyFullName] = value reportDict[optimizeKeyFullName] = value # Return info return(reportDict, optimizeDict)
def _handleModelRunnerException(jobID, modelID, jobsDAO, experimentDir, logger, e): """ Perform standard handling of an exception that occurs while running a model. Parameters: ------------------------------------------------------------------------- jobID: ID for this hypersearch job in the jobs table modelID: model ID jobsDAO: ClientJobsDAO instance experimentDir: directory containing the experiment logger: the logger to use e: the exception that occurred retval: (completionReason, completionMsg) """ msg = StringIO.StringIO() print >>msg, "Exception occurred while running model %s: %r (%s)" % ( modelID, e, type(e)) traceback.print_exc(None, msg) completionReason = jobsDAO.CMPL_REASON_ERROR completionMsg = msg.getvalue() logger.error(completionMsg) # Write results to the model database for the error case. Ignore # InvalidConnectionException, as this is usually caused by orphaned models # # TODO: do we really want to set numRecords to 0? Last updated value might # be useful for debugging if type(e) is not InvalidConnectionException: jobsDAO.modelUpdateResults(modelID, results=None, numRecords=0) # TODO: Make sure this wasn't the best model in job. If so, set the best # appropriately # If this was an exception that should mark the job as failed, do that # now. if type(e) == JobFailException: workerCmpReason = jobsDAO.jobGetFields(jobID, ['workerCompletionReason'])[0] if workerCmpReason == ClientJobsDAO.CMPL_REASON_SUCCESS: jobsDAO.jobSetFields(jobID, fields=dict( cancel=True, workerCompletionReason = ClientJobsDAO.CMPL_REASON_ERROR, workerCompletionMsg = ": ".join(str(i) for i in e.args)), useConnectionID=False, ignoreUnchanged=True) return (completionReason, completionMsg)
def runModelGivenBaseAndParams(modelID, jobID, baseDescription, params, predictedField, reportKeys, optimizeKey, jobsDAO, modelCheckpointGUID, logLevel=None, predictionCacheMaxRecords=None): """ This creates an experiment directory with a base.py description file created from 'baseDescription' and a description.py generated from the given params dict and then runs the experiment. Parameters: ------------------------------------------------------------------------- modelID: ID for this model in the models table jobID: ID for this hypersearch job in the jobs table baseDescription: Contents of a description.py with the base experiment description params: Dictionary of specific parameters to override within the baseDescriptionFile. predictedField: Name of the input field for which this model is being optimized reportKeys: Which metrics of the experiment to store into the results dict of the model's database entry optimizeKey: Which metric we are optimizing for jobsDAO Jobs data access object - the interface to the jobs database which has the model's table. modelCheckpointGUID: A persistent, globally-unique identifier for constructing the model checkpoint key logLevel: override logging level to this value, if not None retval: (completionReason, completionMsg) """ from nupic.swarming.ModelRunner import OPFModelRunner # The logger for this method logger = logging.getLogger('com.numenta.nupic.hypersearch.utils') # -------------------------------------------------------------------------- # Create a temp directory for the experiment and the description files experimentDir = tempfile.mkdtemp() try: logger.info("Using experiment directory: %s" % (experimentDir)) # Create the decription.py from the overrides in params paramsFilePath = os.path.join(experimentDir, 'description.py') paramsFile = open(paramsFilePath, 'wb') paramsFile.write(_paramsFileHead()) items = params.items() items.sort() for (key,value) in items: quotedKey = _quoteAndEscape(key) if isinstance(value, basestring): paramsFile.write(" %s : '%s',\n" % (quotedKey , value)) else: paramsFile.write(" %s : %s,\n" % (quotedKey , value)) paramsFile.write(_paramsFileTail()) paramsFile.close() # Write out the base description baseParamsFile = open(os.path.join(experimentDir, 'base.py'), 'wb') baseParamsFile.write(baseDescription) baseParamsFile.close() # Store the experiment's sub-description file into the model table # for reference fd = open(paramsFilePath) expDescription = fd.read() fd.close() jobsDAO.modelSetFields(modelID, {'genDescription': expDescription}) # Run the experiment now try: runner = OPFModelRunner( modelID=modelID, jobID=jobID, predictedField=predictedField, experimentDir=experimentDir, reportKeyPatterns=reportKeys, optimizeKeyPattern=optimizeKey, jobsDAO=jobsDAO, modelCheckpointGUID=modelCheckpointGUID, logLevel=logLevel, predictionCacheMaxRecords=predictionCacheMaxRecords) signal.signal(signal.SIGINT, runner.handleWarningSignal) (completionReason, completionMsg) = runner.run() except InvalidConnectionException: raise except Exception, e: (completionReason, completionMsg) = _handleModelRunnerException(jobID, modelID, jobsDAO, experimentDir, logger, e) finally: # delete our temporary directory tree shutil.rmtree(experimentDir) signal.signal(signal.SIGINT, signal.default_int_handler) # Return completion reason and msg return (completionReason, completionMsg)
def rCopy(d, f=identityConversion, discardNoneKeys=True, deepCopy=True): """Recursively copies a dict and returns the result. Args: d: The dict to copy. f: A function to apply to values when copying that takes the value and the list of keys from the root of the dict to the value and returns a value for the new dict. discardNoneKeys: If True, discard key-value pairs when f returns None for the value. deepCopy: If True, all values in returned dict are true copies (not the same object). Returns: A new dict with keys and values from d replaced with the result of f. """ # Optionally deep copy the dict. if deepCopy: d = copy.deepcopy(d) newDict = {} toCopy = [(k, v, newDict, ()) for k, v in d.iteritems()] while len(toCopy) > 0: k, v, d, prevKeys = toCopy.pop() prevKeys = prevKeys + (k,) if isinstance(v, dict): d[k] = dict() toCopy[0:0] = [(innerK, innerV, d[k], prevKeys) for innerK, innerV in v.iteritems()] else: #print k, v, prevKeys newV = f(v, prevKeys) if not discardNoneKeys or newV is not None: d[k] = newV return newDict
def rApply(d, f): """Recursively applies f to the values in dict d. Args: d: The dict to recurse over. f: A function to apply to values in d that takes the value and a list of keys from the root of the dict to the value. """ remainingDicts = [(d, ())] while len(remainingDicts) > 0: current, prevKeys = remainingDicts.pop() for k, v in current.iteritems(): keys = prevKeys + (k,) if isinstance(v, dict): remainingDicts.insert(0, (v, keys)) else: f(v, keys)
def clippedObj(obj, maxElementSize=64): """ Return a clipped version of obj suitable for printing, This is useful when generating log messages by printing data structures, but don't want the message to be too long. If passed in a dict, list, or namedtuple, each element of the structure's string representation will be limited to 'maxElementSize' characters. This will return a new object where the string representation of each element has been truncated to fit within maxElementSize. """ # Is it a named tuple? if hasattr(obj, '_asdict'): obj = obj._asdict() # Printing a dict? if isinstance(obj, dict): objOut = dict() for key,val in obj.iteritems(): objOut[key] = clippedObj(val) # Printing a list? elif hasattr(obj, '__iter__'): objOut = [] for val in obj: objOut.append(clippedObj(val)) # Some other object else: objOut = str(obj) if len(objOut) > maxElementSize: objOut = objOut[0:maxElementSize] + '...' return objOut
def validate(value, **kwds): """ Validate a python value against json schema: validate(value, schemaPath) validate(value, schemaDict) value: python object to validate against the schema The json schema may be specified either as a path of the file containing the json schema or as a python dictionary using one of the following keywords as arguments: schemaPath: Path of file containing the json schema object. schemaDict: Python dictionary containing the json schema object Returns: nothing Raises: ValidationError when value fails json validation """ assert len(kwds.keys()) >= 1 assert 'schemaPath' in kwds or 'schemaDict' in kwds schemaDict = None if 'schemaPath' in kwds: schemaPath = kwds.pop('schemaPath') schemaDict = loadJsonValueFromFile(schemaPath) elif 'schemaDict' in kwds: schemaDict = kwds.pop('schemaDict') try: validictory.validate(value, schemaDict, **kwds) except validictory.ValidationError as e: raise ValidationError(e)
def loadJsonValueFromFile(inputFilePath): """ Loads a json value from a file and converts it to the corresponding python object. inputFilePath: Path of the json file; Returns: python value that represents the loaded json value """ with open(inputFilePath) as fileObj: value = json.load(fileObj) return value
def sortedJSONDumpS(obj): """ Return a JSON representation of obj with sorted keys on any embedded dicts. This insures that the same object will always be represented by the same string even if it contains dicts (where the sort order of the keys is normally undefined). """ itemStrs = [] if isinstance(obj, dict): items = obj.items() items.sort() for key, value in items: itemStrs.append('%s: %s' % (json.dumps(key), sortedJSONDumpS(value))) return '{%s}' % (', '.join(itemStrs)) elif hasattr(obj, '__iter__'): for val in obj: itemStrs.append(sortedJSONDumpS(val)) return '[%s]' % (', '.join(itemStrs)) else: return json.dumps(obj)
def tick(self): """ Activity tick handler; services all activities Returns: True if controlling iterator says it's okay to keep going; False to stop """ # Run activities whose time has come for act in self.__activities: if not act.iteratorHolder[0]: continue try: next(act.iteratorHolder[0]) except StopIteration: act.cb() if act.repeating: act.iteratorHolder[0] = iter(xrange(act.period)) else: act.iteratorHolder[0] = None return True
def rUpdate(original, updates): """Recursively updates the values in original with the values from updates.""" # Keep a list of the sub-dictionaries that need to be updated to avoid having # to use recursion (which could fail for dictionaries with a lot of nesting. dictPairs = [(original, updates)] while len(dictPairs) > 0: original, updates = dictPairs.pop() for k, v in updates.iteritems(): if k in original and isinstance(original[k], dict) and isinstance(v, dict): dictPairs.append((original[k], v)) else: original[k] = v
def dictDiffAndReport(da, db): """ Compares two python dictionaries at the top level and report differences, if any, to stdout da: first dictionary db: second dictionary Returns: The same value as returned by dictDiff() for the given args """ differences = dictDiff(da, db) if not differences: return differences if differences['inAButNotInB']: print ">>> inAButNotInB: %s" % differences['inAButNotInB'] if differences['inBButNotInA']: print ">>> inBButNotInA: %s" % differences['inBButNotInA'] for key in differences['differentValues']: print ">>> da[%s] != db[%s]" % (key, key) print "da[%s] = %r" % (key, da[key]) print "db[%s] = %r" % (key, db[key]) return differences
def dictDiff(da, db): """ Compares two python dictionaries at the top level and return differences da: first dictionary db: second dictionary Returns: None if dictionaries test equal; otherwise returns a dictionary as follows: { 'inAButNotInB': <sequence of keys that are in da but not in db> 'inBButNotInA': <sequence of keys that are in db but not in da> 'differentValues': <sequence of keys whose corresponding values differ between da and db> } """ different = False resultDict = dict() resultDict['inAButNotInB'] = set(da) - set(db) if resultDict['inAButNotInB']: different = True resultDict['inBButNotInA'] = set(db) - set(da) if resultDict['inBButNotInA']: different = True resultDict['differentValues'] = [] for key in (set(da) - resultDict['inAButNotInB']): comparisonResult = da[key] == db[key] if isinstance(comparisonResult, bool): isEqual = comparisonResult else: # This handles numpy arrays (but only at the top level) isEqual = comparisonResult.all() if not isEqual: resultDict['differentValues'].append(key) different = True assert (((resultDict['inAButNotInB'] or resultDict['inBButNotInA'] or resultDict['differentValues']) and different) or not different) return resultDict if different else None
def _seed(self, seed=-1): """ Initialize the random seed """ if seed != -1: self.random = NupicRandom(seed) else: self.random = NupicRandom()
def _newRep(self): """Generate a new and unique representation. Returns a numpy array of shape (n,). """ maxAttempts = 1000 for _ in xrange(maxAttempts): foundUnique = True population = numpy.arange(self.n, dtype=numpy.uint32) choices = numpy.arange(self.w, dtype=numpy.uint32) oneBits = sorted(self.random.sample(population, choices)) sdr = numpy.zeros(self.n, dtype='uint8') sdr[oneBits] = 1 for i in xrange(self.ncategories): if (sdr == self.sdrs[i]).all(): foundUnique = False break if foundUnique: break; if not foundUnique: raise RuntimeError("Error, could not find unique pattern %d after " "%d attempts" % (self.ncategories, maxAttempts)) return sdr
def getScalars(self, input): """ See method description in base.py """ if input == SENTINEL_VALUE_FOR_MISSING_DATA: return numpy.array([0]) index = self.categoryToIndex.get(input, None) if index is None: if self._learningEnabled: self._addCategory(input) index = self.ncategories - 1 else: # if not found, we encode category 0 index = 0 return numpy.array([index])
def decode(self, encoded, parentFieldName=''): """ See the function description in base.py """ assert (encoded[0:self.n] <= 1.0).all() resultString = "" resultRanges = [] overlaps = (self.sdrs * encoded[0:self.n]).sum(axis=1) if self.verbosity >= 2: print "Overlaps for decoding:" for i in xrange(0, self.ncategories): print "%d %s" % (overlaps[i], self.categories[i]) matchingCategories = (overlaps > self.thresholdOverlap).nonzero()[0] for index in matchingCategories: if resultString != "": resultString += " " resultString += str(self.categories[index]) resultRanges.append([int(index),int(index)]) if parentFieldName != '': fieldName = "%s.%s" % (parentFieldName, self.name) else: fieldName = self.name return ({fieldName: (resultRanges, resultString)}, [fieldName])
def _getTopDownMapping(self): """ Return the interal _topDownMappingM matrix used for handling the bucketInfo() and topDownCompute() methods. This is a matrix, one row per category (bucket) where each row contains the encoded output for that category. """ # ------------------------------------------------------------------------- # Do we need to build up our reverse mapping table? if self._topDownMappingM is None: # Each row represents an encoded output pattern self._topDownMappingM = SM32(self.ncategories, self.n) outputSpace = numpy.zeros(self.n, dtype=GetNTAReal()) for i in xrange(self.ncategories): self.encodeIntoArray(self.categories[i], outputSpace) self._topDownMappingM.setRowFromDense(i, outputSpace) return self._topDownMappingM
def getBucketInfo(self, buckets): """ See the function description in base.py """ if self.ncategories==0: return 0 topDownMappingM = self._getTopDownMapping() categoryIndex = buckets[0] category = self.categories[categoryIndex] encoding = topDownMappingM.getRow(categoryIndex) return [EncoderResult(value=category, scalar=categoryIndex, encoding=encoding)]
def topDownCompute(self, encoded): """ See the function description in base.py """ if self.ncategories==0: return 0 topDownMappingM = self._getTopDownMapping() categoryIndex = topDownMappingM.rightVecProd(encoded).argmax() category = self.categories[categoryIndex] encoding = topDownMappingM.getRow(categoryIndex) return EncoderResult(value=category, scalar=categoryIndex, encoding=encoding)
def getScalarNames(self, parentFieldName=''): """ See method description in base.py """ names = [] # This forms a name which is the concatenation of the parentFieldName # passed in and the encoder's own name. def _formFieldName(encoder): if parentFieldName == '': return encoder.name else: return '%s.%s' % (parentFieldName, encoder.name) # ------------------------------------------------------------------------- # Get the scalar values for each sub-field if self.seasonEncoder is not None: names.append(_formFieldName(self.seasonEncoder)) if self.dayOfWeekEncoder is not None: names.append(_formFieldName(self.dayOfWeekEncoder)) if self.customDaysEncoder is not None: names.append(_formFieldName(self.customDaysEncoder)) if self.weekendEncoder is not None: names.append(_formFieldName(self.weekendEncoder)) if self.holidayEncoder is not None: names.append(_formFieldName(self.holidayEncoder)) if self.timeOfDayEncoder is not None: names.append(_formFieldName(self.timeOfDayEncoder)) return names
def getEncodedValues(self, input): """ See method description in base.py """ if input == SENTINEL_VALUE_FOR_MISSING_DATA: return numpy.array([None]) assert isinstance(input, datetime.datetime) values = [] # ------------------------------------------------------------------------- # Get the scalar values for each sub-field timetuple = input.timetuple() timeOfDay = timetuple.tm_hour + float(timetuple.tm_min)/60.0 if self.seasonEncoder is not None: dayOfYear = timetuple.tm_yday # input.timetuple() computes the day of year 1 based, so convert to 0 based values.append(dayOfYear-1) if self.dayOfWeekEncoder is not None: dayOfWeek = timetuple.tm_wday + timeOfDay / 24.0 values.append(dayOfWeek) if self.weekendEncoder is not None: # saturday, sunday or friday evening if timetuple.tm_wday == 6 or timetuple.tm_wday == 5 \ or (timetuple.tm_wday == 4 and timeOfDay > 18): weekend = 1 else: weekend = 0 values.append(weekend) if self.customDaysEncoder is not None: if timetuple.tm_wday in self.customDays: customDay = 1 else: customDay = 0 values.append(customDay) if self.holidayEncoder is not None: # A "continuous" binary value. = 1 on the holiday itself and smooth ramp # 0->1 on the day before the holiday and 1->0 on the day after the holiday. # Currently the only holiday we know about is December 25 # holidays is a list of holidays that occur on a fixed date every year if len(self.holidays) == 0: holidays = [(12, 25)] else: holidays = self.holidays val = 0 for h in holidays: # hdate is midnight on the holiday if len(h) == 3: hdate = datetime.datetime(h[0], h[1], h[2], 0, 0, 0) else: hdate = datetime.datetime(timetuple.tm_year, h[0], h[1], 0, 0, 0) if input > hdate: diff = input - hdate if diff.days == 0: # return 1 on the holiday itself val = 1 break elif diff.days == 1: # ramp smoothly from 1 -> 0 on the next day val = 1.0 - (float(diff.seconds) / 86400) break else: diff = hdate - input if diff.days == 0: # ramp smoothly from 0 -> 1 on the previous day val = 1.0 - (float(diff.seconds) / 86400) values.append(val) if self.timeOfDayEncoder is not None: values.append(timeOfDay) return values
def getBucketIndices(self, input): """ See method description in base.py """ if input == SENTINEL_VALUE_FOR_MISSING_DATA: # Encoder each sub-field return [None] * len(self.encoders) else: assert isinstance(input, datetime.datetime) # Get the scalar values for each sub-field scalars = self.getScalars(input) # Encoder each sub-field result = [] for i in xrange(len(self.encoders)): (name, encoder, offset) = self.encoders[i] result.extend(encoder.getBucketIndices(scalars[i])) return result
def encodeIntoArray(self, input, output): """ See method description in base.py """ if input == SENTINEL_VALUE_FOR_MISSING_DATA: output[0:] = 0 else: if not isinstance(input, datetime.datetime): raise ValueError("Input is type %s, expected datetime. Value: %s" % ( type(input), str(input))) # Get the scalar values for each sub-field scalars = self.getScalars(input) # Encoder each sub-field for i in xrange(len(self.encoders)): (name, encoder, offset) = self.encoders[i] encoder.encodeIntoArray(scalars[i], output[offset:])
def getSpec(cls): """Return the Spec for IdentityRegion. """ spec = { "description":IdentityRegion.__doc__, "singleNodeOnly":True, "inputs":{ "in":{ "description":"The input vector.", "dataType":"Real32", "count":0, "required":True, "regionLevel":False, "isDefaultInput":True, "requireSplitterMap":False}, }, "outputs":{ "out":{ "description":"A copy of the input vector.", "dataType":"Real32", "count":0, "regionLevel":True, "isDefaultOutput":True}, }, "parameters":{ "dataWidth":{ "description":"Size of inputs", "accessMode":"Read", "dataType":"UInt32", "count":1, "constraints":""}, }, } return spec
def _setRandomEncoderResolution(minResolution=0.001): """ Given model params, figure out the correct resolution for the RandomDistributed encoder. Modifies params in place. """ encoder = ( model_params.MODEL_PARAMS["modelParams"]["sensorParams"]["encoders"]["value"] ) if encoder["type"] == "RandomDistributedScalarEncoder": rangePadding = abs(_INPUT_MAX - _INPUT_MIN) * 0.2 minValue = _INPUT_MIN - rangePadding maxValue = _INPUT_MAX + rangePadding resolution = max(minResolution, (maxValue - minValue) / encoder.pop("numBuckets") ) encoder["resolution"] = resolution
def addLabel(self, start, end, labelName): """ Add the label labelName to each record with record ROWID in range from start to end, noninclusive of end. This will recalculate all points from end to the last record stored in the internal cache of this classifier. """ if len(self.saved_states) == 0: raise HTMPredictionModelInvalidRangeError("Invalid supplied range for 'addLabel'. " "Model has no saved records.") startID = self.saved_states[0].ROWID clippedStart = max(0, start - startID) clippedEnd = max(0, min( len( self.saved_states) , end - startID)) if clippedEnd <= clippedStart: raise HTMPredictionModelInvalidRangeError("Invalid supplied range for 'addLabel'.", debugInfo={ 'requestRange': { 'startRecordID': start, 'endRecordID': end }, 'clippedRequestRange': { 'startRecordID': clippedStart, 'endRecordID': clippedEnd }, 'validRange': { 'startRecordID': startID, 'endRecordID': self.saved_states[len(self.saved_states)-1].ROWID }, 'numRecordsStored': len(self.saved_states) }) # Add label to range [clippedStart, clippedEnd) for state in self.saved_states[clippedStart:clippedEnd]: if labelName not in state.anomalyLabel: state.anomalyLabel.append(labelName) state.setByUser = True self._addRecordToKNN(state) assert len(self.saved_categories) > 0 # Recompute [end, ...) for state in self.saved_states[clippedEnd:]: self._updateState(state)
def removeLabels(self, start=None, end=None, labelFilter=None): """ Remove labels from each record with record ROWID in range from start to end, noninclusive of end. Removes all records if labelFilter is None, otherwise only removes the labels eqaul to labelFilter. This will recalculate all points from end to the last record stored in the internal cache of this classifier. """ if len(self.saved_states) == 0: raise HTMPredictionModelInvalidRangeError("Invalid supplied range for " "'removeLabels'. Model has no saved records.") startID = self.saved_states[0].ROWID clippedStart = 0 if start is None else max(0, start - startID) clippedEnd = len(self.saved_states) if end is None else \ max(0, min( len( self.saved_states) , end - startID)) if clippedEnd <= clippedStart: raise HTMPredictionModelInvalidRangeError("Invalid supplied range for " "'removeLabels'.", debugInfo={ 'requestRange': { 'startRecordID': start, 'endRecordID': end }, 'clippedRequestRange': { 'startRecordID': clippedStart, 'endRecordID': clippedEnd }, 'validRange': { 'startRecordID': startID, 'endRecordID': self.saved_states[len(self.saved_states)-1].ROWID }, 'numRecordsStored': len(self.saved_states) }) # Remove records within the cache recordsToDelete = [] for state in self.saved_states[clippedStart:clippedEnd]: if labelFilter is not None: if labelFilter in state.anomalyLabel: state.anomalyLabel.remove(labelFilter) else: state.anomalyLabel = [] state.setByUser = False recordsToDelete.append(state) self._deleteRecordsFromKNN(recordsToDelete) # Remove records not in cache self._deleteRangeFromKNN(start, end) # Recompute [clippedEnd, ...) for state in self.saved_states[clippedEnd:]: self._updateState(state) return {'status': 'success'}
def _addRecordToKNN(self, record): """ This method will add the record to the KNN classifier. """ classifier = self.htm_prediction_model._getAnomalyClassifier() knn = classifier.getSelf()._knn prototype_idx = classifier.getSelf().getParameter('categoryRecencyList') category = self._labelListToCategoryNumber(record.anomalyLabel) # If record is already in the classifier, overwrite its labeling if record.ROWID in prototype_idx: knn.prototypeSetCategory(record.ROWID, category) return # Learn this pattern in the knn pattern = self._getStateAnomalyVector(record) rowID = record.ROWID knn.learn(pattern, category, rowID=rowID)
def _deleteRecordsFromKNN(self, recordsToDelete): """ This method will remove the given records from the classifier. parameters ------------ recordsToDelete - list of records to delete from the classififier """ classifier = self.htm_prediction_model._getAnomalyClassifier() knn = classifier.getSelf()._knn prototype_idx = classifier.getSelf().getParameter('categoryRecencyList') idsToDelete = [r.ROWID for r in recordsToDelete if \ not r.setByUser and r.ROWID in prototype_idx] nProtos = knn._numPatterns knn.removeIds(idsToDelete) assert knn._numPatterns == nProtos - len(idsToDelete)
def _deleteRangeFromKNN(self, start=0, end=None): """ This method will remove any stored records within the range from start to end. Noninclusive of end. parameters ------------ start - integer representing the ROWID of the start of the deletion range, end - integer representing the ROWID of the end of the deletion range, if None, it will default to end. """ classifier = self.htm_prediction_model._getAnomalyClassifier() knn = classifier.getSelf()._knn prototype_idx = numpy.array( classifier.getSelf().getParameter('categoryRecencyList')) if end is None: end = prototype_idx.max() + 1 idsIdxToDelete = numpy.logical_and(prototype_idx >= start, prototype_idx < end) idsToDelete = prototype_idx[idsIdxToDelete] nProtos = knn._numPatterns knn.removeIds(idsToDelete.tolist()) assert knn._numPatterns == nProtos - len(idsToDelete)
def _recomputeRecordFromKNN(self, record): """ return the classified labeling of record """ inputs = { "categoryIn": [None], "bottomUpIn": self._getStateAnomalyVector(record), } outputs = {"categoriesOut": numpy.zeros((1,)), "bestPrototypeIndices":numpy.zeros((1,)), "categoryProbabilitiesOut":numpy.zeros((1,))} # Run inference only to capture state before learning classifier = self.htm_prediction_model._getAnomalyClassifier() knn = classifier.getSelf()._knn # Only use points before record to classify and after the wait period. classifier_indexes = \ numpy.array(classifier.getSelf().getParameter('categoryRecencyList')) valid_idx = numpy.where( (classifier_indexes >= self._autoDetectWaitRecords) & (classifier_indexes < record.ROWID) )[0].tolist() if len(valid_idx) == 0: return None classifier.setParameter('inferenceMode', True) classifier.setParameter('learningMode', False) classifier.getSelf().compute(inputs, outputs) classifier.setParameter('learningMode', True) classifier_distances = classifier.getSelf().getLatestDistances() valid_distances = classifier_distances[valid_idx] if valid_distances.min() <= self._classificationMaxDist: classifier_indexes_prev = classifier_indexes[valid_idx] rowID = classifier_indexes_prev[valid_distances.argmin()] indexID = numpy.where(classifier_indexes == rowID)[0][0] category = classifier.getSelf().getCategoryList()[indexID] return category return None
def _constructClassificationRecord(self): """ Construct a _HTMClassificationRecord based on the current state of the htm_prediction_model of this classifier. ***This will look into the internals of the model and may depend on the SP, TM, and KNNClassifier*** """ model = self.htm_prediction_model sp = model._getSPRegion() tm = model._getTPRegion() tpImp = tm.getSelf()._tfdr # Count the number of unpredicted columns activeColumns = sp.getOutputData("bottomUpOut").nonzero()[0] score = numpy.in1d(activeColumns, self._prevPredictedColumns).sum() score = (self._activeColumnCount - score)/float(self._activeColumnCount) spSize = sp.getParameter('activeOutputCount') tpSize = tm.getParameter('cellsPerColumn') * tm.getParameter('columnCount') classificationVector = numpy.array([]) if self._vectorType == 'tpc': # Classification Vector: [---TM Cells---] classificationVector = numpy.zeros(tpSize) activeCellMatrix = tpImp.getLearnActiveStateT().reshape(tpSize, 1) activeCellIdx = numpy.where(activeCellMatrix > 0)[0] if activeCellIdx.shape[0] > 0: classificationVector[numpy.array(activeCellIdx, dtype=numpy.uint16)] = 1 elif self._vectorType == 'sp_tpe': # Classification Vecotr: [---SP---|---(TM-SP)----] classificationVector = numpy.zeros(spSize+spSize) if activeColumns.shape[0] > 0: classificationVector[activeColumns] = 1.0 errorColumns = numpy.setdiff1d(self._prevPredictedColumns, activeColumns) if errorColumns.shape[0] > 0: errorColumnIndexes = ( numpy.array(errorColumns, dtype=numpy.uint16) + spSize ) classificationVector[errorColumnIndexes] = 1.0 else: raise TypeError("Classification vector type must be either 'tpc' or" " 'sp_tpe', current value is %s" % (self._vectorType)) # Store the state for next time step numPredictedCols = len(self._prevPredictedColumns) predictedColumns = tm.getOutputData("topDownOut").nonzero()[0] self._prevPredictedColumns = copy.deepcopy(predictedColumns) if self._anomalyVectorLength is None: self._anomalyVectorLength = len(classificationVector) result = _CLAClassificationRecord( ROWID=int(model.getParameter('__numRunCalls') - 1), #__numRunCalls called #at beginning of model.run anomalyScore=score, anomalyVector=classificationVector.nonzero()[0].tolist(), anomalyLabel=[] ) return result
def compute(self): """ Run an iteration of this anomaly classifier """ result = self._constructClassificationRecord() # Classify this point after waiting the classification delay if result.ROWID >= self._autoDetectWaitRecords: self._updateState(result) # Save new classification record and keep history as moving window self.saved_states.append(result) if len(self.saved_states) > self._history_length: self.saved_states.pop(0) return result
def setAutoDetectWaitRecords(self, waitRecords): """ Sets the autoDetectWaitRecords. """ if not isinstance(waitRecords, int): raise HTMPredictionModelInvalidArgument("Invalid argument type \'%s\'. WaitRecord " "must be a number." % (type(waitRecords))) if len(self.saved_states) > 0 and waitRecords < self.saved_states[0].ROWID: raise HTMPredictionModelInvalidArgument("Invalid value. autoDetectWaitRecord value " "must be valid record within output stream. Current minimum ROWID in " "output stream is %d." % (self.saved_states[0].ROWID)) self._autoDetectWaitRecords = waitRecords # Update all the states in the classifier's cache for state in self.saved_states: self._updateState(state)
def setAutoDetectThreshold(self, threshold): """ Sets the autoDetectThreshold. TODO: Ensure previously classified points outside of classifier are valid. """ if not (isinstance(threshold, float) or isinstance(threshold, int)): raise HTMPredictionModelInvalidArgument("Invalid argument type \'%s\'. threshold " "must be a number." % (type(threshold))) self._autoDetectThreshold = threshold # Update all the states in the classifier's cache for state in self.saved_states: self._updateState(state)
def _getAdditionalSpecs(spatialImp, kwargs={}): """Build the additional specs in three groups (for the inspector) Use the type of the default argument to set the Spec type, defaulting to 'Byte' for None and complex types Determines the spatial parameters based on the selected implementation. It defaults to SpatialPooler. """ typeNames = {int: 'UInt32', float: 'Real32', str: 'Byte', bool: 'bool', tuple: 'tuple'} def getArgType(arg): t = typeNames.get(type(arg), 'Byte') count = 0 if t == 'Byte' else 1 if t == 'tuple': t = typeNames.get(type(arg[0]), 'Byte') count = len(arg) if t == 'bool': t = 'UInt32' return (t, count) def getConstraints(arg): t = typeNames.get(type(arg), 'Byte') if t == 'Byte': return 'multiple' elif t == 'bool': return 'bool' else: return '' # Get arguments from spatial pooler constructors, figure out types of # variables and populate spatialSpec. SpatialClass = getSPClass(spatialImp) sArgTuples = _buildArgs(SpatialClass.__init__) spatialSpec = {} for argTuple in sArgTuples: d = dict( description=argTuple[1], accessMode='ReadWrite', dataType=getArgType(argTuple[2])[0], count=getArgType(argTuple[2])[1], constraints=getConstraints(argTuple[2])) spatialSpec[argTuple[0]] = d # Add special parameters that weren't handled automatically # Spatial parameters only! spatialSpec.update(dict( columnCount=dict( description='Total number of columns (coincidences).', accessMode='Read', dataType='UInt32', count=1, constraints=''), inputWidth=dict( description='Size of inputs to the SP.', accessMode='Read', dataType='UInt32', count=1, constraints=''), spInputNonZeros=dict( description='The indices of the non-zero inputs to the spatial pooler', accessMode='Read', dataType='UInt32', count=0, constraints=''), spOutputNonZeros=dict( description='The indices of the non-zero outputs from the spatial pooler', accessMode='Read', dataType='UInt32', count=0, constraints=''), spOverlapDistribution=dict( description="""The overlaps between the active output coincidences and the input. The overlap amounts for each coincidence are sorted from highest to lowest. """, accessMode='Read', dataType='Real32', count=0, constraints=''), sparseCoincidenceMatrix=dict( description='The coincidences, as a SparseMatrix', accessMode='Read', dataType='Byte', count=0, constraints=''), denseOutput=dict( description='Score for each coincidence.', accessMode='Read', dataType='Real32', count=0, constraints=''), spLearningStatsStr=dict( description="""String representation of dictionary containing a number of statistics related to learning.""", accessMode='Read', dataType='Byte', count=0, constraints='handle'), spatialImp=dict( description="""Which spatial pooler implementation to use. Set to either 'py', or 'cpp'. The 'cpp' implementation is optimized for speed in C++.""", accessMode='ReadWrite', dataType='Byte', count=0, constraints='enum: py, cpp'), )) # The last group is for parameters that aren't specific to spatial pooler otherSpec = dict( learningMode=dict( description='1 if the node is learning (default 1).', accessMode='ReadWrite', dataType='UInt32', count=1, constraints='bool'), inferenceMode=dict( description='1 if the node is inferring (default 0).', accessMode='ReadWrite', dataType='UInt32', count=1, constraints='bool'), anomalyMode=dict( description='1 if an anomaly score is being computed', accessMode='ReadWrite', dataType='UInt32', count=1, constraints='bool'), topDownMode=dict( description='1 if the node should do top down compute on the next call ' 'to compute into topDownOut (default 0).', accessMode='ReadWrite', dataType='UInt32', count=1, constraints='bool'), activeOutputCount=dict( description='Number of active elements in bottomUpOut output.', accessMode='Read', dataType='UInt32', count=1, constraints=''), logPathInput=dict( description='Optional name of input log file. If set, every input vector' ' will be logged to this file.', accessMode='ReadWrite', dataType='Byte', count=0, constraints=''), logPathOutput=dict( description='Optional name of output log file. If set, every output vector' ' will be logged to this file.', accessMode='ReadWrite', dataType='Byte', count=0, constraints=''), logPathOutputDense=dict( description='Optional name of output log file. If set, every output vector' ' will be logged to this file as a dense vector.', accessMode='ReadWrite', dataType='Byte', count=0, constraints=''), ) return spatialSpec, otherSpec
def _initializeEphemeralMembers(self): """ Initialize all ephemeral data members, and give the derived class the opportunity to do the same by invoking the virtual member _initEphemerals(), which is intended to be overridden. NOTE: this is used by both __init__ and __setstate__ code paths. """ for attrName in self._getEphemeralMembersBase(): if attrName != "_loaded": if hasattr(self, attrName): if self._loaded: # print self.__class__.__name__, "contains base class member '%s' " \ # "after loading." % attrName # TODO: Re-enable warning or turn into error in a future release. pass else: print self.__class__.__name__, "contains base class member '%s'" % \ attrName if not self._loaded: for attrName in self._getEphemeralMembersBase(): if attrName != "_loaded": # if hasattr(self, attrName): # import pdb; pdb.set_trace() assert not hasattr(self, attrName) else: assert hasattr(self, attrName) # Profiling information self._profileObj = None self._iterations = 0 # Let derived class initialize ephemerals self._initEphemerals() self._checkEphemeralMembers()
def initialize(self): """ Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.initialize`. """ # Zero out the spatial output in case it is requested self._spatialPoolerOutput = numpy.zeros(self.columnCount, dtype=GetNTAReal()) # Zero out the rfInput in case it is requested self._spatialPoolerInput = numpy.zeros((1, self.inputWidth), dtype=GetNTAReal()) # Allocate the spatial pooler self._allocateSpatialFDR(None)
def _allocateSpatialFDR(self, rfInput): """Allocate the spatial pooler instance.""" if self._sfdr: return # Retrieve the necessary extra arguments that were handled automatically autoArgs = dict((name, getattr(self, name)) for name in self._spatialArgNames) # Instantiate the spatial pooler class. if ( (self.SpatialClass == CPPSpatialPooler) or (self.SpatialClass == PYSpatialPooler) ): autoArgs['columnDimensions'] = [self.columnCount] autoArgs['inputDimensions'] = [self.inputWidth] autoArgs['potentialRadius'] = self.inputWidth self._sfdr = self.SpatialClass( **autoArgs )
def compute(self, inputs, outputs): """ Run one iteration, profiling it if requested. :param inputs: (dict) mapping region input names to numpy.array values :param outputs: (dict) mapping region output names to numpy.arrays that should be populated with output values by this method """ # Uncomment this to find out who is generating divide by 0, or other numpy warnings # numpy.seterr(divide='raise', invalid='raise', over='raise') # Modify this line to turn on profiling for a given node. The results file # ('hotshot.stats') will be sensed and printed out by the vision framework's # RunInference.py script at the end of inference. # Also uncomment the hotshot import at the top of this file. if False and self.learningMode \ and self._iterations > 0 and self._iterations <= 10: import hotshot if self._iterations == 10: print "\n Collecting and sorting internal node profiling stats generated by hotshot..." stats = hotshot.stats.load("hotshot.stats") stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats() # The guts of the compute are contained in the _compute() call so that we # can profile it if requested. if self._profileObj is None: print "\n Preparing to capture profile using hotshot..." if os.path.exists('hotshot.stats'): # There is an old hotshot stats profile left over, remove it. os.remove('hotshot.stats') self._profileObj = hotshot.Profile("hotshot.stats", 1, 1) # filename, lineevents, linetimings self._profileObj.runcall(self._compute, *[inputs, outputs]) else: self._compute(inputs, outputs)
def _compute(self, inputs, outputs): """ Run one iteration of SPRegion's compute """ #if self.topDownMode and (not 'topDownIn' in inputs): # raise RuntimeError("The input topDownIn must be linked in if " # "topDownMode is True") if self._sfdr is None: raise RuntimeError("Spatial pooler has not been initialized") if not self.topDownMode: # # BOTTOM-UP compute # self._iterations += 1 # Get our inputs into numpy arrays buInputVector = inputs['bottomUpIn'] resetSignal = False if 'resetIn' in inputs: assert len(inputs['resetIn']) == 1 resetSignal = inputs['resetIn'][0] != 0 # Perform inference and/or learning rfOutput = self._doBottomUpCompute( rfInput = buInputVector.reshape((1,buInputVector.size)), resetSignal = resetSignal ) outputs['bottomUpOut'][:] = rfOutput.flat else: # # TOP-DOWN inference # topDownIn = inputs.get('topDownIn',None) spatialTopDownOut, temporalTopDownOut = self._doTopDownInfer(topDownIn) outputs['spatialTopDownOut'][:] = spatialTopDownOut if temporalTopDownOut is not None: outputs['temporalTopDownOut'][:] = temporalTopDownOut # OBSOLETE outputs['anomalyScore'][:] = 0
def _doBottomUpCompute(self, rfInput, resetSignal): """ Do one iteration of inference and/or learning and return the result Parameters: -------------------------------------------- rfInput: Input vector. Shape is: (1, inputVectorLen). resetSignal: True if reset is asserted """ # Conditional compute break self._conditionalBreak() # Save the rfInput for the spInputNonZeros parameter self._spatialPoolerInput = rfInput.reshape(-1) assert(rfInput.shape[0] == 1) # Run inference using the spatial pooler. We learn on the coincidences only # if we are in learning mode and trainingStep is set appropriately. # Run SFDR bottom-up compute and cache output in self._spatialPoolerOutput inputVector = numpy.array(rfInput[0]).astype('uint32') outputVector = numpy.zeros(self._sfdr.getNumColumns()).astype('uint32') self._sfdr.compute(inputVector, self.learningMode, outputVector) self._spatialPoolerOutput[:] = outputVector[:] # Direct logging of SP outputs if requested if self._fpLogSP: output = self._spatialPoolerOutput.reshape(-1) outputNZ = output.nonzero()[0] outStr = " ".join(["%d" % int(token) for token in outputNZ]) print >>self._fpLogSP, output.size, outStr # Direct logging of SP inputs if self._fpLogSPInput: output = rfInput.reshape(-1) outputNZ = output.nonzero()[0] outStr = " ".join(["%d" % int(token) for token in outputNZ]) print >>self._fpLogSPInput, output.size, outStr return self._spatialPoolerOutput
def getBaseSpec(cls): """ Doesn't include the spatial, temporal and other parameters :returns: (dict) The base Spec for SPRegion. """ spec = dict( description=SPRegion.__doc__, singleNodeOnly=True, inputs=dict( bottomUpIn=dict( description="""The input vector.""", dataType='Real32', count=0, required=True, regionLevel=False, isDefaultInput=True, requireSplitterMap=False), resetIn=dict( description="""A boolean flag that indicates whether or not the input vector received in this compute cycle represents the start of a new temporal sequence.""", dataType='Real32', count=1, required=False, regionLevel=True, isDefaultInput=False, requireSplitterMap=False), topDownIn=dict( description="""The top-down input signal, generated from feedback from upper levels""", dataType='Real32', count=0, required = False, regionLevel=True, isDefaultInput=False, requireSplitterMap=False), sequenceIdIn=dict( description="Sequence ID", dataType='UInt64', count=1, required=False, regionLevel=True, isDefaultInput=False, requireSplitterMap=False), ), outputs=dict( bottomUpOut=dict( description="""The output signal generated from the bottom-up inputs from lower levels.""", dataType='Real32', count=0, regionLevel=True, isDefaultOutput=True), topDownOut=dict( description="""The top-down output signal, generated from feedback from upper levels""", dataType='Real32', count=0, regionLevel=True, isDefaultOutput=False), spatialTopDownOut = dict( description="""The top-down output, generated only from the current SP output. This can be used to evaluate how well the SP is representing the inputs independent of the TM.""", dataType='Real32', count=0, regionLevel=True, isDefaultOutput=False), temporalTopDownOut = dict( description="""The top-down output, generated only from the current TM output feedback down through the SP.""", dataType='Real32', count=0, regionLevel=True, isDefaultOutput=False), anomalyScore = dict( description="""The score for how 'anomalous' (i.e. rare) this spatial input pattern is. Higher values are increasingly rare""", dataType='Real32', count=1, regionLevel=True, isDefaultOutput=False), ), parameters=dict( breakPdb=dict( description='Set to 1 to stop in the pdb debugger on the next compute', dataType='UInt32', count=1, constraints='bool', defaultValue=0, accessMode='ReadWrite'), breakKomodo=dict( description='Set to 1 to stop in the Komodo debugger on the next compute', dataType='UInt32', count=1, constraints='bool', defaultValue=0, accessMode='ReadWrite'), ), ) return spec
def getSpec(cls): """ Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.getSpec`. The parameters collection is constructed based on the parameters specified by the various components (spatialSpec, temporalSpec and otherSpec) """ spec = cls.getBaseSpec() s, o = _getAdditionalSpecs(spatialImp=getDefaultSPImp()) spec['parameters'].update(s) spec['parameters'].update(o) return spec
def getParameter(self, parameterName, index=-1): """ Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.getParameter`. Most parameters are handled automatically by PyRegion's parameter get mechanism. The ones that need special treatment are explicitly handled here. """ if parameterName == 'activeOutputCount': return self.columnCount elif parameterName == 'spatialPoolerInput': return list(self._spatialPoolerInput.reshape(-1)) elif parameterName == 'spatialPoolerOutput': return list(self._spatialPoolerOutput) elif parameterName == 'spNumActiveOutputs': return len(self._spatialPoolerOutput.nonzero()[0]) elif parameterName == 'spOutputNonZeros': return [len(self._spatialPoolerOutput)] + \ list(self._spatialPoolerOutput.nonzero()[0]) elif parameterName == 'spInputNonZeros': import pdb; pdb.set_trace() return [len(self._spatialPoolerInput)] + \ list(self._spatialPoolerInput.nonzero()[0]) elif parameterName == 'spLearningStatsStr': try: return str(self._sfdr.getLearningStats()) except: return str(dict()) else: return PyRegion.getParameter(self, parameterName, index)
def setParameter(self, parameterName, index, parameterValue): """ Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.setParameter`. Set the value of a Spec parameter. Most parameters are handled automatically by PyRegion's parameter set mechanism. The ones that need special treatment are explicitly handled here. """ if parameterName in self._spatialArgNames: setattr(self._sfdr, parameterName, parameterValue) elif parameterName == "logPathInput": self.logPathInput = parameterValue # Close any existing log file if self._fpLogSPInput: self._fpLogSPInput.close() self._fpLogSPInput = None # Open a new log file if parameterValue: self._fpLogSPInput = open(self.logPathInput, 'w') elif parameterName == "logPathOutput": self.logPathOutput = parameterValue # Close any existing log file if self._fpLogSP: self._fpLogSP.close() self._fpLogSP = None # Open a new log file if parameterValue: self._fpLogSP = open(self.logPathOutput, 'w') elif parameterName == "logPathOutputDense": self.logPathOutputDense = parameterValue # Close any existing log file if self._fpLogSPDense: self._fpLogSPDense.close() self._fpLogSPDense = None # Open a new log file if parameterValue: self._fpLogSPDense = open(self.logPathOutputDense, 'w') elif hasattr(self, parameterName): setattr(self, parameterName, parameterValue) else: raise Exception('Unknown parameter: ' + parameterName)
def writeToProto(self, proto): """ Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.writeToProto`. Write state to proto object. :param proto: SPRegionProto capnproto object """ proto.spatialImp = self.spatialImp proto.columnCount = self.columnCount proto.inputWidth = self.inputWidth proto.learningMode = 1 if self.learningMode else 0 proto.inferenceMode = 1 if self.inferenceMode else 0 proto.anomalyMode = 1 if self.anomalyMode else 0 proto.topDownMode = 1 if self.topDownMode else 0 self._sfdr.write(proto.spatialPooler)
def readFromProto(cls, proto): """ Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.readFromProto`. Read state from proto object. :param proto: SPRegionProto capnproto object """ instance = cls(proto.columnCount, proto.inputWidth) instance.spatialImp = proto.spatialImp instance.learningMode = proto.learningMode instance.inferenceMode = proto.inferenceMode instance.anomalyMode = proto.anomalyMode instance.topDownMode = proto.topDownMode spatialImp = proto.spatialImp instance._sfdr = getSPClass(spatialImp).read(proto.spatialPooler) return instance
def _initEphemerals(self): """ Initialize all ephemerals used by derived classes. """ if hasattr(self, '_sfdr') and self._sfdr: self._spatialPoolerOutput = numpy.zeros(self.columnCount, dtype=GetNTAReal()) else: self._spatialPoolerOutput = None # Will be filled in initInNetwork # Direct logging support (faster than node watch) self._fpLogSPInput = None self._fpLogSP = None self._fpLogSPDense = None self.logPathInput = "" self.logPathOutput = "" self.logPathOutputDense = ""
def getParameterArrayCount(self, name, index): """ Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.getParameterArrayCount`. TODO: as a temporary hack, getParameterArrayCount checks to see if there's a variable, private or not, with that name. If so, it returns the value of the variable. """ p = self.getParameter(name) if (not hasattr(p, '__len__')): raise Exception("Attempt to access parameter '%s' as an array but it is not an array" % name) return len(p)
def getParameterArray(self, name, index, a): """ Overrides :meth:`~nupic.bindings.regions.PyRegion.PyRegion.getParameterArray`. TODO: as a temporary hack, getParameterArray checks to see if there's a variable, private or not, with that name. If so, it returns the value of the variable. """ p = self.getParameter(name) if (not hasattr(p, '__len__')): raise Exception("Attempt to access parameter '%s' as an array but it is not an array" % name) if len(p) > 0: a[:] = p[:]
def _cacheSequenceInfoType(self): """Figure out whether reset, sequenceId, both or neither are present in the data. Compute once instead of every time. Taken from filesource.py""" hasReset = self.resetFieldName is not None hasSequenceId = self.sequenceIdFieldName is not None if hasReset and not hasSequenceId: self._sequenceInfoType = self.SEQUENCEINFO_RESET_ONLY self._prevSequenceId = 0 elif not hasReset and hasSequenceId: self._sequenceInfoType = self.SEQUENCEINFO_SEQUENCEID_ONLY self._prevSequenceId = None elif hasReset and hasSequenceId: self._sequenceInfoType = self.SEQUENCEINFO_BOTH else: self._sequenceInfoType = self.SEQUENCEINFO_NONE
def _getTPClass(temporalImp): """ Return the class corresponding to the given temporalImp string """ if temporalImp == 'py': return backtracking_tm.BacktrackingTM elif temporalImp == 'cpp': return backtracking_tm_cpp.BacktrackingTMCPP elif temporalImp == 'tm_py': return backtracking_tm_shim.TMShim elif temporalImp == 'tm_cpp': return backtracking_tm_shim.TMCPPShim elif temporalImp == 'monitored_tm_py': return backtracking_tm_shim.MonitoredTMShim else: raise RuntimeError("Invalid temporalImp '%s'. Legal values are: 'py', " "'cpp', 'tm_py', 'monitored_tm_py'" % (temporalImp))
def _buildArgs(f, self=None, kwargs={}): """ Get the default arguments from the function and assign as instance vars. Return a list of 3-tuples with (name, description, defaultValue) for each argument to the function. Assigns all arguments to the function as instance variables of TMRegion. If the argument was not provided, uses the default value. Pops any values from kwargs that go to the function. """ # Get the name, description, and default value for each argument argTuples = getArgumentDescriptions(f) argTuples = argTuples[1:] # Remove 'self' # Get the names of the parameters to our own constructor and remove them # Check for _originial_init first, because if LockAttributesMixin is used, # __init__'s signature will be just (self, *args, **kw), but # _original_init is created with the original signature #init = getattr(self, '_original_init', self.__init__) init = TMRegion.__init__ ourArgNames = [t[0] for t in getArgumentDescriptions(init)] # Also remove a few other names that aren't in our constructor but are # computed automatically (e.g. numberOfCols for the TM) ourArgNames += [ 'numberOfCols', # TM ] for argTuple in argTuples[:]: if argTuple[0] in ourArgNames: argTuples.remove(argTuple) # Build the dictionary of arguments if self: for argTuple in argTuples: argName = argTuple[0] if argName in kwargs: # Argument was provided argValue = kwargs.pop(argName) else: # Argument was not provided; use the default value if there is one, and # raise an exception otherwise if len(argTuple) == 2: # No default value raise TypeError("Must provide '%s'" % argName) argValue = argTuple[2] # Set as an instance variable if 'self' was passed in setattr(self, argName, argValue) return argTuples