def testCreateFromString(self, ceptMock): term = Term() term = term.createFromString("fox") # Check that our mock object was called ceptMock.assert_called_with("fox") # Check that we have a Term type self.assertIsInstance(term, Term)
def testFeedTermReturnsTerm(self, mockBitmap, mockTPCompute): model = self.Model() term = Term().createFromString("test") result = model.feedTerm(term) self.assertIsInstance(result, Term, "Result is not a Term")
def testFeedTermReturnsTerm(self): model = Model() term = Term().createFromString("test") result = model.feedTerm(term) self.assertIsInstance(result, Term, "Result is not a Term")
def feedTerm(self, term): """ Feed a Term to model, returning next predicted Term """ tp = self.tp array = numpy.array(term.toArray(), dtype="uint32") tp.compute(array, enableLearn=True, computeInfOutput=True) predictedCells = tp.getPredictedState() predictedColumns = predictedCells.max(axis=1) predictedBitmap = predictedColumns.nonzero()[0].tolist() return Term().createFromBitmap(predictedBitmap)
def readFile(filename, model, resetSequences=False, format=None): if model.canCheckpoint() and model.hasCheckpoint(): model.load() exclusions = ('!', '.', ':', ',', '"', '\'', '\n') if format == "csv": fmt = "%s,%s,%s,%s,%s,%s,%s" else: # No format specified, so pretty print it fmt = "%10s | %10s | %20s | %20s | %20s | %20s | %20s" print(fmt % ("Sequence #", "Term #", "Current Term", "Predicted Term 1", "Predicted Term 2", "Predicted Term 3", "Predicted Term 3")) print("-----------------------------------" "-----------------------------------" "-----------------------------------" "-----------------------------------") s = 1 t = 1 with open(filename) as f: for line in f: line = "".join([c for c in line if c not in exclusions]) strings = line.split(" ") for string in strings: if not len(string): continue term = Term().createFromString(string) prediction = model.feedTerm(term) closestStrings = prediction.closestStrings() closestStringsIter = iter(closestStrings) print(fmt % (s, t, string, next(closestStringsIter, ""), next(closestStringsIter, ""), next(closestStringsIter, ""), next(closestStringsIter, ""))) t += 1 if model.canCheckpoint(): model.save() if resetSequences: model.resetSequence() s += 1 t = 1
def test_createFromString(self): # Test enablePlaceholder term = Term().createFromString("thisisaninvalidterm", enablePlaceholder=False) self.assertEqual(sum(term.toArray()), 0) term = Term().createFromString("thisisaninvalidterm", enablePlaceholder=True) self.assertGreater(sum(term.toArray()), 0) self.assertGreater(term.sparsity, 0) placeholder = term.bitmap # Make sure we get the same placeholder back for the same term term = Term().createFromString("thisisaninvalidterm", enablePlaceholder=True) self.assertEqual(term.bitmap, placeholder) # Make sure we get a different placeholder back for a different term term = Term().createFromString("differentinvalidterm", enablePlaceholder=True) self.assertNotEqual(term.bitmap, placeholder)
def test_training(self): term0 = Term().createFromString("the") term1 = Term().createFromString("fox") term2 = Term().createFromString("eats") term3 = Term().createFromString("rodent") model = Model() prediction = model.feedTerm(term0) self.assertFalse(len(prediction.bitmap)) for _ in range(5): model.feedTerm(term1) model.feedTerm(term2) model.feedTerm(term3) model.resetSequence() model.feedTerm(term1) prediction = model.feedTerm(term2) self.assertEqual(prediction.closestString(), "rodent")
def POST(self, uid, string): model = getModel(uid) term = Term().createFromString(string) prediction = model.feedTerm(term) model.save() web.header('Content-Type', 'application/json') return json.dumps([{ "type": "term", "term": { "string": prediction.closestString() } }])
def test_checkpoint(self): model1 = Model(checkpointDir=MODEL_CHECKPOINT_DIR) term = Term().createFromString("fox") for _ in range(5): prediction = model1.feedTerm(term) self.assertTrue(len(prediction.bitmap)) model1.save() model2 = Model(checkpointDir=MODEL_CHECKPOINT_DIR) model2.load() prediction = model2.feedTerm(term) self.assertTrue(len(prediction.bitmap))
def POST(self, uid, string): model = getModel(uid) term = Term().createFromString(string) learning = False if web.input().learning == "false" else True prediction = model.feedTerm(term, learning) model.save() closestStrings = prediction.closestStrings() web.header('Content-Type', 'application/json') return json.dumps([{ "type": "term", "term": { "string": string } } for string in closestStrings])
def feedTerm(self, term, learn=True): """ Feed a Term to model, returning next predicted Term """ tp = self.tp array = numpy.array(term.toArray(), dtype="uint32") tp.resetStats() tp.compute(array, enableLearn = learn, computeInfOutput = True) #print "ret: " + repr(ret) #if ret.all() == array.all(): # print "EQUAL to input" ret = tp.getStats() #ret = tp.printStates() print "ret: " + repr(ret) print print print "*****************************************" predictedCells = tp.getPredictedState() predictedColumns = predictedCells.max(axis=1) predictedBitmap = predictedColumns.nonzero()[0].tolist() return Term().createFromBitmap(predictedBitmap)
def readFile(filename, model, resetSequences=False): if model.canCheckpoint() and model.hasCheckpoint(): model.load() exclusions = ('!', '.', ':', ',', '"', '\'', '\n') print("%10s | %10s | %20s | %20s" % ("Sequence #", "Term #", "Current Term", "Predicted Term")) print("-----------------------------------" "-----------------------------------") s = 1 t = 1 with open(filename) as f: for line in f: line = "".join([c for c in line if c not in exclusions]) strings = line.split(" ") for string in strings: if not len(string): continue term = Term().createFromString(string) prediction = model.feedTerm(term) print("%10i | %10i | %20s | %20s" % (s, t, string, prediction.closestString())) t += 1 if model.canCheckpoint(): model.save() if resetSequences: model.resetSequence() s += 1 t = 1