alexanderdann commited on
Commit
16211ac
·
verified ·
1 Parent(s): a4dfc3d

Fixing changed loading script

Browse files
Files changed (1) hide show
  1. CTSpine1K.py +23 -33
CTSpine1K.py CHANGED
@@ -1,7 +1,6 @@
1
  """Wrapper to load the actual data using Python."""
2
 
3
  from collections.abc import Generator
4
- from functools import lru_cache
5
  from pathlib import Path
6
  from typing import ClassVar
7
 
@@ -98,23 +97,6 @@ class CTSpine1K(datasets.GeneratorBasedBuilder):
98
  ),
99
  ]
100
 
101
- @property
102
- def volumetric(self) -> bool:
103
- """Mode indicating whether we use 3D or 2D data."""
104
- return self.config.volumetric
105
-
106
- def __len__(self) -> int:
107
- """Length attribute of the class.
108
-
109
- Returns:
110
- Return the amount of samples based on mode.
111
-
112
- """
113
- if self.config.volumetric:
114
- return len(self._lookup)
115
-
116
- return sum(elem[2] for elem in self._lookup.values())
117
-
118
  def _info(self) -> datasets.DatasetInfo:
119
  if self.config.volumetric:
120
  features = datasets.Features(
@@ -128,6 +110,7 @@ class CTSpine1K(datasets.GeneratorBasedBuilder):
128
  dtype="int32",
129
  ),
130
  "patient_id": datasets.Value("string"),
 
131
  },
132
  )
133
  else:
@@ -136,6 +119,8 @@ class CTSpine1K(datasets.GeneratorBasedBuilder):
136
  "image": datasets.Array2D(shape=(512, 512), dtype="float32"),
137
  "segmentation": datasets.Array2D(shape=(512, 512), dtype="int32"),
138
  "patient_id": datasets.Value("string"),
 
 
139
  },
140
  )
141
 
@@ -263,10 +248,6 @@ class CTSpine1K(datasets.GeneratorBasedBuilder):
263
 
264
  return lookup
265
 
266
- @lru_cache(maxsize=1) # since it does not change # noqa: B019
267
- def _sorted_lookup(self) -> list[Path]:
268
- return sorted(self._lookup.keys())
269
-
270
  @staticmethod
271
  def _get_sample_length(file_path: Path) -> int:
272
  expected_ndim = 3
@@ -283,21 +264,30 @@ class CTSpine1K(datasets.GeneratorBasedBuilder):
283
  return np.transpose(volume, (2, 0, 1))
284
 
285
  def _generate_examples(self, pairs: list[tuple[Path, Path]]) -> Generator:
286
- for volume_path, label_path in pairs:
287
  patient_id = Path(volume_path.stem).stem
288
  image = self._volumetric_sample(volume_path)
289
  segmentation = self._volumetric_sample(label_path).astype(np.uint32)
290
 
291
  if self.config.volumetric:
292
- yield {
293
- "image": image,
294
- "segmentation": segmentation,
295
- "patient_id": patient_id,
296
- }
 
 
 
 
297
  else:
298
  for idx in range(image.shape[2]): # iterate over axial slices
299
- yield {
300
- "image": image[idx],
301
- "segmentation": segmentation[idx],
302
- "patient_id": patient_id + f"_{idx}",
303
- }
 
 
 
 
 
 
1
  """Wrapper to load the actual data using Python."""
2
 
3
  from collections.abc import Generator
 
4
  from pathlib import Path
5
  from typing import ClassVar
6
 
 
97
  ),
98
  ]
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  def _info(self) -> datasets.DatasetInfo:
101
  if self.config.volumetric:
102
  features = datasets.Features(
 
110
  dtype="int32",
111
  ),
112
  "patient_id": datasets.Value("string"),
113
+ "index": datasets.Value("int32"),
114
  },
115
  )
116
  else:
 
119
  "image": datasets.Array2D(shape=(512, 512), dtype="float32"),
120
  "segmentation": datasets.Array2D(shape=(512, 512), dtype="int32"),
121
  "patient_id": datasets.Value("string"),
122
+ "index": datasets.Value("int32"),
123
+ "slice_index": datasets.Value("int32"),
124
  },
125
  )
126
 
 
248
 
249
  return lookup
250
 
 
 
 
 
251
  @staticmethod
252
  def _get_sample_length(file_path: Path) -> int:
253
  expected_ndim = 3
 
264
  return np.transpose(volume, (2, 0, 1))
265
 
266
  def _generate_examples(self, pairs: list[tuple[Path, Path]]) -> Generator:
267
+ for pair_idx, (volume_path, label_path) in enumerate(pairs):
268
  patient_id = Path(volume_path.stem).stem
269
  image = self._volumetric_sample(volume_path)
270
  segmentation = self._volumetric_sample(label_path).astype(np.uint32)
271
 
272
  if self.config.volumetric:
273
+ yield (
274
+ patient_id,
275
+ {
276
+ "image": image,
277
+ "segmentation": segmentation,
278
+ "patient_id": patient_id,
279
+ "index": pair_idx,
280
+ },
281
+ )
282
  else:
283
  for idx in range(image.shape[2]): # iterate over axial slices
284
+ yield (
285
+ patient_id + f"_{idx}",
286
+ {
287
+ "image": image[idx],
288
+ "segmentation": segmentation[idx],
289
+ "patient_id": patient_id,
290
+ "index": pair_idx,
291
+ "slice_index": idx,
292
+ },
293
+ )