使用默认和自定义规则匿名化dicom文件的程序
dicom-anonymizer的Python项目详细描述
双氰胺
匿名DICOM文件的Python包。 标准的匿名化答案。有关匿名dicom字段的详细信息,请参阅here。在
此包的默认行为是匿名dicomfields中引用的DICOM字段。在
Dicom字段被分成不同的组。每个小组将以不同的方式匿名。在
Group | Action | Action definition |
---|---|---|
D_TAGS | replace | Replace with a non-zero length value that may be a dummy value and consistent with the VR** |
Z_TAGS | empty | Replace with a zero length value, or a non-zero length value that may be a dummy value and consistent with the VR** |
X_TAGS | delete | Completely remove the tag |
U_TAGS | replaceUID | Replace all UID's number with a random one in order to keep consistent. Same UID will have the same replaced value |
Z_D_TAGS | emptyOrReplace | Replace with a non-zero length value that may be a dummy value and consistent with the VR** |
X_Z_TAGS | deleteOrEmpty | Replace with a zero length value, or a non-zero length value that may be a dummy value and consistent with the VR** |
X_D_TAGS | deleteOrReplace | Replace with a non-zero length value that may be a dummy value and consistent with the VR** |
X_Z_D_TAGS | deleteOrEmptyOrReplace | Replace with a non-zero length value that may be a dummy value and consistent with the VR** |
X_Z_U_STAR_TAGS | deleteOrEmptyOrReplaceUID | If 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_TAGS | Contains 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:价值表征
最初由埃德恩·豪蒙特完成的工作
- 项目
标签: