Example #1
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
Example #2
0
def load_data(file_path):
    with h5py.File(file_path) as f:
        fs, channels, p_names = get_info(f, ['A1', 'A2'])
        data = [f['protocol{}/raw_data'.format(k + 1)][:] for k in range(len(p_names))]

        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)])

    return df, fs, p_names, channels
Example #3
0
def load_data(file_path):
    with h5py.File(file_path) as f:
        fs, channels, p_names = get_info(f, ['A1', 'A2'])
        data = [f['protocol{}/raw_data'.format(k + 1)][:] for k in range(len(p_names))]

        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)])

    return df, fs, p_names, channels
Example #4
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
Example #5
0
dir_ = settings['dir']
subj = 3
experiments = settings['subjects'][subj]
experiment = experiments[2]

def preproc(x, fs, rej=None):
    x = dc_blocker(x)
    x = fft_filter(x, fs, band=(0, 45))
    if rej is not None:
        x = np.dot(x, rej)
    return x


reject = False
with h5py.File('{}\\{}\\{}'.format(settings['dir'], experiment, 'experiment_data.h5')) as f:
    fs, channels, p_names = get_info(f, settings['drop_channels'])
    rejection, alpha, ica = load_rejections(f, reject_alpha=True)
    raw_before = OrderedDict()
    raw_after = OrderedDict()
    for j, name in enumerate(p_names):
        if j < 5:
            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)



# plot raw data
Example #6
0
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

