clatlan / cdiutils

A python package to help Coherent Diffraction Imaging (CDI) practitioners in their analysis.
MIT License
12 stars 7 forks source link

[FEATURE] plot surface projection #29

Closed ewbellec closed 9 months ago

ewbellec commented 9 months ago

Made a rough surface projection function that can be quite useful if you don't want to deal with a 3D representation image

Here's the code

def one_surface_projection(strain, axis, inverse):
    strain_used = np.copy(strain)
    if inverse:
        strain_used = np.flip(strain_used, axis=axis)

    # I have strain=nan outside the support so I can define the support here
    # If not, use your own support as input
    support = 1-np.isnan(strain_used) 
    support_surface = np.cumsum(support,axis=axis)
    support_surface[support_surface>1] = 0

    surface_strain = np.copy(strain_used)
    surface_strain[support_surface==0] = np.nan
    surface_strain = np.nanmean(surface_strain, axis=axis)
    return surface_strain

def plot_surface_projections(strain, voxel_sizes,
                             fw=3, fig_title=None):
    fig,ax = plt.subplots(2,3, figsize=(fw*3.3,fw*2))

    extent = [[0, strain.shape[2]*voxel_sizes[2]*.1, 0, strain.shape[1]*voxel_sizes[1]*.1],
              [0, strain.shape[2]*voxel_sizes[2]*.1, 0, strain.shape[0]*voxel_sizes[0]*.1],
              [0, strain.shape[1]*voxel_sizes[1]*.1, 0, strain.shape[0]*voxel_sizes[0]*.1]]
    imgs = []
    for n1, axis in enumerate(range(3)):
        for n0,inverse in enumerate([False, True]):
            surface_strain = one_surface_projection(strain, axis, inverse)
            imgs.append(ax[n0,n1].matshow(1e2*surface_strain, cmap='coolwarm', extent=extent[n1],
                                          vmin=np.nanmin(1e2*strain), vmax=np.nanmax(1e2*strain)))

    add_colorbar_subplot(fig,ax,imgs)
    for axe in ax.flatten():
        axe.locator_params(nbins=4)

    xlabel = ['Z (nm)', 'Z (nm)', 'Y (nm)']
    ylabel = ['Y (nm)', 'X (nm)', 'X (nm)']
    for n in range(3):
        for ii in range(2):
            ax[ii,n].set_xlabel(xlabel[n],fontsize=15*fw/4.)
            ax[ii,n].set_ylabel(ylabel[n],fontsize=15*fw/4.)
            ax[ii,n].xaxis.set_ticks_position('bottom')

    if fig_title is not None:
        fig.suptitle(fig_title, fontsize=fw*20/4.)

    fig.tight_layout()
    return
ewbellec commented 9 months ago

image Quite nice on this particle

ewbellec commented 9 months ago

Remark : only works for convex particles