Ejemplo n.º 1
0
def run():
    """ Run classification network(s) on artificial sensor data """
    with open("network_config_template.json", "rb") as jsonFile:
        templateNetworkConfig = json.load(jsonFile)

    networkConfigurations = generateSampleNetworkConfig(
        templateNetworkConfig, NUM_CATEGORIES)

    for networkConfig in networkConfigurations:
        for noiseAmplitude in WHITE_NOISE_AMPLITUDES:
            for signalMean in SIGNAL_MEANS:
                for signalAmplitude in SIGNAL_AMPLITUDES:
                    for signalPeriod in SIGNAL_PERIODS:
                        sensorType = networkConfig["sensorRegionConfig"].get(
                            "regionType")
                        spEnabled = networkConfig["sensorRegionConfig"].get(
                            "regionEnabled")
                        tmEnabled = networkConfig["tmRegionConfig"].get(
                            "regionEnabled")
                        upEnabled = networkConfig["tpRegionConfig"].get(
                            "regionEnabled")
                        classifierType = networkConfig[
                            "classifierRegionConfig"].get("regionType")

                        expParams = (
                            "RUNNING EXPERIMENT WITH PARAMS:\n"
                            " * numRecords=%s\n"
                            " * signalAmplitude=%s\n"
                            " * signalMean=%s\n"
                            " * signalPeriod=%s\n"
                            " * noiseAmplitude=%s\n"
                            " * sensorType=%s\n"
                            " * spEnabled=%s\n"
                            " * tmEnabled=%s\n"
                            " * tpEnabled=%s\n"
                            " * classifierType=%s\n") % (
                                NUM_RECORDS, signalAmplitude, signalMean,
                                signalPeriod, noiseAmplitude,
                                sensorType.split(".")[1], spEnabled, tmEnabled,
                                upEnabled, classifierType.split(".")[1])
                        print expParams

                        inputFile = generateSensorData(
                            DATA_DIR, OUTFILE_NAME, signalMean, signalPeriod,
                            SEQUENCE_LENGTH, NUM_RECORDS, signalAmplitude,
                            NUM_CATEGORIES, noiseAmplitude)

                        dataSource = FileRecordStream(streamID=inputFile)
                        network = configureNetwork(dataSource, networkConfig)
                        partitions = generateNetworkPartitions(
                            networkConfig, NUM_RECORDS)

                        trainNetwork(network, networkConfig, partitions,
                                     NUM_RECORDS)
Ejemplo n.º 2
0
  def train(self, training_set_size, partitions):
    """
    Train the HTM network
    @param training_set_size: size of the training set.
    @param partitions: (list of namedtuple) list of partitions to train the 
      network. E.g: 
      [Partition(partName=SP, index=0), ..., Partition(partName=test, index=0)]
    @return classification_accuracy: (float) classification accuracy, 0 to 100.

    """

    return trainNetwork(self.network,
                        self.network_config,
                        partitions,
                        training_set_size)
Ejemplo n.º 3
0
def run():
  """ Run classification network(s) on artificial sensor data """

  if USE_JSON_CONFIG:
    with open('config/network_configs.json', 'rb') as fr:
      networkConfigurations = json.load(fr)
  else:
    with open("config/network_config_template.json", "rb") as jsonFile:
      templateNetworkConfig = json.load(jsonFile)
      networkConfigurations = generateSampleNetworkConfig(templateNetworkConfig,
                                                          NUM_CATEGORIES)

  headers = ['numRecords', 'seqLength', 'numClasses', 'signalAmplitude',
             'signalMean', 'signalPeriod', 'noiseAmplitude', 'spEnabled',
             'tmEnabled', 'tpEnabled', 'classifierType',
             'classificationAccuracy']

  with open(RESULTS_FILE, 'wb') as fw:
    writer = csv.writer(fw)
    writer.writerow(headers)
    t = PrettyTable(headers)
    for networkConfig in networkConfigurations:
      for noiseAmplitude in WHITE_NOISE_AMPLITUDES:
        for signalMean in SIGNAL_MEANS:
          for signalAmplitude in SIGNAL_AMPLITUDES:
            for signalPeriod in SIGNAL_PERIODS:
              spEnabled = networkConfig["sensorRegionConfig"].get(
                "regionEnabled")
              tmEnabled = networkConfig["tmRegionConfig"].get(
                "regionEnabled")
              upEnabled = networkConfig["tpRegionConfig"].get(
                "regionEnabled")
              classifierType = networkConfig["classifierRegionConfig"].get(
                "regionType")
              inputFile = generateSensorData(DATA_DIR,
                                             OUTFILE_NAME,
                                             signalMean,
                                             signalPeriod,
                                             SEQUENCE_LENGTH,
                                             NUM_RECORDS,
                                             signalAmplitude,
                                             NUM_CATEGORIES,
                                             noiseAmplitude)

              dataSource = FileRecordStream(streamID=inputFile)
              network = configureNetwork(dataSource,
                                         networkConfig)
              partitions = generateNetworkPartitions(networkConfig,
                                                     NUM_RECORDS)

              classificationAccuracy = trainNetwork(network, networkConfig,
                                                    partitions, NUM_RECORDS,
                                                    VERBOSITY)

              results = [NUM_RECORDS,
                         SEQUENCE_LENGTH,
                         NUM_CATEGORIES,
                         signalAmplitude,
                         signalMean,
                         signalPeriod,
                         noiseAmplitude,
                         spEnabled,
                         tmEnabled,
                         upEnabled,
                         classifierType.split(".")[1],
                         classificationAccuracy]

              writer.writerow(results)
              t.add_row(results)
              
  print '%s\n' % t
  print '==> Results saved to %s\n' % RESULTS_FILE
