Example #1
0
def main(terms):
    parser = argparse.ArgumentParser()
    parser.add_argument("--retinaId", default="en_synonymous", type=str)
    parser.add_argument("--corticalApiKey",
                        default=os.environ.get("CORTICAL_API_KEY"),
                        type=str)
    parser.add_argument(
        "--plot",
        default="histogram",
        choices=["histogram", "heatmap", "frequencyHistogram", "None", "none"])
    parser.add_argument("--cacheDir", default=None, type=str)

    opts = parser.parse_args()

    if opts.retinaId == "random":
        counts = countRandomBitFrequencies()
    else:
        client = CorticalClient(opts.corticalApiKey,
                                retina=opts.retinaId,
                                verbosity=0,
                                cacheDir=opts.cacheDir,
                                fillSDR=None)

        counts = countBitFrequenciesForTerms(client, terms)

    if opts.plot == "histogram":
        plotHistogram(counts)
    elif opts.plot == "heatmap":
        plotlyHeatmap(counts)
    elif opts.plot == "frequencyHistogram":
        plotlyFrequencyHistogram(counts)
Example #2
0
    def __init__(self,
                 w=128,
                 h=128,
                 retina=DEFAULT_RETINA,
                 cacheDir="./cache",
                 verbosity=0,
                 fingerprintType=EncoderTypes.document):
        """
    @param w               (int)      Width dimension of the SDR topology.
    @param h               (int)      Height dimension of the SDR topology.
    @param cacheDir        (str)      Where to cache results of API queries.
    @param verbosity       (int)      Amount of info printed out, 0, 1, or 2.
    @param fingerprintType (Enum)     Specify word- or document-level encoding.
    """
        if "CORTICAL_API_KEY" not in os.environ:
            print(
                "Missing CORTICAL_API_KEY environment variable. If you have a "
                "key, set it with $ export CORTICAL_API_KEY=api_key\n"
                "You can retrieve a key by registering for the REST API at "
                "http://www.cortical.io/resources_apikey.html")
            raise OSError("Missing API key.")

        self.apiKey = os.environ["CORTICAL_API_KEY"]
        self.client = CorticalClient(self.apiKey,
                                     retina=retina,
                                     cacheDir=cacheDir)
        self.targetSparsity = 5.0
        self.w = w
        self.h = h
        self.n = w * h
        self.verbosity = verbosity
        self.fingerprintType = fingerprintType
        self.description = ("Cio Encoder", 0)
Example #3
0
 def cacheDir(self, value):
     if value:
         # Only set cacheDir if value explicitly provided
         self._cacheDir = value
         # Re-init the encoder's Cio client for the new cacheDir
         self.client = CorticalClient(self.apiKey,
                                      retina=self.retina,
                                      cacheDir=value)
Example #4
0
    def __init__(self,
                 retina=DEFAULT_RETINA,
                 retinaScaling=1.0,
                 cacheDir=None,
                 verbosity=0,
                 fingerprintType=EncoderTypes.document,
                 unionSparsity=0.20,
                 apiKey=None,
                 maxSparsity=0.50):
        """
    @param retina          (str)      Cortical.io retina, either "en_synonymous"
                                      or "en_associative".
    @param retinaScaling   (float)    Scale each dimension of the SDR bitmap
                                      by this factor.
    @param cacheDir        (str)      Where to cache results of API queries.
    @param verbosity       (int)      Amount of info printed out, 0, 1, or 2.
    @param fingerprintType (Enum)     Specify word- or document-level encoding.
    @param unionSparsity   (float)    Any union'ing done in this encoder will
                                      stop once this sparsity is reached.
    @param maxSparsity     (float)    The maximum sparsity of the returned
                                      bitmap. If the percentage of bits in the
                                      encoding is > maxSparsity, it will be
                                      randomly subsampled.

    TODO: replace enum with a simple string
    """
        if apiKey is None and "CORTICAL_API_KEY" not in os.environ:
            print(
                "Missing CORTICAL_API_KEY environment variable. If you have a "
                "key, set it with $ export CORTICAL_API_KEY=api_key\n"
                "You can retrieve a key by registering for the REST API at "
                "http://www.cortical.io/resources_apikey.html")
            raise OSError("Missing API key.")

        super(CioEncoder, self).__init__(unionSparsity=unionSparsity)

        if cacheDir is None:
            # Use the default cache directory
            root = os.path.dirname(os.path.realpath(__file__))
            cacheDir = os.path.join(root, "CioCache")

        self.apiKey = apiKey if apiKey else os.environ["CORTICAL_API_KEY"]
        self.retina = retina

        self.client = CorticalClient(self.apiKey,
                                     retina=retina,
                                     cacheDir=cacheDir)

        self._setDimensions(retinaScaling)

        self.fingerprintType = fingerprintType
        self.description = ("Cio Encoder", 0)

        self.verbosity = verbosity
        self.maxSparsity = maxSparsity
