Coverage for src/bioimageio/core/tensor.py: 79%

555 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 09:46 +0000

1from __future__ import annotations 

2 

3import collections.abc 

4from collections.abc import Callable, Collection, Hashable, Iterator, Mapping, Sequence 

5from dataclasses import dataclass 

6from itertools import permutations 

7from typing import ( 

8 Any, 

9 Literal, 

10 Protocol, 

11 TypeAlias, 

12 Union, 

13 cast, 

14 get_args, 

15) 

16 

17import dask.array as da 

18import numpy as np 

19import pandas as pd # pyright: ignore[reportMissingTypeStubs] 

20import xarray as xr 

21from loguru import logger 

22from numpy.typing import DTypeLike, NDArray 

23from typing_extensions import Self, assert_never 

24 

25from bioimageio.spec.model import v0_5 

26from bioimageio.spec.model.v0_5 import SpaceUnit, TimeUnit 

27 

28from ._magic_tensor_ops import MagicTensorOpsMixin 

29from ._utils.color import get_default_channel_colors 

30from ._utils.type_guards import is_dict, is_mapping, is_sequence 

31from ._utils.type_hints import cast_and_assert 

32from .axis import ( 

33 BATCH_AXIS_ID, 

34 AxisId, 

35 AxisInfo, 

36 AxisLike, 

37 PerAxis, 

38 axis_id_from_axis_like, 

39) 

40from .common import ( 

41 CropWhere, 

42 DTypeStr, 

43 Halo, 

44 HaloLike, 

45 MemberId, 

46 PadMode, 

47 PadWhere, 

48 PadWidth, 

49 PadWidthLike, 

50 PerMember, 

51 QuantileMethod, 

52 SliceInfo, 

53 resolve_pad_mode, 

54) 

55 

56MultiIndex = pd.MultiIndex 

57 

58Array: TypeAlias = NDArray[Any] | da.Array 

59 

60 

61class _SupportsArray(Protocol): 

62 def __array__(self) -> NDArray[Any]: ... 

63 

64 

65@dataclass 

66class TensorMetaData: 

67 """Metadata for a tensor""" 

68 

69 name: str | None 

70 channel_names: list[str] | None 

71 physical_scale: PerAxis[float | None] | None 

72 physical_scale_unit: PerAxis[str | None] | None 

73 

74 

75# TODO: complete docstrings 

76# TODO: in the long run---with improved typing in xarray---we should probably replace `Tensor` with xr.DataArray 

77class Tensor(MagicTensorOpsMixin): 

78 """A wrapper around an xr.DataArray for better integration with bioimageio.spec 

79 and improved type annotations.""" 

80 

81 _Compatible: TypeAlias = Union[ 

82 "Tensor", 

83 xr.DataArray, 

84 xr.Variable, 

85 _SupportsArray, 

86 Sequence[Any], 

87 "np.generic[Any]", 

88 ] 

89 

90 def __init__( 

91 self, 

92 array: Array | xr.DataArray, 

93 dims: Sequence[AxisId | AxisLike], 

94 ) -> None: 

95 super().__init__() 

96 axes = tuple(axis_id_from_axis_like(a) for a in dims) 

97 if isinstance(array, xr.DataArray): 

98 self._data: xr.DataArray = array.transpose(*axes) 

99 assert isinstance(self._data, xr.DataArray) 

100 else: 

101 self._data = xr.DataArray(array, dims=axes) 

102 

103 def __repr__(self) -> str: 

104 return f"<Tensor {self._data!r}>" 

105 

106 def __array__(self, dtype: DTypeLike | None = None): 

107 return np.asarray(self._data, dtype=dtype) 

108 

109 def __getitem__( 

110 self, 

111 key: SliceInfo 

112 | slice 

113 | int 

114 | PerAxis[SliceInfo | slice | int] 

115 | Tensor 

116 | xr.DataArray, 

117 ) -> Self: 

118 if isinstance(key, SliceInfo): 118 ↛ 119line 118 didn't jump to line 119 because the condition on line 118 was never true

119 key = slice(key.start, key.stop) 

120 elif isinstance(key, collections.abc.Mapping): 

121 key = { 

122 a: s 

123 if isinstance(s, int) 

124 else s 

125 if isinstance(s, slice) 

126 else slice(s.start, s.stop) 

127 for a, s in key.items() 

128 } 

129 elif isinstance(key, Tensor): 

130 key = key._data 

131 

132 selected = cast_and_assert(xr.DataArray, self._data[key]) 

133 return self.__class__.from_xarray(selected) 

134 

135 def __setitem__( 

136 self, 

137 key: PerAxis[SliceInfo | slice] | Tensor | xr.DataArray, 

138 value: Tensor | xr.DataArray | float, 

139 ) -> None: 

140 if isinstance(key, Tensor): 

141 key = key._data 

142 elif isinstance(key, xr.DataArray): 

143 pass 

144 else: 

145 key = { 

146 a: s if isinstance(s, slice) else slice(s.start, s.stop) 

147 for a, s in key.items() 

148 } 

149 

150 if isinstance(value, Tensor): 

151 value = value._data 

152 

153 self._data[key] = value 

154 

155 def __len__(self) -> int: 

156 return len(self.data) 

157 

158 def _iter(self: Any) -> Iterator[Any]: 

159 for n in range(len(self)): 

160 yield self[n] 

161 

162 def __iter__(self: Any) -> Iterator[Any]: 

163 if self.ndim == 0: 163 ↛ 164line 163 didn't jump to line 164 because the condition on line 163 was never true

164 raise TypeError("iteration over a 0-d array") 

165 return self._iter() 

166 

167 def _binary_op( 

168 self, 

169 other: _Compatible, 

170 f: Callable[[Any, Any], Any], 

171 reflexive: bool = False, 

172 ) -> Self: 

173 data = cast_and_assert( 

174 xr.DataArray, 

175 self._data._binary_op( # pyright: ignore[reportPrivateUsage] 

176 (other._data if isinstance(other, Tensor) else other), 

177 f, 

178 reflexive, 

179 ), 

180 ) 

181 data.attrs.update(self._data.attrs) 

182 return self.__class__.from_xarray(data) 

183 

184 def get_metadata(self) -> TensorMetaData: 

185 """Return the tensor's supported metadata as a typed snapshot.""" 

186 return TensorMetaData( 

187 name=self.name, 

188 channel_names=self.channel_names, 

189 physical_scale=self.get_physical_scale(), 

190 physical_scale_unit=self.get_physical_scale_unit(), 

191 ) 

192 

193 def set_metadata(self, metadata: TensorMetaData) -> None: 

194 """Apply non-None metadata fields using the corresponding setters.""" 

195 self.name = metadata.name 

196 self.channel_names = metadata.channel_names 

197 self.set_physical_scale(metadata.physical_scale) 

198 self.set_physical_scale_unit(metadata.physical_scale_unit) 

199 

200 def get_physical_scale(self) -> PerAxis[float]: 

201 """Return the physical scale of each axis in units (see `get_physical_scale_unit`).""" 

202 _scale = self._data.attrs.get("physical_scale", None) 

203 scale: Mapping[Any, Any] = ( 

204 _scale if _scale is not None and is_mapping(_scale) else {} 

205 ) 

