def run(self, inputRecord): """Run one iteration of this model. Args: inputRecord: A record object formatted according to nupic.data.FileSource.getNext() result format. Returns: A ModelResult named tuple (see opf_utils.py). The contents of ModelResult.inferences depends on the specific inference type of this model, which can be queried by getInferenceType(). TODO: Implement getInferenceType()? """ # set the results. note that there is no translation to sensorInput results = super(PreviousValueModel, self).run(inputRecord) results.sensorInput = opf_utils.SensorInput(dataRow= \ [inputRecord[fn] for fn in self._fieldNames]) # select the current value for the prediction with probablity of 1 results.inferences = {opf_utils.InferenceElement.multiStepBestPredictions : \ dict((steps, inputRecord[self._predictedField]) \ for steps in self._predictionSteps), opf_utils.InferenceElement.multiStepPredictions : \ dict((steps, {inputRecord[self._predictedField] : 1}) \ for steps in self._predictionSteps) } # set the next step prediction if step of 1 is selected if 1 in self._predictionSteps: results.inferences[opf_utils.InferenceElement.prediction] = \ inputRecord[self._predictedField] return results
def run(self, inputRecord): results = super(TwoGramModel, self).run(inputRecord) # Set up the lists of values, defaults, and encoded values. values = [inputRecord[k] for k in self._fieldNames] defaults = ['' if type(v) == str else 0 for v in values] inputFieldEncodings = self._encoder.encodeEachField(inputRecord) inputBuckets = self._encoder.getBucketIndices(inputRecord) results.sensorInput = opf_utils.SensorInput( dataRow=values, dataEncodings=inputFieldEncodings, sequenceReset=int(self._reset)) # Keep track of the last value associated with each encoded value for that # predictions can be returned in the original value format. for value, bucket in zip(values, inputBuckets): self._hashToValueDict[bucket] = value # Update the two-gram dict if learning is enabled. for bucket, prevValue, twoGramDict in zip(inputBuckets, self._prevValues, self._twoGramDicts): if self._learningEnabled and not self._reset: if prevValue not in twoGramDict: twoGramDict[prevValue] = collections.defaultdict(int) twoGramDict[prevValue][bucket] += 1 # Populate the results.inferences dict with the predictions and encoded # predictions. predictions = [] encodedPredictions = [] for bucket, twoGramDict, default, fieldName in (zip( inputBuckets, self._twoGramDicts, defaults, self._fieldNames)): if bucket in twoGramDict: probabilities = list(twoGramDict[bucket].items()) prediction = self._hashToValueDict[max(probabilities, key=lambda x: x[1])[0]] predictions.append(prediction) encodedPredictions.append( self._encoder.encodeField(fieldName, prediction)) else: predictions.append(default) encodedPredictions.append( self._encoder.encodeField(fieldName, default)) results.inferences = dict() results.inferences[opf_utils.InferenceElement.prediction] = predictions results.inferences[ opf_utils.InferenceElement.encodings] = encodedPredictions self._prevValues = inputBuckets self._reset = False return results
def run(self, inputRecord): # set the results. note that there is no translation to sensorInput results = super(PreviousValueModel, self).run(inputRecord) results.sensorInput = opf_utils.SensorInput(dataRow= \ [inputRecord[fn] for fn in self._fieldNames]) # select the current value for the prediction with probablity of 1 results.inferences = {opf_utils.InferenceElement.multiStepBestPredictions : \ dict((steps, inputRecord[self._predictedField]) \ for steps in self._predictionSteps), opf_utils.InferenceElement.multiStepPredictions : \ dict((steps, {inputRecord[self._predictedField] : 1}) \ for steps in self._predictionSteps) } # set the next step prediction if step of 1 is selected if 1 in self._predictionSteps: results.inferences[opf_utils.InferenceElement.prediction] = \ inputRecord[self._predictedField] return results
def run(self, inputRecord): """Run one iteration of this model. Args: inputRecord: A record object formatted according to nupic.data.FileSource.getNext() result format. Returns: A ModelResult named tuple (see opf_utils.py). The contents of ModelResult.inferences depends on the specific inference type of this model, which can be queried by getInferenceType(). TODO: Implement getInferenceType()? """ results = super(TwoGramModel, self).run(inputRecord) # Set up the lists of values, defaults, and encoded values. values = [inputRecord[k] for k in self._fieldNames] defaults = ['' if type(v) == str else 0 for v in values] inputFieldEncodings = self._encoder.encodeEachField(inputRecord) inputBuckets = self._encoder.getBucketIndices(inputRecord) results.sensorInput = opf_utils.SensorInput( dataRow=values, dataEncodings=inputFieldEncodings, sequenceReset=int(self._reset)) # Keep track of the last value associated with each encoded value for that # predictions can be returned in the original value format. for value, bucket in itertools.izip(values, inputBuckets): self._hashToValueDict[bucket] = value # Update the two-gram dict if learning is enabled. for bucket, prevValue, twoGramDict in itertools.izip( inputBuckets, self._prevValues, self._twoGramDicts): if self._learningEnabled and not self._reset: if prevValue not in twoGramDict: twoGramDict[prevValue] = collections.defaultdict(int) twoGramDict[prevValue][bucket] += 1 # Populate the results.inferences dict with the predictions and encoded # predictions. predictions = [] encodedPredictions = [] for bucket, twoGramDict, default, fieldName in ( itertools.izip(inputBuckets, self._twoGramDicts, defaults, self._fieldNames)): if bucket in twoGramDict: probabilities = twoGramDict[bucket].items() prediction = self._hashToValueDict[ max(probabilities, key=lambda x: x[1])[0]] predictions.append(prediction) encodedPredictions.append(self._encoder.encodeField(fieldName, prediction)) else: predictions.append(default) encodedPredictions.append(self._encoder.encodeField(fieldName, default)) results.inferences = dict() results.inferences[opf_utils.InferenceElement.prediction] = predictions results.inferences[opf_utils.InferenceElement.encodings] = encodedPredictions self._prevValues = inputBuckets self._reset = False return results