Skip to content

Commit fb08b03

Browse files
author
Corentin
committed
commenting the code
1 parent 51a21a3 commit fb08b03

File tree

8 files changed

+196
-23
lines changed

8 files changed

+196
-23
lines changed

myoquant/__main__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import typer
22
from rich.console import Console
33

4+
from .commands.docs import app as docs_app
5+
from .commands import run_sdh, run_he
6+
47
console = Console()
58

69
app = typer.Typer(
@@ -9,12 +12,8 @@
912
help="MyoQuant Analysis Command Line Interface",
1013
pretty_exceptions_show_locals=False,
1114
)
12-
13-
from .commands.docs import app as docs_app
14-
1515
app.add_typer(docs_app, name="docs", help="Generate documentation")
1616

17-
from .commands import run_sdh, run_he
1817

1918
app.registered_commands += (
2019
run_sdh.app.registered_commands + run_he.app.registered_commands

myoquant/commands/docs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""
2+
CLI Command that automatically generates documentation for the CLI commands
3+
including options and args values.
4+
Source: https://github.com/tiangolo/typer-cli/pull/67#issuecomment-1271983950
5+
"""
6+
17
from pathlib import Path
28
from typing import cast
39

myoquant/commands/run_he.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"""
2+
Module that contains the main function to run the nuclei analysis for HE and fluo images
3+
"""
14
import time
25
from pathlib import Path
36
import typer
@@ -90,11 +93,17 @@ def he_analysis(
9093
resolve_path=True,
9194
),
9295
):
93-
"""Run the HE analysis and quantification on the image."""
96+
"""Run the nuclei position analysis on HE and fluo images.
97+
First input arguments and option are printed in stdout and all modules are imported. Then the input image is mask with the binary mask if provided.
98+
Then depending on the presence of cellpose and stardist path, Cellpose and Stardist are run or not and mask accordingly if binary mask is provided.
99+
Finally the nuclei analysis is run with run_he_analysis() function and the results are saved in the output folder and some info are printed in stdout.
100+
"""
94101
start_time = time.time()
95102
console.print(
96-
f"👋 [bold dark_orange]Welcome to the nuclei position analysis (HE and fluo images)",
103+
"👋 [bold dark_orange]Welcome to the nuclei position analysis (HE and fluo images)",
97104
)
105+
106+
# Print input arguments and options
98107
console.print(f"📄 INPUT: raw image: {image_path}", style="blue")
99108

100109
if fluo_nuc is not None:
@@ -118,6 +127,7 @@ def he_analysis(
118127
if mask_path is not None:
119128
console.print(f"📄 INPUT: binary mask: {mask_path}", style="blue")
120129

130+
# Import all modules
121131
with Progress(
122132
SpinnerColumn(),
123133
TextColumn("[progress.description]{task.description}"),
@@ -141,7 +151,7 @@ def he_analysis(
141151

142152
try:
143153
from imageio.v2 import imread
144-
except:
154+
except ImportError:
145155
from imageio import imread
146156

147157
if output_path is None:
@@ -150,10 +160,11 @@ def he_analysis(
150160
Path(output_path).mkdir(parents=True, exist_ok=True)
151161

152162
if is_gpu_availiable():
153-
console.print(f"💡 INFO: GPU is available.", style="blue")
163+
console.print("💡 INFO: GPU is available.", style="blue")
154164
else:
155-
console.print(f"❌ INFO: GPU is not available. Using CPU only.", style="red")
165+
console.print("❌ INFO: GPU is not available. Using CPU only.", style="red")
156166

167+
# Load raw image, binary mask, cellpose and stardist mask if provided.
157168
with Progress(
158169
SpinnerColumn(),
159170
TextColumn("[progress.description]{task.description}"),
@@ -190,6 +201,7 @@ def he_analysis(
190201
if stardist_path is not None:
191202
mask_stardist = imread(stardist_path)
192203

204+
# Run Cellpose if no mask provided
193205
if cellpose_path is None:
194206
with Progress(
195207
SpinnerColumn(),
@@ -209,6 +221,8 @@ def he_analysis(
209221
f"💾 OUTPUT: CellPose mask saved as {output_path/cellpose_mask_filename}",
210222
style="green",
211223
)
224+
225+
# Run Stardist if no mask provided
212226
if stardist_path is None:
213227
with Progress(
214228
SpinnerColumn(),
@@ -240,6 +254,7 @@ def he_analysis(
240254
style="green",
241255
)
242256

257+
# If binary mask provided, mask cellpose and stardist mask
243258
if mask_path is not None:
244259
with Progress(
245260
SpinnerColumn(),
@@ -255,6 +270,7 @@ def he_analysis(
255270
mask_stardist = mask_stardist * mask_ndarray
256271
mask_cellpose = mask_cellpose * mask_ndarray
257272

273+
# Run the nuclei position analysis and get the results table, label map and dataframes
258274
with Progress(
259275
SpinnerColumn(),
260276
TextColumn("[progress.description]{task.description}"),
@@ -288,6 +304,7 @@ def he_analysis(
288304
overlay_filename = image_path.stem + "_label_blend.tiff"
289305
overlay_img.save(output_path / overlay_filename)
290306

307+
# Construct the summary table, print all output in stdout and save files in output folder.
291308
table.add_column("Feature", justify="left", style="cyan")
292309
table.add_column("Raw Count", justify="center", style="magenta")
293310
table.add_column("Proportion (%)", justify="right", style="green")

myoquant/commands/run_sdh.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"""
2+
Module that contains the main function to run the mitochondrial distribution analysis for SDH images
3+
"""
14
import time
25
from pathlib import Path
36
import typer
@@ -66,11 +69,17 @@ def sdh_analysis(
6669
),
6770
export_stats: bool = typer.Option(True, help="Export per fiber stat table."),
6871
):
69-
"""Run the SDH analysis and quantification on the image."""
72+
"""Run the mitochondiral analysis and quantification on the image.
73+
First input arguments and option are printed in stdout and all modules are imported and latest SDH model is downloaded.
74+
Then the input image is mask with the binary mask if provided.
75+
Then depending on the presence of cellpose path, Cellpose is run or not and mask accordingly if binary mask is provided.
76+
Finally the mitochondiral classificaiton is run with run_sdh_analysis() function and the results are saved in the output folder and some info are printed in stdout.
77+
"""
7078
start_time = time.time()
7179
console.print(
72-
f"👋 [bold dark_orange]Welcome to the mitochondrial distribution analysis (SDH images)",
80+
"👋 [bold dark_orange]Welcome to the mitochondrial distribution analysis (SDH images)",
7381
)
82+
# Print input arguments and options
7483
console.print(f"📄 INPUT: raw image: {image_path}", style="blue")
7584

7685
if cellpose_path is None:
@@ -85,6 +94,7 @@ def sdh_analysis(
8594
console.print(f"📄 INPUT: binary mask: {mask_path}", style="blue")
8695
import os
8796

97+
# If the model path is not provided, download latest version or check existence.
8898
if model_path is None:
8999
console.print(
90100
"💡 INFO: No SDH model provided, will download or use latest one.",
@@ -106,6 +116,7 @@ def sdh_analysis(
106116
model_path = model_path_abs
107117
console.print(f"📄 INPUT: SDH Model: {model_path}", style="blue")
108118

119+
# Import all modules
109120
with Progress(
110121
SpinnerColumn(),
111122
TextColumn("[progress.description]{task.description}"),
@@ -129,7 +140,7 @@ def sdh_analysis(
129140

130141
try:
131142
from imageio.v2 import imread
132-
except:
143+
except ImportError:
133144
from imageio import imread
134145

135146
if output_path is None:
@@ -138,10 +149,11 @@ def sdh_analysis(
138149
Path(output_path).mkdir(parents=True, exist_ok=True)
139150

140151
if is_gpu_availiable():
141-
console.print(f"💡 INFO: GPU is available.", style="blue")
152+
console.print("💡 INFO: GPU is available.", style="blue")
142153
else:
143-
console.print(f"❌ INFO: GPU is not available. Using CPU only.", style="red")
154+
console.print("❌ INFO: GPU is not available. Using CPU only.", style="red")
144155

156+
# Load raw image, binary mask, cellpose and stardist mask if provided.
145157
with Progress(
146158
SpinnerColumn(),
147159
TextColumn("[progress.description]{task.description}"),
@@ -169,6 +181,7 @@ def sdh_analysis(
169181
if cellpose_path is not None:
170182
mask_cellpose = imread(cellpose_path)
171183

184+
# Run Cellpose if no mask provided
172185
if cellpose_path is None:
173186
with Progress(
174187
SpinnerColumn(),
@@ -189,6 +202,7 @@ def sdh_analysis(
189202
style="green",
190203
)
191204

205+
# Load Tensorflow SDH Model
192206
with Progress(
193207
SpinnerColumn(),
194208
TextColumn("[progress.description]{task.description}"),
@@ -202,6 +216,7 @@ def sdh_analysis(
202216
with HiddenPrints():
203217
model_SDH = load_sdh_model(model_path)
204218

219+
# If binary mask provided, mask cellpose mask
205220
if mask_path is not None:
206221
with Progress(
207222
SpinnerColumn(),
@@ -216,6 +231,7 @@ def sdh_analysis(
216231
mask_ndarray = imread(mask_path)
217232
mask_cellpose = mask_cellpose * mask_ndarray
218233

234+
# Run the mitoC distribution analysis and get the results table, label map and dataframes
219235
with Progress(
220236
SpinnerColumn(),
221237
TextColumn("[progress.description]{task.description}"),
@@ -244,6 +260,7 @@ def sdh_analysis(
244260
overlay_filename = image_path.stem + "_label_blend.tiff"
245261
overlay_img.save(output_path / overlay_filename)
246262

263+
# Construct the summary table, print all output in stdout and save files in output folder.
247264
table.add_column("Feature", justify="left", style="cyan")
248265
table.add_column("Raw Count", justify="center", style="magenta")
249266
table.add_column("Proportion (%)", justify="right", style="green")

myoquant/src/SDH_analysis.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
from rich.progress import track
88
from skimage.measure import regionprops_table
99

10-
from .gradcam import get_img_array, make_gradcam_heatmap, save_and_display_gradcam
11-
from .random_brightness import *
10+
from .gradcam import make_gradcam_heatmap, save_and_display_gradcam
1211
import numpy as np
1312

1413
labels_predict = ["control", "sick"]

0 commit comments

Comments
 (0)