예제 #1
0
  def testCreateModel(self):

    # Setup fake ImbuModels instance
    imbu = ImbuModels(
      cacheRoot="fake_cache_root",
      dataPath=self.dataPath,
      retina="en_associative",
      apiKey=os.environ.get("CORTICAL_API_KEY")
    )

    # You must specify a known model type
    self.assertRaises(TypeError, imbu.createModel, "random model type")

    # Assert that you can create models of known types from scratch (i.e. with
    # empty loadPath value)
    for modelType in imbu.modelMappings:
      # You _must_ specify loadPath and savePath.  Assert that attempts to not
      # specify will result in failure
      self.assertRaises(TypeError, imbu.createModel, modelType)
      self.assertRaises(TypeError, imbu.createModel, modelType, None)
      self.assertRaises(ValueError, imbu.createModel, modelType, None, None)

      # Attempt to create model using default arguments
      model = imbu.createModel(
        modelType, "", "", networkConfigName="imbu_sensor_knn.json"
      )

      # Assert model created was one of the expected types
      self.assertTrue(isinstance(model, ClassificationModel) or
                      isinstance(model, ClassificationNetworkAPI))
    def testCreateModel(self):

        # Setup fake ImbuModels instance
        imbu = ImbuModels(cacheRoot="fake_cache_root",
                          dataPath=self.dataPath,
                          retina="en_associative",
                          apiKey=os.environ.get("CORTICAL_API_KEY"))

        # You must specify a known model type
        self.assertRaises(TypeError, imbu.createModel, "random model type")

        # Assert that you can create models of known types from scratch (i.e. with
        # empty loadPath value)
        for modelType in imbu.modelMappings:
            # You _must_ specify loadPath and savePath.  Assert that attempts to not
            # specify will result in failure
            self.assertRaises(TypeError, imbu.createModel, modelType)
            self.assertRaises(TypeError, imbu.createModel, modelType, None)
            self.assertRaises(ValueError, imbu.createModel, modelType, None,
                              None)

            # Attempt to create model using default arguments
            model = imbu.createModel(modelType,
                                     "",
                                     "",
                                     networkConfigName="imbu_sensor_knn.json")

            # Assert model created was one of the expected types
            self.assertTrue(
                isinstance(model, ClassificationModel)
                or isinstance(model, ClassificationNetworkAPI))
  def testMappingModelNamesToModelTypes(self):
    imbu = ImbuModels(dataPath=self.dataPath)

    for modelName, mappedName in imbu.modelMappings.iteritems():
      self.assertEquals(mappedName, imbu._mapModelName(modelName),
        "Incorrect mapping returned for model named '{}'".format(modelName))

    self.assertRaises(ValueError, imbu._mapModelName, "fakeModel")
    def testMappingModelNamesToModelTypes(self):
        imbu = ImbuModels(cacheRoot="fake_cache_root", dataPath=self.dataPath)

        for modelName, mappedName in imbu.modelMappings.iteritems():
            self.assertEquals(
                mappedName, imbu._mapModelName(modelName),
                "Incorrect mapping returned for model named '{}'".format(
                    modelName))

        self.assertRaises(ValueError, imbu._mapModelName, "fakeModel")
예제 #5
0
    def testCacheDirProperty(self):
        imbu = ImbuModels(dataPath=self.dataPath)

        checkpointLocation = self._createTempModelCheckpoint()

        model = imbu.createModel("HTMNetwork",
                                 loadPath="",
                                 savePath=checkpointLocation,
                                 networkConfigName="imbu_sensor_knn.json")

        # Test for default cache directory
        encoder = model.getEncoder()
        defaultCacheLocation = "nupic.research/htmresearch/encoders/CioCache"
        self.assertIn(
            defaultCacheLocation, getattr(encoder.client, "cacheDir"),
            "Cio encoder cache dir is not the expected default location.")

        # Now explicitly set the cache directory
        encoder.cacheDir = "fake_cache_root"
        self.assertEqual("fake_cache_root", getattr(encoder.client,
                                                    "cacheDir"),
                         "Cio encoder cache dir did not set properly.")
