dbdicom 0.3.0__py3-none-any.whl → 0.3.2__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.
Potentially problematic release.
This version of dbdicom might be problematic. Click here for more details.
- dbdicom/api.py +53 -10
- dbdicom/database.py +122 -0
- dbdicom/dataset.py +22 -71
- dbdicom/dbd.py +124 -182
- dbdicom/external/__pycache__/__init__.cpython-311.pyc +0 -0
- dbdicom/external/dcm4che/__pycache__/__init__.cpython-311.pyc +0 -0
- dbdicom/external/dcm4che/bin/__pycache__/__init__.cpython-311.pyc +0 -0
- dbdicom/register.py +315 -234
- dbdicom/sop_classes/mr_image.py +122 -130
- dbdicom/sop_classes/parametric_map.py +94 -20
- dbdicom/utils/image.py +7 -6
- {dbdicom-0.3.0.dist-info → dbdicom-0.3.2.dist-info}/METADATA +2 -4
- {dbdicom-0.3.0.dist-info → dbdicom-0.3.2.dist-info}/RECORD +16 -15
- {dbdicom-0.3.0.dist-info → dbdicom-0.3.2.dist-info}/WHEEL +1 -1
- {dbdicom-0.3.0.dist-info → dbdicom-0.3.2.dist-info}/licenses/LICENSE +0 -0
- {dbdicom-0.3.0.dist-info → dbdicom-0.3.2.dist-info}/top_level.txt +0 -0
dbdicom/register.py
CHANGED
|
@@ -1,120 +1,173 @@
|
|
|
1
1
|
import os
|
|
2
|
-
import pandas as pd
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
COLUMNS = [
|
|
6
|
-
# Identifiers (unique)
|
|
7
|
-
'PatientID',
|
|
8
|
-
'StudyInstanceUID',
|
|
9
|
-
'SeriesInstanceUID',
|
|
10
|
-
'SOPInstanceUID',
|
|
11
|
-
# Human-readable identifiers (not unique)
|
|
12
|
-
'PatientName',
|
|
13
|
-
'StudyDescription',
|
|
14
|
-
'StudyDate',
|
|
15
|
-
'SeriesDescription',
|
|
16
|
-
'SeriesNumber',
|
|
17
|
-
'InstanceNumber',
|
|
18
|
-
]
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def index(df:pd.DataFrame, entity):
|
|
22
|
-
if isinstance(entity, str):
|
|
23
|
-
rows = (df.removed==False)
|
|
24
|
-
elif len(entity)==2:
|
|
25
|
-
patient_id = uid(df, entity)
|
|
26
|
-
rows = (df.PatientID==patient_id) & (df.removed==False)
|
|
27
|
-
elif len(entity)==3:
|
|
28
|
-
study_uid = uid(df, entity)
|
|
29
|
-
rows = (df.StudyInstanceUID==study_uid) & (df.removed==False)
|
|
30
|
-
elif len(entity)==4:
|
|
31
|
-
series_uid = uid(df, entity)
|
|
32
|
-
rows = (df.SeriesInstanceUID==series_uid) & (df.removed==False)
|
|
33
|
-
return df.index[rows].tolist()
|
|
34
2
|
|
|
35
3
|
|
|
36
|
-
def
|
|
4
|
+
def add_instance(dbtree:list, instance, rel_path):
|
|
5
|
+
|
|
6
|
+
# Get patient and create if needed
|
|
7
|
+
pts = [pt for pt in dbtree if pt['PatientID']==instance['PatientID']]
|
|
8
|
+
if pts==[]:
|
|
9
|
+
pt = {
|
|
10
|
+
'PatientName': instance['PatientName'],
|
|
11
|
+
'PatientID': instance['PatientID'],
|
|
12
|
+
'studies': [],
|
|
13
|
+
}
|
|
14
|
+
dbtree.append(pt)
|
|
15
|
+
else:
|
|
16
|
+
pt = pts[0]
|
|
17
|
+
|
|
18
|
+
# Get study and create if needed
|
|
19
|
+
sts = [st for st in pt['studies'] if st['StudyInstanceUID']==instance['StudyInstanceUID']]
|
|
20
|
+
if sts==[]:
|
|
21
|
+
st = {
|
|
22
|
+
'StudyDescription': instance['StudyDescription'],
|
|
23
|
+
'StudyDate': instance['StudyDate'],
|
|
24
|
+
'StudyInstanceUID': instance['StudyInstanceUID'],
|
|
25
|
+
'series': [],
|
|
26
|
+
}
|
|
27
|
+
pt['studies'].append(st)
|
|
28
|
+
else:
|
|
29
|
+
st = sts[0]
|
|
30
|
+
|
|
31
|
+
# Get series and create if needed
|
|
32
|
+
srs = [sr for sr in st['series'] if sr['SeriesInstanceUID']==instance['SeriesInstanceUID']]
|
|
33
|
+
if srs==[]:
|
|
34
|
+
sr = {
|
|
35
|
+
'SeriesNumber': instance['SeriesNumber'],
|
|
36
|
+
'SeriesDescription': instance['SeriesDescription'],
|
|
37
|
+
'SeriesInstanceUID': instance['SeriesInstanceUID'],
|
|
38
|
+
'instances': {},
|
|
39
|
+
}
|
|
40
|
+
st['series'].append(sr)
|
|
41
|
+
else:
|
|
42
|
+
sr = srs[0]
|
|
43
|
+
|
|
44
|
+
# Add instance
|
|
45
|
+
sr['instances'][instance['InstanceNumber']] = rel_path
|
|
46
|
+
|
|
47
|
+
return dbtree
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def files(dbtree, entity):
|
|
37
51
|
# Raises an error if the entity does not exist or has no files
|
|
38
|
-
|
|
39
|
-
relpath = index(df, entity)
|
|
52
|
+
relpath = index(dbtree, entity)
|
|
40
53
|
if relpath==[]:
|
|
41
54
|
raise ValueError(f'No files in entity {entity}')
|
|
42
55
|
if isinstance(entity, str):
|
|
43
56
|
return [os.path.join(entity, f) for f in relpath]
|
|
44
57
|
else:
|
|
45
58
|
return [os.path.join(entity[0], f) for f in relpath]
|
|
59
|
+
|
|
46
60
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
61
|
+
def index(dbtree, entity):
|
|
62
|
+
if isinstance(entity, str):
|
|
63
|
+
idx = []
|
|
64
|
+
for pt in dbtree:
|
|
65
|
+
for st in pt['studies']:
|
|
66
|
+
for sr in st['series']:
|
|
67
|
+
idx += list(sr['instances'].values())
|
|
68
|
+
return idx
|
|
69
|
+
elif len(entity)==2:
|
|
70
|
+
patient_id = uid(dbtree, entity)
|
|
71
|
+
idx = []
|
|
72
|
+
for pt in dbtree:
|
|
73
|
+
if pt['PatientID'] == patient_id:
|
|
74
|
+
for st in pt['studies']:
|
|
75
|
+
for sr in st['series']:
|
|
76
|
+
idx += list(sr['instances'].values())
|
|
77
|
+
return idx
|
|
78
|
+
elif len(entity)==3:
|
|
79
|
+
study_uid = uid(dbtree, entity)
|
|
80
|
+
idx = []
|
|
81
|
+
for pt in dbtree:
|
|
82
|
+
for st in pt['studies']:
|
|
83
|
+
if st['StudyInstanceUID'] == study_uid:
|
|
84
|
+
for sr in st['series']:
|
|
85
|
+
idx += list(sr['instances'].values())
|
|
86
|
+
return idx
|
|
87
|
+
elif len(entity)==4:
|
|
88
|
+
series_uid = uid(dbtree, entity)
|
|
89
|
+
for pt in dbtree:
|
|
90
|
+
for st in pt['studies']:
|
|
91
|
+
for sr in st['series']:
|
|
92
|
+
if sr['SeriesInstanceUID'] == series_uid:
|
|
93
|
+
return list(sr['instances'].values())
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def drop(dbtree, relpaths):
|
|
97
|
+
for pt in dbtree[:]:
|
|
98
|
+
for st in pt['studies'][:]:
|
|
99
|
+
for sr in st['series'][:]:
|
|
100
|
+
for nr, relpath in list(sr['instances'].items()):
|
|
101
|
+
if relpath in relpaths:
|
|
102
|
+
del sr['instances'][nr]
|
|
103
|
+
if sr['instances'] == []:
|
|
104
|
+
st['series'].remove(sr)
|
|
105
|
+
if st['series'] == []:
|
|
106
|
+
pt['studies'].remove(st)
|
|
107
|
+
return dbtree
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
# def entity(df, path, uid):# information entity from uid
|
|
111
|
+
# dbtree = tree(df)
|
|
112
|
+
# patient_idx = {}
|
|
113
|
+
# for pt in dbtree:
|
|
114
|
+
# patient_name = pt['PatientName']
|
|
115
|
+
# uid_patient = pt['PatientID']
|
|
116
|
+
# if patient_name in patient_idx:
|
|
117
|
+
# patient_idx[patient_name] += 1
|
|
118
|
+
# else:
|
|
119
|
+
# patient_idx[patient_name] = 0
|
|
120
|
+
# patient_desc = (patient_name, patient_idx[patient_name])
|
|
121
|
+
# if uid == uid_patient:
|
|
122
|
+
# return [path, patient_desc]
|
|
68
123
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
124
|
+
# else:
|
|
125
|
+
|
|
126
|
+
# study_idx = {}
|
|
127
|
+
# for st in pt['studies']:
|
|
128
|
+
# study_name = st['StudyDescription']
|
|
129
|
+
# uid_study = st['StudyInstanceUID']
|
|
130
|
+
# if study_name in study_idx:
|
|
131
|
+
# study_idx[study_name] += 1
|
|
132
|
+
# else:
|
|
133
|
+
# study_idx[study_name] = 0
|
|
134
|
+
# study_desc = (study_name, study_idx[study_name])
|
|
135
|
+
# if uid == uid_study:
|
|
136
|
+
# return [path, patient_desc, study_desc]
|
|
82
137
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
138
|
+
# else:
|
|
139
|
+
|
|
140
|
+
# series_idx = {}
|
|
141
|
+
# for sr in st['series']:
|
|
142
|
+
# series_name = sr['SeriesDescription']
|
|
143
|
+
# uid_series = sr['SeriesInstanceUID']
|
|
144
|
+
# if series_name in series_idx:
|
|
145
|
+
# series_idx[series_name] += 1
|
|
146
|
+
# else:
|
|
147
|
+
# series_idx[series_name] = 0
|
|
148
|
+
# series_desc = (series_name, series_idx[series_name])
|
|
149
|
+
# if uid == uid_series:
|
|
150
|
+
# return [path, patient_desc, study_desc, series_desc]
|
|
96
151
|
|
|
97
|
-
|
|
152
|
+
# raise ValueError(f"No information entity with UID {uid} was found.")
|
|
98
153
|
|
|
99
154
|
|
|
100
|
-
def uid(
|
|
101
|
-
df = df[df.removed == False]
|
|
155
|
+
def uid(dbtree, entity): # uid from entity
|
|
102
156
|
if len(entity)==2:
|
|
103
|
-
return _patient_uid(
|
|
157
|
+
return _patient_uid(dbtree, entity)
|
|
104
158
|
if len(entity)==3:
|
|
105
|
-
return _study_uid(
|
|
159
|
+
return _study_uid(dbtree, entity)
|
|
106
160
|
if len(entity)==4:
|
|
107
|
-
return _series_uid(
|
|
161
|
+
return _series_uid(dbtree, entity)
|
|
108
162
|
|
|
109
163
|
|
|
110
|
-
def _patient_uid(
|
|
164
|
+
def _patient_uid(dbtree, patient):
|
|
111
165
|
patient = patient[1]
|
|
112
|
-
df = df[df.removed == False]
|
|
113
166
|
patients = {}
|
|
114
167
|
patient_idx = {}
|
|
115
|
-
for
|
|
116
|
-
|
|
117
|
-
|
|
168
|
+
for pt in dbtree:
|
|
169
|
+
patient_name = pt['PatientName']
|
|
170
|
+
uid_patient = pt['PatientID']
|
|
118
171
|
if patient_name in patient_idx:
|
|
119
172
|
patient_idx[patient_name] += 1
|
|
120
173
|
else:
|
|
@@ -136,76 +189,75 @@ def _patient_uid(df, patient):
|
|
|
136
189
|
raise ValueError(f"Patient {patient} not found in database.")
|
|
137
190
|
|
|
138
191
|
|
|
139
|
-
def _study_uid(
|
|
140
|
-
uid_patient = _patient_uid(
|
|
192
|
+
def _study_uid(dbtree, study):
|
|
193
|
+
uid_patient = _patient_uid(dbtree, study[:-1])
|
|
141
194
|
patient, study = study[1], study[2]
|
|
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
|
-
|
|
167
|
-
|
|
195
|
+
for pt in dbtree:
|
|
196
|
+
if pt['PatientID'] == uid_patient:
|
|
197
|
+
studies = {}
|
|
198
|
+
study_idx = {}
|
|
199
|
+
for st in pt['studies']:
|
|
200
|
+
study_desc = st['StudyDescription']
|
|
201
|
+
uid_study = st['StudyInstanceUID']
|
|
202
|
+
if study_desc in study_idx:
|
|
203
|
+
study_idx[study_desc] += 1
|
|
204
|
+
else:
|
|
205
|
+
study_idx[study_desc] = 0
|
|
206
|
+
study_desc = (study_desc, study_idx[study_desc])
|
|
207
|
+
if study == study_desc:
|
|
208
|
+
return uid_study
|
|
209
|
+
studies[study_desc] = uid_study
|
|
210
|
+
if isinstance(study, str):
|
|
211
|
+
studies_list = [s for s in studies.keys() if s[0]==study]
|
|
212
|
+
if len(studies_list) == 1:
|
|
213
|
+
return studies[(study, 0)]
|
|
214
|
+
elif len(studies_list) > 1:
|
|
215
|
+
raise ValueError(
|
|
216
|
+
f"Multiple studies with name {study}."
|
|
217
|
+
f"Please specify the index in the call to study_uid(). "
|
|
218
|
+
f"For instance ({study}, {len(studies)-1})'. "
|
|
219
|
+
)
|
|
220
|
+
raise ValueError(f"Study {study} not found in patient {patient}.")
|
|
168
221
|
|
|
169
222
|
|
|
170
|
-
def _series_uid(
|
|
171
|
-
uid_study = _study_uid(
|
|
223
|
+
def _series_uid(dbtree, series): # absolute path to series
|
|
224
|
+
uid_study = _study_uid(dbtree, series[:-1])
|
|
172
225
|
study, sery = series[2], series[3]
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
226
|
+
for pt in dbtree:
|
|
227
|
+
for st in pt['studies']:
|
|
228
|
+
if st['StudyInstanceUID'] == uid_study:
|
|
229
|
+
series = {}
|
|
230
|
+
series_idx = {}
|
|
231
|
+
for sr in st['series']:
|
|
232
|
+
series_desc = sr['SeriesDescription']
|
|
233
|
+
uid_series = sr['SeriesInstanceUID']
|
|
234
|
+
if series_desc in series_idx:
|
|
235
|
+
series_idx[series_desc] += 1
|
|
236
|
+
else:
|
|
237
|
+
series_idx[series_desc] = 0
|
|
238
|
+
series_desc = (series_desc, series_idx[series_desc])
|
|
239
|
+
if sery == series_desc:
|
|
240
|
+
return uid_series
|
|
241
|
+
series[series_desc] = uid_series
|
|
242
|
+
if isinstance(sery, str):
|
|
243
|
+
series_list = [s for s in series.keys() if s[0]==sery]
|
|
244
|
+
if len(series_list) == 1:
|
|
245
|
+
return series[(sery, 0)]
|
|
246
|
+
elif len(series_list) > 1:
|
|
247
|
+
raise ValueError(
|
|
248
|
+
f"Multiple series with name {sery}."
|
|
249
|
+
f"Please specify the index in the call to series_uid(). "
|
|
250
|
+
f"For instance ({sery}, {len(series)-1})'. "
|
|
251
|
+
)
|
|
252
|
+
raise ValueError(f"Series {sery} not found in study {study}.")
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def patients(dbtree, database, name=None, contains=None, isin=None):
|
|
203
256
|
simplified_patients = []
|
|
204
257
|
patients = []
|
|
205
258
|
patient_idx = {}
|
|
206
|
-
for
|
|
207
|
-
|
|
208
|
-
patient_name = df_patient.PatientName.values[0]
|
|
259
|
+
for pt in dbtree:
|
|
260
|
+
patient_name = pt['PatientName']
|
|
209
261
|
if patient_name in patient_idx:
|
|
210
262
|
patient_idx[patient_name] += 1
|
|
211
263
|
else:
|
|
@@ -224,7 +276,7 @@ def patients(df, database, name=None, contains=None, isin=None):
|
|
|
224
276
|
patients_result.append(s)
|
|
225
277
|
elif s[0] == name:
|
|
226
278
|
patients_result.append(s)
|
|
227
|
-
return [[
|
|
279
|
+
return [[database, p] for p in patients_result]
|
|
228
280
|
elif contains is not None:
|
|
229
281
|
patients_result = []
|
|
230
282
|
for s in simplified_patients:
|
|
@@ -247,17 +299,15 @@ def patients(df, database, name=None, contains=None, isin=None):
|
|
|
247
299
|
return [[database, p] for p in simplified_patients]
|
|
248
300
|
|
|
249
301
|
|
|
250
|
-
def studies(
|
|
302
|
+
def studies(dbtree, pat, name=None, contains=None, isin=None):
|
|
251
303
|
database, patient = pat[0], pat[1]
|
|
252
304
|
patient_as_str = isinstance(patient, str)
|
|
253
305
|
if patient_as_str:
|
|
254
306
|
patient = (patient, 0)
|
|
255
|
-
df = _prep(df)
|
|
256
307
|
simplified_studies = []
|
|
257
308
|
patient_idx = {}
|
|
258
|
-
for
|
|
259
|
-
|
|
260
|
-
patient_name = df_patient.PatientName.values[0]
|
|
309
|
+
for pt in dbtree:
|
|
310
|
+
patient_name = pt['PatientName']
|
|
261
311
|
if patient_name in patient_idx:
|
|
262
312
|
patient_idx[patient_name] += 1
|
|
263
313
|
else:
|
|
@@ -272,9 +322,8 @@ def studies(df, pat, name=None, contains=None, isin=None):
|
|
|
272
322
|
if patient == (patient_name, patient_idx[patient_name]):
|
|
273
323
|
studies = []
|
|
274
324
|
study_idx = {}
|
|
275
|
-
for
|
|
276
|
-
|
|
277
|
-
study_desc = df_study.StudyDescription.values[0]
|
|
325
|
+
for st in pt['studies']:
|
|
326
|
+
study_desc = st['StudyDescription']
|
|
278
327
|
if study_desc in study_idx:
|
|
279
328
|
study_idx[study_desc] += 1
|
|
280
329
|
else:
|
|
@@ -319,7 +368,7 @@ def studies(df, pat, name=None, contains=None, isin=None):
|
|
|
319
368
|
|
|
320
369
|
|
|
321
370
|
|
|
322
|
-
def series(
|
|
371
|
+
def series(dbtree, stdy, name=None, contains=None, isin=None):
|
|
323
372
|
database, patient, study = stdy[0], stdy[1], stdy[2]
|
|
324
373
|
patient_as_str = isinstance(patient, str)
|
|
325
374
|
if patient_as_str:
|
|
@@ -327,12 +376,10 @@ def series(df, stdy, name=None, contains=None, isin=None):
|
|
|
327
376
|
study_as_str = isinstance(study, str)
|
|
328
377
|
if study_as_str:
|
|
329
378
|
study = (study, 0)
|
|
330
|
-
df = _prep(df)
|
|
331
379
|
simplified_series = []
|
|
332
380
|
patient_idx = {}
|
|
333
|
-
for
|
|
334
|
-
|
|
335
|
-
patient_name = df_patient.PatientName.values[0]
|
|
381
|
+
for pt in dbtree:
|
|
382
|
+
patient_name = pt['PatientName']
|
|
336
383
|
if patient_name in patient_idx:
|
|
337
384
|
patient_idx[patient_name] += 1
|
|
338
385
|
else:
|
|
@@ -345,9 +392,8 @@ def series(df, stdy, name=None, contains=None, isin=None):
|
|
|
345
392
|
)
|
|
346
393
|
if patient == (patient_name, patient_idx[patient_name]):
|
|
347
394
|
study_idx = {}
|
|
348
|
-
for
|
|
349
|
-
|
|
350
|
-
study_desc = df_study.StudyDescription.values[0]
|
|
395
|
+
for st in pt['studies']:
|
|
396
|
+
study_desc = st['StudyDescription']
|
|
351
397
|
if study_desc in study_idx:
|
|
352
398
|
study_idx[study_desc] += 1
|
|
353
399
|
else:
|
|
@@ -361,9 +407,8 @@ def series(df, stdy, name=None, contains=None, isin=None):
|
|
|
361
407
|
if study == (study_desc, study_idx[study_desc]):
|
|
362
408
|
series = []
|
|
363
409
|
series_idx = {}
|
|
364
|
-
for
|
|
365
|
-
|
|
366
|
-
series_desc = df_series.SeriesDescription.values[0]
|
|
410
|
+
for sr in st['series']:
|
|
411
|
+
series_desc = sr['SeriesDescription']
|
|
367
412
|
if series_desc in series_idx:
|
|
368
413
|
series_idx[series_desc] += 1
|
|
369
414
|
else:
|
|
@@ -407,47 +452,40 @@ def series(df, stdy, name=None, contains=None, isin=None):
|
|
|
407
452
|
return [[database, patient, study, series] for series in simplified_series]
|
|
408
453
|
|
|
409
454
|
|
|
410
|
-
def print_tree(df):
|
|
411
|
-
tree = summary(df)
|
|
412
|
-
for patient, studies in tree.items():
|
|
413
|
-
print(f"Patient: ({patient[0]}, {patient[1]})")
|
|
414
|
-
for study, series in studies.items():
|
|
415
|
-
print(f" Study: ({study[0]}, {study[1]})")
|
|
416
|
-
for s in series:
|
|
417
|
-
print(f" Series: ({s[0]}, {s[1]})")
|
|
418
455
|
|
|
419
|
-
|
|
456
|
+
|
|
457
|
+
def append(dbtree, parent, child_name):
|
|
420
458
|
if len(parent) == 1:
|
|
421
|
-
return _new_patient(
|
|
459
|
+
return _new_patient(dbtree, parent, child_name)
|
|
422
460
|
elif len(parent) == 2:
|
|
423
|
-
return _new_study(
|
|
461
|
+
return _new_study(dbtree, parent, child_name)
|
|
424
462
|
elif len(parent) == 3:
|
|
425
|
-
return _new_series(
|
|
463
|
+
return _new_series(dbtree, parent, child_name)
|
|
426
464
|
|
|
427
|
-
def _new_patient(
|
|
465
|
+
def _new_patient(dbtree, database, patient_name):
|
|
428
466
|
# Count the number of series with the same description
|
|
429
467
|
desc = patient_name if isinstance(patient_name, str) else patient_name[0]
|
|
430
|
-
patients_in_db = patients(
|
|
468
|
+
patients_in_db = patients(dbtree, database, name=desc)
|
|
431
469
|
cnt = len(patients_in_db)
|
|
432
470
|
if cnt==0:
|
|
433
471
|
return [database, desc]
|
|
434
472
|
else:
|
|
435
473
|
return [database, (desc, cnt+1)]
|
|
436
474
|
|
|
437
|
-
def _new_study(
|
|
475
|
+
def _new_study(dbtree, patient, study_name): #len(patient)=2
|
|
438
476
|
# Count the number of series with the same description
|
|
439
477
|
desc = study_name if isinstance(study_name, str) else study_name[0]
|
|
440
|
-
studies_in_patient = studies(
|
|
478
|
+
studies_in_patient = studies(dbtree, patient, name=desc)
|
|
441
479
|
cnt = len(studies_in_patient)
|
|
442
480
|
if cnt==0:
|
|
443
481
|
return patient + [desc]
|
|
444
482
|
else:
|
|
445
483
|
return patient + [(desc, cnt+1)]
|
|
446
484
|
|
|
447
|
-
def _new_series(
|
|
485
|
+
def _new_series(dbtree, study, series_name): #len(study)=3
|
|
448
486
|
# Count the number of series with the same description
|
|
449
487
|
desc = series_name if isinstance(series_name, str) else series_name[0]
|
|
450
|
-
series_in_study = series(
|
|
488
|
+
series_in_study = series(dbtree, study, name=desc)
|
|
451
489
|
cnt = len(series_in_study)
|
|
452
490
|
if cnt==0:
|
|
453
491
|
return study + [desc]
|
|
@@ -455,49 +493,92 @@ def _new_series(df, study, series_name): #len(study)=3
|
|
|
455
493
|
return study + [(desc, cnt+1)]
|
|
456
494
|
|
|
457
495
|
|
|
458
|
-
def uid_tree(df, path, depth=3):
|
|
496
|
+
# def uid_tree(df, path, depth=3):
|
|
459
497
|
|
|
460
|
-
|
|
461
|
-
raise ValueError('Cannot build tree - no database open')
|
|
462
|
-
df = df[df.removed == False]
|
|
463
|
-
df.sort_values(['PatientName','StudyDate','SeriesNumber','InstanceNumber'], inplace=True)
|
|
498
|
+
# dbtree = summary(df)
|
|
464
499
|
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
500
|
+
# database = {'uid': path}
|
|
501
|
+
# database['patients'] = []
|
|
502
|
+
# for pat in dbtree:
|
|
503
|
+
# patient = {'uid': pat['PatientID']}
|
|
504
|
+
# database['patients'].append(patient)
|
|
505
|
+
# if depth >= 1:
|
|
506
|
+
# df_patient = df[df.PatientID == pat['PatientID']]
|
|
507
|
+
# patient['key'] = df_patient.index[0]
|
|
508
|
+
# patient['studies'] = []
|
|
509
|
+
# for stdy in pat['studies']:
|
|
510
|
+
# study = {'uid': stdy['StudyInstanceUID']}
|
|
511
|
+
# patient['studies'].append(study)
|
|
512
|
+
# if depth >= 2:
|
|
513
|
+
# df_study = df_patient[df_patient.StudyInstanceUID == stdy['StudyInstanceUID']]
|
|
514
|
+
# study['key'] = df_study.index[0]
|
|
515
|
+
# study['series'] = []
|
|
516
|
+
# for sery in stdy['series']:
|
|
517
|
+
# series = {'uid': sery['SeriesInstanceUID']}
|
|
518
|
+
# study['series'].append(series)
|
|
519
|
+
# if depth == 3:
|
|
520
|
+
# df_series = df_study[df_study.SeriesInstanceUID == sery['SeriesInstanceUID']]
|
|
521
|
+
# series['key'] = df_series.index[0]
|
|
522
|
+
# return database
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
def print_tree(dbtree):
|
|
526
|
+
tree = summary(dbtree)
|
|
527
|
+
for patient, studies in tree.items():
|
|
528
|
+
print(f"Patient: ({patient[0]}, {patient[1]})")
|
|
529
|
+
for study, series in studies.items():
|
|
530
|
+
print(f" Study: ({study[0]}, {study[1]})")
|
|
531
|
+
for s in series:
|
|
532
|
+
print(f" Series: ({s[0]}, {s[1]})")
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
# def summary(df):
|
|
536
|
+
# # A human-readable summary tree
|
|
537
|
+
|
|
538
|
+
# df = _prep(df)
|
|
539
|
+
# summary = {}
|
|
540
|
+
|
|
541
|
+
# patient_idx = {}
|
|
542
|
+
# for uid_patient in df.PatientID.dropna().unique():
|
|
543
|
+
# df_patient = df[df.PatientID == uid_patient]
|
|
544
|
+
# patient_name = df_patient.PatientName.values[0]
|
|
545
|
+
# if patient_name in patient_idx:
|
|
546
|
+
# patient_idx[patient_name] += 1
|
|
547
|
+
# else:
|
|
548
|
+
# patient_idx[patient_name] = 0
|
|
549
|
+
# summary[patient_name, patient_idx[patient_name]] = {}
|
|
550
|
+
|
|
551
|
+
# study_idx = {}
|
|
552
|
+
# for uid_study in df_patient.StudyInstanceUID.dropna().unique():
|
|
553
|
+
# df_study = df_patient[df_patient.StudyInstanceUID == uid_study]
|
|
554
|
+
# study_desc = df_study.StudyDescription.values[0]
|
|
555
|
+
# if study_desc in study_idx:
|
|
556
|
+
# study_idx[study_desc] += 1
|
|
557
|
+
# else:
|
|
558
|
+
# study_idx[study_desc] = 0
|
|
559
|
+
# summary[patient_name, patient_idx[patient_name]][study_desc, study_idx[study_desc]] = []
|
|
560
|
+
|
|
561
|
+
# series_idx = {}
|
|
562
|
+
# for uid_sery in df_study.SeriesInstanceUID.dropna().unique():
|
|
563
|
+
# df_series = df_study[df_study.SeriesInstanceUID == uid_sery]
|
|
564
|
+
# series_desc = df_series.SeriesDescription.values[0]
|
|
565
|
+
# if series_desc in series_idx:
|
|
566
|
+
# series_idx[series_desc] += 1
|
|
567
|
+
# else:
|
|
568
|
+
# series_idx[series_desc] = 0
|
|
569
|
+
# summary[patient_name, patient_idx[patient_name]][study_desc, study_idx[study_desc]].append((series_desc, series_idx[series_desc]))
|
|
488
570
|
|
|
571
|
+
# return summary
|
|
489
572
|
|
|
490
573
|
|
|
491
|
-
def summary(
|
|
574
|
+
def summary(dbtree):
|
|
492
575
|
# A human-readable summary tree
|
|
493
576
|
|
|
494
|
-
df = _prep(df)
|
|
495
577
|
summary = {}
|
|
496
578
|
|
|
497
579
|
patient_idx = {}
|
|
498
|
-
for
|
|
499
|
-
|
|
500
|
-
patient_name = df_patient.PatientName.values[0]
|
|
580
|
+
for patient in dbtree:
|
|
581
|
+
patient_name = patient['PatientName']
|
|
501
582
|
if patient_name in patient_idx:
|
|
502
583
|
patient_idx[patient_name] += 1
|
|
503
584
|
else:
|
|
@@ -505,9 +586,8 @@ def summary(df):
|
|
|
505
586
|
summary[patient_name, patient_idx[patient_name]] = {}
|
|
506
587
|
|
|
507
588
|
study_idx = {}
|
|
508
|
-
for
|
|
509
|
-
|
|
510
|
-
study_desc = df_study.StudyDescription.values[0]
|
|
589
|
+
for study in patient['studies']:
|
|
590
|
+
study_desc = study['StudyDescription']
|
|
511
591
|
if study_desc in study_idx:
|
|
512
592
|
study_idx[study_desc] += 1
|
|
513
593
|
else:
|
|
@@ -515,13 +595,14 @@ def summary(df):
|
|
|
515
595
|
summary[patient_name, patient_idx[patient_name]][study_desc, study_idx[study_desc]] = []
|
|
516
596
|
|
|
517
597
|
series_idx = {}
|
|
518
|
-
for
|
|
519
|
-
|
|
520
|
-
series_desc = df_series.SeriesDescription.values[0]
|
|
598
|
+
for series in study['series']:
|
|
599
|
+
series_desc = series['SeriesDescription']
|
|
521
600
|
if series_desc in series_idx:
|
|
522
601
|
series_idx[series_desc] += 1
|
|
523
602
|
else:
|
|
524
603
|
series_idx[series_desc] = 0
|
|
525
604
|
summary[patient_name, patient_idx[patient_name]][study_desc, study_idx[study_desc]].append((series_desc, series_idx[series_desc]))
|
|
526
605
|
|
|
527
|
-
return summary
|
|
606
|
+
return summary
|
|
607
|
+
|
|
608
|
+
|