Пример #1
0
def get_maps(prior,
             prior_list,
             std_list,
             CR_std_maps,
             VE_list,
             tdof_list,
             mask_file,
             smoothing=False):
    volume_indices = np.asarray(nb.load(mask_file).dataobj).astype(bool)

    Y = np.array(prior_list)
    average = Y.mean(axis=0)

    # compute MAD to be resistant to outliers
    mad = np.median(np.abs(Y - np.median(Y, axis=0)), axis=0)
    #network_var=Y.std(axis=0)
    network_var = mad

    X = np.array(std_list)
    corr_map_std = elementwise_spearman(X, Y)
    X = np.array(CR_std_maps)
    corr_map_CR_std = elementwise_spearman(X, Y)
    X = np.array(VE_list)
    corr_map_VE = elementwise_spearman(X, Y)

    # tdof effect; if there's no variability don't compute
    if np.array(tdof_list).std() == 0:
        corr_map_tdof = None
    else:
        tdof = np.array(tdof_list).reshape(-1, 1)
        corr_map_tdof = elementwise_spearman(tdof, Y)

    if smoothing:
        prior = np.array(
            nilearn.image.smooth_img(recover_3D(mask_file, prior),
                                     0.3).dataobj)[volume_indices]
        average = np.array(
            nilearn.image.smooth_img(recover_3D(mask_file, average),
                                     0.3).dataobj)[volume_indices]
        corr_map_std = np.array(
            nilearn.image.smooth_img(recover_3D(mask_file, corr_map_std),
                                     0.3).dataobj)[volume_indices]
        corr_map_CR_std = np.array(
            nilearn.image.smooth_img(recover_3D(mask_file, corr_map_CR_std),
                                     0.3).dataobj)[volume_indices]
        corr_map_VE = np.array(
            nilearn.image.smooth_img(recover_3D(mask_file, corr_map_VE),
                                     0.3).dataobj)[volume_indices]
        if np.array(tdof_list).std() == 0:
            corr_map_tdof = None
        else:
            corr_map_tdof = np.array(
                nilearn.image.smooth_img(recover_3D(mask_file, corr_map_tdof),
                                         0.3).dataobj)[volume_indices]

    return prior, average, network_var, corr_map_std, corr_map_CR_std, corr_map_VE, corr_map_tdof
Пример #2
0
def spatial_external_formating(spatial_info, file_dict):
    import os
    import pathlib  # Better path manipulation
    from rabies.analysis_pkg import analysis_functions
    mask_file = file_dict['brain_mask_file']
    bold_file = file_dict['bold_file']
    filename_split = pathlib.Path(bold_file).name.rsplit(".nii")

    # calculate STD and tSNR map on preprocessed timeseries
    std_filename = os.path.abspath(filename_split[0] + '_tSTD.nii.gz')
    analysis_functions.recover_3D(
        mask_file, spatial_info['temporal_std']).to_filename(std_filename)

    GS_corr_filename = os.path.abspath(filename_split[0] + '_GS_corr.nii.gz')
    analysis_functions.recover_3D(
        mask_file, spatial_info['GS_corr']).to_filename(GS_corr_filename)

    DVARS_corr_filename = os.path.abspath(filename_split[0] +
                                          '_DVARS_corr.nii.gz')
    analysis_functions.recover_3D(
        mask_file, spatial_info['DVARS_corr']).to_filename(DVARS_corr_filename)

    FD_corr_filename = os.path.abspath(filename_split[0] + '_FD_corr.nii.gz')
    analysis_functions.recover_3D(
        mask_file, spatial_info['FD_corr']).to_filename(FD_corr_filename)

    DR_maps_filename = os.path.abspath(filename_split[0] + '_DR_maps.nii.gz')
    analysis_functions.recover_3D_multiple(
        mask_file, spatial_info['DR_all']).to_filename(DR_maps_filename)

    if len(spatial_info['prior_modeling_maps']) > 0:
        import numpy as np
        prior_modeling_filename = os.path.abspath(filename_split[0] +
                                                  '_prior_modeling.nii.gz')
        analysis_functions.recover_3D_multiple(
            mask_file,
            np.array(spatial_info['prior_modeling_maps'])).to_filename(
                prior_modeling_filename)
    else:
        prior_modeling_filename = None

    return std_filename, GS_corr_filename, DVARS_corr_filename, FD_corr_filename, DR_maps_filename, prior_modeling_filename
