napari-tmidas 0.1.8__tar.gz → 0.1.8.5__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.
Files changed (45) hide show
  1. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/PKG-INFO +33 -9
  2. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/README.md +23 -7
  3. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/pyproject.toml +9 -1
  4. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/_crop_anything.py +137 -5
  5. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/_file_conversion.py +40 -18
  6. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/_file_selector.py +120 -13
  7. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/_version.py +2 -2
  8. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/processing_functions/basic.py +104 -0
  9. napari_tmidas-0.1.8.5/src/napari_tmidas/processing_functions/cellpose_env_manager.py +172 -0
  10. napari_tmidas-0.1.8.5/src/napari_tmidas/processing_functions/cellpose_segmentation.py +511 -0
  11. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/processing_functions/colocalization.py +17 -19
  12. napari_tmidas-0.1.8.5/src/napari_tmidas/processing_functions/file_compression.py +205 -0
  13. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/processing_functions/skimage_filters.py +25 -6
  14. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas.egg-info/PKG-INFO +33 -9
  15. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas.egg-info/SOURCES.txt +3 -0
  16. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas.egg-info/requires.txt +8 -0
  17. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/.github/dependabot.yml +0 -0
  18. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/.github/workflows/test_and_deploy.yml +0 -0
  19. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/.gitignore +0 -0
  20. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/.napari-hub/DESCRIPTION.md +0 -0
  21. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/.napari-hub/config.yml +0 -0
  22. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/.pre-commit-config.yaml +0 -0
  23. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/LICENSE +0 -0
  24. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/MANIFEST.in +0 -0
  25. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/setup.cfg +0 -0
  26. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/__init__.py +0 -0
  27. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/_label_inspection.py +0 -0
  28. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/_reader.py +0 -0
  29. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/_registry.py +0 -0
  30. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/_roi_colocalization.py +0 -0
  31. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/_sample_data.py +0 -0
  32. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/_tests/__init__.py +0 -0
  33. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/_tests/test_reader.py +0 -0
  34. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/_tests/test_sample_data.py +0 -0
  35. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/_tests/test_widget.py +0 -0
  36. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/_tests/test_writer.py +0 -0
  37. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/_widget.py +0 -0
  38. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/_writer.py +0 -0
  39. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/napari.yaml +0 -0
  40. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/processing_functions/__init__.py +0 -0
  41. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas/processing_functions/scipy_filters.py +0 -0
  42. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas.egg-info/dependency_links.txt +0 -0
  43. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas.egg-info/entry_points.txt +0 -0
  44. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/src/napari_tmidas.egg-info/top_level.txt +0 -0
  45. {napari_tmidas-0.1.8 → napari_tmidas-0.1.8.5}/tox.ini +0 -0
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: napari-tmidas
3
- Version: 0.1.8
4
- Summary: Tissue Microscopy Image Data Analysis Suite
3
+ Version: 0.1.8.5
4
+ Summary: A plugin for batch processing of confocal microscopy images
5
5
  Author: Marco Meer
6
6
  Author-email: marco.meer@pm.me
7
7
  License:
@@ -58,6 +58,14 @@ Requires-Dist: magicgui
58
58
  Requires-Dist: qtpy
59
59
  Requires-Dist: scikit-image
60
60
  Requires-Dist: pyqt5
61
+ Requires-Dist: tqdm
62
+ Requires-Dist: scikit-image
63
+ Requires-Dist: ome-zarr
64
+ Requires-Dist: napari-ome-zarr
65
+ Requires-Dist: torch
66
+ Requires-Dist: torchvision
67
+ Requires-Dist: timm
68
+ Requires-Dist: opencv-python
61
69
  Provides-Extra: testing
62
70
  Requires-Dist: tox; extra == "testing"
63
71
  Requires-Dist: pytest; extra == "testing"
@@ -80,7 +88,15 @@ The `napari-tmidas` plugin consists of a growing collection of pipelines for fas
80
88
  ## Feature Overview
81
89
 
82
90
  1. **Image Processing**