예제 #6
0
  def _exerciseModelLifecycle(self, modelType, queryTerm="unicorn",
                              networkConfigName="imbu_sensor_knn.json"):
    # Setup fake ImbuModels instance
    imbu = ImbuModels(
      cacheRoot="fake_cache_root",
      dataPath=self.dataPath,
      retina="en_associative",
      apiKey=os.environ.get("CORTICAL_API_KEY")
    )

    tmpDir = tempfile.mkdtemp()
    self.addCleanup(shutil.rmtree, tmpDir)

    checkpointLocation = os.path.join(tmpDir, "checkpoint")

    # Create model with no load path.  This should trigger the creation of a
    # new intance, and train it.  Specify save path so that the trained model
    # gets saved
    originalModel = imbu.createModel(modelType,
                                     loadPath="",
                                     savePath=checkpointLocation,
                                     networkConfigName=networkConfigName)

    # Make a query, keep around for later comparison
    originalQueryResults = imbu.query(originalModel, queryTerm)

    del originalModel

    # Create model, specifying previous load path
    model = imbu.createModel(modelType,
                             loadPath=checkpointLocation,
                             savePath=checkpointLocation,
                             networkConfigName=networkConfigName)

    self.assertTrue(all(numpy.array_equal(original, new)
                        for (original, new)
                        in zip(originalQueryResults,
                               imbu.query(model, queryTerm))))
    def testCacheSaveAndReload(self):
        imbuTempDir = self._createTempDir()
        originalCacheDir = os.path.join(imbuTempDir, "original_test_cache")
        imbu = ImbuModels(cacheRoot=originalCacheDir,
                          dataPath=self.dataPath,
                          retina="en_associative",
                          apiKey=os.environ.get("CORTICAL_API_KEY"))

        # Train with standard cache location, save
        modelType = "HTMNetwork"
        networkConfigName = "imbu_sensor_knn.json"
        checkpointLocation = os.path.join(imbuTempDir, "checkpoint")
        originalModel = imbu.createModel(modelType,
                                         loadPath="",
                                         savePath=checkpointLocation,
                                         networkConfigName=networkConfigName)

        # Physically move the cache to a new directory; we copy it b/c moving it
        # would break the test cleanup method
        encoder = originalModel.getEncoder()
        newCacheDir = os.path.join(imbuTempDir, "new_test_cache")
        shutil.copytree(originalCacheDir, newCacheDir)
        self.addCleanup(shutil.rmtree, newCacheDir)
        self.assertGreater(len(os.listdir(newCacheDir)), 0,
                           "The new cache directory is empty!")

        # Load a new model and set cache location to the new directory
        newModel = imbu.createModel(modelType,
                                    loadPath=checkpointLocation,
                                    savePath=checkpointLocation,
                                    networkConfigName=networkConfigName)
        newEncoder = newModel.getEncoder()
        newEncoder.cacheDir = newCacheDir
        newEncoderCacheDir = getattr(newEncoder, "cacheDir")
        self.assertNotEquals(
            newEncoderCacheDir, getattr(encoder, "cacheDir"),
            "Old and new cache locations shouldn't be the same.")
        del originalModel

        # Run inference with old data, expecting no new caching
        sizeOfCache = len(os.listdir(newEncoderCacheDir))
        imbu.query(newModel, "unicorn")
        self.assertEquals(sizeOfCache, len(os.listdir(newEncoderCacheDir)), "")

        # Run inference with new data, adding to the new cache location
        imbu.query(newModel, "brains")
        self.assertEquals(sizeOfCache + 1, len(os.listdir(newEncoderCacheDir)),
                          "")