Пример #3
0
def scan_diagnosis(bold_file, mask_file_dict, temporal_info, spatial_info, CR_data_dict, regional_grayplot=False):
    template_file = mask_file_dict['template_file']
    
    fig = plt.figure(figsize=(6, 18))
    #fig.suptitle(name, fontsize=30, color='white')
    
    ax0 = fig.add_subplot(3,1,1)
    ax1 = fig.add_subplot(12,1,5)
    ax1_ = fig.add_subplot(12,1,6)
    ax2 = fig.add_subplot(6,1,4)
    ax3 = fig.add_subplot(6,1,5)
    ax4 = fig.add_subplot(6,1,6)

    # disable function
    regional_grayplot=False
    if regional_grayplot:
        
        from mpl_toolkits.axes_grid1 import make_axes_locatable
        divider = make_axes_locatable(ax0)
        
        im, slice_alt, region_mask_label = grayplot_regional(
            bold_file, mask_file_dict, fig, ax0)
        ax0.yaxis.labelpad = 40
        ax_slice = divider.append_axes('left', size='5%', pad=0.0)
        ax_label = divider.append_axes('left', size='5%', pad=0.0)

        ax_slice.imshow(slice_alt.reshape(-1, 1), cmap='gray',
                        vmin=0, vmax=1.1, aspect='auto')
        ax_label.imshow(region_mask_label.reshape(-1, 1),
                        cmap='Spectral', aspect='auto')
        ax_slice.axis('off')
        ax_label.axis('off')

    else:
        im = grayplot(bold_file, mask_file_dict, fig, ax0)

    ax0.set_ylabel('Voxels', fontsize=20)
    ax0.spines['right'].set_visible(False)
    ax0.spines['top'].set_visible(False)
    ax0.spines['bottom'].set_visible(False)
    ax0.spines['left'].set_visible(False)
    ax0.axes.get_yaxis().set_ticks([])
    plt.setp(ax1.get_xticklabels(), visible=False)

    y = temporal_info['FD_trace'].to_numpy()
    x = range(len(y))
    ax0.set_xlim([0, len(y)-1])
    ax1.set_xlim([0, len(y)-1])
    ax1_.set_xlim([0, len(y)-1])
    ax2.set_xlim([0, len(y)-1])
    ax3.set_xlim([0, len(y)-1])
    ax4.set_xlim([0, len(y)-1])

    # plot the motion timecourses
    confounds_csv = CR_data_dict['confounds_csv']
    time_range = CR_data_dict['time_range']
    frame_mask = CR_data_dict['frame_mask']
    df = pd.read_csv(confounds_csv)
    # take proper subset of timepoints
    ax1.plot(x,df['mov1'].to_numpy()[time_range][frame_mask])
    ax1.plot(x,df['mov2'].to_numpy()[time_range][frame_mask])
    ax1.plot(x,df['mov3'].to_numpy()[time_range][frame_mask])
    ax1.legend(['translation 1', 'translation 2', 'translation 3'],
               loc='center left', bbox_to_anchor=(1.15, 0.5))
    ax1.spines['right'].set_visible(False)
    ax1.spines['top'].set_visible(False)
    plt.setp(ax1.get_xticklabels(), visible=False)

    ax1_.plot(x,df['rot1'].to_numpy()[time_range][frame_mask])
    ax1_.plot(x,df['rot2'].to_numpy()[time_range][frame_mask])
    ax1_.plot(x,df['rot3'].to_numpy()[time_range][frame_mask])
    ax1_.legend(['rotation 1', 'rotation 2', 'rotation 3'],
                loc='center left', bbox_to_anchor=(1.15, 0.5))
    plt.setp(ax1_.get_xticklabels(), visible=False)
    ax1_.spines['right'].set_visible(False)
    ax1_.spines['top'].set_visible(False)

    y = temporal_info['FD_trace'].to_numpy()
    ax2.plot(x,y, 'r')
    ax2.set_ylabel('FD in mm', fontsize=15)
    DVARS = temporal_info['DVARS']
    DVARS[0] = None
    ax2_ = ax2.twinx()
    y2 = DVARS
    ax2_.plot(x,y2, 'b')
    ax2_.set_ylabel('DVARS', fontsize=15)
    ax2.spines['right'].set_visible(False)
    ax2.spines['top'].set_visible(False)
    ax2_.spines['top'].set_visible(False)
    plt.setp(ax2.get_xticklabels(), visible=False)
    plt.setp(ax2_.get_xticklabels(), visible=False)
    ax2.legend(['Framewise \nDisplacement (FD)'
                ], loc='center left', bbox_to_anchor=(1.15, 0.6))
    ax2_.legend(['DVARS'
                ], loc='center left', bbox_to_anchor=(1.15, 0.4))

    ax3.plot(x,temporal_info['edge_trace'])
    ax3.plot(x,temporal_info['WM_trace'])
    ax3.plot(x,temporal_info['CSF_trace'])
    ax3.plot(x,temporal_info['predicted_time'])
    ax3.set_ylabel('Mask L2-norm', fontsize=15)
    ax3_ = ax3.twinx()
    ax3_.plot(x,temporal_info['VE_temporal'], 'darkviolet')
    ax3_.set_ylabel('CR R^2', fontsize=15)
    ax3_.spines['right'].set_visible(False)
    ax3_.spines['top'].set_visible(False)
    plt.setp(ax3_.get_xticklabels(), visible=False)
    ax3.legend(['Edge Mask', 'WM Mask', 'CSF Mask', 'CR prediction'
                ], loc='center left', bbox_to_anchor=(1.15, 0.7))
    ax3_.legend(['CR R^2'
                ], loc='center left', bbox_to_anchor=(1.15, 0.3))
    ax3_.set_ylim([0,1])

    y = temporal_info['signal_trace']
    ax4.plot(x,y)
    ax4.plot(x,temporal_info['noise_trace'])
    ax4.legend(['BOLD components', 'Confound components'
                ], loc='center left', bbox_to_anchor=(1.15, 0.5))
    ax4.spines['right'].set_visible(False)
    ax4.spines['top'].set_visible(False)
    ax4.set_xlabel('Timepoint', fontsize=15)
    ax4.set_ylabel('Abs. Beta \ncoefficients (Avg.)', fontsize=15)

    dr_maps = spatial_info['DR_BOLD']
    mask_file = mask_file_dict['brain_mask']

    nrows = 6+dr_maps.shape[0]

    fig2, axes2 = plt.subplots(nrows=nrows, ncols=3, figsize=(12*3, 2*nrows))
    plt.tight_layout()

    from rabies.visualization import otsu_scaling, plot_3d

    axes = axes2[0, :]
    scaled = otsu_scaling(template_file)
    plot_3d(axes, scaled, fig2, vmin=0, vmax=1,
            cmap='gray', alpha=1, cbar=False, num_slices=6)
    temporal_std = spatial_info['temporal_std']
    analysis_functions.recover_3D(
        mask_file, temporal_std).to_filename('temp_img.nii.gz')
    sitk_img = sitk.ReadImage('temp_img.nii.gz')

    # select vmax at 95th percentile value
    vector = temporal_std.flatten()
    vector.sort()
    vmax = vector[int(len(vector)*0.95)]
    cbar_list = plot_3d(axes, sitk_img, fig2, vmin=0, vmax=vmax,
            cmap='inferno', alpha=1, cbar=True, num_slices=6)
    for cbar in cbar_list:
        cbar.ax.get_yaxis().labelpad = 35
        cbar.set_label('Standard \n Deviation', fontsize=15, rotation=270, color='white')
    for ax in axes:
        ax.set_title('BOLD-Temporal s.d.', fontsize=30, color='white')


    axes = axes2[1, :]
    scaled = otsu_scaling(template_file)
    plot_3d(axes, scaled, fig2, vmin=0, vmax=1,
            cmap='gray', alpha=1, cbar=False, num_slices=6)
    predicted_std = spatial_info['predicted_std']
    analysis_functions.recover_3D(
        mask_file, predicted_std).to_filename('temp_img.nii.gz')
    sitk_img = sitk.ReadImage('temp_img.nii.gz')

    # select vmax at 95th percentile value
    vector = predicted_std.flatten()
    vector.sort()
    vmax = vector[int(len(vector)*0.95)]
    cbar_list = plot_3d(axes, sitk_img, fig2, vmin=0, vmax=vmax,
            cmap='inferno', alpha=1, cbar=True, num_slices=6)
    for cbar in cbar_list:
        cbar.ax.get_yaxis().labelpad = 35
        cbar.set_label('Standard \n Deviation', fontsize=15, rotation=270, color='white')
    for ax in axes:
        ax.set_title('CR-Temporal s.d.', fontsize=30, color='white')


    axes = axes2[2, :]
    plot_3d(axes, scaled, fig2, vmin=0, vmax=1,
            cmap='gray', alpha=1, cbar=False, num_slices=6)
    analysis_functions.recover_3D(
        mask_file, spatial_info['VE_spatial']).to_filename('temp_img.nii.gz')
    sitk_img = sitk.ReadImage('temp_img.nii.gz')
    cbar_list = plot_3d(axes, sitk_img, fig2, vmin=0, vmax=1, cmap='inferno',
            alpha=1, cbar=True, threshold=0.1, num_slices=6)
    for cbar in cbar_list:
        cbar.ax.get_yaxis().labelpad = 20
        cbar.set_label('CR R^2', fontsize=15, rotation=270, color='white')
    for ax in axes:
        ax.set_title('CR R^2', fontsize=30, color='white')

    axes = axes2[3, :]
    plot_3d(axes, scaled, fig2, vmin=0, vmax=1,
            cmap='gray', alpha=1, cbar=False, num_slices=6)
    analysis_functions.recover_3D(
        mask_file, spatial_info['GS_corr']).to_filename('temp_img.nii.gz')
    sitk_img = sitk.ReadImage('temp_img.nii.gz')
    cbar_list = plot_3d(axes, sitk_img, fig2, vmin=-1, vmax=1, cmap='cold_hot',
            alpha=1, cbar=True, threshold=0.1, num_slices=6)
    for cbar in cbar_list:
        cbar.ax.get_yaxis().labelpad = 20
        cbar.set_label("Pearson's' r", fontsize=15, rotation=270, color='white')
    for ax in axes:
        ax.set_title('Global Signal Correlation', fontsize=30, color='white')

    axes = axes2[4, :]
    plot_3d(axes, scaled, fig2, vmin=0, vmax=1,
            cmap='gray', alpha=1, cbar=False, num_slices=6)
    analysis_functions.recover_3D(
        mask_file, spatial_info['DVARS_corr']).to_filename('temp_img.nii.gz')
    sitk_img = sitk.ReadImage('temp_img.nii.gz')
    cbar_list = plot_3d(axes, sitk_img, fig2, vmin=-1, vmax=1, cmap='cold_hot',
            alpha=1, cbar=True, threshold=0.1, num_slices=6)
    for cbar in cbar_list:
        cbar.ax.get_yaxis().labelpad = 20
        cbar.set_label("Pearson's' r", fontsize=15, rotation=270, color='white')
    for ax in axes:
        ax.set_title('DVARS Correlation', fontsize=30, color='white')

    axes = axes2[5, :]
    plot_3d(axes, scaled, fig2, vmin=0, vmax=1,
            cmap='gray', alpha=1, cbar=False, num_slices=6)
    analysis_functions.recover_3D(
        mask_file, spatial_info['FD_corr']).to_filename('temp_img.nii.gz')
    sitk_img = sitk.ReadImage('temp_img.nii.gz')
    cbar_list = plot_3d(axes, sitk_img, fig2, vmin=-1, vmax=1, cmap='cold_hot',
            alpha=1, cbar=True, threshold=0.1, num_slices=6)
    for cbar in cbar_list:
        cbar.ax.get_yaxis().labelpad = 20
        cbar.set_label("Pearson's' r", fontsize=15, rotation=270, color='white')
    for ax in axes:
        ax.set_title('FD Correlation', fontsize=30, color='white')

    for i in range(dr_maps.shape[0]):
        axes = axes2[i+6, :]

        analysis_functions.recover_3D(
            mask_file, dr_maps[i, :]).to_filename('temp_img.nii.gz')
        sitk_img = sitk.ReadImage('temp_img.nii.gz')
        cbar_list = masked_plot(fig2,axes, sitk_img, scaled, percentile=0.015, vmax=None)

        for cbar in cbar_list:
            cbar.ax.get_yaxis().labelpad = 35
            cbar.set_label("Beta \nCoefficient", fontsize=15, rotation=270, color='white')
        for ax in axes:
            ax.set_title(f'BOLD component {i}', fontsize=30, color='white')

    return fig, fig2
