Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Remove conversion error message from DB when no longer exists #357

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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