-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_sampling.py
More file actions
351 lines (302 loc) · 12.8 KB
/
Copy pathdata_sampling.py
File metadata and controls
351 lines (302 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from dataclasses import dataclass
from logging import info
from pathlib import Path
from typing import Callable, Dict, List, NamedTuple, Optional, Tuple
import numpy as np
from dataclasses_json import Undefined, dataclass_json
from scipy.cluster.vq import kmeans2
import tiling
from data import DataEntry
from processing import Processing, ProcessingConfig
class DataEntryGroup(NamedTuple):
all_entries: List[DataEntry]
num_entries_to_choose: Optional[int]
def __len__(self) -> int:
return (
self.num_entries_to_choose
if self.num_entries_to_choose is not None
else len(self.all_entries)
)
@dataclass_json(undefined=Undefined.RAISE)
@dataclass
class FilteringDataSamplingConfig(ProcessingConfig):
tile_filter_label_fg_ratio: Optional[float] = None
tile_filter_tissue_fg_ratio: Optional[float] = None
class FilteringDataSampling(Processing):
def __init__(
self,
working_dir: Path,
cfg: FilteringDataSamplingConfig,
dry_run: bool = False,
name: Optional[str] = None,
):
super().__init__(
name if name is not None else "filtering_data_sampling",
working_dir,
cfg,
dry_run,
)
def _run(
self,
train_entries: Dict[str, List[DataEntry]],
val_entries: Dict[str, List[DataEntry]],
) -> Tuple[List[DataEntryGroup], List[DataEntryGroup]]:
cfg: FilteringDataSamplingConfig = self._cfg
train_entries = _flatten_and_filter(train_entries, cfg)
val_entries = _flatten_and_filter(val_entries, cfg)
num_train = len(train_entries)
num_val = len(val_entries)
train_entries = DataEntryGroup(train_entries, num_train)
val_entries = DataEntryGroup(val_entries, num_val)
num_total = num_train + num_val
info(
(
f"Number of filtered examples: train={num_train} ({num_train/num_total:.2f}), "
f"val={num_val} ({num_val/num_total:.2f}), total={num_total}."
)
)
return [train_entries], [val_entries]
def _flatten_and_filter(
entries_per_image: Dict[str, List[DataEntry]], cfg: FilteringDataSamplingConfig
) -> List[DataEntry]:
filtered_entries = []
for image_name, entries in entries_per_image.items():
fullscale_sample_tiling = entries[0].samples[0].parent
target_tiling = (
entries[0].targets[0].parent if len(entries[0].targets) > 0 else None
)
tissue_ratio_path = fullscale_sample_tiling / "tissue_fg_ratios.lut"
label_ratio_path = (
target_tiling / "label_fg_ratios.lut" if target_tiling is not None else None
)
entry_filter = _create_entry_filter_predicate(
image_name,
cfg,
tissue_ratio_path,
label_ratio_path,
)
for entry in entries:
fullscale_sample = entry.samples[0]
assert fullscale_sample.parent == fullscale_sample_tiling
assert len(entry.targets) == 0 or entry.targets[0].parent == target_tiling
assert (
len(entry.targets) == 0
or entry.targets[0].name == fullscale_sample.name
)
if entry_filter(entry):
filtered_entries.append(entry)
return filtered_entries
def _create_entry_filter_predicate(
image_name: str,
cfg: FilteringDataSamplingConfig,
tissue_ratio_path: Path,
label_ratio_path: Optional[Path],
) -> Callable[[DataEntry], bool]:
predicates = []
if cfg.tile_filter_label_fg_ratio is not None:
info(
(
f"Filtering {image_name} by label FG/BG ratio. Ratios path: {label_ratio_path}, threshold: "
f"{cfg.tile_filter_label_fg_ratio}."
)
)
predicates.append(
_create_fg_ratio_filter_predicate(
lambda e: e.targets[0],
label_ratio_path,
cfg.tile_filter_label_fg_ratio,
)
)
if cfg.tile_filter_tissue_fg_ratio is not None:
info(
(
f"Filtering {image_name} by tissue FG/BG ratio. Ratios path: {tissue_ratio_path}, threshold: "
f"{cfg.tile_filter_tissue_fg_ratio}."
)
)
predicates.append(
_create_fg_ratio_filter_predicate(
lambda e: e.samples[0],
tissue_ratio_path,
cfg.tile_filter_tissue_fg_ratio,
)
)
def _apply_predicates(entry: DataEntry):
for p in predicates:
if not p(entry):
return False
return True
return _apply_predicates
def _create_fg_ratio_filter_predicate(
tile_from_entry: Callable[[DataEntry], Path],
tile_ratios_path: Path,
threshold_ratio: float,
) -> Callable[[DataEntry], bool]:
tile_ratios = np.load(tile_ratios_path)
def fg_ratio_predicate(entry: DataEntry) -> bool:
tile = tile_from_entry(entry)
flat_idx, _ = tiling.get_tile_indices(tile)
return tile_ratios[flat_idx] > threshold_ratio
return fg_ratio_predicate
##### Stratified sampling #####
class StratifyingDataSampling(Processing):
def __init__(
self,
working_dir: Path,
dry_run: bool = False,
name: Optional[str] = None,
):
super().__init__(
name if name is not None else "stratifying_data_sampling",
working_dir,
None,
dry_run,
)
def _run(
self,
train_entries: Dict[str, List[DataEntry]],
val_entries: Dict[str, List[DataEntry]],
) -> Tuple[List[DataEntryGroup], List[DataEntryGroup]]:
train_entries = _flatten_and_add_fg_ratios(train_entries)
fg_ratios = list(train_entries.values())
# Cluster tissue and label ratios of all training tiles. This allows stratifying the tiles based on their
# "relevance" for training. At the moment, three levels of relevance are presumed:
# 1. Highly relevant tiles: contain significant portions of both tissue and labels
# 2. Reasonably relevant tiles: contain significant portions of tissue but less labels (i.e. they are mostly
# stroma)
# 3. Less relevant tiles: contain significant portions of neither tissue nor labels (i.e. they are mostly glass
# slide).
# These presumptions are encouraged by the initialization of the clusters' centroids below.
# Note that we do not include the validation tiles into the clustering but stratify them based on the clustering
# results on only the training tiles, i.e. the stratification is a learned characteristic of the training data.
mean_tissue = np.mean([r.tissue for r in fg_ratios])
mean_label = np.mean([r.label for r in fg_ratios])
info(
f"Mean FG/BG ratios in training split prior to sampling: tissue={mean_tissue}, label={mean_label}."
)
init_centroids = np.array([[0, 0], [mean_tissue, mean_label], [1, 1]])
k = len(init_centroids)
centroids, cluster_labels = kmeans2(
np.asarray([(r.tissue, r.label) for r in fg_ratios]), init_centroids
)
# Order clusters from most to least relevant (i.e. order according to label ratio).
cluster_order = np.argsort(centroids[:, 1])[::-1]
info(
(
f"Stratifying training and validation splits into {k} strata, respectively, "
f"around centroids derived from training split:\n{centroids[cluster_order]}."
)
)
# Stratify training entries according to the determined clustering.
train_entries_strata = [[] for _ in range(k)]
for ct, entry in zip(cluster_labels, train_entries.keys()):
train_entries_strata[cluster_order[ct]].append(entry)
info(
f"Stratified training split into strata of sizes: {[len(stratum) for stratum in train_entries_strata]}."
)
# Stratify validation entries by assigning each sample to its closest centroid in terms of tissue and label
# ratios.
val_entries = _flatten_and_add_fg_ratios(val_entries)
val_entries_strata = [[] for _ in range(k)]
for entry, ratios in val_entries.items():
ct = np.argmin(
[
(ct[0] - ratios.tissue) ** 2 + (ct[1] - ratios.label) ** 2
for ct in centroids
]
)
val_entries_strata[cluster_order[ct]].append(entry)
info(
f"Stratified validation split into strata of sizes: {[len(stratum) for stratum in val_entries_strata]}."
)
train_strata, num_train = _add_num_samples(
train_entries_strata, centroids, cluster_order, 1.0, "training"
)
val_strata, num_val = _add_num_samples(
val_entries_strata, centroids, cluster_order, 1.0, "validation"
)
num_total = num_train + num_val
info(
(
f"Number of sampled examples: train={num_train} ({num_train/num_total:.2f}), "
f"val={num_val} ({num_val/num_total:.2f}), total={num_total}."
)
)
return train_strata, val_strata
class _FgRatios(NamedTuple):
tissue: float
label: float
def _flatten_and_add_fg_ratios(
entries_per_image: Dict[str, List[DataEntry]]
) -> Dict[DataEntry, _FgRatios]:
entries_and_ratios = {}
for entries in entries_per_image.values():
fullscale_sample_tiling = entries[0].samples[0].parent
target_tiling = entries[0].targets[0].parent
tissue_ratios = np.load(fullscale_sample_tiling / "tissue_fg_ratios.lut")
label_ratios = np.load(target_tiling / "label_fg_ratios.lut")
for entry in entries:
fullscale_sample = entry.samples[0]
target = entry.targets[0]
assert fullscale_sample.parent == fullscale_sample_tiling
assert target.parent == target_tiling
assert fullscale_sample.name == target.name
fullscale_sample_flat_idx, _ = tiling.get_tile_indices(fullscale_sample)
target_flat_idx, _ = tiling.get_tile_indices(target)
assert fullscale_sample_flat_idx == target_flat_idx
tissue = tissue_ratios[fullscale_sample_flat_idx]
label = label_ratios[target_flat_idx]
assert entry not in entries_and_ratios
entries_and_ratios[entry] = _FgRatios(tissue, label)
return entries_and_ratios
def _add_num_samples(
entries_strata: List[List[DataEntry]],
centroids: np.ndarray,
cluster_order: np.ndarray,
highest_stratum_sample_rate: float,
split_name: str,
) -> Tuple[List[DataEntryGroup], int]:
strata = []
num_samples_strata = 0
mean_tissue = 0
mean_label = 0
desired_mean_label = 0.51
for i, entries_stratum in enumerate(entries_strata):
centroid = centroids[cluster_order[i]]
num_total_stratum = len(entries_stratum)
# Undersample less relevant entries compared to more relevant ones such that the resulting overall sampling is
# roughly balanced in terms of label foreground and still contains some glass slide as negative examples.
if i == 0:
num_samples_stratum = int(highest_stratum_sample_rate * num_total_stratum)
else:
while True:
info(f"The desired mean label FG/BG ratio is {desired_mean_label:.3f}.")
try:
current_mean_label = mean_label / num_samples_strata
sample_rate = (current_mean_label - desired_mean_label) / (
desired_mean_label - centroid[1]
)
except ZeroDivisionError: # Just in case.
sample_rate = highest_stratum_sample_rate
if sample_rate >= 0.0:
break
else:
info(
"Could not fulfill desired ratio. Retrying with decreased number."
)
desired_mean_label = current_mean_label - 0.01
num_samples_stratum = int(sample_rate * num_samples_strata)
desired_mean_label -= 0.01
info(
f"Sampling {num_samples_stratum} out of {num_total_stratum} {split_name} entries from stratum #{i+1}."
)
strata.append(DataEntryGroup(entries_stratum, num_samples_stratum))
num_samples_strata += num_samples_stratum
mean_tissue += num_samples_stratum * centroid[0]
mean_label += num_samples_stratum * centroid[1]
mean_tissue /= num_samples_strata
mean_label /= num_samples_strata
info(
f"Expected mean FG/BG ratios in {split_name} split: tissue={mean_tissue}, label={mean_label}."
)
return strata, num_samples_strata