Пример #4
0
def spatial_external_formating(spatial_info, file_dict):
    import os
    import pathlib  # Better path manipulation
    from rabies.analysis_pkg import analysis_functions
    mask_file = file_dict['mask_file']
    bold_file = file_dict['bold_file']
    filename_split = pathlib.Path(
        bold_file).name.rsplit(".nii")

    VE_filename = os.path.abspath(filename_split[0]+'_VE.nii.gz')
    analysis_functions.recover_3D(
        mask_file, spatial_info['VE_spatial']).to_filename(VE_filename)

    std_filename = os.path.abspath(filename_split[0]+'_tSTD.nii.gz')
    analysis_functions.recover_3D(
        mask_file, spatial_info['temporal_std']).to_filename(std_filename)

    predicted_std_filename = os.path.abspath(filename_split[0]+'_predicted_std.nii.gz')
    analysis_functions.recover_3D(
        mask_file, spatial_info['predicted_std']).to_filename(predicted_std_filename)

    GS_corr_filename = os.path.abspath(filename_split[0]+'_GS_corr.nii.gz')
    analysis_functions.recover_3D(
        mask_file, spatial_info['GS_corr']).to_filename(GS_corr_filename)

    DVARS_corr_filename = os.path.abspath(
        filename_split[0]+'_DVARS_corr.nii.gz')
    analysis_functions.recover_3D(
        mask_file, spatial_info['DVARS_corr']).to_filename(DVARS_corr_filename)

    FD_corr_filename = os.path.abspath(filename_split[0]+'_FD_corr.nii.gz')
    analysis_functions.recover_3D(
        mask_file, spatial_info['FD_corr']).to_filename(FD_corr_filename)

    DR_maps_filename = os.path.abspath(filename_split[0]+'_DR_maps.nii.gz')
    analysis_functions.recover_3D_multiple(
        mask_file, spatial_info['DR_all']).to_filename(DR_maps_filename)

    if len(spatial_info['dual_ICA_maps']) > 0:
        import numpy as np
        dual_ICA_filename = os.path.abspath(
            filename_split[0]+'_dual_ICA.nii.gz')
        analysis_functions.recover_3D_multiple(mask_file, np.array(
            spatial_info['dual_ICA_maps'])).to_filename(dual_ICA_filename)
    else:
        dual_ICA_filename = None

    return VE_filename, std_filename, predicted_std_filename, GS_corr_filename, DVARS_corr_filename, FD_corr_filename, DR_maps_filename, dual_ICA_filename
