SiddiqueAkhonda commited on
Commit
ececc1a
1 Parent(s): 720d608

Update msynth.py

Browse files
Files changed (1) hide show
  1. msynth.py +74 -23
msynth.py CHANGED
@@ -311,7 +311,33 @@ class msynth(datasets.GeneratorBasedBuilder):
311
  else:
312
  return "Not available for this raw file"
313
 
314
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
315
 
316
  def _generate_examples(self, files, name):
317
  if self.config.name == "device_data":
@@ -350,37 +376,62 @@ class msynth(datasets.GeneratorBasedBuilder):
350
 
351
 
352
  if self.config.name == "segmentation_mask":
353
- key = 0
354
  data_dir = []
 
355
  for folder in files:
356
  tmp_dir = []
357
  tmp_dir = self.get_all_file_paths(os.path.join(folder, DATA_DIR[name]))
358
  data_dir = data_dir + tmp_dir
359
 
360
- for path in data_dir:
361
- res_dic = {}
362
- for word in _DENSITY:
363
- if word in path:
364
- breast_density = word
365
- pattern = rf"(\d+\.\d+)_{word}"
366
- match = re.search(pattern, path)
367
- matched_text = match.group(1)
368
- break
369
 
370
- # Get image id to filter the respective row of the csv
371
- image_id = os.path.basename(path)
372
- # Use os.path.splitext() to split the filename into root and extension
373
- root, extension = os.path.splitext(image_id)
374
- # Get the extension without the dot
375
- image_labels = extension.lstrip(".")
376
- res_dic["Raw"] = path
377
- res_dic["mhd"] = self.get_support_file_path(path, "mhd")
378
- res_dic["loc"] = self.get_support_file_path(path, "loc")
379
- res_dic["density"] = breast_density
380
- res_dic["mass_radius"] = matched_text
381
 
382
- yield key, res_dic
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
383
  key += 1
 
384
 
385
  if self.config.name == "metadata":
386
  key = 0
 
311
  else:
312
  return "Not available for this raw file"
313
 
314
+ def sort_file_paths(self, file_paths):
315
+ digit_numbers = []
316
+ for file_path in file_paths:
317
+ for word in _DENSITY:
318
+ if word in file_path:
319
+ if self.config.name == "device-data":
320
+ pattern = rf"{word}.(\d+\.)(\d+)"
321
+ elif self.config.name == "segmentation-mask":
322
+ pattern = rf"{word}.(\d+)"
323
+ match = re.search(pattern, file_path)
324
+ if self.config.name == "device-data":
325
+ digit_numbers.append(int(match.group(2)))
326
+ elif self.config.name == "segmentation-mask":
327
+ digit_numbers.append(int(match.group(1)))
328
+ break
329
+
330
+
331
+ # Sort the list of numbers while keeping track of the original indices
332
+ sorted_numbers_with_indices = sorted(enumerate(digit_numbers), key=lambda x: x[1])
333
+
334
+ # Extract the sorted numbers and their original indices
335
+ sorted_indices, sorted_numbers = zip(*sorted_numbers_with_indices)
336
+
337
+ # Sort the file paths
338
+ sorted_file_paths = [file_paths[i] for i in sorted_indices]
339
+
340
+ return sorted_file_paths
341
 
342
  def _generate_examples(self, files, name):
343
  if self.config.name == "device_data":
 
376
 
377
 
378
  if self.config.name == "segmentation_mask":
379
+ key = 0
380
  data_dir = []
381
+ examples = []
382
  for folder in files:
383
  tmp_dir = []
384
  tmp_dir = self.get_all_file_paths(os.path.join(folder, DATA_DIR[name]))
385
  data_dir = data_dir + tmp_dir
386
 
387
+ data_dir = self.sort_file_paths(data_dir)
 
 
 
 
 
 
 
 
388
 
389
+ new_data_dir = [];
390
+ count = 1;
391
+ loc = 0;
392
+ while loc < len(data_dir):
393
+ if count % 2 == 1:
394
+ new_data_dir.append(data_dir[loc])
395
+ loc += 1
396
+ else:
397
+ new_data_dir.append('None')
398
+ count += 1
 
399
 
400
+ for path in new_data_dir:
401
+ res_dic = {}
402
+ if path == "None":
403
+ res_dic["Raw"] = "None"
404
+ res_dic["mhd"] = "None"
405
+ res_dic["loc"] = "None"
406
+ res_dic["density"] = "None"
407
+ res_dic["mass_radius"] = "None"
408
+
409
+ else:
410
+ for word in _DENSITY:
411
+ if word in path:
412
+ breast_density = word
413
+ pattern = rf"(\d+\.\d+)_{word}"
414
+ match = re.search(pattern, path)
415
+ matched_text = match.group(1)
416
+ break
417
+ # Get image id to filter the respective row of the csv
418
+ image_id = os.path.basename(path)
419
+ # Use os.path.splitext() to split the filename into root and extension
420
+ root, extension = os.path.splitext(image_id)
421
+ # Get the extension without the dot
422
+ image_labels = extension.lstrip(".")
423
+ res_dic["Raw"] = path
424
+ res_dic["mhd"] = self.get_support_file_path(path, "mhd")
425
+ res_dic["loc"] = self.get_support_file_path(path, "loc")
426
+ res_dic["density"] = breast_density
427
+ res_dic["mass_radius"] = matched_text
428
+
429
+ examples.append(res_dic)
430
+
431
+ for example in examples:
432
+ yield key, {**example}
433
  key += 1
434
+ examples = []
435
 
436
  if self.config.name == "metadata":
437
  key = 0