使用默认和自定义规则匿名化dicom文件的程序

dicom-anonymizer的Python项目详细描述


双氰胺

匿名DICOM文件的Python包。 标准的匿名化答案。有关匿名dicom字段的详细信息,请参阅here。在

此包的默认行为是匿名dicomfields中引用的DICOM字段。在

Dicom字段被分成不同的组。每个小组将以不同的方式匿名。在

GroupActionAction definition
D_TAGSreplaceReplace with a non-zero length value that may be a dummy value and consistent with the VR**
Z_TAGSemptyReplace with a zero length value, or a non-zero length value that may be a dummy value and consistent with the VR**
X_TAGSdeleteCompletely remove the tag
U_TAGSreplaceUIDReplace all UID's number with a random one in order to keep consistent. Same UID will have the same replaced value
Z_D_TAGSemptyOrReplaceReplace with a non-zero length value that may be a dummy value and consistent with the VR**
X_Z_TAGSdeleteOrEmptyReplace with a zero length value, or a non-zero length value that may be a dummy value and consistent with the VR**
X_D_TAGSdeleteOrReplaceReplace with a non-zero length value that may be a dummy value and consistent with the VR**
X_Z_D_TAGSdeleteOrEmptyOrReplaceReplace with a non-zero length value that may be a dummy value and consistent with the VR**
X_Z_U_STAR_TAGSdeleteOrEmptyOrReplaceUIDIf it's a UID, then all numbers are randomly replaced. Else, replace with a zero length value, or a non-zero length value that may be a dummy value and consistent with the VR**
ALL_TAGSContains all previous defined tags

如何建造它?在

源文件可以使用以下方法打包: python .\setup.py bdist_wheel

此命令将在dist文件夹中生成一个wheel包,然后可以使用 pip install .\dist\DicomAnonymizer-0.0.1-py2.py3-none-any.whl

安装此软件包还将安装名为dicom-anonymizer的可执行文件。为了使用它,请参考下一节。在

如何使用它?在

此软件包允许匿名选择DICOM字段(已定义或已覆盖)。 DICOM字段如何匿名的方法也可以被覆盖。在

  • [required]InputPath=单个DICOM映像或包含DICOM文件的文件夹的完整路径
  • [required]OutputPath=匿名DICOM映像或文件夹的完整路径。此文件夹必须存在。在
  • [可选]ActionName=定义了一个将应用于DICOM标记的操作名称。在
  • [可选]Dictionary=JSON文件的路径,该文件定义将应用于特定dicom标记的操作(见下文)

违约行为

您可以使用上面描述的默认匿名行为。在

dicom-anonymizerInputOutput

自定义规则

您可以手动添加新规则,以便对某些标记具有不同的行为。 这将允许您覆盖默认规则:

可执行文件

^{pr2}$

这将把ActionName应用于标记'(0x0001, 0x0001)',并将{}应用到'(0x0001, 0x0005)'

Note:ActionName必须在actions list中定义

例1:患者ID的默认行为是用空值或空值替换。如果要保留此值,则必须运行:

pythonanonymizer.pyInputFilePathOutputFilePath-t'(0x0010, 0x0020)'keep

此命令将覆盖对此标记执行的默认行为,并保留患者的ID。在

示例2:我们只想将研究日期从20080701更改为20080000,然后使用regexp

pythonanonymizer.pyInputFilePathOutputFilePath-t'(0x0008, 0x0020)''regexp''0701$''0000'

带词典文件的自定义规则

您可以通过创建一个json文件dictionary.json来创建自己的字典,而不是使用一个大的命令行来执行几个新操作:

{"(0x0002, 0x0002)":"ActionName","(0x0003, 0x0003)":"ActionName","(0x0004, 0x0004)":"ActionName","(0x0005, 0x0005)":"ActionName"}

与之前一样,必须在actions list中定义ActionName。在

dicom-anonymizerInputFilePathOutputFilePath--dictionarydictionary.json

如果要在字典中使用regexp操作:

{"(0x0002, 0x0002)":"ActionName","(0x0008, 0x0020)":{"action":"regexp","find":"0701$","replace":"0000"}}

自定义/覆盖操作

下面是一个小示例,它保留了所有元数据,但更新了序列描述 通过添加作为参数传递的后缀。在

importargparsefromdicomanonymizerimport*defmain():parser=argparse.ArgumentParser(add_help=True)parser.add_argument('input',help='Path to the input dicom file or input directory which contains dicom files')parser.add_argument('output',help='Path to the output dicom file or output directory which will contains dicom files')parser.add_argument('--suffix',action='store',help='Suffix that will be added at the end of series description')args=parser.parse_args()input_dicom_path=args.inputoutput_dicom_path=args.outputextraAnonymizationRules={}defsetupSeriesDescription(dataset,tag):element=dataset.get(tag)ifelementisnotNone:element.value=element.value+'-'+args.suffix# ALL_TAGS variable is defined on file dicomfields.py# the 'keep' method is already defined into the dicom-anonymizer# It will overrides the default behaviourforiinallTags:extraAnonymizationRules[i]=keepifargs.suffix:extraAnonymizationRules[(0x0008,0x103E)]=setupSeriesDescription# Launch the anonymizationanonymize(input_dicom_path,output_dicom_path,extraAnonymizationRules)if__name__=="__main__":main()

在您自己的文件中,您必须定义:

  • 你的自定义函数。请小心,您的函数总是在输入中包含一个数据集和一个标记
  • 将函数映射到标记的字典

不使用dicom文件匿名dicom标记

如果出于某种原因,您需要匿名dicom字段而不需要初始dicom文件(例如从数据库中提取)。以下是如何做到这一点:

fromdicomanonymizerimport*defmain():# Create a list of tags object that should contains id, type and valuefields=[{# Replaced by Anonymized'id':(0x0040,0xA123),'type':'LO','value':'Annie de la Fontaine',},{# Replaced with empty value'id':(0x0008,0x0050),'type':'TM','value':'bar',},{# Deleted'id':(0x0018,0x4000),'type':'VR','value':'foo',}]# Create a readable dataset for pydicomdata=pydicom.Dataset()# Add each field into the datasetforfieldinfields:data.add_new(field['id'],field['type'],field['value'])anonymizeDataset(data)if__name__=="__main__":main()

有关pydicom数据集的更多信息,请参考here。 您也可以像以前一样添加措辞:

dictionary={}defnewMethod(dataset,tag):element=dataset.get(tag)ifelementisnotNone:element.value=element.value+'- generated with new method'dictionary[(0x0008,0x103E)]=newMethodanonymizeDataset(data,dictionary)

操作列表

^{tb2}$

**VR:价值表征

最初由埃德恩·豪蒙特完成的工作

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java无法将自定义数据类型转换为字符串?   JavaLog4j和appender,这个Log4j定义正确吗?   用于换行的java Android Eclipse拆分   与某个方法关联的java启用/禁用JButton   java小部件列表视图加载视图   java国家/地区名称中的正则表达式   从Java调用Kotlin时,如何获取错误的行号?   java将视图传递给AsyncTask以访问findViewById   java SQL性能:多个绑定还是绑定到一个SQL变量以供重用?   BluetoothAdapter上的安卓 Java NullPointerException。isEnabled()   在clojure中取消引用java方法   JAVA网SocketException:IP_添加_成员身份失败(硬件筛选器不足?)   java从类对象的方法接收nullpointer异常   java使用for循环创建多个对象   java无法使用NTLM身份验证apache camel cxf   java Eclipse不喜欢@Override注释   java Spark SQL模拟红移(Oracle)“系统日期”或“当前日期”