Chris Oswald commited on
Commit
508d191
1 Parent(s): 01d5f4f

added image standardization

Browse files
Files changed (1) hide show
  1. SPIDER.py +29 -18
SPIDER.py CHANGED
@@ -24,6 +24,7 @@ import numpy as np
24
  import pandas as pd
25
 
26
  import datasets
 
27
  import SimpleITK as sitk
28
 
29
  # Define functions
@@ -36,12 +37,20 @@ def import_csv_data(filepath: str) -> List[Dict[str, str]]:
36
  results.append(line)
37
  return results
38
 
39
- def standardize_3D_image(image: np.ndarray) -> np.ndarray:
40
- """TODO"""
 
 
 
 
 
41
  if image.shape[0] < image.shape[2]:
42
  image = np.transpose(image, axes=[1, 2, 0])
 
 
43
  return image
44
 
 
45
  # Define constants
46
  N_PATIENTS = 218
47
  MIN_IVD = 0
@@ -91,11 +100,11 @@ class CustomBuilderConfig(datasets.BuilderConfig):
91
  data_files: Optional[Union[str, Sequence, Mapping]] = None,
92
  description: Optional[str] = None,
93
  scan_types: List[str] = DEFAULT_SCAN_TYPES,
94
- resize_dims: Tuple[int, int, int] = DEFAULT_RESIZE,
95
  ):
96
  super().__init__(name, version, data_dir, data_files, description)
97
  self.scan_types = scan_types
98
- self.resize_dims = resize_dims
99
 
100
 
101
  class SPIDER(datasets.GeneratorBasedBuilder):
@@ -111,28 +120,28 @@ class SPIDER(datasets.GeneratorBasedBuilder):
111
  # version=VERSION,
112
  # description="Use images of all scan types (t1, t2, t2 SPACE)",
113
  # scan_types=['t1', 't2', 't2_SPACE'],
114
- # resize_dims=DEFAULT_RESIZE,
115
  # ),
116
  # CustomBuilderConfig(
117
  # name="t1_scan_types",
118
  # version=VERSION,
119
  # description="Use images of t1 scan types only",
120
  # scan_types=['t1'],
121
- # resize_dims=DEFAULT_RESIZE,
122
  # ),
123
  # CustomBuilderConfig(
124
  # name="t2_scan_types",
125
  # version=VERSION,
126
  # description="Use images of t2 scan types only",
127
  # scan_types=['t2'],
128
- # resize_dims=DEFAULT_RESIZE,
129
  # ),
130
  # CustomBuilderConfig(
131
  # name="t2_SPACE_scan_types",
132
  # version=VERSION,
133
  # description="Use images of t2 SPACE scan types only",
134
  # scan_types=['t2_SPACE'],
135
- # resize_dims=DEFAULT_RESIZE,
136
  # ),
137
  # ]
138
 
@@ -142,12 +151,12 @@ class SPIDER(datasets.GeneratorBasedBuilder):
142
  self,
143
  *args,
144
  scan_types: List[str] = DEFAULT_SCAN_TYPES,
145
- resize_dims: Tuple[int, int, int] = DEFAULT_RESIZE,
146
  **kwargs,
147
  ):
148
  super().__init__(*args, **kwargs)
149
  self.scan_types = scan_types
150
- self.resize_dims = resize_dims
151
 
152
  def _info(self):
