Skip to content

Commit 65c9ddb

Browse files
committed
Fix tensorstore empty array handling
- Add validation for empty arrays in _save_transformed before tensorstore write - Skip write operations for empty arrays with warning messages - Add comprehensive error handling with detailed diagnostics for tensorstore failures - Improve error messages to include array shapes, sizes, and tensorstore details This resolves the ValueError: Error aligning dimensions issue when empty arrays are passed to tensorstore write operations.
1 parent ffc8a9d commit 65c9ddb

1 file changed

Lines changed: 34 additions & 3 deletions

File tree

iohub/ngff/utils.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,45 @@ def _save_transformed(
195195
output_channel_indices: list[int] | slice,
196196
output_time_indices: int | list[int],
197197
) -> None:
198+
# Add validation for empty arrays
199+
if transformed is not None:
200+
if isinstance(transformed, list):
201+
for i, t in enumerate(transformed):
202+
if hasattr(t, 'size') and t.size == 0:
203+
click.echo(f"Warning: Empty array found at time index {i}, shape: {t.shape}")
204+
click.echo(f"Skipping write for position: {output_position_path}")
205+
return
206+
elif hasattr(transformed, 'size') and transformed.size == 0:
207+
click.echo(f"Warning: Empty transformed array, shape: {transformed.shape}")
208+
click.echo(f"Skipping write for position: {output_position_path}")
209+
return
210+
198211
# NOTE: use tensorstore due to zarr-python#3221
199212
with open_ome_zarr(
200213
output_position_path, layout="fov", mode="r+"
201214
) as output_dataset:
202215
ts = output_dataset.data.tensorstore(concurrency=4)
203-
ts.oindex[output_time_indices, output_channel_indices].write(
204-
transformed
205-
).result()
216+
217+
try:
218+
ts.oindex[output_time_indices, output_channel_indices].write(
219+
transformed
220+
).result()
221+
except ValueError as e:
222+
click.echo(f"ERROR: Failed to write to {output_position_path}")
223+
click.echo(f"Output time indices: {output_time_indices}")
224+
click.echo(f"Output channel indices: {output_channel_indices}")
225+
if transformed is not None:
226+
if isinstance(transformed, list):
227+
click.echo(f"Transformed shapes: {[t.shape if hasattr(t, 'shape') else 'No shape attr' for t in transformed]}")
228+
click.echo(f"Transformed sizes: {[t.size if hasattr(t, 'size') else 'No size attr' for t in transformed]}")
229+
else:
230+
click.echo(f"Transformed shape: {transformed.shape if hasattr(transformed, 'shape') else 'No shape attr'}")
231+
click.echo(f"Transformed size: {transformed.size if hasattr(transformed, 'size') else 'No size attr'}")
232+
else:
233+
click.echo("Transformed data is None")
234+
click.echo(f"TensorStore error: {str(e)}")
235+
raise e
236+
206237
# NOTE: explicit GC due to tensorstore#223
207238
del ts
208239

0 commit comments

Comments
 (0)