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