Exemplo n.º 1
0
def tfce_process(conf):
    print('\trun tfce...')
    path_home = conf.path_home

    grand_average = conf.grand_average
    kind = conf.kind
    train = conf.train
    baseline = conf.baseline
    legend = conf.legend
    verbose = conf.verbose

    # perfom statistical check by means of Threshold Free Cluster Enhancement
    options = {
        'page-size': 'A3',
        'orientation': 'Landscape',
        'zoom': 0.57,
        'no-outline': None,
        'quiet': ''
    }
    planars = ['planar1', 'planar2', 'combine_planar']

    cur_dir = conf.path_tfce
    os.chdir(cur_dir)  #папка где будут сохраняться картинки
    if conf.grand_average == True:
        frequency = None
    else:
        frequency = conf.frequency
    if conf.plot_spectrogram:
        print('Spectrograms are ready, cannot continue')
        return 0
    comp1_mean, comp2_mean, contr, temp1, temp2, p_val, binary, subjects1 = compute_p_val(
        conf, conf.subjects, kind, train, frequency, conf.check_num_sens)
    df1 = contr[:, 0, 204:, :]
    df2 = contr[:, 1, 204:, :]
    if verbose:
        print('df1 shape', df1.shape)
        print('df2 shape', df2.shape)
    df1 = df1.transpose(2, 0, 1)
    df2 = df2.transpose(2, 0, 1)
    df1_mean = df1.mean(axis=1)
    df2_mean = df2.mean(axis=1)
    df1_stderr = np.zeros((df1.shape[0], df1.shape[2]))
    df2_stderr = np.zeros((df1.shape[0], df1.shape[2]))
    t_stat = np.zeros((df1.shape[0], df1.shape[2]))
    p_val = np.zeros((df1.shape[0], df1.shape[2]))
    res_tfce = np.zeros((df1.shape[0], df1.shape[2]))
    if verbose:
        print(res_tfce.shape)

    pos = io.loadmat(f'{path_home}pos_store.mat')['pos']
    chan_labels = to_str_ar(
        io.loadmat(f'{path_home}channel_labels.mat')['chanlabels'])
    dict_col = {
        'risk': 'salmon',
        'norisk': 'olivedrab',
        'prerisk': 'mediumpurple',
        'postrisk': 'darkturquoise',
        'prerisk_fb_negative': 'crimson',
        'prerisk_fb_positive': 'cyan',
        'risk_fb_negative': 'crimson',
        'risk_fb_positive': 'cyan',
        'postrisk_fb_negative': 'crimson',
        'postrisk_fb_positive': 'cyan',
        'norisk_fb_negative': 'red',
        'norisk_fb_positive': 'blue',
        'fb_positive_risk': 'cyan',
        'fb_negative_risk': 'red',
        'fb_positive_norisk': 'salmon',
        'fb_negative_norisk': 'crimson'
    }

    #working with combined planars
    for i in range(102):
        df1_stderr[:, i] = scipy.stats.sem(df1[:, :, i], axis=1)
        df2_stderr[:, i] = scipy.stats.sem(df2[:, :, i], axis=1)
        t_stat[:, i], p_val[:, i] = stats.ttest_rel(df1[:, :, i],
                                                    df2[:, :, i],
                                                    axis=1)
        res_tfce[:, i] = np.array(
            tfce(conf, df1[:, :, i], df2[:, :, i], title=chan_labels[i + 204]))
        res = plot_stat_comparison_tfce(conf,
                                        df1_mean[:, i],
                                        df2_mean[:, i],
                                        df1_stderr[:, i],
                                        df2_stderr[:, i],
                                        p_val[:, i],
                                        res_tfce[:, i],
                                        conf.time,
                                        title=chan_labels[i + 204],
                                        folder=f'{legend[0]}_vs_{legend[1]}',
                                        comp1_label=kind[0],
                                        comp2_label=kind[1],
                                        comp1_color=dict_col[f'{kind[0]}'],
                                        comp2_color=dict_col[f'{kind[1]}'])
        if verbose:
            print('\tPictures generated')

    for ind, planar in enumerate(planars):
        #place the channel time courses in html file
        html_name = conf.path_tfce + 'output_tfce/pic_compose_%s_%s_vs_%s_%s.html' % (
            planar, f'{legend[0]}', f'{legend[1]}', 'all')
        clear_html(html_name)
        add_str_html(html_name, '<!DOCTYPE html>')
        add_str_html(html_name, '<html>')
        add_str_html(html_name, '<body>')
        if grand_average == True:
            add_str_html(
                html_name,
                '<p style="font-size:32px;"><b> %s, %s, trained, TFCE corrected, %s,  %d subjects <span style="color:cyan;"> (p_val < 0.05)*(res_tfce==0)) </span> <span style="color:crimson;"> res_tfce == 1 </span> </b></p>'
                % (planar, conf.ERF, baseline, len(subjects1)))
        else:
            assert (grand_average == False)
            if conf.response:
                add_str_html(
                    html_name,
                    '<p style="font-size:32px;"><b> %s, averaged %s, trained, TFCE corrected, %s, %s, %d subjects <span style="color:cyan;"> (p_val < 0.05)*(res_tfce==0)) </span> <span style="color:crimson;"> res_tfce == 1 </span> </b></p>'
                    % (planar, frequency, baseline, conf.zero_point,
                       len(subjects1)))
            if conf.stim:
                add_str_html(
                    html_name,
                    '<p style="font-size:32px;"><b> %s, averaged %s, trained, TFCE corrected, %s, zero point at stimulus, %d subjects <span style="color:cyan;"> (p_val < 0.05)*(res_tfce==0)) </span> <span style="color:crimson;"> res_tfce == 1 </span> </b></p>'
                    % (planar, frequency, baseline, len(subjects1)))
        add_str_html(
            html_name,
            '<p style="font-size:32px;"><b> <span style="color: blue;"> %s </span> vs <span style="color: red;"> %s </span> </b></p>'
            % (legend[0], legend[1]))
        #placing the channel time courses and save the html
        if ind == 2:
            for ch_num in range(204, len(chan_labels)):
                pic = chan_labels[ch_num] + '.svg'
                add_pic_time_course_html(html_name, pic,
                                         f'{legend[0]}_vs_{legend[1]}',
                                         pos[ch_num], [200, 150])
        else:
            for ch_num in range(ind, 204, 2):
                pic = chan_labels[ch_num] + '.svg'
                add_pic_time_course_html(html_name, pic,
                                         f'{legend[0]}_vs_{legend[1]}',
                                         pos[ch_num], [200, 150])

        add_str_html(html_name, '</body>')
        add_str_html(html_name, '</html>')
        pdf_file = html_name.split("/")[1].split('.')[0]
        #print('/%s' % html_name)
        path = f'{cur_dir}/output_tfce/{legend[0]}_vs_{legend[1]}/all_pdf/'
        if verbose:
            print(path)
        os.makedirs(path, exist_ok=True)
    if verbose:
        print('\tAll printed')
    print('\ttfce completed')
