11import  time 
22import  logging 
33import  numpy  as  np 
4+ import  wkw 
45from  argparse  import  ArgumentParser 
56from  os  import  path 
67from  natsort  import  natsorted 
2223    setup_logging ,
2324)
2425from  .image_readers  import  image_reader 
26+ from  .metadata  import  convert_element_class_to_dtype 
2527
2628BLOCK_LEN  =  32 
2729
@@ -121,15 +123,14 @@ def cubing_job(args):
121123        source_file_batches ,
122124        batch_size ,
123125        image_size ,
124-         num_channels ,
125126        pad ,
126127    ) =  args 
127128    if  len (z_batches ) ==  0 :
128129        return 
129130
130131    downsampling_needed  =  target_mag  !=  Mag (1 )
131132
132-     with  open_wkw (target_wkw_info ,  num_channels = num_channels ) as  target_wkw :
133+     with  open_wkw (target_wkw_info ) as  target_wkw :
133134        # Iterate over batches of continuous z sections 
134135        # The batches have a maximum size of `batch_size` 
135136        # Batched iterations allows to utilize IO more efficiently 
@@ -144,7 +145,9 @@ def cubing_job(args):
144145                # Iterate over each z section in the batch 
145146                for  z , file_name  in  zip (z_batch , source_file_batch ):
146147                    # Image shape will be (x, y, channel_count, z=1) 
147-                     image  =  read_image_file (file_name , target_wkw_info .dtype )
148+                     image  =  read_image_file (
149+                         file_name , target_wkw_info .header .voxel_type 
150+                     )
148151                    if  not  pad :
149152                        assert  (
150153                            image .shape [0 :2 ] ==  image_size 
@@ -171,7 +174,9 @@ def cubing_job(args):
171174                        for  _slice  in  slices 
172175                    ]
173176
174-                 buffer  =  prepare_slices_for_wkw (slices , num_channels )
177+                 buffer  =  prepare_slices_for_wkw (
178+                     slices , target_wkw_info .header .num_channels 
179+                 )
175180                if  downsampling_needed :
176181                    buffer  =  downsample_unpadded_data (
177182                        buffer , target_mag , interpolation_mode 
@@ -195,8 +200,20 @@ def cubing_job(args):
195200
196201def  cubing (source_path , target_path , layer_name , dtype , batch_size , args = None ) ->  dict :
197202
203+     source_files  =  find_source_filenames (source_path )
204+ 
205+     # All images are assumed to have equal dimensions 
206+     num_x , num_y  =  image_reader .read_dimensions (source_files [0 ])
207+     num_channels  =  image_reader .read_channel_count (source_files [0 ])
208+     num_z  =  len (source_files )
209+ 
198210    target_mag  =  Mag (args .target_mag )
199-     target_wkw_info  =  WkwDatasetInfo (target_path , layer_name , dtype , target_mag )
211+     target_wkw_info  =  WkwDatasetInfo (
212+         target_path ,
213+         layer_name ,
214+         target_mag ,
215+         wkw .Header (convert_element_class_to_dtype (dtype ), num_channels ),
216+     )
200217    interpolation_mode  =  parse_interpolation_mode (
201218        args .interpolation_mode , target_wkw_info .layer_name 
202219    )
@@ -205,16 +222,9 @@ def cubing(source_path, target_path, layer_name, dtype, batch_size, args=None) -
205222            f"Downsampling the cubed image to { target_mag }   in memory with interpolation mode { interpolation_mode }  ." 
206223        )
207224
208-     source_files  =  find_source_filenames (source_path )
209- 
210-     # All images are assumed to have equal dimensions 
211-     num_x , num_y  =  image_reader .read_dimensions (source_files [0 ])
212-     num_channels  =  image_reader .read_channel_count (source_files [0 ])
213-     num_z  =  len (source_files )
214- 
215225    logging .info ("Found source files: count={} size={}x{}" .format (num_z , num_x , num_y ))
216226
217-     ensure_wkw (target_wkw_info ,  num_channels = num_channels )
227+     ensure_wkw (target_wkw_info )
218228
219229    with  get_executor_for_args (args ) as  executor :
220230        job_args  =  []
@@ -233,7 +243,6 @@ def cubing(source_path, target_path, layer_name, dtype, batch_size, args=None) -
233243                    source_files [z :max_z ],
234244                    batch_size ,
235245                    (num_x , num_y ),
236-                     num_channels ,
237246                    args .pad ,
238247                )
239248            )
0 commit comments