def toggle_complete_channel(self): if self.channel_mode == 'off': return False channel = self.ch_names[self.channel_mode_index] if channel in self.complete_bad_channel: self.complete_bad_channel.pop(self.complete_bad_channel.index(channel)) for bc in self.bad_channels: if bc.channel == channel and bc.annotation == 'all': self.delete_bad_epoch(bc.epoch_id) break else: sboundary = bad_epoch.Boundary(0,'start') eboundary = bad_epoch.Boundary(self.data.shape[1],'end') self.bad_channels.append(bad_channel.Bad_channel(channel,start_boundary = sboundary,end_boundary = eboundary, pp_id = self.pp_id, coder = self.coder,exp_type = self.exp_type, bid = self.bid, block_st_sample = self.block_st_sample, epoch_id = self.make_bad_epoch_id(),offset = self.channel_mode_index *self.offset_value,annotation = 'all')) self.complete_bad_channel.append(channel) self.handle_plot(force_redraw=True)
def xml2bad_channels(self, load_data=True, multiplier=1): '''Create a list of bad epochs from xml file.''' self.bad_channels = [] if load_data: self.load_xml() for bc_xml in self.artifacts.iter('bad_channel'): # fetch subelements element_names = 'channel,st_sample,et_sample,block_st_sample,block_et_sample,pp_id,bid,annotation,color,exp_type,epoch_ids,coder,correct'.split( ',') element_values = [] for e in element_names: if not bc_xml.find(e) == None: element_values.append(bc_xml.find(e).text) else: element_values.append('NA') channel, st_sample, et_sample, block_st_sample, block_et_sample, pp_id, bid, annotation, color, exp_type, epoch_ids, coder, correct = element_values if st_sample == 'NA' or et_sample == 'NA': continue epoch_id = bc_xml.attrib['id'] #create start and end boundary # print(st_sample,et_sample) start = bad_epoch.Boundary(x=int(int(st_sample) * multiplier), boundary_type='start', visible=False) end = bad_epoch.Boundary(x=int(int(et_sample) * multiplier), boundary_type='end', visible=False) # create bad epoch bc = bad_channel.Bad_channel(channel, start_boundary=start, end_boundary=end, annotation=annotation, pp_id=pp_id, exp_type=exp_type, bid=bid, block_st_sample=block_st_sample, epoch_id=epoch_id, visible=False, epoch_ids=epoch_ids, block_et_sample=block_et_sample, coder=coder, correct=correct, color=color) self.bad_channels.append(bc) # print('N bad channels:',len(self.bad_channels)) return self.bad_channels
def handle_end(self): '''Create a end boundary, and either add this to closest start boundary or create new epoch.''' if self.event.xdata < self.data.shape[1]: x = self.event.xdata else: x = self.data.shape[1] - 1 boundary = bad_epoch.Boundary(x,'end') if self.channel_mode == 'on': self.channel_boundaries.append(boundary) else: self.boundaries.append(boundary) be = self.find_completion_bad_epoch(boundary_type = 'start') if be: print('combining boundaries') be.set_end(boundary) if self.channel_mode == 'on': i = self.ch_names.index(be.channel) be.plot(channel_data = self.data[i],offset = i * self.offset_value) else: if self.channel_mode == 'on': channel = self.ch_names[self.channel_mode_index] self.bad_channels.append(bad_channel.Bad_channel(channel,end_boundary = boundary, pp_id = self.pp_id, exp_type = self.exp_type, bid = self.bid, block_st_sample = self.block_st_sample, epoch_id = self.make_bad_epoch_id(),offset = self.channel_mode_index * self.offset_value,annotation = self.default_annotation_channel)) else: self.bad_epochs.append(bad_epoch.Bad_epoch(end_boundary = boundary, pp_id = self.pp_id, exp_type = self.exp_type, bid = self.bid, block_st_sample = self.block_st_sample, epoch_id = self.make_bad_epoch_id(),annotation = self.default_annotation))
def xml2bad_channel(self): '''Create a list of bad epochs from xml file.''' self.artifacts = [] # print('Starting with list of',len(self.artifacts),' bad epochs') for bc_xml in self.cnn_result.iter('bad_channel'): # fetch subelements element_names = 'channel,st_sample,et_sample,block_st_sample,pp_id,bid,annotation,color,exp_type,coder,note,correct,corrector,perc_clean'.split( ',') element_values = [] for e in element_names: if bc_xml.find(e) != None: element_values.append(bc_xml.find(e).text) else: element_values.append('NA') channel, st_sample, et_sample, block_st_sample, pp_id, bid, annotation, color, exp_type, coder, note, correct, corrector, perc_clean = element_values epoch_id = bc_xml.attrib['id'] #create start and end boundary start = Boundary(x=int(st_sample), boundary_type='start', visible=False) end = Boundary(x=int(et_sample), boundary_type='end', visible=False) # create bad epoch bc = bad_channel.Bad_channel(channel=channel, start_boundary=start, end_boundary=end, annotation=annotation, color=color, pp_id=pp_id, exp_type=exp_type, bid=bid, block_st_sample=block_st_sample, epoch_id=epoch_id, visible=False, correct=correct, perc_clean=perc_clean, coder=self.cnn_ch_model_name) self.artifacts.append(bc) # print('N bad epoch:',len(self.artifacts),'artifacts') return self.artifacts
def make_bad_channel(self, channel_index, minimal_duration=None): if minimal_duration == None: minimal_duration = self.minimal_clean_duration ws = self.w.windows['sf1000'] previous_annotation = '' perc_clean = [] bad_channels = [] name = self.ch_names[channel_index] for sample_index in range(self.pred_class.shape[0]): annotation = 'clean' if self.pred_class[ sample_index, channel_index] == 0 else 'artifact' perc_clean.append(self.pred_perc[sample_index, channel_index]) if previous_annotation == '': # first index st_sample = ws.start_snippets[sample_index] previous_annotation = annotation elif previous_annotation != annotation or sample_index == self.pred_class.shape[ 0] - 1: # start of a new bad channel, handle creation previous bad channel b = self.b if sample_index == self.pred_class.shape[0] - 1: # last sample of block if b.start_marker_missing or b.end_marker_missing: et_sample = self.pred_class.shape[0] * 10 else: et_sample = b.duration_sample else: # the start/end of bad_channel is half way between start/end sample index et_sample = (ws.start_snippets[sample_index - 1] + ws.end_snippets[sample_index - 1]) / 2 start = Boundary(x=int(st_sample), boundary_type='start', visible=False) end = Boundary(x=int(et_sample), boundary_type='end', visible=False) epoch_id = get_cnn_ch_epoch_id(increment=True) perc_clean_last = perc_clean.pop(-1) perc_clean = ' '.join([ str(np.mean(np.array(perc_clean))), str(np.std(np.array(perc_clean))) ]) # create bad channel bc = bad_channel.Bad_channel(channel=name, start_boundary=start, end_boundary=end, annotation=previous_annotation, pp_id=b.pp_id, exp_type=b.exp_type, bid=b.bid, block_st_sample=b.st_sample, epoch_id=epoch_id, coder=self.cnn_ch_model_name, correct='unk', visible=False, perc_clean=perc_clean) self.bad_channels[name].append(bc) st_sample = et_sample + 1 previous_annotation = annotation perc_clean = [perc_clean_last] if len(self.bad_channels[name]) > 1: self.combine_bad_channels(channel_index, minimal_duration) else: self.artifacts.extend(copy.deepcopy(self.bad_channels[name]))