# create info
info = mne.create_info(ch_names=channels,
                       sfreq=fs,
                       montage=mne.channels.read_montage(kind='standard_1005'),
Example #7
0
# set trans
trans = r'C:\Users\nsmetanin\PycharmProjects\nfb\tests\sloreta\av_brain\fsaverage\bem\fsaverage-trans.fif'


# 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)
Example #8
0
            eeg_data = eeg_data[
                without_emp_mask, :].T  # Remove constant (zero) channels and prespecified channels
            ch_names = [
                name[0][0] for name in chan_names[:, without_emp_mask].T
            ]
            labels = states_labels1[0]
            datasets.append((eeg_data, labels))
    else:
        import h5py
        import pylab as plt

        file = r'C:\Users\Nikolai\PycharmProjects\nfb\pynfb\results\mu_ica_000000_04-17_19-11-50\experiment_data.h5'

        labels_map = {'Open': 6, 'Right': 2, 'Left': 1}
        with h5py.File(file) as f:
            fs, ch_names, p_names = get_info(f, [])
            before = []
            after = []
            before_labels = []
            after_labels = []
            k = 0
            for protocol in [
                    'protocol{}'.format(j + 1)
                    for j in range(len(f.keys()) - 3)
            ]:
                name = f[protocol].attrs['name']
                if name in ['Right', 'Open', 'Left']:
                    data = f[protocol + '/raw_data'][:]
                    labels = np.ones(len(data), dtype=int) * labels_map[name]
                    if k < 3:
                        before.append(data)
Example #9
0
            fs = fs[0, 0]
            nozeros_mask = np.sum(eeg_data[:, :fs * 2], 1) != 0  # Detect constant (zero) channels
            without_emp_mask = nozeros_mask & (chan_names[0, :] != 'A1') & (chan_names[0, :] != 'A2') & (chan_names[0, :] != 'AUX')
            eeg_data = eeg_data[without_emp_mask, :].T  # Remove constant (zero) channels and prespecified channels
            ch_names = [name[0][0] for name in chan_names[:, without_emp_mask].T]
            labels = states_labels1[0]
            datasets.append((eeg_data, labels))
    else:
        import h5py
        import pylab as plt

        file = r'C:\Users\Nikolai\PycharmProjects\nfb\pynfb\results\mu_ica_000000_04-17_19-11-50\experiment_data.h5'

        labels_map = {'Open': 6, 'Right': 2, 'Left': 1}
        with h5py.File(file) as f:
            fs, ch_names, p_names = get_info(f, [])
            before = []
            after = []
            before_labels = []
            after_labels = []
            k = 0
            for protocol in ['protocol{}'.format(j + 1) for j in range(len(f.keys()) - 3)]:
                name = f[protocol].attrs['name']
                if name in ['Right', 'Open', 'Left']:
                    data = f[protocol + '/raw_data'][:]
                    labels = np.ones(len(data), dtype=int) * labels_map[name]
                    if k < 3:
                        before.append(data)
                        before_labels.append(labels)
                    else:
                        after.append(data)
Example #10
0

if __name__ == '__main__':
    np.random.seed(42)

    # loading anp pre processing

    import h5py
    import pylab as plt

    file = r'E:\Develop\NFB\pynfb\results\experiment_02-06_12-36-50\experiment_data.h5'
    #file = r'C:\Users\Nikolai\PycharmProjects\nfb\pynfb\results\BCI_Test_2_1_06-22_17-01-07\experiment_data.h5'

    labels_map = {'Open': 0, 'Right': 2, 'Left': 1}
    with h5py.File(file) as f:
        fs, ch_names, p_names = get_info(f,
                                         [])  #['AUX', 'A1', 'A2', 'F4', 'Pz']
        print(f.keys())
        before = []
        after = []
        before_labels = []
        after_labels = []
        k = 0
        for protocol in [
                'protocol0'
        ]:  #['protocol{}'.format(j + 1) for j in range(len(f.keys()) - 3)]:
            name = f[protocol].attrs['name']
            print(str(f[protocol].attrs['name']))
            if name in ['unknown', 'Right', 'Open', 'Left']:
                data = f[protocol + '/raw_data'][:]
                labels = np.ones(len(data), dtype=int) * labels_map[name]
                if k < 9:
Example #11
0
def plot_results(pilot_dir, subj, channel, alpha_band=(9, 14), theta_band=(3, 6), drop_channels=None, dc=False,
                 reject_alpha=True, normalize_by='opened'):
    drop_channels = drop_channels or []
    cm = get_colors()
    fg = plt.figure(figsize=(30, 6))
    for j_s, experiment in enumerate(subj):
        with h5py.File('{}\\{}\\{}'.format(pilot_dir, experiment, 'experiment_data.h5')) as f:
            rejections, top_alpha, top_ica = load_rejections(f, reject_alpha=reject_alpha)
            fs, channels, p_names = get_info(f, drop_channels)
            ch = channels.index(channel)
            #plt.plot(fft_filter(f['protocol6/raw_data'][:, ch], fs, band=(3, 35)))
            #plt.plot(fft_filter(np.dot(f['protocol6/raw_data'], rejections)[:, ch], fs, band=(3, 35)))
            #plt.show()
            #from scipy.signal import welch
            #plt.plot(*welch(f['protocol1/raw_data'][:60*500//2, channels.index('C3')], fs, nperseg=1000))
            #plt.plot(*welch(f['protocol1/raw_data'][60*500//2:, channels.index('C3')], fs, nperseg=1000))

            #plt.plot(*welch(f['protocol2/raw_data'][:30*500//2, channels.index('C3')], fs, nperseg=1000))
            #plt.plot(*welch(f['protocol2/raw_data'][30*500//2:, channels.index('C3')], fs, nperseg=1000))
            #plt.legend(['Close', 'Open', 'Left', 'Right'])
            #plt.show()

            # collect powers
            powers = OrderedDict()
            raw = OrderedDict()
            alpha = OrderedDict()
            pow_theta = []
            for j, name in enumerate(p_names):
                pow, alpha_x, x = get_protocol_power(f, j, fs, rejections, ch, alpha_band, dc=dc)
                if 'FB' in name:
                    pow_theta.append(get_protocol_power(f, j, fs, rejections, ch, theta_band, dc=dc)[0].mean())
                powers = add_data(powers, name, pow, j)
                raw = add_data(raw, name, x, j)
                alpha = add_data(alpha, name, alpha_x, j)

            # plot rejections
            n_tops = top_ica.shape[1] + top_alpha.shape[1]
            for j_t in range(top_ica.shape[1]):
                ax = fg.add_subplot(4, n_tops * len(subj), n_tops * len(subj) * 3 + n_tops * j_s + j_t + 1)
                ax.set_xlabel('ICA{}'.format(j_t + 1))
                labels, fs = get_lsl_info_from_xml(f['stream_info.xml'][0])
                channels = [label for label in labels if label not in drop_channels]
                pos = ch_names_to_2d_pos(channels)
                plot_topomap(data=top_ica[:, j_t], pos=pos, axes=ax, show=False)
            for j_t in range(top_alpha.shape[1]):
                ax = fg.add_subplot(4, n_tops * len(subj),
                                    n_tops * len(subj) * 3 + n_tops * j_s + j_t + 1 + top_ica.shape[1])
                ax.set_xlabel('CSP{}'.format(j_t + 1))
                labels, fs = get_lsl_info_from_xml(f['stream_info.xml'][0])
                channels = [label for label in labels if label not in drop_channels]
                pos = ch_names_to_2d_pos(channels)
                plot_topomap(data=top_alpha[:, j_t], pos=pos, axes=ax, show=False)

            # plot powers
            if normalize_by == 'opened':
                norm = powers['1. Opened'].mean()
            elif normalize_by == 'beta':
                norm = np.mean(pow_theta)
            else:
                print('WARNING: norm = 1')
            print('norm', norm)

            ax1 = fg.add_subplot(3, len(subj), j_s + 1)
            ax = fg.add_subplot(3, len(subj), j_s + len(subj) + 1)
            t = 0
            for j_p, ((name, pow), (name, x)) in enumerate(zip(powers.items(), raw.items())):
                if name == '2228. FB':
                    from scipy.signal import periodogram
                    fff = plt.figure()
                    fff.gca().plot(*periodogram(x, fs, nfft=fs * 3), c=cm[name.split()[1]])
                    plt.xlim(0, 80)
                    plt.ylim(0, 3e-11)
                    plt.show()
                print(name)
                time = np.arange(t, t + len(x)) / fs
                color = cm[''.join([i for i in name.split()[1] if not i.isdigit()])]
                ax1.plot(time, fft_filter(x, fs, (2, 45)), c=color, alpha=0.4)
                ax1.plot(time, alpha[name], c=color)
                t += len(x)
                ax.plot([j_p], [pow.mean() / norm], 'o', c=color, markersize=10)
                ax.errorbar([j_p], [pow.mean() / norm], yerr=pow.std() / norm, c=color, ecolor=color)
            fb_x = np.hstack([[j] * len(pows) for j, (key, pows) in enumerate(powers.items()) if 'FB' in key])
            fb_y = np.hstack([pows for key, pows in powers.items() if 'FB' in key]) / norm
            sns.regplot(x=fb_x, y=fb_y, ax=ax, color=cm['FB'], scatter=False, truncate=True)

            ax1.set_xlim(0, t / fs)
            ax1.set_ylim(-40, 40)
            plt.setp(ax.xaxis.get_majorticklabels(), rotation=70)
            ax.set_xticks(range(len(powers)))
            ax.set_xticklabels(powers.keys())
            ax.set_ylim(0, 3)
            ax.set_xlim(-1, len(powers))
            ax1.set_title('Day {}'.format(j_s + 1))
    return fg
Example #12
0
    r'C:\Users\Nikolai\PycharmProjects\nfb\pynfb\results\ghosts-in-111_01-31_21-17-36\experiment_data.h5',
    '405': r'D:\bipolar-test_02-03_21-26-20\experiment_data.h5',
    '405-eeg': r'D:\new-lab-eeg-test_02-03_17-09-40\experiment_data.h5'
}
room = '104'

