Esempio n. 1
0
def runica(x, fs, channels, mode='ica'):
    from PyQt5.QtWidgets import QApplication
    from 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
Esempio n. 2
0
def runica(x, fs, channels, mode='ica'):
    from PyQt5.QtWidgets import QApplication
    from 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
Esempio n. 3
0
def run_ica_and_select_band(data, channels, fs):
    app = QApplication([])
    ica = ICADialog(data, channels, fs)
    ica.exec_()
    band = select_band(data.dot(ica.spatial), fs)
    results = {
        'spatial_filter': ica.spatial,
        'spatial_filter_ind': ica.table.get_checked_rows()[0],
        'filters': ica.decomposition.filters,
        'topographies': ica.decomposition.topographies,
        'band': band
    }
    return results
Esempio n. 4
0
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
Esempio n. 5
0
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
Esempio n. 6
0
 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 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 PyQt4.QtGui 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
Esempio n. 7
0
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 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
Esempio n. 8
0
#data[channels] = fft_filter(data[channels], fs, (1, 45))
#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))

data['SMR'].plot()
data['SMR_band_filt'].plot()
Esempio n. 9
0
            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([
                    np.concatenate(
                        [raw[key] for key in data if 'Close' in key]),
                    np.concatenate([raw[key] for key in data if 'Open' in key])
                ])
                rej, spatial, top = ICADialog.get_rejection(raw_data,
                                                            channels,
                                                            fs,
                                                            mode='csp',
                                                            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(
Esempio n. 10
0
File: s0.py Progetto: nikolaims/nfb
from mne.io import read_epochs_eeglab
import numpy as np
import pylab as plt
from scipy.io import loadmat
from pynfb.widgets.helpers import ch_names_to_2d_pos
from 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')
Esempio n. 11
0
from pynfb.signal_processing.helpers import get_outliers_mask
import numpy as np
from pynfb.protocols.ssd.topomap_selector_ica import ICADialog
from PyQt5 import QtGui, QtWidgets

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()
Esempio n. 12
0
    # 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_()
Esempio n. 13
0
            fs, channels, p_names = get_info(f, settings['drop_channels'])
            rejection, alpha, ica = load_rejections(f, reject_alpha=False)
            raw_before = OrderedDict()
            raw_after = OrderedDict()
            for j, name in enumerate(p_names):
                if j < 3:
                    x = preproc(f['protocol{}/raw_data'.format(j + 1)][:], fs, rejection if reject else None)
                    raw_before = add_data_simple(raw_before, name, x)
                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()



Esempio n. 14
0
    good_samples_mask = th < threshold
    print('********Remove {} bad samples'.format(sum(~good_samples_mask)))
    return df.loc[good_samples_mask].values, good_samples_mask


N_EEG_CHANNELS = 30

raw = mne.io.read_raw_fif('/media/kolai/prispasobo/Kondrashin_raw.fif')
channels = raw.info['ch_names'][:N_EEG_CHANNELS]
fs = int(raw.info['sfreq'])



data = raw.load_data().notch_filter([50, 100]).filter(0.5, 40.).get_data()[:N_EEG_CHANNELS].T



# plt.plot(data[:fs*100])

ica_data, good_samples_mask = remove_bad_samples(data[:fs*60*10], window_size=fs*5, threshold=2000)

ica = ICADialog(ica_data, channels, fs)
ica.exec_()
# ica.table.get_checked_rows()

plt.plot(data[:, :]+np.arange(30)*2000, 'r')
plt.plot(ica.rejection.apply(data)[:, :]+np.arange(30)*2000, 'k')
plt.fill_between(np.arange(data.shape[0]), (~good_samples_mask).astype(int)*30*2000, color='r', alpha=0.4)
plt.gca().set_yticks(np.arange(30)*2000)
plt.gca().set_yticklabels(channels)
plt.xlim(0, fs*30)
Esempio n. 15
0
    data = np.random.normal(loc=0, scale=0.00001, size=(5000, len(channels))).T
    fs = 500
else:
    import h5py
    from 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 PyQt4.QtGui 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

# create info
info = mne.create_info(ch_names=channels,
                       sfreq=fs,
                       montage=mne.channels.read_montage(kind='standard_1005'),
                       ch_types=['eeg' for ch in channels])

# raw instance
raw = mne.io.RawArray(data, info)
#raw.set_eeg_reference()
#noise_cov = mne.compute_raw_covariance(raw)
noise_cov = mne.make_ad_hoc_cov(info, verbose=None)
Esempio n. 16
0
    return df.loc[good_samples_mask].values, good_samples_mask


N_EEG_CHANNELS = 30

raw = mne.io.read_raw_fif('/media/kolai/prispasobo/Kondrashin_raw.fif')
channels = raw.info['ch_names'][:N_EEG_CHANNELS]
fs = int(raw.info['sfreq'])

data = raw.load_data().notch_filter([50, 100]).filter(
    0.5, 40.).get_data()[:N_EEG_CHANNELS].T

# plt.plot(data[:fs*100])

ica_data, good_samples_mask = remove_bad_samples(data[:fs * 60 * 10],
                                                 window_size=fs * 5,
                                                 threshold=2000)

ica = ICADialog(ica_data, channels, fs)
ica.exec_()
# ica.table.get_checked_rows()

plt.plot(data[:, :] + np.arange(30) * 2000, 'r')
plt.plot(ica.rejection.apply(data)[:, :] + np.arange(30) * 2000, 'k')
plt.fill_between(np.arange(data.shape[0]),
                 (~good_samples_mask).astype(int) * 30 * 2000,
                 color='r',
                 alpha=0.4)
plt.gca().set_yticks(np.arange(30) * 2000)
plt.gca().set_yticklabels(channels)
plt.xlim(0, fs * 30)
Esempio n. 17
0
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 = QtGui.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()
Esempio n. 18
0
    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 = QtGui.QMessageBox.Yes
            if len(self.signals[row].rejections) > 0:
                reply = QtGui.QMessageBox.question(
                    self, 'Warning', 'Changing ICA base selection will '
                    'invalidate the current rejections (CSP, SSD). '
                    'Are you sure you want to continue?',
                    QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
            if reply == QtGui.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)
Esempio n. 19
0
    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)
Esempio n. 20
0
#data[channels] = fft_filter(data[channels], fs, (1, 45))
#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))

data['SMR'].plot()
data['SMR_band_filt'].plot()
Esempio n. 21
0
    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_()
Esempio n. 22
0
import pylab as plt
from scipy.io import loadmat
from pynfb.widgets.helpers import ch_names_to_2d_pos
from pynfb.protocols.ssd.topomap_selector_ica import ICADialog
from PyQt4.QtGui 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')
Esempio n. 23
0
# 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 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

standard_montage = mne.channels.read_montage(kind='standard_1005')
standard_montage_names = [name.upper() for name in standard_montage.ch_names]
for j, channel in enumerate(channels):
    channels[j] = standard_montage.ch_names[standard_montage_names.index(channel.upper())]
# create info
info = mne.create_info(ch_names=channels, sfreq=fs, montage=mne.channels.read_montage(kind='standard_1005'), ch_types=['eeg' for ch in channels])

# raw instance
raw = mne.io.RawArray(data, info)
#raw.set_eeg_reference()
#noise_cov = mne.compute_raw_covariance(raw)
noise_cov = mne.make_ad_hoc_cov(info, verbose=None)
# forward solution
Esempio n. 24
0
                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()
Esempio n. 25
0
        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([
                    np.concatenate([raw[key] for key in data if 'Close' in key]),
                    np.concatenate([raw[key] for key in data if 'Open' in key])
                    ])
                rej, spatial, top = ICADialog.get_rejection(raw_data, channels, fs, mode='csp', 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)