Skip to content

Commit

Permalink
fix chunking issues with napari and bioio.ome_zarr_writer_2
Browse files Browse the repository at this point in the history
  • Loading branch information
soham1202 committed Jan 5, 2025
1 parent a289144 commit 8a1e1d2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
26 changes: 13 additions & 13 deletions software/control/stitcher/stitcher_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def init_stitching_parameters(self):
self.flatfields = {}
self.acquisition_metadata = {}
self.dtype = np.uint16
self.chunks = (1, 1, 1, 4096, 4096)
self.chunks = (1, 1, 1, 2048, 2048) # (1, 1, 1, 4096, 4096)
self.h_shift = (0, 0)
if self.scan_pattern == 'S-Pattern':
self.h_shift_rev = (0, 0)
Expand Down Expand Up @@ -186,25 +186,24 @@ def check_stop(self):
def cleanup(self):
"""Clean up resources before termination."""
try:
# Close any open file handles
# Clear queues
for queue in [self.progress_queue, self.status_queue, self.complete_queue]:
if queue:
while not queue.empty():
try:
queue.get_nowait()
except Empty:
pass

import gc
gc.collect() # Force garbage collection

# Clear zarr stores if any are open
if hasattr(self, 'zarr_stores'):
for store in self.zarr_stores:
try:
store.close()
except:
pass

self.emit_status("Process Stopped...")

except Exception as e:
print(f"Error during cleanup: {str(e)}")
# Continue with termination even if cleanup fails


def get_timepoints(self):
"""Get list of timepoints from input directory.
Expand Down Expand Up @@ -1123,6 +1122,7 @@ def save_region_parallel(self, timepoint, region, stitched_region):
self.output_folder, f"{timepoint}_stitched", f"{region}_stitched{self.output_format}"
)
os.makedirs(os.path.dirname(output_path), exist_ok=True)
self.emit_status(f"Saving... (Timepoint:{timepoint} Region:{region})", is_saving=True)

# Configure Zarr store and dataset
store = zarr.DirectoryStore(output_path)
Expand Down Expand Up @@ -1224,6 +1224,7 @@ def save_region_bioio(self, timepoint, region, stitched_region):
self.output_folder, f"{timepoint}_stitched", f"{region}_stitched{self.output_format}"
)
os.makedirs(os.path.dirname(output_path), exist_ok=True)
self.emit_status(f"Saving... (Timepoint:{timepoint} Region:{region})", is_saving=True)

try:
if self.output_format.endswith('.zarr'):
Expand Down Expand Up @@ -1275,6 +1276,7 @@ def save_region_bioio_2(self, timepoint, region, stitched_region):
self.output_folder, f"{timepoint}_stitched", f"{region}_stitched.ome.zarr"
)
os.makedirs(os.path.dirname(output_path), exist_ok=True)
self.emit_status(f"Saving... (Timepoint:{timepoint} Region:{region})", is_saving=True)

try:
shapes = compute_level_shapes(stitched_region.shape, (1, 1, 1, 2, 2), self.num_pyramid_levels)
Expand All @@ -1301,8 +1303,6 @@ def save_region_bioio_2(self, timepoint, region, stitched_region):
self.status_queue.put(('error', f"Error saving region {region}: {str(e)}"))
raise



def _save_debug_slice(self, stitched_region, zarr_path):
"""Save a debug RGB image slice for verification.
Expand Down
20 changes: 18 additions & 2 deletions software/control/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4327,7 +4327,12 @@ def viewOutputNapari(self):
try:
napari_viewer = napari.Viewer()
if ".ome.zarr" in self.output_path:
napari_viewer.open(self.output_path, plugin="napari-ome-zarr")
napari_viewer.open(
self.output_path,
plugin='napari-ome-zarr',
chunks=(1, 1, 1, 2048, 2048), # Smaller than (1, 1, 1, 4096, 4096) for better interactive viewing
downscale=True
)
else:
napari_viewer.open(self.output_path)

Expand All @@ -4345,6 +4350,9 @@ def viewOutputNapari(self):
min_val, max_val = self.contrastManager.get_limits(layer_name)
layer.contrast_limits = (min_val, max_val)

layer.multiscale = True
layer.downsample = True

except Exception as e:
QMessageBox.critical(self, "Error Opening in Napari", str(e))
print(f"An error occurred while opening output in Napari: {e}")
Expand All @@ -4365,8 +4373,16 @@ def resetUI(self):
self.statusLabel.setVisible(False)

def closeEvent(self, event):
"""Clean up resources when closing"""
# Stop stitching process if running
self.stop_stitching()
# Clean up queues
for queue in [self.progress_queue, self.status_queue, self.complete_queue]:
if queue:
while not queue.empty():
queue.get_nowait()
queue.close()
queue.join_thread()
# Stop queue timer
if hasattr(self, 'queue_timer'):
self.queue_timer.stop()
super().closeEvent(event)
Expand Down

0 comments on commit 8a1e1d2

Please sign in to comment.