def _update(self): # Have we collected enough samples without the new input? self.output = self.input_node.output enough_collected = self._samples_collected >=\ self._samples_to_be_collected if not enough_collected: if self.input_node.output is not None and\ self.input_node.output.shape[TIME_AXIS] > 0: self._update_statistics() elif not self._enough_collected: # We just got enough samples self._enough_collected = True print('COLLECTED ENOUGH SAMPLES') ica = ICADialog( self._collected_timeseries.T, list(np.array(self._mne_info['ch_names'])[self._good_ch_inds]), self._ch_locs[self._good_ch_inds, :], self._frequency) ica.exec_() self._ica_rejector = ica.rejection.val.T else: self.output[self._good_ch_inds, :] = np.dot( self._ica_rejector, self.input_node.output[self._good_ch_inds, :])
def runica(x, fs, channels, mode='ica'): from PyQt5.QtWidgets import QApplication from vendor.nfb.pynfb.protocols.ssd.topomap_selector_ica import ICADialog a = QApplication([]) ica = ICADialog(x, channels, fs, mode=mode) ica.exec_() a.exit() return ica.spatial, ica.topography
def get_some_data(real=False): if not real: channels = [ 'Fp1', 'Fp2', 'F7', 'F3', 'Fz', 'F4', 'F8', 'Ft9', 'Fc5', 'Fc1', 'Fc2', 'Fc6', 'Ft10', 'T7', 'C3', 'Cz', 'C4', 'T8', 'Tp9', 'Cp5', 'Cp1', 'Cp2', 'Cp6', 'Tp10', 'P7', 'P3', 'Pz', 'P4', 'P8', 'O1', 'Oz', 'O2' ] data = np.random.normal(loc=0, scale=0.00001, size=(5000, len(channels))).T fs = 500 else: import h5py from vendor.nfb.pynfb.postprocessing.utils import get_info with h5py.File( r'D:\mu_ica\mu_ica\mu_ica_S1_D3_04-21_18-16-03\experiment_data.h5' ) as f: fs, channels, p_names = get_info(f, []) data = f['protocol{}/raw_data'.format( p_names.index('Baseline') + 1)][:].T from PyQt5.QtWidgets import QApplication a = QApplication([]) rej, spatial, top = ICADialog.get_rejection(data.T, channels, fs, mode='ica', states=None)[:3] data = rej.apply(data.T).T return data, fs, channels
def runica2(x, fs, channels, names=('Right', 'Left'), mode='ica'): from PyQt5.QtWidgets import QApplication from vendor.nfb.pynfb.protocols.ssd.topomap_selector_ica import ICADialog a = QApplication([]) res = [] decomposition = None for n in names: print('*** Select component for condition: ' + n) ica = ICADialog(x, channels, fs, decomposition=decomposition, mode=mode) ica.exec_() res.append(np.array((ica.spatial, ica.topography))) decomposition = ica.decomposition a.exit() return res
#data = data.iloc[~get_outliers_mask(data[channels], std=3, iter_numb=2)] #plt.plot(data[channels] + 1000*np.arange(len(channels))) #plt.show() print('ww', sum(get_outliers_mask(data[channels][data['block_number'] < 3]))) # spatial filter (SMR detection) try: filt = np.load('ica_{}.npy'.format(experiment)) #filt = np.zeros(len(channels)) #filt[channels.index('C3')] = 1 except FileNotFoundError: a = QApplication([]) (rej, filt, topo, _unmix, _bandpass, _) = ICADialog.get_rejection( data[channels][data['block_number'].isin( [1, 2] )] #.iloc[~get_outliers_mask(data[channels][data['block_number']<12], std=2)] , channels, fs, mode='csp') #(_rej, filt, topo, _unmix, _bandpass, _) = ICADialog.get_rejection(np.concatenate(list(x[y=='Left']) + l1ist(x[y=='Legs'])), channels, fs, mode='csp') # filt, topography, bandpass, rejections = SelectSSDFilterWidget.select_filter_and_bandpass(np.concatenate(x), ch_names_to_2d_pos(channels), channels, sampling_freq=fs) np.save('ica_{}.npy'.format(experiment), filt) data['SMR'] = np.dot(data[channels], filt) #data = data.iloc[~get_outliers_mask(data[['C3', 'SMR']], std=3, iter_numb=10)] # temporal filter data['SMR_band_filt'] = fft_filter(data['SMR'], fs, (11, 13)) data['SMR_env'] = np.abs(signal.hilbert(data['SMR_band_filt'])) data.to_pickle('data-{}.pkl'.format(experiment))
del raw[list(raw.keys())[-1]] # make csp: if run_ica: from PyQt5.QtWidgets import QApplication ap = QApplication([]) all_keys = [key for key in raw.keys() if 'Left' in key or 'Right' in key or 'Close' in key or 'Open' in key] first = all_keys[:len(all_keys)//2] last = all_keys[len(all_keys)//2:] tops = [] spats = [] fig1, axes = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=False) for j, data in enumerate([first, last]): raw_data = np.concatenate([raw[key] for key in data]) rej, spatial, top = ICADialog.get_rejection(raw_data, channels, fs, mode='ica', states=None)[:3] tops.append(top) spats.append(spatial) plot_topomap(top, ch_names_to_2d_pos(channels), axes=axes[j, 0], show=False) plot_topomap(spatial, ch_names_to_2d_pos(channels), axes=axes[j, 1], show=False) axes[j, 0].set_xlabel('Topography ({})'.format('before' if j == 0 else 'after')) axes[j, 1].set_xlabel('Spatial filter ({})'.format('before' if j == 0 else 'after')) #plt.show() # plot raw data ch_plot = ['C3', 'P3', 'ICA']#, 'Pz', 'Fp1'] fig1, axes = plt.subplots(len(ch_plot), ncols=1, sharex=True, sharey=False) print(axes)
import pylab as plt from scipy.io import loadmat from vendor.nfb.pynfb.widgets.helpers import ch_names_to_2d_pos from vendor.nfb.pynfb.protocols.ssd.topomap_selector_ica import ICADialog from PyQt5.QtWidgets import QApplication fs = 258 pre = loadmat( "C:\\Users\\nsmetanin\\Downloads\\nfb_bci\\wetransfer-07cfaf\\Subj01_POSTdata.mat" ) channels = [a[0] for a in pre["EEGchanslabels"][0]] pre = pre["EEGPOSTdata"] post = loadmat( "C:\\Users\\nsmetanin\\Downloads\\nfb_bci\\wetransfer-07cfaf\\Subj01_PREdata.mat" )["EEGPREdata"] print(pre.shape, post.shape, channels) # print(ch_names_to_2d_pos(channels)) a = QApplication([]) n_samples = 50 print(pre[:, -n_samples:, 0].shape) x = np.concatenate([ np.concatenate([pre[:, -n_samples:, k].T for k in range(pre.shape[2])]), np.concatenate([post[:, :n_samples, k].T for k in range(post.shape[2])]), ]) print(x.shape) ICADialog.get_rejection(x, channels, fs, mode="csp")
# return vals, vecs and topographics (in descending order) reversed_slice = slice(-1, None, -1) topo = inv(vecs[:,reversed_slice]).T return vals[reversed_slice], vecs[:, reversed_slice], topo if __name__ == '__main__': dir_ = 'D:\\vnd_spbu\\pilot\\mu5days' experiment = 'pilot5days_Rakhmankulov_Day3_03-01_12-51-41' with h5py.File('{}\\{}\\{}'.format(dir_, experiment, 'experiment_data.h5')) as f: ica = f['protocol1/signals_stats/left/rejections/rejection1'][:] x_filters = dc_blocker(np.dot(f['protocol1/raw_data'][:], ica)) x_rotation = dc_blocker(np.dot(f['protocol2/raw_data'][:], ica)) x_dict = { 'closed': x_filters[:x_filters.shape[0] // 2], 'opened': x_filters[x_filters.shape[0] // 2:], 'rotate': x_rotation } drop_channels = ['AUX', 'A1', 'A2'] labels, fs = get_lsl_info_from_xml(f['stream_info.xml'][0]) print('fs: {}\nall labels {}: {}'.format(fs, len(labels), labels)) channels = [label for label in labels if label not in drop_channels] scores, unmixing_matrix, topographies = csp3(x_dict, 250, (9, 14), lambda_=0.5, regularization_coef=0.01) app = QtWidgets.QApplication([]) selector = ICADialog(np.vstack((x_filters, x_rotation)), channels, fs, unmixing_matrix=unmixing_matrix, mode='csp', scores=scores) selector.exec_()
elif j > len(p_names) - 3: x = preproc(f['protocol{}/raw_data'.format(j + 1)][:], fs, rejection if reject else None) print(x.shape) raw_after = add_data_simple(raw_after, name, x) xx = np.concatenate([ raw_before['Opened'], raw_before['Baseline'], raw_after['Baseline'], raw_before['Left'], raw_before['Right'], raw_after['Left'], raw_after['Right'], ]) #xx[:, channels.index('C3')] = xx[:, channels.index('C3')] rejection, spatial, topography, unmixing_matrix, bandpass, _ = ICADialog.get_rejection( xx, channels, fs, mode='csp', states=None) from mne.viz import plot_topomap print(spatial) fig, axes = plt.subplots(ncols=rejection.topographies.shape[1]) if not isinstance(axes, type(axes1)): axes = [axes] for ax, top in zip(axes, rejection.topographies.T): plot_topomap(top, ch_names_to_2d_pos(channels), axes=ax, show=False) fig.savefig('csp_S{}_D{}.png'.format(subj, day + 1)) fig.show()
from vendor.nfb.pynfb.signal_processing.helpers import get_outliers_mask import numpy as np from PyQt5 import QtGui, QtWidgets from vendor.nfb.pynfb.protocols.ssd.topomap_selector_ica import ICADialog fs = 1000 band = (8, 14) channels = ['Fp1', 'Fp2', 'F7', 'F3', 'Fz', 'F4', 'F8', 'Ft9', 'Fc5', 'Fc1', 'Fc2', 'Fc6', 'Ft10', 'T3', 'C3', 'Cz', 'C4', 'T4', 'Tp9', 'Cp5', 'Cp1', 'Cp2', 'Cp6', 'Tp10', 'T5', 'P3', 'Pz', 'P4', 'T6', 'O1', 'Oz', 'O2'] data = [loadmat(r'C:\Users\Nikolai\Desktop\Liza_diplom_data\Liza_diplom_data\treatment\p25\day2\proba{}.mat'.format(k))['X1Topo'].T for k in range(1, 16)] df = pd.DataFrame(data=np.concatenate(data), columns=channels) for ch in['Pz', 'Cz']: channels.remove(ch) del df[ch] df = df.loc[~get_outliers_mask(df[channels])] #plt.plot(*welch(df['C3'], fs, nperseg=4*fs)) plt.plot(df[channels]) plt.show() #plt.show() #df['C3env'] = np.abs(hilbert(fft_filter(df['C3'], fs, band))) #df['C4env'] = np.abs(hilbert(fft_filter(df['C4'], fs, band))) #plt.plot(df['C3env']+df['C4env']) a = QtWidgets.QApplication([]) (rej, filt, topo, _unmix, _bandpass, _) = ICADialog.get_rejection(df.iloc[:fs*60*3], channels, fs) df['SMR'] = np.dot(df.as_matrix(), filt) df.to_pickle('p4') plt.plot(df['SMR']) plt.show()
def run_ssd(self, row=None, csp=False, ica=False): if row is None and ica: row = self.table.ica_buttons.index(self.sender()) elif row is None and not csp: row = self.table.buttons.index(self.sender()) elif row is None and csp: row = self.table.csp_buttons.index(self.sender()) ind = self.get_checked_protocols() print(ind) x = concatenate([self.x[j] for j in ind[0]]) if csp: x = concatenate([x] + [self.x[j] for j in ind[1]]) x = dot(x, self.signals[row].rejections.get_prod()) x = x[:, self.channels_mask] to_all = False ica_rejection = None topography = None if ica: reply = QtWidgets.QMessageBox.Yes if len(self.signals[row].rejections) > 0: reply = QtWidgets.QMessageBox.question(self, 'Warning', 'Changing ICA base selection will ' 'invalidate the current rejections (CSP, SSD). ' 'Are you sure you want to continue?', QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No) if reply == QtWidgets.QMessageBox.Yes: result = ICADialog.get_rejection(x, self.channels_names, self.sampling_freq, decomposition=self.ica_unmixing_matrix) ica_rejection, filter, topography, self.ica_unmixing_matrix, bandpass, to_all = result rejections = [] elif csp: rejection, filter, topography, _, bandpass, to_all = ICADialog.get_rejection(x, self.channels_names, self.sampling_freq, mode='csp', _stimulus_split=self.stimulus_split.isChecked(), marks=self.marks) rejections = [rejection] if rejection is not None else [] else: filter, topography, bandpass, rejections = SelectSSDFilterWidget.select_filter_and_bandpass(x, self.pos, self.channels_names, sampling_freq= self.sampling_freq) if filter is not None: filter_copy = np.zeros(len(self.channels_mask)) filter_copy[self.channels_mask] = filter filter = filter_copy if topography is not None: topography_copy = np.zeros(len(self.channels_mask)) topography_copy[self.channels_mask] = topography topography = topography_copy if ica_rejection is not None: ica_rejection = ica_rejection.expand_by_mask(self.channels_mask) rejections = [rej.expand_by_mask(self.channels_mask) for rej in rejections] rows = range(len(self.signals)) if to_all else [row] print(to_all, rows) for row_ in rows: if ica_rejection is not None: self.signals[row_].update_ica_rejection(ica_rejection) if filter is not None: self.signals[row_].update_spatial_filter(filter, topography=topography) if bandpass is not None: self.signals[row_].update_bandpass(bandpass) self.signals[row_].update_rejections(rejections, append=True) modified_flag = len(rejections)>0 or bandpass is not None or filter is not None self.table.update_row(row_, modified=modified_flag)