Skip to content

Instantly share code, notes, and snippets.

@dmtmov
Created June 10, 2022 10:20
Show Gist options
  • Save dmtmov/75d2b9754458d1f484c0aa5b35e7097d to your computer and use it in GitHub Desktop.
Save dmtmov/75d2b9754458d1f484c0aa5b35e7097d to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
The script exports of Pinnacle data from the given Patient sources.
It takes 2 arguments:
:input_path: (str) - The path to folder of Patient source files;
:output_directory: (str) - The output files of this tool for Patient;
Settings:
ERROR_LOG_FILE_LOCATION - location of errors log file;
Usage:
$ python pymedphys_tool.py Patient_15000/ results_for_patient_15000/
On any error appear, it will quietly finish without interrupting. Output
directory will be renamed to `results_for_patient_15000_failed_20221006_032145`
and there will be added a proper record in your error log file.
Both success and failure cases are accompanied by information in output.
This info does not track.
Built & tested on Macos in python3.10 environment.
"""
import datetime
import logging
import os
import sys
import traceback
from pymedphys._experimental.pinnacle.pinnacle_cli import export_cli
ERROR_LOG_FILE_LOCATION: str = "errors.log"
class Args:
"""
Mock arguments required by the lib
"""
def __init__(self, input_path, output_path):
self.input_path = input_path
self.output_directory = output_path
self.modality = ["CT", "RTSTRUCT", "RTDOSE", "RTPLAN"]
self.plan = None
self.trial = None
self.list = []
self.image = None
self.uid_prefix = None
self.roiskip = None
self.mrn = None
self.verbose = False
def run_tool(args):
logger = logging.getLogger(__name__)
fh = logging.FileHandler(ERROR_LOG_FILE_LOCATION)
fh.setLevel(logging.DEBUG)
err_string = (f"%(asctime)s\t%(levelname)s\tinput_path={args.input_path}\t"
f"output_directory={args.output_directory}\t%(message)s")
fh.setFormatter(logging.Formatter(err_string))
logger.addHandler(fh)
try:
export_cli(args)
except Exception: # catch any error
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
logger.error(traceback.format_exc())
failed_folder_name = f"{args.output_directory}_failed_{timestamp}"
os.rename(args.output_directory, failed_folder_name)
if __name__ == "__main__":
run_tool(Args(input_path=sys.argv[1], output_path=sys.argv[2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment