in python/de-identifier/research_pacs/de_identifier/main.py [0:0]
def process_new_dicom_orthanc(src_instance_id, msg):
"""
Process new DICOM instances stored in the source Orthanc server as follows:
- Download the configuration file how original DICOM files are processed
- De-identify the DICOM file uing the function `deidentify_dicom_orthanc`
- Write the de-identified DICOM file to the destination (destination Orthanc server, S3 or local)
- Write detailed logs to S3 or file, if needed
Args:
src_instance_id (str): Instance ID in the source Orthanc server
msg (dict): Content of the SQS message
"""
err = None
logs = {'Message': msg}
try:
# Load the config file, either from the custom location passed in the message or from the
# default location, and create a DicomDeidentifier object
try:
if 'ConfigFile' in msg:
config_location = msg['ConfigFile']
logger.info(f'Using a custom config file "{config_location}"')
else:
config_location = env.config_file
logger.debug(f'Loading the config file at "{config_location}"')
logs['ConfigFile'] = config_location
config = rpacs_util.load_file(config_location, env.region, 'yaml')
except Exception as e:
raise Exception(f'Failed to download the config file - {e}')
# Download the original DICOM file
try:
logger.debug('Loading the original DICOM file from Orthanc')
src_dicom = client.src_orthanc.download_instance_dicom(src_instance_id)
except Exception as e:
raise Exception(f'Failed to download the original DICOM file from Orthanc - {e}')
# De-identify the DICOM file
logger.debug('De-identifying the original DICOM file from Orthanc')
dst_dicom = deidentify_dicom_orthanc(src_instance_id, src_dicom, config, logs)
# Send the de-identified DICOM file to the destination, if the call is `deidentify_dicom`
# returned a DICOM file (the file might be skipped based on its labels)
try:
if dst_dicom != None:
if 'Destination' in msg:
rpacs_util.write_file(dst_dicom, msg['Destination'], env.region, 'bytes')
logger.info(f"Uploaded the de-identified DICOM file to \"{msg['Destination']}\"")
else:
dst_instance_id = client.dst_orthanc.upload_instance(dst_dicom)
logger.info(f"Uploaded the de-identified DICOM file to Orthanc - ID={dst_instance_id}")
except Exception as e:
raise Exception(f'Failed the write the de-identified DICOM file - {e}')
except Exception as e:
logger.error(f'Failed to process the DICOM file - {e}')
logs.update({'Error': str(e)})
err = e
# Print the result logs to the screen
for key, value in logs.items():
if key == 'Message':
continue
elif key == 'TransformationsApplied':
for t_key, t_value in value.items():
logger.info(f'Result: {key} {t_key}={json.dumps(t_value)}')
else:
logger.info(f'Result: {key}={json.dumps(value)}')
# Upload the detailed logs
if 'LogFile' in msg:
try:
rpacs_util.write_file(logs, msg['LogFile'], env.region, 'json')
logger.info(f"Uploaded the detailed logs to \"{msg['LogFile']}\"")
except Exception as e:
logger.error(f'Failed to upload the log file - {e}')
# Raise the exception err if it was catched earlier
if err != None:
raise err