Ejemplo n.º 1
0
def run():
  """ Run classification network(s) on artificial sensor data """
  with open("network_config.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["upRegionConfig"].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"
                         " * upEnabled=%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)

            runNetwork(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["upRegionConfig"].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"
                                " * upEnabled=%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)

                            (numCorrect, numTestRecords, predictionAccuracy) = runNetwork(
                                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(predictionAccuracy, 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(predictionAccuracy, 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(predictionAccuracy, 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(predictionAccuracy, 80, delta=1)
                            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(predictionAccuracy, 81, delta=1)