Skip to content

Commit 5473462

Browse files
committed
Extract sub-volume of n5 file
1 parent e185ee1 commit 5473462

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

scripts/extract_block.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import os
2+
import argparse
3+
import numpy as np
4+
import h5py
5+
import z5py
6+
7+
"""
8+
This script extracts data around an input center coordinate in a given ROI halo.
9+
"""
10+
11+
12+
def main(input_file, output_dir, input_key, resolution, coords, roi_halo):
13+
"""
14+
15+
:param str input_file: File path to input folder in n5 format
16+
:param str output_dir: output directory for saving cropped n5 file as <basename>_crop.n5
17+
:param str input_key: Key for accessing volume in n5 format, e.g. 'setup0/s0'
18+
:param float resolution: Resolution of input data in micrometer
19+
:param str coords: Center coordinates of extracted 3D volume in format 'z,y,x'
20+
:param str roi_halo: ROI halo of extracted 3D volume in format 'z,y,x'
21+
"""
22+
23+
coords = [int(r) for r in coords.split(",")]
24+
roi_halo = [int(r) for r in roi_halo.split(",")]
25+
26+
input_content = list(filter(None, input_file.split("/")))
27+
basename = "".join(input_content[-1].split(".")[:-1])
28+
input_dir = input_file.split(basename)[0]
29+
input_dir = os.path.abspath(input_dir)
30+
31+
if "" == output_dir:
32+
output_dir = input_dir
33+
34+
input_key = "setup0/timepoint0/s0"
35+
36+
output_file = os.path.join(output_dir, basename + "_crop" + ".n5")
37+
38+
#M_LR_000167_R, coords = '806,1042,1334', coords = (z, y, x) compared to MoBIE view
39+
40+
coords = np.array(coords)
41+
coords = coords / resolution
42+
coords = np.round(coords).astype(np.int32)
43+
44+
roi = tuple(slice(co - rh, co + rh) for co, rh in zip(coords, roi_halo))
45+
46+
with z5py.File(input_file, "r") as f:
47+
raw = f[input_key][roi]
48+
49+
with z5py.File(output_file, "w") as f_out:
50+
f_out.create_dataset("raw", data=raw, compression="gzip")
51+
52+
if __name__ == "__main__":
53+
54+
parser = argparse.ArgumentParser(
55+
description="Script to extract region of interest (ROI) block around center coordinate.")
56+
57+
parser.add_argument('input', type=str, help="Input file in n5 format.")
58+
parser.add_argument('-o', "--output", type=str, default="", help="Output directory")
59+
parser.add_argument('-c', "--coord", type=str, required=True, help="3D coordinate in format 'z,y,x' as center of extracted block. Dimensions are inversed to view in MoBIE (x y z) -> (z y x)")
60+
61+
parser.add_argument('-k', "--input_key", type=str, default="setup0/timepoint0/s0", help="Input key for data in input file")
62+
parser.add_argument('-r', "--resolution", type=float, default=0.38, help="Resolution of input in micrometer")
63+
64+
parser.add_argument("--roi_halo", type=str, default="128,128,64", help="ROI halo around center coordinate")
65+
66+
args = parser.parse_args()
67+
68+
main(args.input, args.output, args.input_key, args.resolution, args.coord, args.roi_halo)

0 commit comments

Comments
 (0)