Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# You can also provide your own deid recipe file, and in doing so, you
# won't load a default
path = os.path.abspath("%s/../examples/deid/" % get_installdir())
recipe = DeidRecipe(deid=path)
# You can also choose to load the default base with your own recipe
recipe = DeidRecipe(deid=path, base=True)
# Or specify a different base entirely. The base is the deid. in the
# deid.data folder. So for example, under deid/data/deid.dicom.chest.xray we would
# do:
recipe = DeidRecipe(deid=path, base=True, default_base="dicom.xray.chest")
# We can also specify one of the deid recipes provided by the library as our
# only to use.
recipe = DeidRecipe(deid="dicom.xray.chest")
# This is to encourage sharing! If you have a general recipe that others might
# use, please contribute it to the library.
# You can also load multiple deid files, just put them in a list in the order
# that you want them loaded.
# To see an entire (raw in a dictionary) recipe just look at
recipe.deid
# Now let's look at parts of this recipe. There are two categories of things:
# - filters: are used for parsing over headers and flagging images
# - header: is a set of rules that govern a replacement procedure
################################################################################
# Format
recipe = DeidRecipe()
# Since we didn't load a custom deid recipe text file, we get a default
# WARNING No specification, loading default base deid.dicom
# You can add custom deid files with .load().
# We can look at the criteria loaded:
recipe.deid
# You can also provide your own deid recipe file, and in doing so, you
# won't load a default
path = os.path.abspath("%s/../examples/deid/" % get_installdir())
recipe = DeidRecipe(deid=path)
# You can also choose to load the default base with your own recipe
recipe = DeidRecipe(deid=path, base=True)
# Or specify a different base entirely. The base is the deid. in the
# deid.data folder. So for example, under deid/data/deid.dicom.chest.xray we would
# do:
recipe = DeidRecipe(deid=path, base=True, default_base="dicom.xray.chest")
# We can also specify one of the deid recipes provided by the library as our
# only to use.
recipe = DeidRecipe(deid="dicom.xray.chest")
# This is to encourage sharing! If you have a general recipe that others might
# use, please contribute it to the library.
# You can also load multiple deid files, just put them in a list in the order
# that you want them loaded.
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
# Create a DeidRecipe
from deid.config import DeidRecipe
recipe = DeidRecipe()
# Since we didn't load a custom deid recipe text file, we get a default
# WARNING No specification, loading default base deid.dicom
# You can add custom deid files with .load().
# We can look at the criteria loaded:
recipe.deid
# You can also provide your own deid recipe file, and in doing so, you
# won't load a default
path = os.path.abspath("%s/../examples/deid/" % get_installdir())
recipe = DeidRecipe(deid=path)
# You can also choose to load the default base with your own recipe
recipe = DeidRecipe(deid=path, base=True)
# Or specify a different base entirely. The base is the deid. in the
# deid.data folder. So for example, under deid/data/deid.dicom.chest.xray we would
# do:
recipe = DeidRecipe(deid=path, base=True, default_base="dicom.xray.chest")
# We can also specify one of the deid recipes provided by the library as our
# only to use.
recipe = DeidRecipe(deid="dicom.xray.chest")
# This is to encourage sharing! If you have a general recipe that others might
# use, please contribute it to the library.
# ['whitelist', 'blacklist']
# To get a list of specific filters under a group
recipe.get_filters("blacklist")
################################################################################
# Header Actions
# A header action is a step (e.g., replace, remove, blank) to be applied to
# a dicom image header. The headers are also part of the deid recipe, and you
# don't need to necessarily use header actions and filters at the same time.
################################################################################
# For the example we were doing above, the recipe didn't have a header section.
# Let's load one that does.
recipe = DeidRecipe()
# We can get a complete list of actions
recipe.get_actions()
# We can filter to an action type
recipe.get_actions(action="ADD")
# [{'action': 'ADD',
# 'field': 'IssuerOfPatientID',
# 'value': 'STARR. In an effort to remove PHI all dates are offset from their original values.'},
# {'action': 'ADD',
# 'field': 'PatientBirthDate',
# 'value': 'var:entity_timestamp'},
# {'action': 'ADD', 'field': 'StudyDate', 'value': 'var:item_timestamp'},
# {'action': 'ADD', 'field': 'PatientID', 'value': 'var:entity_id'},
# {'action': 'ADD', 'field': 'AccessionNumber', 'value': 'var:item_id'},
# FORMAT dicom
# %header
# REPLACE StudyInstanceUID func:generate_uid
# REPLACE SeriesInstanceUID func:generate_uid
# ADD FrameOfReferenceUID func:generate_uid
#
# In the above we are saying we want to replace the fields above with the
# output from the generate_uid function, which is expected in the item dict
##################################
# Create the DeidRecipe Instance from deid.dicom
from deid.config import DeidRecipe
recipe = DeidRecipe("deid.dicom")
# To see an entire (raw in a dictionary) recipe just look at
recipe.deid
# What is the format?
recipe.get_format()
# dicom
# What actions do we want to do on the header?
recipe.get_actions()
"""
[{'action': 'REPLACE',
'field': 'StudyInstanceUID',
'value': 'func:generate_uid'},
{'action': 'REPLACE',
def has_burned_pixels(dicom_files, force=True, deid=None):
""" has burned pixels is an entrypoint for has_burned_pixels_multi (for
multiple images) or has_burned_pixels_single (for one detailed repor)
We will use the MIRCTP criteria (see ref folder with the
original scripts used by CTP) to determine if an image is likely to have
PHI, based on fields in the header alone. This script does NOT perform
pixel cleaning, but returns a dictionary of results (for multi) or one
detailed result (for single)
"""
# if the user has provided a custom deid, load it
if not isinstance(deid, DeidRecipe):
if deid is None:
deid = "dicom"
deid = DeidRecipe(deid)
if isinstance(dicom_files, list):
return _has_burned_pixels_multi(dicom_files, force, deid)
return _has_burned_pixels_single(dicom_files, force, deid)
################################################################################
# Replacing Identifiers
#
# The %header section of a deid recipe defines a set of actions and associated
# fields to perform them on. As we saw in the examples above, we could easily
# view and filter the actions based on the header field or action type.
#
# For this next section, we will pretend that we've just extracted ids from
# our data files (in a dictionary called ids) and we will prepare a second
# dictionary of updated fields.
##################################
# Load the dummy / example deid
path = os.path.abspath("%s/../examples/deid/" % get_installdir())
recipe = DeidRecipe(deid=path)
# What actions are defined?
recipe.get_actions()
# [{'action': 'ADD', 'field': 'PatientIdentityRemoved', 'value': 'Yes'},
# {'action': 'REPLACE', 'field': 'PatientID', 'value': 'var:id'},
# {'action': 'REPLACE', 'field': 'SOPInstanceUID', 'value': 'var:source_id'}]
# The above says that we are going to:
# ADD a field PatientIdentityRemoved with value Yes
# REPLACE PatientID with whatever value is under "id" in our updated lookup
# REPLACE SOPInstanceUID with whatever value is under "source_id"
# We have 7 dicom cookie images we loaded above, so we have two options. We can
# either loop through the dictionary of ids and update values (in this case,
# adding values to be used as new variables) or we can make a new datastructure
add_padding=False,
margin=3,
deid=None,
font=None,
force=True,
):
if output_folder is None:
output_folder = get_temporary_name(prefix="clean")
if font is None:
font = self.default_font()
self.font = font
self.cmap = "gray"
self.output_folder = output_folder
self.recipe = DeidRecipe(deid)
self.results = None
self.force = force
# We can look at the criteria loaded:
recipe.deid
# You can also provide your own deid recipe file, and in doing so, you
# won't load a default
path = os.path.abspath("%s/../examples/deid/" % get_installdir())
recipe = DeidRecipe(deid=path)
# You can also choose to load the default base with your own recipe
recipe = DeidRecipe(deid=path, base=True)
# Or specify a different base entirely. The base is the deid. in the
# deid.data folder. So for example, under deid/data/deid.dicom.chest.xray we would
# do:
recipe = DeidRecipe(deid=path, base=True, default_base="dicom.xray.chest")
# We can also specify one of the deid recipes provided by the library as our
# only to use.
recipe = DeidRecipe(deid="dicom.xray.chest")
# This is to encourage sharing! If you have a general recipe that others might
# use, please contribute it to the library.
# You can also load multiple deid files, just put them in a list in the order
# that you want them loaded.
# To see an entire (raw in a dictionary) recipe just look at
recipe.deid
# Now let's look at parts of this recipe. There are two categories of things:
# - filters: are used for parsing over headers and flagging images