|
| 1 | +import os |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import tifffile |
| 5 | + |
| 6 | +NIS3D_DIR = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/training_data/nucleus/NIS3D" |
| 7 | +TRAIN_DIR = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/training_data/nucleus/2025-07_NIS3D/train" |
| 8 | +VAL_DIR = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/training_data/nucleus/2025-07_NIS3D/val" |
| 9 | +TEST_DIR = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/training_data/nucleus/2025-07_NIS3D(test" |
| 10 | + |
| 11 | +# ---Training data--- |
| 12 | + |
| 13 | +# clear: contains only 2,3,4 as seg ids |
| 14 | +train_dict_01 = { |
| 15 | + "data_dir": os.path.join(NIS3D_DIR, "suggestive_splitting/cross-image/train"), |
| 16 | + "name": "Drosophila_2", |
| 17 | + "conf_file": "ConfidenceScore.tif", |
| 18 | + "gt_file": "GroundTruth.tif", |
| 19 | + "output_dir": TRAIN_DIR, |
| 20 | + "output_name": "Drosophila_2_annotations.tif", |
| 21 | +} |
| 22 | + |
| 23 | +# contains 1, 2, 3, 4 |
| 24 | +train_dict_02 = { |
| 25 | + "data_dir": os.path.join(NIS3D_DIR, "suggestive_splitting/cross-image/train"), |
| 26 | + "name": "Zebrafish_2", |
| 27 | + "conf_file": "ConfidenceScore.tif", |
| 28 | + "gt_file": "GroundTruth.tif", |
| 29 | + "output_dir": TRAIN_DIR, |
| 30 | + "output_name": "Zebrafish_2_annotations.tif", |
| 31 | +} |
| 32 | + |
| 33 | +# contains 1, 3, 4 |
| 34 | +train_dict_03 = { |
| 35 | + "data_dir": os.path.join(NIS3D_DIR, "suggestive_splitting/cross-image/train"), |
| 36 | + "name": "MusMusculus_2", |
| 37 | + "conf_file": "scoreOfConfidence.tif", |
| 38 | + "gt_file": "gt.tif", |
| 39 | + "output_dir": TRAIN_DIR, |
| 40 | + "output_name": "MusMusculus_2_annotations.tif", |
| 41 | +} |
| 42 | + |
| 43 | +# ---Validation data--- |
| 44 | + |
| 45 | +val_dict_01 = { |
| 46 | + "data_dir": os.path.join(NIS3D_DIR, "suggestive_splitting/in-image/train"), |
| 47 | + "name": "Drosophila_1", |
| 48 | + "conf_file": "ConfidenceScore.tif", |
| 49 | + "gt_file": "GroundTruth.tif", |
| 50 | + "output_dir": VAL_DIR, |
| 51 | + "output_name": "Drosophila_1_iitrain_annotations.tif", |
| 52 | +} |
| 53 | + |
| 54 | +val_dict_02 = { |
| 55 | + "data_dir": os.path.join(NIS3D_DIR, "suggestive_splitting/in-image/train"), |
| 56 | + "name": "Zebrafish_1", |
| 57 | + "conf_file": "ConfidenceScore.tif", |
| 58 | + "gt_file": "GroundTruth.tif", |
| 59 | + "output_dir": VAL_DIR, |
| 60 | + "output_name": "Zebrafish_1_iitrain_annotations.tif", |
| 61 | +} |
| 62 | + |
| 63 | +val_dict_03 = { |
| 64 | + "data_dir": os.path.join(NIS3D_DIR, "suggestive_splitting/in-image/train"), |
| 65 | + "name": "MusMusculus_1", |
| 66 | + "conf_file": "ConfidenceScore.tif", |
| 67 | + "gt_file": "GroundTruth.tif", |
| 68 | + "output_dir": VAL_DIR, |
| 69 | + "output_name": "MusMusculus_1_iitrain_annotations.tif", |
| 70 | +} |
| 71 | + |
| 72 | +# ---Test data--- |
| 73 | + |
| 74 | +test_dict_01 = { |
| 75 | + "data_dir": os.path.join(NIS3D_DIR, "suggestive_splitting/in-image/test"), |
| 76 | + "name": "Drosophila_1", |
| 77 | + "conf_file": "ConfidenceScore.tif", |
| 78 | + "gt_file": "GroundTruth.tif", |
| 79 | + "output_dir": TEST_DIR, |
| 80 | + "output_name": "Drosophila_1_iitest_annotations.tif", |
| 81 | +} |
| 82 | + |
| 83 | +test_dict_02 = { |
| 84 | + "data_dir": os.path.join(NIS3D_DIR, "suggestive_splitting/in-image/test"), |
| 85 | + "name": "Zebrafish_1", |
| 86 | + "conf_file": "ConfidenceScore.tif", |
| 87 | + "gt_file": "GroundTruth.tif", |
| 88 | + "output_dir": TEST_DIR, |
| 89 | + "output_name": "Zebrafish_1_iitest_annotations.tif", |
| 90 | +} |
| 91 | + |
| 92 | +test_dict_03 = { |
| 93 | + "data_dir": os.path.join(NIS3D_DIR, "suggestive_splitting/in-image/test"), |
| 94 | + "name": "MusMusculus_1", |
| 95 | + "conf_file": "ConfidenceScore.tif", |
| 96 | + "gt_file": "GroundTruth.tif", |
| 97 | + "output_dir": TEST_DIR, |
| 98 | + "output_name": "MusMusculus_1_iitest_annotations.tif", |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +def filter_unmasked_data(conf_path, in_path, out_path): |
| 103 | + conf = tifffile.imread(conf_path) |
| 104 | + gt = tifffile.imread(in_path) |
| 105 | + segmentation_ids = list(np.unique(conf)[1:]) |
| 106 | + if 1 in segmentation_ids: |
| 107 | + instance_ids = list(np.unique(gt)[1:]) |
| 108 | + print(f"Number of instances before filtering: {len(instance_ids)}") |
| 109 | + gt[conf == 1] = 0 |
| 110 | + instance_ids = list(np.unique(gt)[1:]) |
| 111 | + print(f"Number of instances after filtering: {len(instance_ids)}") |
| 112 | + tifffile.imwrite(out_path, gt) |
| 113 | + else: |
| 114 | + instance_ids = list(np.unique(gt)[1:]) |
| 115 | + print(f"Number of instances: {len(instance_ids)}") |
| 116 | + tifffile.imwrite(out_path, gt) |
| 117 | + |
| 118 | + |
| 119 | +def process_data_dicts(data_dicts): |
| 120 | + for data_dict in data_dicts: |
| 121 | + data_dir = data_dict["data_dir"] |
| 122 | + dataset = os.path.join(data_dir, data_dict["name"]) |
| 123 | + conf_path = os.path.join(dataset, data_dict["conf_file"]) |
| 124 | + gt_path = os.path.join(dataset, data_dict["gt_file"]) |
| 125 | + |
| 126 | + out_dir = data_dict["output_dir"] |
| 127 | + out_name = data_dict["output_name"] |
| 128 | + out_path = os.path.join(out_dir, out_name) |
| 129 | + filter_unmasked_data(conf_path, in_path=gt_path, out_path=out_path) |
| 130 | + |
| 131 | + |
| 132 | +def prepare_training_data(): |
| 133 | + """Prepare training data based on NIS3D data. |
| 134 | +
|
| 135 | + Cross-image data of half of the samples is used for training. |
| 136 | + The other half of the samples is divided into validation data used for training and test data. |
| 137 | + The in-image data is used for this, so that every remaining sample is split in half. |
| 138 | + """ |
| 139 | + process_data_dicts([test_dict_01, test_dict_02, test_dict_03]) |
0 commit comments