Ejemplo n.º 4
0
def run():
    """ Run classification network(s) on artificial sensor data """

    if USE_JSON_CONFIG:
        with open('config/network_configs.json', 'rb') as fr:
            networkConfigurations = json.load(fr)
    else:
        with open("config/network_config_template.json", "rb") as jsonFile:
            templateNetworkConfig = json.load(jsonFile)
            networkConfigurations = generateSampleNetworkConfig(
                templateNetworkConfig, NUM_CATEGORIES)

    headers = [
        'numRecords', 'seqLength', 'numClasses', 'signalAmplitude',
        'signalMean', 'signalPeriod', 'noiseAmplitude', 'spEnabled',
        'tmEnabled', 'tpEnabled', 'classifierType', 'classificationAccuracy'
    ]

    with open(RESULTS_FILE, 'wb') as fw:
        writer = csv.writer(fw)
        writer.writerow(headers)
        t = PrettyTable(headers)
        for networkConfig in networkConfigurations:
            for noiseAmplitude in WHITE_NOISE_AMPLITUDES:
                for signalMean in SIGNAL_MEANS:
                    for signalAmplitude in SIGNAL_AMPLITUDES:
                        for signalPeriod in SIGNAL_PERIODS:
                            spEnabled = networkConfig[
                                "sensorRegionConfig"].get("regionEnabled")
                            tmEnabled = networkConfig["tmRegionConfig"].get(
                                "regionEnabled")
                            upEnabled = networkConfig["tpRegionConfig"].get(
                                "regionEnabled")
                            classifierType = networkConfig[
                                "classifierRegionConfig"].get("regionType")
                            inputFile = generateSensorData(
                                DATA_DIR, OUTFILE_NAME, signalMean,
                                signalPeriod, SEQUENCE_LENGTH, NUM_RECORDS,
                                signalAmplitude, NUM_CATEGORIES,
                                noiseAmplitude)

                            dataSource = FileRecordStream(streamID=inputFile)
                            network = configureNetwork(dataSource,
                                                       networkConfig)
                            partitions = generateNetworkPartitions(
                                networkConfig, NUM_RECORDS)

                            classificationAccuracy = trainNetwork(
                                network, networkConfig, partitions,
                                NUM_RECORDS, VERBOSITY)

                            results = [
                                NUM_RECORDS, SEQUENCE_LENGTH, NUM_CATEGORIES,
                                signalAmplitude, signalMean, signalPeriod,
                                noiseAmplitude, spEnabled, tmEnabled,
                                upEnabled,
                                classifierType.split(".")[1],
                                classificationAccuracy
                            ]

                            writer.writerow(results)
                            t.add_row(results)

    print '%s\n' % t
    print '==> Results saved to %s\n' % RESULTS_FILE
  def testClassificationAccuracy(self):
    """Test classification accuracy for sensor data."""

    networkConfigurations = generateSampleNetworkConfig(
      self.templateNetworkConfig, NUM_CATEGORIES)

    for networkConfig in networkConfigurations:
      for noiseAmplitude in WHITE_NOISE_AMPLITUDES:
        for signalMean in SIGNAL_MEANS:
          for signalAmplitude in SIGNAL_AMPLITUDES:
            for signalPeriod in SIGNAL_PERIODS:
              sensorType = networkConfig["sensorRegionConfig"].get(
                "regionType")
              spEnabled = networkConfig["sensorRegionConfig"].get(
                "regionEnabled")
              tmEnabled = networkConfig["tmRegionConfig"].get(
                "regionEnabled")
              upEnabled = networkConfig["tpRegionConfig"].get(
                "regionEnabled")
              classifierType = networkConfig["classifierRegionConfig"].get(
                "regionType")

              expParams = ("RUNNING EXPERIMENT WITH PARAMS:\n"
                           " * numRecords=%s\n"
                           " * signalAmplitude=%s\n"
                           " * signalMean=%s\n"
                           " * signalPeriod=%s\n"
                           " * noiseAmplitude=%s\n"
                           " * sensorType=%s\n"
                           " * spEnabled=%s\n"
                           " * tmEnabled=%s\n"
                           " * tpEnabled=%s\n"
                           " * classifierType=%s\n"
                           ) % (NUM_RECORDS,
                                signalAmplitude,
                                signalMean,
                                signalPeriod,
                                noiseAmplitude,
                                sensorType.split(".")[1],
                                spEnabled,
                                tmEnabled,
                                upEnabled,
                                classifierType.split(".")[1])
              print expParams

              inputFile = generateSensorData(DATA_DIR,
                                             OUTFILE_NAME,
                                             signalMean,
                                             signalPeriod,
                                             SEQUENCE_LENGTH,
                                             NUM_RECORDS,
                                             signalAmplitude,
                                             NUM_CATEGORIES,
                                             noiseAmplitude)

              dataSource = FileRecordStream(streamID=inputFile)
              network = configureNetwork(dataSource,
                                         networkConfig)
              partitions = generateNetworkPartitions(networkConfig,
                                                     NUM_RECORDS)

              classificationAccuracy = trainNetwork(network, networkConfig,
                                                  partitions, NUM_RECORDS)

              if (noiseAmplitude == 0
                  and signalMean == 1.0
                  and signalAmplitude == 1.0
                  and signalPeriod == 20.0
                  and classifierType == KNN_CLASSIFIER_TYPE
                  and spEnabled
                  and tmEnabled
                  and not upEnabled):
                self.assertEqual(classificationAccuracy, 100.00)
              elif (noiseAmplitude == 0
                    and signalMean == 1.0
                    and signalAmplitude == 1.0
                    and signalPeriod == 20.0
                    and classifierType == CLA_CLASSIFIER_TYPE
                    and spEnabled
                    and tmEnabled
                    and not upEnabled):
                self.assertEqual(classificationAccuracy, 100.00)
              elif (noiseAmplitude == 0
                    and signalMean == 1.0
                    and signalAmplitude == 1.0
                    and signalPeriod == 20.0
                    and classifierType == CLA_CLASSIFIER_TYPE
                    and spEnabled
                    and not tmEnabled
                    and not upEnabled):
                self.assertEqual(classificationAccuracy, 100.00)
              elif (noiseAmplitude == 1.0
                    and signalMean == 1.0
                    and signalAmplitude == 1.0
                    and signalPeriod == 20.0
                    and classifierType == CLA_CLASSIFIER_TYPE
                    and spEnabled
                    and tmEnabled
                    and not upEnabled):
                # using AlmostEqual until the random bug issue is fixed
                self.assertAlmostEqual(classificationAccuracy, 80, delta=5)
              elif (noiseAmplitude == 1.0
                    and signalMean == 1.0
                    and signalAmplitude == 1.0
                    and signalPeriod == 20.0
                    and classifierType == CLA_CLASSIFIER_TYPE
                    and spEnabled
                    and not tmEnabled
                    and not upEnabled):
                # using AlmostEqual until the random bug issue is fixed
                self.assertAlmostEqual(classificationAccuracy, 81, delta=5)
