Skip to content

Commit 17f115b

Browse files
committed
Add option to not verify image path
1 parent 9d1b3b2 commit 17f115b

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

src/torchio/data/image.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ class Image(dict):
9696
is saved in a custom format, such as ``.npy`` (see example below).
9797
If the affine matrix is ``None``, an identity matrix will be used.
9898
**kwargs: Items that will be added to the image dictionary, e.g.
99-
acquisition parameters.
99+
acquisition parameters or image ID.
100+
verify_path: If ``True``, the path will be checked to see if it exists. If
101+
``False``, the path will not be verified. This is useful when it is
102+
expensive to check the path, e.g., when reading a large dataset from a
103+
mounted drive.
100104
101105
TorchIO images are `lazy loaders`_, i.e. the data is only loaded from disk
102106
when needed.
@@ -136,6 +140,7 @@ def __init__(
136140
affine: TypeData | None = None,
137141
check_nans: bool = False, # removed by ITK by default
138142
reader: Callable[[TypePath], TypeDataAffine] = read_image,
143+
verify_path: bool = True,
139144
**kwargs: dict[str, Any],
140145
):
141146
self.check_nans = check_nans
@@ -175,7 +180,7 @@ def __init__(
175180

176181
super().__init__(**kwargs)
177182
self._check_data_loader()
178-
self.path = self._parse_path(path)
183+
self.path = self._parse_path(path, verify=verify_path)
179184

180185
self[PATH] = '' if self.path is None else str(self.path)
181186
self[STEM] = '' if self.path is None else get_stem(self.path)
@@ -456,6 +461,8 @@ def get_bounds(self) -> TypeBounds:
456461
@staticmethod
457462
def _parse_single_path(
458463
path: TypePath,
464+
*,
465+
verify: bool = True,
459466
) -> Path:
460467
try:
461468
path = Path(path).expanduser()
@@ -468,6 +475,8 @@ def _parse_single_path(
468475
except RuntimeError as err:
469476
message = f'Conversion to path not possible for variable: {path}'
470477
raise RuntimeError(message) from err
478+
if not verify:
479+
return path
471480

472481
if not (path.is_file() or path.is_dir()): # might be a dir with DICOM
473482
raise FileNotFoundError(f'File not found: "{path}"')
@@ -476,16 +485,18 @@ def _parse_single_path(
476485
def _parse_path(
477486
self,
478487
path: TypePath | Sequence[TypePath] | None,
488+
*,
489+
verify: bool = True,
479490
) -> Path | list[Path] | None:
480491
if path is None:
481492
return None
482493
elif isinstance(path, dict):
483494
# https://github.com/TorchIO-project/torchio/pull/838
484495
raise TypeError('The path argument cannot be a dictionary')
485496
elif self._is_paths_sequence(path):
486-
return [self._parse_single_path(p) for p in path] # type: ignore[union-attr]
497+
return [self._parse_single_path(p, verify=verify) for p in path] # type: ignore[union-attr]
487498
else:
488-
return self._parse_single_path(path) # type: ignore[arg-type]
499+
return self._parse_single_path(path, verify=verify) # type: ignore[arg-type]
489500

490501
def _parse_tensor(
491502
self,

tests/data/test_image.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import copy
55
import sys
66
import tempfile
7+
from pathlib import Path
78

89
import nibabel as nib
910
import numpy as np
@@ -309,3 +310,20 @@ def test_slicing(self):
309310

310311
with pytest.raises(ValueError):
311312
image[3::-1]
313+
314+
def test_verify_path(self):
315+
path = Path(self.get_image_path('im_verify'))
316+
317+
image = tio.ScalarImage(path, verify_path=False)
318+
assert image.path == path
319+
320+
image = tio.ScalarImage(path, verify_path=True)
321+
assert image.path == path
322+
323+
fake_path = 'fake_path.nii'
324+
325+
image = tio.ScalarImage(path, verify_path=False)
326+
assert image.path == path
327+
328+
with pytest.raises(FileNotFoundError):
329+
tio.ScalarImage(fake_path, verify_path=True)

0 commit comments

Comments
 (0)