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 03a7736b..330cf889 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 @@ -984,15 +984,18 @@ def process_images_and_add_to_dataframe(df_fmb, df, seg_img_list, raw_img_list): raw_slices_rs = rescale_intensities(raw_slices, "egfp", "uint8") + dfi = df[df["track_id"] == track_id].set_index("index_sequence") + predicted_breakdown = dfi["predicted_breakdown"].values[0] + # now add items to the dataframe for i, yx_zx_zy in enumerate(["yx", "zx"]): df_fmb.loc[timepoint, f"raw_{yx_zx_zy}"] = [raw_slices_rs[i]] df_fmb.loc[timepoint, f"seg_{yx_zx_zy}"] = [seg_slices[i]] # now retrieve the contours - if ( - label_img_val > 0 - ): # only draw the contour if the cell is present in the segmentation image + if label_img_val > 0 and timepoint < (predicted_breakdown + 1): + # only draw the contour if the cell is present in the segmentation image + # and the timepoint is before breakdown # identify the contours from the label image and save them as matplotlib Polygons contour_factor = [1 / RESCALE_FACTOR_100x_to_20X, 1 / RESCALE_FACTOR_100x_to_20X] contour_and_color_list = get_matplotlib_contours_on_image( diff --git a/nuc_morph_analysis/analyses/dataset_images_for_figures/movie_dataset_formation_breakdown.py b/nuc_morph_analysis/analyses/dataset_images_for_figures/movie_dataset_formation_breakdown.py index 7bc77d63..bda8da0e 100644 --- a/nuc_morph_analysis/analyses/dataset_images_for_figures/movie_dataset_formation_breakdown.py +++ b/nuc_morph_analysis/analyses/dataset_images_for_figures/movie_dataset_formation_breakdown.py @@ -15,8 +15,9 @@ matplotlib.rcParams["pdf.fonttype"] = 42 matplotlib.rcParams["font.family"] = "Arial" -load_local = False +load_save_local = False make_frames = True +make_movie = True # set figure, panel to get save directory info figure = "dataset" @@ -27,26 +28,29 @@ colony = "medium" if make_frames: - if load_local: - if not os.path.exists(savedir/ "df_fmb.pkl"): - print("no saved df_fmb.pkl, processing images and saving") - # load the tracking CSV for medium from FMS - # define dataset from which to collect images - # collect information for the dataset - df = global_dataset_filtering.load_dataset_with_features() - df = df[df["colony"] == colony] - df_fb = figure_helper.assemble_formation_breakdown_movie_dataframe(df) - # load the images for each timepoint and add them to the dataframe - seg_img_list, raw_img_list = figure_helper.load_images_for_formation_middle_breakdown( - df_fb, df, colony - ) - df_fb = figure_helper.process_images_and_add_to_dataframe(df_fb, df, seg_img_list, raw_img_list) - df_fb.to_pickle(os.path.join(savedir, "df_fmb.pkl")) - print("saved df_fmb.pkl") - else: - print("reading from saved df_fmb.pkl") - df_fb = pd.read_pickle(os.path.join(savedir, "df_fmb.pkl")) - + # if you want to load locally and the file exists, load it + if load_save_local and os.path.exists(savedir/ "df_.pkl"): + print("reading from saved df_fb.pkl") + df_fb = pd.read_pickle(os.path.join(savedir, "df_fb.pkl")) + # if you dont' want to load locally or the file doesn't exist, process from scratch + else: + print("processing images from scratch") + # load the tracking CSV for medium from FMS + # define dataset from which to collect images + # collect information for the dataset + df = global_dataset_filtering.load_dataset_with_features() + df = df[df["colony"] == colony] + df_fb = figure_helper.assemble_formation_breakdown_movie_dataframe(df) + # load the images for each timepoint and add them to the dataframe + seg_img_list, raw_img_list = figure_helper.load_images_for_formation_middle_breakdown( + df_fb, df, colony + ) + df_fb = figure_helper.process_images_and_add_to_dataframe(df_fb, df, seg_img_list, raw_img_list) + # if you wanted to load and save locally, save the dataframe + if load_save_local: + df_fb.to_pickle(os.path.join(savedir, "df_fb.pkl")) + print("saved df_fb.pkl") + # %% # determine line width for tails and contours and ROI drawn with matplotlib contour_linewidth = 0.3 @@ -160,7 +164,8 @@ # now save the figure as a png track_id = EXAMPLE_TRACKS["figure_dataset_formation_and_breakdown"] - savepath = os.path.join(savedir, "movie", f"frame_{i}.png") + frame = int(dfb.loc[i, "index_sequence"] - 2) + savepath = os.path.join(savedir, "movie", f"frame_{frame}.png") fig.savefig( savepath, bbox_inches="tight", @@ -169,9 +174,11 @@ ) os.chmod(savepath, 0o777) + print ("Making mp4") # get list of paths to all movie frames, make sure they are # sorted in timepoint order then save to an mp4 movie frame_list = os.listdir(savedir / "movie") sorted_frames = sorted(frame_list, key=lambda x: int(x.split("_")[1].split(".")[0])) figure_helper.make_mp4(savedir, "formation_breakdown_example_movie", sorted_frames) +