Chris Oswald commited on
Commit
fbb72cc
1 Parent(s): 38e8e2b

added raw image and mask objects

Browse files
Files changed (1) hide show
  1. SPIDER.py +28 -13
SPIDER.py CHANGED
@@ -24,6 +24,7 @@ import numpy as np
24
  import pandas as pd
25
 
26
  import datasets
 
27
  import skimage
28
  import SimpleITK as sitk
29
 
@@ -110,7 +111,7 @@ class CustomBuilderConfig(datasets.BuilderConfig):
110
  class SPIDER(datasets.GeneratorBasedBuilder):
111
  """TODO: Short description of my dataset."""
112
 
113
- DEFAULT_WRITER_BATCH_SIZE = 64
114
 
115
  VERSION = datasets.Version("1.1.0")
116
 
@@ -169,9 +170,9 @@ class SPIDER(datasets.GeneratorBasedBuilder):
169
  features = datasets.Features({
170
  "patient_id": datasets.Value("string"),
171
  "scan_type": datasets.Value("string"),
172
- # "raw_image": datasets.Image(),
 
173
  "image_array": datasets.Array3D(shape=image_size, dtype='float64'),
174
- # "raw_mask": datasets.Image(),
175
  "mask_array": datasets.Array3D(shape=image_size, dtype='float64'),
176
  "metadata": {
177
  "num_vertebrae": datasets.Value(dtype="string"), #TODO: more specific types
@@ -478,20 +479,34 @@ class SPIDER(datasets.GeneratorBasedBuilder):
478
  image_path = os.path.join(paths_dict['images'], 'images', example)
479
  image = sitk.ReadImage(image_path)
480
 
 
 
 
481
  # Convert .mha image to standardized numeric array
482
- image_array = standardize_3D_image(
483
- sitk.GetArrayFromImage(image), resize_shape
 
484
  )
485
 
 
 
 
486
  # Load .mha mask file
487
  mask_path = os.path.join(paths_dict['masks'], 'masks', example)
488
  mask = sitk.ReadImage(mask_path)
489
 
 
 
 
490
  # Convert .mha mask to standardized numeric array
491
- mask_array = standardize_3D_image(
492
- sitk.GetArrayFromImage(mask), resize_shape
 
493
  )
494
-
 
 
 
495
  # Extract overview data corresponding to image
496
  image_overview = overview_dict[scan_id]
497
 
@@ -502,13 +517,13 @@ class SPIDER(datasets.GeneratorBasedBuilder):
502
  return_dict = {
503
  'patient_id':patient_id,
504
  'scan_type':scan_type,
505
- 'raw_image':None, #TODO
506
- 'raw_mask':None, #TODO
507
- 'image_array':image_array,
508
- 'mask_array':mask_array,
509
  'metadata':image_overview,
510
  'rad_gradings':patient_grades_dict,
511
- }
512
 
513
  # Yield example
514
  yield scan_id, return_dict
 
24
  import pandas as pd
25
 
26
  import datasets
27
+ import PIL
28
  import skimage
29
  import SimpleITK as sitk
30
 
 
111
  class SPIDER(datasets.GeneratorBasedBuilder):
112
  """TODO: Short description of my dataset."""
113
 
114
+ DEFAULT_WRITER_BATCH_SIZE = 16 # PyArrow default is too large for image data
115
 
116
  VERSION = datasets.Version("1.1.0")
117
 
 
170
  features = datasets.Features({
171
  "patient_id": datasets.Value("string"),
172
  "scan_type": datasets.Value("string"),
173
+ "raw_image": datasets.Image(decode=False),
174
+ "raw_mask": datasets.Image(decode=False),
175
  "image_array": datasets.Array3D(shape=image_size, dtype='float64'),
 
176
  "mask_array": datasets.Array3D(shape=image_size, dtype='float64'),
177
  "metadata": {
178
  "num_vertebrae": datasets.Value(dtype="string"), #TODO: more specific types
 
479
  image_path = os.path.join(paths_dict['images'], 'images', example)
480
  image = sitk.ReadImage(image_path)
481
 
482
+ # Convert .mha image to original size numeric array
483
+ image_array_original = sitk.GetArrayFromImage(image)
484
+
485
  # Convert .mha image to standardized numeric array
486
+ image_array_standardized = standardize_3D_image(
487
+ image_array_original,
488
+ resize_shape,
489
  )
490
 
491
+ # Create PIL image object of original image
492
+ PIL_original_image = PIL.Image.fromarray(image_array_original)
493
+
494
  # Load .mha mask file
495
  mask_path = os.path.join(paths_dict['masks'], 'masks', example)
496
  mask = sitk.ReadImage(mask_path)
497
 
498
+ # Convert .mha mask to original size numeric array
499
+ mask_array_original = sitk.GetArrayFromImage(mask)
500
+
501
  # Convert .mha mask to standardized numeric array
502
+ mask_array_standardized = standardize_3D_image(
503
+ mask_array_original,
504
+ resize_shape,
505
  )
506
+
507
+ # Create PIL image object of original mask
508
+ PIL_original_mask = PIL.Image.fromarray(mask_array_original)
509
+
510
  # Extract overview data corresponding to image
511
  image_overview = overview_dict[scan_id]
512
 
 
517
  return_dict = {
518
  'patient_id':patient_id,
519
  'scan_type':scan_type,
520
+ 'raw_image':PIL_original_image,
521
+ 'raw_mask':PIL_original_mask,
522
+ 'image_array':image_array_standardized,
523
+ 'mask_array':mask_array_standardized,
524
  'metadata':image_overview,
525
  'rad_gradings':patient_grades_dict,
526
+ }
527
 
528
  # Yield example
529
  yield scan_id, return_dict