def __init__(self,
               user_id,
               device_type,
               rmq_address,
               rmq_user,
               rmq_pwd,
               input_metrics,
               output_metrics):

    self.module_id = str(uuid.uuid4())
    
    self.user_id = user_id
    self.device_type = device_type
    self.rmq_address = rmq_address
    self.rmq_user = rmq_user
    self.rmq_pwd = rmq_pwd
    self.input_metrics = None
    self.output_metrics = None

    self.eeg_subscriber = None
    self.mu_publisher = None
    self.routing_keys = None
    self.preprocessor = None

    self.num_channels = get_num_channels(self.device_type, "eeg")
    self.eeg_data = np.zeros((0, self.num_channels))
    self.count = 0

    self.eyeblinks_remover = EyeBlinksFilter()

    self.step_size = None
    self.electrodes_placement = None
    self.enable_ica = False
    
    self.started_fit = False
  def train(self):
    categories = range(self.num_categories)
    num_channels = get_num_channels(self.device_type, self.input_metric)

    labels = []
    input_values = []
    for category in categories:
      input_file = TRAINING_DATA_PATH % {
        "tag": category,
        "metric": self.input_metric,
        "device": self.device_type
      }
      with open(input_file, "rb") as csvFile:
        reader = csv.reader(csvFile)
        headers = reader.next()

        for row in reader:
          data = dict(zip(headers, row))
          channel_values = []
          category = data["tag"]
          for i in range(num_channels):
            channel = "channel_%s" % i
            channel_values.append(float(data[channel]))
          input_values.append(channel_values)
          labels.append(category)

    num_records = len(input_values)
    num_train_records = num_records * 2 / 3
    train_input_values = input_values[:num_train_records]
    test_input_values = input_values[num_train_records:]
    train_labels = labels[:num_train_records]
    test_labels = labels[num_train_records:]

    X = np.array(train_input_values)
    y = np.array(train_labels)

    if self.classifier_type == "svm":
      clf = svm.LinearSVC()
    else:
      raise ValueError("Classifier type is '%s' but can only be: %s"
                       % VALID_CLASSIFIER_TYPES)

    clf.fit(X, y)

    X_test = np.array(test_input_values)
    y_test = np.array(test_labels)

    score = clf.score(X_test, y_test)

    classifier_path = CLASSIFIER_PATH % {
      "classifier_type": self.classifier_type,
      "device": self.device_type,
      "input_metric": self.input_metric
    }

    model_path = os.path.join(
      os.path.dirname(brainsquared.__file__), "module_runners",
      classifier_path)
    joblib.dump(clf, model_path)
    return classifier_path, score
Example #3
0
  def __init__(self,
               user_id,
               device_type,
               rmq_address,
               rmq_user,
               rmq_pwd,
               input_metrics,
               output_metrics,
               module_id=None):
    """
    Collect, tag data (optional) and write to a CSV file. 
    
    :param user_id: ID of the user using the device.
    :type user_id: string
    
    :param device_type: type of the device publishing to this module. 
    :type device_type: string
    
    :param rmq_address: address of the RabbitMQ server.
    :type rmq_address: string
        
    :param rmq_user: login for RabbitMQ connection.
    :type rmq_user: string
    
    :param rmq_pwd: password for RabbitMQ connection.
    :type rmq_pwd: string
        
    :param input_metrics: name of the input metric.
    :type input_metrics: dict
    
    :param output_metrics: name of the output metric.
    :type output_metrics: dict
    
    :param module_id: (Optional. Default = None) ID of the module
    :type module_id: string
    """

    super(CSVWriterSink, self).__init__(user_id,
                                        device_type,
                                        rmq_address,
                                        rmq_user,
                                        rmq_pwd,
                                        input_metrics,
                                        output_metrics,
                                        module_id)

    self.num_input_channels = get_num_channels(device_type, self.input_metric)

    # Set when configure() is called.
    self.tag = None
    self.time_offset = None
    self.recording_time = None
    self.csv_writer = None

    # Module specific.
    self.start_time = int(time.time() * 1000)
    self.channels = ["channel_%s" % i for i in range(self.num_input_channels)]
    self.headers = ["timestamp"] + self.channels
  def __init__(self,
               user_id,
               device_type,
               rmq_address,
               rmq_user,
               rmq_pwd,
               input_metrics,
               output_metrics,
               module_id=None):

    """
    Classify with simple thresholds.
    
    :param user_id: ID of the user using the device.
    :type user_id: string
    
    :param device_type: type of the device publishing to this module. 
    :type device_type: string
    
    :param rmq_address: address of the RabbitMQ server.
    :type rmq_address: string
        
    :param rmq_user: login for RabbitMQ connection.
    :type rmq_user: string
    
    :param rmq_pwd: password for RabbitMQ connection.
    :type rmq_pwd: string
        
    :param input_metrics: name of the input metric.
    :type input_metrics: dict
    
    :param output_metrics: name of the output metric.
    :type output_metrics: dict
    
    :param module_id: (Optional. Default = None) ID of the module
    :type module_id: string
    """

    super(ThresholdClassifier, self).__init__(user_id,
                                              device_type,
                                              rmq_address,
                                              rmq_user,
                                              rmq_pwd,
                                              input_metrics,
                                              output_metrics,
                                              module_id)

    self.num_input_channels = get_num_channels(device_type, self.input_metric)

    # Set when configure() is called.
    self.thresholds = None

    # Module specific. Keep track of the last classification.
    self._last_classification = {
      "timestamp": int(time.time() * 1000),
      "channel_0": self.num_input_channels
    }
  def __init__(self,
               user_id,
               device_type,
               rmq_address,
               rmq_user,
               rmq_pwd,
               input_metrics,
               output_metrics,
               module_id=None):

    """
    :param user_id: ID of the user using the device.
    :type user_id: string
    
    :param device_type: type of the device publishing to this module. 
    :type device_type: string
    
    :param rmq_address: address of the RabbitMQ server.
    :type rmq_address: string
        
    :param rmq_user: login for RabbitMQ connection.
    :type rmq_user: string
    
    :param rmq_pwd: password for RabbitMQ connection.
    :type rmq_pwd: string
        
    :param input_metrics: name of the input metric.
    :type input_metrics: dict
    
    :param output_metrics: name of the output metric.
    :type output_metrics: dict
    
    :param module_id: (Optional. Default = None) ID of the module
    :type module_id: string
    """

    super(SKLearnClassifier, self).__init__(user_id,
                                            device_type,
                                            rmq_address,
                                            rmq_user,
                                            rmq_pwd,
                                            input_metrics,
                                            output_metrics,
                                            module_id)

    self.num_input_channels = get_num_channels(device_type, self.input_metric)

    # Set when configure() is called.
    self.classifier_path = None
    self.classifier = None
    self.classifier_type = None
    self.num_categories = None