if room == '111-eeg':
    import mne
    a = mne.io.read_raw_brainvision(r'D:\bp-ghost-111-01-29-21-00-00\fff.vhdr')
    x = a.get_data([a.ch_names.index('Aux1')])[0] / 1000 / 10
    fs = 1000
else:
    with h5py.File(rooms[room]) as f:
        print(list(f.keys()), list(f['protocol1'].keys()))
        from pynfb.postprocessing.utils import get_info
        get_info(f, [])
        data = [
            f['protocol{}/raw_data'.format(k + 1)][:]
            for k in range(len(f.keys()) - 3)
        ]
    x = np.concatenate(data)[:500 * 4 * 60, 0]
    fs = 500

fig, axes = plt.subplots(2, 1, sharex=True)

t = np.arange(len(x)) / fs / 60
axes[0].plot(t, x * 1000 * 1000)
axes[0].set_ylabel('Voltage [$\mu$V]')
axes[0].set_ylim(-50, 50)
axes[0].set_title('Room: {} Date: {} Start time: {}'.format(
    room.split('-')[0], *rooms[room].split('\\')[-2].split('_')[-2:]))
Example #13
0
         '111-wed': r'C:\Users\Nikolai\PycharmProjects\nfb\pynfb\results\ghosts-in-111_01-31_21-17-36\experiment_data.h5',
         '405': r'D:\bipolar-test_02-03_21-26-20\experiment_data.h5',
         '405-eeg': r'D:\new-lab-eeg-test_02-03_17-09-40\experiment_data.h5'}
room = '104'


if room == '111-eeg':
    import mne
    a = mne.io.read_raw_brainvision(r'D:\bp-ghost-111-01-29-21-00-00\fff.vhdr')
    x = a.get_data([a.ch_names.index('Aux1')])[0]/1000/10
    fs = 1000