206 del _scale 

207 return { 

208 a if isinstance(a, AxisId) else AxisId(a): 1.0 

209 if (scale_a := scale.get(a)) is None 

210 else float(scale_a) 

211 for a in self.dims 

212 } 

213 

214 def set_physical_scale(self, scale: PerAxis[float | None] | None) -> None: 

215 """Set the physical scale of each axis in units (see `set_physical_scale_unit`).""" 

216 if scale is None: 

217 self._data.attrs.pop("physical_scale", None) 

218 else: 

219 if unknown_axes := [d for d in scale if d not in self.dims]: 

220 raise ValueError( 

221 f"Cannot set physical scale for axes {unknown_axes} not in tensor axes {self.dims}" 

222 ) 

223 

224 self._data.attrs["physical_scale"] = { 

225 d: 1.0 if (s := scale.get(d)) is None else float(s) for d in self.dims 

226 } 

227 

228 def get_physical_scale_unit(self) -> PerAxis[SpaceUnit | TimeUnit | str]: 

229 """Return the physical unit of each axis.""" 

230 _unit = self._data.attrs.get("physical_scale_unit", None) 

231 unit: Mapping[Any, Any] = ( 

232 _unit if _unit is not None and is_mapping(_unit) else {} 

233 ) 

234 del _unit 

235 return {d: "" if (u := unit.get(d)) is None else str(u) for d in self.dims} 

236 

237 def set_physical_scale_unit( 

238 self, unit: PerAxis[SpaceUnit | TimeUnit | str | None] | None 

239 ) -> None: 

240 """Set the physical unit of each axis.""" 

241 if unit is None: 

242 self._data.attrs.pop("physical_scale_unit", None) 

243 else: 

244 if unknown_axes := [d for d in unit if d not in self.dims]: 

245 raise ValueError( 

246 f"Cannot set physical scale unit for axes {unknown_axes} not in tensor axes {self.dims}" 

247 ) 

248 self._data.attrs["physical_scale_unit"] = { 

249 d: "" if (u := unit.get(d)) is None else str(u) for d in self.dims 

250 } 

251 

252 @property 

253 def channel_names(self) -> list[str] | None: 

254 """Return the channel names if available, otherwise None.""" 

255 

256 names = self._data.attrs.get("channel_names", None) 

257 if names is None or not is_sequence(names): 

258 return None 

259 

260 if len(names) != self.sizes.get(AxisId("channel"), 0): 

261 logger.warning( 

262 "Number of channel names ({}) does not match number of channels ({})", 

263 len(names), 

264 self.sizes.get(AxisId("channel"), 0), 

265 ) 

266 return None 

267 

268 return [str(name) for name in names] 

269 

270 @channel_names.setter 

271 def channel_names(self, names: list[str] | None) -> None: 

272 """Set the channel names.""" 

273 if names is not None and len(names) != self.sizes.get(AxisId("channel"), 0): 

274 logger.warning( 

275 "Number of channel names ({}) does not match number of channels ({})", 

276 len(names), 

277 self.sizes.get(AxisId("channel"), 0), 

278 ) 

279 self._data.attrs["channel_names"] = names 

280 

281 @property 

282 def channel_colors(self) -> list[str] | None: 

283 """Return channel colors .""" 

284 

285 n_channels = self.sizes.get(AxisId("channel"), 0) 

286 if n_channels == 0: 

287 return None 

288 

289 colors = self._data.attrs.get("channel_colors", None) 

290 if colors is None: 290 ↛ 292line 290 didn't jump to line 292 because the condition on line 290 was always true

291 pass 

292 elif not is_sequence(colors): 

293 colors = None 

294 

295 if colors is not None and len(colors) < n_channels: 295 ↛ 296line 295 didn't jump to line 296 because the condition on line 295 was never true

296 logger.warning( 

297 "Overwriting existing channel colors with default colors due to too few channel colors: {} colors for {} channels", 

298 len(colors), 

299 n_channels, 

300 ) 

301 colors = None 

302 

303 if colors is None: 303 ↛ 308line 303 didn't jump to line 308 because the condition on line 303 was always true

304 default_channel_colors = get_default_channel_colors(n_channels) 

305 self.channel_colors = default_channel_colors 

306 return default_channel_colors 

307 else: 

308 return [str(c) for c in colors[:n_channels]] 

309 

310 @channel_colors.setter 

311 def channel_colors(self, colors: list[str] | None) -> None: 

312 """Set the channel colors.""" 

313 n_channels = self.sizes.get(AxisId("channel"), 0) 

314 if colors is not None and len(colors) < n_channels: 314 ↛ 315line 314 didn't jump to line 315 because the condition on line 314 was never true

315 logger.warning( 

316 "Number of channel colors ({}) lower than number of channels ({})", 

317 len(colors), 

318 n_channels, 

319 ) 

320 self._data.attrs["channel_colors"] = colors 

321 

322 @property 

323 def name(self) -> str | None: 

324 """Return the name of the tensor if available, otherwise None.""" 

325 name = self._data.attrs.get("name") 

326 return None if name is None else str(name) 

327 

328 @name.setter 

329 def name(self, name: str | None) -> None: 

330 """Set the name of the tensor.""" 

331 self._data.attrs["name"] = name 

332 

