| @@ 103-152 (lines=50) @@ | ||
| 100 | path_to_test = os.path.join(path_to_data_folder, "test") |
|
| 101 | path_to_valid = os.path.join(path_to_data_folder, "valid") |
|
| 102 | ||
| 103 | if os.path.exists(path_to_test) is not True: |
|
| 104 | ||
| 105 | os.mkdir(path_to_test) |
|
| 106 | os.mkdir(os.path.join(path_to_test, "fixed_images")) |
|
| 107 | os.mkdir(os.path.join(path_to_test, "fixed_labels")) |
|
| 108 | os.mkdir(os.path.join(path_to_test, "moving_images")) |
|
| 109 | os.mkdir(os.path.join(path_to_test, "moving_labels")) |
|
| 110 | ||
| 111 | ratio_of_test_and_valid_samples = 0.4 |
|
| 112 | ||
| 113 | unique_case_names = [] |
|
| 114 | for file in images_fnames: |
|
| 115 | case_name_as_list = file.split("_")[0:2] |
|
| 116 | case_name = case_name_as_list[0] + "_" + case_name_as_list[1] |
|
| 117 | unique_case_names.append(case_name) |
|
| 118 | unique_case_names = np.unique(unique_case_names) |
|
| 119 | ||
| 120 | test_and_valid_cases = random.sample( |
|
| 121 | list(unique_case_names), |
|
| 122 | int(ratio_of_test_and_valid_samples * len(unique_case_names)), |
|
| 123 | ) |
|
| 124 | test_cases = test_and_valid_cases[ |
|
| 125 | 0 : int(int(ratio_of_test_and_valid_samples * len(unique_case_names) / 2)) |
|
| 126 | ] |
|
| 127 | valid_cases = test_and_valid_cases[ |
|
| 128 | int(int(ratio_of_test_and_valid_samples * len(unique_case_names) / 2)) + 1 : |
|
| 129 | ] |
|
| 130 | ||
| 131 | def move_test_cases_into_correct_path(test_cases, path_to_train, path_to_test): |
|
| 132 | folder_names = os.listdir(path_to_train) |
|
| 133 | os.chdir(path_to_train) |
|
| 134 | for case in test_cases: |
|
| 135 | for folder in folder_names: |
|
| 136 | file_names = os.listdir(os.path.join(path_to_train, folder)) |
|
| 137 | for file in file_names: |
|
| 138 | if case in file: |
|
| 139 | os.chdir(os.path.join(path_to_train, folder)) |
|
| 140 | source = file |
|
| 141 | destination = os.path.join(path_to_test, folder) |
|
| 142 | shutil.move(source, destination) |
|
| 143 | ||
| 144 | move_test_cases_into_correct_path(test_cases, path_to_train, path_to_test) |
|
| 145 | ||
| 146 | os.mkdir(path_to_valid) |
|
| 147 | os.mkdir(os.path.join(path_to_valid, "fixed_images")) |
|
| 148 | os.mkdir(os.path.join(path_to_valid, "fixed_labels")) |
|
| 149 | os.mkdir(os.path.join(path_to_valid, "moving_images")) |
|
| 150 | os.mkdir(os.path.join(path_to_valid, "moving_labels")) |
|
| 151 | ||
| 152 | move_test_cases_into_correct_path(valid_cases, path_to_train, path_to_valid) |
|
| 153 | ||
| 154 | ######## NAMING FILES SUCH THAT THEIR NAMES MATCH FOR PAIRING ######## |
|
| 155 | ||
| @@ 98-147 (lines=50) @@ | ||
| 95 | path_to_test = os.path.join(path_to_data_folder, "test") |
|
| 96 | path_to_valid = os.path.join(path_to_data_folder, "valid") |
|
| 97 | ||
| 98 | if os.path.exists(path_to_test) is not True: |
|
| 99 | ||
| 100 | os.mkdir(path_to_test) |
|
| 101 | os.mkdir(os.path.join(path_to_test, "fixed_images")) |
|
| 102 | os.mkdir(os.path.join(path_to_test, "fixed_labels")) |
|
| 103 | os.mkdir(os.path.join(path_to_test, "moving_images")) |
|
| 104 | os.mkdir(os.path.join(path_to_test, "moving_labels")) |
|
| 105 | ||
| 106 | ratio_of_test_and_valid_samples = 0.2 |
|
| 107 | ||
| 108 | unique_case_names = [] |
|
| 109 | for file in images_fnames: |
|
| 110 | case_name_as_list = file.split("_")[0:2] |
|
| 111 | case_name = case_name_as_list[0] + "_" + case_name_as_list[1] |
|
| 112 | unique_case_names.append(case_name) |
|
| 113 | unique_case_names = np.unique(unique_case_names) |
|
| 114 | ||
| 115 | test_and_valid_cases = random.sample( |
|
| 116 | list(unique_case_names), |
|
| 117 | int(ratio_of_test_and_valid_samples * len(unique_case_names)), |
|
| 118 | ) |
|
| 119 | test_cases = test_and_valid_cases[ |
|
| 120 | 0 : int(int(ratio_of_test_and_valid_samples * len(unique_case_names) / 2)) |
|
| 121 | ] |
|
| 122 | valid_cases = test_and_valid_cases[ |
|
| 123 | int(int(ratio_of_test_and_valid_samples * len(unique_case_names) / 2)) + 1 : |
|
| 124 | ] |
|
| 125 | ||
| 126 | def move_test_cases_into_correct_path(test_cases, path_to_train, path_to_test): |
|
| 127 | folder_names = os.listdir(path_to_train) |
|
| 128 | os.chdir(path_to_train) |
|
| 129 | for case in test_cases: |
|
| 130 | for folder in folder_names: |
|
| 131 | file_names = os.listdir(os.path.join(path_to_train, folder)) |
|
| 132 | for file in file_names: |
|
| 133 | if case in file: |
|
| 134 | os.chdir(os.path.join(path_to_train, folder)) |
|
| 135 | source = file |
|
| 136 | destination = os.path.join(path_to_test, folder) |
|
| 137 | shutil.move(source, destination) |
|
| 138 | ||
| 139 | move_test_cases_into_correct_path(test_cases, path_to_train, path_to_test) |
|
| 140 | ||
| 141 | os.mkdir(path_to_valid) |
|
| 142 | os.mkdir(os.path.join(path_to_valid, "fixed_images")) |
|
| 143 | os.mkdir(os.path.join(path_to_valid, "fixed_labels")) |
|
| 144 | os.mkdir(os.path.join(path_to_valid, "moving_images")) |
|
| 145 | os.mkdir(os.path.join(path_to_valid, "moving_labels")) |
|
| 146 | ||
| 147 | move_test_cases_into_correct_path(valid_cases, path_to_train, path_to_valid) |
|
| 148 | ||
| 149 | ######## NAMING FILES SUCH THAT THEIR NAMES MATCH FOR PAIRING ######## |
|
| 150 | ||