예제 #8
0
  def testResultsFormatting(self):
    imbu = ImbuModels(cacheRoot="fake_cache_root", dataPath=self.dataPath)

    query = "Hello world!"
    distanceArray = numpy.random.rand(35)
    idList = range(27) + [x+1000 for x in xrange(8)]

    # Test a model with windows
    modelName = "HTM_sensor_simple_tp_knn"
    results = imbu.formatResults(modelName, query, distanceArray, idList)
    self.checkResultsFormatting(results, modelName, windowSize=10)

    # Test a word-level model
    modelName = "HTM_sensor_knn"
    results = imbu.formatResults(modelName, query, distanceArray, idList)
    self.checkResultsFormatting(results, modelName, windowSize=1)

    # Test a document-level model
    modelName = "CioDocumentFingerprint"
    distanceArray = numpy.random.rand(2)
    idList = range(1)
    results = imbu.formatResults(modelName, query, distanceArray, idList)
    self.checkResultsFormatting(results, modelName)
    def testResultsFormatting(self):
        imbu = ImbuModels(cacheRoot="fake_cache_root", dataPath=self.dataPath)

        query = "Hello world!"
        distanceArray = numpy.random.rand(35)
        idList = range(27) + [x + 1000 for x in xrange(8)]

        # Test a model with windows
        modelName = "HTM_sensor_simple_tp_knn"
        results = imbu.formatResults(modelName, query, distanceArray, idList)
        self.checkResultsFormatting(results, modelName, windowSize=10)

        # Test a word-level model
        modelName = "HTM_sensor_knn"
        results = imbu.formatResults(modelName, query, distanceArray, idList)
        self.checkResultsFormatting(results, modelName, windowSize=1)

        # Test a document-level model
        modelName = "CioDocumentFingerprint"
        distanceArray = numpy.random.rand(2)
        idList = range(1)
        results = imbu.formatResults(modelName, query, distanceArray, idList)
        self.checkResultsFormatting(results, modelName)
  def testCacheDirProperty(self):
    imbu = ImbuModels(dataPath=self.dataPath)

    checkpointLocation = self._createTempModelCheckpoint()

    model = imbu.createModel("HTMNetwork",
                             loadPath="",
                             savePath=checkpointLocation,
                             networkConfigName="imbu_sensor_knn.json")

    # Test for default cache directory
    encoder = model.getEncoder()
    defaultCacheLocation = "nupic.research/htmresearch/encoders/CioCache"
    self.assertIn(
      defaultCacheLocation,
      getattr(encoder.client, "cacheDir"),
      "Cio encoder cache dir is not the expected default location.")

    # Now explicitly set the cache directory
    encoder.cacheDir = "fake_cache_root"
    self.assertEquals(
      "fake_cache_root",
      getattr(encoder.client, "cacheDir"),
      "Cio encoder cache dir did not set properly.")
  def testCacheSaveAndReload(self):
    imbuTempDir = self._createTempDir()
    originalCacheDir = os.path.join(imbuTempDir, "original_test_cache")
    imbu = ImbuModels(
      cacheRoot=originalCacheDir,
      dataPath=self.dataPath,
      retina="en_associative",
      apiKey=os.environ.get("CORTICAL_API_KEY")
    )

    # Train with standard cache location, save
    modelType = "HTMNetwork"
    networkConfigName = "imbu_sensor_knn.json"
    checkpointLocation = os.path.join(imbuTempDir, "checkpoint")
    originalModel = imbu.createModel(modelType,
                                     loadPath="",
                                     savePath=checkpointLocation,
                                     networkConfigName=networkConfigName)

    # Physically move the cache to a new directory; we copy it b/c moving it
    # would break the test cleanup method
    encoder = originalModel.getEncoder()
    newCacheDir = os.path.join(imbuTempDir, "new_test_cache")
    shutil.copytree(originalCacheDir, newCacheDir)
    self.addCleanup(shutil.rmtree, newCacheDir)
    self.assertGreater(len(os.listdir(newCacheDir)), 0,
      "The new cache directory is empty!")

    # Load a new model and set cache location to the new directory
    newModel = imbu.createModel(modelType,
                                loadPath=checkpointLocation,
                                savePath=checkpointLocation,
                                networkConfigName=networkConfigName)
    newEncoder = newModel.getEncoder()
    newEncoder.cacheDir = newCacheDir
    newEncoderCacheDir = getattr(newEncoder, "cacheDir")
    self.assertNotEquals(
      newEncoderCacheDir,
      getattr(encoder, "cacheDir"),
      "Old and new cache locations shouldn't be the same.")
    del originalModel

    # Run inference with old data, expecting no new caching
    sizeOfCache = len(os.listdir(newEncoderCacheDir))
    imbu.query(newModel, "unicorn")
    self.assertEquals(sizeOfCache, len(os.listdir(newEncoderCacheDir)), "")

    # Run inference with new data, adding to the new cache location
    imbu.query(newModel, "brains")
    self.assertEquals(sizeOfCache + 1, len(os.listdir(newEncoderCacheDir)), "")
    def _exerciseModelLifecycle(self,
                                modelType,
                                queryTerm="food",
                                networkConfigName="imbu_sensor_knn.json"):
        # Setup fake ImbuModels instance
        imbu = ImbuModels(cacheRoot="fake_cache_root",
                          dataPath=self.dataPath,
                          retina="en_associative",
                          apiKey=os.environ.get("CORTICAL_API_KEY"))

        tmpDir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, tmpDir)

        checkpointLocation = os.path.join(tmpDir, "checkpoint")

        # Create model with no load path.  This should trigger the creation of a
        # new intance, and train it.  Specify save path so that the trained model
        # gets saved
        originalModel = imbu.createModel(modelType,
                                         loadPath="",
                                         savePath=checkpointLocation,
                                         networkConfigName=networkConfigName)

        # Make a query, keep around for later comparison
        originalQueryResults = imbu.query(originalModel, queryTerm)

        del originalModel

        # Create model, specifying previous load path
        model = imbu.createModel(modelType,
                                 loadPath=checkpointLocation,
                                 savePath=checkpointLocation,
                                 networkConfigName=networkConfigName)

        self.assertTrue(
            all(
                numpy.array_equal(original, new) for (original, new) in zip(
                    originalQueryResults, imbu.query(model, queryTerm))))
