def test_update_figure(self): from pynfb.inlets.montage import Montage montage = Montage(names=['Fp1', 'Fp2', 'Cz', 'AUX', 'MEG 2632']) print(montage) data = np.random.randn(3) pos = np.array([(0, 0), (1, -1), (-1, -1)]) self.update_figure(data=data, pos=pos, names=['c1', 'c2', 'oz'], montage=montage)
] signals += [CompositeSignal(signals, '', 'Composite', 3, fs=500)] signals += [BCISignal(500, channels, 'bci', n_ch)] app = QtGui.QApplication([]) x = np.random.randn(5000, n_ch) from pynfb.widgets.helpers import ch_names_to_2d_pos #x[2500:3000, channels.index('Cz')] /= 50 x = x[:50000] #marks = marks[:50000] #x[2500:2600, [0, 3]] *= 100 #marks = np.zeros(len(x)*9) #marks[2500::5000] = 1 #marks[10000] = 1 montage = Montage(channels[:-3] + ['Oz', 'O1', 'AUX']) print(montage) #montage = None w = SignalsSSDManager(signals, [x], montage, None, None, [], protocol_seq=['One'], sampling_freq=258) w.exec() #plt.plot(np.arange(50000)/258, np.dot(x, signals[0].spatial_filter)) #plt.plot(np.arange(50000) / 258, marks * np.max(np.dot(x, signals[0].spatial_filter))) #plt.show() app.exec_()
a = QApplication([]) ica = ICADialog(x, channels, fs, mode=mode) ica.exec_() a.exit() return ica.spatial, ica.topography def runica2(x, fs, channels, names=('Right', 'Left'), mode='ica'): from PyQt5.QtWidgets import QApplication from 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 if __name__ == '__main__': from mne.viz import plot_topomap from pynfb.inlets.montage import Montage from pynfb.generators import ch_names32 montage = Montage(ch_names32) spatial, topo = runica(np.random.normal(size=(100000, 32)), 1000, montage.get_names(), mode='csp') plot_topomap(spatial, montage.get_pos()) plot_topomap(topo, montage.get_pos())
from pynfb.postprocessing.mu_or_not.meta import * from pynfb.signal_processing.decompositions import ICADecomposition from mne.viz import plot_topomap from pynfb.inlets.montage import Montage group = 'treatment' subj = 'VV' day = 1 for day in [1, 2]: mat = loadmat(r'C:\Users\Nikolai\Desktop\Liza_diplom_data\Liza_diplom_data\treatment\{0}\bci\{0}_{1}1.mat'.format(subj, day)) channels = [ch[0] for ch in mat['chan_names'][0]] montage = Montage(channels) df = pd.DataFrame(data=mat['data_cur'].T, columns=channels) df['state'] = mat['states_cur'][0] print(df['state'].unique()) df = df.loc[~get_outliers_mask(df[channels], iter_numb=10, std=3)] plt.plot(df[channels]) plt.show() a = QtGui.QApplication([]) #ica = ICADialog(np.concatenate([df.loc[df['state']==6, channels], df.loc[df['state']==1, channels]]), channels, fs, mode='csp') ica = ICADialog(df[channels], channels, fs, mode='ica') ica.exec_() print(ica.table.get_checked_rows())
subj = all_subjects[0] day = 1 fig, axes = plt.subplots(2*len(norm_subjects), 6) for s, subj in enumerate(norm_subjects[:]): for day in range(2): axes[day + 2*s, 0].set_ylabel('{}-d{}'.format(subj, day+1)) mat = loadmat( r'C:\Users\Nikolai\Desktop\Liza_diplom_data\Liza_diplom_data\treatment\{0}\bci\{0}_{1}1.mat'.format(subj, day+1)) channels = [ch[0] for ch in mat['chan_names'][0]] montage = Montage(channels) df = pd.DataFrame(data=mat['data_cur'].T, columns=channels) df['state'] = mat['states_cur'][0] df = df.loc[~get_outliers_mask(df[channels], iter_numb=10, std=3)] df['block_name'] = df['state'].apply(lambda x: {6: 'Rest', 1: 'Left', 2: 'Right'}[x]) filters = np.load(r'treatment\{0}d{1}_filters.npy'.format(subj, day+1)) topos = np.load(r'treatment\{0}d{1}_topographies.npy'.format(subj, day+1)) ind = np.load(r'treatment\{0}d{1}_smr_ind.npy'.format(subj, day+1)) for k in range(2): spat = filters[:, ind[k]] topo = topos[:, ind[k]] df['smr'] = np.dot(df[channels], spat) axes[day + 2*s, 0 + 3*k].set_xlabel('Spat. filt.') plot_topomap(spat, montage.get_pos(), axes=axes[day + 2*s, 0+ 3*k], show=False, contours=0) axes[day + 2*s, 1+ 3*k].set_xlabel('Topography')
df, fs, p_names, channels = load_data('{}{}/experiment_data.h5'.format( data_dir, exp.dataset)) df = df[~get_outliers_mask(df[channels], std=3)] right, left = runica2( df.loc[df['block_number'].isin([1, 2, 3, 7, 8, 9]), channels], fs, channels, ['RIGHT', 'LEFT']) np.save(wdir + desc + '-RIGHT.npy', right) np.save(wdir + desc + '-LEFT.npy', left) # load and plot right = np.load(wdir + desc + '-RIGHT.npy') left = np.load(wdir + desc + '-LEFT.npy') f, ax = plt.subplots(1, 4) plot_topomap(right[0], Montage(channels).get_pos(), contours=0, axes=ax[0], show=False) plot_topomap(right[1], Montage(channels).get_pos(), contours=0, axes=ax[1], show=False) plot_topomap(left[0], Montage(channels).get_pos(), contours=0, axes=ax[2], show=False) plot_topomap(left[1], Montage(channels).get_pos(),
norm_subjects = ['p4', 'p6', 'IO', 'KM'] all_subjects = good_subjects + norm_subjects subj = all_subjects[0] day = 1 fig, axes = plt.subplots(2 * len(norm_subjects), 6) for s, subj in enumerate(norm_subjects[:]): for day in range(2): axes[day + 2 * s, 0].set_ylabel('{}-d{}'.format(subj, day + 1)) mat = loadmat( r'C:\Users\Nikolai\Desktop\Liza_diplom_data\Liza_diplom_data\treatment\{0}\bci\{0}_{1}1.mat' .format(subj, day + 1)) channels = [ch[0] for ch in mat['chan_names'][0]] montage = Montage(channels) df = pd.DataFrame(data=mat['data_cur'].T, columns=channels) df['state'] = mat['states_cur'][0] df = df.loc[~get_outliers_mask(df[channels], iter_numb=10, std=3)] df['block_name'] = df['state'].apply(lambda x: { 6: 'Rest', 1: 'Left', 2: 'Right' }[x]) filters = np.load(r'treatment\{0}d{1}_filters.npy'.format( subj, day + 1)) topos = np.load(r'treatment\{0}d{1}_topographies.npy'.format( subj, day + 1)) ind = np.load(r'treatment\{0}d{1}_smr_ind.npy'.format(subj, day + 1)) for k in range(2): spat = filters[:, ind[k]]
from pynfb.signal_processing.decompositions import ICADecomposition from mne.viz import plot_topomap from pynfb.inlets.montage import Montage from scipy.signal import hilbert with h5py.File( r'C:\Users\Nikolai\PycharmProjects\nfb\pynfb\results\bci-example_12-28_17-57-26\experiment_data.h5' ) as f: fs, channels, p_names = get_info(f, []) channels = [ch.split('-')[0] for ch in channels] data = [ f['protocol{}/raw_data'.format(k + 1)][:] for k in range(len(p_names)) ] print(p_names) montage = Montage(channels) df = pd.DataFrame(np.concatenate(data), columns=channels) df['block_name'] = np.concatenate([[p] * len(d) for p, d in zip(p_names, data)]) df['block_number'] = np.concatenate([[j + 1] * len(d) for j, d in enumerate(data)]) df['times'] = np.concatenate([np.arange(len(d)) for d in data]) df = df[df['block_name'].isin(['Legs', 'Rest', 'Left', 'Right'])] # eeg = df[channels].as_matrix() try: filters = np.load('filters_ex.npy') topos = np.load('topos_ex.npy') except FileNotFoundError:
from pynfb.postprocessing.mu_or_not.meta import * from pynfb.signal_processing.decompositions import CSPDecomposition from mne.viz import plot_topomap from pynfb.inlets.montage import Montage group = 'treatment' subj = 'p6' day = 1 mat = loadmat(r'C:\Users\Nikolai\Desktop\Liza_diplom_data\Liza_diplom_data\treatment\p4\bci\p4_11.mat') channels = [ch[0] for ch in mat['chan_names'][0]] montage = Montage(channels) df = pd.DataFrame(data=mat['data_cur'].T, columns=channels) print(channels) print(1000*np.isin(np.array(channels), ['Fp1', 'Fp2', 'F7', 'F8', 'Ft9', 'Ft10', 'T7', 'T8', 'Tp9', 'Tp10'])) df['state'] = mat['states_cur'][0] print(df['state'].unique()) df = df.loc[~get_outliers_mask(df[channels], iter_numb=10, std=3)] plt.plot(df[channels]) plt.show() ica = CSPDecomposition(channels, fs, band) df = df.loc[df.state.isin([6, 1])] scores, filters, topos = ica.decompose(df[channels].as_matrix(), df['state']-1) for k in range(len(topos)): plot_topomap(topos[:, k], montage.get_pos()) sources = fft_filter(np.dot(df[channels], filters), fs, band) desyncs = sources[df.state == 6].std(0)/sources[df.state == 1].std(0)
snr }), ignore_index=True) if PLOT_ARTIFACTS_RES: ax = ts_axes[j_dataset // 2, j_dataset % 2] ax.plot(df['P4'].loc[:fs * 100]) ax.plot(th.loc[th < GFP_THRESHOLD].loc[:fs * 100]) ax.axhline(GFP_THRESHOLD, color='k', alpha=0.4) ax.axhline(-GFP_THRESHOLD, color='k', alpha=0.4) ax.legend(['{:40s} {:10s}'.format(dataset, fb_type)]) ax.set_ylabel('{:.2f}'.format(snr)) topo = (var_before - var_after)[th.values < GFP_THRESHOLD].std(0) spec_axes[j_dataset // 2, j_dataset % 2].plot(freq, pxx) spec_axes[j_dataset // 2, j_dataset % 2].legend( ['{:40s} {:10s}'.format(dataset, fb_type)]) spec_axes[j_dataset // 2, j_dataset % 2].set_ylabel('{:.2f}'.format(snr)) plot_topomap(topo, Montage(channels[:-1]).get_pos(), axes=topo_axes[j_dataset], show=False) if PLOT_ARTIFACTS_RES: ts_axes[-1, -1].set_xlim(0, FS * 30) spec_axes[-1, -1].set_xlim(0, 30) plt.savefig('raw.png', dpi=200) plt.show() # save all subj envelopes probes_df.to_pickle('envelopes.pkl')
df['block_name'] = df['block_name'].apply(lambda x: 'FB' if 'FB' in x else x) # remove eyes artifacts ICA df[channels] = df[channels].values.dot(eye_rejection_matrix) # GFP threshold arthifact segments th = np.abs(df[channels[:-1]]).rolling(int(fs), center=True).max().mean(1) df = df.loc[th < GFP_THRESHOLD] # filter data # estimate snr montage = Montage(channels[:-1]) b_numbers = df.query('block_name=="FB"')['block_number'].unique() baseline_b = [2, 4] band = subj_bands[dataset] ba = sg.butter(4, [band[0] / fs * 2, band[1] / fs * 2], 'band') xs = [ sg.filtfilt(*ba, df.query('block_number=={}'.format(b))[ channels[:-1]].values[:int(fs) * 100], axis=0).T for b in b_numbers ] x += [xx for xx in np.array(xs) / np.std(xs)] metric = stats_df.query( 'dataset=="{}" & metric_type=="magnitude" & threshold_factor==2'. format(dataset))['metric'].values
from PyQt5.QtWidgets import QApplication from 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 if __name__ == '__main__': from mne.viz import plot_topomap from pynfb.inlets.montage import Montage from pynfb.generators import ch_names32 montage = Montage(ch_names32) spatial, topo = runica(np.random.normal(size=(100000, 32)), 1000, montage.get_names(), mode='csp') plot_topomap(spatial, montage.get_pos()) plot_topomap(topo, montage.get_pos())
def ch_names_to_2d_pos(list_of_ch_names, kind='standard_1005', azimuthal=True): montage = Montage(list_of_ch_names) pos = montage.get_pos() return pos
# import nfb lab data loader sys.path.insert(0, '/home/kolai/Projects/nfblab/nfb') from utils.load_results import load_data from pynfb.inlets.montage import Montage from pynfb.signal_processing.decompositions import ICADecomposition df, fs, channels, p_names = load_data( '/media/kolai/OS/Projects/nfblab/nfb/pynfb/results/vr1_02-18_15-30-10/experiment_data.h5' ) df = df.query('block_number<=2') df2, fs_, channels_, p_names_ = load_data( '/media/kolai/OS/Projects/nfblab/nfb/pynfb/results/vr1_02-18_15-52-17/experiment_data.h5' ) df2['block_number'] += 4 channels = channels[:30] montage = Montage(channels) df = df.append(df2, ignore_index=True) b, a = sg.butter(4, 3 / (fs / 2), 'highpass') df[channels] = sg.filtfilt(b, a, df[channels], axis=0) # plt.plot(df['C3']) # plt.show() # # freq, pxx = sg.welch(df['C3'], fs) # plt.plot(freq, pxx) alpha_pows = [] for j, block_name in enumerate(df['block_name'].unique()): freq, pxx = sg.welch(df.query(
CHANNELS = [ 'FP1', 'FP2', 'F7', 'F3', 'FZ', 'F4', 'F8', 'FT9', 'FC5', 'FC1', 'FC2', 'FC6', 'FT10', 'C3', 'CZ', 'C4', 'T8', 'TP9', 'CP5', 'CP1', 'CP2', 'CP6', 'TP10', 'P7', 'P3', 'P4', 'P8', 'O1', 'OZ', 'O2', 'T7', 'PZ' ] BLOCK_NAMES = [ None, 'Close', 'Baseline', 'PauseBL', 'Baseline', 'PauseFB', 'FB', 'PauseFB', 'FB', 'PauseFB', 'FB', 'PauseFB', 'FB', 'PauseFB', 'FB', 'PauseFB', 'FB', 'PauseFB', 'FB', 'PauseFB', 'FB', 'PauseFB', 'FB', 'PauseFB', 'FB', 'PauseFB', 'FB', 'PauseFB', 'FB', 'PauseFB', 'FB', 'PauseFB', 'FB', 'PauseFB', 'FB', 'PauseBL', 'Baseline' ] FB_ALL = [k for k, name in enumerate(BLOCK_NAMES) if name == 'FB'] CLOSE = 1 OPEN = 2 BASELINE_BEFORE = 4 BASELINE_AFTER = len(BLOCK_NAMES) - 1 ICA_BLOCKS = [CLOSE, OPEN, BASELINE_BEFORE, FB_ALL[0]] MONTAGE = Montage(CHANNELS) ICA_CHANNELS = ['ICA{}'.format(k + 1) for k in range(len(CHANNELS))] def save_ica(ica, filename): with open(filename, 'wb') as output: # Overwrites any existing file. pickle.dump(ica, output, pickle.HIGHEST_PROTOCOL) def load_ica(filename): with open(filename, 'rb') as input: return pickle.load(input)
from utils.load_results import load_data from pynfb.inlets.montage import Montage import pylab as plt import mne import numpy as np from helpers import get_XY, get_ideal_H, band_hilbert, ridge from mne.viz import plot_topomap df, fs, channels, p_names = load_data( '/home/kolai/_Work/predict_eeg/results/alpha-delay-subj-13_05-16_12-25-56/experiment_data.h5' ) montage = Montage(channels) x = df.loc[df.block_name == 'FB0', channels].values[:5 * 60 * fs] lapl = 0 if lapl: x = x.dot(montage.make_laplacian_proj('EEG')) del df x *= 1e6 x_split = np.split(x[:x.shape[0] - x.shape[0] % 10], 10) X_split = [] Y_split = [] delay = 0 n_taps = 200 band = (8, 12)
df = df.loc[th < GFP_THRESHOLD] # estimate snr freq, pxx = sg.welch(df.query('block_name=="Baseline0"')['P4'], FS, nperseg=FS * 2) band = subj_bands[dataset] sig = pxx[(freq >= band[0]) & (freq <= band[1])].mean() noise = pxx[((freq >= band[0] - FLANKER_WIDTH) & (freq <= band[0])) | ((freq >= band[1]) & (freq <= band[1] + FLANKER_WIDTH))].mean() snr = sig / noise # exclude subjects with snr < 1 if snr < 1: continue montage = Montage(channels[:-1]) csp = CSPDecomposition(montage.get_names(), FS, subj_bands[dataset].tolist()) b_numbers = df.query('block_name=="FB"')['block_number'].unique() x = np.concatenate([ df.query('block_number=={}'.format(b))[channels[:-1]].values for b in b_numbers[1:] ]) y = np.concatenate([ np.zeros(sum(df['block_number'] == b)) + int(b < b_numbers[8]) for b in b_numbers[1:] ]) csp.fit(x, y) #plt.figure()
def restart(self): timestamp_str = datetime.strftime(datetime.now(), '%m-%d_%H-%M-%S') self.dir_name = 'results/{}_{}/'.format(self.params['sExperimentName'], timestamp_str) os.makedirs(self.dir_name) wait_bar = WaitMessage(WAIT_BAR_MESSAGES['EXPERIMENT_START']).show_and_return() self.test_mode = False if self.main_timer is not None: self.main_timer.stop() if self.stream is not None: self.stream.disconnect() if self.thread is not None: self.thread.terminate() # timer self.main_timer = QtCore.QTimer(self.app) self.is_finished = False # current protocol index self.current_protocol_index = 0 # samples counter for protocol sequence self.samples_counter = 0 # run file lsl stream in a thread self.thread = None if self.params['sInletType'] == 'lsl_from_file': self.restart_lsl_from_file() # run simulated eeg lsl stream in a thread elif self.params['sInletType'] == 'lsl_generator': self.thread = stream_generator_in_a_thread(self.params['sStreamName']) # use FTB inlet aux_streams = None if self.params['sInletType'] == 'ftbuffer': hostname, port = self.params['sFTHostnamePort'].split(':') port = int(port) stream = FieldTripBufferInlet(hostname, port) # use LSL inlet else: stream_names = re.split(r"[,; ]+", self.params['sStreamName']) streams = [LSLInlet(name=name) for name in stream_names] stream = streams[0] aux_streams = streams[1:] if len(streams) > 1 else None # setup events stream by name events_stream_name = self.params['sEventsStreamName'] events_stream = LSLInlet(events_stream_name) if events_stream_name else None # setup main stream self.stream = ChannelsSelector(stream, exclude=self.params['sReference'], subtractive_channel=self.params['sReferenceSub'], dc=self.params['bDC'], events_inlet=events_stream, aux_inlets=aux_streams, prefilter_band=self.params['sPrefilterBand']) self.stream.save_info(self.dir_name + 'stream_info.xml') save_channels_and_fs(self.dir_name + 'experiment_data.h5', self.stream.get_channels_labels(), self.stream.get_frequency()) save_xml_str_to_hdf5_dataset(self.dir_name + 'experiment_data.h5', self.stream.info_as_xml(), 'stream_info.xml') self.freq = self.stream.get_frequency() self.n_channels = self.stream.get_n_channels() self.n_channels_other = self.stream.get_n_channels_other() channels_labels = self.stream.get_channels_labels() montage = Montage(channels_labels) print(montage) self.seconds = 2 * self.freq self.raw_std = None # signals self.signals = [DerivedSignal.from_params(ind, self.freq, self.n_channels, channels_labels, signal) for ind, signal in enumerate(self.params['vSignals']['DerivedSignal']) if not signal['bBCIMode']] # composite signals self.composite_signals = [CompositeSignal([s for s in self.signals], signal['sExpression'], signal['sSignalName'], ind + len(self.signals), self.freq) for ind, signal in enumerate(self.params['vSignals']['CompositeSignal'])] # bci signals self.bci_signals = [BCISignal(self.freq, channels_labels, signal['sSignalName'], ind) for ind, signal in enumerate(self.params['vSignals']['DerivedSignal']) if signal['bBCIMode']] self.signals += self.composite_signals self.signals += self.bci_signals # self.current_samples = np.zeros_like(self.signals) # signals outlet self.signals_outlet = SignalsOutlet([signal.name for signal in self.signals], fs=self.freq) # protocols self.protocols = [] signal_names = [signal.name for signal in self.signals] for protocol in self.params['vProtocols']: # some general protocol arguments source_signal_id = None if protocol['fbSource'] == 'All' else signal_names.index(protocol['fbSource']) reward_signal_id = signal_names.index(protocol['sRewardSignal']) if protocol['sRewardSignal'] != '' else 0 mock_path = (protocol['sMockSignalFilePath'] if protocol['sMockSignalFilePath'] != '' else None, protocol['sMockSignalFileDataset']) m_signal = protocol['sMSignal'] m_signal_index = None if m_signal not in signal_names else signal_names.index(m_signal) # general protocol arguments dictionary kwargs = dict( source_signal_id=source_signal_id, name=protocol['sProtocolName'], duration=protocol['fDuration'], random_over_time=protocol['fRandomOverTime'], update_statistics_in_the_end=bool(protocol['bUpdateStatistics']), stats_type=protocol['sStatisticsType'], mock_samples_path=mock_path, show_reward=bool(protocol['bShowReward']), reward_signal_id=reward_signal_id, reward_threshold=protocol['bRewardThreshold'], ssd_in_the_end=protocol['bSSDInTheEnd'], timer=self.main_timer, freq=self.freq, mock_previous=int(protocol['iMockPrevious']), drop_outliers=int(protocol['iDropOutliers']), experiment=self, pause_after=bool(protocol['bPauseAfter']), beep_after=bool(protocol['bBeepAfter']), reverse_mock_previous=bool(protocol['bReverseMockPrevious']), m_signal_index=m_signal_index, shuffle_mock_previous=bool(protocol['bRandomMockPrevious']), as_mock=bool(protocol['bMockSource']), auto_bci_fit=bool(protocol['bAutoBCIFit']), montage=montage ) # type specific arguments if protocol['sFb_type'] == 'Baseline': self.protocols.append( BaselineProtocol( self.signals, text=protocol['cString'] if protocol['cString'] != '' else 'Relax', half_time_text=protocol['cString2'] if bool(protocol['bUseExtraMessage']) else None, **kwargs )) elif protocol['sFb_type'] in ['Feedback', 'CircleFeedback']: self.protocols.append( FeedbackProtocol( self.signals, circle_border=protocol['iRandomBound'], m_threshold=protocol['fMSignalThreshold'], **kwargs)) elif protocol['sFb_type'] == 'ThresholdBlink': self.protocols.append( ThresholdBlinkFeedbackProtocol( self.signals, threshold=protocol['fBlinkThreshold'], time_ms=protocol['fBlinkDurationMs'], **kwargs)) elif protocol['sFb_type'] == 'Video': self.protocols.append( VideoProtocol( self.signals, video_path=protocol['sVideoPath'], **kwargs)) else: raise TypeError('Undefined protocol type \"{}\"'.format(protocol['sFb_type'])) # protocols sequence names = [protocol.name for protocol in self.protocols] group_names = [p['sName'] for p in self.params['vPGroups']['PGroup']] print(group_names) self.protocols_sequence = [] for name in self.params['vPSequence']: if name in names: self.protocols_sequence.append(self.protocols[names.index(name)]) if name in group_names: group = self.params['vPGroups']['PGroup'][group_names.index(name)] subgroup = [] if len(group['sList'].split(' ')) == 1: subgroup.append([group['sList']] * int(group['sNumberList'])) else: for s_name, s_n in zip(group['sList'].split(' '), list(map(int, group['sNumberList'].split(' ')))): subgroup.append([s_name] * s_n) if group['bShuffle']: subgroup = np.concatenate(subgroup) subgroup = list(subgroup[np.random.permutation(len(subgroup))]) else: subgroup = [k for k in chain(*zip_longest(*subgroup)) if k is not None] print(subgroup) for subname in subgroup: self.protocols_sequence.append(self.protocols[names.index(subname)]) if len(group['sSplitBy']): self.protocols_sequence.append(self.protocols[names.index(group['sSplitBy'])]) # reward from pynfb.reward import Reward self.reward = Reward(self.protocols[0].reward_signal_id, threshold=self.protocols[0].reward_threshold, rate_of_increase=self.params['fRewardPeriodS'], fs=self.freq) self.reward.set_enabled(isinstance(self.protocols_sequence[0], FeedbackProtocol)) # timer # self.main_timer = QtCore.QTimer(self.app) self.main_timer.timeout.connect(self.update) self.main_timer.start(1000 * 1. / self.freq) # current protocol number of samples ('frequency' * 'protocol duration') self.current_protocol_n_samples = self.freq * (self.protocols_sequence[self.current_protocol_index].duration + np.random.uniform(0, self.protocols_sequence[ self.current_protocol_index].random_over_time)) # experiment number of samples max_protocol_n_samples = int( max([self.freq * (p.duration + p.random_over_time) for p in self.protocols_sequence])) # data recorders self.experiment_n_samples = max_protocol_n_samples self.samples_counter = 0 self.raw_recorder = np.zeros((max_protocol_n_samples * 110 // 100, self.n_channels)) * np.nan self.timestamp_recorder = np.zeros((max_protocol_n_samples * 110 // 100)) * np.nan self.raw_recorder_other = np.zeros((max_protocol_n_samples * 110 // 100, self.n_channels_other)) * np.nan self.signals_recorder = np.zeros((max_protocol_n_samples * 110 // 100, len(self.signals))) * np.nan self.reward_recorder = np.zeros((max_protocol_n_samples * 110 // 100)) * np.nan self.mark_recorder = np.zeros((max_protocol_n_samples * 110 // 100)) * np.nan # save init signals save_signals(self.dir_name + 'experiment_data.h5', self.signals, group_name='protocol0') # save settings params_to_xml_file(self.params, self.dir_name + 'settings.xml') save_xml_str_to_hdf5_dataset(self.dir_name + 'experiment_data.h5', params_to_xml(self.params), 'settings.xml') # windows self.main = MainWindow(signals=self.signals, protocols=self.protocols_sequence, parent=None, experiment=self, current_protocol=self.protocols_sequence[self.current_protocol_index], n_signals=len(self.signals), max_protocol_n_samples=max_protocol_n_samples, freq=self.freq, n_channels=self.n_channels, plot_raw_flag=self.params['bPlotRaw'], plot_signals_flag=self.params['bPlotSignals'], plot_source_space_flag=self.params['bPlotSourceSpace'], show_subject_window=self.params['bShowSubjectWindow'], channels_labels=channels_labels, photo_rect=self.params['bShowPhotoRectangle']) self.subject = self.main.subject_window if self.params['bPlotSourceSpace']: self.source_space_window = self.main.source_space_window if self.params['sInletType'] == 'lsl_from_file': self.main.player_panel.start_clicked.connect(self.restart_lsl_from_file) # create real fb list self.real_fb_number_list = [] wait_bar.close()
from pynfb.postprocessing.mu_or_not.meta import * from pynfb.signal_processing.decompositions import ICADecomposition from mne.viz import plot_topomap from pynfb.inlets.montage import Montage group = 'treatment' subj = 'VV' day = 1 for day in [1, 2]: mat = loadmat( r'C:\Users\Nikolai\Desktop\Liza_diplom_data\Liza_diplom_data\treatment\{0}\bci\{0}_{1}1.mat' .format(subj, day)) channels = [ch[0] for ch in mat['chan_names'][0]] montage = Montage(channels) df = pd.DataFrame(data=mat['data_cur'].T, columns=channels) df['state'] = mat['states_cur'][0] print(df['state'].unique()) df = df.loc[~get_outliers_mask(df[channels], iter_numb=10, std=3)] plt.plot(df[channels]) plt.show() a = QtGui.QApplication([]) #ica = ICADialog(np.concatenate([df.loc[df['state']==6, channels], df.loc[df['state']==1, channels]]), channels, fs, mode='csp') ica = ICADialog(df[channels], channels, fs, mode='ica') ica.exec_() print(ica.table.get_checked_rows()) ind = ica.table.get_checked_rows()
from scipy.signal import hilbert, welch from scipy.stats import linregress, ttest_ind, ranksums, ttest_1samp with h5py.File(r'C:\Users\Nikolai\PycharmProjects\nfb\pynfb\results\alpha-nfb-example_01-13_17-37-02\experiment_data.h5') as f: fs, channels, p_names = get_info(f, ['A1', 'A2', 'AUX']) channels = [ch.split('-')[0] for ch in channels] mock_ind = [f['protocol{}'.format(k + 1)].attrs['mock_previous'] for k in range(len(p_names))] data = [f['protocol{}/raw_data'.format(k + 1)][:] for k in range(len(p_names))] signal = [f['protocol{}/signals_data'.format(k + 1)][:] for k in range(len(p_names))] spat = f['protocol{}/signals_stats/Alpha/spatial_filter'.format(p_names.index('Baseline')+1)][:] band = f['protocol{}/signals_stats/Alpha/bandpass'.format(p_names.index('Baseline') + 1)][:] #print(p_names) montage = Montage(channels) print(mock_ind) df = pd.DataFrame(np.concatenate(data), columns=channels) df['block_name'] = np.concatenate([[p]*len(d) for p, d in zip(p_names, data)]) df['block_number'] = np.concatenate([[j + 1]*len(d) for j, d in enumerate(data)]) df['alpha'] = np.dot(df[channels], spat) df['alpha_env'] = df['alpha']*0 df['time'] = np.arange(len(df)) / fs df['signal'] = np.concatenate(signal) fig, axes = plt.subplots(1, 5)
from pynfb.signal_processing.decompositions import ICADecomposition, CSPDecomposition from pynfb.inlets.montage import Montage from mne.viz import plot_topomap # settings h5_file = r'C:\Projects\nfblab\nfb\pynfb\results\exoatlet_kolai_stay_go_10-24_15-47-00\experiment_data.h5' band = (15, 30) method = 'ica' np.random.seed(401) # load data df, fs, channels, p_names = load_data(h5_file) fs = int(fs) eeg_channels = channels[:30] n_channels = len(eeg_channels) montage = Montage(eeg_channels) print( 'Fs: {}Hz\nAll channels: {}\nEEG channels: {}\nBlock sequence: {}'.format( fs, ', '.join(channels), ', '.join(eeg_channels), '-'.join(p_names))) # pre filter pre_filter = ButterFilter(band, fs, n_channels) df[eeg_channels] = pre_filter.apply(df[eeg_channels]) df = df.iloc[fs * 5:] # spatial decomposition if method == 'ica': decomposition = ICADecomposition(eeg_channels, fs) elif method == 'csp': decomposition = CSPDecomposition(eeg_channels, fs) else: