-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfact_sheet_utils.py
More file actions
executable file
·357 lines (322 loc) · 12.8 KB
/
Copy pathfact_sheet_utils.py
File metadata and controls
executable file
·357 lines (322 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
352
353
354
355
356
357
# vim:ts=4:sw=4:tw=0:sts=4:et
"""Helpers for analysing collection fact sheets and aggregate rows."""
from typing import Any
from oomutils import count_matches_oom, get_oom_interval
FACT_DIMENSION_KEYS = ("sex", "age_range", "sample_type", "disease")
NO_STAR_FACT_SUMS_WARNING = (
"No-star fact-sheet fallback is enabled. Derived marginal sums may "
"double-count overlapping records or undercount omitted rows and violate "
"fact-sheet aggregation assumptions."
)
def _is_numeric_count(value: Any) -> bool:
"""Return whether a value is an integer count rather than a boolean."""
return isinstance(value, int) and not isinstance(value, bool)
def normalize_fact_dimension_value(value: Any) -> Any:
"""Return a comparable scalar value for a fact-sheet dimension cell."""
if isinstance(value, dict):
if "id" in value:
return value["id"]
if "name" in value:
return value["name"]
return value
def count_star_dimensions(fact: dict[str, Any], dimension_keys=FACT_DIMENSION_KEYS) -> int:
"""Count how many dimensions of a fact row are aggregated as ``*``."""
return sum(
1
for key in dimension_keys
if normalize_fact_dimension_value(fact.get(key)) == "*"
)
def has_fact_sheet(collection: dict[str, Any]) -> bool:
"""Return whether a collection advertises at least one fact-sheet row."""
return bool(collection.get("facts"))
def get_all_star_rows(
facts: list[dict[str, Any]],
dimension_keys=FACT_DIMENSION_KEYS,
) -> list[dict[str, Any]]:
"""Return rows where all tracked dimensions are aggregated as ``*``."""
return [
fact
for fact in facts
if all(
normalize_fact_dimension_value(fact.get(key)) == "*"
for key in dimension_keys
)
]
def get_all_but_one_star_rows(
facts: list[dict[str, Any]],
dimension_keys=FACT_DIMENSION_KEYS,
) -> list[dict[str, Any]]:
"""Return rows with one concrete dimension and stars in all others."""
rows = []
for fact in facts:
values = {
key: normalize_fact_dimension_value(fact.get(key))
for key in dimension_keys
}
concrete_keys = [
key for key, value in values.items() if value not in (None, "", "*")
]
if len(concrete_keys) != 1:
continue
concrete_key = concrete_keys[0]
if all(key == concrete_key or value == "*" for key, value in values.items()):
rows.append(fact)
return rows
def get_no_star_rows(
facts: list[dict[str, Any]],
dimension_keys=FACT_DIMENSION_KEYS,
) -> list[dict[str, Any]]:
"""Return fully concrete rows with no missing or aggregate dimensions."""
return [
fact
for fact in facts
if all(
normalize_fact_dimension_value(fact.get(key)) not in (None, "", "*")
for key in dimension_keys
)
]
def get_dimension_values(
facts: list[dict[str, Any]],
dimension_keys=FACT_DIMENSION_KEYS,
) -> dict[str, list[Any]]:
"""Collect normalized non-star values present for each fact dimension."""
values: dict[str, set[Any]] = {key: set() for key in dimension_keys}
for fact in facts:
for key in dimension_keys:
value = normalize_fact_dimension_value(fact.get(key))
if value not in (None, "", "*"):
values[key].add(value)
return {key: sorted(values[key]) for key in dimension_keys}
def get_matching_one_star_rows(
facts: list[dict[str, Any]],
dimension_key: str,
expected_value: Any,
dimension_keys=FACT_DIMENSION_KEYS,
) -> list[dict[str, Any]]:
"""Return all rows aggregated on every dimension except one expected value."""
normalized_expected = normalize_fact_dimension_value(expected_value)
rows = []
for fact in facts:
normalized_value = normalize_fact_dimension_value(fact.get(dimension_key))
if normalized_value != normalized_expected:
continue
if count_star_dimensions(fact, dimension_keys) != len(dimension_keys) - 1:
continue
if all(
key == dimension_key
or normalize_fact_dimension_value(fact.get(key)) == "*"
for key in dimension_keys
):
rows.append(fact)
return rows
def _fact_dimension_and_value(
fact: dict[str, Any],
dimension_keys=FACT_DIMENSION_KEYS,
) -> tuple[str, Any]:
"""Return the concrete dimension and value from an all-but-one-star row."""
concrete = [
(key, normalize_fact_dimension_value(fact.get(key)))
for key in dimension_keys
if normalize_fact_dimension_value(fact.get(key)) not in (None, "", "*")
]
if len(concrete) != 1:
raise ValueError(f"Expected one concrete fact dimension, found {len(concrete)}.")
return concrete[0]
def _append_oom_warning(
warnings: list[dict[str, Any]],
*,
count: Any,
oom_value: Any,
count_name: str,
oom_name: str,
) -> None:
"""Append an OoM consistency warning without failing on malformed metadata."""
if not _is_numeric_count(count) or oom_value in (None, ""):
return
try:
lower, upper = get_oom_interval(oom_value)
matches = count_matches_oom(count, oom_value)
except (TypeError, ValueError):
warnings.append(
{
"code": f"invalid_{oom_name}",
"message": f"Collection {oom_name} value {oom_value!r} is invalid.",
"actual": oom_value,
"expected": "non-negative integer order of magnitude",
}
)
return
if not matches:
warnings.append(
{
"code": f"all_star_{count_name}_oom_mismatch",
"message": (
f"All-star aggregate {count_name} ({count}) is outside the "
f"collection {oom_name} interval [{lower}, {upper})."
),
"actual": count,
"expected": f"[{lower}, {upper})",
}
)
def analyze_collection_fact_sheet(
collection: dict[str, Any],
facts: list[dict[str, Any]],
dimension_keys=FACT_DIMENSION_KEYS,
) -> dict[str, Any]:
"""Summarize aggregate-row consistency for one collection fact sheet."""
all_star_rows = get_all_star_rows(facts, dimension_keys)
all_star_row = all_star_rows[0] if len(all_star_rows) == 1 else None
all_star_samples = None if all_star_row is None else all_star_row.get("number_of_samples")
all_star_donors = None if all_star_row is None else all_star_row.get("number_of_donors")
all_but_one_rows = get_all_but_one_star_rows(facts, dimension_keys)
collection_size = collection.get("size")
collection_donors = collection.get("number_of_donors")
warnings = []
if facts and len(all_star_rows) != 1:
warnings.append(
{
"code": "missing_all_star" if not all_star_rows else "multiple_all_star",
"message": (
f"Expected exactly one all-star aggregate row, found {len(all_star_rows)}."
),
"actual": len(all_star_rows),
"expected": 1,
}
)
if _is_numeric_count(collection_size) and _is_numeric_count(all_star_samples):
if collection_size != all_star_samples:
warnings.append(
{
"code": "all_star_samples_mismatch",
"message": (
"All-star aggregate number_of_samples does not match "
f"collection size ({all_star_samples} != {collection_size})."
),
"actual": all_star_samples,
"expected": collection_size,
}
)
if _is_numeric_count(collection_donors) and _is_numeric_count(all_star_donors):
if collection_donors != all_star_donors:
warnings.append(
{
"code": "all_star_donors_mismatch",
"message": (
"All-star aggregate number_of_donors does not match "
f"collection number_of_donors ({all_star_donors} != {collection_donors})."
),
"actual": all_star_donors,
"expected": collection_donors,
}
)
_append_oom_warning(
warnings,
count=all_star_samples,
oom_value=collection.get("order_of_magnitude"),
count_name="samples",
oom_name="order_of_magnitude",
)
_append_oom_warning(
warnings,
count=all_star_donors,
oom_value=collection.get("order_of_magnitude_donors"),
count_name="donors",
oom_name="order_of_magnitude_donors",
)
dimension_values = get_dimension_values(facts, dimension_keys)
missing_all_but_one_values = []
duplicate_all_but_one_values = []
if facts and not all_but_one_rows:
warnings.append(
{
"code": "missing_all_but_one",
"message": "Fact sheet has no all-but-one-star aggregate rows.",
"actual": 0,
"expected": "at least one row and one row per represented dimension value",
}
)
for dimension in dimension_keys:
for value in dimension_values[dimension]:
rows = get_matching_one_star_rows(facts, dimension, value, dimension_keys)
if not rows:
missing = {"dimension": dimension, "value": value, "rows": 0}
missing_all_but_one_values.append(missing)
warnings.append(
{
"code": "missing_all_but_one_value",
"message": (
f"Missing all-but-one-star aggregate for {dimension} value {value}."
),
"actual": 0,
"expected": 1,
**missing,
}
)
elif len(rows) > 1:
duplicate = {
"dimension": dimension,
"value": value,
"rows": len(rows),
}
duplicate_all_but_one_values.append(duplicate)
warnings.append(
{
"code": "multiple_all_but_one_value",
"message": (
f"Expected one all-but-one-star aggregate for {dimension} "
f"value {value}, found {len(rows)}."
),
"actual": len(rows),
"expected": 1,
**duplicate,
}
)
if all_star_row is not None:
for row in all_but_one_rows:
dimension, value = _fact_dimension_and_value(row, dimension_keys)
for field, all_star_value, code_suffix in (
("number_of_samples", all_star_samples, "samples"),
("number_of_donors", all_star_donors, "donors"),
):
row_value = row.get(field)
if (
_is_numeric_count(row_value)
and _is_numeric_count(all_star_value)
and row_value > all_star_value
):
warnings.append(
{
"code": f"all_but_one_{code_suffix}_above_all_star",
"message": (
f"All-but-one-star {field} for {dimension} value {value} "
f"({row_value}) exceeds the all-star aggregate ({all_star_value})."
),
"actual": row_value,
"expected": f"<= {all_star_value}",
"dimension": dimension,
"value": value,
"fact_id": row.get("id", ""),
}
)
donors_present = any(
_is_numeric_count(fact.get("number_of_donors"))
and fact["number_of_donors"] > 0
for fact in facts
)
return {
"fact_rows": len(facts),
"all_star_rows": len(all_star_rows),
"all_star_row": all_star_row,
"all_star_number_of_samples": all_star_samples,
"all_star_number_of_donors": all_star_donors,
"all_but_one_rows": len(all_but_one_rows),
"all_but_one_complete": bool(all_but_one_rows)
and not missing_all_but_one_values
and not duplicate_all_but_one_values,
"missing_all_but_one_values": missing_all_but_one_values,
"duplicate_all_but_one_values": duplicate_all_but_one_values,
"collection_size": collection_size,
"collection_number_of_donors": collection_donors,
"warnings": warnings,
"donors_present": donors_present,
}