333 def squeeze(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: 

334 """Return a new tensor with squeezed data.""" 

335 squeezed = cast_and_assert(xr.DataArray, self._data.squeeze(dim=dim)) 

336 return self.__class__.from_xarray(squeezed) 

337 

338 def _inplace_binary_op( 

339 self, 

340 other: _Compatible, 

341 f: Callable[[Any, Any], Any], 

342 ) -> Self: 

343 _ = cast( 

344 Any, 

345 self._data._inplace_binary_op( # pyright: ignore[reportPrivateUsage] 

346 other._data if isinstance(other, Tensor) else other, 

347 f, 

348 ), 

349 ) 

350 return self 

351 

352 def _unary_op(self, f: Callable[[Any], Any], *args: Any, **kwargs: Any) -> Self: 

353 data = cast_and_assert( 

354 xr.DataArray, 

355 self._data._unary_op( # pyright: ignore[reportPrivateUsage] 

356 f, *args, **kwargs 

357 ), 

358 ) 

359 data.attrs.update(self._data.attrs) 

360 return self.__class__.from_xarray(data) 

361 

362 @classmethod 

363 def from_xarray(cls, data_array: xr.DataArray) -> Self: 

364 """create a `Tensor` from an xarray data array 

365 

366 note for internal use: this factory method is round-trip save 

367 for any `Tensor`'s `data` property (an xarray.DataArray). 

368 """ 

369 dims = [AxisId(d) for d in data_array.dims] 

370 dim_map = {d: a for d, a in zip(data_array.dims, dims) if d != a} 

371 if dim_map: 

372 data_array = cast_and_assert(xr.DataArray, data_array.rename(dim_map)) 

373 

374 return cls(array=data_array, dims=dims) 

375 

376 @classmethod 

377 def from_numpy( 

378 cls, 

379 array: NDArray[Any], 

380 *, 

381 dims: AxisLike | Sequence[AxisLike] | None, 

382 ) -> Self: 

383 return cls.from_array(array, dims=dims) 

384 

385 @classmethod 

386 def from_array( 

387 cls, 

388 array: Array, 

389 *, 

390 dims: AxisLike | Sequence[AxisLike] | None, 

391 ) -> Self: 

392 """create a `Tensor` from a numpy array 

393 

394 Args: 

395 array: the nd numpy array 

396 dims: A description of the array's axes. 

397 If None axes are guessed (which might fail and raise a ValueError.) 

398 If dims do not match array shape, permutations and singleton dimensions are tried to find a match. 

399 Raises: 

400 ValueError: if `dims` is None and dims guessing fails. 

401 """ 

402 

403 if dims is None: 

404 return cls._interprete_array_wo_known_axes(array) 

405 elif isinstance(dims, collections.abc.Sequence): 405 ↛ 408line 405 didn't jump to line 408 because the condition on line 405 was always true

406 dim_seq = list(dims) 

407 else: 

408 dim_seq = [dims] 

409 

410 axis_infos = [AxisInfo.create(a) for a in dim_seq] 

411 original_shape = tuple(array.shape) 

412 

413 successful_view = _get_array_view(array, axis_infos) 

414 if successful_view is None: 414 ↛ 415line 414 didn't jump to line 415 because the condition on line 414 was never true

415 dims_formatted = "".join(f"\n{d}" for d in dim_seq) 

416 raise ValueError( 

417 f"Array shape {original_shape} does not map to axes: {dims_formatted}" 

418 ) 

419 

420 return cls(successful_view, dims=tuple(a.id for a in axis_infos)) 

421 

422 @property 

423 def data(self): 

424 return self._data 

425 

426 @property 

427 def dims(self): # TODO: rename to `axes`? 

428 """Tuple of dimension names associated with this tensor.""" 

429 return cast(tuple[AxisId, ...], self._data.dims) 

430 

431 @property 

432 def dtype(self) -> DTypeStr: 

433 dt = str(self.data.dtype) 

434 assert dt in get_args(DTypeStr) 

435 return dt # pyright: ignore[reportReturnType] 

436 

437 @property 

438 def ndim(self): 

439 """Number of tensor dimensions.""" 

440 return self._data.ndim 

441 

442 @property 

443 def shape(self): 

444 """Tuple of tensor axes lengths""" 

445 return self._data.shape 

446 

447 @property 

448 def shape_tuple(self): 

449 """Tuple of tensor axes lengths""" 

450 return self._data.shape 

451 

452 @property 

453 def size(self): 

454 """Number of elements in the tensor. 

455 

456 Equal to math.prod(tensor.shape), i.e., the product of the tensors’ dimensions. 

457 """ 

458 return self._data.size 

459 

460 @property 

461 def sizes(self): 

462 """Ordered, immutable mapping from axis ids to axis lengths.""" 

463 return cast(Mapping[AxisId, int], self.data.sizes) 

464 

465 @property 

466 def tagged_shape(self): 

467 """(alias for `sizes`) Ordered, immutable mapping from axis ids to lengths.""" 

468 return self.sizes 

469 

470 def to_numpy(self) -> NDArray[Any]: 

471 """Return the data of this tensor as a numpy array. 

472 

473 Use `as_numpy()` to get a new `Tensor` instance with the data as a numpy array (wrapped in an xarray DataArray).""" 

474 return self.data.to_numpy() 

475 

476 def as_numpy(self) -> Self: 

477 """Return a new Tensor instance with this tensor's data as a numpy array (wrapped in an xarray DataArray). 

478 Use `to_numpy()` to get the raw numpy array.""" 

479 return self.from_xarray(cast_and_assert(xr.DataArray, self.data.as_numpy())) 

480 

481 def as_dask(self) -> Self: 

482 """Return a Dask-backed tensor without evaluating its data. 

483 

484 NumPy data is chunked automatically. An existing Dask-backed tensor is 

485 returned unchanged, preserving its graph and chunks. 

486 """ 

487 if isinstance(self.data.data, da.Array): 

488 return self 

489 

490 return self.from_xarray( 

491 cast_and_assert( 

492 xr.DataArray, 

493 self.data.copy(data=da.from_array(self.data.data, chunks="auto")), 

494 ) 

495 ) 

496 

497 def crop_halo(self, halo: PerAxis[HaloLike] | None) -> Self: 

498 """helper to crop a halo from the tensor""" 

499 if not halo: 

500 return self 

501 

502 return self[ 

503 { 

504 d: slice(h[0], self.tagged_shape[d] - h[1]) 

505 if (h := Halo.create(halo.get(d, 0))) != Halo(0, 0) 

506 else slice(None) 

507 for d in self.dims 

508 } 

509 ] 

510 

511 def chunk( 

512 self, 

513 chunk_sizes: PerAxis[int], 

514 halo: PerAxis[HaloLike] | None = None, 

515 pad_mode: PadMode | Literal["none"] = "none", 

516 ) -> Self: 

517 """Return a tensor with the same data but chunked according to `chunk_sizes`.""" 

518 if da is None: 518 ↛ 519line 518 didn't jump to line 519 because the condition on line 518 was never true

519 raise ImportError( 

520 "Dask is not installed. Please install dask to use the chunking functionality." 

521 ) 

522 

523 if pad_mode != "none" and halo is not None: 523 ↛ 524line 523 didn't jump to line 524 because the condition on line 523 was never true

524 data = self.pad(pad_width=halo, mode=pad_mode).data 

525 else: 

526 data = self.data 

527 

528 chunked = cast_and_assert(xr.DataArray, data.chunk(chunk_sizes)) 

529 if halo is not None: 

530 for a, h in halo.items(): 

531 if a not in self.dims: 531 ↛ 532line 531 didn't jump to line 532 because the condition on line 531 was never true

532 raise ValueError(f"Axis {a} not in tensor dimensions {self.dims}") 

533 

534 if isinstance(h, int): 534 ↛ 539line 534 didn't jump to line 539 because the condition on line 534 was always true

535 if h < 0: 535 ↛ 536line 535 didn't jump to line 536 because the condition on line 535 was never true

536 raise ValueError( 

537 f"Halo size for axis {a} must be non-negative, got {h}" 

538 ) 

539 elif isinstance(h, tuple): 

540 if len(h) != 2 or any(x < 0 for x in h): 

541 raise ValueError( 

542 f"Halo size for axis {a} must be a non-negative integer or a tuple of two non-negative integers, got {h}" 

543 ) 

544 

545 if len(set(h)) != 1 and pad_mode != "none": 

546 raise ValueError( 

547 f"Halo size for chunking with pad_mode {pad_mode} must be symmetric, got {h}" 

548 ) 

549 

550 else: 

551 raise TypeError( 

552 f"Halo size for axis {a} must be a non-negative integer or a tuple of two non-negative integers, got {h}" 

553 ) 

554 

555 depth = [ 

556 h 

557 if isinstance((h := halo.get(d, 0)), int) 

558 else h[0] 

559 if len(set(h)) == 1 

560 else {"-": h[0], "+": h[1]} 

561 for d in self.dims 

562 ] 

563 

564 enlarged_chunk_shape = tuple( 

565 chunk_sizes.get(a, self.tagged_shape[a]) 

566 + (2 * h if isinstance(h, int) else h["-"] + h["+"]) 

567 for a, h in zip(self.dims, depth) 

568 ) 

569 

570 def map_overlap_chunk(x: NDArray[Any]) -> NDArray[Any]: 

571 return x 

572 

573 overlapping = cast_and_assert( 

574 da.Array, 

575 da.map_overlap( 

576 map_overlap_chunk, 

577 chunked.data, 

578 depth=depth, 

579 boundary="none", # we explicitly pad above to avoid mapping `pad_mode` to `boundary` 

580 dtype=chunked.data.dtype, 

581 chunks=enlarged_chunk_shape, 

582 trim=False, 

583 allow_rechunk=False, 

584 # meta=np.array((), dtype=chunked.data.dtype), # possibly not a numpy array 

585 ), 

586 ) 

587 ret = self.__class__.from_array(overlapping, dims=self.dims) 

588 ret.data.attrs.update(self._data.attrs) 

589 if self.batch_multi_index is not None: 589 ↛ 590line 589 didn't jump to line 590 because the condition on line 589 was never true

590 ret = ret.assign_batch_multi_index(self.batch_multi_index) 

591 else: 

592 ret = self.__class__.from_xarray(chunked) 

593 

594 assert ret.name == self.name 

595 assert ret.channel_names == self.channel_names 

596 assert ret.channel_colors == self.channel_colors 

597 assert ( 

598 ret.batch_multi_index is None 

599 and self.batch_multi_index is None 

600 or (ret.batch_multi_index == self.batch_multi_index).all() 

601 ) 

602 return ret 

603 

604 def argmax(self) -> Mapping[AxisId, int]: 

605 ret = cast(dict[Hashable, Any], self._data.argmax(...)) 

606 assert is_dict(ret) 

607 return {cast(AxisId, k): cast(int, v.item()) for k, v in ret.items()} 

608 

609 def astype(self, dtype: DTypeStr, *, copy: bool = False): 

610 """Return tensor cast to `dtype` 

611 

612 note: if dtype is already satisfied copy if `copy`""" 

613 return self.__class__.from_xarray( 

614 cast_and_assert( 

615 xr.DataArray, self._data.astype(dtype, copy=copy, keep_attrs=True) 

616 ) 

617 ) 

618 

619 def clip(self, min: float | None = None, max: float | None = None): 

620 """Return a tensor whose values are limited to [min, max]. 

621 At least one of max or min must be given.""" 

622 return self.__class__.from_xarray( 

623 cast_and_assert(xr.DataArray, self._data.clip(min, max, keep_attrs=True)) 

624 ) 

625 

626 def crop_to( 

627 self, 

628 sizes: PerAxis[int], 

629 crop_where: CropWhere | PerAxis[CropWhere] = "left_and_right", 

630 ) -> Self: 

631 """crop to match `sizes`""" 

632 if isinstance(crop_where, str): 632 ↛ 635line 632 didn't jump to line 635 because the condition on line 632 was always true

633 crop_axis_where: PerAxis[CropWhere] = {a: crop_where for a in self.dims} 

634 else: 

635 crop_axis_where = crop_where 

636 

637 slices: dict[AxisId, SliceInfo] = {} 

638 

639 for a, s_is in self.sizes.items(): 

640 if a not in sizes or sizes[a] == s_is: 

641 pass 

642 elif sizes[a] > s_is: 642 ↛ 643line 642 didn't jump to line 643 because the condition on line 642 was never true

643 logger.warning( 

644 "Cannot crop axis {} of size {} to larger size {}", 

645 a, 

646 s_is, 

647 sizes[a], 

648 ) 

649 elif a not in crop_axis_where: 649 ↛ 650line 649 didn't jump to line 650 because the condition on line 649 was never true

650 raise ValueError( 

651 f"Don't know where to crop axis {a}, `crop_where`={crop_where}" 

652 ) 

653 else: 

654 crop_this_axis_where = crop_axis_where[a] 

655 if crop_this_axis_where == "left": 655 ↛ 656line 655 didn't jump to line 656 because the condition on line 655 was never true

656 slices[a] = SliceInfo(s_is - sizes[a], s_is) 

657 elif crop_this_axis_where == "right": 657 ↛ 658line 657 didn't jump to line 658 because the condition on line 657 was never true

658 slices[a] = SliceInfo(0, sizes[a]) 

659 elif crop_this_axis_where == "left_and_right": 

660 slices[a] = SliceInfo( 

661 start := (s_is - sizes[a]) // 2, sizes[a] + start 

662 ) 

663 else: 

664 assert_never(crop_this_axis_where) 

665 

666 return self[slices] 

667 

668 def expand_dims(self, dims: Sequence[AxisId] | PerAxis[int]) -> Self: 

669 return self.__class__.from_xarray( 

670 cast_and_assert(xr.DataArray, self._data.expand_dims(dims=dims)) 

671 ) 

672 

673 def item( 

674 self, 

675 key: None | SliceInfo | slice | int | PerAxis[SliceInfo | slice | int] = None, 

676 ): 

677 """Copy a tensor element to a standard Python scalar and return it.""" 

678 if key is None: 

679 ret = self._data.item() 

680 else: 

681 ret = self[key]._data.item() 

682 

683 assert isinstance(ret, (bool, float, int)) 

684 return ret 

685 

686 @classmethod 

687 def _from_reduced_xarray(cls, data: xr.DataArray) -> Self: 

688 tensor = cls.from_xarray(data) 

689 for key in ("physical_scale", "physical_scale_unit"): 

690 if (values := tensor.data.attrs.get(key)) is not None and is_mapping( 

691 values 

692 ): 

693 tensor.data.attrs[key] = { 

694 a: value for a, value in values.items() if a in tensor.dims 

695 } 

696 return tensor 

697 

698 def mean(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: 

699 return self.__class__._from_reduced_xarray( 

700 cast_and_assert(xr.DataArray, self._data.mean(dim=dim, keep_attrs=True)) 

701 ) 

702 

703 def pad( 

704 self, 

705 pad_width: PerAxis[PadWidthLike], 

706 mode: PadMode = "symmetric", 

707 ) -> Self: 

708 pad_width = {a: PadWidth.create(p) for a, p in pad_width.items()} 

709 mode_name, constant_value = resolve_pad_mode(mode) 

710 

711 padded_tensor = self.__class__.from_xarray( 

712 cast_and_assert( 

713 xr.DataArray, 

714 self._data.pad( 

715 pad_width=pad_width, 

716 mode=mode_name, 

717 constant_values=constant_value, 

718 keep_attrs=True, 

719 ), 

720 ) 

721 ) 

722 

723 if ( 

724 BATCH_AXIS_ID in self.dims 

725 # maybe future xarray versions will keep a MultiIndex when padding, but currently they don't 

726 and padded_tensor.batch_multi_index is not None 

727 and pad_width.get(BATCH_AXIS_ID, 0) in (0, (0, 0), PadWidth(0, 0)) 

728 and (idx := self.batch_multi_index) is not None 

729 ): 

730 # add multi-index removed by padding when batch axis is not padded 

731 padded_tensor = padded_tensor.assign_batch_multi_index(idx) 

732 

733 return padded_tensor 

734 

735 def pad_to( 

736 self, 

737 sizes: PerAxis[int], 

738 pad_where: PadWhere | PerAxis[PadWhere] = "left_and_right", 

739 mode: PadMode = "symmetric", 

740 ) -> Self: 

741 """pad `tensor` to match `sizes`""" 

742 if isinstance(pad_where, str): 742 ↛ 745line 742 didn't jump to line 745 because the condition on line 742 was always true

743 pad_axis_where: PerAxis[PadWhere] = {a: pad_where for a in self.dims} 

744 else: 

745 pad_axis_where = pad_where 

746 

747 pad_width: dict[AxisId, PadWidth] = {} 

748 for a, s_is in self.sizes.items(): 

749 if a not in sizes or sizes[a] == s_is: 

750 pad_width[a] = PadWidth(0, 0) 

751 elif s_is > sizes[a]: 751 ↛ 752line 751 didn't jump to line 752 because the condition on line 751 was never true

752 pad_width[a] = PadWidth(0, 0) 

753 logger.warning( 

754 "Cannot pad axis {} of size {} to smaller size {}", 

755 a, 

756 s_is, 

757 sizes[a], 

758 ) 

759 elif a not in pad_axis_where: 759 ↛ 760line 759 didn't jump to line 760 because the condition on line 759 was never true

760 raise ValueError( 

761 f"Don't know where to pad axis {a}, `pad_where`={pad_where}" 

762 ) 

763 else: 

764 pad_this_axis_where = pad_axis_where[a] 

765 d = sizes[a] - s_is 

766 if pad_this_axis_where == "left": 766 ↛ 767line 766 didn't jump to line 767 because the condition on line 766 was never true

767 pad_width[a] = PadWidth(d, 0) 

768 elif pad_this_axis_where == "right": 768 ↛ 769line 768 didn't jump to line 769 because the condition on line 768 was never true

769 pad_width[a] = PadWidth(0, d) 

770 elif pad_this_axis_where == "left_and_right": 

771 pad_width[a] = PadWidth(left := d // 2, d - left) 

772 else: 

773 assert_never(pad_this_axis_where) 

774 

775 return self.pad(pad_width, mode) 

776 

777 def quantile( 

778 self, 

779 q: float | Sequence[float], 

780 dim: AxisId | Sequence[AxisId] | None = None, 

781 method: QuantileMethod = "linear", 

782 ) -> Self: 

783 assert ( 

784 isinstance(q, (float, int)) 

785 and q >= 0.0 

786 or not isinstance(q, (float, int)) 

787 and all(qq >= 0.0 for qq in q) 

788 ) 

789 assert ( 

790 isinstance(q, (float, int)) 

791 and q <= 1.0 

792 or not isinstance(q, (float, int)) 

793 and all(qq <= 1.0 for qq in q) 

794 ) 

795 assert dim is None or ( 

796 (quantile_dim := AxisId("quantile")) != dim and quantile_dim not in set(dim) 

797 ) 

798 return self.__class__._from_reduced_xarray( 

799 cast_and_assert( 

800 xr.DataArray, 

801 self._data.quantile(q, dim=dim, method=method, keep_attrs=True), 

802 ) 

803 ) 

804 

805 def resize_to( 

806 self, 

807 sizes: PerAxis[int], 

808 *, 

809 pad_where: PadWhere | PerAxis[PadWhere] = "left_and_right", 

810 crop_where: CropWhere | PerAxis[CropWhere] = "left_and_right", 

811 pad_mode: PadMode = "symmetric", 

812 ): 

813 """return cropped/padded tensor with `sizes`""" 

814 crop_to_sizes: dict[AxisId, int] = {} 

815 pad_to_sizes: dict[AxisId, int] = {} 

816 new_axes = dict(sizes) 

817 for a, s_is in self.sizes.items(): 

818 a = AxisId(str(a)) 

819 _ = new_axes.pop(a, None) 

820 if a not in sizes or sizes[a] == s_is: 

821 pass 

822 elif s_is > sizes[a]: 

823 crop_to_sizes[a] = sizes[a] 

824 else: 

825 pad_to_sizes[a] = sizes[a] 

826 

827 tensor = self 

828 if crop_to_sizes: 

829 tensor = tensor.crop_to(crop_to_sizes, crop_where=crop_where) 

830 

831 if pad_to_sizes: 

832 tensor = tensor.pad_to(pad_to_sizes, pad_where=pad_where, mode=pad_mode) 

833 

834 if new_axes: 834 ↛ 835line 834 didn't jump to line 835 because the condition on line 834 was never true

835 tensor = tensor.expand_dims(new_axes) 

836 

837 return tensor 

838 

839 def std(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: 

840 return self.__class__._from_reduced_xarray( 

841 cast_and_assert(xr.DataArray, self._data.std(dim=dim, keep_attrs=True)) 

842 ) 

843 

844 def sum(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: 

845 """Reduce this Tensor's data by applying sum along some dimension(s).""" 

846 return self.__class__._from_reduced_xarray( 

847 cast_and_assert(xr.DataArray, self._data.sum(dim=dim, keep_attrs=True)) 

848 ) 

849 

850 @property 

851 def batch_multi_index(self) -> MultiIndex | None: 

852 """Return the batch multi-index if available, otherwise None.""" 

853 if BATCH_AXIS_ID not in self.dims: 

854 return None 

855 

856 idx = self._data.indexes.get(BATCH_AXIS_ID) # pyright: ignore[reportUnknownVariableType] 

857 if not isinstance(idx, MultiIndex): 

858 return None 

859 

860 return idx 

861 

862 def assign_batch_multi_index(self, multi_index: MultiIndex) -> Self: 

863 """Set the batch multi-index for this tensor. 

864 

865 Args: 

866 multi_index: The multi-index to set. 

867 """ 

868 if BATCH_AXIS_ID not in self.dims: 

869 raise ValueError( 

870 "Cannot set batch multi-index on a tensor without a 'batch' axis." 

871 ) 

872 

873 # # TODO: check if we really need to create/assign coordinates object 

874 # alternatively create new multi-index: self._data.set_xindex(multi_index.names) 

875 data = cast_and_assert( 

876 xr.DataArray, 

877 self._data.drop_vars( 

878 list(multi_index.names) + [BATCH_AXIS_ID], errors="ignore" 

879 ), 

880 ) 

881 mindex_coords = cast_and_assert( 

882 xr.Coordinates, 

883 xr.Coordinates.from_pandas_multiindex(multi_index, BATCH_AXIS_ID), 

884 ) 

885 data = cast_and_assert(xr.DataArray, data.assign_coords(mindex_coords)) 

886 return self.__class__.from_xarray(data) 

887 

888 def unstack_batch_multi_index( 

889 self, 

890 *, 

891 errors: Literal["raise", "warn", "ignore"] = "raise", 

892 unstacked_dim_order: Sequence[AxisId] | None = None, 

893 ) -> Self: 

894 """Unstack the batch multi-index of this tensor. 

895 

896 Returns: 

897 A new tensor with the batch multi-index unstacked into separate axes. 

898 """ 

899 if BATCH_AXIS_ID not in self.dims: 

900 error_msg = ( 

901 "Cannot unstack batch multi-index on a tensor without a 'batch' axis." 

902 ) 

903 if errors == "raise": 

904 raise ValueError(error_msg) 

905 elif errors == "warn": 905 ↛ 906line 905 didn't jump to line 906 because the condition on line 905 was never true

906 logger.warning(error_msg) 

907 return self 

908 elif errors == "ignore": 

909 return self 

910 else: 

911 assert_never(errors) 

912 

913 if not isinstance(self._data.indexes.get(BATCH_AXIS_ID), MultiIndex): 

914 error_msg = "Cannot unstack batch multi-index on a tensor whose 'batch' axis does not have a MultiIndex." 

915 if errors == "raise": 

916 raise ValueError(error_msg) 

917 elif errors == "warn": 917 ↛ 918line 917 didn't jump to line 918 because the condition on line 917 was never true

918 logger.warning(error_msg) 

919 return self 

920 elif errors == "ignore": 

921 return self 

922 else: 

923 assert_never(errors) 

924 

925 old_dims = self.dims 

926 array = cast_and_assert(xr.DataArray, self._data.unstack(BATCH_AXIS_ID)) 

927 added_dims = [ 

928 BATCH_AXIS_ID if d == "original_batch" else AxisId(d) 

929 for d in array.dims 

930 if d not in self._data.dims 

931 ] 

932 

933 if unstacked_dim_order is None: 

934 # restore expected axis order, replace batch dim with added dims 

935 new_dims: Sequence[AxisId] = [] 

936 for d in old_dims: 

937 if d in array.dims: 

938 new_dims.append(d) 

939 elif d == BATCH_AXIS_ID: 939 ↛ 942line 939 didn't jump to line 942 because the condition on line 939 was always true

940 new_dims.extend(added_dims) 

941 else: 

942 raise ValueError(f"Expected axis {d} not found in unstacked array.") 

943 else: 

944 new_dims = unstacked_dim_order 

945 

946 if AxisId("original_batch") in array.dims: 946 ↛ 951line 946 didn't jump to line 951 because the condition on line 946 was always true

947 array = cast_and_assert( 

948 xr.DataArray, array.rename({AxisId("original_batch"): BATCH_AXIS_ID}) 

949 ) 

950 

951 array = cast_and_assert(xr.DataArray, array.transpose(*new_dims)) 

952 return self.__class__.from_xarray(array) 

953 

954 def transpose( 

955 self, 

956 axes: Sequence[AxisLike], 

957 *, 

958 extra_dims: Literal[ 

959 "raise", "squeeze", "stack", "squeeze_or_stack" 

960 ] = "squeeze", 

961 missing_dims: Literal[ 

962 "raise", "expand", "unstack", "unstack_or_expand" 

963 ] = "unstack_or_expand", 

964 ) -> Self: 

965 """Return a transposed tensor, missing axes are expanded (if `unstack_missing_dims_from_batch` is False) or unstacked from batch (if `unstack_missing_dims_from_batch` is True), extra axes are stacked to batch (if `stack_extra_dims_to_batch` is True). Additional axes raise (if `stack_extra_dims_to_batch` is True). 

966 

967 Args: 

968 axes: The desired tensor axes 

969 extra_dims: 

970 Extra dimensions are any dimensions in the tensor that are not specified in `axes`. 

971 If "raise", any extra dimensions will raise an error. 

972 If "squeeze", any extra singleton dimensions will be squeezed, non-singleton dimensions will raise an error. 

973 If "stack", any extra dimensions will be stacked to the batch dimension. Such a stacked batch dimension then has a multi-index that can be unstacked using `Tensor.unstack_batch_multi_index()`. 

974 If "squeeze_or_stack", any extra singleton dimensions will be squeezed, non-singleton dimensions will be stacked to the batch dimension. 

975 missing_dims: 

976 Missing dimensions are any dimensions specified in `axes` that are not present in the tensor. 

977 If "raise", any missing dimensions will raise an error. 

978 If "expand", any missing dimensions will be added as singleton dimensions. 

979 If "unstack", any missing dimensions will be unstacked from the batch dimension. For this option a batch dimension with a multi-index must be present from previous stacking operations or assigned by `Tensor.assign_batch_multi_index()`. 

980 If "unstack_or_expand", any missing dimensions will be unstacked from the batch dimension if it has a multi-index, otherwise they will be added as singleton dimensions. 

981 """ 

982 transposed, _dim_order = self.transpose_and_return_unstacked_dim_order( 

983 axes, extra_dims=extra_dims, missing_dims=missing_dims 

984 ) 

985 return transposed 

986 

987 def transpose_and_return_unstacked_dim_order( 

988 self, 

989 axes: Sequence[AxisLike], 

990 *, 

991 extra_dims: Literal[ 

992 "raise", "squeeze", "stack", "squeeze_or_stack" 

993 ] = "squeeze", 

994 missing_dims: Literal[ 

995 "raise", "expand", "unstack", "unstack_or_expand" 

996 ] = "unstack_or_expand", 

997 ) -> tuple[Self, list[AxisId] | None]: 

998 axes = [axis_id_from_axis_like(a) for a in axes] 

999 array = self._data 

1000 

1001 unhandled_missing_dims = [a for a in axes if a not in self.dims] 

1002 if unhandled_missing_dims and missing_dims == "raise": 1002 ↛ 1003line 1002 didn't jump to line 1003 because the condition on line 1002 was never true

1003 raise ValueError(f"Found missing dimensions {unhandled_missing_dims}.") 

1004 

1005 unstack_error = None 

1006 if unhandled_missing_dims and missing_dims in ("unstack", "unstack_or_expand"): 

1007 lets_unstack = BATCH_AXIS_ID in array.dims 

1008 if not lets_unstack: 

1009 unstack_error = f"Missing dimensions {unhandled_missing_dims} found, but 'batch' axis is not in the tensor. Cannot unstack missing dimensions from batch." 

1010 if missing_dims == "unstack": 1010 ↛ 1011line 1010 didn't jump to line 1011 because the condition on line 1010 was never true

1011 raise ValueError(unstack_error) 

1012 

1013 if lets_unstack and not isinstance( 

1014 array.indexes.get(BATCH_AXIS_ID), MultiIndex 

1015 ): 

1016 lets_unstack = False 

1017 unstack_error = f"Missing dimensions {unhandled_missing_dims} found, but 'batch' axis does not have a MultiIndex. Cannot unstack missing dimensions from non-multi-index batch." 

1018 if missing_dims == "unstack": 

1019 raise ValueError(unstack_error) 

1020 else: 

1021 lets_unstack = False 

1022 

1023 if lets_unstack: 

1024 array = cast_and_assert(xr.DataArray, array.unstack(BATCH_AXIS_ID)) 

1025 

1026 if AxisId("original_batch") in array.dims: 1026 ↛ 1037line 1026 didn't jump to line 1037 because the condition on line 1026 was always true

1027 if BATCH_AXIS_ID in axes: 

1028 array = cast_and_assert( 

1029 xr.DataArray, 

1030 array.rename({AxisId("original_batch"): BATCH_AXIS_ID}), 

1031 ) 

1032 else: 

1033 array = cast_and_assert( 

1034 xr.DataArray, array.squeeze(AxisId("original_batch")) 

1035 ) 

1036 

1037 unhandled_missing_dims = [a for a in axes if a not in array.dims] 

1038 

1039 if unhandled_missing_dims and missing_dims in ("expand", "unstack_or_expand"): 

1040 array = cast_and_assert( 

1041 xr.DataArray, array.expand_dims(unhandled_missing_dims) 

1042 ) 

1043 unhandled_missing_dims = [] 

1044 

1045 if unhandled_missing_dims: 1045 ↛ 1046line 1045 didn't jump to line 1046 because the condition on line 1045 was never true

1046 if unstack_error is not None: 

1047 raise ValueError(unstack_error) 

1048 

1049 raise ValueError(f"Missing dimensions {unhandled_missing_dims}.") 

1050 

1051 assert isinstance(array, xr.DataArray) 

1052 unstacked_dim_order = None 

1053 unhandled_extra_dims = [a for a in array.dims if a not in axes] 

1054 

1055 if unhandled_extra_dims and extra_dims == "raise": 1055 ↛ 1056line 1055 didn't jump to line 1056 because the condition on line 1055 was never true

1056 raise ValueError(f"Found extra dimensions {unhandled_extra_dims}.") 

1057 

1058 if unhandled_extra_dims and extra_dims in ("squeeze", "squeeze_or_stack"): 

1059 for d in list(unhandled_extra_dims): 

1060 if array.sizes[d] == 1: 

1061 array = cast_and_assert(xr.DataArray, array.squeeze(d)) 

1062 assert isinstance(array, xr.DataArray) 

1063 unhandled_extra_dims.remove(d) 

1064 elif extra_dims == "squeeze": 1064 ↛ 1059line 1064 didn't jump to line 1059 because the condition on line 1064 was always true

1065 raise ValueError( 

1066 f"Extra dimension {d} found but stack_extra_dims_to_batch is False and the dimension is not a singleton." 

1067 ) 

1068 

1069 if unhandled_extra_dims and extra_dims in ("stack", "squeeze_or_stack"): 

1070 if BATCH_AXIS_ID not in axes: 

1071 raise ValueError( 

1072 f"Extra dimensions {unhandled_extra_dims} found but 'batch' axis is not in the desired axes {axes}." 

1073 + " Cannot stack extra dimensions to batch." 

1074 ) 

1075 

1076 unstacked_dim_order = [AxisId(d) for d in array.dims] 

1077 if BATCH_AXIS_ID in array.dims: 1077 ↛ 1084line 1077 didn't jump to line 1084 because the condition on line 1077 was always true

1078 array = cast_and_assert( 

1079 xr.DataArray, 

1080 array.rename({BATCH_AXIS_ID: AxisId("original_batch")}), 

1081 ) 

1082 unhandled_extra_dims.insert(0, AxisId("original_batch")) 

1083 

1084 array = cast_and_assert( 

1085 xr.DataArray, array.stack({BATCH_AXIS_ID: unhandled_extra_dims}) 

1086 ) 

1087 unhandled_extra_dims = [] 

1088 

1089 if unhandled_extra_dims: 1089 ↛ 1090line 1089 didn't jump to line 1090 because the condition on line 1089 was never true

1090 raise ValueError( 

1091 f"Non-singleton extra dimensions {unhandled_extra_dims} found, but `extra_dims` not in ('stack', 'squeeze_or_stack')." 

1092 ) 

1093 

1094 # transpose to the correct axis order 

1095 return self.__class__.from_xarray( 

1096 cast_and_assert(xr.DataArray, array.transpose(*axes)) 

1097 ), unstacked_dim_order 

1098 

1099 def var(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: 

1100 return self.__class__._from_reduced_xarray( 

1101 cast_and_assert(xr.DataArray, self._data.var(dim=dim, keep_attrs=True)) 

1102 ) 

1103 

1104 @classmethod 

1105 def _interprete_array_wo_known_axes(cls, array: Array) -> Self: 

1106 ndim = array.ndim 

1107 shape = [s if isinstance(s, int) else -1 for s in array.shape] 

1108 

1109 current_axes: list[v0_5.InputAxis] 

1110 if ndim == 2: 

1111 current_axes = [ 

1112 v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[0]), 

1113 v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[1]), 

1114 ] 

1115 elif ndim == 3 and any(s <= 10 for s in shape): 

1116 current_axes = [ 

1117 v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[1]), 

1118 v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[2]), 

1119 ] 

1120 current_axes.insert( 

1121 np.argmin(shape), 

1122 v0_5.ChannelAxis( 

1123 channel_names=[ 

1124 v0_5.Identifier(f"channel{i}") for i in range(min(shape)) 

1125 ] 

1126 ), 

1127 ) 

1128 elif ndim == 3: 

1129 current_axes = [ 

1130 v0_5.SpaceInputAxis(id=v0_5.AxisId("z"), size=shape[0]), 

1131 v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[1]), 

1132 v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[2]), 

1133 ] 