Exemplo n.º 2
0
                        ch_type='planar1',
                        time_unit='s',
                        show=False,
                        title='%s - %s (cut) with fdr' %
                        (legend[1], legend[0]),
                        colorbar=True,
                        vmax=p_mul_topo_fdr_contrast,
                        vmin=-p_mul_topo_fdr_contrast,
                        extrapolate="local")
fig.savefig(os.path.join(output, legend[0] + '_vs_' + legend[1],
                         'p_value_cut_with_fdr.png'),
            dpi=300)
plt.close()

html_name = os.path.join(output, legend[0] + '_vs_' + legend[1] + '.html')
clear_html(html_name)
add_str_html(html_name, '<!DOCTYPE html>')
add_str_html(html_name, '<html>')
add_str_html(html_name, '<body>')
add_str_html(
    html_name,
    '<p style="font-size:20px;"><b> %s, average %s, %s, trained, %d subjects </b></p>'
    % (legend[0] + '_vs_' + legend[1], frequency, stimulus, len(subjects1)))
add_str_html(
    html_name,
    '<p style="font-size:20px;"><b> P_val < 0.05 marked (or saved from cutting) </b></p>'
)
add_str_html(html_name, '<table>')
for topo in topomaps:
    add_str_html(html_name, "<tr>")
    add_pic_topo_html(
def topo_stat(conf):
    print('\trun fdr for topomaps...')
    grand_average = conf.grand_average
    time = conf.time
    times_to_plot = conf.times_to_plot
    p_mul_topo = conf.p_mul_topo
    p_mul_topo_contrast = conf.p_mul_topo_contrast
    p_mul_topo_fdr_contrast = conf.p_mul_topo_fdr_contrast
    baseline = conf.baseline
    legend = conf.legend

    cur_dir = conf.path_fdr
    verbose = conf.verbose

    os.chdir(cur_dir)

    topomaps = [
        f'{legend[0]}', f'{legend[1]}', 'difference_fdr', 'difference_deep_fdr'
    ]
    options = {
        'page-size': 'A3',
        'orientation': 'Landscape',
        'zoom': 1.0,
        'no-outline': None,
        'quiet': ''
    }

    os.makedirs(os.path.join(conf.path_fdr, f'{legend[0]}_vs_{legend[1]}'),
                exist_ok=True)
    if grand_average == True:
        frequency = None
    else:
        frequency = conf.frequency

    #donor data file
    temp = mne.Evoked(f'{conf.path_home}donor-ave.fif', verbose='ERROR')
    temp.times = conf.time
    subjects = conf.subjects
    comp1_mean, comp2_mean, contr, temp1, temp2, p_val, binary, subjects1 = compute_p_val(
        conf, subjects, conf.kind, conf.train, frequency, conf.check_num_sens)
    df1 = contr[:, 0, 204:, :]  #per channel
    df2 = contr[:, 1, 204:, :]
    if verbose:
        print('df1 shape', df1.shape)
        print('df2 shape', df2.shape)

    #res_tfce = np.zeros((876, 102))
    pos = io.loadmat(f'{conf.path_home}pos_store.mat')['pos']
    chan_labels = to_str_ar(
        io.loadmat(f'{conf.path_home}channel_labels.mat')['chanlabels'])
    dict_col = {
        'risk': 'salmon',
        'norisk': 'olivedrab',
        'prerisk': 'mediumpurple',
        'postrisk': 'darkturquoise'
    }
    p_val_fdr = space_fdr(p_val)

    ##### CONDITION1 and Stat  ######
    # average = 0.1 means averaging of the power data over 100 ms
    if verbose:
        print('comp1_mean', comp1_mean.shape)
        print('df1', df1.shape)
        print('time', len(time))
        print('times to plot', len(times_to_plot))
    t_stat_con1, p_val_con1 = stats.ttest_1samp(df1, 0, axis=0)
    if verbose:
        print('p val con1 shape', p_val_con1.shape)
    width, height = p_val_con1.shape
    p_val_con1 = p_val_con1.reshape(width * height)
    _, p_val_con1 = mul.fdrcorrection(p_val_con1)
    p_val_con1 = p_val_con1.reshape((width, height))
    binary = p_val_binary(p_val_con1, treshold=0.05)

    temp.data = comp1_mean[204:, :]
    fig = temp.plot_topomap(times=times_to_plot,
                            average=0.1,
                            units="dB",
                            scalings=dict(eeg=1e6, grad=1, mag=1e15),
                            ch_type='planar1',
                            time_unit='s',
                            show=False,
                            title=legend[0] + ' stat fdr against zero',
                            colorbar=True,
                            vmax=p_mul_topo,
                            vmin=-p_mul_topo,
                            extrapolate="local",
                            mask=np.bool_(binary),
                            mask_params=dict(marker='o',
                                             markerfacecolor='w',
                                             markeredgecolor='k',
                                             linewidth=0,
                                             markersize=7,
                                             markeredgewidth=2))
    fig.savefig(os.path.join(conf.path_fdr, legend[0] + '_vs_' + legend[1],
                             legend[0] + '.png'),
                dpi=300)
    plt.close()

    ##### CONDITION2 and Stat ######

    #temp.data = comp2_mean[204:,:]
    t_stat_con2, p_val_con2 = stats.ttest_1samp(df2, 0, axis=0)
    width, height = p_val_con2.shape
    p_val_con2 = p_val_con2.reshape(width * height)
    _, p_val_con2 = mul.fdrcorrection(p_val_con2)
    p_val_con2 = p_val_con2.reshape((width, height))
    binary = p_val_binary(p_val_con2, treshold=0.05)

    temp.data = comp2_mean[204:, :]
    fig = temp.plot_topomap(times=times_to_plot,
                            average=0.1,
                            units='dB',
                            scalings=dict(eeg=1e6, grad=1, mag=1e15),
                            ch_type='planar1',
                            time_unit='s',
                            show=False,
                            title=legend[1] + ' stat fdr  against zero',
                            colorbar=True,
                            vmax=p_mul_topo,
                            vmin=-p_mul_topo,
                            extrapolate="local",
                            mask=np.bool_(binary),
                            mask_params=dict(marker='o',
                                             markerfacecolor='w',
                                             markeredgecolor='k',
                                             linewidth=0,
                                             markersize=7,
                                             markeredgewidth=2))
    fig.savefig(os.path.join(conf.path_fdr, legend[0] + '_vs_' + legend[1],
                             legend[1] + '.png'),
                dpi=300)
    plt.close()

    ##### CONDITION2 - CONDITION1 with marks no time  (space FDR) ######
    p_val_fdr = space_fdr(p_val)
    binary_fdr = p_val_binary(p_val_fdr, treshold=0.05)
    temp.data = comp2_mean[204:, :] - comp1_mean[204:, :]
    fig = temp.plot_topomap(
        times=times_to_plot,
        average=0.1,
        units='dB',
        scalings=dict(eeg=1e6, grad=1, mag=1e15),
        ch_type='planar1',
        time_unit='s',
        show=False,
        title='%s - %s  space fdr: marks for each head separately' %
        (legend[1], legend[0]),
        colorbar=True,
        vmax=p_mul_topo_contrast,
        vmin=-p_mul_topo_contrast,
        extrapolate="local",
        mask=np.bool_(binary_fdr[204:, :]),
        mask_params=dict(marker='o',
                         markerfacecolor='w',
                         markeredgecolor='k',
                         linewidth=0,
                         markersize=7,
                         markeredgewidth=2))
    fig.savefig(os.path.join(conf.path_fdr, legend[0] + '_vs_' + legend[1],
                             'difference_fdr.png'),
                dpi=300)
    plt.close()

    #### CONDITION2 - CONDITION1 with marks (WITH FDR) deep FDR with time ####

    t_stat, p_val_deep = stats.ttest_rel(df1, df2, axis=0)
    width, height = p_val_deep.shape
    p_val_resh = p_val_deep.reshape(width * height)
    _, p_val_deep_fdr = mul.fdrcorrection(p_val_resh)
    p_val_deep_fdr = p_val_deep_fdr.reshape((width, height))
    binary_deep_fdr = p_val_binary(p_val_deep_fdr, treshold=0.05)
    temp.data = comp2_mean[204:, :] - comp1_mean[204:, :]

    fig = temp.plot_topomap(times=times_to_plot,
                            average=0.1,
                            units='dB',
                            scalings=dict(eeg=1e6, grad=1, mag=1e15),
                            ch_type='planar1',
                            time_unit='s',
                            show=False,
                            title='%s - %s deep fdr over all heads' %
                            (legend[1], legend[0]),
                            colorbar=True,
                            vmax=p_mul_topo_fdr_contrast,
                            vmin=-p_mul_topo_fdr_contrast,
                            extrapolate="local",
                            mask=np.bool_(binary_deep_fdr),
                            mask_params=dict(marker='o',
                                             markerfacecolor='w',
                                             markeredgecolor='k',
                                             linewidth=0,
                                             markersize=7,
                                             markeredgewidth=2))
    fig.savefig(os.path.join(conf.path_fdr, legend[0] + '_vs_' + legend[1],
                             'difference_deep_fdr.png'),
                dpi=300)
    plt.close()
    html_name = os.path.join(conf.path_fdr,
                             legend[0] + '_vs_' + legend[1] + '.html')
    clear_html(html_name)
    add_str_html(html_name, '<!DOCTYPE html>')
    add_str_html(html_name, '<html>')
    add_str_html(html_name, '<body>')
    if grand_average:
        add_str_html(
            html_name,
            '<p style="font-size:20px;"><b> %s, %s, trained, %s, %s, %d subjects </b></p>'
            % (legend[0] + '_vs_' + legend[1], conf.ERF, baseline,
               conf.zero_point, len(subjects1)))
    else:
        add_str_html(
            html_name,
            '<p style="font-size:20px;"><b> %s, %s, trained, %s, %s, %d subjects </b></p>'
            % (legend[0] + '_vs_' + legend[1], frequency, baseline,
               conf.zero_point, len(subjects1)))

    add_str_html(
        html_name,
        '<p style="font-size:20px;"><b> boolean fdr  = 1  marked </b></p>')
    add_str_html(html_name, '<table>')
    for topo in topomaps:
        add_str_html(html_name, "<tr>")
        add_pic_topo_html(
            html_name,
            os.path.join(legend[0] + '_vs_' + legend[1], topo + '.png'))
    add_str_html(html_name, "</tr>")
    add_str_html(html_name, '</body>')
    add_str_html(html_name, '</html>')
    pdf_file = html_name.replace("html", "pdf")
    if verbose:
        print('All done!')
    print('\tfdr for topomaps completed')