def __init__(self, p_mgrs, p_info_source, p_avg_name, p_samples_to_norm=0, p_avg_len=None): avg_samples = erp_avg.get_normalised_avgs(p_mgrs, p_samples_to_norm, p_avg_len) tags_source = read_tags_source.MemoryTagsSource([]) p_info_source.get_params()['epoch_name'] = p_avg_name samples_source = read_data_source.MemoryDataSource(avg_samples) super(AveragedEpochs, self).__init__(p_info_source, samples_source, tags_source)
def get_averaged_samples(self, p_train_set, p_num_per_avg, p_start_samples_to_norm=0): """ >>> dr = '/media/windows/wiedza/bci/EKSPERYMENTY_DANE/p300_10_12_2010/numbered_squares/' >>> f2_name = 'p300_128hz_laptop_training_6x6_squareNUMBERS_CATDOGFISHWATERBOWL_longer_8trials' >>> f2 = {'info': os.path.join(dr, f2_name+'.obci.filtered.xml'), 'data': os.path.join(dr, f2_name+'_10HZ.obci.bin'), 'tags':os.path.join(dr, f2_name+'.obci.arts_free.svarog.tags')} >>> t = extract_trials_from_file(f2) >>> avg = RandomAverager() >>> s, c = avg.get_averaged_samples(t, 5) >>> sm = sum([len(i['target']) for i in t])/5+sum([len(i['non-target']) for i in t])/5 >>> len(s[0]) == sm == len(c) True >>> len(s) == 24 True """ # Gather target and non-target tag to separate collections target_tags = [] non_target_tags = [] for i_set in p_train_set: target_tags += i_set['target'] non_target_tags += i_set['non-target'] # shuffle both collections random.shuffle(target_tags) random.shuffle(non_target_tags) # Define some crucial variables n_target = len(target_tags) / p_num_per_avg n_non_target = len(non_target_tags) / p_num_per_avg avg_size = len(target_tags[0].get_samples()[0]) ch_num = len(target_tags[0].get_samples()) # Define to-be-returned variables: # an array of numbers representing trials classes (target 1, non-target -1) ret_classes = scipy.zeros(n_non_target + n_target) # A list of arrays (for every channel) # One array is a 2dim array with: ret_arrs = [] ret_arr_index = 0 for i in range(ch_num): ret_arrs.append(scipy.zeros((n_target + n_non_target, avg_size))) # Fill ret_arss i = 0 while i/p_num_per_avg < n_target: avg_arr = erp_avg.get_normalised_avgs(target_tags[i:(i+p_num_per_avg)], p_start_samples_to_norm, avg_size) for j in range(ch_num): ret_arrs[j][ret_arr_index, :] = avg_arr[j, :] ret_classes[ret_arr_index] = 1.0 ret_arr_index += 1 i += p_num_per_avg i = 0 while i/p_num_per_avg < n_non_target: avg_arr = erp_avg.get_normalised_avgs(non_target_tags[i:(i+p_num_per_avg)], p_start_samples_to_norm, avg_size) for j in range(ch_num): ret_arrs[j][ret_arr_index, :] = avg_arr[j, :] ret_classes[ret_arr_index] = -1.0 ret_arr_index += 1 i += p_num_per_avg #print("R: ", str(ret_arr_index), ", N: ", str(n_target + n_non_target)) assert(ret_arr_index == n_target + n_non_target) return ret_arrs, ret_classes
def plot_avgs_for(p_files, plot_type='all', p_show=True, x=None, y=None, channel_names=None): mgr_neu1 = smart_tags_manager.SmartTagsManager(NEU1_TAG_DEF, p_files['info'], p_files['data'], p_files['tags']) mgr_neu1.kamil_desc = "Neutr1" mgr_neu3 = smart_tags_manager.SmartTagsManager(NEU3_TAG_DEF, p_files['info'], p_files['data'], p_files['tags']) mgr_neu3.kamil_desc = "Neutr3" mgr_neg1 = smart_tags_manager.SmartTagsManager(NEG1_TAG_DEF, p_files['info'], p_files['data'], p_files['tags']) mgr_neg1.kamil_desc = "N1" mgr_neg3 = smart_tags_manager.SmartTagsManager(NEG3_TAG_DEF, p_files['info'], p_files['data'], p_files['tags']) mgr_neg3.kamil_desc = "N3" mgr_pos1 = smart_tags_manager.SmartTagsManager(POS1_TAG_DEF, p_files['info'], p_files['data'], p_files['tags']) mgr_pos1.kamil_desc = "P1" mgr_pos3 = smart_tags_manager.SmartTagsManager(POS3_TAG_DEF, p_files['info'], p_files['data'], p_files['tags']) mgr_pos3.kamil_desc = "P3" mgr_dr = smart_tags_manager.SmartTagsManager(DREWNO_TAG_DEF, p_files['info'], p_files['data'], p_files['tags']) mgr_dr.kamil_desc = "Drewno" mgrs = [] if plot_type == 'all': mgrs.append(mgr_neu1) mgrs.append(mgr_neu3) mgrs.append(mgr_neg1) mgrs.append(mgr_neg3) mgrs.append(mgr_pos1) mgrs.append(mgr_pos3) mgrs.append(mgr_dr) elif plot_type == 'system': mgr_pos1.kamil_desc = "Aut1 = P1+N1" mgr_pos1.get_smart_tags() mgr_pos1._smart_tags += mgr_neg1.get_smart_tags() mgrs.append(mgr_pos1) mgr_pos3.kamil_desc = "Refl3 = P3+N3" mgr_pos3.get_smart_tags() mgr_pos3._smart_tags += mgr_neg3.get_smart_tags() mgrs.append(mgr_pos3) mgr_neu1.kamil_desc = "Neu = Neu1+Neu3" mgr_neu1.get_smart_tags() mgr_neu1._smart_tags += mgr_neu3.get_smart_tags() mgrs.append(mgr_neu1) mgrs.append(mgr_dr) elif plot_type == 'sign': mgr_pos1.kamil_desc = "Pos = P1+P3" mgr_pos1.get_smart_tags() mgr_pos1._smart_tags += mgr_pos3.get_smart_tags() mgrs.append(mgr_pos1) mgr_neg1.kamil_desc = "Neg = N1+N3" mgr_neg1.get_smart_tags() mgr_neg1._smart_tags += mgr_neg3.get_smart_tags() mgrs.append(mgr_neg1) mgr_neu1.kamil_desc = "Neu = Neu1+Neu3" mgr_neu1.get_smart_tags() mgr_neu1._smart_tags += mgr_neu3.get_smart_tags() mgrs.append(mgr_neu1) mgrs.append(mgr_dr) #mgr_pos3.kamil_desc = "P3+N3" #mgr_pos3.get_smart_tags() #mgr_pos3._smart_tags += mgr_neg3.get_smart_tags() #mgrs.append(mgr_pos3) if channel_names is None: import math channel_names = [mgr_pos.get_param('channels_names')] x = int(math.sqrt(len(channels_names))) y = ceil(math.sqrt(len(channels_names))) for i, channels in enumerate(channel_names): plotter = erp_plot.Plotter(x, y, i) plt_id = 0 for i_ch_name in channels: plt_id += 1 for mgr in mgrs: avgs_for_channels = erp_avg.get_normalised_avgs( mgr, abs(START_SEC_OFFSET) * 1024) * 0.0715 ch = get_channel_data(mgr_dr, avgs_for_channels, i_ch_name) if ch is None: LOGGER.warning("Couldn`t find channel for name: " + i_ch_name) else: samp = float( mgr.get_read_manager().get_param('sampling_frequency')) plotter.add_plot(ch, mgr.kamil_desc + " " + i_ch_name, [ i for i in range( START_SEC_OFFSET * samp - 1, len(ch) - (abs(START_SEC_OFFSET) * samp)) ], plt_id) #store_avgs_for_svarog([ch]) plotter.prepare_to_show() if p_show: erp_plot.show()
def plot_avgs_for(p_files, plot_type='all', p_show=True, x=None, y=None, channel_names=None): mgr_neu1 = smart_tags_manager.SmartTagsManager(NEU1_TAG_DEF, p_files['info'], p_files['data'], p_files['tags']) mgr_neu1.kamil_desc = "Neutr1" mgr_neu3 = smart_tags_manager.SmartTagsManager(NEU3_TAG_DEF, p_files['info'], p_files['data'], p_files['tags']) mgr_neu3.kamil_desc = "Neutr3" mgr_neg1 = smart_tags_manager.SmartTagsManager(NEG1_TAG_DEF, p_files['info'], p_files['data'], p_files['tags']) mgr_neg1.kamil_desc = "N1" mgr_neg3 = smart_tags_manager.SmartTagsManager(NEG3_TAG_DEF, p_files['info'], p_files['data'], p_files['tags']) mgr_neg3.kamil_desc = "N3" mgr_pos1 = smart_tags_manager.SmartTagsManager(POS1_TAG_DEF, p_files['info'], p_files['data'], p_files['tags']) mgr_pos1.kamil_desc = "P1" mgr_pos3 = smart_tags_manager.SmartTagsManager(POS3_TAG_DEF, p_files['info'], p_files['data'], p_files['tags']) mgr_pos3.kamil_desc = "P3" mgr_dr = smart_tags_manager.SmartTagsManager(DREWNO_TAG_DEF, p_files['info'], p_files['data'], p_files['tags']) mgr_dr.kamil_desc = "Drewno" mgrs = [] if plot_type == 'all': mgrs.append(mgr_neu1) mgrs.append(mgr_neu3) mgrs.append(mgr_neg1) mgrs.append(mgr_neg3) mgrs.append(mgr_pos1) mgrs.append(mgr_pos3) mgrs.append(mgr_dr) elif plot_type == 'system': mgr_pos1.kamil_desc = "Aut1 = P1+N1" mgr_pos1.get_smart_tags() mgr_pos1._smart_tags += mgr_neg1.get_smart_tags() mgrs.append(mgr_pos1) mgr_pos3.kamil_desc = "Refl3 = P3+N3" mgr_pos3.get_smart_tags() mgr_pos3._smart_tags += mgr_neg3.get_smart_tags() mgrs.append(mgr_pos3) mgr_neu1.kamil_desc = "Neu = Neu1+Neu3" mgr_neu1.get_smart_tags() mgr_neu1._smart_tags += mgr_neu3.get_smart_tags() mgrs.append(mgr_neu1) mgrs.append(mgr_dr) elif plot_type == 'sign': mgr_pos1.kamil_desc = "Pos = P1+P3" mgr_pos1.get_smart_tags() mgr_pos1._smart_tags += mgr_pos3.get_smart_tags() mgrs.append(mgr_pos1) mgr_neg1.kamil_desc = "Neg = N1+N3" mgr_neg1.get_smart_tags() mgr_neg1._smart_tags += mgr_neg3.get_smart_tags() mgrs.append(mgr_neg1) mgr_neu1.kamil_desc = "Neu = Neu1+Neu3" mgr_neu1.get_smart_tags() mgr_neu1._smart_tags += mgr_neu3.get_smart_tags() mgrs.append(mgr_neu1) mgrs.append(mgr_dr) #mgr_pos3.kamil_desc = "P3+N3" #mgr_pos3.get_smart_tags() #mgr_pos3._smart_tags += mgr_neg3.get_smart_tags() #mgrs.append(mgr_pos3) if channel_names is None: import math channel_names = [mgr_pos.get_param('channels_names')] x = int(math.sqrt(len(channels_names))) y = ceil(math.sqrt(len(channels_names))) for i, channels in enumerate(channel_names): plotter = erp_plot.Plotter(x, y, i) plt_id = 0 for i_ch_name in channels: plt_id += 1 for mgr in mgrs: avgs_for_channels = erp_avg.get_normalised_avgs(mgr, abs(START_SEC_OFFSET)*1024)*0.0715 ch = get_channel_data(mgr_dr, avgs_for_channels, i_ch_name) if ch is None: LOGGER.warning("Couldn`t find channel for name: "+i_ch_name) else: samp = float(mgr.get_read_manager().get_param('sampling_frequency')) plotter.add_plot(ch, mgr.kamil_desc+" "+i_ch_name, [i for i in range(START_SEC_OFFSET*samp-1, len(ch)-(abs(START_SEC_OFFSET)*samp))], plt_id) #store_avgs_for_svarog([ch]) plotter.prepare_to_show() if p_show: erp_plot.show()