Spaces:
Sleeping
Sleeping
File size: 8,717 Bytes
78661be b13e551 78661be b13e551 | 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 | """
Milder mixer: keep light reds light, do not crush near-equal warms, legacy still
importable, and post-reencode whole-image rescan catches new clashes.
"""
import numpy as np
from pipeline.conflict import detect_conflicts
from pipeline.reencoding import enforce_rg_separation, reencode
from pipeline.reencoding_mild import (
MILD_GREEN_UP_CAP,
MILD_MAX_DELTA_L,
enforce_rg_separation_mild,
reencode_legacy,
reencode_mild,
rescan_and_reencode_mild,
)
def test_legacy_alias_is_current_reencode():
assert reencode_legacy is not reencode_mild
red = np.array([70.0, 40.0, 20.0])
green = np.array([40.0, -40.0, 20.0])
centers = np.stack([red, green])
out_a = reencode(centers, [(0, 1)], 0.8, "deutan")
out_b = reencode_legacy(centers, [(0, 1)], 0.8, "deutan")
assert np.allclose(out_a, out_b)
def test_light_red_dark_green_keeps_red_light():
# Pink-ish / light red vs dark green: polarity must keep red light.
light_red = np.array([72.0, 42.0, 18.0])
dark_green = np.array([38.0, -36.0, 22.0])
centers = np.stack([light_red, dark_green])
out = reencode_mild(centers, [(0, 1)], 0.8, "deutan")
assert out[0, 0] >= light_red[0] - 1.0, out[0, 0]
assert out[1, 0] <= dark_green[0] + 1.0, out[1, 0]
assert out[0, 0] > out[1, 0]
def test_near_equal_warm_green_not_crushed():
# Color-wheel case: pink and green almost the same L*.
pink = np.array([62.0, 38.0, 8.0])
green = np.array([60.0, -34.0, 18.0])
centers = np.stack([pink, green])
out = reencode_mild(centers, [(0, 1)], 0.8, "deutan")
assert abs(out[0, 0] - pink[0]) <= MILD_MAX_DELTA_L + 1e-6
assert abs(out[1, 0] - green[0]) <= MILD_MAX_DELTA_L + 1e-6
# Must not apply the old 20+ L* crush on the warm side.
assert (pink[0] - out[0, 0]) < 20.0
def test_mild_safety_net_caps_gap():
orig = np.array([
[62.0, 38.0, 8.0],
[60.0, -34.0, 18.0],
])
out = enforce_rg_separation_mild(
orig.copy(),
target_gap=30.0,
conflict_pairs=[(0, 1)],
original_centers=orig,
max_delta_l=38.0,
)
assert abs(out[0, 0] - orig[0, 0]) <= MILD_MAX_DELTA_L + 1e-6
assert abs(out[1, 0] - orig[1, 0]) <= MILD_MAX_DELTA_L + 1e-6
def test_detect_conflicts_after_reencode_finds_new_pair():
"""Original L* skip hides pink-brown; after_reencode=True must surface it."""
# 0: light pink, 1: green (near-equal L*), 2: darker warm/brown
orig = np.array([
[72.0, 42.0, 12.0],
[70.0, -36.0, 18.0],
[44.0, 28.0, 22.0],
], dtype=np.float64)
assert abs(orig[0, 0] - orig[2, 0]) >= 20.0
# CVD view of the originals: pink/green collapse; brown still far in L*.
sim_orig = np.array([
[71.0, 4.0, 16.0],
[70.5, 3.5, 16.5],
[45.0, 8.0, 20.0],
], dtype=np.float64)
found = detect_conflicts(orig, sim_orig, "deutan", after_reencode=False)
assert (0, 2) not in found and (2, 0) not in found
# After the mix darkens pink next to brown, simulated view collapses them.
sim_moved = np.array([
[46.0, 5.0, 18.0],
[70.5, 3.5, 16.5],
[45.0, 6.0, 19.0],
], dtype=np.float64)
found_after = detect_conflicts(orig, sim_moved, "deutan", after_reencode=True)
assert (0, 2) in found_after or (2, 0) in found_after, found_after
def test_resolution_rate_is_honest_when_pair_still_flagged():
"""A pair still flagged after the mix must NOT count as resolved.
Regression: the metric used to count a pair resolved on de_post >= 20 even
when the same detector still flagged it, printing 100% while the colors
still collapsed.
"""
from pipeline.metrics import compute_metrics
from pipeline.cvd_simulation import get_simulation_matrix
from pipeline.reencoding import _simulate_lab_center
red = np.array([62.0, 45.0, 20.0])
green = np.array([60.0, -42.0, 20.0])
centers = np.stack([red, green])
sim = _simulate_lab_center(centers, get_simulation_matrix(0.8, "deutan"))
img = np.zeros((40, 40, 3), dtype=np.float64)
img[..., :] = centers[0]
m = compute_metrics(
img, img, 0.8, "deutan",
centers_orig=centers, centers_sim=sim,
centers_corr=centers, # unchanged: pair must still be flagged
cluster_weights=np.array([0.5, 0.5]),
)
assert m["n_conflicts_total"] == 1
assert m["conflict_resolution_rate"] == 0.0
assert m["conflict_diagnostics"]["unresolved_pairs"] == [[0, 1]]
def test_rescan_and_reencode_mild_runs_second_pass():
"""After fixing only pink-green, rescan should handle leftovers without exploding L*."""
orig = np.array([
[72.0, 42.0, 12.0],
[70.0, -36.0, 18.0],
[44.0, 28.0, 22.0],
], dtype=np.float64)
# First mix: only the pink-green pair (what today's pipeline would re-encode).
after_first = reencode_mild(orig, [(0, 1)], 0.8, "deutan")
after_first = enforce_rg_separation_mild(
after_first,
conflict_pairs=[(0, 1)],
original_centers=orig,
severity=0.8,
cvd_type="deutan",
max_delta_l=38.0,
)
mod, found = rescan_and_reencode_mild(
orig,
after_first,
0.8,
"deutan",
already_seen=[(0, 1)],
max_delta_l=38.0,
)
# Must not explode L* past the mild cap relative to centers entering the rescan.
for i in range(3):
assert abs(float(mod[i, 0]) - float(after_first[i, 0])) <= MILD_MAX_DELTA_L + 1e-6
assert isinstance(found, list)
def test_green_naturalness_not_shifted_to_blue():
"""Verify that reencode_mild never reduces b* for green clusters (preventing blue/cyan shift in normal vision)."""
from pipeline.reencoding_mild import _is_any_green
green_lab = np.array([55.0, -35.0, 20.0])
red_lab = np.array([53.0, 45.0, 25.0])
assert _is_any_green(green_lab)
assert not _is_any_green(red_lab)
# Red-green conflict pair
centers = np.stack([red_lab, green_lab])
mod = reencode_mild(centers, [(0, 1)], 0.8, "deutan")
# Green is index 1. Its b* must NOT have decreased.
orig_b = green_lab[2]
mod_b = mod[1, 2]
assert mod_b >= orig_b - 1e-5, f"Green b* must not drop below original {orig_b}, got {mod_b}"
# Its a* should also remain negative (green)
assert mod[1, 1] < -2.0, f"Green a* must remain green, got {mod[1, 1]}"
def test_green_not_washed_out_and_already_separated_rg_skipped():
"""Green with >= 15 L* separation from red is not flagged, and conflicting green is capped at MILD_GREEN_UP_CAP."""
from pipeline.cvd_simulation import get_simulation_matrix
from pipeline.reencoding import _simulate_lab_center
# Ring-chart palette: light green (L=60.1), dark green (L=45.8), red (L=44.4)
centers = np.array([
[79.5, 6.0, 9.3], # 0: background
[60.1, -45.4, 48.0], # 1: light green (already +15.7 L* above red)
[45.8, -41.6, 27.8], # 2: dark green (only +1.4 L* above red -> conflicts)
[44.4, 69.0, 45.1], # 3: red
], dtype=np.float64)
labels = ["background", "roi-color #1", "roi-object #1", "roi-object #2"]
weights = np.array([0.85, 0.06, 0.04, 0.05], dtype=np.float64)
pinned = {0}
sim_mat = get_simulation_matrix(1.0, "deutan")
sim_centers = _simulate_lab_center(centers, sim_mat)
conflicts = detect_conflicts(
centers, sim_centers, "deutan",
cluster_weights=weights, cluster_labels=labels, pinned_indices=pinned,
)
# Light green (1) has |60.1 - 44.4| = 15.7 >= 15.0, so it must NOT be flagged!
assert (1, 3) not in conflicts and (3, 1) not in conflicts, f"Light green should not conflict: {conflicts}"
assert (2, 3) in conflicts or (3, 2) in conflicts, f"Dark green vs red must conflict: {conflicts}"
mod = reencode_mild(centers, conflicts, 1.0, "deutan", cluster_labels=labels, pinned_indices=pinned)
mod = enforce_rg_separation_mild(
mod, conflict_pairs=conflicts, cluster_labels=labels,
pinned_indices=pinned, original_centers=centers, severity=1.0, cvd_type="deutan",
)
mod, _ = rescan_and_reencode_mild(
centers, mod, 1.0, "deutan",
cluster_labels=labels, pinned_indices=pinned, cluster_weights=weights, already_seen=conflicts,
)
# Light green (1) must stay completely untouched
assert np.isclose(mod[1, 0], centers[1, 0]), f"Unconflicted light green moved: {centers[1, 0]} -> {mod[1, 0]}"
# Dark green (2) upward shift must be capped at MILD_GREEN_UP_CAP (+5.0) even after rescan
assert (mod[2, 0] - centers[2, 0]) <= MILD_GREEN_UP_CAP + 1e-6, (
f"Dark green pushed too light: {centers[2, 0]} -> {mod[2, 0]}"
)
|