Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Default values for split are length 1 and character empty space
bot.debug("Parsing action %s" % action)
split_by = " "
minlength = 1
if "value" in action:
for param in action["value"].split(";"):
param_name, param_val = param.split("=")
param_name = param_name.strip()
param_val = param_val.strip()
# Set a custom parameter legnth
if param_name == "minlength":
minlength = int(param_val)
bot.debug("Minimum length set to %s" % minlength)
elif param_name == "by":
split_by = param_val.strip("'").strip('"')
bot.debug("Splitting value set to %s" % split_by)
for uid, field in subset.items():
new_values = (str(field.element.value) or "").split(split_by)
for new_value in new_values:
if len(new_value) >= minlength:
values.add(new_value)
else:
bot.warning(
"Unrecognized action %s for values list extraction." % action["action"]
)
return list(values)
"""
# A default deid will be loaded if all else fails
default_deid = os.path.join(get_installdir(), "data", "deid.dicom")
if path is None:
path = os.getcwd()
# The user has provided a directory
if os.path.isdir(path):
contenders = [
"%s/%s" % (path, x) for x in os.listdir(path) if x.startswith("deid")
]
if len(contenders) == 0:
bot.warning(
"No deid settings files found in %s, will use default dicom.deid."
% path
)
contenders.append(default_deid)
elif len(contenders) > 1:
bot.warning("Multiple deid files found in %s, will use first." % (path))
path = contenders[0]
# We have a file path at this point
if not os.path.exists(path):
bot.exit("Cannot find deid file %s, exiting." % (path))
return path
elif action == "REPLACE":
self.replace_field(field, value)
# Code the value with something in the response
elif action == "JITTER":
value = parse_value(
item=self.lookup, dicom=self.dicom, value=value, field=field
)
if value is not None:
# Jitter the field by the supplied value
jitter_timestamp(
dicom=self.dicom, field=field.element.keyword, value=value
)
else:
bot.warning("JITTER %s unsuccessful" % field)
# elif "KEEP" --> Do nothing. Keep the original
# Remove the field entirely
elif action == "REMOVE":
# If a value is defined, parse it (could be filter)
do_removal = True
if value != None:
do_removal = parse_value(
item=self.lookup, dicom=self.dicom, value=value, field=field
)
if do_removal == True:
self.delete_field(field)
% (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"]:
bot.custom(
prefix="CLEAN", message="%s files" % len(result["clean"]), color="CYAN"
)
if result["flagged"]:
for group, files in result["flagged"].items():
bot.flag("%s %s files" % (group, len(files)))
if args.save:
folders = "-".join([os.path.basename(folder) for folder in base])
outfile = "pixel-flag-results-%s-%s.tsv" % (
folders,
datetime.datetime.now().strftime("%y-%m-%d"),
)
with open(outfile, "w") as filey:
filey.writelines("dicom_file\tpixels_flagged\tflag_list\treason\n")
from deid.config.utils import (
load_deid,
get_deid,
load_combined_deid
)
from deid.config.standards import (
actions,
sections,
formats
)
from deid.logger import bot
import os
import re
bot.level = 3
class DeidRecipe:
'''
Create and work with a deid recipe to filter and perform operations on
a dicom header. Usage typically looks like:
deid = 'dicom.deid'
recipe = DeidRecipe(deid)
If deid is None, the default provided by the application is used.
Parameters
==========
deid: the deid recipe (or recipes) files to use. If more than one
is provided, should be done in order of preference for load
(later in the list overrides earlier loaded).
def clean(self, fix_interpretation=True, pixel_data_attribute="PixelData"):
"""
take a dicom image and a list of pixel coordinates, and return
a cleaned file (if output file is specified) or simply plot
the cleaned result (if no file is specified)
Parameters
==========
add_padding: add N=margin pixels of padding
margin: pixels of padding to add, if add_padding True
fix_interpretation: fix the photometric interpretation if found off
"""
if not self.results:
bot.warning("Use %s.detect() to find coordinates first." % self)
else:
bot.info("Scrubbing %s." % self.dicom_file)
# Load in dicom file, and image data
dicom = read_file(self.dicom_file, force=True)
pixel_data = getattr(dicom, pixel_data_attribute)
# Get expected and actual length of the pixel data (bytes, expected does not include trailing null byte)
expected_length = get_expected_length(dicom)
actual_length = len(pixel_data)
padded_expected_length = expected_length + expected_length % 2
full_length = expected_length / 2 * 3 # upsampled data is a third larger
full_length += (
1 if full_length % 2 else 0
) # trailing padding byte if even length
def clean_pixels():
bot.warning('BEEP-BOOP - I am not written yet!')
if x is not None]
for func,actions in filters.items():
for action in actions:
flagged = apply_filter(dicom=dicom,
field=action['field'],
filter_name=func,
value=action["value"])
if flagged:
label = " ".join(label)
bot.warning("FLAG for %s: %s" %(dicom_name,label))
return flagged
bot.debug("%s header filter indicates pixels are clean." %dicom_name)
return flagged
def validate_dicoms(dcm_files, force=False):
"""validate dicoms will test opening one or more dicom files,
and return a list of valid files.
Parameters
==========
dcm_files: one or more dicom files to test
"""
if not isinstance(dcm_files, list):
dcm_files = [dcm_files]
valids = []
bot.debug("Checking %s dicom files for validation." % (len(dcm_files)))
for dcm_file in dcm_files:
try:
with open(dcm_file, "rb") as filey:
read_file(filey, force=force)
valids.append(dcm_file)
except:
bot.warning("Cannot read input file {0!s}, skipping.".format(dcm_file))
bot.debug("Found %s valid dicom files" % (len(valids)))
return valids