153
  """
@@ -158,7 +167,7 @@ class SPIDER(datasets.GeneratorBasedBuilder):
158
  "patient_id": datasets.Value("string"),
159
  "scan_type": datasets.Value("string"),
160
  # "raw_image": datasets.Image(),
161
- "numeric_array": datasets.Array3D(shape=self.resize_dims, dtype='int16'),
162
  "metadata": {
163
  "num_vertebrae": datasets.Value(dtype="string"),
164
  "num_discs": datasets.Value(dtype="string"),
@@ -246,7 +255,7 @@ class SPIDER(datasets.GeneratorBasedBuilder):
246
  "paths_dict": paths_dict,
247
  "split": "train",
248
  "scan_types": self.scan_types,
249
- "resize_dims": self.resize_dims,
250
  },
251
  ),
252
  datasets.SplitGenerator(
@@ -255,7 +264,7 @@ class SPIDER(datasets.GeneratorBasedBuilder):
255
  "paths_dict": paths_dict,
256
  "split": "validate",
257
  "scan_types": self.scan_types,
258
- "resize_dims": self.resize_dims,
259
  },
260
  ),
261
  datasets.SplitGenerator(
@@ -264,7 +273,7 @@ class SPIDER(datasets.GeneratorBasedBuilder):
264
  "paths_dict": paths_dict,
265
  "split": "test",
266
  "scan_types": self.scan_types,
267
- "resize_dims": self.resize_dims,
268
  },
269
  ),
270
  ]
@@ -274,7 +283,7 @@ class SPIDER(datasets.GeneratorBasedBuilder):
274
  paths_dict: Dict[str, str],
275
  split: str,
276
  scan_types: List[str],
277
- resize_dims: Tuple[int, int, int],
278
  validate_share: float = 0.3,
279
  test_share: float = 0.2,
280
  raw_image: bool = True,
@@ -475,9 +484,11 @@ class SPIDER(datasets.GeneratorBasedBuilder):
475
  image_path = os.path.join(paths_dict['images'], 'images', example)
476
  image = sitk.ReadImage(image_path)
477
 
478
- # Convert .mha image to numeric array
479
- image_array = sitk.GetArrayFromImage(image)
480
-
 
 
481
  #TODO: load mask file and numeric array
482
 
483
  # Extract overview data corresponding to image
 
24
  import pandas as pd
25
 
26
  import datasets
27
+ import skimage
28
  import SimpleITK as sitk
29
 
30
  # Define functions
 
37
  results.append(line)
38
  return results
39
 
40
+ def standardize_3D_image(
41
+ image: np.ndarray,
42
+ resize_shape: Tuple[int, int, int]
43
+ ) -> np.ndarray:
44
+ """Aligns dimensions of image to be (height, width, channels) and resizes
45
+ images to values specified in resize_shape."""
46
+ # Align height, width, channel dims
47
  if image.shape[0] < image.shape[2]:
48
  image = np.transpose(image, axes=[1, 2, 0])
49
+ # Resize image
50
+ image = skimage.transform.resize(image, resize_shape)
51
  return image
52
 
53
+
54
  # Define constants
55
  N_PATIENTS = 218
56
  MIN_IVD = 0
 
100
  data_files: Optional[Union[str, Sequence, Mapping]] = None,
101
  description: Optional[str] = None,
102
  scan_types: List[str] = DEFAULT_SCAN_TYPES,
103
+ resize_shape: Tuple[int, int, int] = DEFAULT_RESIZE,
104
  ):
105
  super().__init__(name, version, data_dir, data_files, description)
106
  self.scan_types = scan_types
107
+ self.resize_shape = resize_shape
108
 
109
 
110
  class SPIDER(datasets.GeneratorBasedBuilder):
 
120
  # version=VERSION,
121
  # description="Use images of all scan types (t1, t2, t2 SPACE)",
122
  # scan_types=['t1', 't2', 't2_SPACE'],
123
+ # resize_shape=DEFAULT_RESIZE,
124
  # ),
125
  # CustomBuilderConfig(
126
  # name="t1_scan_types",
127
  # version=VERSION,
128
  # description="Use images of t1 scan types only",
129
  # scan_types=['t1'],
130
+ # resize_shape=DEFAULT_RESIZE,
131
  # ),
132
  # CustomBuilderConfig(
133
  # name="t2_scan_types",
134
  # version=VERSION,
135
  # description="Use images of t2 scan types only",
136
  # scan_types=['t2'],
137
+ # resize_shape=DEFAULT_RESIZE,
138
  # ),
139
  # CustomBuilderConfig(
140
  # name="t2_SPACE_scan_types",
141
  # version=VERSION,
142
  # description="Use images of t2 SPACE scan types only",
143
  # scan_types=['t2_SPACE'],
144
+ # resize_shape=DEFAULT_RESIZE,
145
  # ),
146
  # ]
147
 
 
151
  self,
152
  *args,
153
  scan_types: List[str] = DEFAULT_SCAN_TYPES,
154
+ resize_shape: Tuple[int, int, int] = DEFAULT_RESIZE,
155
  **kwargs,
156
  ):
157
  super().__init__(*args, **kwargs)
158
  self.scan_types = scan_types
159
+ self.resize_shape = resize_shape
160
 
161
  def _info(self):
162
  """
 
167
  "patient_id": datasets.Value("string"),
168
  "scan_type": datasets.Value("string"),
169
  # "raw_image": datasets.Image(),
170
+ "numeric_array": datasets.Array3D(shape=self.resize_shape, dtype='int16'),
171
  "metadata": {
172
  "num_vertebrae": datasets.Value(dtype="string"),
173
  "num_discs": datasets.Value(dtype="string"),
 
255
  "paths_dict": paths_dict,
256
  "split": "train",
257
  "scan_types": self.scan_types,
258
+ "resize_shape": self.resize_shape,
259
  },
260
  ),
261
  datasets.SplitGenerator(
 
264
  "paths_dict": paths_dict,
265
  "split": "validate",
266
  "scan_types": self.scan_types,
267
+ "resize_shape": self.resize_shape,
268
  },
269
  ),
270
  datasets.SplitGenerator(
 
273
  "paths_dict": paths_dict,
274
  "split": "test",
275
  "scan_types": self.scan_types,
276
+ "resize_shape": self.resize_shape,
277
  },
278
  ),
279
  ]
 
283
  paths_dict: Dict[str, str],
284
  split: str,
285
  scan_types: List[str],
286
+ resize_shape: Tuple[int, int, int],
287
  validate_share: float = 0.3,
288
  test_share: float = 0.2,
289
  raw_image: bool = True,
 
484
  image_path = os.path.join(paths_dict['images'], 'images', example)
485
  image = sitk.ReadImage(image_path)
486
 
487
+ # Convert .mha image to standardized numeric array
488
+ image_array = standardize_3D_image(
489
+ sitk.GetArrayFromImage(image), resize_shape
490
+ )
491
+
492
  #TODO: load mask file and numeric array
493
 
494
  # Extract overview data corresponding to image