From d103f6864efa250ef96db764ea5b499e5386100f Mon Sep 17 00:00:00 2001 From: arnaudbore Date: Thu, 27 Jun 2024 09:31:29 -0400 Subject: [PATCH] answer Sam comments --- Dockerfile | 4 ++-- dcm2bids/dcm2bids_gen.py | 11 ++++------- dcm2bids/dcm2niix_gen.py | 5 ++++- dcm2bids/utils/logger.py | 29 ++++++++++++++++++++++++++++- environment.yml | 1 - 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0f067048..fec52d41 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,7 +19,7 @@ FROM debian:stable AS runtime COPY --from=build /venv /venv # Point to conda executables -ENV PATH /venv/bin:$PATH +ENV PATH="/venv/bin:$PATH" # Set FSL variables ENV FSLDIR="/venv" @@ -54,4 +54,4 @@ RUN pip install -e . RUN pip install pydeface -ENTRYPOINT [""] \ No newline at end of file +ENTRYPOINT ["dcm2bids"] \ No newline at end of file diff --git a/dcm2bids/dcm2bids_gen.py b/dcm2bids/dcm2bids_gen.py index 5b6e72bc..414d083e 100644 --- a/dcm2bids/dcm2bids_gen.py +++ b/dcm2bids/dcm2bids_gen.py @@ -123,19 +123,17 @@ def run(self): if self.bids_validate: try: - self.logger.info(f"BIDS VALIDATION") + self.logger.info("BIDS VALIDATION") bids_version = run_shell_command(['bids-validator', '-v'], False) self.logger.info(f"Use bids-validator version: {bids_version.decode()[:-1]}") bids_report = run_shell_command(['bids-validator', self.bids_dir]) self.logger.info("Report from bids-validator") self.logger.info(bids_report.decode()) except Exception: - self.logger.error("\033[91m" - "The bids-validator does not seem to work properly. " + self.logger.error("The bids-validator does not seem to work properly. " "The bids-validator may not be installed on your " "computer. Please check: " - "https://github.com/bids-standard/bids-validator." - "\033[0m") + "https://github.com/bids-standard/bids-validator.") def move(self, acq, idList, post_op): """Move an acquisition to BIDS format""" @@ -198,11 +196,10 @@ def move(self, acq, idList, post_op): continue except Exception: self.logger.error( - "\033[91m" f"The command post_op: \"{cmd}\" " "does not seem to work properly. " "Check if it is installed on your " - "computer.\033[0m\n") + "computer.\n") if ".json" in ext: data = acq.dstSidecarData(idList) diff --git a/dcm2bids/dcm2niix_gen.py b/dcm2bids/dcm2niix_gen.py index 7e9b6bc5..47698b6a 100644 --- a/dcm2bids/dcm2niix_gen.py +++ b/dcm2bids/dcm2niix_gen.py @@ -144,7 +144,10 @@ def execute(self): if "Warning" in output or "Error" in output: self.logger.info("Log from dcm2niix execution") - self.logger.info(f"\033[93m{output}\033[0m") + if "Warning" in output: + self.logger.warning(f"{output}") + else: + self.logger.error(f"{output}") else: self.logger.debug(f"\n{output}") self.logger.info("Check log file for dcm2niix output\n") diff --git a/dcm2bids/utils/logger.py b/dcm2bids/utils/logger.py index eb5af2e3..9529ce15 100644 --- a/dcm2bids/utils/logger.py +++ b/dcm2bids/utils/logger.py @@ -19,7 +19,7 @@ def setup_logging(log_level, log_file=None): sh = logging.StreamHandler(sys.stdout) sh.setLevel(log_level) - sh_fmt = logging.Formatter(fmt="%(levelname)-8s| %(message)s") + sh_fmt = CustomFormatter(fmt="%(levelname)-8s| %(message)s") sh.setFormatter(sh_fmt) # default formatting is kept for the log file" @@ -30,3 +30,30 @@ def setup_logging(log_level, log_file=None): datefmt="%Y-%m-%d %H:%M:%S", handlers=[fh, sh] ) + + +class CustomFormatter(logging.Formatter): + """Logging colored formatter, adapted from https://stackoverflow.com/a/56944256/3638629""" + + grey = '\x1b[38;21m' + blue = '\x1b[38;5;39m' + yellow = '\x1b[38;5;226m' + red = '\x1b[38;5;196m' + bold_red = '\x1b[31;1m' + reset = '\x1b[0m' + + def __init__(self, fmt): + super().__init__() + self.fmt = fmt + self.FORMATS = { + logging.DEBUG: self.grey + self.fmt + self.reset, + logging.INFO: self.blue + self.fmt + self.reset, + logging.WARNING: self.yellow + self.fmt + self.reset, + logging.ERROR: self.red + self.fmt + self.reset, + logging.CRITICAL: self.bold_red + self.fmt + self.reset + } + + def format(self, record): + log_fmt = self.FORMATS.get(record.levelno) + formatter = logging.Formatter(log_fmt) + return formatter.format(record) \ No newline at end of file diff --git a/environment.yml b/environment.yml index 1ce5a83a..75447a4f 100644 --- a/environment.yml +++ b/environment.yml @@ -2,7 +2,6 @@ name: fsl channels: - https://fsl.fmrib.ox.ac.uk/fsldownloads/fslconda/public/ - conda-forge - - HCC dependencies: - fsl-bet2 - fsl-flirt \ No newline at end of file