def downsample(mgr, factor): assert (factor >= 1) ch_num = len(mgr.get_samples()) ch_len = len(mgr.get_samples()[0]) # To be determined ... ret_ch_len = 0 i = 0 left_inds = [] # Determine ret_ch_len - a size of returned channel while i < ch_len: left_inds.append(i) ret_ch_len += 1 i += factor new_samples = array([zeros(ret_ch_len) for i in range(ch_num)]) for i in range(ch_num): for j, ind in enumerate(left_inds): new_samples[i, j] = mgr.get_samples()[i, ind] info_source = copy.deepcopy(mgr.info_source) info_source.get_params()['number_of_samples'] = str(ret_ch_len * ch_num) info_source.get_params()['sampling_frequency'] = str( float(mgr.get_param('sampling_frequency')) / factor) tags_source = copy.deepcopy(mgr.tags_source) samples_source = read_data_source.MemoryDataSource(new_samples) return read_manager.ReadManager(info_source, samples_source, tags_source)
def montage_csa(mgr): new_samples = get_montage( mgr.get_samples(), get_montage_matrix_csa(int(mgr.get_param('number_of_channels')))) info_source = copy.deepcopy(mgr.info_source) tags_source = copy.deepcopy(mgr.tags_source) samples_source = read_data_source.MemoryDataSource(new_samples) return read_manager.ReadManager(info_source, samples_source, tags_source)
def mgr_filter(mgr, wp, ws, gpass, gstop, analog=0, ftype='ellip', output='ba', unit='hz', use_filtfilt=False, meancorr=1.0): if unit == 'radians': b, a = signal.iirdesign(wp, ws, gpass, gstop, analog, ftype, output) elif unit == 'hz': nyquist = float(mgr.get_param('sampling_frequency')) / 2.0 try: wp = wp / nyquist ws = ws / nyquist except TypeError: wp = [i / nyquist for i in wp] ws = [i / nyquist for i in ws] b, a = signal.iirdesign(wp, ws, gpass, gstop, analog, ftype, output) if use_filtfilt: from scipy.signal import filtfilt #samples_source = read_data_source.MemoryDataSource(mgr.get_samples(), False) for i in range(int(mgr.get_param('number_of_channels'))): print("FILT FILT CHANNEL " + str(i)) mgr.get_samples()[i, :] = signal.filtfilt( b, a, mgr.get_samples()[i] - np.mean(mgr.get_samples()[i]) * meancorr) samples_source = read_data_source.MemoryDataSource( mgr.get_samples(), False) else: print("FILTER CHANNELs") filtered = signal.lfilter(b, a, mgr.get_samples()) print("FILTER CHANNELs finished") samples_source = read_data_source.MemoryDataSource(filtered, True) info_source = copy.deepcopy(mgr.info_source) tags_source = copy.deepcopy(mgr.tags_source) new_mgr = read_manager.ReadManager(info_source, samples_source, tags_source) return new_mgr
def exclude_channels(mgr, channels): '''exclude all channels in channels list''' new_params = copy.deepcopy(mgr.get_params()) samples = mgr.get_samples() new_tags = copy.deepcopy(mgr.get_tags()) ex_channels_inds = [ new_params['channels_names'].index(ch) for ch in channels ] assert (-1 not in ex_channels_inds) new_samples = zeros((int(new_params['number_of_channels']) - len(channels), len(samples[0]))) # Define new samples and params list values keys = [ 'channels_names', 'channels_numbers', 'channels_gains', 'channels_offsets' ] keys_to_remove = [] for k in keys: try: #Exclude from keys those keys that are missing in mgr mgr.get_params()[k] except KeyError: keys_to_remove.append(k) continue new_params[k] = [] for k in keys_to_remove: keys.remove(k) new_ind = 0 for ch_ind, ch in enumerate(samples): if ch_ind in ex_channels_inds: continue else: new_samples[new_ind, :] = ch for k in keys: new_params[k].append(mgr.get_params()[k][ch_ind]) new_ind += 1 # Define other new new_params new_params['number_of_channels'] = str( int(new_params['number_of_channels']) - len(channels)) new_params['number_of_samples'] = str(int(new_params['number_of_samples']) - \ len(channels)*len(samples[0])) info_source = read_info_source.MemoryInfoSource(new_params) tags_source = read_tags_source.MemoryTagsSource(new_tags) samples_source = read_data_source.MemoryDataSource(new_samples) return read_manager.ReadManager(info_source, samples_source, tags_source)
def montage_ears(mgr, l_ear_channel, r_ear_channel): left_index = mgr.get_param('channels_names').index(l_ear_channel) right_index = mgr.get_param('channels_names').index(r_ear_channel) if left_index < 0 or right_index < 0: raise Exception("Montage - couldn`t find ears channels: " + str(l_ear_channel) + ", " + str(r_ear_channel)) new_samples = get_montage( mgr.get_samples(), get_montage_matrix_ears(int(mgr.get_param('number_of_channels')), left_index, right_index)) info_source = copy.deepcopy(mgr.info_source) tags_source = copy.deepcopy(mgr.tags_source) samples_source = read_data_source.MemoryDataSource(new_samples) return read_manager.ReadManager(info_source, samples_source, tags_source)
def montage_custom(mgr, chnls): '''apply custom montage to manager, by chnls''' indexes = [] for chnl in chnls: index = mgr.get_param('channels_names').index(chnl) if index < 0: raise Exception("Montage - couldn`t channel: " + str(chnl)) else: indexes.append(index) new_samples = get_montage( mgr.get_samples(), get_montage_matrix_custom(int(mgr.get_param('number_of_channels')), indexes)) info_source = copy.deepcopy(mgr.info_source) tags_source = copy.deepcopy(mgr.tags_source) samples_source = read_data_source.MemoryDataSource(new_samples) return read_manager.ReadManager(info_source, samples_source, tags_source)
def montage_custom_matrix(mgr, montage_matrix): new_samples = get_montage(mgr.get_samples(), montage_matrix) info_source = copy.deepcopy(mgr.info_source) tags_source = copy.deepcopy(mgr.tags_source) samples_source = read_data_source.MemoryDataSource(new_samples) return read_manager.ReadManager(info_source, samples_source, tags_source)