Пример #5
0
def scan_diagnosis(bold_file,
                   mask_file_dict,
                   temporal_info,
                   spatial_info,
                   confounds_csv,
                   regional_grayplot=False):
    template_file = mask_file_dict['template_file']
    from mpl_toolkits.axes_grid1 import make_axes_locatable
    fig, axCenter = plt.subplots(figsize=(6, 18))
    fig.subplots_adjust(.2, .1, .8, .95)

    divider = make_axes_locatable(axCenter)
    ax1 = divider.append_axes('bottom', size='25%', pad=0.5)
    ax1_ = divider.append_axes('bottom', size='25%', pad=0.1)
    ax2 = divider.append_axes('bottom', size='50%', pad=0.5)
    ax3 = divider.append_axes('bottom', size='50%', pad=0.5)
    ax4 = divider.append_axes('bottom', size='50%', pad=0.5)

    if regional_grayplot:
        im, slice_alt, region_mask_label = grayplot_regional(
            bold_file, mask_file_dict, fig, axCenter)
        axCenter.yaxis.labelpad = 40
        ax_slice = divider.append_axes('left', size='5%', pad=0.0)
        ax_label = divider.append_axes('left', size='5%', pad=0.0)

        ax_slice.imshow(slice_alt.reshape(-1, 1),
                        cmap='gray',
                        vmin=0,
                        vmax=1.1,
                        aspect='auto')
        ax_label.imshow(region_mask_label.reshape(-1, 1),
                        cmap='Spectral',
                        aspect='auto')
        ax_slice.axis('off')
        ax_label.axis('off')

    else:
        im = grayplot(bold_file, mask_file_dict, fig, axCenter)

    axCenter.set_ylabel('Voxels', fontsize=20)
    axCenter.spines['right'].set_visible(False)
    axCenter.spines['top'].set_visible(False)
    axCenter.spines['bottom'].set_visible(False)
    axCenter.spines['left'].set_visible(False)
    axCenter.axes.get_yaxis().set_ticks([])
    plt.setp(axCenter.get_xticklabels(), visible=False)

    # plot the motion timecourses
    import pandas as pd
    df = pd.read_csv(confounds_csv)
    ax1.plot(df['mov1'])
    ax1.plot(df['mov2'])
    ax1.plot(df['mov3'])
    ax1.legend(['translation 1', 'translation 2', 'translation 3'],
               loc='center left',
               bbox_to_anchor=(1, 0.5))
    ax1.spines['right'].set_visible(False)
    ax1.spines['top'].set_visible(False)
    plt.setp(ax1.get_xticklabels(), visible=False)

    ax1_.plot(df['rot1'])
    ax1_.plot(df['rot2'])
    ax1_.plot(df['rot3'])
    ax1_.legend(['rotation 1', 'rotation 2', 'rotation 3'],
                loc='center left',
                bbox_to_anchor=(1, 0.5))
    plt.setp(ax1_.get_xticklabels(), visible=False)
    ax1_.spines['right'].set_visible(False)
    ax1_.spines['top'].set_visible(False)

    #ax1.set_title(name, fontsize=15)
    y = temporal_info['FD_trace']
    ax2.plot(y, 'r')
    ax2.set_xlim([0, len(y)])
    ax2.legend(['Framewise Displacement (FD)'], loc='upper right')
    ax2.spines['right'].set_visible(False)
    ax2.spines['top'].set_visible(False)
    ax2.set_ylim([0.0, 0.1])
    plt.setp(ax2.get_xticklabels(), visible=False)

    DVARS = temporal_info['DVARS']
    DVARS[0] = None
    y = DVARS
    ax3.plot(y)
    ax3.set_xlim([0, len(y)])
    ax3.plot(temporal_info['edge_trace'])
    ax3.plot(temporal_info['WM_trace'])
    ax3.plot(temporal_info['CSF_trace'])
    ax3.plot(temporal_info['VE_temporal'])
    ax3.legend(['DVARS', 'Edge Mask', 'WM Mask', 'CSF Mask', 'CR R^2'],
               loc='center left',
               bbox_to_anchor=(1, 0.5))
    ax3.spines['right'].set_visible(False)
    ax3.spines['top'].set_visible(False)
    ax3.set_ylim([0.0, 1.5])
    plt.setp(ax3.get_xticklabels(), visible=False)

    y = temporal_info['signal_trace']
    ax4.plot(y)
    ax4.set_xlim([0, len(y)])
    ax4.plot(temporal_info['noise_trace'])
    ax4.legend(['BOLD components', 'Confound components'], loc='upper right')
    ax4.spines['right'].set_visible(False)
    ax4.spines['top'].set_visible(False)
    ax4.set_ylim([0.0, 4.0])
    ax4.set_xlabel('Timepoint', fontsize=15)

    dr_maps = spatial_info['DR_BOLD']
    mask_file = mask_file_dict['brain_mask']

    nrows = 5 + dr_maps.shape[0]

    fig2, axes2 = plt.subplots(nrows=nrows,
                               ncols=3,
                               figsize=(12 * 3, 2 * nrows))
    plt.tight_layout()

    from rabies.preprocess_pkg.preprocess_visual_QC import plot_3d, otsu_scaling

    axes = axes2[0, :]
    scaled = otsu_scaling(template_file)
    plot_3d(axes,
            scaled,
            fig2,
            vmin=0,
            vmax=1,
            cmap='gray',
            alpha=1,
            cbar=False,
            num_slices=6)
    analysis_functions.recover_3D(
        mask_file, spatial_info['temporal_std']).to_filename('temp_img.nii.gz')
    sitk_img = sitk.ReadImage('temp_img.nii.gz')
    plot_3d(axes,
            sitk_img,
            fig2,
            vmin=0,
            vmax=1,
            cmap='inferno',
            alpha=1,
            cbar=True,
            num_slices=6)
    for ax in axes:
        ax.set_title('Temporal STD', fontsize=25)

    axes = axes2[1, :]
    plot_3d(axes,
            scaled,
            fig2,
            vmin=0,
            vmax=1,
            cmap='gray',
            alpha=1,
            cbar=False,
            num_slices=6)
    analysis_functions.recover_3D(
        mask_file, spatial_info['VE_spatial']).to_filename('temp_img.nii.gz')
    sitk_img = sitk.ReadImage('temp_img.nii.gz')
    plot_3d(axes,
            sitk_img,
            fig2,
            vmin=0,
            vmax=1,
            cmap='inferno',
            alpha=1,
            cbar=True,
            threshold=0.1,
            num_slices=6)
    for ax in axes:
        ax.set_title('CR R^2', fontsize=25)

    axes = axes2[2, :]
    plot_3d(axes,
            scaled,
            fig2,
            vmin=0,
            vmax=1,
            cmap='gray',
            alpha=1,
            cbar=False,
            num_slices=6)
    analysis_functions.recover_3D(
        mask_file, spatial_info['GS_corr']).to_filename('temp_img.nii.gz')
    sitk_img = sitk.ReadImage('temp_img.nii.gz')
    plot_3d(axes,
            sitk_img,
            fig2,
            vmin=-1,
            vmax=1,
            cmap='cold_hot',
            alpha=1,
            cbar=True,
            threshold=0.1,
            num_slices=6)
    for ax in axes:
        ax.set_title('Global Signal Correlation', fontsize=25)

    axes = axes2[3, :]
    plot_3d(axes,
            scaled,
            fig2,
            vmin=0,
            vmax=1,
            cmap='gray',
            alpha=1,
            cbar=False,
            num_slices=6)
    analysis_functions.recover_3D(
        mask_file, spatial_info['DVARS_corr']).to_filename('temp_img.nii.gz')
    sitk_img = sitk.ReadImage('temp_img.nii.gz')
    plot_3d(axes,
            sitk_img,
            fig2,
            vmin=-1,
            vmax=1,
            cmap='cold_hot',
            alpha=1,
            cbar=True,
            threshold=0.1,
            num_slices=6)
    for ax in axes:
        ax.set_title('DVARS Correlation', fontsize=25)

    axes = axes2[4, :]
    plot_3d(axes,
            scaled,
            fig2,
            vmin=0,
            vmax=1,
            cmap='gray',
            alpha=1,
            cbar=False,
            num_slices=6)
    analysis_functions.recover_3D(
        mask_file, spatial_info['FD_corr']).to_filename('temp_img.nii.gz')
    sitk_img = sitk.ReadImage('temp_img.nii.gz')
    plot_3d(axes,
            sitk_img,
            fig2,
            vmin=-1,
            vmax=1,
            cmap='cold_hot',
            alpha=1,
            cbar=True,
            threshold=0.1,
            num_slices=6)
    for ax in axes:
        ax.set_title('FD Correlation', fontsize=25)

    for i in range(dr_maps.shape[0]):
        axes = axes2[i + 5, :]
        plot_3d(axes,
                scaled,
                fig2,
                vmin=0,
                vmax=1,
                cmap='gray',
                alpha=1,
                cbar=False,
                num_slices=6)
        analysis_functions.recover_3D(
            mask_file, dr_maps[i, :]).to_filename('temp_img.nii.gz')
        sitk_img = sitk.ReadImage('temp_img.nii.gz')
        plot_3d(axes,
                sitk_img,
                fig2,
                vmin=-1,
                vmax=1,
                cmap='cold_hot',
                alpha=1,
                cbar=True,
                threshold=0.1,
                num_slices=6)
        for ax in axes:
            ax.set_title('BOLD component %s' % (i), fontsize=25)

    return fig, fig2