Example #5
0
    def __setstate__(self, state):
        """ Called when CioEncoder is unpickled per pickle protocol.  This includes
    a mechanism to gracefully re-init the CorticalClient instance with a
    calculated cacheDir, in the event that the previously pickled
    CorticalClient instance includes a cacheDir that does not exist, which is
    likely the case when a model is trained on one machine for reuse elsewhere.
    """
        if "_cacheDir" not in state:
            state["client"] = CorticalClient(state["apiKey"],
                                             retina=state["client"].retina,
                                             cacheDir=self.cacheDir)

        self.__dict__ = state
Example #6
0
    def __init__(self, verbosity=1, numLabels=1):
        """
    Initialize the CorticalClient and CioEncoder. Requires a valid API key
    """
        super(ClassificationModelContext, self).__init__(verbosity)

        self.encoder = CioEncoder(cacheDir="./experiments/cache")
        self.client = CorticalClient(self.encoder.apiKey)

        self.n = self.encoder.n
        self.w = int((self.encoder.targetSparsity / 100) * self.n)

        self.categoryBitmaps = {}
        self.numLabels = numLabels
Example #7
0
    def __init__(self, w=128, h=128):
        if 'CORTICAL_API_KEY' not in os.environ:
            print(
                "Missing CORTICAL_API_KEY environment variable. If you have a "
                "key, set it with $ export CORTICAL_API_KEY=api_key\n"
                "You can retrieve a key by registering for the REST API at "
                "http://www.cortical.io/resources_apikey.html")
            raise Exception("Missing API key.")

        self.apiKey = os.environ['CORTICAL_API_KEY']
        self.client = CorticalClient(self.apiKey, cacheDir=os.join("./cache"))
        self.targetSparsity = 1.0
        self.w = w  ## Alternatively get dimensions from cortipy client object?
        self.h = h
        self.n = w * h
Example #8
0
  def __init__(self, verbosity=1, numLabels=1):
    """
    Initialize the CorticalClient and CioEncoder. Requires a valid API key.
    """
    super(ClassificationModelContext, self).__init__(verbosity)

    root = os.path.dirname(os.path.realpath(__file__))
    self.encoder = CioEncoder(cacheDir=os.path.join(root, "CioCache"))
    self.client = CorticalClient(self.encoder.apiKey)

    self.n = self.encoder.n
    self.w = int((self.encoder.targetSparsity / 100) * self.n)

    self.categoryBitmaps = {}
    self.numLabels = numLabels
Example #9
0
  def __init__(self, retina=DEFAULT_RETINA, retinaScaling=1.0, cacheDir=None,
               verbosity=0, fingerprintType=EncoderTypes.document,
               unionSparsity=0.20, apiKey=None):
    """
    @param retina          (str)      Cortical.io retina, either "en_synonymous"
                                      or "en_associative".
    @param retinaScaling   (float)    Scales the dimensions of the SDR topology,
                                      where the width and height are both 128.
    @param cacheDir        (str)      Where to cache results of API queries.
    @param verbosity       (int)      Amount of info printed out, 0, 1, or 2.
    @param fingerprintType (Enum)     Specify word- or document-level encoding.

    TODO: replace enum with a simple string
    """
    if apiKey is None and "CORTICAL_API_KEY" not in os.environ:
      print ("Missing CORTICAL_API_KEY environment variable. If you have a "
        "key, set it with $ export CORTICAL_API_KEY=api_key\n"
        "You can retrieve a key by registering for the REST API at "
        "http://www.cortical.io/resources_apikey.html")
      raise OSError("Missing API key.")

    super(CioEncoder, self).__init__(unionSparsity=unionSparsity)

    if cacheDir is None:
      root = os.path.dirname(os.path.realpath(__file__))
      cacheDir = os.path.join(root, "CioCache")

    self.apiKey = apiKey if apiKey else os.environ["CORTICAL_API_KEY"]
    self.client = CorticalClient(self.apiKey, retina=retina, cacheDir=cacheDir)

    self._setDimensions(retina, retinaScaling)

    self.fingerprintType = fingerprintType
    self.description = ("Cio Encoder", 0)

    self.verbosity = verbosity
Example #10
0
  parser = argparse.ArgumentParser()
  parser.add_argument("--retinaId",
                      default="en_synonymous",
                      type=str)
  parser.add_argument("--corticalApiKey",
                      default=os.environ.get("CORTICAL_API_KEY"),
                      type=str)
  parser.add_argument("--cacheDir",
                      default="../../htmresearch/encoders/CioCache", type=str)

  opts = parser.parse_args()

  client = CorticalClient(opts.corticalApiKey,
                          retina=opts.retinaId,
                          verbosity=0,
                          cacheDir=opts.cacheDir,
                          fillSDR=None)


  # Read in words from dictionary
  with open("enable1.txt", "r") as f:
    lines = f.readlines()

  print "Processing",len(lines),"lines..."
  words = []
  random.seed(42)
  # Subsample small percentage of words
  for line in lines:
      p = random.uniform(0,1)
      if p <= 1.05:
Example #11
0
 def testExceptionIfAPIKeyNotPresent(self, mockOS):
     with self.assertRaises(Exception) as e:
         cClient = CorticalClient()
     self.assertIn("Missing API key.", e.exception)
Example #12
0
 def testAPIKeyPresent(self):
     with patch.dict("os.environ", {"CIO_API_KEY": "apikey123"}):
         cClient = CorticalClient()