예제 #13
0


# There is no longer training in imbu web app.  User must specify loadPath
if "IMBU_LOAD_PATH_PREFIX" in os.environ:
  _IMBU_LOAD_PATH_PREFIX = os.environ["IMBU_LOAD_PATH_PREFIX"]
else:
  raise KeyError("Required IMBU_LOAD_PATH_PREFIX missing from environment")

g_models = {} # Global model cache

imbu = ImbuModels(
  cacheRoot=os.environ.get("MODEL_CACHE_DIR", os.getcwd()),
  modelSimilarityMetric=None,
  dataPath=os.environ.get("IMBU_DATA",
                          pkg_resources.resource_filename(__name__,
                                                          "data.csv")),
  retina=os.environ["IMBU_RETINA_ID"],
  apiKey=os.environ["CORTICAL_API_KEY"]
)


def addStandardHeaders(contentType="application/json; charset=UTF-8"):
  """
  Add Standard HTTP Headers ("Content-Type", "Server") to the response.
  Here is an example of the headers added by this method using the default
  values::
      Content-Type: application/json; charset=UTF-8
      Server: Imbu x.y.z
  :param content_type: The value for the "Content-Type" header.
                       (default "application/json; charset=UTF-8")
예제 #14
0
# No training in Imbu web app, user must specify loadPath
if "IMBU_LOAD_PATH_PREFIX" in os.environ:
    _IMBU_LOAD_PATH_PREFIX = os.environ["IMBU_LOAD_PATH_PREFIX"]
else:
    raise KeyError("Required IMBU_LOAD_PATH_PREFIX missing from environment")

g_imbus = {}  # Global ImbuModels cache
g_models = {}  # Global NLP model cache
for datasetName in os.listdir(_IMBU_LOAD_PATH_PREFIX):
    datasetPath = os.path.join(_IMBU_LOAD_PATH_PREFIX, datasetName)
    if os.path.isdir(datasetPath) and "egg" not in datasetPath:
        # Create an imbu instance for each dataset
        imbu = ImbuModels(cacheRoot=os.environ.get("MODEL_CACHE_DIR",
                                                   os.getcwd()),
                          modelSimilarityMetric=None,
                          dataPath=os.path.join(datasetPath, "data.csv"),
                          retina=os.environ["IMBU_RETINA_ID"],
                          apiKey=os.environ["CORTICAL_API_KEY"])
        g_imbus.update(((datasetName, imbu), ))
        # Init the dict for this dataset's models
        g_models[datasetName] = {}


def addStandardHeaders(contentType="application/json; charset=UTF-8"):
    """
  Add Standard HTTP Headers ("Content-Type", "Server") to the response.
  Here is an example of the headers added by this method using the default
  values::
      Content-Type: application/json; charset=UTF-8
      Server: Imbu x.y.z
  :param content_type: The value for the "Content-Type" header.
예제 #15
0
 def _setupFakeImbuModelsInstance(self, retina="en_associative"):
     return ImbuModels(cacheRoot="fake_cache_root",
                       dataPath=self.dataPath,
                       retina=retina,
                       apiKey=os.environ.get("CORTICAL_API_KEY"))