Skip to content

Commit 6ea3bf7

Browse files
authored
Improve error when a tensor is passed to an image (#1334)
1 parent 348a699 commit 6ea3bf7

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

src/torchio/data/image.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,17 +458,25 @@ def get_bounds(self) -> TypeBounds:
458458
bounds_x, bounds_y, bounds_z = array.T.tolist() # type: ignore[misc]
459459
return bounds_x, bounds_y, bounds_z # type: ignore[return-value]
460460

461-
@staticmethod
462461
def _parse_single_path(
462+
self,
463463
path: TypePath,
464464
*,
465465
verify: bool = True,
466466
) -> Path:
467+
if isinstance(path, (torch.Tensor, np.ndarray)):
468+
class_name = self.__class__.__name__
469+
message = (
470+
'Expected type str or Path but found a tensor/array. Instead of'
471+
f' {class_name}(your_tensor),'
472+
f' use {class_name}(tensor=your_tensor).'
473+
)
474+
raise TypeError(message)
467475
try:
468476
path = Path(path).expanduser()
469477
except TypeError as err:
470478
message = (
471-
f'Expected type str or Path but found {path} with type'
479+
f'Expected type str or Path but found an object with type'
472480
f' {type(path)} instead'
473481
)
474482
raise TypeError(message) from err

0 commit comments

Comments
 (0)