Пример #6
0
    def _run_interface(self, runtime):
        from rabies.preprocess_pkg.utils import flatten_list
        merged = flatten_list(list(self.inputs.spatial_info_list))
        if len(merged) < 3:
            import logging
            log = logging.getLogger('root')
            log.warning(
                "Cannot run statistics on a sample size smaller than 3, so an empty figure is generated."
            )
            fig, axes = plt.subplots()
            fig.savefig(os.path.abspath('empty_dataset_diagnosis.png'),
                        bbox_inches='tight')

            setattr(self, 'figure_dataset_diagnosis',
                    os.path.abspath('empty_dataset_diagnosis.png'))
            return runtime

        dict_keys = [
            'temporal_std', 'VE_spatial', 'GS_corr', 'DVARS_corr', 'FD_corr',
            'DR_BOLD', 'prior_modeling_maps'
        ]

        voxelwise_list = []
        for spatial_info in merged:
            sub_list = [spatial_info[key] for key in dict_keys]
            voxelwise_sub = np.array(sub_list[:5])
            if len(sub_list[6]) > 0:
                voxelwise_sub = np.concatenate(
                    (voxelwise_sub, np.array(sub_list[5]), np.array(
                        sub_list[6])),
                    axis=0)
            else:
                voxelwise_sub = np.concatenate(
                    (voxelwise_sub, np.array(sub_list[5])), axis=0)
            voxelwise_list.append(voxelwise_sub)
            num_DR_maps = len(sub_list[5])
            num_prior_maps = len(sub_list[6])
        voxelwise_array = np.array(voxelwise_list)

        label_name = [
            'temporal_std', 'VE_spatial', 'GS_corr', 'DVARS_corr', 'FD_corr'
        ]
        label_name += [
            'BOLD Dual Regression map %s' % (i) for i in range(num_DR_maps)
        ]
        label_name += [
            'BOLD Dual Convergence map %s' % (i) for i in range(num_prior_maps)
        ]

        template_file = self.inputs.mask_file_dict['template_file']
        mask_file = self.inputs.mask_file_dict['brain_mask']
        from rabies.preprocess_pkg.preprocess_visual_QC import plot_3d, otsu_scaling
        scaled = otsu_scaling(template_file)

        ncols = 5
        fig, axes = plt.subplots(nrows=voxelwise_array.shape[1],
                                 ncols=ncols,
                                 figsize=(12 * ncols,
                                          2 * voxelwise_array.shape[1]))
        for i, x_label in zip(range(voxelwise_array.shape[1]), label_name):
            for j, y_label in zip(range(ncols), label_name[:ncols]):
                ax = axes[i, j]
                if i <= j:
                    ax.axis('off')
                    continue

                X = voxelwise_array[:, i, :]
                Y = voxelwise_array[:, j, :]
                corr = elementwise_corrcoef(X, Y)

                plot_3d([ax],
                        scaled,
                        fig,
                        vmin=0,
                        vmax=1,
                        cmap='gray',
                        alpha=1,
                        cbar=False,
                        num_slices=6,
                        planes=('coronal'))
                analysis_functions.recover_3D(
                    mask_file, corr).to_filename('temp_img.nii.gz')
                sitk_img = sitk.ReadImage('temp_img.nii.gz')
                plot_3d([ax],
                        sitk_img,
                        fig,
                        vmin=-0.7,
                        vmax=0.7,
                        cmap='cold_hot',
                        alpha=1,
                        cbar=True,
                        threshold=0.1,
                        num_slices=6,
                        planes=('coronal'))
                ax.set_title('Cross-correlation for %s and %s' %
                             (x_label, y_label),
                             fontsize=15)
        fig.savefig(os.path.abspath('dataset_diagnosis.png'),
                    bbox_inches='tight')

        setattr(self, 'figure_dataset_diagnosis',
                os.path.abspath('dataset_diagnosis.png'))
        return runtime
