6
6
import json
7
7
import pathlib
8
8
import shutil
9
+ from packaging import version
9
10
10
11
import argschema
11
-
12
- from acpreprocessing .stitching_modules .convert_to_n5 .tiff_to_n5 import (
13
- tiffdir_to_n5_group ,
14
- N5GenerationParameters
15
- )
12
+ from acpreprocessing .stitching_modules .convert_to_n5 .tiff_to_ngff import tiffdir_to_ngff_group , NGFFGenerationParameters
16
13
17
14
18
15
def yield_position_paths_from_rootdir (
@@ -27,17 +24,40 @@ def yield_position_paths_from_rootdir(
27
24
yield root_path / stripdir_bn
28
25
29
26
27
+ def get_position_names_from_rootdir (
28
+ root_dir , stripjson_bn = "hh.log" ,
29
+ stripjson_key = "stripdirs" ):
30
+ root_path = pathlib .Path (root_dir )
31
+ stripjson_path = root_path / stripjson_bn
32
+ with stripjson_path .open () as f :
33
+ stripjson_md = json .load (f )
34
+ return stripjson_md [stripjson_key ]
35
+
36
+
30
37
def get_pixel_resolution_from_rootdir (
31
38
root_dir , md_bn = "acqinfo_metadata.json" ):
32
39
root_path = pathlib .Path (root_dir )
33
40
md_path = root_path / md_bn
34
41
with md_path .open () as f :
35
42
md = json .load (f )
36
43
xy = md ["settings" ]["pixel_spacing_um" ]
37
- z = md ["positions" ][1 ]["x_step_um" ]
44
+ z = md ["positions" ][0 ]["x_step_um" ]
38
45
return [xy , xy , z ]
39
46
40
47
48
+ def get_strip_positions_from_rootdir (
49
+ root_dir , md_bn = "acqinfo_metadata.json" ):
50
+ root_path = pathlib .Path (root_dir )
51
+ md_path = root_path / md_bn
52
+ with md_path .open () as f :
53
+ md = json .load (f )
54
+
55
+ if version .parse (md ["version" ]) >= version .parse ("0.0.3" ):
56
+ return [(p ["z_start_um" ], p ["y_start_um" ], p ["x_start_um" ]) for p in md ["positions" ]]
57
+ else :
58
+ return [(0 , p ["y_start_um" ], p ["x_start_um" ]) for p in md ["positions" ]]
59
+
60
+
41
61
def get_number_interleaved_channels_from_rootdir (
42
62
root_dir , md_bn = "acqinfo_metadata.json" ):
43
63
root_path = pathlib .Path (root_dir )
@@ -49,29 +69,34 @@ def get_number_interleaved_channels_from_rootdir(
49
69
return interleaved_channels
50
70
51
71
52
- def acquisition_to_n5 (acquisition_dir , out_dir , concurrency = 5 ,
53
- n5_generation_kwargs = None , copy_top_level_files = True ):
72
+ def acquisition_to_ngff (acquisition_dir , output , out_dir , concurrency = 5 ,
73
+ ngff_generation_kwargs = None , copy_top_level_files = True ):
54
74
"""
55
75
"""
56
- n5_generation_kwargs = (
57
- {} if n5_generation_kwargs is None
58
- else n5_generation_kwargs )
76
+ ngff_generation_kwargs = (
77
+ {} if ngff_generation_kwargs is None
78
+ else ngff_generation_kwargs )
59
79
60
80
acquisition_path = pathlib .Path (acquisition_dir )
61
81
out_path = pathlib .Path (out_dir )
62
- out_n5_dir = str (out_path / f"{ out_path .name } .n5" )
82
+ if output == 'zarr' :
83
+ output_dir = str (out_path / f"{ out_path .name } .zarr" )
84
+ else :
85
+ output_dir = str (out_path / f"{ out_path .name } .n5" )
63
86
64
87
interleaved_channels = get_number_interleaved_channels_from_rootdir (
65
88
acquisition_path )
89
+ positionList = get_strip_positions_from_rootdir (acquisition_path )
66
90
67
91
try :
68
- setup_group_attributes = {
92
+ setup_group_attributes = [ {
69
93
"pixelResolution" : {
70
94
"dimensions" : get_pixel_resolution_from_rootdir (
71
95
acquisition_path ),
72
96
"unit" : "um"
73
- }
74
- }
97
+ },
98
+ "position" : p
99
+ } for p in positionList ]
75
100
except (KeyError , FileNotFoundError ):
76
101
setup_group_attributes = {}
77
102
@@ -85,16 +110,22 @@ def acquisition_to_n5(acquisition_dir, out_dir, concurrency=5,
85
110
# pos_group = pospath.name
86
111
# below is more like legacy structure
87
112
# out_n5_dir = str(out_path / f"{pos_group}.n5")
113
+ if output == 'zarr' :
114
+ group_names = [pospath .name ]
115
+ group_attributes = [setup_group_attributes [i ]]
116
+ else :
117
+ group_names = [
118
+ f"channel{ channel_idx } " , f"setup{ i } " , "timepoint0" ]
119
+ group_attributes = [channel_group_attributes ,
120
+ setup_group_attributes [i ]]
121
+
88
122
futs .append (e .submit (
89
- tiffdir_to_n5_group ,
90
- str (pospath ), out_n5_dir , [
91
- f"channel{ channel_idx } " , f"setup{ i } " , "timepoint0" ],
92
- group_attributes = [
93
- channel_group_attributes ,
94
- setup_group_attributes ],
123
+ tiffdir_to_ngff_group ,
124
+ str (pospath ), output , output_dir , group_names ,
125
+ group_attributes = group_attributes ,
95
126
interleaved_channels = interleaved_channels ,
96
127
channel = channel_idx ,
97
- ** n5_generation_kwargs
128
+ ** ngff_generation_kwargs
98
129
))
99
130
100
131
for fut in concurrent .futures .as_completed (futs ):
@@ -109,33 +140,35 @@ def acquisition_to_n5(acquisition_dir, out_dir, concurrency=5,
109
140
shutil .copy (str (tlf_path ), str (out_tlf_path ))
110
141
111
142
112
- class AcquisitionDirToN5DirParameters (
113
- argschema .ArgSchema , N5GenerationParameters ):
143
+ class AcquisitionDirToNGFFParameters (
144
+ argschema .ArgSchema , NGFFGenerationParameters ):
114
145
input_dir = argschema .fields .Str (required = True )
115
- output_dir = argschema .fields .Str (required = True )
146
+ output_format = argschema .fields .Str (required = True )
147
+ # output_dir = argschema.fields.Str(required=True)
116
148
copy_top_level_files = argschema .fields .Bool (required = False , default = True )
117
149
position_concurrency = argschema .fields .Int (required = False , default = 5 )
118
150
119
151
120
- class AcquisitionDirToN5Dir (argschema .ArgSchemaParser ):
121
- default_schema = AcquisitionDirToN5DirParameters
152
+ class AcquisitionDirToNGFF (argschema .ArgSchemaParser ):
153
+ default_schema = AcquisitionDirToNGFFParameters
122
154
123
- def _get_n5_kwargs (self ):
124
- n5_keys = {
155
+ def _get_ngff_kwargs (self ):
156
+ ngff_keys = {
125
157
"max_mip" , "concurrency" , "compression" ,
126
- "lvl_to_mip_kwargs" , "chunk_size" , "mip_dsfactor" }
127
- return {k : self .args [k ] for k in (n5_keys & self .args .keys ())}
158
+ "lvl_to_mip_kwargs" , "chunk_size" , "mip_dsfactor" ,
159
+ "deskew_options" }
160
+ return {k : self .args [k ] for k in (ngff_keys & self .args .keys ())}
128
161
129
162
def run (self ):
130
- n5_kwargs = self ._get_n5_kwargs ()
131
- acquisition_to_n5 (
132
- self .args ["input_dir" ], self .args ["output_dir " ],
163
+ ngff_kwargs = self ._get_ngff_kwargs ()
164
+ acquisition_to_ngff (
165
+ self .args ["input_dir" ], self .args ["output_format" ], self . args [ "output_file " ],
133
166
concurrency = self .args ["position_concurrency" ],
134
- n5_generation_kwargs = n5_kwargs ,
167
+ ngff_generation_kwargs = ngff_kwargs ,
135
168
copy_top_level_files = self .args ["copy_top_level_files" ]
136
- )
169
+ )
137
170
138
171
139
172
if __name__ == "__main__" :
140
- mod = AcquisitionDirToN5Dir ()
141
- mod .run ()
173
+ mod = AcquisitionDirToNGFF ()
174
+ mod .run ()
0 commit comments