Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
do_put = False
ids = None
if args.action == "all":
bot.info("GET and PUT identifiers from %s" %(basename))
do_get = True
do_put = True
elif args.action == "get":
do_get = True
bot.info("GET and PUT identifiers from %s" %(basename))
elif args.action == "put":
bot.info("PUT identifiers from %s" %(basename))
do_put = True
if args.ids is None:
bot.error("To PUT without GET you must provide a json file with ids.")
sys.exit(1)
ids = args.ids
# GET identifiers
if do_get is True:
from deid.dicom import get_identifiers
ids = get_identifiers(dicom_files)
if args.do_print is True:
print(ids)
else:
from deid.identifiers import save_identifiers
save_identifiers(ids,output_folder)
if do_put is True:
from deid.dicom import replace_identifiers
cleaned_files = replace_identifiers(dicom_files=dicom_files,
# Initialize the message bot, with level above
from deid.logger import bot
from deid.dicom import get_files
from deid.data import get_dataset
output_folder = args.outfolder
if output_folder is None:
if args.do_print is False:
output_folder = tempfile.mkdtemp()
# If a deid is given, check against format
if args.deid is not None:
from deid.config import load_deid
params = load_deid(args.deid)
if params['format'] != args.format:
bot.error("Format in deid (%s) doesn't match choice here (%s) exiting." %(params['format'],
args.format))
# Get list of dicom files
base = args.folder
if base is None:
bot.info("No input folder specified, will use demo dicom-cookies.")
base = get_dataset('dicom-cookies')
basename = os.path.basename(base)
dicom_files = get_files(base)
# Does the user want to inspect files?
if args.action == "inspect":
from deid.dicom import has_burned_pixels
has_burned_pixels(dicom_files)
sys.exit(0)
tag = "dicom"
# If it's already loaded
if isinstance(tag, dict):
bot.debug("deid is already loaded.")
return tag
# If it's a path, get full path
if os.path.exists(tag):
deid = os.path.abspath(tag)
else:
deid = "%s/deid.%s" % (data_base, tag)
if not os.path.exists(deid):
if quiet is False:
bot.error("Cannot find %s" % (deid))
if exit_on_fail is True:
sys.exit(1)
else:
return None
if load is True:
return load_deid(deid)
return deid
overwrite: overwrite any existing file? (default is False)
"""
if output_folder is None:
if overwrite is False:
output_folder = tempfile.mkdtemp()
else:
output_folder = os.path.dirname(dicom_file)
dicom_name = os.path.basename(dicom_file)
output_dicom = os.path.join(output_folder, dicom_name)
dowrite = True
if overwrite is False:
if os.path.exists(output_dicom):
bot.error(
"%s already exists, overwrite set to False. Not writing." % dicom_name
)
dowrite = False
if dowrite:
dicom.save_as(output_dicom)
return output_dicom
def main(args, parser):
"""inspect currently serves to inspect the header fields of a set
of dicom files against a standard, and flag images that don't
pass the different levels of criteria
"""
# If a deid is given, check against format
deid = args.deid
if deid is not None:
params = load_deid(deid)
if params["format"] != args.format:
bot.error(
"Format in deid (%s) doesn't match choice here (%s) exiting."
% (params["format"], args.format)
)
# Get list of dicom files
base = args.folder
if base is None:
bot.info("No input folder specified, will use demo dicom-cookies.")
base = get_dataset("dicom-cookies")
dicom_files = list(
get_files(base, pattern=args.pattern)
) # todo : consider using generator functionality
result = has_burned_pixels(dicom_files, deid=deid)
print("\nSUMMARY ================================\n")
if result["clean"]:
==========
dicom: the pydicom.dataset Dataset (pydicom.read_file)
field: the name of the field to update
value: the value to set, if name is a valid tag
"""
if field not in dicom:
return dicom
# Case 1: Dealing with a string tag (field name)
if isinstance(field, str):
tag = get_tag(field)
if tag:
dicom.add_new(tag["tag"], tag["VR"], value)
else:
bot.error("%s is not a valid field to add. Skipping." % (field))
# Case 2: we already have a tag for the field name (type BaseTag)
else:
tag = dicom.get(field)
dicom.add_new(field, tag.VR, value)
return dicom
def __init__(self, dicom_file, recipe=None, config=None, force=True):
# Lookup for the dicom
self.lookup = {}
# Will be a list of DicomField
self.fields = {}
# Load default configuration, or a custom one
config = config or os.path.join(here, "config.json")
if not os.path.exists(config):
bot.error("Cannot find config %s, exiting" % (config))
self.config = read_json(config, ordered_dict=True)
# Deid can be a recipe or filename
if not isinstance(recipe, DeidRecipe):
recipe = DeidRecipe(recipe)
self.load(dicom_file, force=force)
self.recipe = recipe
# [0018,1012].equals("") * DateofSecondaryCapture flat not present AND
# ![0008,103e].contains("SAVE") * SeriesDescription does not contain save AND
# [0018,1016].equals("") * SecondaryDeviceCaptureManufacturer flag not present AND
# [0018,1018].equals("") * SecondaryDeviceCaptureManufacturerModelName flag not present AND
# [0018,1019].equals("") * SecondaryDeviceCaptureDeviceSoftwareVersion flag not present AND
# ![0028,0301].contains("YES") BurnedInAnnotation is not YES
# We continue processing given that:
# Image was not saved with some secondary software or device
# Image is not flagged to have burned pixels
if config is None:
config = "%s/pixels.json" %(here)
if not os.path.exists(config):
bot.error("Cannot find config %s, exiting" %(config))
config = read_json(config)
# Load criteria (actions) for flagging
for criteria in config:
flagged = False
filters = criteria["filters"]
label = [x for x in [criteria['modality'],
criteria['manufacturer'],
criteria['label']]
if x is not None]
for func,actions in filters.items():
for action in actions:
flagged = apply_filter(dicom=dicom,
def remove_private(self):
"""Remove private tags from the loaded dicom
"""
try:
self.dicom.remove_private_tags()
except:
bot.error(
"""Private tags for %s could not be completely removed, usually
this is due to invalid data type. Removing others."""
% self.dicom_name
)
for ptag in get_private(self.dicom):
del self.dicom[ptag.tag]