Пример #7
0
def eval_relationships(maps, mask_file, percentile=0.01):
    prior, average, network_var, corr_map_std, corr_map_CR_std, corr_map_VE, corr_map_tdof = maps

    # 1) the correlation between the prior and the fitted average
    #prior_cor = np.corrcoef(prior,average)[0,1]
    # 2) the weighted average of the temporal s.d. correlations
    #weights=prior/prior.sum()
    #weighted_std_corr=(weights*corr_map_std)[np.isnan(corr_map_std)==0].sum()
    #std_corr = np.corrcoef(prior[np.isnan(corr_map_std)==0], corr_map_std[np.isnan(corr_map_std)==0])[0,1]
    # 3) the correlation between the prior and the fitted average
    #VE_corr = np.corrcoef(prior[np.isnan(corr_map_VE)==0], corr_map_VE[np.isnan(corr_map_VE)==0])[0,1]
    #weighted_VE_corr=(weights*corr_map_VE)[np.isnan(corr_map_VE)==0].sum()

    tmppath = tempfile.mkdtemp()
    map_masks = []
    for map in maps:
        if map is None:
            map_masks.append(None)
            tdof_avg_corr = None
            continue
        recover_3D(mask_file, map).to_filename(f'{tmppath}/temp_img.nii.gz')
        img = sitk.ReadImage(f'{tmppath}/temp_img.nii.gz')
        mask = percent_masking(img, percentile=percentile)
        map_masks.append(mask)

        # get also estimates of effect sizes for power estimation
        # defined by average correlation within the temporal s.d. map for temporal s.d./CR R^2
        if map is corr_map_std:
            std_avg_corr = sitk.GetArrayFromImage(img)[map_masks[0]].mean()
        if map is corr_map_CR_std:
            CR_std_avg_corr = sitk.GetArrayFromImage(img)[map_masks[0]].mean()
        if map is corr_map_VE:
            VE_avg_corr = sitk.GetArrayFromImage(img)[map_masks[0]].mean()
        if map is corr_map_tdof:
            tdof_avg_corr = sitk.GetArrayFromImage(img)[map_masks[0]].mean()

    prior_mask, average_mask, std_mask, corr_map_std_mask, corr_map_CR_std_mask, corr_map_VE_mask, tdof_mask = map_masks

    if tdof_mask is None:
        tdof_spec = None
    else:
        tdof_spec = dice_coefficient(prior_mask, tdof_mask)

    return {
        'Overlap: Prior - Dataset avg.':
        dice_coefficient(prior_mask, average_mask),
        'Overlap: Prior - Dataset MAD':
        dice_coefficient(prior_mask, std_mask),
        'Overlap: Prior - BOLD-Temporal s.d.':
        dice_coefficient(prior_mask, corr_map_std_mask),
        'Overlap: Prior - CR-Temporal s.d.':
        dice_coefficient(prior_mask, corr_map_CR_std_mask),
        'Overlap: Prior - tDOF':
        tdof_spec,
        'Avg.: BOLD-Temporal s.d.':
        std_avg_corr,
        'Avg.: CR-Temporal s.d.':
        CR_std_avg_corr,
        'Avg.: tDOF':
        tdof_avg_corr,
    }
