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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Phase 8: Metadata Audit — completeness, consistency, correctness."""
|
| 2 |
+
import json, time
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from collections import Counter
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
OUT = Path("scripts/audit_reports")
|
| 8 |
+
DATASET = "dataset/entries_final_v3.json"
|
| 9 |
+
AUDIT_DIR = Path.cwd() if Path.cwd().name == "Scandium-Dataset" else Path("/home/shamique/Scandium Labs SSB/Scandium-Dataset")
|
| 10 |
+
|
| 11 |
+
severity_counts = {"CRITICAL": 0, "HIGH": 0, "MEDIUM": 0, "LOW": 0, "PASS": 0}
|
| 12 |
+
findings = []
|
| 13 |
+
|
| 14 |
+
def finding(severity, phase, check, status, detail):
|
| 15 |
+
severity_counts[severity] += 1
|
| 16 |
+
findings.append({"severity": severity, "phase": phase, "check": check, "status": status, "detail": str(detail)[:200]})
|
| 17 |
+
s = "🔴" if severity == "CRITICAL" else "🟠" if severity == "HIGH" else "🟡" if severity == "MEDIUM" else "🔵" if severity == "LOW" else "✅"
|
| 18 |
+
print(f" {s} [{severity:8s}] {check}: {str(detail)[:120]}")
|
| 19 |
+
|
| 20 |
+
REQUIRED_FIELDS = ["source_id", "formula", "elements", "source",
|
| 21 |
+
"space_group", "volume", "density", "structure_json",
|
| 22 |
+
"quality_score", "quality_sub_scores", "quality_flags",
|
| 23 |
+
"tier", "provenance", "nsites", "band_gap", "formation_energy_per_atom",
|
| 24 |
+
"families"]
|
| 25 |
+
|
| 26 |
+
def main():
|
| 27 |
+
print("=" * 60)
|
| 28 |
+
print(" PHASE 8: METADATA AUDIT")
|
| 29 |
+
print("=" * 60)
|
| 30 |
+
|
| 31 |
+
with open(AUDIT_DIR / DATASET) as f:
|
| 32 |
+
entries = json.load(f)
|
| 33 |
+
N = len(entries)
|
| 34 |
+
|
| 35 |
+
# Field completeness
|
| 36 |
+
print(f"\n--- Field Completeness ---")
|
| 37 |
+
field_missing = Counter()
|
| 38 |
+
for field in REQUIRED_FIELDS:
|
| 39 |
+
for e in entries:
|
| 40 |
+
if field not in e or e[field] is None:
|
| 41 |
+
field_missing[field] += 1
|
| 42 |
+
|
| 43 |
+
for field, count in sorted(field_missing.items()):
|
| 44 |
+
bad = count > 0
|
| 45 |
+
sev = "PASS"
|
| 46 |
+
if bad and count > N * 0.01:
|
| 47 |
+
sev = "HIGH" if count > N * 0.1 else "MEDIUM"
|
| 48 |
+
elif bad and count > 10:
|
| 49 |
+
sev = "LOW"
|
| 50 |
+
elif bad:
|
| 51 |
+
sev = "PASS"
|
| 52 |
+
finding(sev, "completeness", f"missing_{field}", f"{count:,} ({100*count/N:.1f}%)", "")
|
| 53 |
+
|
| 54 |
+
# Check tier distribution
|
| 55 |
+
print(f"\n--- Tier Distribution ---")
|
| 56 |
+
tier_counts = Counter(e.get("tier") for e in entries)
|
| 57 |
+
for tier, cnt in sorted(tier_counts.items()):
|
| 58 |
+
print(f" {tier:12s}: {cnt:>7,}")
|
| 59 |
+
if tier_counts.get("raw") is None:
|
| 60 |
+
finding("LOW", "tiers", "no_raw_tier", "raw tier missing from frozen set", "")
|
| 61 |
+
|
| 62 |
+
# Source distribution
|
| 63 |
+
print(f"\n--- Source Distribution ---")
|
| 64 |
+
src_counts = Counter(e.get("source") for e in entries)
|
| 65 |
+
for src, cnt in sorted(src_counts.items()):
|
| 66 |
+
print(f" {src:8s}: {cnt:>7,} ({100*cnt/N:.1f}%)")
|
| 67 |
+
|
| 68 |
+
# Family distribution
|
| 69 |
+
print(f"\n--- Family Distribution ---")
|
| 70 |
+
family_counts = Counter()
|
| 71 |
+
for e in entries:
|
| 72 |
+
for fam in e.get("families", []):
|
| 73 |
+
family_counts[fam] += 1
|
| 74 |
+
for fam, cnt in sorted(family_counts.most_common(20)):
|
| 75 |
+
print(f" {fam:20s}: {cnt:>7,}")
|
| 76 |
+
|
| 77 |
+
# Provenance completeness
|
| 78 |
+
print(f"\n--- Provenance ---")
|
| 79 |
+
prov_fields = 0
|
| 80 |
+
for e in entries:
|
| 81 |
+
if e.get("provenance"):
|
| 82 |
+
prov_fields += 1
|
| 83 |
+
print(f" Entries with provenance: {prov_fields:,} ({100*prov_fields/N:.1f}%)")
|
| 84 |
+
if prov_fields < N:
|
| 85 |
+
finding("HIGH", "provenance", "missing_provenance",
|
| 86 |
+
f"{N - prov_fields:,} entries ({100*(N-prov_fields)/N:.1f}%) missing", "")
|
| 87 |
+
else:
|
| 88 |
+
finding("PASS", "provenance", "all_have_provenance", "", "")
|
| 89 |
+
|
| 90 |
+
# Check duplicate_group metadata
|
| 91 |
+
dg_cnt = sum(1 for e in entries if e.get("duplicate_group") is not None)
|
| 92 |
+
print(f"\n Entries with duplicate_group: {dg_cnt:,} ({100*dg_cnt/N:.1f}%)")
|
| 93 |
+
|
| 94 |
+
# Check that all entries have structure_json
|
| 95 |
+
no_struct = sum(1 for e in entries if not e.get("structure_json"))
|
| 96 |
+
if no_struct > 0:
|
| 97 |
+
finding("CRITICAL", "structure", "missing_structure",
|
| 98 |
+
f"{no_struct:,} ({100*no_struct/N:.1f}%)", "")
|
| 99 |
+
else:
|
| 100 |
+
finding("PASS", "structure", "all_have_structure", "", "")
|
| 101 |
+
|
| 102 |
+
# Check structured_formula parity
|
| 103 |
+
sf_missing = sum(1 for e in entries if not e.get("structured_formula"))
|
| 104 |
+
if sf_missing > 0:
|
| 105 |
+
finding("MEDIUM", "metadata", "missing_structured_formula",
|
| 106 |
+
f"{sf_missing:,} entries", "")
|
| 107 |
+
|
| 108 |
+
# ID format consistency
|
| 109 |
+
print(f"\n--- ID Consistency ---")
|
| 110 |
+
id_formats = Counter()
|
| 111 |
+
for e in entries:
|
| 112 |
+
mid = e.get("source_id", "")
|
| 113 |
+
if mid.startswith("mp-"):
|
| 114 |
+
id_formats["mp-"] += 1
|
| 115 |
+
elif mid.startswith("oqmd-"):
|
| 116 |
+
id_formats["oqmd-"] += 1
|
| 117 |
+
elif mid.startswith("jv-") or mid.startswith("jarvis-"):
|
| 118 |
+
id_formats["jarvis"] += 1
|
| 119 |
+
else:
|
| 120 |
+
id_formats["other"] += 1
|
| 121 |
+
for fmt, cnt in sorted(id_formats.items()):
|
| 122 |
+
print(f" {fmt:12s}: {cnt:>7,}")
|
| 123 |
+
|
| 124 |
+
if id_formats.get("other", 0) > 0:
|
| 125 |
+
finding("MEDIUM", "metadata", "nonstandard_ids",
|
| 126 |
+
f"{id_formats['other']:,} nonstandard IDs", "")
|
| 127 |
+
|
| 128 |
+
# Quality flags staleness (OQMD space_group flag vs actual space_group)
|
| 129 |
+
stale_flags = 0
|
| 130 |
+
for e in entries:
|
| 131 |
+
if e.get("source") == "oqmd" and e.get("space_group") is not None:
|
| 132 |
+
flags = e.get("quality_flags", [])
|
| 133 |
+
if "missing_spacegroup" in flags:
|
| 134 |
+
stale_flags += 1
|
| 135 |
+
if stale_flags > 100:
|
| 136 |
+
finding("MEDIUM", "metadata", "stale_quality_flags",
|
| 137 |
+
f"{stale_flags:,} OQMD entries flagged missing_spacegroup but SG now populated", "flags need recalculation")
|
| 138 |
+
elif stale_flags > 0:
|
| 139 |
+
finding("LOW", "metadata", "stale_quality_flags", f"{stale_flags} entries", "")
|
| 140 |
+
else:
|
| 141 |
+
finding("PASS", "metadata", "flags_current", "", "")
|
| 142 |
+
|
| 143 |
+
# Summary
|
| 144 |
+
print(f"\n{'=' * 60}")
|
| 145 |
+
print(f" PHASE 8 SUMMARY")
|
| 146 |
+
print(f" CRITICAL: {severity_counts['CRITICAL']}")
|
| 147 |
+
print(f" HIGH: {severity_counts['HIGH']}")
|
| 148 |
+
print(f" MEDIUM: {severity_counts['MEDIUM']}")
|
| 149 |
+
print(f" LOW: {severity_counts['LOW']}")
|
| 150 |
+
print(f" PASS: {severity_counts['PASS']}")
|
| 151 |
+
print(f"{'=' * 60}")
|
| 152 |
+
|
| 153 |
+
report = {
|
| 154 |
+
"phase": "Phase 8: Metadata Audit",
|
| 155 |
+
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
| 156 |
+
"field_completeness": dict(field_missing),
|
| 157 |
+
"tier_distribution": dict(tier_counts),
|
| 158 |
+
"source_distribution": dict(src_counts),
|
| 159 |
+
"family_distribution": dict(family_counts),
|
| 160 |
+
"id_formats": dict(id_formats),
|
| 161 |
+
"stale_quality_flags": stale_flags,
|
| 162 |
+
"findings": findings,
|
| 163 |
+
"summary": dict(severity_counts),
|
| 164 |
+
}
|
| 165 |
+
with open(OUT / "phase8_metadata_audit.json", "w") as f:
|
| 166 |
+
json.dump(report, f, indent=2)
|
| 167 |
+
print(f"\n Report: {OUT / 'phase8_metadata_audit.json'}")
|
| 168 |
+
|
| 169 |
+
if __name__ == "__main__":
|
| 170 |
+
main()
|