1134 elif ndim == 4: 1134 ↛ 1148line 1134 didn't jump to line 1148 because the condition on line 1134 was always true

1135 current_axes = [ 

1136 v0_5.SpaceInputAxis(id=v0_5.AxisId("z"), size=shape[1]), 

1137 v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[2]), 

1138 v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[3]), 

1139 ] 

1140 current_axes.insert( 

1141 np.argmin(shape), 

1142 v0_5.ChannelAxis( 

1143 channel_names=[ 

1144 v0_5.Identifier(f"channel{i}") for i in range(min(shape)) 

1145 ] 

1146 ), 

1147 ) 

1148 elif ndim == 5: 

1149 current_axes = [ 

1150 v0_5.TimeInputAxis(size=shape[1]), 

1151 v0_5.ChannelAxis( 

1152 channel_names=[ 

1153 v0_5.Identifier(f"channel{i}") for i in range(shape[1]) 

1154 ] 

1155 ), 

1156 v0_5.SpaceInputAxis(id=v0_5.AxisId("z"), size=shape[2]), 

1157 v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[3]), 

1158 v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[4]), 

1159 ] 

1160 else: 

1161 raise ValueError(f"Could not guess an axis mapping for {shape}") 

1162 

1163 dims = tuple(a.id for a in current_axes) 

