-
Notifications
You must be signed in to change notification settings - Fork 0
/
deidentify.py
166 lines (156 loc) · 4.26 KB
/
deidentify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# -*- coding: utf-8 -*-
"""
Anonymize anything (medical)
Authors: Moritz Rempe & Lukas Heine
"""
import argparse
from dicom_skullstrip_defacing import Inference
from dicom_deidentification import DicomDeidentifier
from text_detection import TextRemoval
from wsi_deidentification import WSIDeidentifier
from twix_deidentification import anonymize_twix
import torch
import os
import logging
logging.basicConfig(level=logging.INFO)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="deidentify.py", epilog="If you find this useful, please cite our work."
)
parser.add_argument(
"-v",
"--verbose",
required=False,
default=True,
action=argparse.BooleanOptionalAction,
)
parser.add_argument(
"-t",
"--text-removal",
required=False,
default=False,
action=argparse.BooleanOptionalAction,
)
parser.add_argument(
"-i",
"--input",
required=False,
default=None,
help="Path to the input data.",
)
parser.add_argument(
"-o",
"--output",
type=str,
required=False,
default=None,
help="Path to save the output data.",
)
parser.add_argument(
"--gpu",
type=int,
default=0,
help="GPU device number. (default 0)",
)
parser.add_argument(
"-s",
"--skull_strip",
required=False,
default=False,
action=argparse.BooleanOptionalAction,
)
parser.add_argument(
"-de",
"--deface",
required=False,
default=False,
action=argparse.BooleanOptionalAction,
)
parser.add_argument(
"-tw",
"--twix",
required=False,
default=False,
action=argparse.BooleanOptionalAction,
)
parser.add_argument(
"-w",
"--wsi",
required=False,
default=False,
action=argparse.BooleanOptionalAction,
help="Anonymize WSI label and overview file.",
)
parser.add_argument(
"-p",
"--processes",
required=False,
default=1,
type=int,
help="Number of processes to use for multiprocessing.",
)
parser.add_argument(
"-d",
"--deidentification-profile",
choices=[
"basicProfile",
"cleanDescOpt",
"cleanGraphOpt",
"cleanStructContOpt",
"rtnDevIdOpt",
"rtnInstIdOpt",
"rtnLongFullDatesOpt",
"rtnLongModifDatesOpt",
"rtnPatCharsOpt",
"rtnSafePrivOpt",
"rtnUIDsOpt",
],
required=False,
nargs="+",
default=None,
help="Which DICOM deidentification profile(s) to apply. (default None)",
)
parser.parse_args()
args = parser.parse_args()
torch.backends.cudnn.benchmark = True
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
torch.autograd.set_detect_anomaly(True)
torch.set_num_threads(args.processes)
_input = args.input
_out = args.output
if args.deidentification_profile is not None:
dicom_deidentifier = DicomDeidentifier(
args.deidentification_profile,
processes=args.processes,
out_path=args.output,
verbose=args.verbose,
)
dicom_deidentifier(_input)
_input = _out
else:
logging.info(
"No DICOM deidentification profile specified. No Metadata anonymization will be performed!"
)
if args.wsi:
wsi_deidentifier = WSIDeidentifier(verbose=args.verbose, out_path=args.output)
wsi_deidentifier(_input)
_input = _out
if args.skull_strip:
skull_strip = Inference(
output_path=args.output, gpu=args.gpu, skullstrip=True, verbose=args.verbose
)
skull_strip(_input)
_input = _out
if args.deface:
deface = Inference(
output_path=args.output, gpu=args.gpu, deface=True, verbose=args.verbose
)
deface(_input)
_input = _out
if args.twix:
anonymize_twix(_input, args.output)
_input = _out
if args.text_removal:
txt_removal = TextRemoval()
txt_removal(_input)