Chris Oswald
commited on
Commit
·
9997395
1
Parent(s):
18d6eb1
flattened radiological gradings
Browse files
SPIDER.py
CHANGED
|
@@ -37,6 +37,7 @@ def import_csv_data(filepath: str) -> List[Dict[str, str]]:
|
|
| 37 |
|
| 38 |
# Define constants
|
| 39 |
N_PATIENTS = 257
|
|
|
|
| 40 |
MAX_IVD = 9
|
| 41 |
|
| 42 |
# TODO: Add BibTeX citation
|
|
@@ -181,96 +182,15 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
| 181 |
"WindowWidth": datasets.Value(dtype="string"),
|
| 182 |
},
|
| 183 |
"rad_gradings": {
|
| 184 |
-
"
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
},
|
| 194 |
-
"IVD2": {
|
| 195 |
-
"Modic": datasets.Value(dtype="string"),
|
| 196 |
-
"UP endplate": datasets.Value(dtype="string"),
|
| 197 |
-
"LOW endplate": datasets.Value(dtype="string"),
|
| 198 |
-
"Spondylolisthesis": datasets.Value(dtype="string"),
|
| 199 |
-
"Disc herniation": datasets.Value(dtype="string"),
|
| 200 |
-
"Disc narrowing": datasets.Value(dtype="string"),
|
| 201 |
-
"Disc bulging": datasets.Value(dtype="string"),
|
| 202 |
-
"Pfirrman grade": datasets.Value(dtype="string"),
|
| 203 |
-
},
|
| 204 |
-
"IVD3": {
|
| 205 |
-
"Modic": datasets.Value(dtype="string"),
|
| 206 |
-
"UP endplate": datasets.Value(dtype="string"),
|
| 207 |
-
"LOW endplate": datasets.Value(dtype="string"),
|
| 208 |
-
"Spondylolisthesis": datasets.Value(dtype="string"),
|
| 209 |
-
"Disc herniation": datasets.Value(dtype="string"),
|
| 210 |
-
"Disc narrowing": datasets.Value(dtype="string"),
|
| 211 |
-
"Disc bulging": datasets.Value(dtype="string"),
|
| 212 |
-
"Pfirrman grade": datasets.Value(dtype="string"),
|
| 213 |
-
},
|
| 214 |
-
"IVD4": {
|
| 215 |
-
"Modic": datasets.Value(dtype="string"),
|
| 216 |
-
"UP endplate": datasets.Value(dtype="string"),
|
| 217 |
-
"LOW endplate": datasets.Value(dtype="string"),
|
| 218 |
-
"Spondylolisthesis": datasets.Value(dtype="string"),
|
| 219 |
-
"Disc herniation": datasets.Value(dtype="string"),
|
| 220 |
-
"Disc narrowing": datasets.Value(dtype="string"),
|
| 221 |
-
"Disc bulging": datasets.Value(dtype="string"),
|
| 222 |
-
"Pfirrman grade": datasets.Value(dtype="string"),
|
| 223 |
-
},
|
| 224 |
-
"IVD5": {
|
| 225 |
-
"Modic": datasets.Value(dtype="string"),
|
| 226 |
-
"UP endplate": datasets.Value(dtype="string"),
|
| 227 |
-
"LOW endplate": datasets.Value(dtype="string"),
|
| 228 |
-
"Spondylolisthesis": datasets.Value(dtype="string"),
|
| 229 |
-
"Disc herniation": datasets.Value(dtype="string"),
|
| 230 |
-
"Disc narrowing": datasets.Value(dtype="string"),
|
| 231 |
-
"Disc bulging": datasets.Value(dtype="string"),
|
| 232 |
-
"Pfirrman grade": datasets.Value(dtype="string"),
|
| 233 |
-
},
|
| 234 |
-
"IVD6": {
|
| 235 |
-
"Modic": datasets.Value(dtype="string"),
|
| 236 |
-
"UP endplate": datasets.Value(dtype="string"),
|
| 237 |
-
"LOW endplate": datasets.Value(dtype="string"),
|
| 238 |
-
"Spondylolisthesis": datasets.Value(dtype="string"),
|
| 239 |
-
"Disc herniation": datasets.Value(dtype="string"),
|
| 240 |
-
"Disc narrowing": datasets.Value(dtype="string"),
|
| 241 |
-
"Disc bulging": datasets.Value(dtype="string"),
|
| 242 |
-
"Pfirrman grade": datasets.Value(dtype="string"),
|
| 243 |
-
},
|
| 244 |
-
"IVD7": {
|
| 245 |
-
"Modic": datasets.Value(dtype="string"),
|
| 246 |
-
"UP endplate": datasets.Value(dtype="string"),
|
| 247 |
-
"LOW endplate": datasets.Value(dtype="string"),
|
| 248 |
-
"Spondylolisthesis": datasets.Value(dtype="string"),
|
| 249 |
-
"Disc herniation": datasets.Value(dtype="string"),
|
| 250 |
-
"Disc narrowing": datasets.Value(dtype="string"),
|
| 251 |
-
"Disc bulging": datasets.Value(dtype="string"),
|
| 252 |
-
"Pfirrman grade": datasets.Value(dtype="string"),
|
| 253 |
-
},
|
| 254 |
-
"IVD8": {
|
| 255 |
-
"Modic": datasets.Value(dtype="string"),
|
| 256 |
-
"UP endplate": datasets.Value(dtype="string"),
|
| 257 |
-
"LOW endplate": datasets.Value(dtype="string"),
|
| 258 |
-
"Spondylolisthesis": datasets.Value(dtype="string"),
|
| 259 |
-
"Disc herniation": datasets.Value(dtype="string"),
|
| 260 |
-
"Disc narrowing": datasets.Value(dtype="string"),
|
| 261 |
-
"Disc bulging": datasets.Value(dtype="string"),
|
| 262 |
-
"Pfirrman grade": datasets.Value(dtype="string"),
|
| 263 |
-
},
|
| 264 |
-
"IVD9": {
|
| 265 |
-
"Modic": datasets.Value(dtype="string"),
|
| 266 |
-
"UP endplate": datasets.Value(dtype="string"),
|
| 267 |
-
"LOW endplate": datasets.Value(dtype="string"),
|
| 268 |
-
"Spondylolisthesis": datasets.Value(dtype="string"),
|
| 269 |
-
"Disc herniation": datasets.Value(dtype="string"),
|
| 270 |
-
"Disc narrowing": datasets.Value(dtype="string"),
|
| 271 |
-
"Disc bulging": datasets.Value(dtype="string"),
|
| 272 |
-
"Pfirrman grade": datasets.Value(dtype="string"),
|
| 273 |
-
},
|
| 274 |
}
|
| 275 |
})
|
| 276 |
|
|
@@ -444,10 +364,10 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
| 444 |
patient_grades = [
|
| 445 |
x for x in grades_data if x['Patient'] == str(patient_id)
|
| 446 |
]
|
| 447 |
-
# Pad
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
patient_grades.append({
|
| 452 |
"Patient": f"{patient_id}",
|
| 453 |
"IVD label": f"{i}",
|
|
@@ -460,9 +380,19 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
| 460 |
"Disc bulging": "",
|
| 461 |
"Pfirrman grade": "",
|
| 462 |
})
|
| 463 |
-
assert len(patient_grades) == MAX_IVD
|
| 464 |
-
|
| 465 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
|
| 467 |
# Import image and mask data
|
| 468 |
image_files = [
|
|
@@ -534,14 +464,7 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
| 534 |
image_overview = overview_dict[scan_id]
|
| 535 |
|
| 536 |
# Extract patient radiological gradings corresponding to patient
|
| 537 |
-
patient_grades_dict =
|
| 538 |
-
for item in grades_dict[patient_id]:
|
| 539 |
-
key = f'IVD{item["IVD label"]}'
|
| 540 |
-
value = {
|
| 541 |
-
k:v for k,v in item.items()
|
| 542 |
-
if k not in ['Patient', 'IVD label']
|
| 543 |
-
}
|
| 544 |
-
patient_grades_dict[key] = value
|
| 545 |
|
| 546 |
# Prepare example return dict
|
| 547 |
return_dict = {'patient_id':patient_id, 'scan_type':scan_type}
|
|
@@ -555,4 +478,4 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
| 555 |
return_dict['rad_gradings'] = patient_grades_dict
|
| 556 |
|
| 557 |
# Yield example
|
| 558 |
-
yield
|
|
|
|
| 37 |
|
| 38 |
# Define constants
|
| 39 |
N_PATIENTS = 257
|
| 40 |
+
MIN_IVD = 0
|
| 41 |
MAX_IVD = 9
|
| 42 |
|
| 43 |
# TODO: Add BibTeX citation
|
|
|
|
| 182 |
"WindowWidth": datasets.Value(dtype="string"),
|
| 183 |
},
|
| 184 |
"rad_gradings": {
|
| 185 |
+
"IVD label": datasets.Sequence(datasets.Value("string")),
|
| 186 |
+
"Modic": datasets.Sequence(datasets.Value("string")),
|
| 187 |
+
"UP endplate": datasets.Sequence(datasets.Value("string")),
|
| 188 |
+
"LOW endplate": datasets.Sequence(datasets.Value("string")),
|
| 189 |
+
"Spondylolisthesis": datasets.Sequence(datasets.Value("string")),
|
| 190 |
+
"Disc herniation": datasets.Sequence(datasets.Value("string")),
|
| 191 |
+
"Disc narrowing": datasets.Sequence(datasets.Value("string")),
|
| 192 |
+
"Disc bulging": datasets.Sequence(datasets.Value("string")),
|
| 193 |
+
"Pfirrman grade": datasets.Sequence(datasets.Value("string")),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
}
|
| 195 |
})
|
| 196 |
|
|
|
|
| 364 |
patient_grades = [
|
| 365 |
x for x in grades_data if x['Patient'] == str(patient_id)
|
| 366 |
]
|
| 367 |
+
# Pad so that all patients have same number of IVD observations
|
| 368 |
+
IVD_values = [x['IVD label'] for x in patient_grades]
|
| 369 |
+
for i in range(MIN_IVD, MAX_IVD + 1):
|
| 370 |
+
if str(i) not in IVD_values:
|
| 371 |
patient_grades.append({
|
| 372 |
"Patient": f"{patient_id}",
|
| 373 |
"IVD label": f"{i}",
|
|
|
|
| 380 |
"Disc bulging": "",
|
| 381 |
"Pfirrman grade": "",
|
| 382 |
})
|
| 383 |
+
assert len(patient_grades) == (MAX_IVD - MIN_IVD + 1), "Radiological\
|
| 384 |
+
gradings not padded correctly"
|
| 385 |
+
|
| 386 |
+
# Convert to sequences
|
| 387 |
+
df = (
|
| 388 |
+
pd.DataFrame(patient_grades)
|
| 389 |
+
.sort_values("IVD label")
|
| 390 |
+
.reset_index(drop=True)
|
| 391 |
+
)
|
| 392 |
+
grades_dict[str(patient_id)] = {
|
| 393 |
+
col:df[col].tolist() for col in df.columns
|
| 394 |
+
if col not in ['Patient']
|
| 395 |
+
}
|
| 396 |
|
| 397 |
# Import image and mask data
|
| 398 |
image_files = [
|
|
|
|
| 464 |
image_overview = overview_dict[scan_id]
|
| 465 |
|
| 466 |
# Extract patient radiological gradings corresponding to patient
|
| 467 |
+
patient_grades_dict = grades_dict[patient_id]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
|
| 469 |
# Prepare example return dict
|
| 470 |
return_dict = {'patient_id':patient_id, 'scan_type':scan_type}
|
|
|
|
| 478 |
return_dict['rad_gradings'] = patient_grades_dict
|
| 479 |
|
| 480 |
# Yield example
|
| 481 |
+
yield scan_id, return_dict
|