1164 return cls(array, dims=dims) 

1165 

1166 

1167def restore_batch_multi_index( 

1168 inputs: Collection[Tensor | MultiIndex | None], outputs: PerMember[Tensor] 

1169) -> dict[MemberId, Tensor]: 

1170 """Restore any batch multi-index found in the inputs to any output with a batch dimension of matching length.""" 

1171 not_processed_outputs = dict(outputs) 

1172 ret: dict[MemberId, Tensor] = {} 

1173 for m in tuple(outputs): 

1174 if BATCH_AXIS_ID not in outputs[m].dims: 1174 ↛ 1175line 1174 didn't jump to line 1175 because the condition on line 1174 was never true

1175 ret[m] = not_processed_outputs.pop(m) 

1176 

1177 input_has_multi_index = False 

1178 for ipt in inputs: 

1179 if isinstance(ipt, Tensor): 1179 ↛ 1180line 1179 didn't jump to line 1180 because the condition on line 1179 was never true

1180 idx = ipt.batch_multi_index 

1181 else: 

1182 idx = ipt 

1183 

1184 if not isinstance(idx, MultiIndex): 1184 ↛ 1185line 1184 didn't jump to line 1185 because the condition on line 1184 was never true

1185 continue 

1186 

1187 input_has_multi_index = True 

