1
1
import sys
2
+
2
3
if sys .version_info >= (3 , 8 ):
3
4
import importlib .metadata as metadata
4
5
else :
8
9
import ast
9
10
import json
10
11
import os
11
- import sys
12
12
import tqdm
13
13
14
- from dicomanonymizer .simpledicomanonymizer import anonymize_dicom_file , ActionsMapNameFunctions
14
+ from dicomanonymizer .simpledicomanonymizer import (
15
+ anonymize_dicom_file ,
16
+ ActionsMapNameFunctions ,
17
+ )
15
18
16
19
17
20
def isDICOMType (filePath ):
18
21
"""
19
22
:returns True if input file is a DICOM File. False otherwise.
20
23
"""
21
24
try :
22
- with open (filePath , 'rb' ) as tempFile :
25
+ with open (filePath , "rb" ) as tempFile :
23
26
tempFile .seek (0x80 , os .SEEK_SET )
24
- return tempFile .read (4 ) == b' DICM'
27
+ return tempFile .read (4 ) == b" DICM"
25
28
except IOError :
26
29
return False
27
30
28
31
29
- def anonymize (input_path : str , output_path : str , anonymization_actions : dict , delete_private_tags : bool ) -> None :
32
+ def anonymize (
33
+ input_path : str ,
34
+ output_path : str ,
35
+ anonymization_actions : dict ,
36
+ delete_private_tags : bool ,
37
+ ) -> None :
30
38
"""
31
39
Read data from input path (folder or file) and launch the anonymization.
32
40
@@ -37,37 +45,42 @@ def anonymize(input_path: str, output_path: str, anonymization_actions: dict, de
37
45
:param deletePrivateTags: Whether to delete private tags.
38
46
"""
39
47
# Get input arguments
40
- input_folder = ''
41
- output_folder = ''
48
+ input_folder = ""
49
+ output_folder = ""
42
50
43
51
if os .path .isdir (input_path ):
44
52
input_folder = input_path
45
53
46
54
if os .path .isdir (output_path ):
47
55
output_folder = output_path
48
- if input_folder == '' :
56
+ if input_folder == "" :
49
57
output_path = os .path .join (output_folder , os .path .basename (input_path ))
50
58
51
- if input_folder != '' and output_folder == '' :
52
- print (' Error, please set a correct output folder path' )
59
+ if input_folder != "" and output_folder == "" :
60
+ print (" Error, please set a correct output folder path" )
53
61
sys .exit ()
54
62
55
63
# Generate list of input file if a folder has been set
56
64
input_files_list = []
57
65
output_files_list = []
58
- if input_folder == '' :
66
+ if input_folder == "" :
59
67
input_files_list .append (input_path )
60
68
output_files_list .append (output_path )
61
69
else :
62
70
files = os .listdir (input_folder )
63
71
for fileName in files :
64
- if isDICOMType (input_folder + '/' + fileName ):
65
- input_files_list .append (input_folder + '/' + fileName )
66
- output_files_list .append (output_folder + '/' + fileName )
72
+ if isDICOMType (input_folder + "/" + fileName ):
73
+ input_files_list .append (input_folder + "/" + fileName )
74
+ output_files_list .append (output_folder + "/" + fileName )
67
75
68
76
progress_bar = tqdm .tqdm (total = len (input_files_list ))
69
77
for cpt in range (len (input_files_list )):
70
- anonymize_dicom_file (input_files_list [cpt ], output_files_list [cpt ], anonymization_actions , delete_private_tags )
78
+ anonymize_dicom_file (
79
+ input_files_list [cpt ],
80
+ output_files_list [cpt ],
81
+ anonymization_actions ,
82
+ delete_private_tags ,
83
+ )
71
84
progress_bar .update (1 )
72
85
73
86
progress_bar .close ()
@@ -83,7 +96,9 @@ def parse_tag_actions_arguments(t_arguments: list, new_anonymization_actions: di
83
96
for current_tag_parameters in t_arguments :
84
97
nb_parameters = len (current_tag_parameters )
85
98
if nb_parameters < 2 :
86
- raise ValueError ("-t parameters should always have 2 values: tag and action" )
99
+ raise ValueError (
100
+ "-t parameters should always have 2 values: tag and action"
101
+ )
87
102
88
103
tag = current_tag_parameters [0 ]
89
104
action_name = current_tag_parameters [1 ]
@@ -96,9 +111,15 @@ def parse_tag_actions_arguments(t_arguments: list, new_anonymization_actions: di
96
111
action_arguments = current_tag_parameters [2 :]
97
112
98
113
if len (action_arguments ) != action_object .number_of_expected_arguments :
99
- raise ValueError (f"Wrong number of arguments for action { action_name } : found { len (action_arguments )} " )
100
-
101
- action = action_object .function if not len (action_arguments ) else action_object .function (action_arguments )
114
+ raise ValueError (
115
+ f"Wrong number of arguments for action { action_name } : found { len (action_arguments )} "
116
+ )
117
+
118
+ action = (
119
+ action_object .function
120
+ if not len (action_arguments )
121
+ else action_object .function (action_arguments )
122
+ )
102
123
tag_list = [ast .literal_eval (tag )]
103
124
104
125
new_anonymization_actions .update ({tag : action for tag in tag_list })
@@ -116,7 +137,7 @@ def parse_dictionary_argument(dictionary_argument, new_anonymization_actions):
116
137
for tag , action_or_options in data .items ():
117
138
if isinstance (action_or_options , dict ):
118
139
try :
119
- action_name = action_or_options .pop (' action' )
140
+ action_name = action_or_options .pop (" action" )
120
141
except KeyError as e :
121
142
raise ValueError (f"Missing field in tag { tag } : { e .args [0 ]} " )
122
143
try :
@@ -125,7 +146,8 @@ def parse_dictionary_argument(dictionary_argument, new_anonymization_actions):
125
146
raise ValueError (f"Action { action_name } is not recognized." )
126
147
if len (action_or_options ) != action_object .number_of_expected_arguments :
127
148
raise ValueError (
128
- f"Wrong number of arguments for action { action_name } : found { len (action_or_options )} " )
149
+ f"Wrong number of arguments for action { action_name } : found { len (action_or_options )} "
150
+ )
129
151
action = action_object .function (action_or_options )
130
152
else :
131
153
try :
@@ -140,18 +162,35 @@ def parse_dictionary_argument(dictionary_argument, new_anonymization_actions):
140
162
def main ():
141
163
version_info = metadata .version ("dicom_anonymizer" )
142
164
parser = argparse .ArgumentParser (add_help = True )
143
- parser .add_argument ('input' , help = 'Path to the input dicom file or input directory which contains dicom files' )
144
165
parser .add_argument (
145
- 'output' , help = 'Path to the output dicom file or output directory which will contains dicom files' )
166
+ "input" ,
167
+ help = "Path to the input dicom file or input directory which contains dicom files" ,
168
+ )
169
+ parser .add_argument (
170
+ "output" ,
171
+ help = "Path to the output dicom file or output directory which will contains dicom files" ,
172
+ )
173
+ parser .add_argument (
174
+ "-t" ,
175
+ action = "append" ,
176
+ nargs = "*" ,
177
+ help = "tags action : Defines a new action to apply on the tag.'regexp' action takes two arguments: "
178
+ "1. regexp to find substring 2. the string that will replace the previous found string" ,
179
+ )
180
+ parser .add_argument (
181
+ "-v" , "--version" , action = "version" , version = f"%(prog)s { version_info } "
182
+ )
183
+ parser .add_argument (
184
+ "--dictionary" ,
185
+ action = "store" ,
186
+ help = "File which contains a dictionary that can be added to the original one" ,
187
+ )
146
188
parser .add_argument (
147
- '-t' , action = 'append' , nargs = '*' ,
148
- help = 'tags action : Defines a new action to apply on the tag.\' regexp\' action takes two arguments: '
149
- '1. regexp to find substring 2. the string that will replace the previous found string' )
150
- parser .add_argument ('-v' , '--version' , action = 'version' , version = f'%(prog)s { version_info } ' )
151
- parser .add_argument ('--dictionary' , action = 'store' ,
152
- help = 'File which contains a dictionary that can be added to the original one' )
153
- parser .add_argument ('--keepPrivateTags' , action = 'store_true' , dest = 'keepPrivateTags' ,
154
- help = 'If used, then private tags won\' t be deleted' )
189
+ "--keepPrivateTags" ,
190
+ action = "store_true" ,
191
+ dest = "keepPrivateTags" ,
192
+ help = "If used, then private tags won't be deleted" ,
193
+ )
155
194
parser .set_defaults (keepPrivateTags = False )
156
195
args = parser .parse_args ()
157
196
@@ -168,8 +207,10 @@ def main():
168
207
parse_dictionary_argument (args .dictionary , new_anonymization_actions )
169
208
170
209
# Launch the anonymization
171
- anonymize (input_path , output_path , new_anonymization_actions , not args .keepPrivateTags )
210
+ anonymize (
211
+ input_path , output_path , new_anonymization_actions , not args .keepPrivateTags
212
+ )
172
213
173
214
174
- if __name__ == ' __main__' :
215
+ if __name__ == " __main__" :
175
216
main ()
0 commit comments