From 3d0463a6da3ffc0695604984003d1f6da92334dd Mon Sep 17 00:00:00 2001 From: Chris Frick Date: Wed, 27 Nov 2024 12:34:05 -0800 Subject: [PATCH] address PR comments and clean up code --- .../figure_helper.py | 1 + .../check_for_size_dependence.py} | 8 +++-- ...l_density_at_different_zarr_resolutions.py | 0 ...lter_for_uncaught_pseudo_cell_artifacts.py | 0 ...date_watershed_psuedo_cell_seg_workflow.py | 0 ...igure_watershed_based_density_schematic.py | 31 +++++++------------ .../density/run_all_density_workflows.py | 9 ------ 7 files changed, 18 insertions(+), 31 deletions(-) rename nuc_morph_analysis/analyses/density/{confirm_no_corr_btn_density_and _nuclear_area.py => extra_checks/check_for_size_dependence.py} (92%) rename nuc_morph_analysis/analyses/density/{ => extra_checks}/confirm_identical_density_at_different_zarr_resolutions.py (100%) rename nuc_morph_analysis/analyses/density/{ => extra_checks}/visualize_filter_for_uncaught_pseudo_cell_artifacts.py (100%) rename nuc_morph_analysis/analyses/density/{ => extra_checks}/visually_validate_watershed_psuedo_cell_seg_workflow.py (100%) delete mode 100644 nuc_morph_analysis/analyses/density/run_all_density_workflows.py diff --git a/nuc_morph_analysis/analyses/dataset_images_for_figures/figure_helper.py b/nuc_morph_analysis/analyses/dataset_images_for_figures/figure_helper.py index 99d72f0a..31071488 100644 --- a/nuc_morph_analysis/analyses/dataset_images_for_figures/figure_helper.py +++ b/nuc_morph_analysis/analyses/dataset_images_for_figures/figure_helper.py @@ -109,6 +109,7 @@ def return_glasbey_on_dark(N=255, cell_id=None, cell_color=None, from_list=False N: int, the number of colors to return cell_id: int, the cell id to change the color of cell_color: array, the RGB color to change the cell to + from_list: bool, whether colormap should be returned from ListedColormap or not """ from matplotlib.colors import LinearSegmentedColormap, ListedColormap from nuc_morph_analysis.analyses.dataset_images_for_figures.glasbey_on_dark import ( diff --git a/nuc_morph_analysis/analyses/density/confirm_no_corr_btn_density_and _nuclear_area.py b/nuc_morph_analysis/analyses/density/extra_checks/check_for_size_dependence.py similarity index 92% rename from nuc_morph_analysis/analyses/density/confirm_no_corr_btn_density_and _nuclear_area.py rename to nuc_morph_analysis/analyses/density/extra_checks/check_for_size_dependence.py index 648fc30a..bf9e5852 100644 --- a/nuc_morph_analysis/analyses/density/confirm_no_corr_btn_density_and _nuclear_area.py +++ b/nuc_morph_analysis/analyses/density/extra_checks/check_for_size_dependence.py @@ -1,4 +1,5 @@ #%% +# this code is not used in the final analysis, but was used to confirm that there is no correlation between density and nuclear area from nuc_morph_analysis.lib.visualization.reference_points import COLONY_COLORS, COLONY_LABELS import matplotlib.pyplot as plt import numpy as np @@ -9,6 +10,8 @@ from pathlib import Path from sklearn.linear_model import LinearRegression from matplotlib.ticker import MaxNLocator +import matplotlib + #%% # set figure directory @@ -22,7 +25,6 @@ dfm = filter_data.all_timepoints_minimal_filtering(df) #%% # plot density over time for each colony along colony time -import matplotlib matplotlib.rcParams.update({'font.size': 8}) matplotlib.rcParams.update({'axes.titlesize': 8}) matplotlib.rcParams.update({'axes.labelsize': 8}) @@ -36,10 +38,10 @@ #%% -# plot density as a function of nucleus size (and compare to old density metric) +# plot density as a function of nucleus size colony='medium' x_col = '2d_area_nucleus' -for yi,y_col in enumerate(['2d_area_nuc_cell_ratio','density','inv_cyto_density']): +for yi,y_col in enumerate(['2d_area_nuc_cell_ratio']): dfsub = dfm[dfm['colony']==colony].copy() dfsub.dropna(subset=[y_col],inplace=True) diff --git a/nuc_morph_analysis/analyses/density/confirm_identical_density_at_different_zarr_resolutions.py b/nuc_morph_analysis/analyses/density/extra_checks/confirm_identical_density_at_different_zarr_resolutions.py similarity index 100% rename from nuc_morph_analysis/analyses/density/confirm_identical_density_at_different_zarr_resolutions.py rename to nuc_morph_analysis/analyses/density/extra_checks/confirm_identical_density_at_different_zarr_resolutions.py diff --git a/nuc_morph_analysis/analyses/density/visualize_filter_for_uncaught_pseudo_cell_artifacts.py b/nuc_morph_analysis/analyses/density/extra_checks/visualize_filter_for_uncaught_pseudo_cell_artifacts.py similarity index 100% rename from nuc_morph_analysis/analyses/density/visualize_filter_for_uncaught_pseudo_cell_artifacts.py rename to nuc_morph_analysis/analyses/density/extra_checks/visualize_filter_for_uncaught_pseudo_cell_artifacts.py diff --git a/nuc_morph_analysis/analyses/density/visually_validate_watershed_psuedo_cell_seg_workflow.py b/nuc_morph_analysis/analyses/density/extra_checks/visually_validate_watershed_psuedo_cell_seg_workflow.py similarity index 100% rename from nuc_morph_analysis/analyses/density/visually_validate_watershed_psuedo_cell_seg_workflow.py rename to nuc_morph_analysis/analyses/density/extra_checks/visually_validate_watershed_psuedo_cell_seg_workflow.py diff --git a/nuc_morph_analysis/analyses/density/figure_watershed_based_density_schematic.py b/nuc_morph_analysis/analyses/density/figure_watershed_based_density_schematic.py index 7fc1078d..55ac9e02 100644 --- a/nuc_morph_analysis/analyses/density/figure_watershed_based_density_schematic.py +++ b/nuc_morph_analysis/analyses/density/figure_watershed_based_density_schematic.py @@ -14,8 +14,7 @@ from nuc_morph_analysis.analyses.dataset_images_for_figures.figure_helper import return_glasbey_on_dark - -from nuc_morph_analysis.analyses.density.visually_validate_watershed_psuedo_cell_seg_workflow import get_contours_from_pair_of_2d_seg_image, draw_contours_on_image +from nuc_morph_analysis.analyses.density.extra_checks.visually_validate_watershed_psuedo_cell_seg_workflow import get_contours_from_pair_of_2d_seg_image, draw_contours_on_image from nuc_morph_analysis.analyses.dataset_images_for_figures.figure_helper import INTENSITIES_DICT import matplotlib @@ -64,7 +63,7 @@ def determine_colormaps(img,key,crop_exp): def run_validation_and_plot(TIMEPOINT=88,track=81463,colony='medium',RESOLUTION_LEVEL=1,plot_everything=False, testing=False): """ - run an image through the watershed based pseudo cell segmentation and examine the outputs + run an image through the watershed based pseudo cell segmentation and examine the outputs (as full fov and crop within that fov) optionally, run a test image through the same pipeline Parameters @@ -121,7 +120,6 @@ def run_validation_and_plot(TIMEPOINT=88,track=81463,colony='medium',RESOLUTION_ for full_crop, sizes in [('crop',(track_x,track_y,crop_w,crop_h)),('full',(0,0,mip.shape[1],mip.shape[0]))]: x1,y1,w,h = sizes crop_exp = np.index_exp[y1:y1+h,x1:x1+w] - # key_list = ['raw_image','mip_of_labeled_image','binarized_mip','distance','pseudo_cells_img','overlay','colorize'] key_list = ['mip_of_labeled_image','distance','pseudo_cells_img','colorize'] @@ -201,29 +199,24 @@ def run_validation_and_plot(TIMEPOINT=88,track=81463,colony='medium',RESOLUTION_ ax.set_position(position) # adjust position of colorbar cbar_position = cbar.ax.get_position() - # cbar.ax.set_position([cbar_position.x0+0.05,cbar_position.y0,cbar_position.width,cbar_position.height]) # for left location cbar.ax.set_position([cbar_position.x0,cbar_position.y0-0.15,cbar_position.width,cbar_position.height]) # for bottom location # now save the figure savedir = Path(__file__).parent / 'figures' / 'watershed_figure_illustration' savedir.mkdir(exist_ok=True,parents=True) - for ext in ['.png','.pdf']: - savename = f'{colony}_{track}_{TIMEPOINT}_{full_crop}_res{RESOLUTION_LEVEL}_{cmapstr}' - savepath = savedir / savename - print(f'Saved figure to {savepath}') - save_and_show_plot(str(savepath), - file_extension=ext, - figure=fig, - transparent=True, - keep_open=True, - **{'dpi':600} - ) + ext = '.pdf' + savename = f'{colony}_{track}_{TIMEPOINT}_{full_crop}_res{RESOLUTION_LEVEL}_{cmapstr}' + savepath = savedir / savename + save_and_show_plot(str(savepath), + file_extension=ext, + figure=fig, + transparent=True, + keep_open=True, + **{'dpi':600} + ) if __name__ == '__main__': # set the details - # dft0 = run_validation_and_plot(120,track=84619,colony='medium',RESOLUTION_LEVEL=1,plot_everything=True) - # dft1 = run_validation_and_plot(0,track=87172,colony='medium',RESOLUTION_LEVEL=1,plot_everything=False) - # dft1 = run_validation_and_plot(48,track=87099,colony='medium',RESOLUTION_LEVEL=1,plot_everything=False) dft1 = run_validation_and_plot(88,track=81463,colony='medium',RESOLUTION_LEVEL=1,plot_everything=False) diff --git a/nuc_morph_analysis/analyses/density/run_all_density_workflows.py b/nuc_morph_analysis/analyses/density/run_all_density_workflows.py deleted file mode 100644 index a4e36a51..00000000 --- a/nuc_morph_analysis/analyses/density/run_all_density_workflows.py +++ /dev/null @@ -1,9 +0,0 @@ -# this code runs all workflows in the density folder (not all of these are in the paper) -from nuc_morph_analysis.analyses.density import visually_validate_watershed_psuedo_cell_seg_workflow -visually_validate_watershed_psuedo_cell_seg_workflow.run_validation_and_plot(plot_everything=False) -from nuc_morph_analysis.analyses.density import confirm_identical_density_at_different_zarr_resolutions -from nuc_morph_analysis.analyses.density import figure_watershed_based_density_schematic -figure_watershed_based_density_schematic.run_validation_and_plot() -from nuc_morph_analysis.analyses.density import confirm_no_corr_btn_density_and_nuclear_area - -print('done') \ No newline at end of file