Skip to content

Commit

Permalink
add df
Browse files Browse the repository at this point in the history
  • Loading branch information
Namsaraeva committed Dec 12, 2023
1 parent eed437a commit 6b747df
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/sparcscore/pipeline/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import os
import sys
import numpy as np
import pandas as pd
import torch
import gc
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -811,16 +812,21 @@ def cellpose_segmentation(self, input_image):
masks_cytosol_unfiltered = masks_cytosol.copy()

#log start time of cell filtering to track
timing_info = []

start = time.time()

timing_info.append(("1. Time when started the whole approach", start))

all_nucleus_ids = np.unique(masks_nucleus)[1:]
nucleus_cytosol_pairs = {}

self.log(f"1. Number of nuclei to filter: {len(all_nucleus_ids)}")
self.log(f"Number of nuclei to filter: {len(all_nucleus_ids)}")

current_time = time.time()

self.log("2. Started filtering cells (for nucleus_id in all_nucleus_ids).")
timing_info.append(("2. Time when started filtering cells (for nucleus_id in all_nucleus_ids)", current_time))

for nucleus_id in all_nucleus_ids:
time_in_the_loop = time.time()

Expand All @@ -829,14 +835,14 @@ def cellpose_segmentation(self, input_image):
# now get the coordinates of the nucleus
nucleus_pixels = np.nonzero(nucleus)

self.log(f"3. Time required for getting nucleus pixels in seconds: {time.time() - time_in_the_loop}")
timing_info.append((f"3. Time required for getting nucleus pixels in seconds for nucleus {nucleus_id}", time.time() - time_in_the_loop))

time_in_the_loop = time.time()

# check if those indices are not background in the cytosol mask
potential_cytosol = masks_cytosol[nucleus_pixels]

self.log(f"4. Time required for getting potential cytosol pixels in seconds: {time.time() - time_in_the_loop}")
timing_info.append((f"4. Time required for getting potential cytosol pixels in seconds for nucleus {nucleus_id}", time.time() - time_in_the_loop))

if np.all(potential_cytosol != 0):
time_in_the_loop = time.time()
Expand All @@ -847,7 +853,7 @@ def cellpose_segmentation(self, input_image):
all_counts = np.sum(counts)
cytosol_proportions = counts / all_counts

self.log(f"5. Time required for getting unique cytosol pixels and calculating their proportions in seconds: {time.time() - time_in_the_loop}")
timing_info.append((f"5. Time required for getting unique cytosol pixels and calculating their proportions in seconds for nucleus {nucleus_id}", time.time() - time_in_the_loop))

if np.any(cytosol_proportions >= self.config["filtering_threshold"]):
time_in_the_loop = time.time()
Expand All @@ -860,31 +866,31 @@ def cellpose_segmentation(self, input_image):
else:
nucleus_cytosol_pairs[nucleus_id] = 0

self.log(f"6. Time required for getting cytosol_id with max proportion in seconds: {time.time() - time_in_the_loop}")
timing_info.append((f"6. Time required for getting cytosol_id with max proportion in seconds for nucleus {nucleus_id}", time.time() - time_in_the_loop))

self.log("7. Time required for filtering cells (for nucleus_id in all_nucleus_ids) in seconds: {}".format(time.time() - current_time))
timing_info.append(("7. Time required for filtering cells (for nucleus_id in all_nucleus_ids) in seconds", time.time() - current_time))

# check if there are any cytosol masks that are assigned to multiple nuclei
cytosol_count = defaultdict(int)

self.log("8. Started filtering cytosol masks assigned to multiple nuclei.")

new_time = time.time()

timing_info.append(("8. Time when started counting the occurences of each cytosol id", new_time))

# Count the occurrences of each cytosol value
for cytosol in nucleus_cytosol_pairs.values():
cytosol_count[cytosol] += 1

self.log("9. Time required for counting the occurences of each cytosol id in seconds: {}".format(time.time() - new_time))
timing_info.append(("9. Time required for counting the occurences of each cytosol id in seconds", time.time() - new_time))

new_time = time.time()

# Find cytosol values assigned to more than one nucleus
for nucleus, cytosol in nucleus_cytosol_pairs.items():
if cytosol_count[cytosol] > 1:
nucleus_cytosol_pairs[nucleus] = 0
self.log("10. Time required for counting cytosol masks in seconds: {}".format(time.time() - new_time))

timing_info.append(("10. Time required for filtering cytosol ids that are assigned to more than one nucleus in seconds", time.time() - new_time))

new_time = time.time()

Expand All @@ -898,7 +904,7 @@ def cellpose_segmentation(self, input_image):
for cytosol_id in not_used_cytosol_ids:
masks_cytosol[masks_cytosol == cytosol_id] = 0

self.log("11. Time required for filtering cytosol masks that are not in the lookup table in seconds: {}".format(time.time() - new_time))
timing_info.append(("11. Time required for filtering cytosol masks that are not in the lookup table in seconds", time.time() - new_time))

new_time = time.time()

Expand All @@ -911,8 +917,8 @@ def cellpose_segmentation(self, input_image):
# set all nucleus ids that are not present in lookup table to 0 in the nucleus mask
for nucleus_id in not_used_nucleus_ids:
masks_nucleus[masks_nucleus == nucleus_id] = 0

self.log("12. Time required for filtering nucleus masks that are not in the lookup table in seconds: {}".format(time.time() - new_time))
timing_info.append(("12. Time required for filtering nucleus masks that are not in the lookup table in seconds", time.time() - new_time))

new_time = time.time()

Expand All @@ -929,11 +935,15 @@ def cellpose_segmentation(self, input_image):
masks_cytosol[condition] = nucleus_id
updated_cytosol_mask = np.logical_or(updated_cytosol_mask, condition)

self.log("13. Time required for updating nucleus-cytosol pairs in seconds: {}".format(time.time() - new_time))
timing_info.append(("13. Time required for filtering cytosol masks that are not in the lookup table in seconds", time.time() - new_time))

end = time.time()

timing_info.append(("14. Time required for filtering generated masks in seconds", end - start))

self.log(f"Time required for filtering generated masks in seconds: {end - start}")

df_timing = pd.DataFrame(timing_info, columns=["Step", "Time (s)"])

if self.debug:
# plot nucleus and cytosol masks before and after filtering
Expand Down

0 comments on commit 6b747df

Please sign in to comment.