Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorTatarnikov committed Jan 12, 2024
1 parent 5565acf commit 32a2999
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
4 changes: 2 additions & 2 deletions mesospim_stitcher/big_stitcher_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


def run_big_stitcher(
imageJ_path: Path,
imagej_path: Path,
xml_path: Path,
tile_config_path: Path,
all_channels: bool = False,
Expand All @@ -14,7 +14,7 @@ def run_big_stitcher(
):
stitch_macro_path = Path(__file__).resolve().parent / "stitch_macro.ijm"
command = (
f"{imageJ_path} --ij2"
f"{imagej_path} --ij2"
f" --headless -macro {stitch_macro_path} "
f'"{xml_path} {tile_config_path} {int(all_channels)}'
f' {selected_channel} {downsample_x} {downsample_y} {downsample_z}"'
Expand Down
78 changes: 39 additions & 39 deletions mesospim_stitcher/fuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,62 @@
from mesospim_stitcher.file_utils import write_bdv_xml


def fuse_image(
xml_path: Path, input_path: Path, output_path: Path, overwrite: bool = True
):
tree = ET.parse(xml_path)
def fuse_image(xml_path: Path, input_path: Path, output_path: Path):
input_file = h5py.File(input_path, "r")
group = input_file["t00000"]
tile_names = list(group.keys())

root = tree.getroot()
tile = group[f"{tile_names[0]}/0/cells"]
z_size, y_size, x_size = tile.shape

tile_positions = get_big_stitcher_transforms(
xml_path, x_size, y_size, z_size
)

fused_image_shape = (
max(tile_position[5] for tile_position in tile_positions),
max(tile_position[3] for tile_position in tile_positions),
max(tile_position[1] for tile_position in tile_positions),
)

if output_path.suffix == ".zarr":
fuse_to_zarr(
fused_image_shape, group, output_path, tile_names, tile_positions
)
elif output_path.suffix == ".h5":
fuse_to_bdv_h5(
fused_image_shape,
group,
output_path,
tile_names,
tile_positions,
xml_path,
)

input_file.close()


def get_big_stitcher_transforms(xml_path, x_size, y_size, z_size):
tree = ET.parse(xml_path)
root = tree.getroot()
stitch_transforms = root.findall(
".//ViewTransform/[Name='Stitching Transform']/affine"
)
assert (
stitch_transforms is not None
), "No stitching transforms found in XML file"

grid_transforms = root.findall(
".//ViewTransform/[Name='Translation from Tile Configuration']/affine"
)

if len(grid_transforms) == 0:
grid_transforms = root.findall(
".//ViewTransform/[Name='Translation to Regular Grid']/affine"
)

assert grid_transforms is not None, "No grid transforms found in XML file"

z_scale_str = root.find(".//ViewTransform/[Name='calibration']/affine")
assert z_scale_str is not None, "No z scale found in XML file"
assert z_scale_str.text is not None, "No z scale found in XML file"
z_scale = float(z_scale_str.text.split()[-2])

deltas = []
grids = []
for i in range(len(stitch_transforms)):
Expand Down Expand Up @@ -76,50 +102,24 @@ def fuse_image(

min_grid = [min([grid[i] for grid in grids]) for i in range(3)]
grids = [[grid[i] - min_grid[i] for i in range(3)] for grid in grids]

input_file = h5py.File(input_path, "r")
group = input_file["t00000"]
tile_names = list(group.keys())
max_delta = [max([abs(delta[i]) for delta in deltas]) for i in range(3)]

tile = group[f"{tile_names[0]}/0/cells"]
z_size = tile.shape[0]
x_y_size = tile.shape[1]

translations = []

for i in range(len(deltas)):
curr_delta = deltas[i]
curr_grid = grids[i]

x_start = curr_grid[0] + curr_delta[0] + max_delta[0]
x_end = x_start + x_y_size
x_end = x_start + x_size
y_start = curr_grid[1] + curr_delta[1] + max_delta[1]
y_end = y_start + x_y_size
y_end = y_start + y_size
z_start = curr_grid[2] + curr_delta[2] + max_delta[2]
z_end = z_start + z_size

translations.append([x_start, x_end, y_start, y_end, z_start, z_end])

fused_image_shape = (
max(translation[5] for translation in translations),
max(translation[3] for translation in translations),
max(translation[1] for translation in translations),
)

# fuse_to_bdv_h5(
# fused_image_shape,
# group,
# output_path,
# tile_names,
# translations,
# xml_path,
# )
fuse_to_zarr(
fused_image_shape, group, output_path, tile_names, translations
)

input_file.close()
return translations


def fuse_to_zarr(
Expand Down
5 changes: 5 additions & 0 deletions mesospim_stitcher/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
"C:/Users/Igor/Documents/NIU-dev/stitching/One_Channel/"
"2.5x_tile_igor_rightonly_Mag2.5x_ch488_ch561_ch647_bdv.xml"
)

H5_PATH = "C:/Users/Igor/Documents/NIU-dev/stitching/One_Channel/test.h5"

OUT_PATH = (
"C:/Users/Igor/Documents/NIU-dev/stitching/One_Channel/test_out.zarr"
)

META_PATH = (
"C:/Users/Igor/Documents/NIU-dev/stitching/One_Channel/"
"2.5x_tile_igor_rightonly_Mag2.5x_ch488_ch561_ch647_bdv.h5_meta.txt"
)

TILE_CONFIG_PATH = (
"C:/Users/Igor/Documents/NIU-dev/stitching/One_Channel/"
"2.5x_tile_igor_rightonly_Mag2.5x"
Expand All @@ -25,6 +29,7 @@
DOWNSAMPLE_ARRAY = np.array(
[[1, 1, 1], [2, 2, 2], [4, 4, 4], [8, 8, 8], [16, 16, 16]]
)

SUBDIVISION_ARRAY = np.array(
[[32, 32, 16], [32, 32, 16], [32, 32, 16], [32, 32, 16], [32, 32, 16]]
)
Expand Down

0 comments on commit 32a2999

Please sign in to comment.