seglint 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- seglint/__init__.py +29 -0
- seglint/_toml.py +12 -0
- seglint/api.py +524 -0
- seglint/calibrate.py +270 -0
- seglint/checks/__init__.py +0 -0
- seglint/checks/_limits.py +41 -0
- seglint/checks/_robust.py +30 -0
- seglint/checks/base.py +174 -0
- seglint/checks/catalog.py +306 -0
- seglint/checks/geometric.py +309 -0
- seglint/checks/leakage.py +265 -0
- seglint/checks/relative.py +185 -0
- seglint/checks/statistical.py +316 -0
- seglint/checks/structural.py +175 -0
- seglint/cli.py +715 -0
- seglint/config.py +297 -0
- seglint/demo.py +152 -0
- seglint/features.py +86 -0
- seglint/hashing.py +226 -0
- seglint/loaders/__init__.py +0 -0
- seglint/loaders/base.py +29 -0
- seglint/loaders/coco.py +158 -0
- seglint/loaders/png_masks.py +274 -0
- seglint/maskthumb.py +53 -0
- seglint/models.py +122 -0
- seglint/pipeline.py +319 -0
- seglint/profile.py +64 -0
- seglint/py.typed +0 -0
- seglint/relative.py +274 -0
- seglint/report/__init__.py +0 -0
- seglint/report/html.py +1462 -0
- seglint/report/json_out.py +83 -0
- seglint/report/templates/mask_note.j2 +1 -0
- seglint/report/templates/pair_panels.j2 +12 -0
- seglint/report/templates/panels.j2 +12 -0
- seglint/report/templates/report.html.j2 +665 -0
- seglint/report/text.py +226 -0
- seglint/review.py +380 -0
- seglint-0.2.0.dist-info/METADATA +217 -0
- seglint-0.2.0.dist-info/RECORD +43 -0
- seglint-0.2.0.dist-info/WHEEL +4 -0
- seglint-0.2.0.dist-info/entry_points.txt +2 -0
- seglint-0.2.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
"""Leakage and duplicate checks L001-L004 (dataset scope, records only, no disk access).
|
|
2
|
+
|
|
3
|
+
L001 groups samples by the sha256 of the image file (one finding per sample).
|
|
4
|
+
L002/L003 report near duplicates PER PAIR (one finding per unordered pair, primary sample_id =
|
|
5
|
+
the lexicographically smaller id): pHash Hamming <= `near_dup_hamming` (banded index over the
|
|
6
|
+
original hashes, queried with the hashes of all 8 dihedral variants, so mirrored and rotated
|
|
7
|
+
copies are found) and dHash Hamming <= `near_dup_dhash_hamming` for the same variant (the second
|
|
8
|
+
hash removes pairs that merely share a composition); mirrored/rotated matches must pass the
|
|
9
|
+
tighter `near_dup_dihedral_*` limits. Byte-identical files are left to L001.
|
|
10
|
+
L004 compares the masks of duplicate pairs (exact and near) and reports pairs whose masks
|
|
11
|
+
disagree.
|
|
12
|
+
|
|
13
|
+
Pair `data` keys (L002, L003, L004): sample_a, sample_b, split_a, split_b, phash_distance,
|
|
14
|
+
dhash_distance, transform (name) and transform_id: the dihedral transform to apply to image or
|
|
15
|
+
mask B to align it with A, exact (bytes identical), tier ("near_identical", "similar" or, for
|
|
16
|
+
byte-identical pairs in L004, "exact"). L004 adds iou, class_iou, iou_warn, iou_info and
|
|
17
|
+
iou_threshold (= iou_info).
|
|
18
|
+
A pair is "same split" when the splits are equal or either sample has no split; it is "across
|
|
19
|
+
splits" when both have a split and the names differ.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
from dataclasses import dataclass
|
|
25
|
+
|
|
26
|
+
from seglint.checks.base import BaseCheck, register
|
|
27
|
+
from seglint.hashing import (
|
|
28
|
+
DIHEDRAL_INVERSE,
|
|
29
|
+
DIHEDRAL_NAMES,
|
|
30
|
+
apply_dihedral,
|
|
31
|
+
find_near_pairs_dihedral,
|
|
32
|
+
hamming,
|
|
33
|
+
)
|
|
34
|
+
from seglint.maskthumb import load_thumb, mask_iou
|
|
35
|
+
from seglint.models import Severity
|
|
36
|
+
|
|
37
|
+
MAX_LISTED = 10 # duplicates named in an L001 finding
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _has_hash(r) -> bool:
|
|
41
|
+
return bool(r.phash or r.dhash)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _cross(a, b) -> bool:
|
|
45
|
+
return bool(a.split and b.split and a.split != b.split)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@dataclass(frozen=True)
|
|
49
|
+
class Pair:
|
|
50
|
+
a: object # SampleRecord with the smaller sample_id
|
|
51
|
+
b: object
|
|
52
|
+
transform: int # apply to B (image or mask) to align it with A
|
|
53
|
+
phash_distance: int
|
|
54
|
+
dhash_distance: int
|
|
55
|
+
exact: bool = False
|
|
56
|
+
tier: str = "similar" # near_identical | similar | exact
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def cross(self) -> bool:
|
|
60
|
+
return _cross(self.a, self.b)
|
|
61
|
+
|
|
62
|
+
def data(self) -> dict:
|
|
63
|
+
return {
|
|
64
|
+
"sample_a": self.a.sample_id,
|
|
65
|
+
"sample_b": self.b.sample_id,
|
|
66
|
+
"split_a": self.a.split,
|
|
67
|
+
"split_b": self.b.split,
|
|
68
|
+
"phash_distance": self.phash_distance,
|
|
69
|
+
"dhash_distance": self.dhash_distance,
|
|
70
|
+
"transform": DIHEDRAL_NAMES[self.transform],
|
|
71
|
+
"transform_id": self.transform,
|
|
72
|
+
"exact": self.exact,
|
|
73
|
+
"tier": self.tier,
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _near_pairs(ctx, records) -> list[Pair]:
|
|
78
|
+
"""Near-duplicate pairs (not byte-identical), best match per unordered pair, sorted by id."""
|
|
79
|
+
recs = sorted((r for r in records if _has_hash(r)), key=lambda r: r.sample_id)
|
|
80
|
+
th = ctx.config.thresholds
|
|
81
|
+
key = (
|
|
82
|
+
"near", len(recs), id(recs[0]) if recs else 0, id(recs[-1]) if recs else 0,
|
|
83
|
+
th.near_dup_hamming, th.near_dup_dhash_hamming,
|
|
84
|
+
th.near_dup_dihedral_hamming, th.near_dup_dihedral_dhash_hamming,
|
|
85
|
+
th.near_identical_hamming, th.near_identical_dhash_hamming,
|
|
86
|
+
th.near_identical_dihedral_hamming, th.near_identical_dihedral_dhash_hamming,
|
|
87
|
+
) # fmt: skip
|
|
88
|
+
if key in ctx.cache:
|
|
89
|
+
return ctx.cache[key]
|
|
90
|
+
variants = [tuple(v[0] for v in r.hash_variants) for r in recs]
|
|
91
|
+
hashes = [r.phash for r in recs]
|
|
92
|
+
best: dict[tuple[int, int], tuple[tuple[int, int], Pair]] = {}
|
|
93
|
+
for i, j, t, dp in find_near_pairs_dihedral(hashes, variants, th.near_dup_hamming):
|
|
94
|
+
a, b = recs[i], recs[j]
|
|
95
|
+
if a.sha256 and a.sha256 == b.sha256:
|
|
96
|
+
continue
|
|
97
|
+
dd = hamming(a.dhash if t == 0 else a.hash_variants[t - 1][1], b.dhash)
|
|
98
|
+
# a mirrored/rotated match has 7 more chances to be a coincidence: tighter limits
|
|
99
|
+
kp = th.near_dup_hamming if t == 0 else th.near_dup_dihedral_hamming
|
|
100
|
+
kd = th.near_dup_dhash_hamming if t == 0 else th.near_dup_dihedral_dhash_hamming
|
|
101
|
+
if dp > kp or dd > kd:
|
|
102
|
+
continue
|
|
103
|
+
# recs is sorted by id. Query side i with variant t: B_j ~ T_t(A_i) when i < j, so B is
|
|
104
|
+
# aligned with A by the inverse of t; when i > j the query side is B and A ~ T_t(B).
|
|
105
|
+
lo, hi = (i, j) if i < j else (j, i)
|
|
106
|
+
# tight limits give the "near_identical" tier, the rest of the pairs are "similar"
|
|
107
|
+
tp = min(kp, th.near_identical_hamming if t == 0 else th.near_identical_dihedral_hamming)
|
|
108
|
+
td = min(
|
|
109
|
+
kd,
|
|
110
|
+
th.near_identical_dhash_hamming if t == 0 else th.near_identical_dihedral_dhash_hamming,
|
|
111
|
+
)
|
|
112
|
+
tier = "near_identical" if dp <= tp and dd <= td else "similar"
|
|
113
|
+
pair = Pair(recs[lo], recs[hi], DIHEDRAL_INVERSE[t] if i < j else t, dp, dd, False, tier)
|
|
114
|
+
rank = (dp + dd, t != 0)
|
|
115
|
+
if (lo, hi) not in best or rank < best[(lo, hi)][0]:
|
|
116
|
+
best[(lo, hi)] = (rank, pair)
|
|
117
|
+
out = [best[k][1] for k in sorted(best)]
|
|
118
|
+
ctx.cache[key] = out
|
|
119
|
+
return out
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _exact_pairs(records) -> list[Pair]:
|
|
123
|
+
"""Byte-identical images: every group member paired with the group's smallest id."""
|
|
124
|
+
groups: dict[str, list] = {}
|
|
125
|
+
for r in records:
|
|
126
|
+
if r.sha256:
|
|
127
|
+
groups.setdefault(r.sha256, []).append(r)
|
|
128
|
+
out = []
|
|
129
|
+
for members in groups.values():
|
|
130
|
+
members.sort(key=lambda r: r.sample_id)
|
|
131
|
+
out += [Pair(members[0], m, 0, 0, 0, True, "exact") for m in members[1:]]
|
|
132
|
+
return sorted(out, key=lambda p: (p.a.sample_id, p.b.sample_id))
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
@register
|
|
136
|
+
class L001(BaseCheck):
|
|
137
|
+
id = "L001"
|
|
138
|
+
default_severity = Severity.WARNING
|
|
139
|
+
scope = "dataset"
|
|
140
|
+
|
|
141
|
+
def run_dataset(self, ctx, records):
|
|
142
|
+
groups: dict[str, list] = {}
|
|
143
|
+
for r in records:
|
|
144
|
+
if r.sha256:
|
|
145
|
+
groups.setdefault(r.sha256, []).append(r)
|
|
146
|
+
out = []
|
|
147
|
+
for members in groups.values():
|
|
148
|
+
if len(members) < 2:
|
|
149
|
+
continue
|
|
150
|
+
for r in members:
|
|
151
|
+
others = [o for o in members if o is not r]
|
|
152
|
+
cross = [o for o in others if _cross(r, o)]
|
|
153
|
+
out.append(
|
|
154
|
+
self.issue(
|
|
155
|
+
r,
|
|
156
|
+
f"image is byte-identical to {len(others)} other sample(s) "
|
|
157
|
+
+ (
|
|
158
|
+
f"including {len(cross)} in another split (possible leakage)"
|
|
159
|
+
if cross
|
|
160
|
+
else "in the same split (duplicate)"
|
|
161
|
+
),
|
|
162
|
+
1.0,
|
|
163
|
+
severity=Severity.ERROR if cross else Severity.WARNING,
|
|
164
|
+
duplicates=sorted(o.sample_id for o in others)[:MAX_LISTED],
|
|
165
|
+
n_duplicates=len(others),
|
|
166
|
+
)
|
|
167
|
+
)
|
|
168
|
+
return out
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class _NearDup(BaseCheck):
|
|
172
|
+
scope = "dataset"
|
|
173
|
+
across = False
|
|
174
|
+
# severity by tier: tight ("near_identical") limits are the serious tier
|
|
175
|
+
tier_severity: dict[str, Severity] = {}
|
|
176
|
+
|
|
177
|
+
def run_dataset(self, ctx, records):
|
|
178
|
+
out = []
|
|
179
|
+
for p in _near_pairs(ctx, records):
|
|
180
|
+
if p.cross != self.across:
|
|
181
|
+
continue
|
|
182
|
+
where = "another split (possible leakage)" if self.across else "the same split"
|
|
183
|
+
tr = f"; B is {DIHEDRAL_NAMES[p.transform]}-transformed" if p.transform else ""
|
|
184
|
+
kind = "near-identical" if p.tier == "near_identical" else "similar"
|
|
185
|
+
out.append(
|
|
186
|
+
self.issue(
|
|
187
|
+
p.a,
|
|
188
|
+
f"{kind} image pair: {p.b.sample_id} in {where} (pHash Hamming "
|
|
189
|
+
f"{p.phash_distance}, dHash {p.dhash_distance}{tr}) (suspicious)",
|
|
190
|
+
1.0 - p.phash_distance / (ctx.config.thresholds.near_dup_hamming + 1),
|
|
191
|
+
severity=self.tier_severity[p.tier],
|
|
192
|
+
**p.data(),
|
|
193
|
+
)
|
|
194
|
+
)
|
|
195
|
+
return out
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
@register
|
|
199
|
+
class L002(_NearDup):
|
|
200
|
+
id = "L002"
|
|
201
|
+
default_severity = Severity.WARNING
|
|
202
|
+
across = False
|
|
203
|
+
tier_severity = {"near_identical": Severity.WARNING, "similar": Severity.INFO}
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
@register
|
|
207
|
+
class L003(_NearDup):
|
|
208
|
+
id = "L003"
|
|
209
|
+
default_severity = Severity.ERROR
|
|
210
|
+
across = True
|
|
211
|
+
tier_severity = {"near_identical": Severity.ERROR, "similar": Severity.WARNING}
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
@register
|
|
215
|
+
class L004(BaseCheck):
|
|
216
|
+
"""Duplicate pair whose masks disagree.
|
|
217
|
+
|
|
218
|
+
Runs on byte-identical pairs and on near-identical-tier pairs only (a merely similar image
|
|
219
|
+
pair has no reason to share a mask). IoU of the two mask thumbnails (nearest-neighbour
|
|
220
|
+
sampled on a common 128x128 grid, mask B first aligned with the pair's dihedral transform).
|
|
221
|
+
IoU = sum over the foreground classes (all but the background and ignore classes) of
|
|
222
|
+
|A and B| divided by the sum of |A or B|, i.e. the ordinary IoU for one foreground class.
|
|
223
|
+
IoU < `mask_iou_warn` is a WARNING, IoU in [`mask_iou_warn`, `mask_iou_info`) an INFO, IoU
|
|
224
|
+
>= `mask_iou_info` nothing. Score = 1 - IoU. Pairs where neither mask has foreground are
|
|
225
|
+
skipped (that is T001's business).
|
|
226
|
+
"""
|
|
227
|
+
|
|
228
|
+
id = "L004"
|
|
229
|
+
default_severity = Severity.WARNING
|
|
230
|
+
scope = "dataset"
|
|
231
|
+
|
|
232
|
+
def run_dataset(self, ctx, records):
|
|
233
|
+
cfg = ctx.config
|
|
234
|
+
warn, info = cfg.thresholds.mask_iou_warn, cfg.thresholds.mask_iou_info
|
|
235
|
+
pairs = [
|
|
236
|
+
*_exact_pairs(records),
|
|
237
|
+
*(p for p in _near_pairs(ctx, records) if p.tier == "near_identical"),
|
|
238
|
+
]
|
|
239
|
+
out = []
|
|
240
|
+
for p in sorted(pairs, key=lambda p: (p.a.sample_id, p.b.sample_id)):
|
|
241
|
+
ta, tb = load_thumb(p.a.mask_thumb), load_thumb(p.b.mask_thumb)
|
|
242
|
+
if ta is None or tb is None:
|
|
243
|
+
continue
|
|
244
|
+
iou, per_class = mask_iou(
|
|
245
|
+
ta, apply_dihedral(tb, p.transform), cfg.background_index, cfg.ignore_index
|
|
246
|
+
)
|
|
247
|
+
if iou is None or iou >= info:
|
|
248
|
+
continue
|
|
249
|
+
kind = "byte-identical" if p.exact else "near-identical"
|
|
250
|
+
out.append(
|
|
251
|
+
self.issue(
|
|
252
|
+
p.a,
|
|
253
|
+
f"{kind} images {p.a.sample_id} and {p.b.sample_id} have different masks "
|
|
254
|
+
f"(IoU {iou:.2f}) (suspicious)",
|
|
255
|
+
1.0 - iou,
|
|
256
|
+
severity=Severity.WARNING if iou < warn else Severity.INFO,
|
|
257
|
+
iou=iou,
|
|
258
|
+
class_iou={str(c): v for c, v in per_class.items()},
|
|
259
|
+
iou_threshold=info,
|
|
260
|
+
iou_warn=warn,
|
|
261
|
+
iou_info=info,
|
|
262
|
+
**p.data(),
|
|
263
|
+
)
|
|
264
|
+
)
|
|
265
|
+
return out
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"""Relative checks R001-R004: is a mask extreme relative to the rest of THIS dataset?
|
|
2
|
+
|
|
3
|
+
Image independent and dataset scope (pass 2, SampleRecords only). Each check judges one family of
|
|
4
|
+
mask-only measures (see seglint.relative) against median +- k * scaled MAD of the same class (or
|
|
5
|
+
of all classes when the class has fewer than `min_class_samples` samples). A finding means
|
|
6
|
+
"unusual for this dataset", never "wrong": a rare but correct mask is exactly what these flag.
|
|
7
|
+
Datasets below `min_relative_samples` samples are skipped with one R000 notice.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from seglint.checks.base import BaseCheck, register
|
|
13
|
+
from seglint.models import Issue, Severity
|
|
14
|
+
from seglint.relative import (
|
|
15
|
+
MEASURES,
|
|
16
|
+
collect_values,
|
|
17
|
+
deviation,
|
|
18
|
+
k_for,
|
|
19
|
+
measure_stats,
|
|
20
|
+
normal_range,
|
|
21
|
+
skip_reason,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
SKIP_KEY = "relative_skip_notice"
|
|
25
|
+
BOUNDED = {"area_frac", "centroid_x", "centroid_y", "solidity"} # displayed range stays in [0, 1]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _fmt(measure: str, v: float) -> str:
|
|
29
|
+
if measure == "area_frac":
|
|
30
|
+
return f"{v:.1%}" if v >= 0.001 else f"{v:.3%}"
|
|
31
|
+
if measure in ("object_count", "speck_count"):
|
|
32
|
+
return f"{v:.0f}"
|
|
33
|
+
if measure == "hole_share":
|
|
34
|
+
return f"{v:.4%}"
|
|
35
|
+
return f"{v:.2f}"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def skip_notice(ctx, n_valid: int) -> list[Issue]:
|
|
39
|
+
"""The single R000 notice for a dataset below `min_relative_samples` (once per run)."""
|
|
40
|
+
reason = skip_reason(ctx.config, n_valid)
|
|
41
|
+
if ctx.cache.get(SKIP_KEY) or reason is None:
|
|
42
|
+
return []
|
|
43
|
+
ctx.cache[SKIP_KEY] = True
|
|
44
|
+
return [
|
|
45
|
+
Issue(
|
|
46
|
+
"R000",
|
|
47
|
+
Severity.INFO,
|
|
48
|
+
"dataset",
|
|
49
|
+
None,
|
|
50
|
+
f"relative checks (R001-R004, relative tiers of G002/G003) skipped: {reason}",
|
|
51
|
+
1.0,
|
|
52
|
+
{
|
|
53
|
+
"n_samples": n_valid,
|
|
54
|
+
"min_relative_samples": ctx.config.thresholds.min_relative_samples,
|
|
55
|
+
"reference": ctx.config.reference is not None,
|
|
56
|
+
},
|
|
57
|
+
)
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def shown_range(measure: str, stats, k: float) -> tuple[float, float]:
|
|
62
|
+
"""normal_range clipped to what the measure can take (shares/positions stay in [0, 1],
|
|
63
|
+
counts at their minimum)."""
|
|
64
|
+
lo, hi = normal_range(stats, k)
|
|
65
|
+
if measure in BOUNDED:
|
|
66
|
+
return max(0.0, lo), min(1.0, hi)
|
|
67
|
+
if measure in ("object_count", "speck_count"):
|
|
68
|
+
return max(1.0 if measure == "object_count" else 0.0, lo), hi
|
|
69
|
+
return lo, hi
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def relative_findings(cfg, valid, group: str) -> dict[tuple[str, int], tuple[object, list[dict]]]:
|
|
73
|
+
"""(sample id, class) -> (record, extreme measures sorted by |z| / k) for one measure group.
|
|
74
|
+
Each entry carries the R-check data keys (measure, value, median, scaled_mad, normal_range,
|
|
75
|
+
k, stats_source, class, ...) plus the private `_warn_at` (|z| of the WARNING boundary)."""
|
|
76
|
+
th = cfg.thresholds
|
|
77
|
+
found: dict[tuple[str, int], tuple[object, list[dict]]] = {}
|
|
78
|
+
for name, spec in MEASURES.items():
|
|
79
|
+
if spec.group != group:
|
|
80
|
+
continue
|
|
81
|
+
stats = measure_stats(valid, cfg, name)
|
|
82
|
+
if not stats:
|
|
83
|
+
continue
|
|
84
|
+
for cls, items in collect_values(valid, name, cfg).items():
|
|
85
|
+
st = stats[cls]
|
|
86
|
+
k = k_for(cfg, spec, cls)
|
|
87
|
+
lo, hi = shown_range(name, st, k)
|
|
88
|
+
for rec, v in items:
|
|
89
|
+
z = deviation(st, v)
|
|
90
|
+
low = z < -k and spec.side in ("both", "low")
|
|
91
|
+
high = z > k and spec.side in ("both", "high")
|
|
92
|
+
if not (low or high):
|
|
93
|
+
continue
|
|
94
|
+
entry = {
|
|
95
|
+
"measure": name,
|
|
96
|
+
"value": v,
|
|
97
|
+
"median": st.median,
|
|
98
|
+
"scaled_mad": st.scaled_mad,
|
|
99
|
+
"scale": st.scale,
|
|
100
|
+
"scale_rule": st.scale_rule,
|
|
101
|
+
"normal_range": [lo, hi],
|
|
102
|
+
"k": k,
|
|
103
|
+
"z": z,
|
|
104
|
+
"direction": "low" if low else "high",
|
|
105
|
+
"stats_source": st.source,
|
|
106
|
+
"n_stats": st.n,
|
|
107
|
+
"class": cls,
|
|
108
|
+
"_warn_at": k * th.for_class(cls, "relative_warn_factor"),
|
|
109
|
+
}
|
|
110
|
+
found.setdefault((rec.sample_id, cls), (rec, []))[1].append(entry)
|
|
111
|
+
for _, entries in found.values():
|
|
112
|
+
entries.sort(key=lambda e: -abs(e["z"]) / e["k"])
|
|
113
|
+
return found
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def describe(entry: dict, cls: int) -> str:
|
|
117
|
+
""" "class 1 area fraction 91.0% is extreme relative to this dataset (...)"."""
|
|
118
|
+
m, (lo, hi) = entry["measure"], entry["normal_range"]
|
|
119
|
+
where = "this class" if entry["stats_source"] == "class" else "all classes"
|
|
120
|
+
return (
|
|
121
|
+
f"class {cls} {MEASURES[m].label} {_fmt(m, entry['value'])} is extreme relative to this "
|
|
122
|
+
f"dataset (median {_fmt(m, entry['median'])} in {where}, normal range "
|
|
123
|
+
f"{_fmt(m, lo)}-{_fmt(m, hi)}) (unusual for this dataset)"
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class _Relative(BaseCheck):
|
|
128
|
+
scope = "dataset"
|
|
129
|
+
default_severity = Severity.INFO
|
|
130
|
+
group = ""
|
|
131
|
+
|
|
132
|
+
def run_dataset(self, ctx, records):
|
|
133
|
+
cfg = ctx.config
|
|
134
|
+
valid = [r for r in records if r.valid_labels]
|
|
135
|
+
if skip_reason(cfg, len(valid)):
|
|
136
|
+
return skip_notice(ctx, len(valid))
|
|
137
|
+
found = relative_findings(cfg, valid, self.group)
|
|
138
|
+
out = []
|
|
139
|
+
for (_sid, cls), (rec, entries) in sorted(found.items(), key=lambda kv: kv[0]):
|
|
140
|
+
top = dict(entries[0])
|
|
141
|
+
warn_at = top.pop("_warn_at")
|
|
142
|
+
others = [{k: v for k, v in e.items() if k != "_warn_at"} for e in entries[1:]]
|
|
143
|
+
out.append(
|
|
144
|
+
self.issue(
|
|
145
|
+
rec,
|
|
146
|
+
describe(top, cls),
|
|
147
|
+
abs(top["z"]) / warn_at, # 1.0 at the WARNING boundary
|
|
148
|
+
severity=Severity.WARNING if abs(top["z"]) >= warn_at else Severity.INFO,
|
|
149
|
+
**top,
|
|
150
|
+
others=others,
|
|
151
|
+
)
|
|
152
|
+
)
|
|
153
|
+
return out
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
@register
|
|
157
|
+
class R001(_Relative):
|
|
158
|
+
"""Area fraction of a class is extreme relative to the dataset (logit scale, both tails)."""
|
|
159
|
+
|
|
160
|
+
id = "R001"
|
|
161
|
+
group = "area_frac"
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
@register
|
|
165
|
+
class R002(_Relative):
|
|
166
|
+
"""Centroid position (x or y, normalised) is extreme relative to the dataset."""
|
|
167
|
+
|
|
168
|
+
id = "R002"
|
|
169
|
+
group = "position"
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
@register
|
|
173
|
+
class R003(_Relative):
|
|
174
|
+
"""Shape of the largest component is extreme: low solidity, or elongation (log, both tails)."""
|
|
175
|
+
|
|
176
|
+
id = "R003"
|
|
177
|
+
group = "shape"
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
@register
|
|
181
|
+
class R004(_Relative):
|
|
182
|
+
"""Number of objects (components above the speckle limit) is extreme (log scale)."""
|
|
183
|
+
|
|
184
|
+
id = "R004"
|
|
185
|
+
group = "object_count"
|