Example #1
0
def plot_frame_displacement(realignment_parameters_file,
                            mean_FD_distribution=None,
                            figsize=(11.7, 8.3)):

    FD_power = calc_frame_dispalcement(realignment_parameters_file)

    fig = Figure(figsize=figsize)
    FigureCanvas(fig)

    if mean_FD_distribution:
        grid = GridSpec(2, 4)
    else:
        grid = GridSpec(1, 4)

    ax = fig.add_subplot(grid[0, :-1])
    ax.plot(FD_power)
    ax.set_xlim((0, len(FD_power)))
    ax.set_ylabel("Frame Displacement [mm]")
    ax.set_xlabel("Frame number")
    ylim = ax.get_ylim()

    ax = fig.add_subplot(grid[0, -1])
    sns.distplot(FD_power, vertical=True, ax=ax)
    ax.set_ylim(ylim)

    if mean_FD_distribution:
        ax = fig.add_subplot(grid[1, :])
        sns.distplot(mean_FD_distribution, ax=ax)
        ax.set_xlabel("Mean Frame Dispalcement (over all subjects) [mm]")
        MeanFD = FD_power.mean()
        label = "MeanFD = %g" % MeanFD
        plot_vline(MeanFD, label, ax=ax)

    return fig
Example #2
0
def plot_frame_displacement(realignment_parameters_file, mean_FD_distribution=None, figsize=(11.7,8.3)):

    FD_power = calc_frame_dispalcement(realignment_parameters_file)

    fig = Figure(figsize=figsize)
    FigureCanvas(fig)
    
    if mean_FD_distribution:
        grid = GridSpec(2, 4)
    else:
        grid = GridSpec(1, 4)
    
    ax = fig.add_subplot(grid[0,:-1])
    ax.plot(FD_power)
    ax.set_xlim((0, len(FD_power)))
    ax.set_ylabel("Frame Displacement [mm]")
    ax.set_xlabel("Frame number")
    ylim = ax.get_ylim()
    
    ax = fig.add_subplot(grid[0,-1])
    sns.distplot(FD_power, vertical=True, ax=ax)
    ax.set_ylim(ylim)
    
    if mean_FD_distribution:
        ax = fig.add_subplot(grid[1,:])
        sns.distplot(mean_FD_distribution, ax=ax)
        ax.set_xlabel("Mean Frame Dispalcement (over all subjects) [mm]")
        MeanFD = FD_power.mean()
        label = "MeanFD = %g"%MeanFD
        plot_vline(MeanFD, label, ax=ax)
        
    return fig
Example #3
0
def plot_epi_T1_corregistration(
        mean_epi_file,
        reg_file,
        fssubjects_dir,
        subject_id,
        similarity_distribution=None,
        figsize=(11.7, 8.3),
):

    fig = plt.figure(figsize=figsize)

    if similarity_distribution:
        ax = plt.subplot(2, 1, 1)
        sns.distplot(similarity_distribution.values(), ax=ax)
        ax.set_xlabel(
            "EPI-T1 similarity after coregistration (over all subjects)")
        cur_similarity = similarity_distribution[subject_id]
        label = "similarity = %g" % cur_similarity
        plot_vline(cur_similarity, label, ax=ax)

        ax = plt.subplot(2, 1, 0)
    else:
        ax = plt.subplot(1, 1, 0)

    res = ApplyVolTransform(source_file=mean_epi_file,
                            reg_file=reg_file,
                            fs_target=True,
                            subjects_dir=fssubjects_dir,
                            terminal_output="none").run()

    func = nb.load(res.outputs.transformed_file).get_data()
    func_affine = nb.load(res.outputs.transformed_file).get_affine()

    ribbon_file = "%s/%s/mri/ribbon.mgz" % (fssubjects_dir, subject_id)
    ribbon_nii = nb.load(ribbon_file)
    ribbon_data = ribbon_nii.get_data()
    ribbon_data[ribbon_data > 1] = 1
    ribbon_affine = ribbon_nii.get_affine()

    slicer = viz.plot_anat(
        np.asarray(func),
        np.asarray(func_affine),
        black_bg=True,
        cmap=cm.Greys_r,  # @UndefinedVariable
        cut_coords=(-6, 3, 32),
        figure=fig,
        axes=ax,
        draw_cross=False)
    slicer.contour_map(np.asarray(ribbon_data),
                       np.asarray(ribbon_affine),
                       levels=[.51],
                       colors=[
                           'r',
                       ])

    return fig
Example #4
0
def plot_distrbution_of_values(main_file, mask_file, xlabel, distribution=None, xlabel2=None, figsize=(11.7,8.3)):
    data = _get_values_inside_a_mask(main_file, mask_file)

    fig = Figure(figsize=figsize)
    FigureCanvas(fig)
    
    gs = GridSpec(2, 1)
    ax = fig.add_subplot(gs[0, 0])
    sns.distplot(data.astype(np.double), kde=False, bins=100, ax=ax)
    ax.set_xlabel(xlabel)
    
    ax = fig.add_subplot(gs[1, 0])
    sns.distplot(np.array(distribution).astype(np.double), ax=ax)
    cur_val = np.median(data)
    label = "%g"%cur_val
    plot_vline(cur_val, label, ax=ax)
    ax.set_xlabel(xlabel2)
    
    return fig