def run():
  """ Run classification network(s) on artificial sensor data """
  with open("network_config_template.json", "rb") as jsonFile:
    templateNetworkConfig = json.load(jsonFile)

  networkConfigurations = generateSampleNetworkConfig(templateNetworkConfig, 
                                                      NUM_CATEGORIES)

  for networkConfig in networkConfigurations:
    for noiseAmplitude in WHITE_NOISE_AMPLITUDES:
      for signalMean in SIGNAL_MEANS:
        for signalAmplitude in SIGNAL_AMPLITUDES:
          for signalPeriod in SIGNAL_PERIODS:
            sensorType = networkConfig["sensorRegionConfig"].get(
              "regionType")
            spEnabled = networkConfig["sensorRegionConfig"].get(
              "regionEnabled")
            tmEnabled = networkConfig["tmRegionConfig"].get(
              "regionEnabled")
            upEnabled = networkConfig["tpRegionConfig"].get(
              "regionEnabled")
            classifierType = networkConfig["classifierRegionConfig"].get(
              "regionType")

            expParams = ("RUNNING EXPERIMENT WITH PARAMS:\n"
                         " * numRecords=%s\n"
                         " * signalAmplitude=%s\n"
                         " * signalMean=%s\n"
                         " * signalPeriod=%s\n"
                         " * noiseAmplitude=%s\n"
                         " * sensorType=%s\n"
                         " * spEnabled=%s\n"
                         " * tmEnabled=%s\n"
                         " * tpEnabled=%s\n"
                         " * classifierType=%s\n"
                         ) % (NUM_RECORDS,
                              signalAmplitude,
                              signalMean,
                              signalPeriod,
                              noiseAmplitude,
                              sensorType.split(".")[1],
                              spEnabled,
                              tmEnabled,
                              upEnabled,
                              classifierType.split(".")[1])
            print expParams

            inputFile = generateSensorData(DATA_DIR,
                                           OUTFILE_NAME,
                                           signalMean,
                                           signalPeriod,
                                           SEQUENCE_LENGTH,
                                           NUM_RECORDS,
                                           signalAmplitude,
                                           NUM_CATEGORIES,
                                           noiseAmplitude)

            dataSource = FileRecordStream(streamID=inputFile)
            network = configureNetwork(dataSource,
                                       networkConfig)
            partitions = generateNetworkPartitions(networkConfig,
                                                   NUM_RECORDS)

            trainNetwork(network, networkConfig, partitions, NUM_RECORDS)
    def testClassificationAccuracy(self):
        """Test classification accuracy for sensor data."""

        networkConfigurations = generateSampleNetworkConfig(
            self.templateNetworkConfig, NUM_CATEGORIES)

        for networkConfig in networkConfigurations:
            for noiseAmplitude in WHITE_NOISE_AMPLITUDES:
                for signalMean in SIGNAL_MEANS:
                    for signalAmplitude in SIGNAL_AMPLITUDES:
                        for signalPeriod in SIGNAL_PERIODS:
                            sensorType = networkConfig[
                                "sensorRegionConfig"].get("regionType")
                            spEnabled = networkConfig[
                                "sensorRegionConfig"].get("regionEnabled")
                            tmEnabled = networkConfig["tmRegionConfig"].get(
                                "regionEnabled")
                            upEnabled = networkConfig["tpRegionConfig"].get(
                                "regionEnabled")
                            classifierType = networkConfig[
                                "classifierRegionConfig"].get("regionType")

                            expParams = ("RUNNING EXPERIMENT WITH PARAMS:\n"
                                         " * numRecords=%s\n"
                                         " * signalAmplitude=%s\n"
                                         " * signalMean=%s\n"
                                         " * signalPeriod=%s\n"
                                         " * noiseAmplitude=%s\n"
                                         " * sensorType=%s\n"
                                         " * spEnabled=%s\n"
                                         " * tmEnabled=%s\n"
                                         " * tpEnabled=%s\n"
                                         " * classifierType=%s\n") % (
                                             NUM_RECORDS, signalAmplitude,
                                             signalMean, signalPeriod,
                                             noiseAmplitude,
                                             sensorType.split(".")[1],
                                             spEnabled, tmEnabled, upEnabled,
                                             classifierType.split(".")[1])
                            print expParams

                            inputFile = generateSensorData(
                                DATA_DIR, OUTFILE_NAME, signalMean,
                                signalPeriod, SEQUENCE_LENGTH, NUM_RECORDS,
                                signalAmplitude, NUM_CATEGORIES,
                                noiseAmplitude)

                            dataSource = FileRecordStream(streamID=inputFile)
                            network = configureNetwork(dataSource,
                                                       networkConfig)
                            partitions = generateNetworkPartitions(
                                networkConfig, NUM_RECORDS)

                            classificationAccuracy = trainNetwork(
                                network, networkConfig, partitions,
                                NUM_RECORDS)

                            if (noiseAmplitude == 0 and signalMean == 1.0
                                    and signalAmplitude == 1.0
                                    and signalPeriod == 20.0
                                    and classifierType == KNN_CLASSIFIER_TYPE
                                    and spEnabled and tmEnabled
                                    and not upEnabled):
                                self.assertEqual(classificationAccuracy,
                                                 100.00)
                            elif (noiseAmplitude == 0 and signalMean == 1.0
                                  and signalAmplitude == 1.0
                                  and signalPeriod == 20.0
                                  and classifierType == CLA_CLASSIFIER_TYPE
                                  and spEnabled and tmEnabled
                                  and not upEnabled):
                                self.assertEqual(classificationAccuracy,
                                                 100.00)
                            elif (noiseAmplitude == 0 and signalMean == 1.0
                                  and signalAmplitude == 1.0
                                  and signalPeriod == 20.0
                                  and classifierType == CLA_CLASSIFIER_TYPE
                                  and spEnabled and not tmEnabled
                                  and not upEnabled):
                                self.assertEqual(classificationAccuracy,
                                                 100.00)
                            elif (noiseAmplitude == 1.0 and signalMean == 1.0
                                  and signalAmplitude == 1.0
                                  and signalPeriod == 20.0
                                  and classifierType == CLA_CLASSIFIER_TYPE
                                  and spEnabled and tmEnabled
                                  and not upEnabled):
                                # using AlmostEqual until the random bug issue is fixed
                                self.assertAlmostEqual(classificationAccuracy,
                                                       80,
                                                       delta=5)
                            elif (noiseAmplitude == 1.0 and signalMean == 1.0
                                  and signalAmplitude == 1.0
                                  and signalPeriod == 20.0
                                  and classifierType == CLA_CLASSIFIER_TYPE
                                  and spEnabled and not tmEnabled
                                  and not upEnabled):
                                # using AlmostEqual until the random bug issue is fixed
                                self.assertAlmostEqual(classificationAccuracy,
                                                       81,
                                                       delta=5)
