如何在没有全局变量的情况下将值传递给回调以进行匿名化?
How to pass a value to callback for anonymization without global variables?
我正在尝试使用带有 pydicom 的假自定义 AN 来匿名化登录号:
def an_callback(ds, data_element):
if data_element.tag == Tag(0x00080050):
data_element.value = '0000000000'
ds.walk(an_callback)
我想将自定义值而不是“0000000”传递给回调函数。我想我可以使用一个全局变量,但我想避免它以减少不必要的错误。有没有不使用全局变量的不同方法?
编辑:我以为walk是一个特殊的python函数,但它只是ds的一个方法,这是代码。您可以在此处更改回调代码以包含一个可选参数。回调(自我,data_element,replace_value=None)
def walk(self, callback, recursive=True):
"""Iterate through the DataElements and run `callback` on each.
Visit all DataElements, possibly recursing into sequences and their
datasets. The callback function is called for each DataElement
(including SQ element). Can be used to perform an operation on certain
types of DataElements. E.g., `remove_private_tags`() finds all private
tags and deletes them. DataElement`s will come back in DICOM order (by
increasing tag number within their dataset).
Parameters
----------
callback
A callable that takes two arguments:
* a Dataset
* a DataElement belonging to that Dataset
recursive : bool
Flag to indicate whether to recurse into Sequences.
"""
taglist = sorted(self.keys())
for tag in taglist:
with tag_in_exception(tag):
data_element = self[tag]
callback(self, data_element) # self = this Dataset
# 'tag in self' below needed in case callback deleted
# data_element
if recursive and tag in self and data_element.VR == "SQ":
sequence = data_element.value
for dataset in sequence:
dataset.walk(callback)
全局变量是最简单的解决方案;如果你想更优雅,那么你可以将函数封装在一些class中。您的问题涉及普通 Python,因此您可能需要阅读 Python Alternatives to Global Variables.
我正在尝试使用带有 pydicom 的假自定义 AN 来匿名化登录号:
def an_callback(ds, data_element):
if data_element.tag == Tag(0x00080050):
data_element.value = '0000000000'
ds.walk(an_callback)
我想将自定义值而不是“0000000”传递给回调函数。我想我可以使用一个全局变量,但我想避免它以减少不必要的错误。有没有不使用全局变量的不同方法?
编辑:我以为walk是一个特殊的python函数,但它只是ds的一个方法,这是代码。您可以在此处更改回调代码以包含一个可选参数。回调(自我,data_element,replace_value=None)
def walk(self, callback, recursive=True):
"""Iterate through the DataElements and run `callback` on each.
Visit all DataElements, possibly recursing into sequences and their
datasets. The callback function is called for each DataElement
(including SQ element). Can be used to perform an operation on certain
types of DataElements. E.g., `remove_private_tags`() finds all private
tags and deletes them. DataElement`s will come back in DICOM order (by
increasing tag number within their dataset).
Parameters
----------
callback
A callable that takes two arguments:
* a Dataset
* a DataElement belonging to that Dataset
recursive : bool
Flag to indicate whether to recurse into Sequences.
"""
taglist = sorted(self.keys())
for tag in taglist:
with tag_in_exception(tag):
data_element = self[tag]
callback(self, data_element) # self = this Dataset
# 'tag in self' below needed in case callback deleted
# data_element
if recursive and tag in self and data_element.VR == "SQ":
sequence = data_element.value
for dataset in sequence:
dataset.walk(callback)
全局变量是最简单的解决方案;如果你想更优雅,那么你可以将函数封装在一些class中。您的问题涉及普通 Python,因此您可能需要阅读 Python Alternatives to Global Variables.