File size: 1,694 Bytes
d2ce7dd | 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 | import sys
import types
import unittest
from pathlib import Path
import numpy as np
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "src"))
omegaconf_stub = types.ModuleType("omegaconf")
omegaconf_stub.OmegaConf = object
omegaconf_stub.DictConfig = object
sys.modules.setdefault("omegaconf", omegaconf_stub)
data_stub = types.ModuleType("data")
octflow_stub = types.ModuleType("data.octflow_dataset")
octflow_stub.OCTFlowDataset = object
sys.modules.setdefault("data", data_stub)
sys.modules.setdefault("data.octflow_dataset", octflow_stub)
from train_sd3_t2i import build_balanced_sampling_weights
class TaxonomySamplingTest(unittest.TestCase):
def test_temperature_sampling_gives_each_group_equal_total_weight_at_zero_temperature(self):
labels = np.array(["fundus"] * 8 + ["ubm"] * 2)
weights, counts, targets = build_balanced_sampling_weights(
labels,
temperature=0.0,
min_count=1,
)
self.assertEqual(counts, {"fundus": 8, "ubm": 2})
self.assertAlmostEqual(weights[:8].sum(), weights[8:].sum(), places=8)
self.assertEqual(set(targets), {"fundus", "ubm"})
def test_min_count_excludes_tiny_groups(self):
labels = np.array(["fundus"] * 8 + ["ir_slo"] * 2)
weights, counts, targets = build_balanced_sampling_weights(
labels,
temperature=0.5,
min_count=3,
)
self.assertEqual(counts, {"fundus": 8, "ir_slo": 2})
self.assertGreater(weights[:8].sum(), 0.0)
self.assertEqual(weights[8:].sum(), 0.0)
self.assertEqual(targets, {"fundus": 1.0})
if __name__ == "__main__":
unittest.main()
|