def _tag_and_classify(self, ch, method, properties, body): """Tag data and runs it through the classifier""" self._update_last_tag() input_data = simplejson.loads(body) timestamp = input_data["timestamp"] if self.maxval is not None and self.minval is not None: value = np.clip(input_data["channel_0"], self.minval, self.maxval) else: value = input_data["channel_0"] classificationResults = classifyNextRecord(self._network, self.network_config, timestamp, value, self.last_tag["channel_0"]) inferredCategory = classificationResults["bestInference"] _LOGGER.debug("Raw results: %s" % classificationResults) buffer = [{"timestamp": timestamp, "channel_0": inferredCategory}] self.output_metric_publisher.publish(self.routing_keys[self.output_metric], buffer)
num_correct = 0 for category in range(_NUM_CATEGORIES): csvFile = open(_INPUT_FILES[category], "rb") reader = csv.reader(csvFile) # skip 3 header rows reader.next() reader.next() reader.next() for row in reader: data = dict(zip(headers, row)) x = int(data["x"]) / 1000000 # s y = float(data["y"]) label = int(category) classificationResults = classifyNextRecord(network, _CONFIG, x, y, label) inferredCategory = classificationResults["bestInference"] print "Actual: %s | Predicted: %s " % (category, inferredCategory) if int(inferredCategory) == int(category): num_correct += 1 print "ACCURACY: %s " % (float(num_correct) / (_NUM_RECORDS * _NUM_CATEGORIES)) print "Serializing model to: %s" % _MODEL_NAME setNetworkLearningMode(network, _REGION_NAMES, False) network.save(_MODEL_NAME) print "Predict 40: %s" % classifyNextRecord(network, _CONFIG, -1, 40, -1)
reader.next() readers[category] = reader num_correct = 0.0 for row_id in range(_NUM_RECORDS): # alternate categories for category in range(_NUM_CATEGORIES): row = readers[category].next() data = dict(zip(headers, row)) x = int(data["x"]) / 1000000 # s y = float(data["y"]) label = int(category) classificationResults = classifyNextRecord(network, _CONFIG, x, y, label) inferredCategory = classificationResults["bestInference"] print "Timestamp: %s, Data: %d " % (x, y) print "Actual: %s | Predicted: %s " % (category, inferredCategory) if int(inferredCategory) == int(category): num_correct += 1 print "ACCURACY: %s " % (num_correct / (_NUM_RECORDS * _NUM_CATEGORIES)) setNetworkLearningMode(network, _REGION_NAMES, False) network.save("motor_imagery.nta") #