Update train_test_splits
Browse files- NuCLS_dataset.py +57 -26
NuCLS_dataset.py
CHANGED
@@ -20,8 +20,7 @@ Created on Tue Mar 12 16:13:56 2024
|
|
20 |
# See the License for the specific language governing permissions and
|
21 |
# limitations under the License.
|
22 |
|
23 |
-
|
24 |
-
|
25 |
import pandas as pd
|
26 |
from PIL import Image as PilImage # Import PIL Image with an alias
|
27 |
import datasets
|
@@ -51,7 +50,7 @@ _HOMEPAGE = "https://sites.google.com/view/nucls/home?authuser=0"
|
|
51 |
|
52 |
_LICENSE = "CC0 1.0 license"
|
53 |
|
54 |
-
_URL = "https://www.dropbox.com/scl/fi/
|
55 |
|
56 |
class NuCLSDataset(GeneratorBasedBuilder):
|
57 |
"""The NuCLS dataset."""
|
@@ -103,6 +102,7 @@ class NuCLSDataset(GeneratorBasedBuilder):
|
|
103 |
citation=_CITATION,
|
104 |
)
|
105 |
|
|
|
106 |
def _split_generators(self, dl_manager: DownloadManager):
|
107 |
# Download source data
|
108 |
data_dir = dl_manager.download_and_extract(_URL)
|
@@ -113,31 +113,62 @@ class NuCLSDataset(GeneratorBasedBuilder):
|
|
113 |
visualization_dir = os.path.join(base_dir, "visualization")
|
114 |
mask_dir = os.path.join(base_dir, "mask")
|
115 |
csv_dir = os.path.join(base_dir, "csv")
|
116 |
-
|
|
|
117 |
# Generate a list of unique filenames (without extensions)
|
118 |
unique_filenames = [os.path.splitext(f)[0] for f in os.listdir(rgb_dir)]
|
119 |
|
120 |
-
#
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
def _map_filenames_to_paths(self, filenames, rgb_dir, visualization_dir, mask_dir, csv_dir):
|
143 |
"""Maps filenames to file paths for each split."""
|
@@ -213,5 +244,5 @@ class NuCLSDataset(GeneratorBasedBuilder):
|
|
213 |
coords_y = row.get('coords_y', '')
|
214 |
annotations['coords_x'].append([int(coord) if coord.isdigit() else 0 for coord in coords_x.split(',')])
|
215 |
annotations['coords_y'].append([int(coord) if coord.isdigit() else 0 for coord in coords_y.split(',')])
|
216 |
-
|
217 |
return annotations
|
|
|
20 |
# See the License for the specific language governing permissions and
|
21 |
# limitations under the License.
|
22 |
|
23 |
+
%%writefile Test7.py
|
|
|
24 |
import pandas as pd
|
25 |
from PIL import Image as PilImage # Import PIL Image with an alias
|
26 |
import datasets
|
|
|
50 |
|
51 |
_LICENSE = "CC0 1.0 license"
|
52 |
|
53 |
+
_URL = "https://www.dropbox.com/scl/fi/zsm9l3bkwx808wfryv5zm/NuCLS_dataset.zip?rlkey=x3358slgrxt00zpn7zpkpjr2h&dl=1"
|
54 |
|
55 |
class NuCLSDataset(GeneratorBasedBuilder):
|
56 |
"""The NuCLS dataset."""
|
|
|
102 |
citation=_CITATION,
|
103 |
)
|
104 |
|
105 |
+
|
106 |
def _split_generators(self, dl_manager: DownloadManager):
|
107 |
# Download source data
|
108 |
data_dir = dl_manager.download_and_extract(_URL)
|
|
|
113 |
visualization_dir = os.path.join(base_dir, "visualization")
|
114 |
mask_dir = os.path.join(base_dir, "mask")
|
115 |
csv_dir = os.path.join(base_dir, "csv")
|
116 |
+
split_dir = os.path.join(base_dir, "train_test_splits")
|
117 |
+
|
118 |
# Generate a list of unique filenames (without extensions)
|
119 |
unique_filenames = [os.path.splitext(f)[0] for f in os.listdir(rgb_dir)]
|
120 |
|
121 |
+
# Process train/test split files to get slide names for each split and fold
|
122 |
+
split_slide_names = self._process_train_test_split_files(split_dir)
|
123 |
+
|
124 |
+
# Create the split generators for each fold
|
125 |
+
split_generators = []
|
126 |
+
for fold in split_slide_names:
|
127 |
+
train_slide_names, test_slide_names = split_slide_names[fold]
|
128 |
+
|
129 |
+
# Filter unique filenames based on slide names
|
130 |
+
train_filenames = [fn for fn in unique_filenames if any(sn in fn for sn in train_slide_names)]
|
131 |
+
test_filenames = [fn for fn in unique_filenames if any(sn in fn for sn in test_slide_names)]
|
132 |
+
|
133 |
+
# Map filenames to file paths
|
134 |
+
train_filepaths = self._map_filenames_to_paths(train_filenames, rgb_dir, visualization_dir, mask_dir, csv_dir)
|
135 |
+
test_filepaths = self._map_filenames_to_paths(test_filenames, rgb_dir, visualization_dir, mask_dir, csv_dir)
|
136 |
+
|
137 |
+
# Add split generators for the fold
|
138 |
+
split_generators.append(
|
139 |
+
datasets.SplitGenerator(
|
140 |
+
name=f"{datasets.Split.TRAIN}_fold_{fold}",
|
141 |
+
gen_kwargs={"filepaths": train_filepaths}
|
142 |
+
)
|
143 |
+
)
|
144 |
+
split_generators.append(
|
145 |
+
datasets.SplitGenerator(
|
146 |
+
name=f"{datasets.Split.TEST}_fold_{fold}",
|
147 |
+
gen_kwargs={"filepaths": test_filepaths}
|
148 |
+
)
|
149 |
+
)
|
150 |
+
|
151 |
+
return split_generators
|
152 |
+
|
153 |
+
def _process_train_test_split_files(self, split_dir):
|
154 |
+
"""Reads the train/test split CSV files and returns a dictionary with fold numbers as keys and tuple of train/test slide names as values."""
|
155 |
+
split_slide_names = {}
|
156 |
+
for split_file in os.listdir(split_dir):
|
157 |
+
file_path = os.path.join(split_dir, split_file)
|
158 |
+
fold_number = split_file.split('_')[1] # Assumes file naming format "fold_X_[train/test].csv"
|
159 |
+
|
160 |
+
with open(file_path, 'r') as f:
|
161 |
+
csv_reader = csv.reader(f)
|
162 |
+
next(csv_reader) # Skip header
|
163 |
+
for row in csv_reader:
|
164 |
+
slide_name = row[1] # Assuming slide_name is in the first column
|
165 |
+
if "train" in split_file:
|
166 |
+
split_slide_names.setdefault(fold_number, ([], []))[0].append(slide_name)
|
167 |
+
elif "test" in split_file:
|
168 |
+
split_slide_names.setdefault(fold_number, ([], []))[1].append(slide_name)
|
169 |
+
|
170 |
+
return split_slide_names
|
171 |
+
|
172 |
|
173 |
def _map_filenames_to_paths(self, filenames, rgb_dir, visualization_dir, mask_dir, csv_dir):
|
174 |
"""Maps filenames to file paths for each split."""
|
|
|
244 |
coords_y = row.get('coords_y', '')
|
245 |
annotations['coords_x'].append([int(coord) if coord.isdigit() else 0 for coord in coords_x.split(',')])
|
246 |
annotations['coords_y'].append([int(coord) if coord.isdigit() else 0 for coord in coords_y.split(',')])
|
247 |
+
|
248 |
return annotations
|