Пример #8
0
def spatial_crosscorrelations(merged, scaled, mask_file, fig_path):

    dict_keys = [
        'temporal_std', 'predicted_std', 'VE_spatial', 'GS_corr', 'DVARS_corr',
        'FD_corr', 'DR_BOLD', 'dual_ICA_maps'
    ]

    voxelwise_list = []
    for scan_data in merged:
        sub_list = [scan_data[key] for key in dict_keys]
        voxelwise_sub = np.array(sub_list[:6])
        if len(sub_list[7]) > 0:
            voxelwise_sub = np.concatenate(
                (voxelwise_sub, np.array(sub_list[6]), np.array(sub_list[7])),
                axis=0)
        else:
            voxelwise_sub = np.concatenate(
                (voxelwise_sub, np.array(sub_list[6])), axis=0)
        voxelwise_list.append(voxelwise_sub)
        num_prior_maps = len(sub_list[6])
    voxelwise_array = np.array(voxelwise_list)

    label_name = [
        'BOLD-Temporal s.d.', 'CR-Temporal s.d.', 'CR R^2', 'GS corr',
        'DVARS corr', 'FD corr'
    ]
    label_name += [
        f'BOLD Dual Regression map {i}' for i in range(num_prior_maps)
    ]
    label_name += [f'BOLD Dual ICA map {i}' for i in range(num_prior_maps)]

    ncols = 6
    fig, axes = plt.subplots(nrows=voxelwise_array.shape[1],
                             ncols=ncols,
                             figsize=(12 * ncols,
                                      2 * voxelwise_array.shape[1]))
    for i, x_label in zip(range(voxelwise_array.shape[1]), label_name):
        for j, y_label in zip(range(ncols), label_name[:ncols]):
            ax = axes[i, j]
            if i <= j:
                ax.axis('off')
                continue

            X = voxelwise_array[:, i, :]
            Y = voxelwise_array[:, j, :]
            corr = elementwise_spearman(X, Y)

            plot_3d([ax],
                    scaled,
                    fig,
                    vmin=0,
                    vmax=1,
                    cmap='gray',
                    alpha=1,
                    cbar=False,
                    num_slices=6,
                    planes=('coronal'))
            recover_3D(mask_file, corr).to_filename('temp_img.nii.gz')
            sitk_img = sitk.ReadImage('temp_img.nii.gz')
            cbar_list = plot_3d([ax],
                                sitk_img,
                                fig,
                                vmin=-1.0,
                                vmax=1.0,
                                cmap='cold_hot',
                                alpha=1,
                                cbar=True,
                                threshold=0.1,
                                num_slices=6,
                                planes=('coronal'))
            ax.set_title(f'Cross-correlation for \n{x_label} and {y_label}',
                         fontsize=20,
                         color='white')
            for cbar in cbar_list:
                cbar.ax.get_yaxis().labelpad = 20
                cbar.set_label("Spearman rho",
                               fontsize=12,
                               rotation=270,
                               color='white')

    fig.savefig(fig_path, bbox_inches='tight')