Example #5
0
def plot_distrbution_of_values(main_file, mask_file, xlabel, distribution=None, xlabel2=None, figsize=(11.7,8.3)):
    data = _get_values_inside_a_mask(main_file, mask_file)

    fig = Figure(figsize=figsize)
    FigureCanvas(fig)
    
    gs = GridSpec(2, 1)
    ax = fig.add_subplot(gs[0, 0])
    sns.distplot(data.astype(np.double), kde=False, bins=100, ax=ax)
    ax.set_xlabel(xlabel)
    
    ax = fig.add_subplot(gs[1, 0])
    sns.distplot(np.array(distribution).astype(np.double), ax=ax)
    cur_val = np.median(data)
    label = "%g"%cur_val
    plot_vline(cur_val, label, ax=ax)
    ax.set_xlabel(xlabel2)
    
    return fig
Example #6
0
def plot_FD(fd1d, mean_FD_distribution, subject,figsize = (8.3,8.3)):

    threshold = 0.2
    FD_power      = np.genfromtxt(fd1d)
    meanFD        = np.round(np.mean(FD_power), decimals = 2)
    rmsFD         = np.sqrt(np.mean(FD_power))
    count         = np.int(FD_power[FD_power>threshold].size)
    percentFD     = (count*100/(len(FD_power)+1))

    fig = plt.figure(figsize=figsize)

    fig.subplots_adjust(wspace=0.3)
    fig.set_size_inches(12, 8)

    grid = GridSpec(2, 4)

    ax = plt.subplot(grid[0,:-1])
    ax.plot(FD_power)
    ylim = ax.get_ylim()
    ax.set_xlim((0, len(FD_power)))
    ax.set_ylabel("%s Frame Displacement [mm]" %subject)
    ax.set_xlabel("Frame #")
    ax.text(20, (ylim[1] - ylim[1]*0.05), 'FD mean = %s'%meanFD, va='center', size = 18, color = 'r')
    ax.text(20, (ylim[1] - ylim[1]*0.2), '%s%% (%s) > threshold  '%(percentFD, count), va='center', size = 18, color = 'r')
    #ax.text(20, (ylim[1] - ylim[1]*0.15), 'are above the threshold '%, va='center', size = 18, color = 'r')
    plt.axhline(threshold, linestyle='dashed', linewidth=2)#,color='r')

    ax = plt.subplot(grid[0,-1])
    sns.distplot(FD_power, vertical = True, ax = ax)
    ax.set_ylim(ylim)

    ax= plt.subplot(grid[1,:])
    sns.distplot(mean_FD_distribution, ax=ax)
    ax.set_xlabel("%s Mean Frame Dispalcement (over all subjects) [mm]"%subject)
    label = "MeanFD = %g"%meanFD
    plot_vline(meanFD, label, ax=ax)

    png_name = str(os.path.join(os.getcwd(), 'qc_fd_plot.png'))
    plt.savefig(png_name, dpi=190, bbox_inches='tight')

    return fig
Example #7
0
def plot_epi_T1_corregistration(mean_epi_file, reg_file, fssubjects_dir, subject_id, similarity_distribution=None, figsize=(11.7,8.3),):
       
    fig = plt.figure(figsize=figsize)
    
    if similarity_distribution:
        ax = plt.subplot(2,1,1)
        sns.distplot(similarity_distribution.values(), ax=ax)
        ax.set_xlabel("EPI-T1 similarity after coregistration (over all subjects)")
        cur_similarity = similarity_distribution[subject_id]
        label = "similarity = %g"%cur_similarity
        plot_vline(cur_similarity, label, ax=ax)
        
        ax = plt.subplot(2,1,0)
    else:
        ax = plt.subplot(1,1,0)
    
    res = ApplyVolTransform(source_file = mean_epi_file,
                            reg_file = reg_file,
                            fs_target = True,
                            subjects_dir = fssubjects_dir,
                            terminal_output = "none").run()

    func = nb.load(res.outputs.transformed_file).get_data()
    func_affine = nb.load(res.outputs.transformed_file).get_affine()
    
    ribbon_file = "%s/%s/mri/ribbon.mgz"%(fssubjects_dir, subject_id)
    ribbon_nii = nb.load(ribbon_file)
    ribbon_data = ribbon_nii.get_data()
    ribbon_data[ribbon_data > 1] = 1
    ribbon_affine = ribbon_nii.get_affine()
    
    slicer = viz.plot_anat(np.asarray(func), np.asarray(func_affine), black_bg=True,
                           cmap = cm.Greys_r,  # @UndefinedVariable
                           cut_coords = (-6,3,32),
                           figure = fig,
                           axes = ax,
                           draw_cross = False)
    slicer.contour_map(np.asarray(ribbon_data), np.asarray(ribbon_affine), levels=[.51], colors=['r',])
    
    return fig
Example #8
0
def plot_distrbution_of_values(main_file, mask_file, xlabel, distribution=None, xlabel2=None, figsize=(11.7,8.3)):

    data = _get_values_inside_a_mask(main_file, mask_file)

    fig = plt.figure(figsize=figsize)
    fig.subplots_adjust(wspace=0.3)
    fig.set_size_inches(12, 8)

    gs = GridSpec(2, 1)
    ax = plt.subplot(gs[0, 0])
    sns.distplot(data.astype(np.double), kde=False, bins=100, ax=ax)
    ax.set_xlabel(xlabel)

    ax = plt.subplot(gs[1, 0])
    sns.distplot(np.array(distribution).astype(np.double), ax=ax)
    cur_val = np.median(data)
    label = "%g"%cur_val
    plot_vline(cur_val, label, ax=ax)
    ax.set_xlabel(xlabel2)

    png_name = str(os.path.join(os.getcwd(), 'qc_tsnr_plot.png'))
    plt.savefig(png_name, dpi=190, bbox_inches='tight')

    return fig