-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdirectory_stats_utils.py
More file actions
executable file
·426 lines (379 loc) · 16.7 KB
/
directory_stats_utils.py
File metadata and controls
executable file
·426 lines (379 loc) · 16.7 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# vim:ts=4:sw=4:tw=0:sts=4:et
"""Helpers for computing per-biobank Directory statistics."""
import logging as log
from collections import Counter
from typing import Any
from fact_sheet_utils import analyze_collection_fact_sheet, has_fact_sheet
from nncontacts import NNContacts
from oomutils import estimate_count_from_oom_or_none
def _normalize_scalar(value: Any) -> Any:
"""Return the scalar identifier/value from EMX-style scalar wrappers."""
if isinstance(value, dict):
if "id" in value:
return value["id"]
if "name" in value:
return value["name"]
return value
def _normalize_country(value: Any) -> str:
"""Return country identifier from a raw country field."""
scalar_value = _normalize_scalar(value)
return "" if scalar_value is None else str(scalar_value)
def extract_staging_area_from_id(entity_id: str) -> str:
"""Return the staging-area code encoded in a Directory entity id."""
return NNContacts.extract_staging_area(entity_id)
def _sort_biobank_rows(rows: list[dict[str, Any]]) -> None:
"""Sort biobank rows according to the requested staging-area semantics."""
if rows and all(row.get("staging_area") == "EXT" for row in rows):
rows.sort(key=lambda row: (str(row.get("country", "")), str(row.get("id", ""))))
return
rows.sort(key=lambda row: str(row.get("id", "")))
def _normalize_multi_value_list(value: Any) -> list[str]:
"""Return a normalized list of scalar values from a list-like field."""
if value in (None, ""):
return []
if not isinstance(value, list):
value = [value]
normalized = []
for item in value:
scalar = _normalize_scalar(item)
if scalar in (None, ""):
continue
normalized.append(str(scalar))
return normalized
def _format_counter(counter: Counter) -> str:
"""Return a compact deterministic counter representation."""
if not counter:
return ""
return "; ".join(f"{key}={counter[key]}" for key in sorted(counter))
def _is_withdrawn(entity: dict[str, Any] | None) -> bool:
"""Return whether an entity is marked as withdrawn."""
if entity is None:
return False
return bool(entity.get("withdrawn"))
def _normalize_filter_values(values: list[str] | None) -> set[str]:
"""Return normalized uppercase filter values."""
if not values:
return set()
normalized = set()
for value in values:
for item in str(value).split(","):
item = item.strip()
if item:
normalized.add(item.upper())
return normalized
def _build_breakdown_rows(
biobank_id: str,
biobank_name: str,
label: str,
counter: Counter,
) -> list[dict[str, Any]]:
"""Return normalized breakdown rows for one biobank."""
return [
{
"biobank_id": biobank_id,
"biobank_name": biobank_name,
label: key,
"count": counter[key],
}
for key in sorted(counter)
]
def _build_breakdown_summary_rows(
rows: list[dict[str, Any]],
label: str,
) -> list[dict[str, Any]]:
"""Return overall totals for a breakdown table."""
totals = Counter()
for row in rows:
totals[row[label]] += row["count"]
return [
{
label: key,
"count": totals[key],
}
for key in sorted(totals)
]
def build_directory_stats(
directory,
*,
country_filters: list[str] | None = None,
staging_area_filters: list[str] | None = None,
collection_type_filters: list[str] | None = None,
) -> dict[str, list[dict[str, Any]]]:
"""Build multi-table Directory statistics for all loaded biobanks."""
normalized_country_filters = _normalize_filter_values(country_filters)
normalized_staging_area_filters = _normalize_filter_values(staging_area_filters)
normalized_collection_type_filters = _normalize_filter_values(
collection_type_filters
)
selected_biobanks: dict[str, dict[str, Any]] = {}
for biobank in directory.getBiobanks():
country = _normalize_country(biobank.get("country")).upper()
staging_area = extract_staging_area_from_id(biobank.get("id", "")).upper()
if normalized_country_filters and country not in normalized_country_filters:
continue
if (
normalized_staging_area_filters
and staging_area not in normalized_staging_area_filters
):
continue
selected_biobanks[biobank["id"]] = biobank
collections_by_biobank: dict[str, list[dict[str, Any]]] = {}
for collection in directory.getCollections():
biobank_id = directory.getCollectionBiobankId(collection["id"])
biobank = selected_biobanks.get(biobank_id)
if biobank is None:
continue
collection_types = {
collection_type.upper()
for collection_type in _normalize_multi_value_list(collection.get("type"))
}
if (
normalized_collection_type_filters
and not collection_types.intersection(normalized_collection_type_filters)
):
continue
collections_by_biobank.setdefault(biobank_id, []).append(collection)
services_by_biobank: dict[str, list[dict[str, Any]]] = {}
for service in directory.getServices():
biobank = service.get("biobank")
if biobank and "id" in biobank:
biobank_record = selected_biobanks.get(biobank["id"])
if biobank_record is None:
continue
services_by_biobank.setdefault(biobank["id"], []).append(service)
biobank_rows: list[dict[str, Any]] = []
collection_type_rows: list[dict[str, Any]] = []
service_type_rows: list[dict[str, Any]] = []
fact_sheet_warning_rows: list[dict[str, Any]] = []
top_level_collection_type_total_counter: Counter = Counter()
subcollection_type_total_counter: Counter = Counter()
for biobank_id in sorted(selected_biobanks):
biobank = selected_biobanks[biobank_id]
biobank_id = biobank["id"]
biobank_name = biobank.get("name", "")
biobank_collections = collections_by_biobank.get(biobank_id, [])
biobank_services = services_by_biobank.get(biobank_id, [])
samples_explicit = 0
donors_explicit = 0
samples_oom = 0
donors_oom = 0
top_level_collections = 0
subcollections = 0
collections_with_facts = 0
collections_with_all_star = 0
collections_missing_valid_all_star = 0
collections_all_star_inconsistent_samples = 0
collections_all_star_inconsistent_donors = 0
collection_type_counter: Counter = Counter()
top_level_collection_type_counter: Counter = Counter()
subcollection_type_counter: Counter = Counter()
service_type_counter: Counter = Counter()
for collection in biobank_collections:
collection_id = collection["id"]
is_top_level = directory.isTopLevelCollection(collection_id)
if is_top_level:
top_level_collections += 1
else:
subcollections += 1
for collection_type in set(_normalize_multi_value_list(collection.get("type"))):
collection_type_counter[collection_type] += 1
if is_top_level:
top_level_collection_type_counter[collection_type] += 1
top_level_collection_type_total_counter[collection_type] += 1
else:
subcollection_type_counter[collection_type] += 1
subcollection_type_total_counter[collection_type] += 1
if directory.isCountableCollection(collection_id, "size"):
samples_explicit += collection["size"]
elif is_top_level:
size_estimate = estimate_count_from_oom_or_none(
collection.get("order_of_magnitude"),
collection_id=collection_id,
field_name="order_of_magnitude",
)
if size_estimate is not None:
samples_oom += size_estimate
if directory.isCountableCollection(collection_id, "number_of_donors"):
donors_explicit += collection["number_of_donors"]
elif is_top_level:
donor_estimate = estimate_count_from_oom_or_none(
collection.get("order_of_magnitude_donors"),
collection_id=collection_id,
field_name="order_of_magnitude_donors",
)
if donor_estimate is not None:
donors_oom += donor_estimate
if has_fact_sheet(collection):
collections_with_facts += 1
fact_sheet = analyze_collection_fact_sheet(
collection,
directory.getCollectionFacts(collection_id),
)
if fact_sheet["all_star_rows"] == 1:
collections_with_all_star += 1
else:
collections_missing_valid_all_star += 1
for warning in fact_sheet["warnings"]:
if warning["code"] == "all_star_samples_mismatch":
collections_all_star_inconsistent_samples += 1
elif warning["code"] == "all_star_donors_mismatch":
collections_all_star_inconsistent_donors += 1
fact_sheet_warning_rows.append(
{
"biobank_id": biobank_id,
"biobank_name": biobank_name,
"collection_id": collection_id,
"collection_name": collection.get("name", ""),
"code": warning["code"],
"message": warning["message"],
"expected": warning.get("expected"),
"actual": warning.get("actual"),
}
)
for service in biobank_services:
for service_type in set(
_normalize_multi_value_list(
service.get("serviceTypes", service.get("service_types"))
)
):
service_type_counter[service_type] += 1
biobank_rows.append(
{
"id": biobank_id,
"name": biobank_name,
"country": _normalize_country(biobank.get("country")),
"staging_area": extract_staging_area_from_id(biobank_id),
"withdrawn": _is_withdrawn(biobank),
"collection_records_total": len(biobank_collections),
"top_level_collections": top_level_collections,
"subcollections": subcollections,
"collection_type_breakdown": _format_counter(collection_type_counter),
"top_level_collection_type_breakdown": _format_counter(
top_level_collection_type_counter
),
"subcollection_type_breakdown": _format_counter(
subcollection_type_counter
),
"samples_explicit": samples_explicit,
"samples_oom": samples_oom,
"samples_total": samples_explicit + samples_oom,
"donors_explicit": donors_explicit,
"donors_oom": donors_oom,
"donors_total": donors_explicit + donors_oom,
"services_total": len(biobank_services),
"service_type_breakdown": _format_counter(service_type_counter),
"collections_with_facts": collections_with_facts,
"collections_with_all_star": collections_with_all_star,
"collections_missing_valid_all_star": collections_missing_valid_all_star,
"collections_all_star_inconsistent_samples": collections_all_star_inconsistent_samples,
"collections_all_star_inconsistent_donors": collections_all_star_inconsistent_donors,
}
)
collection_type_rows.extend(
_build_breakdown_rows(
biobank_id,
biobank_name,
"collection_type",
collection_type_counter,
)
)
service_type_rows.extend(
_build_breakdown_rows(
biobank_id,
biobank_name,
"service_type",
service_type_counter,
)
)
_sort_biobank_rows(biobank_rows)
return {
"biobank_rows": biobank_rows,
"collection_type_rows": collection_type_rows,
"collection_type_summary_rows": _build_breakdown_summary_rows(
collection_type_rows,
"collection_type",
),
"service_type_rows": service_type_rows,
"service_type_summary_rows": _build_breakdown_summary_rows(
service_type_rows,
"service_type",
),
"top_level_collection_type_summary_rows": [
{"collection_type": key, "count": top_level_collection_type_total_counter[key]}
for key in sorted(top_level_collection_type_total_counter)
],
"subcollection_type_summary_rows": [
{"collection_type": key, "count": subcollection_type_total_counter[key]}
for key in sorted(subcollection_type_total_counter)
],
"fact_sheet_warning_rows": fact_sheet_warning_rows,
}
def build_biobank_stats(
directory,
*,
country_filters: list[str] | None = None,
staging_area_filters: list[str] | None = None,
collection_type_filters: list[str] | None = None,
) -> list[dict[str, Any]]:
"""Build the main per-biobank statistics rows."""
return build_directory_stats(
directory,
country_filters=country_filters,
staging_area_filters=staging_area_filters,
collection_type_filters=collection_type_filters,
)["biobank_rows"]
def build_stats_summary(rows: list[dict[str, Any]]) -> dict[str, int]:
"""Build a summary row for per-biobank stats output."""
top_level_collection_type_totals = Counter()
subcollection_type_totals = Counter()
service_type_totals = Counter()
for row in rows:
for item in str(row.get("top_level_collection_type_breakdown", "")).split("; "):
if not item:
continue
key, value = item.split("=", 1)
top_level_collection_type_totals[key] += int(value)
for item in str(row.get("subcollection_type_breakdown", "")).split("; "):
if not item:
continue
key, value = item.split("=", 1)
subcollection_type_totals[key] += int(value)
for item in str(row.get("service_type_breakdown", "")).split("; "):
if not item:
continue
key, value = item.split("=", 1)
service_type_totals[key] += int(value)
return {
"biobanks_total": len(rows),
"withdrawn_biobanks": sum(1 for row in rows if row.get("withdrawn")),
"biobanks_with_collections": sum(
1 for row in rows if row["collection_records_total"] > 0
),
"biobanks_with_services": sum(1 for row in rows if row["services_total"] > 0),
"collection_records_total": sum(row["collection_records_total"] for row in rows),
"top_level_collections": sum(row["top_level_collections"] for row in rows),
"subcollections": sum(row["subcollections"] for row in rows),
"samples_explicit": sum(row["samples_explicit"] for row in rows),
"samples_oom": sum(row["samples_oom"] for row in rows),
"samples_total": sum(row["samples_total"] for row in rows),
"donors_explicit": sum(row["donors_explicit"] for row in rows),
"donors_oom": sum(row["donors_oom"] for row in rows),
"donors_total": sum(row["donors_total"] for row in rows),
"services_total": sum(row["services_total"] for row in rows),
"collections_with_facts": sum(row["collections_with_facts"] for row in rows),
"collections_with_all_star": sum(row["collections_with_all_star"] for row in rows),
"collections_missing_valid_all_star": sum(
row["collections_missing_valid_all_star"] for row in rows
),
"collections_all_star_inconsistent_samples": sum(
row["collections_all_star_inconsistent_samples"] for row in rows
),
"collections_all_star_inconsistent_donors": sum(
row["collections_all_star_inconsistent_donors"] for row in rows
),
"top_level_collection_type_breakdown": _format_counter(
top_level_collection_type_totals
),
"subcollection_type_breakdown": _format_counter(subcollection_type_totals),
"service_type_breakdown": _format_counter(service_type_totals),
}