Skip to content

Commit

Permalink
Tile position writer now handles multi-channel images correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorTatarnikov committed Jan 19, 2024
1 parent d0236eb commit 8499ca1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
18 changes: 13 additions & 5 deletions mesospim_stitcher/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ def create_pyramid_bdv_h5(
yield int(100 * num_done / num_slices)


def write_big_stitcher_tile_config(meta_file_name: Path) -> list[dict]:
def write_big_stitcher_tile_config(
meta_file_name: Path, h5_path: Path
) -> list[dict]:
tile_metadata = parse_mesospim_metadata(meta_file_name)

output_file = str(meta_file_name)[:-12] + "_tile_config.txt"
Expand Down Expand Up @@ -93,13 +95,19 @@ def write_big_stitcher_tile_config(meta_file_name: Path) -> list[dict]:
)
relative_locations.append(rel_tuple)

tile_names = []
with h5py.File(h5_path, "r") as f:
for tile in f["t00000"]:
tile_names.append(tile)

with open(output_file, "w") as f:
f.write("dim=3\n")
for i in range(len(tile_metadata)):
for i, tile_name in enumerate(tile_names):
tile_index = (i % num_channels) + (i // num_tiles) * num_channels
f.write(
f"{i};;"
f"({relative_locations[i%num_tiles][0]},"
f"{relative_locations[i%num_tiles][1]},0)\n"
f"{tile_name[1:]};;"
f"({relative_locations[tile_index][0]},"
f"{relative_locations[tile_index][1]},0)\n"
)

return tile_metadata
Expand Down
56 changes: 38 additions & 18 deletions mesospim_stitcher/stitching_widget.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pathlib import Path
from typing import Dict, List

import dask.array as da
import h5py
Expand All @@ -21,6 +22,7 @@
from mesospim_stitcher.file_utils import (
check_mesospim_directory,
create_pyramid_bdv_h5,
write_big_stitcher_tile_config,
)

DOWNSAMPLE_ARRAY = np.array(
Expand All @@ -41,7 +43,8 @@ def __init__(self, napari_viewer: Viewer):
self.meta_path = None
self.h5_path = None
self.h5_file = None
self.tiles: da = []
self.tiles: List[da] = []
self.tile_metadata: List[Dict] = []

self.setLayout(QVBoxLayout())

Expand Down Expand Up @@ -90,8 +93,13 @@ def __init__(self, napari_viewer: Viewer):
)
self.add_tiles_button.setEnabled(False)
self.layout().addWidget(self.add_tiles_button)

self.layout().addWidget(self.progress_bar)

self.test_button = QPushButton("Test")
self.test_button.clicked.connect(self._on_test_button_clicked)
self.layout().addWidget(self.test_button)

def _on_open_file_dialog_clicked(self):
self.working_directory = Path(
QFileDialog.getExistingDirectory(
Expand All @@ -107,6 +115,21 @@ def _on_mesospim_directory_text_edited(self):
)
self.check_and_load_mesospim_directory()

def _on_create_pyramid_button_clicked(self):
self.progress_bar.setValue(0)
self.progress_bar.setRange(0, 100)

worker = create_worker(
create_pyramid_bdv_h5,
self.h5_path,
DOWNSAMPLE_ARRAY,
SUBDIVISION_ARRAY,
yield_progress=True,
)
worker.yielded.connect(self.progress_bar.setValue)
worker.finished.connect(self.progress_bar.reset)
worker.start()

def _on_add_tiles_button_clicked(self):
self.h5_file = h5py.File(self.h5_path, "r")
self.tiles = []
Expand All @@ -121,34 +144,31 @@ def _on_add_tiles_button_clicked(self):
self.tiles.append(curr_tile)
self._viewer.add_image(
curr_tile,
contrast_limits=[0, 1500],
contrast_limits=[0, 2000],
multiscale=False,
name=child,
)

def _on_create_pyramid_button_clicked(self):
self.progress_bar.setValue(0)
self.progress_bar.setRange(0, 100)

worker = create_worker(
create_pyramid_bdv_h5,
self.h5_path,
DOWNSAMPLE_ARRAY,
SUBDIVISION_ARRAY,
yield_progress=True,
)
worker.yielded.connect(self.progress_bar.setValue)
worker.finished.connect(self.progress_bar.reset)
worker.start()

def check_and_load_mesospim_directory(self):
try:
(
self.xml_path,
self.meta_path,
self.h5_path,
) = check_mesospim_directory(self.working_directory)
with h5py.File(self.h5_path, "r") as f:
if len(f["t00000/s00"].keys()) <= 1:
show_warning("Resolution pyramid not found")
self.create_pyramid_button.setEnabled(True)

self.add_tiles_button.setEnabled(True)
self.create_pyramid_button.setEnabled(True)
except FileNotFoundError:
show_warning("mesoSPIM directory not valid")

def _on_test_button_clicked(self):
self.tile_metadata = write_big_stitcher_tile_config(
self.meta_path, self.h5_path
)
print(self.tile_metadata)
# print([tile['x_pos'] for tile in self.tile_metadata])
# print([tile['y_pos'] for tile in self.tile_metadata])

0 comments on commit 8499ca1

Please sign in to comment.