83
- - Process image folders with: Gamma correction, Z-projection, channel splitting, Gaussian/median filters, thresholding (Otsu/manual), and label cleaning
91
+ - Process image folders with:
92
+ - Gamma correction & histogram equalization
93
+ - Z-projection and channel splitting
94
+ - Gaussian & median filters
95
+ - Thresholding (Otsu/manual)
96
+ - Label cleaning & binary conversion
97
+ - RGB to labels conversion
98
+ - Cellpose 3.0 automated segmentation
99
+ - File compression (Zstandard)
84
100
 
85
101
  2. **Label Inspection**
86
102
  - Review and edit label images with auto-save
@@ -112,19 +128,27 @@ Now you can install `napari-tmidas` via [pip]:
112
128
 
113
129
  pip install napari-tmidas
114
130
 
115
- To install the latest development version:
131
+ It is recommended to install the latest development version:
116
132
 
117
133
  pip install git+https://github.com/macromeer/napari-tmidas.git
118
134
 
119
135
  ### Dependencies
120
- To use the Batch Microscopy Image Conversion pipeline, we need some libraries to read microscopy formats and to write ome-zarr:
121
136
 
122
- pip install nd2 readlif tiffslide pylibCZIrw acquifer-napari ome-zarr napari-ome-zarr
137
+ To use the Batch Microscopy Image Conversion pipeline, we need some libraries to read microscopy formats:
138
+
139
+ pip install nd2 readlif tiffslide pylibCZIrw acquifer-napari
123
140
 
124
141
  For the Batch Crop Anything pipeline, we need to install MobileSAM and its dependencies:
125
142
 
126
143
  pip install git+https://github.com/ChaoningZhang/MobileSAM.git
