generate_directories.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """
  2. Taken from - https://github.com/alexhamiltonRN
  3. """
  4. from pathlib import Path
  5. def generate_patient_ids(dataset_type):
  6. """
  7. This function generates the patient_ids for the directories to be created below.
  8. Ids are extracted from the raw dataset file structure.
  9. """
  10. patient_ids = []
  11. path_to_date = Path()
  12. if dataset_type == str(1):
  13. path_to_data = Path('E:/Memoire/ProstateX/train-data')
  14. else:
  15. path_to_data = Path('E:/Memoire/ProstateX/test-data')
  16. # Get list of patient_ids in folder
  17. patient_folders = [x for x in path_to_data.iterdir() if x.is_dir()]
  18. for patient_folder in patient_folders:
  19. patient_ids.append(str(patient_folder.stem))
  20. return patient_ids
  21. def generate_nifti_ds(patient_ids, dataset_type):
  22. """
  23. This function generates the directory structure for the nifti files
  24. generated from the dicom files.
  25. Directory structure for generated data:
  26. ProstateX/generated/train/nifti
  27. ProstateX/generated/test/nifti
  28. """
  29. for patient_id in patient_ids:
  30. if dataset_type == str(1):
  31. new_path = Path(str('E:/Memoire/ProstateX/generated/train/nifti/' + patient_id))
  32. new_path.mkdir(parents = True, exist_ok = True)
  33. new_path.joinpath('t2').mkdir(parents = True, exist_ok = True)
  34. new_path.joinpath('bval').mkdir(parents = True, exist_ok = True)
  35. new_path.joinpath('adc').mkdir(parents = True, exist_ok = True)
  36. new_path.joinpath('ktrans').mkdir(parents = True, exist_ok = True)
  37. else:
  38. new_path = Path(str('E:/Memoire/ProstateX/generated/test/nifti/' + patient_id))
  39. new_path.mkdir(parents = True, exist_ok = True)
  40. new_path.joinpath('t2').mkdir(parents = True, exist_ok = True)
  41. new_path.joinpath('bval').mkdir(parents = True, exist_ok = True)
  42. new_path.joinpath('adc').mkdir(parents = True, exist_ok = True)
  43. new_path.joinpath('ktrans').mkdir(parents = True, exist_ok = True)
  44. def generate_nifti_resampled_ds(patient_ids, dataset_type):
  45. """
  46. This function generates the directory structure for the nifti files
  47. generated from the dicom files.
  48. Directory structure for generated data:
  49. ProstateX/generated/train/nifti_resampled
  50. ProstateX/generated/test/nifti_resampled
  51. """
  52. for patient_id in patient_ids:
  53. if dataset_type == str(1):
  54. new_path = Path(str('E:/Memoire/ProstateX/generated/train/nifti_resampled/' + patient_id))
  55. new_path.mkdir(parents = True, exist_ok = True)
  56. new_path.joinpath('t2').mkdir(parents = True, exist_ok = True)
  57. new_path.joinpath('bval').mkdir(parents = True, exist_ok = True)
  58. new_path.joinpath('adc').mkdir(parents = True, exist_ok = True)
  59. new_path.joinpath('ktrans').mkdir(parents = True, exist_ok = True)
  60. else:
  61. new_path = Path(str('E:/Memoire/ProstateX/generated/test/nifti_resampled/' + patient_id))
  62. new_path.mkdir(parents = True, exist_ok = True)
  63. new_path.joinpath('t2').mkdir(parents = True, exist_ok = True)
  64. new_path.joinpath('bval').mkdir(parents = True, exist_ok = True)
  65. new_path.joinpath('adc').mkdir(parents = True, exist_ok = True)
  66. new_path.joinpath('ktrans').mkdir(parents = True, exist_ok = True)
  67. def generate_numpy_ds(dataset_type):
  68. """
  69. This function generates the directory structure for the final numpy
  70. arrays for the training and test sets.
  71. Director structure for processed data:
  72. ProstateX/generated/train/numpy
  73. ProstateX/generated/test/numpy
  74. """
  75. if dataset_type == str(1):
  76. new_path = Path('E:/Memoire/ProstateX/generated/train/numpy/')
  77. new_path.mkdir(parents = True, exist_ok = True)
  78. new_path.joinpath('t2').mkdir(parents = True, exist_ok = True)
  79. new_path.joinpath('bval').mkdir(parents = True, exist_ok = True)
  80. new_path.joinpath('adc').mkdir(parents = True, exist_ok = True)
  81. new_path.joinpath('ktrans').mkdir(parents = True, exist_ok = True)
  82. else:
  83. new_path = Path('E:/Memoire/ProstateX/generated/test/numpy/')
  84. new_path.mkdir(parents = True, exist_ok = True)
  85. new_path.joinpath('t2').mkdir(parents = True, exist_ok = True)
  86. new_path.joinpath('bval').mkdir(parents = True, exist_ok = True)
  87. new_path.joinpath('adc').mkdir(parents = True, exist_ok = True)
  88. new_path.joinpath('ktrans').mkdir(parents = True, exist_ok = True)
  89. def generate_dataframe_ds(dataset_type):
  90. if dataset_type == str(1):
  91. new_path = Path('E:/Memoire/ProstateX/generated/train/dataframes/')
  92. new_path.mkdir(parents = True, exist_ok = True)
  93. else:
  94. new_path = Path('E:/Memoire/ProstateX/generated/test/dataframes/')
  95. new_path.mkdir(parents = True, exist_ok = True)
  96. def generate_logs_ds(dataset_type):
  97. if dataset_type == str(1):
  98. new_path = Path('E:/Memoire/ProstateX/generated/train/logs/')
  99. new_path.mkdir(parents = True, exist_ok = True)
  100. else:
  101. new_path = Path('E:/Memoire/ProstateX/generated/test/logs/')
  102. new_path.mkdir(parents = True, exist_ok = True)
  103. def main():
  104. dataset_type = input('Generate directory structure for which type of data (1-Train; 2-Test):')
  105. patient_ids = generate_patient_ids(dataset_type)
  106. generate_nifti_ds(patient_ids, dataset_type)
  107. generate_nifti_resampled_ds(patient_ids, dataset_type)
  108. generate_numpy_ds(dataset_type)
  109. generate_dataframe_ds(dataset_type)
  110. generate_logs_ds(dataset_type)
  111. print('Done creating directory structure...')
  112. main()