def testGetClosestStrings(self): """ Type check """ cept = Cept() response = cept.getBitmap("snake") result = cept.getClosestStrings(response['positions']) self.assertTrue(type(result), 'list')
class Term(): def __init__(self): self.bitmap = None self.cept = Cept() def createFromString(self, string): self.bitmap = self.cept.getBitmap(string)['positions'] return self def createFromBitmap(self, bitmap): self.bitmap = bitmap return self def toArray(self): array = [0] * 128 * 128 for i in self.bitmap: array[i] = 1 return array def closestString(self): if not len(self.bitmap): return "" closestStrings = self.cept.getClosestStrings(self.bitmap) if not len(closestStrings): return "" return closestStrings[0]['term']
def testSamePositions(self): """ Test that the SDR we get from the server hasn't changed, using default settings """ cept = Cept() httpResponse = cept.getBitmap("fox") self.assertEqual(set(httpResponse['positions']), set(foxHardCodedResponse['positions']))
def testGetBitmap(self): """ Type check what we get back from the cept object """ cept = Cept() response = cept.getBitmap("fox") self.assertLessEqual(set(("width", "positions", "sparsity", "height")), set(response)) self.assertIsInstance(response['positions'], list, "Positions field is not a list") self.assertIsInstance(response['sparsity'], float, "Sparsity field is not a list") self.assertIsInstance(response['width'], int, "Width field is not an int") self.assertIsInstance(response['height'], int, "Height field is not an int")
class Term(): def __init__(self): self.bitmap = None self.cept = Cept() def createFromString(self, string): self.bitmap = self.cept.getBitmap(string)['positions'] return self def createFromBitmap(self, bitmap): self.bitmap = bitmap return self def toArray(self): array = [0] * 128 * 128 for i in self.bitmap: array[i] = 1 return array def closestString(self): print "closestString: " + repr(self.bitmap) if not len(self.bitmap): return "" closestStrings = self.cept.getClosestStrings(self.bitmap) if not len(closestStrings): return "" return closestStrings[0]['term']
def __init__(self): self.bitmap = None self.cept = Cept()
def testExceptionIfAPIKeyNotPresent(self, mockOS): with self.assertRaises(Exception) as e: cept = Cept() self.assertIn("Missing API key.", e.exception)
def testAPIKeyPresent(self): with patch.dict('os.environ', {'CEPT_API_KEY': 'apikey123'}): cept = Cept()
def __init__(self): self.bitmap = None self.sparsity = None self.width = None self.height = None self.cept = Cept()
class Term(): def __init__(self): self.bitmap = None self.sparsity = None self.width = None self.height = None self.cept = Cept() def __repr__(self): return termJSONEncoder(self) def createFromString(self, string, enablePlaceholder=True): response = self.cept.getBitmap(string) self.bitmap = response['positions'] self.sparsity = response['sparsity'] self.width = response['width'] self.height = response['height'] if enablePlaceholder and self.sparsity == 0: state = random.getstate() random.seed(string) num = self.width * self.height bitmap = random.sample(range(num), int(TARGET_SPARSITY * num / 100)) self.createFromBitmap(bitmap, self.width, self.height) random.setstate(state) return self def createFromBitmap(self, bitmap, width, height): self.bitmap = bitmap self.width = width self.height = height self.sparsity = (100.0 * len(bitmap)) / (width * height) return self def compare(self, term): """ Compare self with the provided term. Calls CEPT compare and returns the corresponding dict. """ return self.cept.client.compare(self.bitmap, term.bitmap) def toArray(self): array = [0] * self.width * self.height for i in self.bitmap: array[i] = 1 return array def closestStrings(self): if not len(self.bitmap): return [] return [ result['term'] for result in self.cept.getClosestStrings(self.bitmap) ] def closestString(self): closestStrings = self.closestStrings() if not len(closestStrings): return "" return closestStrings[0]
class Term(): def __init__(self): self.bitmap = None self.sparsity = None self.width = None self.height = None self.cept = Cept() def __repr__(self): return termJSONEncoder(self) def createFromString(self, string, enablePlaceholder=True): response = self.cept.getBitmap(string) self.bitmap = response['positions'] self.sparsity = response['sparsity'] self.width = response['width'] self.height = response['height'] if enablePlaceholder and self.sparsity == 0: state = random.getstate() random.seed(string) num = self.width * self.height bitmap = random.sample(range(num), int(TARGET_SPARSITY * num / 100)) self.createFromBitmap(bitmap, self.width, self.height) random.setstate(state) return self def createFromBitmap(self, bitmap, width, height): self.bitmap = bitmap self.width = width self.height = height self.sparsity = (100.0 * len(bitmap)) / (width*height) return self def compare(self, term): """ Compare self with the provided term. Calls CEPT compare and returns the corresponding dict. """ return self.cept.client.compare(self.bitmap, term.bitmap) def toArray(self): array = [0] * self.width * self.height for i in self.bitmap: array[i] = 1 return array def closestStrings(self): if not len(self.bitmap): return [] return [result['term'] for result in self.cept.getClosestStrings(self.bitmap)] def closestString(self): closestStrings = self.closestStrings() if not len(closestStrings): return "" return closestStrings[0]