Skip to content

Commit

Permalink
Merge pull request #357 from DESm1th/conv_err
Browse files Browse the repository at this point in the history
[FIX] Remove conversion error message from DB when no longer exists
  • Loading branch information
DESm1th authored Sep 28, 2023
2 parents 0f14940 + 173ba17 commit 21a9413
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions datman/exporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,9 @@ def outputs_exist(self):
if not scan:
return False

if self.errors_outdated(scan, name):
return False

return True

@classmethod
Expand Down Expand Up @@ -852,7 +855,7 @@ def make_scan(self, file_stem):

self._add_bids_scan_name(scan, file_stem)
self._add_side_car(scan, file_stem)
self._add_conversion_errors(scan, file_stem)
self._update_conversion_errors(scan, file_stem)

def _add_bids_scan_name(self, scan, dm_stem):
"""Add a bids format file name to a series in the QC database.
Expand Down Expand Up @@ -897,7 +900,7 @@ def _add_side_car(self, scan, file_stem):
logger.error("Failed to add JSON side car to dashboard "
f"record for {side_car}. Reason - {exc}")

def _add_conversion_errors(self, scan, file_stem):
def _update_conversion_errors(self, scan, file_stem):
"""Add any dcm2niix conversion errors to the QC database.
Args:
Expand All @@ -907,6 +910,10 @@ def _add_conversion_errors(self, scan, file_stem):
"""
convert_errors = self._get_file(file_stem, ".err")
if not convert_errors:
if scan.conv_errors:
# Erase the error message from the DB, because it
# has been resolved.
scan.add_error(None)
return
message = self._read_file(convert_errors)
scan.add_error(message)
Expand Down Expand Up @@ -941,10 +948,26 @@ def _read_file(self, fpath):
with open(fpath, "r") as file_handle:
message = file_handle.readlines()
except Exception as exc:
logger.debug(f"Can't read file {file_handle} - {exc}")
logger.debug(f"Can't read file {fpath} - {exc}")
return None
return message

def errors_outdated(self, scan, fname):
err_file = self._get_file(fname, ".err")
if not err_file and scan.conv_errors:
# Error is resolved, but still appears in database
return True
if err_file and not scan.conv_errors:
# Error has appeared, but isnt recorded in database
return True
if err_file and scan.conv_errors:
# Error exists in both locations, but may have changed
message = self._read_file(err_file)
if isinstance(message, list):
message = "\n".join(message)
return message != scan.conv_errors
return False


class NiiExporter(SeriesExporter):
"""Export a series to nifti format with datman-style names.
Expand Down

0 comments on commit 21a9413

Please sign in to comment.