pacsifier 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pacsifier/__init__.py +0 -0
- pacsifier/cli/__init__.py +8 -0
- pacsifier/cli/add_karnak_tags.py +230 -0
- pacsifier/cli/anonymize_dicoms.py +685 -0
- pacsifier/cli/create_dicomdir.py +198 -0
- pacsifier/cli/docker_wrappers.py +460 -0
- pacsifier/cli/extract_carestream_report.py +274 -0
- pacsifier/cli/get_pseudonyms.py +477 -0
- pacsifier/cli/move_dumps.py +98 -0
- pacsifier/cli/pacsifier.py +863 -0
- pacsifier/core/__init__.py +0 -0
- pacsifier/core/dcmtk/__init__.py +0 -0
- pacsifier/core/dcmtk/commands.py +429 -0
- pacsifier/core/sanity_checks.py +286 -0
- pacsifier/info.py +55 -0
- pacsifier-1.0.0.dist-info/METADATA +309 -0
- pacsifier-1.0.0.dist-info/RECORD +21 -0
- pacsifier-1.0.0.dist-info/WHEEL +5 -0
- pacsifier-1.0.0.dist-info/entry_points.txt +15 -0
- pacsifier-1.0.0.dist-info/licenses/LICENSE +201 -0
- pacsifier-1.0.0.dist-info/top_level.txt +1 -0
pacsifier/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
# Copyright 2018-2024 Lausanne University Hospital and University of Lausanne,
|
|
2
|
+
# Switzerland & Contributors
|
|
3
|
+
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
"""Add private DICOM tags to several studies so that Karnak can de-identify them
|
|
17
|
+
using provided patient codes and route them to the appropriate Kheops album."""
|
|
18
|
+
|
|
19
|
+
import sys
|
|
20
|
+
import pydicom
|
|
21
|
+
from glob import glob
|
|
22
|
+
import os
|
|
23
|
+
from progressbar import ProgressBar
|
|
24
|
+
import json
|
|
25
|
+
from typing import Dict
|
|
26
|
+
import argparse
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def tag_dicom_file(
|
|
30
|
+
filename: str,
|
|
31
|
+
patient_code: str,
|
|
32
|
+
patient_shift: str,
|
|
33
|
+
album_name: str,
|
|
34
|
+
) -> None:
|
|
35
|
+
"""Tag the dicom image located at filename by adding patient code and
|
|
36
|
+
Kheops album name to private tags for subsequent de-identification.
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
filename: path to dicom image
|
|
40
|
+
patient_code: pseudonymous patient code
|
|
41
|
+
album_name: Kheops album name
|
|
42
|
+
|
|
43
|
+
"""
|
|
44
|
+
# Load the current dicom file to 'deidentify'
|
|
45
|
+
try:
|
|
46
|
+
dataset = pydicom.read_file(filename)
|
|
47
|
+
except pydicom.errors.InvalidDicomError as e:
|
|
48
|
+
print("Dicom Error at file at path: " + filename)
|
|
49
|
+
print(e)
|
|
50
|
+
|
|
51
|
+
block = dataset.private_block(0x000B, "CHUV_DEV_TML", create=True)
|
|
52
|
+
block.add_new(0x01, "LO", patient_code) # Patient Code
|
|
53
|
+
block.add_new(0x02, "LO", album_name) # Album name
|
|
54
|
+
block.add_new(0x03, "LO", patient_shift) # Day shift per patient
|
|
55
|
+
|
|
56
|
+
# write the modified DICOM
|
|
57
|
+
dataset.save_as(filename)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def tag_all_dicoms_within_root_folder(
|
|
61
|
+
data_path: str,
|
|
62
|
+
new_ids: Dict[str, str],
|
|
63
|
+
day_shift: Dict[str, str],
|
|
64
|
+
album_name: str
|
|
65
|
+
) -> None:
|
|
66
|
+
"""Tag all dicom images located at the datapath for Karnak, adding an
|
|
67
|
+
album name and patientCode private tags.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
data_path: path to the dicom images
|
|
71
|
+
new_ids: real:code mapping to be used after de-identifying the original ids
|
|
72
|
+
day_shift: day shift per patient
|
|
73
|
+
album_name: name of the Kheops album
|
|
74
|
+
|
|
75
|
+
"""
|
|
76
|
+
pattern_dicom_files = os.path.join("ses-*", "*", "*")
|
|
77
|
+
|
|
78
|
+
# List all patient directories.
|
|
79
|
+
patients_folders = next(os.walk(data_path))[1]
|
|
80
|
+
# Keep only sub-XXX folders
|
|
81
|
+
patients_folders = [p for p in patients_folders if p.startswith("sub-")]
|
|
82
|
+
|
|
83
|
+
if not patients_folders:
|
|
84
|
+
raise NotADirectoryError(
|
|
85
|
+
"Each patient should have their own directory under the provided root "
|
|
86
|
+
+ data_path
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
if new_ids is None:
|
|
90
|
+
raise ValueError("Must provide a dictionary mapping real to coded patientID")
|
|
91
|
+
|
|
92
|
+
if album_name is None:
|
|
93
|
+
raise ValueError("Must provide non-empty Kheops album name")
|
|
94
|
+
|
|
95
|
+
# Keep a mapping from old to new ids in a dictionary.
|
|
96
|
+
old2new_idx = {
|
|
97
|
+
patients_folders[i]: new_ids[patients_folders[i]]
|
|
98
|
+
for i in range(len(patients_folders))
|
|
99
|
+
}
|
|
100
|
+
personal_day_shift = {
|
|
101
|
+
patients_folders[i]: (
|
|
102
|
+
day_shift[patients_folders[i]] if patients_folders[i] in day_shift else 0
|
|
103
|
+
)
|
|
104
|
+
for i in range(len(patients_folders))
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
# TODO add Cerberus validation on album_name and newid - both should be DICOM VR LO
|
|
108
|
+
|
|
109
|
+
# Loop over patients...
|
|
110
|
+
progress = ProgressBar()
|
|
111
|
+
for patient in progress(patients_folders):
|
|
112
|
+
new_id = old2new_idx[patient]
|
|
113
|
+
patient_shift = personal_day_shift[patient]
|
|
114
|
+
current_path = os.path.join(data_path, patient, pattern_dicom_files)
|
|
115
|
+
|
|
116
|
+
# List all files within patient folder
|
|
117
|
+
all_filenames = glob(current_path)
|
|
118
|
+
|
|
119
|
+
if not all_filenames:
|
|
120
|
+
raise FileNotFoundError(
|
|
121
|
+
"Patient directories are expect to conform to the quasi-BIDS organisation "
|
|
122
|
+
"sub-XXX/ses-YYY/"
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
# List all study dirs for this patient.
|
|
126
|
+
study_dirs = next(os.walk(os.path.join(data_path, patient)))[1]
|
|
127
|
+
|
|
128
|
+
for study_dir in study_dirs:
|
|
129
|
+
# List all series dirs for this patient.
|
|
130
|
+
series_dirs = next(os.walk(os.path.join(data_path, patient, study_dir)))[1]
|
|
131
|
+
|
|
132
|
+
for series_dir in series_dirs:
|
|
133
|
+
all_filenames_series = glob(
|
|
134
|
+
os.path.join(data_path, patient, study_dir, series_dir, "*")
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
# Loop over all dicom files within a patient directory and anonymize them.
|
|
138
|
+
for filename in all_filenames_series:
|
|
139
|
+
tag_dicom_file(
|
|
140
|
+
os.path.join(
|
|
141
|
+
data_path,
|
|
142
|
+
patient,
|
|
143
|
+
study_dir,
|
|
144
|
+
series_dir,
|
|
145
|
+
os.path.basename(filename),
|
|
146
|
+
),
|
|
147
|
+
patient_code=new_id,
|
|
148
|
+
patient_shift=patient_shift,
|
|
149
|
+
album_name=album_name,
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def get_parser() -> argparse.ArgumentParser:
|
|
154
|
+
"""Get parser object for command line arguments of the script."""
|
|
155
|
+
parser = argparse.ArgumentParser(
|
|
156
|
+
description=(
|
|
157
|
+
"Add private DICOM tags to several studies so that Karnak can "
|
|
158
|
+
"de-identify them using provided patient codes and route them to "
|
|
159
|
+
"the appropriate Kheops album."
|
|
160
|
+
)
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
parser.add_argument(
|
|
164
|
+
"--in_folder",
|
|
165
|
+
"-d",
|
|
166
|
+
help="Directory to the dicom files",
|
|
167
|
+
default=os.path.join(".", "data"),
|
|
168
|
+
required=True,
|
|
169
|
+
)
|
|
170
|
+
parser.add_argument(
|
|
171
|
+
"--new_ids", "-n", help="List of new patient IDentifiers", required=True
|
|
172
|
+
)
|
|
173
|
+
parser.add_argument(
|
|
174
|
+
"--day_shift", "-s", help="List of day shift per patient", required=False
|
|
175
|
+
)
|
|
176
|
+
parser.add_argument(
|
|
177
|
+
"--album_name",
|
|
178
|
+
"-a",
|
|
179
|
+
help="Name of destination Kheops album to route the tagged study",
|
|
180
|
+
required=True,
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
return parser
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def main():
|
|
187
|
+
"""Main function of the script that calls :func:`tag_all_dicoms_within_root_folder`."""
|
|
188
|
+
# Create the parser object and parse command line arguments
|
|
189
|
+
parser = get_parser()
|
|
190
|
+
args = parser.parse_args()
|
|
191
|
+
|
|
192
|
+
data_path = os.path.normcase(os.path.abspath(os.path.expanduser(args.in_folder)))
|
|
193
|
+
new_ids_path = os.path.normcase(os.path.abspath(os.path.expanduser(args.new_ids)))
|
|
194
|
+
album_name = args.album_name
|
|
195
|
+
|
|
196
|
+
if not os.path.isdir(data_path):
|
|
197
|
+
print(f"Input directory ({data_path}) does not exist. Please check!")
|
|
198
|
+
sys.exit(1)
|
|
199
|
+
|
|
200
|
+
if not os.path.isfile(new_ids_path):
|
|
201
|
+
print(f"New IDs file ({new_ids_path}) not found. Please check!")
|
|
202
|
+
sys.exit(1)
|
|
203
|
+
|
|
204
|
+
try:
|
|
205
|
+
new_ids = json.load(open(new_ids_path, "r"))
|
|
206
|
+
except json.JSONDecodeError:
|
|
207
|
+
print(
|
|
208
|
+
f"New IDs file ({new_ids_path}) is not a valid JSON file. "
|
|
209
|
+
"Please check it!"
|
|
210
|
+
)
|
|
211
|
+
sys.exit(1)
|
|
212
|
+
|
|
213
|
+
if args.day_shift is not None:
|
|
214
|
+
day_shift_path = os.path.normcase(os.path.abspath(os.path.expanduser(args.day_shift)))
|
|
215
|
+
day_shift = json.load(open(day_shift_path, "r"))
|
|
216
|
+
else:
|
|
217
|
+
day_shift = {}
|
|
218
|
+
|
|
219
|
+
if not os.path.isdir(data_path):
|
|
220
|
+
raise NotADirectoryError("Input directory does not exist. Please check.")
|
|
221
|
+
|
|
222
|
+
print("Tagging DICOM files within path {}".format(data_path))
|
|
223
|
+
|
|
224
|
+
tag_all_dicoms_within_root_folder(
|
|
225
|
+
data_path=data_path, new_ids=new_ids, day_shift=day_shift, album_name=album_name
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
if __name__ == "__main__":
|
|
230
|
+
main()
|