Пример #9
0
def plot_relationships(mask_file, scaled, maps, percentile=0.01):

    prior, average, network_var, corr_map_std, corr_map_CR_std, corr_map_VE, corr_map_tdof = maps

    fig, axes = plt.subplots(nrows=6, ncols=1, figsize=(12, 2 * 6))

    tmppath = tempfile.mkdtemp()
    recover_3D(mask_file, prior).to_filename(f'{tmppath}/temp_img.nii.gz')
    img = sitk.ReadImage(f'{tmppath}/temp_img.nii.gz')
    ax = axes[0]
    cbar_list = masked_plot(fig,
                            ax,
                            img,
                            scaled,
                            vmax=None,
                            percentile=percentile)
    ax.set_title('Prior network', fontsize=25, color='white')
    for cbar in cbar_list:
        cbar.ax.get_yaxis().labelpad = 20
        cbar.set_label("Prior measure",
                       fontsize=12,
                       rotation=270,
                       color='white')

    tmppath = tempfile.mkdtemp()
    recover_3D(mask_file, average).to_filename(f'{tmppath}/temp_img.nii.gz')
    img = sitk.ReadImage(f'{tmppath}/temp_img.nii.gz')
    ax = axes[1]
    cbar_list = masked_plot(fig,
                            ax,
                            img,
                            scaled,
                            vmax=None,
                            percentile=percentile)
    ax.set_title('Dataset average', fontsize=25, color='white')
    for cbar in cbar_list:
        cbar.ax.get_yaxis().labelpad = 20
        cbar.set_label("Mean", fontsize=12, rotation=270, color='white')

    tmppath = tempfile.mkdtemp()
    recover_3D(mask_file,
               network_var).to_filename(f'{tmppath}/temp_img.nii.gz')
    img = sitk.ReadImage(f'{tmppath}/temp_img.nii.gz')
    ax = axes[2]
    cbar_list = masked_plot(fig,
                            ax,
                            img,
                            scaled,
                            vmax=None,
                            percentile=percentile)
    ax.set_title('Dataset MAD', fontsize=25, color='white')
    for cbar in cbar_list:
        cbar.ax.get_yaxis().labelpad = 20
        cbar.set_label("Median Absolute \nDeviation",
                       fontsize=12,
                       rotation=270,
                       color='white')

    tmppath = tempfile.mkdtemp()
    recover_3D(mask_file,
               corr_map_std).to_filename(f'{tmppath}/temp_img.nii.gz')
    img = sitk.ReadImage(f'{tmppath}/temp_img.nii.gz')
    ax = axes[3]
    cbar_list = masked_plot(fig,
                            ax,
                            img,
                            scaled,
                            vmax=1.0,
                            percentile=percentile)
    ax.set_title('BOLD-Temporal s.d.', fontsize=25, color='white')
    for cbar in cbar_list:
        cbar.ax.get_yaxis().labelpad = 20
        cbar.set_label("Spearman rho",
                       fontsize=12,
                       rotation=270,
                       color='white')

    tmppath = tempfile.mkdtemp()
    recover_3D(mask_file,
               corr_map_CR_std).to_filename(f'{tmppath}/temp_img.nii.gz')
    img = sitk.ReadImage(f'{tmppath}/temp_img.nii.gz')
    ax = axes[4]
    cbar_list = masked_plot(fig,
                            ax,
                            img,
                            scaled,
                            vmax=1.0,
                            percentile=percentile)
    ax.set_title('CR-Temporal s.d.', fontsize=25, color='white')
    for cbar in cbar_list:
        cbar.ax.get_yaxis().labelpad = 20
        cbar.set_label("Spearman rho",
                       fontsize=12,
                       rotation=270,
                       color='white')

    ax = axes[5]
    if corr_map_tdof is None:
        ax.axis('off')
    else:
        tmppath = tempfile.mkdtemp()
        recover_3D(mask_file,
                   corr_map_tdof).to_filename(f'{tmppath}/temp_img.nii.gz')
        img = sitk.ReadImage(f'{tmppath}/temp_img.nii.gz')
        cbar_list = masked_plot(fig,
                                ax,
                                img,
                                scaled,
                                vmax=1.0,
                                percentile=percentile)
        ax.set_title('tDOF correlation', fontsize=25, color='white')
        for cbar in cbar_list:
            cbar.ax.get_yaxis().labelpad = 20
            cbar.set_label("Spearman rho",
                           fontsize=12,
                           rotation=270,
                           color='white')

    plt.tight_layout()

    return fig