|
11 | 11 | _update_previous_dim_factors, |
12 | 12 | _get_block, |
13 | 13 | _spatial_dims, |
| 14 | + _next_scale_metadata, |
| 15 | + _next_block_shape, |
14 | 16 | ) |
15 | 17 |
|
16 | 18 | _image_dims: Tuple[str, str, str, str] = ("x", "y", "z", "t") |
@@ -142,47 +144,47 @@ def _downsample_itk_bin_shrink( |
142 | 144 | ) |
143 | 145 | previous_image = _align_chunks(previous_image, default_chunks, dim_factors) |
144 | 146 |
|
| 147 | + translation, scale = _next_scale_metadata( |
| 148 | + previous_image, dim_factors, spatial_dims |
| 149 | + ) |
| 150 | + |
| 151 | + # Blocks 0, ..., N-2 have the same shape |
| 152 | + block_0_input = _get_block(previous_image, 0) |
| 153 | + next_block_0_shape = _next_block_shape( |
| 154 | + previous_image, dim_factors, spatial_dims, block_0_input |
| 155 | + ) |
| 156 | + block_0_size = [] |
| 157 | + for dim in spatial_dims: |
| 158 | + if dim in previous_image.dims: |
| 159 | + block_0_size.append(block_0_input.shape[previous_image.dims.index(dim)]) |
| 160 | + else: |
| 161 | + block_0_size.append(1) |
| 162 | + block_0_size.reverse() |
| 163 | + |
| 164 | + # Block N-1 may be smaller than preceding blocks |
| 165 | + block_neg1_input = _get_block(previous_image, -1) |
| 166 | + next_block_neg1_shape = _next_block_shape( |
| 167 | + previous_image, dim_factors, spatial_dims, block_neg1_input |
| 168 | + ) |
| 169 | + |
145 | 170 | shrink_factors = [dim_factors[sd] for sd in spatial_dims] |
146 | 171 |
|
147 | | - block_0 = _get_block(previous_image, 0) |
| 172 | + dtype = block_0_input.dtype |
148 | 173 |
|
149 | | - # For consistency for now, do not utilize direction until there is standardized support for |
150 | | - # direction cosines / orientation in OME-NGFF |
151 | | - # block_0.attrs.pop("direction", None) |
152 | | - if "c" in previous_image.dims: |
153 | | - raise ValueError( |
154 | | - "Downsampling with ITK BinShrinkImageFilter does not support channel dimension 'c'. " |
155 | | - "Use ITK Gaussian downsampling instead." |
156 | | - ) |
157 | | - block_input = itk.image_from_array(np.ones_like(block_0)) |
158 | | - spacing = [previous_image.scale[d] for d in spatial_dims] |
159 | | - block_input.SetSpacing(spacing) |
160 | | - origin = [previous_image.translation[d] for d in spatial_dims] |
161 | | - block_input.SetOrigin(origin) |
162 | | - filt = itk.BinShrinkImageFilter.New(block_input, shrink_factors=shrink_factors) |
163 | | - filt.UpdateOutputInformation() |
164 | | - block_output = filt.GetOutput() |
165 | | - scale = {_image_dims[i]: s for (i, s) in enumerate(block_output.GetSpacing())} |
166 | | - translation = { |
167 | | - _image_dims[i]: s for (i, s) in enumerate(block_output.GetOrigin()) |
168 | | - } |
169 | | - dtype = block_output.dtype |
170 | 174 | output_chunks = list(previous_image.data.chunks) |
| 175 | + output_chunks_start = 0 |
| 176 | + while previous_image.dims[output_chunks_start] not in _spatial_dims: |
| 177 | + output_chunks_start += 1 |
| 178 | + output_chunks = output_chunks[output_chunks_start:] |
| 179 | + next_block_0_shape = next_block_0_shape[output_chunks_start:] |
171 | 180 | for i, c in enumerate(output_chunks): |
172 | 181 | output_chunks[i] = [ |
173 | | - block_output.shape[i], |
| 182 | + next_block_0_shape[i], |
174 | 183 | ] * len(c) |
175 | 184 |
|
176 | | - block_neg1 = _get_block(previous_image, -1) |
177 | | - # block_neg1.attrs.pop("direction", None) |
178 | | - block_input = itk.image_from_array(np.ones_like(block_neg1)) |
179 | | - block_input.SetSpacing(spacing) |
180 | | - block_input.SetOrigin(origin) |
181 | | - filt = itk.BinShrinkImageFilter.New(block_input, shrink_factors=shrink_factors) |
182 | | - filt.UpdateOutputInformation() |
183 | | - block_output = filt.GetOutput() |
| 185 | + next_block_neg1_shape = next_block_neg1_shape[output_chunks_start:] |
184 | 186 | for i in range(len(output_chunks)): |
185 | | - output_chunks[i][-1] = block_output.shape[i] |
| 187 | + output_chunks[i][-1] = next_block_neg1_shape[i] |
186 | 188 | output_chunks[i] = tuple(output_chunks[i]) |
187 | 189 | output_chunks = tuple(output_chunks) |
188 | 190 |
|
|
0 commit comments