1188 for m in tuple(not_processed_outputs): 

1189 if len(idx) == not_processed_outputs[m].tagged_shape.get(BATCH_AXIS_ID): 1189 ↛ 1188line 1189 didn't jump to line 1188 because the condition on line 1189 was always true

1190 ret[m] = not_processed_outputs.pop(m).assign_batch_multi_index(idx) 

1191 

1192 if not_processed_outputs: 1192 ↛ 1193line 1192 didn't jump to line 1193 because the condition on line 1192 was never true

1193 if input_has_multi_index: 

1194 logger.warning( 

1195 "Could not restore batch multi-index for outputs: {}", 

1196 list(not_processed_outputs), 

1197 ) 

1198 ret.update(not_processed_outputs) 

1199 

1200 return ret 

1201 

1202 

1203def get_unstacked_dim_order( 

1204 stacked_order: Sequence[AxisId], 

1205 *, 

1206 unstacked_reference_order: Sequence[AxisId] | None, 

1207 stacked_dims: Sequence[AxisId] | None, 

1208) -> list[AxisId]: 

1209 if stacked_dims is not None: 1209 ↛ 1215line 1209 didn't jump to line 1215 because the condition on line 1209 was always true

1210 # make sure that any "original_batch" axis id in the stacked_dims is replaced with the actual batch axis id 

