Chris Oswald commited on
Commit
74c2a13
1 Parent(s): 7fc1061
Files changed (1) hide show
  1. SPIDER.py +17 -12
SPIDER.py CHANGED
@@ -66,11 +66,6 @@ DEFAULT_SCAN_TYPES = ['t1', 't2', 't2_SPACE']
66
  DEFAULT_RESIZE = (512, 512, 30)
67
  DEMO_SUBSET_N = 10
68
 
69
- # Import variable types
70
- var_types_path = os.path.join(os.getcwd(), "TextFiles", "var_types.json")
71
- with open(var_types_path, "r") as io:
72
- VAR_TYPES = json.load(io)
73
-
74
  _CITATION = """\
75
  @misc{vandergraaf2023lumbar,
76
  title={Lumbar spine segmentation in MR images: a dataset and a public benchmark},
@@ -146,11 +141,12 @@ class CustomBuilderConfig(datasets.BuilderConfig):
146
  shuffle: bool = True,
147
  ):
148
  super().__init__(name, version, data_dir, data_files, description)
149
- self.scan_types = self.validate_scan_types(scan_types)
150
  self.resize_shape = resize_shape
151
  self.shuffle = shuffle
152
-
153
- def validate_scan_types(self, scan_types):
 
154
  for item in scan_types:
155
  if item not in ['t1', 't2', 't2_SPACE']:
156
  raise ValueError(
@@ -158,6 +154,12 @@ class CustomBuilderConfig(datasets.BuilderConfig):
158
  Verify scan type argument.'
159
  )
160
  return scan_types
 
 
 
 
 
 
161
 
162
 
163
  class SPIDER(datasets.GeneratorBasedBuilder):
@@ -195,7 +197,7 @@ class SPIDER(datasets.GeneratorBasedBuilder):
195
  def _info(self):
196
  """Specify datasets.DatasetInfo object containing information and typing
197
  for the dataset."""
198
-
199
  features = datasets.Features({
200
  "patient_id": datasets.Value("string"),
201
  "scan_type": datasets.Value("string"),
@@ -203,7 +205,10 @@ class SPIDER(datasets.GeneratorBasedBuilder):
203
  "mask": datasets.Array3D(shape=self.config.resize_shape, dtype='uint8'),
204
  "image_path": datasets.Value("string"),
205
  "mask_path": datasets.Value("string"),
206
- "metadata": {k:datasets.Value(v) for k,v in VAR_TYPES['metadata'].items()},
 
 
 
207
  # {
208
  # "num_vertebrae": datasets.Value("int16"),
209
  # "num_discs": datasets.Value("int16"),
@@ -379,8 +384,8 @@ class SPIDER(datasets.GeneratorBasedBuilder):
379
  for scan_id, scan_metadata in overview_dict.items():
380
  cast_dict = {}
381
  for key, value in scan_metadata.items():
382
- if key in VAR_TYPES['metadata']:
383
- new_type = VAR_TYPES['metadata'][key]
384
  if new_type != "string":
385
  cast_dict[key] = eval(f'np.{new_type}({value})')
386
  else:
 
66
  DEFAULT_RESIZE = (512, 512, 30)
67
  DEMO_SUBSET_N = 10
68
 
 
 
 
 
 
69
  _CITATION = """\
70
  @misc{vandergraaf2023lumbar,
71
  title={Lumbar spine segmentation in MR images: a dataset and a public benchmark},
 
141
  shuffle: bool = True,
142
  ):
143
  super().__init__(name, version, data_dir, data_files, description)
144
+ self.scan_types = self._validate_scan_types(scan_types)
145
  self.resize_shape = resize_shape
146
  self.shuffle = shuffle
147
+ self.var_types = self._import_var_types()
148
+
149
+ def _validate_scan_types(self, scan_types):
150
  for item in scan_types:
151
  if item not in ['t1', 't2', 't2_SPACE']:
152
  raise ValueError(
 
154
  Verify scan type argument.'
155
  )
156
  return scan_types
157
+
158
+ def _import_var_types(self):
159
+ var_types_path = os.path.join("TextFiles", "var_types.json")
160
+ with open(var_types_path, "r") as io:
161
+ var_types = json.load(io)
162
+ return var_types
163
 
164
 
165
  class SPIDER(datasets.GeneratorBasedBuilder):
 
197
  def _info(self):
198
  """Specify datasets.DatasetInfo object containing information and typing
199
  for the dataset."""
200
+
201
  features = datasets.Features({
202
  "patient_id": datasets.Value("string"),
203
  "scan_type": datasets.Value("string"),
 
205
  "mask": datasets.Array3D(shape=self.config.resize_shape, dtype='uint8'),
206
  "image_path": datasets.Value("string"),
207
  "mask_path": datasets.Value("string"),
208
+ "metadata": {
209
+ k:datasets.Value(v) for k,v in
210
+ self.config.var_types['metadata'].items()
211
+ },
212
  # {
213
  # "num_vertebrae": datasets.Value("int16"),
214
  # "num_discs": datasets.Value("int16"),
 
384
  for scan_id, scan_metadata in overview_dict.items():
385
  cast_dict = {}
386
  for key, value in scan_metadata.items():
387
+ if key in self.config.var_types['metadata']:
388
+ new_type = self.config.var_types['metadata'][key]
389
  if new_type != "string":
390
  cast_dict[key] = eval(f'np.{new_type}({value})')
391
  else: