2525
2626from spatialdata .models import TableModel
2727
28+ def get_scale_pixel_size (el , microns_coord_sys_name = 'microns' ):
29+ microns_tf = get_transformation (el , to_coordinate_system = microns_coord_sys_name )
30+
31+ if isinstance (microns_tf , Scale ):
32+ pixel_size_yx = [microns_tf .scale [microns_tf .axes .index (d )] for d in ['y' , 'x' ]]
33+ elif isinstance (microns_tf , Identity ):
34+ pixel_size_yx = [1.0 , 1.0 ]
35+ elif isinstance (microns_tf , Sequence ):
36+ py , px = 1.0 , 1.0
37+ for tf in microns_tf .transformations :
38+ if isinstance (tf , Scale ):
39+ py *= tf .scale [tf .axes .index ('y' )]
40+ px *= tf .scale [tf .axes .index ('x' )]
41+ pixel_size_yx = [py , px ]
42+ else :
43+ pixel_size_yx = None
44+ print (f' Warning: unhandled transform type { type (microns_tf )} , skipping calibration' )
45+ return pixel_size_yx
2846
2947def _rename_channel_coord (element_obj , channel_name_map = None ):
3048 if channel_name_map is None :
@@ -91,10 +109,9 @@ def get_cell_labels(section_sdata, table_el='table', labels_el='cell_labels', mu
91109
92110 return cell_labels_sd
93111
94-
95- def extract_bigwarp_images_for_section (section_sdata ,
96- section_n ,
97- bigwarp_projects_folder ,
112+ def extract_bigwarp_images (sdata ,
113+ bigwarp_projects_folder ,
114+ section_n = None ,
98115 el_name = 'morphology_focus' ,
99116 subset_channels = 'all' ,
100117 multiscale_level = 2 ,
@@ -103,22 +120,29 @@ def extract_bigwarp_images_for_section(section_sdata,
103120 z_step_um = None ,
104121 resunit = 'cm' ,
105122 microns_coord_sys_name = 'microns' ,
123+ save_name = None ,
106124 return_sdata = True ):
107- section_bigwarp_folder = bigwarp_projects_folder / f'section_{ section_n } '
108- section_bigwarp_folder .mkdir (exist_ok = True , parents = True )
109- if not isinstance (section_sdata , sd .SpatialData ):
110- section_sdata = sd .read_zarr (section_sdata )
111- section_sdata = add_micron_coord_sys (section_sdata )
112- if _is_multiscale (section_sdata [el_name ]):
113- mf_element = sd .get_pyramid_levels (section_sdata [el_name ], n = multiscale_level )
125+ if section_n is not None :
126+ save_bigwarp_folder = bigwarp_projects_folder / f'section_{ section_n } '
127+ save_bigwarp_folder .mkdir (exist_ok = True , parents = True )
128+ out_path = None
129+ else :
130+ save_bigwarp_folder = bigwarp_projects_folder
131+ save_bigwarp_folder .mkdir (exist_ok = True , parents = True )
132+ if not isinstance (sdata , sd .SpatialData ):
133+ sdata = sd .read_zarr (sdata )
134+ if microns_coord_sys_name not in sdata .coordinate_systems :
135+ sdata = add_micron_coord_sys (sdata )
136+ if _is_multiscale (sdata [el_name ]):
137+ mf_element = sd .get_pyramid_levels (sdata [el_name ], n = multiscale_level )
114138 else :
115- mf_element = section_sdata [el_name ]
139+ mf_element = sdata [el_name ]
116140 mf_element = _rename_channel_coord (mf_element )
117141 if subset_channels == 'all' :
118142 subset_channels = mf_element .coords ['c' ].values
119143
120- for ch in tqdm (subset_channels , desc = f'Extracting channels for section { section_n } ' ):
121- out_path = section_bigwarp_folder / f'{ ch } .tif'
144+ for ch in tqdm (subset_channels , desc = f'Extracting channels' ):
145+ out_path = save_bigwarp_folder / f'{ ch } .tif'
122146 ch_el = mf_element .sel (c = ch )
123147 dims = ch_el .dims
124148 microns_tf = get_transformation (ch_el , to_coordinate_system = microns_coord_sys_name )
@@ -139,7 +163,6 @@ def extract_bigwarp_images_for_section(section_sdata,
139163 pixel_size_yx = None
140164 print (f' Warning: unhandled transform type { type (microns_tf )} for channel { ch } , skipping calibration' )
141165
142-
143166 arr = ch_el .data .compute ()
144167 if np .issubdtype (np .dtype (dtype ), np .integer ) and normalize :
145168 finite = arr [np .isfinite (arr )]
@@ -168,21 +191,24 @@ def extract_bigwarp_images_for_section(section_sdata,
168191 tifffile .imwrite (str (out_path ), arr , ** kwargs )
169192
170193 if return_sdata :
171- return section_sdata
194+ return sdata
172195
173- def extract_bigwarp_label_for_section (
196+ def extract_bigwarp_labels (
174197 labels_el ,
175198 labels_name ,
176- section_n ,
177199 bigwarp_projects_folder ,
200+ section_n = None ,
178201 microns_coord_sys_name = 'microns' ,
179202 dtype = 'uint8' ,
180203 binary = True ,
181204 z_step_um = None ,
182205 resunit = 'cm'
183206):
184- section_bigwarp_folder = bigwarp_projects_folder / f'section_{ section_n } '
185- out_path = section_bigwarp_folder / f'{ labels_name } .tif'
207+ if section_n is None :
208+ save_bigwarp_folder = bigwarp_projects_folder
209+ else :
210+ save_bigwarp_folder = bigwarp_projects_folder / f'section_{ section_n } '
211+ out_path = save_bigwarp_folder / f'{ labels_name } .tif'
186212
187213 # Get the labels DataArray — handle SpatialData objects and raw DataArrays
188214 if isinstance (labels_el , sd .SpatialData ):
@@ -231,6 +257,6 @@ def extract_bigwarp_label_for_section(
231257 kwargs ['resolution' ] = resolution_um if resunit == 'um' else resolution_cm
232258 kwargs ['resolutionunit' ] = tifffile .RESUNIT .MICROMETER if resunit == 'um' else tifffile .RESUNIT .CENTIMETER
233259
234- section_bigwarp_folder .mkdir (exist_ok = True , parents = True )
260+ save_bigwarp_folder .mkdir (exist_ok = True , parents = True )
235261 tifffile .imwrite (str (out_path ), arr , ** kwargs )
236262 print (f' Wrote: { out_path .name } shape={ arr .shape } binary={ binary } ' )
0 commit comments