def run():
  """ Run classification network(s) on artificial sensor data """

  if USE_JSON_CONFIG:
    with open('config/network_configs.json', 'rb') as fr:
      networkConfigurations = json.load(fr)
  else:
    with open("config/network_config_template.json", "rb") as jsonFile:
      templateNetworkConfig = json.load(jsonFile)
      networkConfigurations = generateSampleNetworkConfig(templateNetworkConfig,
                                                          NUM_CATEGORIES)

  expSetups = []
  classificationResults = []
  for signalType in SIGNAL_TYPES:
    for networkConfig in networkConfigurations:
      for noiseAmplitude in WHITE_NOISE_AMPLITUDES:
        for signalMean in SIGNAL_MEANS:
          for signalAmplitude in SIGNAL_AMPLITUDES:
            for numCategories in NUM_CATEGORIES:
              for numReps in NUM_REPS:
                for numPhases in NUM_PHASES:
                  spEnabled = networkConfig["sensorRegionConfig"].get(
                    "regionEnabled")
                  tmEnabled = networkConfig["tmRegionConfig"].get(
                    "regionEnabled")
                  upEnabled = networkConfig["tpRegionConfig"].get(
                    "regionEnabled")
                  classifierType = networkConfig["classifierRegionConfig"].get(
                    "regionType")
      
                  expSetup = generateSensorData(signalType,
                                                DATA_DIR,
                                                numPhases,
                                                numReps,
                                                signalMean,
                                                signalAmplitude,
                                                numCategories,
                                                noiseAmplitude)
                  expSetup['expId'] = len(expSetups)
                  expSetups.append(expSetup)
                  dataSource = FileRecordStream(streamID=expSetup['filePath'])
                  network = configureNetwork(dataSource,
                                             networkConfig)
      
                  partitions = generateNetworkPartitions(networkConfig,
                                                         expSetup['numPoints'])
      
                  classificationAccuracy = trainNetwork(network,
                                                        networkConfig,
                                                        partitions,
                                                        expSetup['numPoints'],
                                                        VERBOSITY)
      
                  classificationResults.append(
                    {
                      'spEnabled': spEnabled,
                      'tmEnabled': tmEnabled,
                      'upEnabled': upEnabled,
                      'classifierType':
                        classifierType.split(".")[1],
                      'classificationAccuracy':
                        classificationAccuracy
                    })

  print_and_save_results(classificationResults, expSetups)