1211 stacked_dims = [ 

1212 BATCH_AXIS_ID if d == "original_batch" else d for d in stacked_dims 

1213 ] 

1214 

1215 if not stacked_dims and unstacked_reference_order is None: 1215 ↛ 1216line 1215 didn't jump to line 1216 because the condition on line 1215 was never true

1216 return list(stacked_order) 

1217 if unstacked_reference_order is not None and list(stacked_order) == [ 

1218 d for d in unstacked_reference_order if d in stacked_order 

1219 ]: 

1220 # no differences in non-stacking axes, so we can preserve the unstacked axis order 

1221 return [ 

1222 d 

1223 for d in unstacked_reference_order 

1224 if d in stacked_order or (stacked_dims is not None and d in stacked_dims) 

1225 ] 

1226 elif stacked_dims is None: 1226 ↛ 1228line 1226 didn't jump to line 1228 because the condition on line 1226 was never true

1227 # no stacked dims provided, so we can just return the stacked_out_order 

1228 return list(stacked_order) 

1229 elif BATCH_AXIS_ID not in stacked_order: 1229 ↛ 1230line 1229 didn't jump to line 1230 because the condition on line 1229 was never true

1230 raise ValueError( 

1231 f"stacked_order={stacked_order} does not contain a batch axis, but stacked_dims={stacked_dims} are provided" 

1232 ) 

