fspi-analysis 0.2.0__tar.gz → 0.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {fspi_analysis-0.2.0/src/fspi_analysis.egg-info → fspi_analysis-0.2.2}/PKG-INFO +1 -1
- {fspi_analysis-0.2.0 → fspi_analysis-0.2.2}/src/fspi_analysis/__init__.py +1 -1
- {fspi_analysis-0.2.0 → fspi_analysis-0.2.2}/src/fspi_analysis/fspi_gui.py +147 -60
- {fspi_analysis-0.2.0 → fspi_analysis-0.2.2/src/fspi_analysis.egg-info}/PKG-INFO +1 -1
- {fspi_analysis-0.2.0 → fspi_analysis-0.2.2}/LICENSE +0 -0
- {fspi_analysis-0.2.0 → fspi_analysis-0.2.2}/MANIFEST.in +0 -0
- {fspi_analysis-0.2.0 → fspi_analysis-0.2.2}/README.md +0 -0
- {fspi_analysis-0.2.0 → fspi_analysis-0.2.2}/pyproject.toml +0 -0
- {fspi_analysis-0.2.0 → fspi_analysis-0.2.2}/setup.cfg +0 -0
- {fspi_analysis-0.2.0 → fspi_analysis-0.2.2}/src/fspi_analysis/_tests/__init__.py +0 -0
- {fspi_analysis-0.2.0 → fspi_analysis-0.2.2}/src/fspi_analysis/napari.yaml +0 -0
- {fspi_analysis-0.2.0 → fspi_analysis-0.2.2}/src/fspi_analysis.egg-info/SOURCES.txt +0 -0
- {fspi_analysis-0.2.0 → fspi_analysis-0.2.2}/src/fspi_analysis.egg-info/dependency_links.txt +0 -0
- {fspi_analysis-0.2.0 → fspi_analysis-0.2.2}/src/fspi_analysis.egg-info/entry_points.txt +0 -0
- {fspi_analysis-0.2.0 → fspi_analysis-0.2.2}/src/fspi_analysis.egg-info/requires.txt +0 -0
- {fspi_analysis-0.2.0 → fspi_analysis-0.2.2}/src/fspi_analysis.egg-info/top_level.txt +0 -0
|
@@ -11,6 +11,8 @@ import json
|
|
|
11
11
|
import tifffile
|
|
12
12
|
import dask.array as da
|
|
13
13
|
import matplotlib.pyplot as plt
|
|
14
|
+
import matplotlib.cm as cm
|
|
15
|
+
from matplotlib.colors import Normalize
|
|
14
16
|
from vispy.color import Colormap
|
|
15
17
|
from dask import delayed
|
|
16
18
|
|
|
@@ -33,8 +35,10 @@ class ImageAnalysisApp:
|
|
|
33
35
|
def setup_ui(self):
|
|
34
36
|
# UI Elements
|
|
35
37
|
self.status_label = Label(value="Welcome! Load an image/stack to start.")
|
|
36
|
-
|
|
38
|
+
|
|
39
|
+
# --- Mode Selectors ---
|
|
37
40
|
self.mode_selector = ComboBox(choices=["Single Image", "Image Stack", "Single Image (RGB)", "Image Stack (RGB)"], label="Mode:")
|
|
41
|
+
self.spi_mode_selector = ComboBox(choices=["fSPI", "tSPI"], label="SPI Mode:")
|
|
38
42
|
|
|
39
43
|
# Explicit Load Buttons
|
|
40
44
|
self.load_image_path = FileEdit(label="Choose File:", mode="r")
|
|
@@ -62,7 +66,7 @@ class ImageAnalysisApp:
|
|
|
62
66
|
# Threshold controls
|
|
63
67
|
self.threshold_value_widget = FloatSlider(value=0.5, min=0.0, max=1.0, step=0.005, label="Threshold:")
|
|
64
68
|
|
|
65
|
-
#
|
|
69
|
+
# RGB Threshold Sliders
|
|
66
70
|
self.threshold_R = FloatSlider(value=0.5, min=0.0, max=1.0, step=0.005, label="Threshold (Red):", visible=False)
|
|
67
71
|
self.threshold_G = FloatSlider(value=0.5, min=0.0, max=1.0, step=0.005, label="Threshold (Green):", visible=False)
|
|
68
72
|
self.threshold_B = FloatSlider(value=0.5, min=0.0, max=1.0, step=0.005, label="Threshold (Blue):", visible=False)
|
|
@@ -71,15 +75,15 @@ class ImageAnalysisApp:
|
|
|
71
75
|
self.apply_threshold_button = PushButton(text="Apply Threshold")
|
|
72
76
|
|
|
73
77
|
# Output controls
|
|
74
|
-
self.real_core_size_widget = FloatSpinBox(value=6, min=0.001, step=0.1, label="Real Core Size (cm):")
|
|
75
|
-
self.core_width_px_widget = FloatSpinBox(value=671, min=1.0, step=1, label="Core Width (px):")
|
|
78
|
+
self.real_core_size_widget = FloatSpinBox(value=6, min=0.001, max=100000, step=0.1, label="Real Core Size (cm):")
|
|
79
|
+
self.core_width_px_widget = FloatSpinBox(value=671, min=1.0, max=100000, step=1, label="Core Width (px):")
|
|
76
80
|
self.process_save_button = PushButton(text="Process & Save Results")
|
|
77
81
|
|
|
78
82
|
self.reset_button = PushButton(text="Reset for Next Image")
|
|
79
83
|
|
|
80
84
|
# Container Assembly
|
|
81
85
|
self.control_container = Container(widgets=[
|
|
82
|
-
self.mode_selector, self.load_image_path, self.load_image_btn,
|
|
86
|
+
self.mode_selector, self.spi_mode_selector, self.load_image_path, self.load_image_btn,
|
|
83
87
|
Label(value="--- Alignment & Contours ---"),
|
|
84
88
|
self.save_contour_button, self.align_button, self.clear_paths_button,
|
|
85
89
|
self.load_ann_path, self.load_ann_btn, self.save_ann_button,
|
|
@@ -113,7 +117,6 @@ class ImageAnalysisApp:
|
|
|
113
117
|
self.apply_threshold_button.clicked.connect(self.apply_manual_threshold)
|
|
114
118
|
self.threshold_value_widget.changed.connect(self._update_threshold_preview)
|
|
115
119
|
|
|
116
|
-
# --- NEW: Connect RGB sliders to preview ---
|
|
117
120
|
self.threshold_R.changed.connect(self._update_threshold_preview)
|
|
118
121
|
self.threshold_G.changed.connect(self._update_threshold_preview)
|
|
119
122
|
self.threshold_B.changed.connect(self._update_threshold_preview)
|
|
@@ -133,7 +136,6 @@ class ImageAnalysisApp:
|
|
|
133
136
|
self.load_ann_path.visible = is_stack
|
|
134
137
|
self.load_ann_btn.visible = is_stack
|
|
135
138
|
|
|
136
|
-
# --- NEW: Swap threshold sliders based on RGB mode ---
|
|
137
139
|
self.threshold_value_widget.visible = not is_rgb
|
|
138
140
|
self.threshold_R.visible = is_rgb
|
|
139
141
|
self.threshold_G.visible = is_rgb
|
|
@@ -283,7 +285,12 @@ class ImageAnalysisApp:
|
|
|
283
285
|
if 'aligned_image' in self.viewer.layers:
|
|
284
286
|
self.viewer.layers['aligned_image'].data = aligned_image
|
|
285
287
|
else:
|
|
286
|
-
|
|
288
|
+
# --- THE FIX: Safely retrieve colormap to prevent silent AttributeError ---
|
|
289
|
+
try:
|
|
290
|
+
c_name = image_layer.colormap.name
|
|
291
|
+
except AttributeError:
|
|
292
|
+
c_name = 'gray'
|
|
293
|
+
self.viewer.add_image(aligned_image, name='aligned_image', colormap=c_name)
|
|
287
294
|
|
|
288
295
|
self.single_path_y_diff = p_diff
|
|
289
296
|
self.single_path_min_y = p_min
|
|
@@ -465,7 +472,6 @@ class ImageAnalysisApp:
|
|
|
465
472
|
if slice_data.ndim < 3 or slice_data.shape[-1] < 3:
|
|
466
473
|
self.status_label.value = "ERROR: RGB mode selected, but image lacks 3 channels."
|
|
467
474
|
return
|
|
468
|
-
# Calculate Otsu for each channel independently
|
|
469
475
|
val_R = float(np.clip(filters.threshold_otsu(slice_data[..., 0]), 0.0, 1.0))
|
|
470
476
|
val_G = float(np.clip(filters.threshold_otsu(slice_data[..., 1]), 0.0, 1.0))
|
|
471
477
|
val_B = float(np.clip(filters.threshold_otsu(slice_data[..., 2]), 0.0, 1.0))
|
|
@@ -502,11 +508,9 @@ class ImageAnalysisApp:
|
|
|
502
508
|
is_rgb = "(RGB)" in self.mode
|
|
503
509
|
|
|
504
510
|
if is_rgb:
|
|
505
|
-
# We need the 3 channels
|
|
506
511
|
ch_dim = 3 if "Stack" in self.mode else 2
|
|
507
512
|
if data.ndim < ch_dim: return
|
|
508
513
|
|
|
509
|
-
# --- Grayscale Background for RGB Thresholding ---
|
|
510
514
|
if 'aligned_image_gray' not in self.viewer.layers:
|
|
511
515
|
if isinstance(data, da.Array):
|
|
512
516
|
gray_vis = data.mean(axis=-1)
|
|
@@ -523,7 +527,6 @@ class ImageAnalysisApp:
|
|
|
523
527
|
|
|
524
528
|
if 'aligned_image' in self.viewer.layers:
|
|
525
529
|
self.viewer.layers['aligned_image'].visible = False
|
|
526
|
-
# --------------------------------------------------------
|
|
527
530
|
|
|
528
531
|
r_data = data[..., 0] / 255.0 if data.dtype == np.uint8 else data[..., 0]
|
|
529
532
|
g_data = data[..., 1] / 255.0 if data.dtype == np.uint8 else data[..., 1]
|
|
@@ -533,10 +536,9 @@ class ImageAnalysisApp:
|
|
|
533
536
|
mask_g = (g_data > self.threshold_G.value).astype(np.uint8)
|
|
534
537
|
mask_b = (b_data > self.threshold_B.value).astype(np.uint8)
|
|
535
538
|
|
|
536
|
-
# Define vispy Colormaps: [Background (Transparent), Foreground (Color)]
|
|
537
539
|
cmap_r = Colormap([[0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 1.0]])
|
|
538
540
|
cmap_g = Colormap([[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 1.0]])
|
|
539
|
-
cmap_b = Colormap([[0.0, 0.0, 0.0, 0.0], [0.0, 0.5, 1.0, 1.0]])
|
|
541
|
+
cmap_b = Colormap([[0.0, 0.0, 0.0, 0.0], [0.0, 0.5, 1.0, 1.0]])
|
|
540
542
|
|
|
541
543
|
channels = [
|
|
542
544
|
('mask_red', mask_r, 'custom_red', cmap_r),
|
|
@@ -548,7 +550,6 @@ class ImageAnalysisApp:
|
|
|
548
550
|
try:
|
|
549
551
|
self.viewer.layers[name].data = mask
|
|
550
552
|
except KeyError:
|
|
551
|
-
# THE FIX: Use add_image with the custom vispy colormap
|
|
552
553
|
self.viewer.add_image(
|
|
553
554
|
mask,
|
|
554
555
|
name=name,
|
|
@@ -567,14 +568,11 @@ class ImageAnalysisApp:
|
|
|
567
568
|
gray_data = gray_data.mean(axis=-1)
|
|
568
569
|
|
|
569
570
|
mask_data = (gray_data > thresh).astype(np.uint8)
|
|
570
|
-
|
|
571
|
-
# Define vispy Colormap for Yellow
|
|
572
571
|
cmap_y = Colormap([[0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 0.0, 1.0]])
|
|
573
572
|
|
|
574
573
|
try:
|
|
575
574
|
self.viewer.layers['thresholded_mask'].data = mask_data
|
|
576
575
|
except KeyError:
|
|
577
|
-
# THE FIX: Use add_image with the custom vispy colormap
|
|
578
576
|
self.viewer.add_image(
|
|
579
577
|
mask_data,
|
|
580
578
|
name='thresholded_mask',
|
|
@@ -605,13 +603,19 @@ class ImageAnalysisApp:
|
|
|
605
603
|
|
|
606
604
|
self.status_label.value = "Processing and saving single image results..."
|
|
607
605
|
try:
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
606
|
+
# Scale calculation in CM
|
|
607
|
+
pixel_size_cm = self.real_core_size_widget.value / self.core_width_px_widget.value
|
|
608
|
+
|
|
609
|
+
# Dynamic Column Naming
|
|
610
|
+
spi_mode = self.spi_mode_selector.value
|
|
611
|
+
al_suffix = "_aL" if spi_mode == "tSPI" else ""
|
|
612
|
+
|
|
613
|
+
p_diff = getattr(self, 'single_path_y_diff', None)
|
|
614
|
+
path_diff_scaled = p_diff * pixel_size_cm if p_diff is not None else np.nan
|
|
611
615
|
|
|
612
616
|
pen_stats = getattr(self, 'single_pen_stats', None)
|
|
613
617
|
if pen_stats:
|
|
614
|
-
pen_min_sc, pen_max_sc, pen_med_sc = [p *
|
|
618
|
+
pen_min_sc, pen_max_sc, pen_med_sc = [p * pixel_size_cm for p in pen_stats]
|
|
615
619
|
else:
|
|
616
620
|
pen_min_sc = pen_max_sc = pen_med_sc = np.nan
|
|
617
621
|
|
|
@@ -619,6 +623,7 @@ class ImageAnalysisApp:
|
|
|
619
623
|
with open(txt_path, 'w') as f:
|
|
620
624
|
f.write("Image Analysis Summary\n")
|
|
621
625
|
f.write(f"Original File Stem: {self.last_loaded_stem}\n")
|
|
626
|
+
f.write(f"SPI Mode: {spi_mode}\n")
|
|
622
627
|
f.write(f"Processing Timestamp: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
|
|
623
628
|
f.write("-" * 30 + "\n")
|
|
624
629
|
f.write("Alignment Path Statistics:\n")
|
|
@@ -630,12 +635,12 @@ class ImageAnalysisApp:
|
|
|
630
635
|
f.write("Scaling Factors:\n")
|
|
631
636
|
f.write(f" Real Core Size Input: {self.real_core_size_widget.value:.3f} cm\n")
|
|
632
637
|
f.write(f" Core Width Input: {self.core_width_px_widget.value:.1f} px\n")
|
|
633
|
-
f.write(f" Pixel Size Calculated: {
|
|
638
|
+
f.write(f" Pixel Size Calculated: {pixel_size_cm:.6f} cm/px\n")
|
|
634
639
|
f.write("-" * 30 + "\n")
|
|
635
640
|
f.write(f"Total Penetration Depth (Surface to Image Bottom):\n")
|
|
636
|
-
f.write(f" Min Penetration: {pen_min_sc:.
|
|
637
|
-
f.write(f" Max Penetration: {pen_max_sc:.
|
|
638
|
-
f.write(f" Median Penetration: {pen_med_sc:.
|
|
641
|
+
f.write(f" Min Penetration: {pen_min_sc:.4f} cm\n")
|
|
642
|
+
f.write(f" Max Penetration: {pen_max_sc:.4f} cm\n")
|
|
643
|
+
f.write(f" Median Penetration: {pen_med_sc:.4f} cm\n")
|
|
639
644
|
f.write("=" * 30 + "\n\n")
|
|
640
645
|
|
|
641
646
|
for ch_name, layer_name in channels:
|
|
@@ -654,6 +659,9 @@ class ImageAnalysisApp:
|
|
|
654
659
|
total_sum = np.sum(mask_data)
|
|
655
660
|
rel_counts = (row_sums_full / total_sum) * 100 if total_sum > 0 else np.zeros_like(row_sums_full, dtype=float)
|
|
656
661
|
|
|
662
|
+
# Area in cm2
|
|
663
|
+
thresholded_area_cm2 = total_sum * (pixel_size_cm ** 2)
|
|
664
|
+
|
|
657
665
|
eff_H = min(int(self.confirmed_y_depth), H_full - 1) + 1 if self.confirmed_y_depth is not None else H_full
|
|
658
666
|
mask_roi = mask_data[:eff_H, :]
|
|
659
667
|
|
|
@@ -670,53 +678,64 @@ class ImageAnalysisApp:
|
|
|
670
678
|
median_dist = np.median(v_dists) if v_dists else np.nan
|
|
671
679
|
max_dist = np.max(v_dists) if v_dists else np.nan
|
|
672
680
|
|
|
673
|
-
mean_dist_scaled = mean_dist *
|
|
674
|
-
min_dist_scaled = min_dist *
|
|
675
|
-
median_dist_scaled = median_dist *
|
|
676
|
-
max_dist_scaled = max_dist *
|
|
681
|
+
mean_dist_scaled = mean_dist * pixel_size_cm if not np.isnan(mean_dist) else np.nan
|
|
682
|
+
min_dist_scaled = min_dist * pixel_size_cm if not np.isnan(min_dist) else np.nan
|
|
683
|
+
median_dist_scaled = median_dist * pixel_size_cm if not np.isnan(median_dist) else np.nan
|
|
684
|
+
max_dist_scaled = max_dist * pixel_size_cm if not np.isnan(max_dist) else np.nan
|
|
677
685
|
|
|
678
|
-
# 3. Save Profile CSV
|
|
686
|
+
# 3. Save Profile CSV (Dynamic Names & Area)
|
|
679
687
|
df = pd.DataFrame({
|
|
688
|
+
'SPI_Mode': spi_mode,
|
|
680
689
|
'Row': depths_full,
|
|
681
690
|
'Row_Sums': row_sums_full,
|
|
682
691
|
'Row_Sum_Relative_Count': rel_counts,
|
|
683
|
-
'
|
|
684
|
-
'
|
|
685
|
-
'
|
|
686
|
-
'
|
|
687
|
-
'
|
|
688
|
-
'
|
|
689
|
-
'
|
|
690
|
-
'
|
|
691
|
-
'
|
|
692
|
+
'Thresholded_Area_cm2': thresholded_area_cm2,
|
|
693
|
+
'Surface_Path_Difference_cm_SBR': path_diff_scaled,
|
|
694
|
+
f'Min_Depth_Distance_cm{al_suffix}': min_dist_scaled,
|
|
695
|
+
f'Mean_Depth_Distance_cm{al_suffix}': mean_dist_scaled,
|
|
696
|
+
f'Median_Depth_Distance_cm{al_suffix}': median_dist_scaled,
|
|
697
|
+
f'Max_Depth_Distance_cm{al_suffix}': max_dist_scaled,
|
|
698
|
+
'Total_Penetration_Min_cm': pen_min_sc,
|
|
699
|
+
'Total_Penetration_Max_cm': pen_max_sc,
|
|
700
|
+
'Total_Penetration_Median_cm': pen_med_sc,
|
|
701
|
+
'cm_per_Pixel': pixel_size_cm
|
|
692
702
|
})
|
|
693
703
|
csv_path = self.last_loaded_directory / f"{self.last_loaded_stem}_Profile{suffix}.csv"
|
|
694
|
-
df.to_csv(csv_path, index=False, float_format='%.
|
|
704
|
+
df.to_csv(csv_path, index=False, float_format='%.6f')
|
|
695
705
|
|
|
696
706
|
# 4. Append to TXT
|
|
697
707
|
with open(txt_path, 'a') as f:
|
|
698
708
|
depth_str = f"Up to Y={eff_H-1} (inclusive)" if self.confirmed_y_depth is not None else "Full Image"
|
|
699
709
|
f.write(f"--- Channel: {ch_name} ---\n")
|
|
710
|
+
f.write(f" Thresholded Area: {thresholded_area_cm2:.6f} cm^2\n")
|
|
700
711
|
f.write(f"ROI Vertical Distances ({depth_str}):\n")
|
|
701
|
-
f.write(f" Mean Vertical Distance: {mean_dist_scaled:.
|
|
702
|
-
f.write(f" Median Vertical Distance: {median_dist_scaled:.
|
|
703
|
-
f.write(f" Max Vertical Distance: {max_dist_scaled:.
|
|
712
|
+
f.write(f" Mean Vertical Distance: {mean_dist_scaled:.4f} cm\n")
|
|
713
|
+
f.write(f" Median Vertical Distance: {median_dist_scaled:.4f} cm\n")
|
|
714
|
+
f.write(f" Max Vertical Distance: {max_dist_scaled:.4f} cm\n\n")
|
|
704
715
|
|
|
705
716
|
# 5. Generate Plot
|
|
706
|
-
plot_rows = depths_full
|
|
707
|
-
plot_sums = row_sums_full
|
|
717
|
+
plot_rows = depths_full
|
|
718
|
+
plot_sums = row_sums_full
|
|
708
719
|
color_map = {'Red': 'tomato', 'Green': 'mediumseagreen', 'Blue': 'cornflowerblue', 'Grayscale': 'dodgerblue'}
|
|
709
720
|
|
|
710
721
|
plt.figure(figsize=(8, 5))
|
|
711
722
|
plt.plot(plot_rows, plot_sums, color=color_map[ch_name], linewidth=2)
|
|
723
|
+
|
|
724
|
+
if self.confirmed_y_depth is not None:
|
|
725
|
+
plt.axvline(x=self.confirmed_y_depth, color='gray', linestyle='--', label='ROI Boundary')
|
|
726
|
+
plt.legend()
|
|
727
|
+
|
|
712
728
|
plt.title(f"Row Sums Profile: {self.last_loaded_stem} ({ch_name})", fontsize=14, pad=10)
|
|
713
729
|
plt.xlabel("Row (Depth in Pixels)", fontsize=12)
|
|
714
730
|
plt.ylabel("Row Sum", fontsize=12)
|
|
715
731
|
plt.grid(True, linestyle='--', alpha=0.6)
|
|
716
732
|
plt.tight_layout()
|
|
717
733
|
|
|
718
|
-
|
|
719
|
-
|
|
734
|
+
# --- NEW: Save as both PNG and SVG ---
|
|
735
|
+
plot_path_png = self.last_loaded_directory / f"{self.last_loaded_stem}_ProfilePlot{suffix}.png"
|
|
736
|
+
plot_path_svg = self.last_loaded_directory / f"{self.last_loaded_stem}_ProfilePlot{suffix}.svg"
|
|
737
|
+
plt.savefig(plot_path_png, dpi=300)
|
|
738
|
+
plt.savefig(plot_path_svg, format='svg') # Vector format!
|
|
720
739
|
plt.close()
|
|
721
740
|
|
|
722
741
|
self.status_label.value = f"Processed & saved to {self.last_loaded_directory.name}."
|
|
@@ -731,6 +750,19 @@ class ImageAnalysisApp:
|
|
|
731
750
|
all_results = []
|
|
732
751
|
is_rgb = "(RGB)" in self.mode
|
|
733
752
|
|
|
753
|
+
# Determine Mode and Suffix for dynamic column naming
|
|
754
|
+
spi_mode = self.spi_mode_selector.value
|
|
755
|
+
al_suffix = "_aL" if spi_mode == "tSPI" else ""
|
|
756
|
+
|
|
757
|
+
pixel_size_cm = self.real_core_size_widget.value / self.core_width_px_widget.value
|
|
758
|
+
|
|
759
|
+
# Setup Folder for RowSums CSVs
|
|
760
|
+
row_sums_dir = self.last_loaded_directory / f"{self.last_loaded_stem}_RowSums"
|
|
761
|
+
row_sums_dir.mkdir(exist_ok=True)
|
|
762
|
+
|
|
763
|
+
# Dictionary to store row sums data for the viridis plot
|
|
764
|
+
stack_plot_data = {}
|
|
765
|
+
|
|
734
766
|
for z in range(total_z):
|
|
735
767
|
img_2d = stack_data[z]
|
|
736
768
|
path_2d = self.get_path_for_z(z)
|
|
@@ -741,7 +773,6 @@ class ImageAnalysisApp:
|
|
|
741
773
|
if aligned_2d is None: continue
|
|
742
774
|
|
|
743
775
|
pen_min, pen_max, pen_med = pen_stats
|
|
744
|
-
pixel_size = (self.real_core_size_widget.value * 1000) / self.core_width_px_widget.value
|
|
745
776
|
|
|
746
777
|
# Prepare channels to process
|
|
747
778
|
if is_rgb:
|
|
@@ -763,10 +794,27 @@ class ImageAnalysisApp:
|
|
|
763
794
|
|
|
764
795
|
suffix = f"_{ch_name}" if is_rgb else ""
|
|
765
796
|
stem_z = f"{self.last_loaded_stem}_Z{z:03d}{suffix}"
|
|
797
|
+
|
|
798
|
+
# Save Individual Mask
|
|
766
799
|
mask_path = self.last_loaded_directory / f"{stem_z}_Mask.png"
|
|
767
800
|
io.imsave(mask_path, (mask_2d * 255).astype(np.uint8), check_contrast=False)
|
|
768
801
|
|
|
769
802
|
H, W = mask_2d.shape
|
|
803
|
+
|
|
804
|
+
# Calculate RowSums & Area
|
|
805
|
+
row_sums_full = np.sum(mask_2d, axis=1)
|
|
806
|
+
depths_full = np.arange(H)
|
|
807
|
+
total_sum = np.sum(mask_2d)
|
|
808
|
+
thresholded_area_cm2 = total_sum * (pixel_size_cm ** 2)
|
|
809
|
+
|
|
810
|
+
# Save Individual RowSums CSV in the subfolder
|
|
811
|
+
df_rs = pd.DataFrame({'Row': depths_full, 'Row_Sums': row_sums_full})
|
|
812
|
+
df_rs.to_csv(row_sums_dir / f"{stem_z}_RowSums.csv", index=False)
|
|
813
|
+
|
|
814
|
+
# Store data for the Stack Plot
|
|
815
|
+
stack_plot_data.setdefault(ch_name, []).append((z, row_sums_full))
|
|
816
|
+
|
|
817
|
+
# Depth Calculations
|
|
770
818
|
clamped_depth = min(int(self.confirmed_y_depth) if self.confirmed_y_depth else H-1, H-1)
|
|
771
819
|
eff_H = clamped_depth + 1
|
|
772
820
|
mask_roi = mask_2d[:eff_H, :]
|
|
@@ -785,26 +833,65 @@ class ImageAnalysisApp:
|
|
|
785
833
|
|
|
786
834
|
all_results.append({
|
|
787
835
|
'Z_Slice': z,
|
|
836
|
+
'SPI_Mode': spi_mode,
|
|
788
837
|
'Channel': ch_name,
|
|
789
|
-
'
|
|
790
|
-
'
|
|
791
|
-
'
|
|
792
|
-
'
|
|
793
|
-
'
|
|
794
|
-
'
|
|
795
|
-
'
|
|
796
|
-
'
|
|
797
|
-
'
|
|
838
|
+
'Thresholded_Area_cm2': thresholded_area_cm2,
|
|
839
|
+
'Surface_Path_Diff_cm_SBR': p_diff * pixel_size_cm,
|
|
840
|
+
f'Min_Vertical_Dist_cm{al_suffix}': min_dist * pixel_size_cm,
|
|
841
|
+
f'Max_Vertical_Dist_cm{al_suffix}': max_dist * pixel_size_cm,
|
|
842
|
+
f'Mean_Vertical_Dist_cm{al_suffix}': mean_dist * pixel_size_cm,
|
|
843
|
+
f'Median_Vertical_Dist_cm{al_suffix}': median_dist * pixel_size_cm,
|
|
844
|
+
'Total_Penetration_Depth_Min_cm': pen_min * pixel_size_cm,
|
|
845
|
+
'Total_Penetration_Depth_Max_cm': pen_max * pixel_size_cm,
|
|
846
|
+
'Total_Penetration_Depth_Median_cm': pen_med * pixel_size_cm,
|
|
847
|
+
'cm_per_Pixel': pixel_size_cm
|
|
798
848
|
})
|
|
799
849
|
|
|
800
850
|
if all_results:
|
|
851
|
+
# Output Master CSV
|
|
801
852
|
df = pd.DataFrame(all_results)
|
|
802
853
|
out_csv = self.last_loaded_directory / f"{self.last_loaded_stem}_Stack_Summary.csv"
|
|
803
|
-
df.to_csv(out_csv, index=False)
|
|
854
|
+
df.to_csv(out_csv, index=False, float_format='%.6f')
|
|
855
|
+
|
|
856
|
+
# Generate Stack Plot (Colored by slice number using Viridis)
|
|
857
|
+
for ch_name, data_list in stack_plot_data.items():
|
|
858
|
+
plt.figure(figsize=(10, 6))
|
|
859
|
+
cmap = plt.get_cmap('viridis')
|
|
860
|
+
|
|
861
|
+
# Handle color normalization safely
|
|
862
|
+
z_vals = [d[0] for d in data_list]
|
|
863
|
+
z_min, z_max = min(z_vals), max(z_vals)
|
|
864
|
+
if z_max == z_min: z_max = z_min + 1 # Prevent ZeroDivision
|
|
865
|
+
|
|
866
|
+
for z, row_sums in data_list:
|
|
867
|
+
# --- NEW: Lowered alpha to 0.3 for a softer, better overlapping visual ---
|
|
868
|
+
plt.plot(row_sums, color=cmap((z - z_min) / (z_max - z_min)), alpha=0.3)
|
|
869
|
+
|
|
870
|
+
plt.title(f"Stack Row Sums: {self.last_loaded_stem} ({ch_name})", fontsize=14, pad=10)
|
|
871
|
+
plt.xlabel("Row (Depth in Pixels)", fontsize=12)
|
|
872
|
+
plt.ylabel("Row Sum", fontsize=12)
|
|
873
|
+
plt.grid(True, linestyle='--', alpha=0.4)
|
|
874
|
+
|
|
875
|
+
# Add Colorbar
|
|
876
|
+
sm = plt.cm.ScalarMappable(cmap=cmap, norm=Normalize(vmin=z_min, vmax=z_max))
|
|
877
|
+
sm.set_array([])
|
|
878
|
+
plt.colorbar(sm, ax=plt.gca(), label='Image Number (Z-Slice)')
|
|
879
|
+
|
|
880
|
+
plt.tight_layout()
|
|
881
|
+
|
|
882
|
+
suffix = f"_{ch_name}" if is_rgb else ""
|
|
883
|
+
|
|
884
|
+
# --- NEW: Save as both PNG and SVG ---
|
|
885
|
+
plot_path_png = self.last_loaded_directory / f"{self.last_loaded_stem}_StackPlot{suffix}.png"
|
|
886
|
+
plot_path_svg = self.last_loaded_directory / f"{self.last_loaded_stem}_StackPlot{suffix}.svg"
|
|
887
|
+
|
|
888
|
+
plt.savefig(plot_path_png, dpi=300)
|
|
889
|
+
plt.savefig(plot_path_svg, format='svg') # Vector format!
|
|
890
|
+
plt.close()
|
|
891
|
+
|
|
804
892
|
self.status_label.value = f"Stack processed. Saved to {out_csv.name}."
|
|
805
893
|
|
|
806
894
|
def reset_viewer_state(self):
|
|
807
|
-
# Added 'aligned_image_gray' to the cleanup list
|
|
808
895
|
layers_to_remove = ['aligned_image', 'aligned_image_gray', 'thresholded_mask', 'mask_red', 'mask_green', 'mask_blue', 'depth_selection_line', 'alignment_path', 'original_image']
|
|
809
896
|
for layer in layers_to_remove:
|
|
810
897
|
if layer in self.viewer.layers:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|