Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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,
ids=ids,
deid=args.deid,
overwrite=args.overwrite,
output_folder=output_folder)
bot.info("%s %s files at %s" %(len(cleaned_files),
args.format,
output_folder))
test_file = read_file(cleaned_files[0])
# test_file
# (0008, 0018) SOP Instance UID UI: cookiemonster-image-1
# (0010, 0020) Patient ID LO: 'cookiemonster'
# (0012, 0062) Patient Identity Removed CS: 'Yes'
# (0028, 0002) Samples per Pixel US: 3
# (0028, 0010) Rows US: 1536
# (0028, 0011) Columns US: 2048
# (7fe0, 0010) Pixel Data OB: Array of 738444 bytes
# Different output folder
cleaned_files = replace_identifiers(
dicom_files=dicom_files, deid=recipe, ids=updated_ids, output_folder="/tmp/"
)
# Force overwrite (be careful!)
cleaned_files = replace_identifiers(
dicom_files=dicom_files,
deid=recipe,
ids=updated_ids,
output_folder="/tmp/",
overwrite=True,
)
bigint_uid = str(uuid.uuid4().int)
full_uid = ORG_ROOT + "." + bigint_uid
sliced_uid = full_uid[0:64] # A DICOM UID is limited to 64 characters
return prefix + "-" + sliced_uid
# Remember, the action is:
# REPLACE StudyInstanceUID func:generate_uid
# so the key needs to be generate_uid
for item in items:
items[item]["generate_uid"] = generate_uid
# Now let's generate the cleaned files! It will output to a temporary directory
# And then use the deid recipe and updated to create new files
cleaned_files = replace_identifiers(dicom_files=dicom_files, deid=recipe, ids=items)
# Print a cleaned file
print(cleaned_files[0])
def generate_date(item, value, field, dicom):
"""This function will generate a dicom uid! You can expect it to be passed
the dictionary of items extracted from the dicom (and your function)
and variables, the original value (func:generate_uid) and the field
object you are applying it to.
"""
return "20200608"
# Add the function to each item to be found
for item in items:
items[item]["generate_date"] = generate_date
# Clean the files
cleaned_files = replace_identifiers(
dicom_files=dicom_files, deid=recipe, strip_sequences=False, ids=items
)
# Print two instances (one in sequence)
print(cleaned_files[0].InstanceCreationDate)
print(cleaned_files[0].ReferencedPerformedProcedureStepSequence[0].InstanceCreationDate)
# (0008, 0018) SOP Instance UID UI: cookiemonster-image-1
# (0010, 0020) Patient ID LO: 'cookiemonster'
# (0012, 0062) Patient Identity Removed CS: 'Yes'
# (0028, 0002) Samples per Pixel US: 3
# (0028, 0010) Rows US: 1536
# (0028, 0011) Columns US: 2048
# (7fe0, 0010) Pixel Data OB: Array of 738444 bytes
# Different output folder
cleaned_files = replace_identifiers(
dicom_files=dicom_files, deid=recipe, ids=updated_ids, output_folder="/tmp/"
)
# Force overwrite (be careful!)
cleaned_files = replace_identifiers(
dicom_files=dicom_files,
deid=recipe,
ids=updated_ids,
output_folder="/tmp/",
overwrite=True,
)
# Let's be lazy and just update the extracted ones
updated_ids = dict()
count = 0
for image, fields in ids.items():
fields["id"] = "cookiemonster"
fields["source_id"] = "cookiemonster-image-%s" % (count)
updated_ids[image] = fields
count += 1
# You can look at each of the updated_ids entries and see the added variables
# 'id': 'cookiemonster',
# 'source_id': 'cookiemonster-image-2'}}
# And then use the deid recipe and updated to create new files
cleaned_files = replace_identifiers(
dicom_files=dicom_files, deid=recipe, ids=updated_ids
)
# We can load in a cleaned file to see what was done
from pydicom import read_file
test_file = read_file(cleaned_files[0])
# test_file
# (0008, 0018) SOP Instance UID UI: cookiemonster-image-1
# (0010, 0020) Patient ID LO: 'cookiemonster'
# (0012, 0062) Patient Identity Removed CS: 'Yes'
# (0028, 0002) Samples per Pixel US: 3
# (0028, 0010) Rows US: 1536