alexanderdann commited on
Commit
991265c
·
verified ·
1 Parent(s): ff723e1

Adding debug to loading script

Browse files
Files changed (1) hide show
  1. CTSpine1K.py +30 -45
CTSpine1K.py CHANGED
@@ -157,20 +157,20 @@ class CTSpine1K(datasets.GeneratorBasedBuilder):
157
 
158
  training, validation, test = self._load_split(downloaded_files[split_file_idx])
159
 
160
- self._validate_check(downloaded_files)
161
 
162
  return [
163
  datasets.SplitGenerator(
164
  name=datasets.Split.TRAIN,
165
- gen_kwargs={"stems": training},
166
  ),
167
  datasets.SplitGenerator(
168
  name=datasets.Split.VALIDATION,
169
- gen_kwargs={"stems": validation},
170
  ),
171
  datasets.SplitGenerator(
172
  name=datasets.Split.TEST,
173
- gen_kwargs={"stems": test},
174
  ),
175
  ]
176
 
@@ -201,21 +201,15 @@ class CTSpine1K(datasets.GeneratorBasedBuilder):
201
  return training, validation, test
202
 
203
  def _validate_check(self, files: list[str]) -> dict:
204
- stems = [file.split(".nii.gz")[0] for file in files]
205
 
206
- print("stems ", stems)
207
- print("files ", files)
208
-
209
- data_candidates = [
210
- file
211
- for file in sorted(cache_dir.rglob("rawdata/volumes/*/*"))
212
- if "nii.gz" in file.as_uri().lower()
213
- ]
214
- label_candidates = [
215
- file
216
- for file in sorted(cache_dir.rglob("rawdata/labels/*/*"))
217
- if "nii.gz" in file.as_uri().lower()
218
- ]
219
 
220
  if len(data_candidates) != len(label_candidates):
221
  msg = (
@@ -224,29 +218,14 @@ class CTSpine1K(datasets.GeneratorBasedBuilder):
224
  )
225
  raise RuntimeError(msg)
226
 
227
- pairs = zip(data_candidates, label_candidates, strict=True)
228
- pairs_lookup = {pair[0].name.split(".")[0]: pair for pair in pairs}
229
-
230
- print("pairs", pairs_lookup)
231
- print("stems", stems)
232
- samples = {}
233
-
234
- for stem in stems:
235
- file_path, segmentation_path = pairs_lookup[stem]
236
-
237
- if stem not in segmentation_path.name:
238
- msg = (
239
- "Naming convention seems invalid or violated. ",
240
- "Ensure all data was downloaded successfully.",
241
- )
242
- raise RuntimeError(msg)
243
-
244
- if not ((file_path).is_file() and (segmentation_path).is_file()):
245
- msg = (
246
- "Data is not a file. Ensure all data was downloaded successfully."
247
- f"Failed for {file_path} and {segmentation_path}."
248
- )
249
- raise RuntimeError(msg)
250
 
251
  image_slices = self._get_sample_length(file_path)
252
  segmentation_slices = self._get_sample_length(segmentation_path)
@@ -258,9 +237,15 @@ class CTSpine1K(datasets.GeneratorBasedBuilder):
258
  )
259
  raise ValueError(msg)
260
 
261
- samples[stem] = (file_path, segmentation_path, image_slices)
 
 
 
 
 
 
262
 
263
- return samples
264
 
265
  @lru_cache(maxsize=1) # since it does not change # noqa: B019
266
  def _sorted_lookup(self) -> list[Path]:
@@ -296,8 +281,8 @@ class CTSpine1K(datasets.GeneratorBasedBuilder):
296
 
297
  return volume.get_fdata()
298
 
299
- def _generate_examples(self, stems: list[str]) -> Generator:
300
- print("stems: ", stems)
301
  for idx in range(len(self)):
302
  image, segmentation, patient_id = self[idx]
303
  yield {
 
157
 
158
  training, validation, test = self._load_split(downloaded_files[split_file_idx])
159
 
160
+ lookup = self._validate_check(downloaded_files)
161
 
162
  return [
163
  datasets.SplitGenerator(
164
  name=datasets.Split.TRAIN,
165
+ gen_kwargs={"pairs": [lookup[stem] for stem in training]},
166
  ),
167
  datasets.SplitGenerator(
168
  name=datasets.Split.VALIDATION,
169
+ gen_kwargs={"pairs": [lookup[stem] for stem in validation]},
170
  ),
171
  datasets.SplitGenerator(
172
  name=datasets.Split.TEST,
173
+ gen_kwargs={"pairs": [lookup[stem] for stem in test]},
174
  ),
175
  ]
176
 
 
201
  return training, validation, test
202
 
203
  def _validate_check(self, files: list[str]) -> dict:
204
+ ffiles = sorted(file for file in files if "nii.gz" in file)
205
 
206
+ data_candidates = []
207
+ label_candidates = []
208
+ for file in ffiles:
209
+ if "labels" in file:
210
+ label_candidates.append(file)
211
+ else:
212
+ data_candidates.append(file)
 
 
 
 
 
 
213
 
214
  if len(data_candidates) != len(label_candidates):
215
  msg = (
 
218
  )
219
  raise RuntimeError(msg)
220
 
221
+ pairs: list[tuple[str, str]] = zip(
222
+ data_candidates,
223
+ label_candidates,
224
+ strict=True,
225
+ )
226
+ lookup = {}
227
+ for input_, label in pairs:
228
+ file_path, segmentation_path = Path(input_), Path(label)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
 
230
  image_slices = self._get_sample_length(file_path)
231
  segmentation_slices = self._get_sample_length(segmentation_path)
 
237
  )
238
  raise ValueError(msg)
239
 
240
+ # convert 'some/path/to/file/dummy_123.nii.gz' to dummy_123.nii.gz
241
+ stem = file_path.name
242
+ if stem.removesuffix("nii.gz") not in segmentation_path:
243
+ msg = f"Mismatch in sorted pairs. {input_} vs. {label}"
244
+ raise ValueError(msg)
245
+
246
+ lookup[stem] = (file_path, segmentation_path)
247
 
248
+ return lookup
249
 
250
  @lru_cache(maxsize=1) # since it does not change # noqa: B019
251
  def _sorted_lookup(self) -> list[Path]:
 
281
 
282
  return volume.get_fdata()
283
 
284
+ def _generate_examples(self, pairs: list[tuple[Path, Path]]) -> Generator:
285
+ print("pairs: ", pairs)
286
  for idx in range(len(self)):
287
  image, segmentation, patient_id = self[idx]
288
  yield {