1+ import argparse
2+ import sys , os
3+
4+ import multiprocessing as mp
5+ from concurrent import futures
6+
7+ import imageio .v3 as imageio
8+ import numpy as np
9+ import nifty .tools as nt
10+ from tqdm import tqdm
11+
12+ from elf .wrapper .resized_volume import ResizedVolume
13+ from elf .io import open_file
14+
15+ def main (input_path , output_folder , scale , input_key , interpolation_order ):
16+ input_ = open_file (input_path , "r" )[input_key ]
17+
18+ abs_path = os .path .abspath (input_path )
19+ basename = "" .join (os .path .basename (abs_path ).split ("." )[:- 1 ])
20+ output_path = os .path .join (output_folder , basename + "_resized.n5" )
21+
22+ shape = input_ .shape
23+ ndim = len (shape )
24+
25+ # Limit the number of cores for parallelization.
26+ n_threads = min (16 , mp .cpu_count ())
27+
28+ shape = input_ .shape
29+ new_shape = tuple (
30+ int (round (sh / scale )) for sh in shape
31+ )
32+
33+ resized_volume = ResizedVolume (input_ , new_shape , order = interpolation_order )
34+
35+ output = open_file (output_path , mode = "a" )
36+ dataset = output .create_dataset (input_key , shape = new_shape , dtype = input_ .dtype , chunks = input_ .chunks , compression = "gzip" )
37+ blocking = nt .blocking ([0 ] * ndim , new_shape , input_ .chunks )
38+
39+ def copy_chunk (block_index ):
40+ block = blocking .getBlock (block_index )
41+ volume_index = tuple (slice (begin , end ) for (begin , end ) in zip (block .begin , block .end ))
42+ data = resized_volume [volume_index ]
43+ output [volume_index ] = data
44+
45+ with futures .ThreadPoolExecutor (n_threads ) as resize_pool :
46+ list (tqdm (resize_pool .map (copy_chunk , range (blocking .numberOfBlocks )), total = blocking .numberOfBlocks ))
47+
48+
49+ if __name__ == "__main__" :
50+
51+ parser = argparse .ArgumentParser (
52+ description = "Script for resizing microscoopy data in n5 format." )
53+
54+ parser .add_argument ('input_file' , type = str , help = "Input file" )
55+ parser .add_argument ('output_folder' , type = str , help = "Output folder. Default resized output is <basename>_resized.n5" )
56+
57+ parser .add_argument ('-s' , "--scale" , type = float , default = 0.38 , help = "Scale of input. Re-scaled to 1." )
58+ parser .add_argument ('-k' , "--input_key" , type = str , default = "setup0/timepoint0/s0" , help = "Input key for n5 file." )
59+ parser .add_argument ('-i' , "--interpolation_order" , type = float , default = 3 , help = "Interpolation order." )
60+
61+ args = parser .parse_args ()
62+
63+ main (args .input , args .output , args .scale , args .input_key , args .interpolation_order )
0 commit comments