@@ -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 ,
0 commit comments