def _apply(self, d): """ Applies classifier to a dataset. """ # Perform preprocessing d = self.preprocessing.apply(d) slices = self.slice_node.apply(d) if slices == None: return if self.application_data == None or not self.application_data_valid: self.application_data = slices self.application_data_valid = True else: self.application_data += slices repetitions_recorded = numpy.min( self.application_data.ninstances_per_class) if repetitions_recorded == self.last_repetitions_recorded: return self.logger.debug('Repetitions recorded: %d' % repetitions_recorded) self.last_repetitions_recorded = repetitions_recorded if repetitions_recorded < self.num_repetitions: return d = psychic.erp(self.application_data) # Perform actual classification try: result = self.classification.apply(d).data candidates = numpy.flatnonzero(numpy.argmax(result, axis=0) == 0) if len(candidates) > 0: winner = candidates[numpy.argmax(result[0, candidates])] else: winner = numpy.argmax(result[0, :]) if winner == self.last_winner: self.num_coherent_classifications += 1 else: self.num_coherent_classifications = 0 self.last_winner = winner self.logger.debug('Coherent classifications: %d' % self.num_coherent_classifications) if self.num_coherent_classifications < self.classifications_needed: self.engine.provide_result([list(result[0, :]), 0]) return self.logger.info('classification result: %d' % winner) self.engine.provide_result([list(result[0, :]), winner + 1]) self.application_data_valid = False self.num_coherent_classifications = 0 self.last_winner = -1 self.last_repetitions_recorded = 0 except ValueError as e: self.logger.error('Classification failed: %s' % e)
def train_(self, d): if self.time_range is None: self.time_range = (0, d.feat_lab[1][-1]) self.time_idx = (0, d.data.shape[1]+1) else: sample_rate = psychic.get_samplerate(d) offset = d.feat_lab[1][0] * sample_rate self.time_idx = [int(x * sample_rate - offset) for x in self.time_range] self.time_idx[1] += 1 erp = psychic.erp(d) if type(self.classes) == int: template = erp.data[:,:,self.classes] else: template = erp.data[:,:,self.classes[0]] - erp.data[:,:,self.classes[1]] self.template = DataSet( data = template, ids = [erp.feat_lab[1]], feat_lab=[erp.feat_lab[0]], ) if self.peak_ch is None: peak = (self.time_idx[0] + np.argmax(np.abs(np.sum( self.template.data[:, self.time_idx[0]:self.time_idx[1]], axis=0)))) else: if type(self.peak_ch) == str: self.peak_ch = self.template.feat_lab[0].index(self.peak_ch) peak = (self.time_idx[0] + np.argmax(np.abs(self.template.data[self.peak_ch, self.time_idx[0]:self.time_idx[1]]))) self.spatial_template = self.template.data[:, [peak]] sigma_x = psychic.nodes.spatialfilter.plain_cov0(self.template) sigma_x += self.reg * np.eye(sigma_x.shape[0]) sigma_x_i = np.linalg.inv(sigma_x) W_spatial = sigma_x_i.dot(self.spatial_template) self.temp_template = psychic.nodes.spatialfilter.sfilter_plain(self.template, W_spatial) data = self.temp_template.data.copy() data[:,:self.time_idx[0]] = 0 data[:,self.time_idx[1]:] = 0 feat_lab=['temp'] self.temp_template = DataSet(data=data, feat_lab=feat_lab, default=self.temp_template)
def train_(self, d): erp = psychic.erp(d) diff = erp.data[:,:,0] - erp.data[:,:,1] self.template = DataSet( data = diff, ids = [erp.feat_lab[1]], feat_lab=erp.feat_lab[0] ) # p0 is the initial guess for the fitting coefficients (A, mu and sigma above) p0 = [.4, .05, 0., -0.25, .75, 1., 0] fit = fit_erp(diff, p0, d.feat_lab[0]) data = fit.reshape(d.data.shape[0], -1) self.template = DataSet(data=data, default=self.template) peak = np.argmax(np.abs(np.sum(self.template.data, axis=0))) self.spatial_template = self.template.data[:, [peak]] sigma_x = psychic.nodes.spatialfilter.plain_cov0(self.template) sigma_x += self.reg * np.eye(sigma_x.shape[0]) sigma_x_i = np.linalg.inv(sigma_x) W_spatial = sigma_x_i.dot(self.spatial_template) self.temp_template = psychic.nodes.spatialfilter.sfilter_plain(self.template, W_spatial)