Skip to content

Commit a293f68

Browse files
committed
feat: migrate unet to torch, fix segservice
1 parent 052568b commit a293f68

9 files changed

Lines changed: 1238 additions & 1381 deletions

File tree

pyproject.toml

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
11
[project]
22
name = "partaker"
33
version = "0.1.0"
4-
description = ""
4+
description = "Bacterial fluorescence analysis Tool for fluorescence microscopy"
55
authors = [{ name = "Henrique Hiram" }]
66
requires-python = ">=3.11,<3.12"
77
readme = "README.md"
88
license = { text = "LGPL" }
99
dependencies = [
1010
"pyside6 (==6.7.2)",
11-
# --- >>> PLATFORM-SPECIFIC TENSORFLOW (THIS BLOCK IS THE FIX) <<<
12-
# Windows, Linux: regular tensorflow
13-
"tensorflow (>=2.13.0,<2.16.0) ; platform_system != 'Darwin' and platform_system != 'Windows'",
14-
# Mac ARM (Apple Silicon)
15-
"tensorflow-macos (==2.15) ; platform_system == 'Darwin' and platform_machine == 'arm64'",
16-
# Mac Intel (if you ever use Intel Mac)
17-
"tensorflow-macos (==2.15) ; platform_system == 'Darwin' and platform_machine != 'arm64'",
18-
# Windows: Intel/AMD optimized
19-
"tensorflow-intel (>=2.13.0,<2.16.0) ; platform_system == 'Windows'",
20-
# Tensorflow IO GCS filesystem for non-Mac-ARM
21-
"tensorflow-io-gcs-filesystem (>=0.23.1) ; platform_machine != 'arm64' or platform_system != 'Darwin'",
22-
# (Old) Windows: keep compatibility version
23-
"tensorflow-io-gcs-filesystem (<0.32.0) ; platform_system == 'Windows'",
24-
# --- >>> END OF PLATFORM-SPECIFIC BLOCK <<<
2511
"polars (>=1.30.0,<2.0.0)",
2612
"nd2 (>=0.10.3,<0.11.0)",
2713
"opencv-python (>=4.11.0.86,<5.0.0.0)",
@@ -63,3 +49,25 @@ dev = [
6349

6450
[tool.hatch.build.targets.wheel]
6551
packages = ["src/nd2_analyzer"]
52+
53+
[tool.ruff]
54+
target-version = "py312"
55+
56+
[tool.ruff.lint]
57+
select = [
58+
"E", # pycodestyle errors
59+
"F", # pyflakes (undefined names, unused imports, etc.)
60+
"W", # pycodestyle warnings
61+
"B", # bugbear (common bugs and design issues)
62+
"I", # isort (import sorting)
63+
"UP", # pyupgrade (modernize syntax)
64+
"RUF", # ruff-specific rules
65+
"N", # pep8-naming
66+
"ASYNC", # async pitfalls
67+
]
68+
ignore = ["E501"] # line too long (ruff format handles this)
69+
70+
[tool.ruff.format]
71+
quote-style = "double"
72+
indent-style = "space"
73+

src/nd2_analyzer/analysis/metrics_service.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,13 @@ def __init__(self):
4646
self._batch_size = 1000
4747
self._pending_count = 0
4848

49-
pub.subscribe(self.compute_metrics_at_frame, "image_ready")
49+
pub.subscribe(self.compute_metrics_at_frame, "frame_segmented")
5050
self._initialized = True
5151

5252
@timing_decorator("compute_metrics_at_frame")
5353
def compute_metrics_at_frame(
54-
self, image: np.ndarray, time, position, channel, mode
54+
self, labeled_frame: np.ndarray, time, position, channel
5555
):
56-
if mode != "segmented":
57-
return
58-
59-
# labeled_frame = segmentation_cache[time, position, 0] # TODO: ensure segmentationservice always returns labeled
60-
labeled_frame = image
61-
6256
chan_n = ImageData.get_instance().channel_n
6357
mcherry_frame = yfp_frame = None
6458
if chan_n == 3:
@@ -112,18 +106,19 @@ def update_frame_metrics(self, batch_data: list):
112106
"""
113107
Computes the metrics for each labeled cell from the segmentation
114108
# Uses regionprops to get geometrical features
115-
116109
# For fluorescence analysis:
117110
# Takes each segmented cell
118111
# 1. calculate physical metrics
119112
# 2. identify which fluorescence channel it is from
120113
# 3. write the actual fluorescence value
121114
"""
122115

123-
def calculate_cell_metrics(_frame: TLFrame):
116+
@classmethod
117+
def calculate_cell_metrics(cls, _frame: TLFrame):
124118
cells = regionprops(_frame.labeled_phc)
125119
batch_data = []
126120

121+
print(f"shape phc {_frame.labeled_phc.shape} mcherry {_frame.mcherry.shape}")
127122
# Check for the fluorescence in any channel, if not, -1 and 0 fluorescence
128123
# mcherry can be None, same for yfp
129124
back_fluo_mcherry = (
@@ -141,6 +136,7 @@ def calculate_cell_metrics(_frame: TLFrame):
141136
fluorescence_level = 0.0
142137
has_fluorescence = False
143138

139+
logging.info(f"[calculate_cell_metrics] has {len(cells)} cells")
144140
for cell in cells:
145141
cell_id = cell.label
146142

@@ -177,15 +173,21 @@ def calculate_cell_metrics(_frame: TLFrame):
177173
if has_fluorescence:
178174
# If only mcherry has fluo
179175
if back_fluo_mcherry != -1 and back_fluo_yfp == -1:
180-
mcherry_fluo = round(_frame.mcherry[_frame.labeled_phc == cell_id].mean(), 4)
176+
mcherry_fluo = round(
177+
_frame.mcherry[_frame.labeled_phc == cell_id].mean(), 4
178+
)
181179
fluorescence_channel = 1
182180
fluorescence_level = mcherry_fluo
183181

184182
# If both have fluorescence, compare them
185183
elif back_fluo_mcherry != -1 and back_fluo_yfp != -1:
186184
# Select the region corresponding to the cell in the frames
187-
mcherry_fluo = round(_frame.mcherry[_frame.labeled_phc == cell_id].mean(), 4)
188-
yfp_fluo = round(_frame.yfp[_frame.labeled_phc == cell_id].mean(), 4)
185+
mcherry_fluo = round(
186+
_frame.mcherry[_frame.labeled_phc == cell_id].mean(), 4
187+
)
188+
yfp_fluo = round(
189+
_frame.yfp[_frame.labeled_phc == cell_id].mean(), 4
190+
)
189191
if back_fluo_mcherry != -1 and back_fluo_yfp != -1:
190192
if (mcherry_fluo / back_fluo_mcherry) > (
191193
yfp_fluo / back_fluo_yfp

0 commit comments

Comments
 (0)