127
- pip install torch torchvision timm opencv-python
144
+
145
+
146
+ If you want to batch compress images using [Zstandard](https://github.com/facebook/zstd), use the package manager of your operating system to install it:
147
+
148
+ sudo apt-get install zstd # for Linux
149
+ brew install zstd # for macOS
150
+ choco install zstandard # for Windows
151
+
128
152
 
129
153
  ## Usage
130
154
 
@@ -169,9 +193,9 @@ If you have already segmented a folder full of images and now you want to maybe
169
193
  ![image](https://github.com/user-attachments/assets/0bf8c6ae-4212-449d-8183-e91b23ba740e)
170
194
 
171
195
  ### Crop Anything
172
- This pipeline combines the Segment Anything Model (SAM) for automatic object detection with an interactive interface for selecting and cropping multiple objects from images. To launch the widget, open `Plugins > T-MIDAS > Batch Crop Anything`
196
+ This pipeline combines the Segment Anything Model (SAM) for automatic object detection with an interactive interface for selecting and cropping multiple objects from images. To launch the widget, open `Plugins > T-MIDAS > Batch Crop Anything`. Click the image below to see a video demo.
173
197
 
174
- ![image](https://github.com/user-attachments/assets/6d72c2a2-1064-4a27-b398-a9b86fcbc443)
198
+ [![image](https://github.com/user-attachments/assets/6d72c2a2-1064-4a27-b398-a9b86fcbc443)](https://youtu.be/xPh0dRD_FbE)
175
199
 
176
200
  ### ROI Colocalization
177
201
  This pipeline quantifies colocalization between labeled regions of interest (ROIs) across multiple image channels. It determines the extent of overlap between ROIs in a reference channel and those in one or two other channels. The output is a table of colocalization counts. Optionally, the size of reference channel ROIs, as well as the total or median size of colocalizing ROIs in the other channels, can be included. Colocalization is determined using Boolean masking. The number of colocalizing instances is determined by counting unique label IDs within the overlapping regions. Typically, the reference channel contains larger structures, while other channels contain smaller, potentially nested, structures. For example, the reference channel might contain cell bodies, with the second and third channels containing nuclei and sub-nuclear objects, respectively.
@@ -11,7 +11,15 @@ The `napari-tmidas` plugin consists of a growing collection of pipelines for fas
11
11
  ## Feature Overview
12
12
 
13
13
  1. **Image Processing**
14
- - Process image folders with: Gamma correction, Z-projection, channel splitting, Gaussian/median filters, thresholding (Otsu/manual), and label cleaning
14
+ - Process image folders with:
15
+ - Gamma correction & histogram equalization
16
+ - Z-projection and channel splitting
17
+ - Gaussian & median filters
18
+ - Thresholding (Otsu/manual)
19
+ - Label cleaning & binary conversion
20
+ - RGB to labels conversion
21
+ - Cellpose 3.0 automated segmentation
22
+ - File compression (Zstandard)
15
23
 
16
24
  2. **Label Inspection**
17
25
  - Review and edit label images with auto-save
@@ -43,19 +51,27 @@ Now you can install `napari-tmidas` via [pip]:
43
51
 
44
52
  pip install napari-tmidas
45
53
 
46
- To install the latest development version:
54
+ It is recommended to install the latest development version:
47
55
 
48
56
  pip install git+https://github.com/macromeer/napari-tmidas.git
49
57
 
50
58
  ### Dependencies
51
- To use the Batch Microscopy Image Conversion pipeline, we need some libraries to read microscopy formats and to write ome-zarr:
52
59
 
53
- pip install nd2 readlif tiffslide pylibCZIrw acquifer-napari ome-zarr napari-ome-zarr
60
+ To use the Batch Microscopy Image Conversion pipeline, we need some libraries to read microscopy formats:
61
+
62
+ pip install nd2 readlif tiffslide pylibCZIrw acquifer-napari
54
63
 
55
64
  For the Batch Crop Anything pipeline, we need to install MobileSAM and its dependencies:
56
65
 
57
66
  pip install git+https://github.com/ChaoningZhang/MobileSAM.git
58
- pip install torch torchvision timm opencv-python
67
+
68
+
69
+ If you want to batch compress images using [Zstandard](https://github.com/facebook/zstd), use the package manager of your operating system to install it:
70
+
71
+ sudo apt-get install zstd # for Linux
72
+ brew install zstd # for macOS
73
+ choco install zstandard # for Windows
74
+
59
75
 
60
76
  ## Usage
61
77
 
@@ -100,9 +116,9 @@ If you have already segmented a folder full of images and now you want to maybe
100
116
  ![image](https://github.com/user-attachments/assets/0bf8c6ae-4212-449d-8183-e91b23ba740e)
101
117
 
102
118
  ### Crop Anything
103
- This pipeline combines the Segment Anything Model (SAM) for automatic object detection with an interactive interface for selecting and cropping multiple objects from images. To launch the widget, open `Plugins > T-MIDAS > Batch Crop Anything`
119
+ This pipeline combines the Segment Anything Model (SAM) for automatic object detection with an interactive interface for selecting and cropping multiple objects from images. To launch the widget, open `Plugins > T-MIDAS > Batch Crop Anything`. Click the image below to see a video demo.
104
120
 
105
- ![image](https://github.com/user-attachments/assets/6d72c2a2-1064-4a27-b398-a9b86fcbc443)
121
+ [![image](https://github.com/user-attachments/assets/6d72c2a2-1064-4a27-b398-a9b86fcbc443)](https://youtu.be/xPh0dRD_FbE)
106
122
 
107
123
  ### ROI Colocalization
108
124
  This pipeline quantifies colocalization between labeled regions of interest (ROIs) across multiple image channels. It determines the extent of overlap between ROIs in a reference channel and those in one or two other channels. The output is a table of colocalization counts. Optionally, the size of reference channel ROIs, as well as the total or median size of colocalizing ROIs in the other channels, can be included. Colocalization is determined using Boolean masking. The number of colocalizing instances is determined by counting unique label IDs within the overlapping regions. Typically, the reference channel contains larger structures, while other channels contain smaller, potentially nested, structures. For example, the reference channel might contain cell bodies, with the second and third channels containing nuclei and sub-nuclear objects, respectively.
@@ -1,7 +1,7 @@
1
1
  [project]
2
2
  name = "napari-tmidas"
3
3
  dynamic = ["version"]
4
- description = "Tissue Microscopy Image Data Analysis Suite"
4
+ description = "A plugin for batch processing of confocal microscopy images"
5
5
  readme = "README.md"
6
6
  license = {file = "LICENSE"}
7
7
  authors = [
@@ -30,6 +30,14 @@ dependencies = [
30
30
  "qtpy",
31
31
  "scikit-image",
32
32
  "pyqt5",
33
+ "tqdm",
34
+ "scikit-image",
35
+ "ome-zarr",
36
+ "napari-ome-zarr",
37
+ "torch",
38
+ "torchvision",
39
+ "timm",
40
+ "opencv-python",
33
41
  ]
34
42
 
35
43
  [project.optional-dependencies]
@@ -29,6 +29,7 @@ from qtpy.QtWidgets import (
29
29
  QWidget,
30
30
  )
31
31
  from skimage.io import imread
32
+ from skimage.transform import resize # Added import for resize function
32
33
  from tifffile import imwrite
33
34
 
34
35
 
@@ -48,6 +49,7 @@ class BatchCropAnything:
48
49
  self.original_image = None
49
50
  self.segmentation_result = None
50
51
  self.current_image_for_segmentation = None
52
+ self.current_scale_factor = 1.0 # Added scale factor tracking
51
53
 
52
54
  # UI references
53
55
  self.image_layer = None
@@ -356,10 +358,41 @@ class BatchCropAnything:
356
358
  # Convert back to uint8
357
359
  image_gamma = (image_gamma * 255).astype(np.uint8)
358
360
 
361
+ # Check if the image is very large and needs downscaling
362
+ orig_shape = image_gamma.shape[:2] # (height, width)
363
+
364
+ # Calculate image size in megapixels
365
+ image_mp = (orig_shape[0] * orig_shape[1]) / 1e6
366
+
367
+ # If image is larger than 2 megapixels, downscale it
368
+ max_mp = 2.0 # Maximum image size in megapixels
369
+ scale_factor = 1.0
370
+
371
+ if image_mp > max_mp:
372
+ scale_factor = np.sqrt(max_mp / image_mp)
373
+ new_height = int(orig_shape[0] * scale_factor)
374
+ new_width = int(orig_shape[1] * scale_factor)
375
+
376
+ self.viewer.status = f"Downscaling image from {orig_shape} to {(new_height, new_width)} for processing (scale: {scale_factor:.2f})"
377
+
378
+ # Resize the image for processing
379
+ image_gamma_resized = resize(
380
+ image_gamma,
381
+ (new_height, new_width),
382
+ anti_aliasing=True,
383
+ preserve_range=True,
384
+ ).astype(np.uint8)
385
+
386
+ # Store scale factor for later use
387
+ self.current_scale_factor = scale_factor
388
+ else:
389
+ image_gamma_resized = image_gamma
390
+ self.current_scale_factor = 1.0
391
+
359
392
  self.viewer.status = f"Generating segmentation with sensitivity {self.sensitivity} (gamma={gamma:.2f})..."
360
393
 
361
- # Generate masks with gamma-corrected image
362
- masks = self.mask_generator.generate(image_gamma)
394
+ # Generate masks with gamma-corrected and potentially resized image
395
+ masks = self.mask_generator.generate(image_gamma_resized)
363
396
  self.viewer.status = f"Generated {len(masks)} masks"
364
397
 
365
398
  if not masks:
@@ -390,9 +423,16 @@ class BatchCropAnything:
390
423
  return
391
424
 
392
425
  # Process segmentation masks
393
- self._process_segmentation_masks(
394
- masks, self.current_image_for_segmentation.shape[:2]
395
- )
426
+ # If image was downscaled, we need to ensure masks are upscaled correctly
427
+ if self.current_scale_factor < 1.0:
428
+ # Upscale the segmentation masks to match the original image dimensions
429
+ self._process_segmentation_masks_with_scaling(
430
+ masks, self.current_image_for_segmentation.shape[:2]
431
+ )
432
+ else:
433
+ self._process_segmentation_masks(
434
+ masks, self.current_image_for_segmentation.shape[:2]
435
+ )
396
436
 
397
437
  # Clear selected labels since segmentation has changed
398
438
  self.selected_labels = set()
@@ -475,6 +515,98 @@ class BatchCropAnything:
475
515
  # image_name = os.path.basename(self.images[self.current_index])
476
516
  self.viewer.status = f"Loaded image {self.current_index + 1}/{len(self.images)} - Found {len(masks)} segments"
477
517
 
518
+ # New method for handling scaled segmentation masks
519
+ def _process_segmentation_masks_with_scaling(self, masks, original_shape):
520
+ """Process segmentation masks with scaling to match the original image size."""
521
+ # Create label image from masks
522
+ # First determine the size of the mask predictions (which are at the downscaled resolution)
523
+ if not masks:
524
+ return
525
+
526
+ mask_shape = masks[0]["segmentation"].shape
527
+
528
+ # Create an empty label image at the downscaled resolution
529
+ downscaled_labels = np.zeros(mask_shape, dtype=np.uint32)
530
+ self.label_info = {} # Reset label info
531
+
532
+ # Fill in the downscaled labels
533
+ for i, mask_data in enumerate(masks):
534
+ mask = mask_data["segmentation"]
535
+ label_id = i + 1 # Start label IDs from 1
536
+ downscaled_labels[mask] = label_id
537
+
538
+ # Store basic label info
539
+ area = np.sum(mask)
540
+ y_indices, x_indices = np.where(mask)
541
+ center_y = np.mean(y_indices) if len(y_indices) > 0 else 0
542
+ center_x = np.mean(x_indices) if len(x_indices) > 0 else 0
543
+
544
+ # Scale centers to original image coordinates
545
+ center_y_orig = center_y / self.current_scale_factor
546
+ center_x_orig = center_x / self.current_scale_factor
547
+
548
+ # Store label info at original scale
549
+ self.label_info[label_id] = {
550
+ "area": area
551
+ / (
552
+ self.current_scale_factor**2
553
+ ), # Approximate area in original scale
554
+ "center_y": center_y_orig,
555
+ "center_x": center_x_orig,
556
+ "score": mask_data.get("stability_score", 0),
557
+ }
558
+
559
+ # Upscale the labels to the original image size
560
+ upscaled_labels = resize(
561
+ downscaled_labels,
562
+ original_shape,
563
+ order=0, # Nearest neighbor interpolation
564
+ preserve_range=True,
565
+ anti_aliasing=False,
566
+ ).astype(np.uint32)
567
+
568
+ # Sort labels by area (largest first)
569
+ self.label_info = dict(
570
+ sorted(
571
+ self.label_info.items(),
572
+ key=lambda item: item[1]["area"],
573
+ reverse=True,
574
+ )
575
+ )
576
+
577
+ # Save segmentation result
578
+ self.segmentation_result = upscaled_labels
579
+
580
+ # Remove existing label layer if exists
581
+ for layer in list(self.viewer.layers):
582
+ if isinstance(layer, Labels) and "Segmentation" in layer.name:
583
+ self.viewer.layers.remove(layer)
584
+
585
+ # Add label layer to viewer
586
+ self.label_layer = self.viewer.add_labels(
587
+ upscaled_labels,
588
+ name=f"Segmentation ({os.path.basename(self.images[self.current_index])})",
589
+ opacity=0.7,
590
+ )
591
+
592
+ # Make the label layer active by default
593
+ self.viewer.layers.selection.active = self.label_layer
594
+
595
+ # Disconnect existing callbacks if any
596
+ if (
597
+ hasattr(self, "label_layer")
598
+ and self.label_layer is not None
599
+ and hasattr(self.label_layer, "mouse_drag_callbacks")
600
+ ):
601
+ # Remove old callbacks
602
+ for callback in list(self.label_layer.mouse_drag_callbacks):
603
+ self.label_layer.mouse_drag_callbacks.remove(callback)
604
+
605
+ # Connect mouse click event to label selection
606
+ self.label_layer.mouse_drag_callbacks.append(self._on_label_clicked)
607
+
608
+ self.viewer.status = f"Loaded image {self.current_index + 1}/{len(self.images)} - Found {len(masks)} segments"
609
+
478
610
  # --------------------------------------------------
479
611
  # Label Selection and UI Elements
480
612
  # --------------------------------------------------
@@ -1105,19 +1105,21 @@ class ConversionWorker(QThread):
1105
1105
  )
1106
1106
  file_size_GB = estimated_size_bytes / (1024**3)
1107
1107
 
1108
- # If file is very large (>4GB), force zarr format regardless of setting
1108
+ # Determine format
1109
1109
  use_zarr = self.use_zarr
1110
- if file_size_GB > 4:
1111
- use_zarr = True
1112
- if not self.use_zarr:
1113
- print(
1114
- f"File size ({file_size_GB:.2f}GB) exceeds 4GB limit for TIF, automatically using ZARR format"
1115
- )
1116
- self.file_done.emit(
1117
- filepath,
1118
- True,
1119
- f"File size ({file_size_GB:.2f}GB) exceeds 4GB, using ZARR format",
1120
- )
1110
+ # If file is very large (>4GB) and user didn't explicitly choose TIF,
1111
+ # auto-switch to ZARR format
1112
+ if file_size_GB > 4 and not self.use_zarr:
1113
+ # Recommend ZARR format but respect user's choice by still allowing TIF
1114
+ print(
1115
+ f"File size ({file_size_GB:.2f}GB) exceeds 4GB, ZARR format is recommended but using TIF with BigTIFF format"
1116
+ )
1117
+ self.file_done.emit(
1118
+ filepath,
1119
+ True,
1120
+ f"File size ({file_size_GB:.2f}GB) exceeds 4GB, using TIF with BigTIFF format",
1121
+ )
1122
+
1121
1123
  # Set up the output path
1122
1124
  if use_zarr:
1123
1125
  output_path = os.path.join(
@@ -1171,12 +1173,22 @@ class ConversionWorker(QThread):
1171
1173
  def _save_tif(
1172
1174
  self, image_data: np.ndarray, output_path: str, metadata: dict = None
1173
1175
  ):
1174
- """Enhanced TIF saving with proper dimension handling"""
1176
+ """Enhanced TIF saving with proper dimension handling and BigTIFF support"""
1175
1177
  import tifffile
1176
1178
 
1177
1179
  print(f"Saving TIF file: {output_path}")
1178
1180
  print(f"Image data shape: {image_data.shape}")
1179
1181
 
1182
+ # Check if this is a large file that needs BigTIFF
1183
+ estimated_size_bytes = np.prod(image_data.shape) * image_data.itemsize
1184
+ file_size_GB = estimated_size_bytes / (1024**3)
1185
+ use_bigtiff = file_size_GB > 4
1186
+
1187
+ if use_bigtiff:
1188
+ print(
1189
+ f"File size ({file_size_GB:.2f}GB) exceeds 4GB, using BigTIFF format"
1190
+ )
1191
+
1180
1192
  if metadata:
1181
1193
  print(f"Metadata keys: {list(metadata.keys())}")
1182
1194
  if "axes" in metadata:
@@ -1198,7 +1210,12 @@ class ConversionWorker(QThread):
1198
1210
  # Basic save if no metadata
1199
1211
  if metadata is None:
1200
1212
  print("No metadata provided, using basic save")
1201
- tifffile.imwrite(output_path, image_data, compression="zstd")
1213
+ tifffile.imwrite(
1214
+ output_path,
1215
+ image_data,
1216
+ compression="zlib",
1217
+ bigtiff=use_bigtiff,
1218
+ )
1202
1219
  return
1203
1220
 
1204
1221
  # Get image dimensions and axis order
@@ -1261,7 +1278,10 @@ class ConversionWorker(QThread):
1261
1278
  print(f"Error reordering dimensions: {e}")
1262
1279
  # Fall back to simple save without reordering
1263
1280
  tifffile.imwrite(
1264
- output_path, image_data, compression="zstd"
1281
+ output_path,
1282
+ image_data,
1283
+ compression="zlib",
1284
+ bigtiff=use_bigtiff,
1265
1285
  )
1266
1286
  return
1267
1287
 
@@ -1287,7 +1307,8 @@ class ConversionWorker(QThread):
1287
1307
  output_path,
1288
1308
  image_data,
1289
1309
  resolution=resolution,
1290
- compression="zstd",
1310
+ compression="zlib",
1311
+ bigtiff=use_bigtiff,
1291
1312
  )
1292
1313
  else:
1293
1314
  # Hyperstack case
@@ -1306,14 +1327,15 @@ class ConversionWorker(QThread):
1306
1327
  imagej=True,
1307
1328
  resolution=resolution,
1308
1329
  metadata=imagej_metadata,
1309
- compression="zstd",
1330
+ compression="zlib",
1331
+ bigtiff=use_bigtiff,
1310
1332
  )
1311
1333
 
1312
1334
  print(f"Successfully saved TIF file: {output_path}")
1313
1335
  except (ValueError, FileNotFoundError) as e:
1314
1336
  print(f"Error saving TIF file: {e}")
1315
1337
  # Try simple save as fallback
1316
- tifffile.imwrite(output_path, image_data)
1338
+ tifffile.imwrite(output_path, image_data, bigtiff=use_bigtiff)
1317
1339
 
1318
1340
  def _save_zarr(
1319
1341
  self, image_data: np.ndarray, output_path: str, metadata: dict = None
@@ -603,15 +603,29 @@ class ProcessingWorker(QThread):
603
603
  self.processing_finished.emit()
604
604
 
605
605
  def process_file(self, filepath):
606
- """Process a single file"""
606
+ """Process a single file with support for large TIFF files and removal of all singleton dimensions"""
607
607
  try:
608
608
  # Load the image
609
609
  image = imread(filepath)
610
610
  image_dtype = image.dtype
611
611
 
612
+ print(f"Original image shape: {image.shape}, dtype: {image_dtype}")
613
+
612
614
  # Apply processing with parameters
613
615
  processed_image = self.processing_func(image, **self.param_values)
614
616
 
617
+ print(
618
+ f"Processed image shape before removing singletons: {processed_image.shape}, dtype: {processed_image.dtype}"
619
+ )
620
+
621
+ # Remove ALL singleton dimensions from the processed image
622
+ # This will keep only dimensions with size > 1
623
+ processed_image = np.squeeze(processed_image)
624
+
625
+ print(
626
+ f"Processed image shape after removing singletons: {processed_image.shape}"
627
+ )
628
+
615
629
  # Generate new filename base
616
630
  filename = os.path.basename(filepath)
617
631
  name, ext = os.path.splitext(filename)
@@ -619,33 +633,91 @@ class ProcessingWorker(QThread):
619
633
  name.replace(self.input_suffix, "") + self.output_suffix
620
634
  )
621
635
 
622
- # Check if the processed image is a stacked array
623
- if processed_image.ndim > image.ndim:
636
+ # Check if the first dimension should be treated as channels
637
+ # If processed_image has more dimensions than the original image,
638
+ # assume the first dimension represents channels
639
+ is_multi_channel = (processed_image.ndim > image.ndim - 1) or (
640
+ processed_image.ndim == image.ndim
641
+ and processed_image.shape[0] <= 10
642
+ )
643
+
644
+ if (
645
+ is_multi_channel and processed_image.shape[0] <= 10
646
+ ): # Reasonable number of channels
624
647
  # Save each channel as a separate image
625
648
  processed_files = []
626
- for i in range(processed_image.shape[0]):
649
+
650
+ num_channels = processed_image.shape[0]
651
+ print(
652
+ f"Treating first dimension as channels. Saving {num_channels} separate channel files"
653
+ )
654
+
655
+ for i in range(num_channels):
627
656
  channel_filename = f"{new_filename_base}_channel_{i}{ext}"
628
657
  channel_filepath = os.path.join(
629
658
  self.output_folder, channel_filename
630
659
  )
631
660
 
661
+ # Extract channel data and remove any remaining singleton dimensions
662
+ channel_image = np.squeeze(processed_image[i])
663
+
664
+ print(f"Channel {i} shape: {channel_image.shape}")
665
+
666
+ # Calculate approx file size in GB
667
+ size_gb = (
668
+ channel_image.size * channel_image.itemsize / (1024**3)
669
+ )
670
+ print(f"Estimated file size: {size_gb:.2f} GB")
671
+
672
+ # Check data range
673
+ data_min = (
674
+ np.min(channel_image) if channel_image.size > 0 else 0
675
+ )
676
+ data_max = (
677
+ np.max(channel_image) if channel_image.size > 0 else 0
678
+ )
679
+ print(f"Channel {i} data range: {data_min} to {data_max}")
680
+
681
+ # For very large files, we need to use BigTIFF format
682
+ use_bigtiff = (
683
+ size_gb > 2.0
684
+ ) # Use BigTIFF for files over 2GB
685
+
632
686
  if (
633
687
  "labels" in channel_filename
634
688
  or "semantic" in channel_filename
635
689
  ):
690
+ # Choose appropriate integer type based on data range
691
+ if data_max <= 255:
692
+ save_dtype = np.uint8
693
+ elif data_max <= 65535:
694
+ save_dtype = np.uint16
695
+ else:
696
+ save_dtype = np.uint32
697
+
698
+ print(
699
+ f"Label image detected, saving as {save_dtype.__name__} with bigtiff={use_bigtiff}"
700
+ )
636
701
  tifffile.imwrite(
637
702
  channel_filepath,
638
- processed_image[i].astype(np.uint32),
703
+ channel_image.astype(save_dtype),
639
704
  compression="zlib",
705
+ bigtiff=use_bigtiff,
640
706
  )
641
707
  else:
642
- # First remove singletons
643
- channel_image = np.squeeze(processed_image[i])
708
+ # Handle large images with bigtiff format
709
+ print(
710
+ f"Regular image channel, saving with dtype {image_dtype} and bigtiff={use_bigtiff}"
711
+ )
712
+
713
+ # Save with original dtype and bigtiff format if needed
644
714
  tifffile.imwrite(
645
715
  channel_filepath,
646
716
  channel_image.astype(image_dtype),
647
717
  compression="zlib",
718
+ bigtiff=use_bigtiff,
648
719
  )
720
+
649
721
  processed_files.append(channel_filepath)
650
722
 
651
723
  # Return processing info
@@ -654,25 +726,61 @@ class ProcessingWorker(QThread):
654
726
  "processed_files": processed_files,
655
727
  }
656
728
  else:
657
- # Save as a single image (original behavior)
729
+ # Save as a single image
658
730
  new_filepath = os.path.join(
659
731
  self.output_folder, new_filename_base + ext
660
732
  )
661
733
 
734
+ print(f"Single output image shape: {processed_image.shape}")
735
+
736
+ # Calculate approx file size in GB
737
+ size_gb = (
738
+ processed_image.size * processed_image.itemsize / (1024**3)
739
+ )
740
+ print(f"Estimated file size: {size_gb:.2f} GB")
741
+
742
+ # For very large files, we need to use BigTIFF format
743
+ use_bigtiff = size_gb > 2.0 # Use BigTIFF for files over 2GB
744
+
745
+ # Check data range
746
+ data_min = (
747
+ np.min(processed_image) if processed_image.size > 0 else 0
748
+ )
749
+ data_max = (
750
+ np.max(processed_image) if processed_image.size > 0 else 0
751
+ )
752
+ print(f"Data range: {data_min} to {data_max}")
753
+
662
754
  if (
663
755
  "labels" in new_filename_base
664
756
  or "semantic" in new_filename_base
665
757
  ):
758
+ # Choose appropriate integer type based on data range
759
+ if data_max <= 255:
760
+ save_dtype = np.uint8
761
+ elif data_max <= 65535:
762
+ save_dtype = np.uint16
763
+ else:
764
+ save_dtype = np.uint32
765
+
766
+ print(
767
+ f"Saving label image as {save_dtype.__name__} with bigtiff={use_bigtiff}"
768
+ )
666
769
  tifffile.imwrite(
667
770
  new_filepath,
668
- processed_image.astype(np.uint32),
771
+ processed_image.astype(save_dtype),
669
772
  compression="zlib",
773
+ bigtiff=use_bigtiff,
670
774
  )
671
775
  else:
776
+ print(
777
+ f"Saving image with dtype {image_dtype} and bigtiff={use_bigtiff}"
778
+ )
672
779
  tifffile.imwrite(
673
780
  new_filepath,
674
781
  processed_image.astype(image_dtype),
675
782
  compression="zlib",
783
+ bigtiff=use_bigtiff,
676
784
  )
677
785
 
678
786
  # Return processing info
@@ -684,6 +792,9 @@ class ProcessingWorker(QThread):
684
792
  except Exception as e:
685
793
  # Log the error and re-raise to be caught by the executor
686
794
  print(f"Error processing {filepath}: {e}")
795
+ import traceback
796
+
797
+ traceback.print_exc()
687
798
  raise
688
799
  finally:
689
800
  # Explicit cleanup to help with memory management
@@ -692,10 +803,6 @@ class ProcessingWorker(QThread):
692
803
  if "processed_image" in locals():
693
804
  del processed_image
694
805
 
695
- def stop(self):
696
- """Request worker to stop processing"""
697
- self.stop_requested = True
698
-
699
806
 
700
807
  class FileResultsWidget(QWidget):
701
808
  """