else:
    with h5py.File(rooms[room]) as f:
        print(list(f.keys()), list(f['protocol1'].keys()))
        from pynfb.postprocessing.utils import get_info
        get_info(f, [])
        data = [f['protocol{}/raw_data'.format(k+1)][:] for k in range(len(f.keys())-3)]
    x = np.concatenate(data)[:500*4*60, 0]
    fs = 500

fig, axes = plt.subplots(2, 1, sharex=True)

t = np.arange(len(x))/fs/60
axes[0].plot(t, x*1000*1000)
axes[0].set_ylabel('Voltage [$\mu$V]')
axes[0].set_ylim(-50, 50)
axes[0].set_title('Room: {} Date: {} Start time: {}'.format(room.split('-')[0], *rooms[room].split('\\')[-2].split('_')[-2:]))

f, t, Sxx = signal.spectrogram(x, fs, scaling='spectrum')
ax = axes[1].pcolormesh(t/60, f, np.log10(Sxx**0.5), vmin=-7.1, vmax=-4.8, cmap='nipy_spectral')
axes[1].set_ylabel('Frequency [Hz]')
Example #14
0
from pynfb.postprocessing.utils import get_info, fft_filter
import h5py
import numpy as np
import pandas as pd
import seaborn as sns
import pylab as plt
from pynfb.signal_processing.decompositions import ICADecomposition
from mne.viz import plot_topomap
from pynfb.inlets.montage import Montage
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)])
def plot_results(pilot_dir,
                 subj,
                 channel,
                 alpha_band=(9, 14),
                 theta_band=(3, 6),
                 drop_channels=None,
                 dc=False,
                 reject_alpha=True,
                 normalize_by='opened'):
    drop_channels = drop_channels or []
    cm = get_colors()
    fg = plt.figure(figsize=(30, 6))
    for j_s, experiment in enumerate(subj):
        with h5py.File('{}\\{}\\{}'.format(pilot_dir, experiment,
                                           'experiment_data.h5')) as f:
            rejections, top_alpha, top_ica = load_rejections(
                f, reject_alpha=reject_alpha)
            fs, channels, p_names = get_info(f, drop_channels)
            ch = channels.index(channel)
            #plt.plot(fft_filter(f['protocol6/raw_data'][:, ch], fs, band=(3, 35)))
            #plt.plot(fft_filter(np.dot(f['protocol6/raw_data'], rejections)[:, ch], fs, band=(3, 35)))
            #plt.show()
            #from scipy.signal import welch
            #plt.plot(*welch(f['protocol1/raw_data'][:60*500//2, channels.index('C3')], fs, nperseg=1000))
            #plt.plot(*welch(f['protocol1/raw_data'][60*500//2:, channels.index('C3')], fs, nperseg=1000))

            #plt.plot(*welch(f['protocol2/raw_data'][:30*500//2, channels.index('C3')], fs, nperseg=1000))
            #plt.plot(*welch(f['protocol2/raw_data'][30*500//2:, channels.index('C3')], fs, nperseg=1000))
            #plt.legend(['Close', 'Open', 'Left', 'Right'])
            #plt.show()

            # collect powers
            powers = OrderedDict()
            raw = OrderedDict()
            alpha = OrderedDict()
            pow_theta = []
            for j, name in enumerate(p_names):
                pow, alpha_x, x = get_protocol_power(f,
                                                     j,
                                                     fs,
                                                     rejections,
                                                     ch,
                                                     alpha_band,
                                                     dc=dc)
                if 'FB' in name:
                    pow_theta.append(
                        get_protocol_power(f,
                                           j,
                                           fs,
                                           rejections,
                                           ch,
                                           theta_band,
                                           dc=dc)[0].mean())
                powers = add_data(powers, name, pow, j)
                raw = add_data(raw, name, x, j)
                alpha = add_data(alpha, name, alpha_x, j)

            # plot rejections
            n_tops = top_ica.shape[1] + top_alpha.shape[1]
            for j_t in range(top_ica.shape[1]):
                ax = fg.add_subplot(
                    4, n_tops * len(subj),
                    n_tops * len(subj) * 3 + n_tops * j_s + j_t + 1)
                ax.set_xlabel('ICA{}'.format(j_t + 1))
                labels, fs = get_lsl_info_from_xml(f['stream_info.xml'][0])
                channels = [
                    label for label in labels if label not in drop_channels
                ]
                pos = ch_names_to_2d_pos(channels)
                plot_topomap(data=top_ica[:, j_t],
                             pos=pos,
                             axes=ax,
                             show=False)
            for j_t in range(top_alpha.shape[1]):
                ax = fg.add_subplot(
                    4, n_tops * len(subj),
                    n_tops * len(subj) * 3 + n_tops * j_s + j_t + 1 +
                    top_ica.shape[1])
                ax.set_xlabel('CSP{}'.format(j_t + 1))
                labels, fs = get_lsl_info_from_xml(f['stream_info.xml'][0])
                channels = [
                    label for label in labels if label not in drop_channels
                ]
                pos = ch_names_to_2d_pos(channels)
                plot_topomap(data=top_alpha[:, j_t],
                             pos=pos,
                             axes=ax,
                             show=False)

            # plot powers
            if normalize_by == 'opened':
                norm = powers['1. Opened'].mean()
            elif normalize_by == 'beta':
                norm = np.mean(pow_theta)
            else:
                print('WARNING: norm = 1')
            print('norm', norm)

            ax1 = fg.add_subplot(3, len(subj), j_s + 1)
            ax = fg.add_subplot(3, len(subj), j_s + len(subj) + 1)
            t = 0
            for j_p, ((name, pow),
                      (name, x)) in enumerate(zip(powers.items(),
                                                  raw.items())):
                if name == '2228. FB':
                    from scipy.signal import periodogram
                    fff = plt.figure()
                    fff.gca().plot(*periodogram(x, fs, nfft=fs * 3),
                                   c=cm[name.split()[1]])
                    plt.xlim(0, 80)
                    plt.ylim(0, 3e-11)
                    plt.show()
                print(name)
                time = np.arange(t, t + len(x)) / fs
                color = cm[''.join(
                    [i for i in name.split()[1] if not i.isdigit()])]
                ax1.plot(time, fft_filter(x, fs, (2, 45)), c=color, alpha=0.4)
                ax1.plot(time, alpha[name], c=color)
                t += len(x)
                ax.plot([j_p], [pow.mean() / norm],
                        'o',
                        c=color,
                        markersize=10)
                ax.errorbar([j_p], [pow.mean() / norm],
                            yerr=pow.std() / norm,
                            c=color,
                            ecolor=color)
            fb_x = np.hstack([[j] * len(pows)
                              for j, (key, pows) in enumerate(powers.items())
                              if 'FB' in key])
            fb_y = np.hstack(
                [pows for key, pows in powers.items() if 'FB' in key]) / norm
            sns.regplot(x=fb_x,
                        y=fb_y,
                        ax=ax,
                        color=cm['FB'],
                        scatter=False,
                        truncate=True)

            ax1.set_xlim(0, t / fs)
            ax1.set_ylim(-40, 40)
            plt.setp(ax.xaxis.get_majorticklabels(), rotation=70)
            ax.set_xticks(range(len(powers)))
            ax.set_xticklabels(powers.keys())
            ax.set_ylim(0, 3)
            ax.set_xlim(-1, len(powers))
            ax1.set_title('Day {}'.format(j_s + 1))
    return fg
