Skip to content

Commit

Permalink
Exception fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeyiasemis committed Sep 22, 2023
1 parent acfa616 commit d5de4e4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
16 changes: 9 additions & 7 deletions direct/data/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,12 @@ def parse_filenames_data(self, filenames, extra_mats=None):
if not filename.exists():
raise OSError(f"{filename} does not exist.")
kspace_shape = h5py.File(filename, "r")[self.kspace_key].shape
self.verify_extra_mat_integrity(filename, kspace_shape, extra_mats=extra_mats)
except Exception as exc:
self.logger.warning("%s failed with Exception: %s. Skipping...", filename, exc)
self.verify_extra_mat_integrity(filename, extra_mats=extra_mats)
except FileNotFoundError as exc:
self.logger.warning("%s not found. Failed with: %s. Skipping...", filename, exc)
continue
except OSError as exc:
self.logger.warning("%s failed with OSError: %s. Skipping...", filename, exc)
continue

if self.kspace_context is None:
Expand All @@ -569,7 +572,7 @@ def parse_filenames_data(self, filenames, extra_mats=None):
current_slice_number += num_slices

@staticmethod
def verify_extra_mat_integrity(image_fn, _, extra_mats):
def verify_extra_mat_integrity(image_fn, extra_mats):
if not extra_mats:
return

Expand All @@ -585,10 +588,9 @@ def __len__(self):

def get_slice_data(self, filename, slice_no, key, pass_attrs=False, extra_keys=None):
extra_data = {}
if not filename.exists():
raise OSError(f"{filename} does not exist.")

try:
if not filename.exists():
raise OSError(f"{filename} does not exist.")
data = h5py.File(filename, "r")
except Exception as e:
raise Exception(f"Reading filename {filename} caused exception: {e}")
Expand Down
26 changes: 21 additions & 5 deletions direct/data/h5_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,33 @@ def parse_filenames_data(self, filenames, extra_h5s=None, filter_slice=None):
try:
kspace_shape = h5py.File(filename, "r")["kspace"].shape # pylint: disable = E1101
self.verify_extra_h5_integrity(filename, kspace_shape, extra_h5s=extra_h5s) # pylint: disable = E1101
if self.sensitivity_maps:
_ = h5py.File(self.sensitivity_maps / filename.name, "r")

except FileNotFoundError as exc:
self.logger.warning("%s sensitivity map not found. Failed with: %s. Skipping...", filename, exc)
self.logger.warning("%s not found. Failed with: %s. Skipping...", filename, exc)
continue

except OSError as exc:
self.logger.warning("%s failed with OSError: %s. Skipping...", filename, exc)
continue

if self.sensitivity_maps:
try:
_ = h5py.File(self.sensitivity_maps / filename.name, "r")
except FileNotFoundError as exc:
self.logger.warning(
"Sensitivity map %s not found. Failed with: %s. Skipping %s...",
self.sensitivity_maps / filename.name,
exc,
filename,
)
continue
except OSError as exc:
self.logger.warning(
"Sensitivity map %s failed with OSError: %s. Skipping %s...",
self.sensitivity_maps / filename.name,
exc,
filename,
)
continue

num_slices = kspace_shape[0]
if not filter_slice:
self.data += [(filename, _) for _ in range(num_slices)]
Expand Down

0 comments on commit d5de4e4

Please sign in to comment.