1233 else: 

1234 # unstack batch axis in its place 

1235 ret = list(stacked_order) 

1236 b_idx = ret.index(AxisId("batch")) 

1237 ret[b_idx : b_idx + 1] = ( 

1238 [ 

1239 d for d in unstacked_reference_order or () if d in stacked_dims 

1240 ] # keep unstacked axis order from reference 

1241 + [ 

1242 d 

1243 for d in stacked_dims 

1244 if unstacked_reference_order is None 

1245 or d not in unstacked_reference_order 

1246 ] # add any remaining stacked dims that are not in the unstacked reference order 

1247 ) 

1248 return ret 

1249 

1250 

1251# def transpose_multiple_tensors( 

1252# tensors: PerMember[Tensor], 

1253# axes: PerMember[Sequence[AxisId]], 

1254# *, 

1255# extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "raise", 

1256# missing_dims: Literal["raise", "expand", "unstack", "unstack_or_expand"] = "raise", 

1257# ) -> PerMember[Tensor]: 

1258# """Transpose multiple tensors and make 

1259# """ 

1260# if any(unknown := [m not in tensors for m in axes]): 

1261# raise ValueError(f"Axes specified for unknown members: {unknown}") 

1262 

1263# transposed_tensors: dict[MemberId, Tensor] = {} 

1264# for m, t in tensors.items(): 

1265# if m in axes: 

1266# transposed_tensors[m] = ( 

1267# t.transpose( 

1268# axes=axes[m], 

1269# extra_dims=extra_dims, 

1270# missing_dims=missing_dims, 

1271# ) 

1272# ) 

1273# else: 

1274# transposed_tensors[m] = t 

1275 

1276# if ( 

1277# len( 

1278# batch_lengths := { 

1279# t.sizes[AxisId("batch")] 

1280# for t in transposed_tensors.values() 

1281# if AxisId("batch") in t.dims 

1282# } 

1283# ) 

1284# > 1 

1285# ): 

1286# raise ValueError( 

1287# f"Transposed sample members have incompatible batch lengths: {batch_lengths}." 

1288# ) 

1289 

1290# return transposed_tensors 

1291 

1292 

1293def _add_singletons(arr: NDArray[Any], axis_infos: Sequence[AxisInfo]): 

1294 if len(arr.shape) > len(axis_infos): 1294 ↛ 1296line 1294 didn't jump to line 1296 because the condition on line 1294 was never true

1295 # remove singletons 

1296 for i, s in enumerate(arr.shape): 

1297 if s == 1: 

1298 arr = np.take(arr, 0, axis=i) 

1299 if len(arr.shape) == len(axis_infos): 

1300 break 

1301 

1302 # add singletons if nececsary 

1303 for i, a in enumerate(axis_infos): 1303 ↛ 1310line 1303 didn't jump to line 1310 because the loop on line 1303 didn't complete

1304 if len(arr.shape) >= len(axis_infos): 1304 ↛ 1307line 1304 didn't jump to line 1307 because the condition on line 1304 was always true

1305 break 

1306 

1307 if a.size.min == 1: 

1308 arr = np.expand_dims(arr, i) 

1309 

1310 return arr 

1311 

1312 

1313def _get_array_view( 

1314 original_array: Array, axis_infos: Sequence[AxisInfo] 

1315) -> Array | None: 

1316 perms = list(permutations(range(len(original_array.shape)))) 

1317 

1318 for perm in perms: 1318 ↛ 1334line 1318 didn't jump to line 1334 because the loop on line 1318 didn't complete

1319 view: Array = original_array.transpose(perm) # pyright: ignore 

1320 view = _add_singletons(view, axis_infos) # pyright: ignore 

1321 if len(view.shape) != len(axis_infos): 1321 ↛ 1322line 1321 didn't jump to line 1322 because the condition on line 1321 was never true

1322 return None 

1323 

1324 for s, a in zip(view.shape, axis_infos): 

1325 if ( 1325 ↛ 1330line 1325 didn't jump to line 1330 because the condition on line 1325 was never true

1326 s < a.size.min 

1327 or (a.size.max is not None and s > a.size.max) 

1328 or (a.size.step is not None and (s - a.size.min) % a.size.step != 0) 

1329 ): 

1330 break 

1331 else: 

1332 return view 

1333 

1334 return None