Example #16
0
from pynfb.protocols.ssd.topomap_selector_ica import ICADialog
from PyQt5 import QtGui, QtWidgets

a = QtWidgets.QApplication([])

fig1, axes1 = plt.subplots(ncols=3)

for subj in range(3, 4):
    for day in range(2, 5):
        experiments = settings['subjects'][subj]
        experiment = experiments[day]
        reject = True
        with h5py.File('{}\\{}\\{}'.format(settings['dir'], experiment,
                                           'experiment_data.h5')) as f:
            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([
Example #17
0
        return predicted_labels

if __name__ == '__main__':
    np.random.seed(42)

    # loading anp pre processing

    import h5py
    import pylab as plt

    file = r'C:\Users\Nikolai\PycharmProjects\nfb\pynfb\results\BCI_Test_4_06-23_16-20-48\experiment_data.h5'
    #file = r'C:\Users\Nikolai\PycharmProjects\nfb\pynfb\results\BCI_Test_2_1_06-22_17-01-07\experiment_data.h5'

    labels_map = {'Open': 0, 'Right': 2, 'Left': 1}
    with h5py.File(file) as f:
        fs, ch_names, p_names = get_info(f, []) #['AUX', 'A1', 'A2', 'F4', 'Pz']
        before = []
        after = []
        before_labels = []
        after_labels = []
        k = 0
        for protocol in ['protocol{}'.format(j + 1) for j in range(len(f.keys()) - 3)]:
            name = f[protocol].attrs['name']
            if name in ['Right', 'Open', 'Left']:
                data = f[protocol + '/raw_data'][:]
                labels = np.ones(len(data), dtype=int) * labels_map[name]
                if k < 9:
                    before.append(data)
                    before_labels.append(labels)
                else:
                    after.append(data)