"""
Failure database coverage and quality audit
============================================

Load four versioned public source extracts without network access. The catalogue
also records restricted sources and missing exposure denominators. Each published
record keeps its population, units, source location and data-quality flags.

See the `failure-rate guide <../../_generated/guides/failure-rates.html>`_.
"""

import matplotlib.pyplot as plt

from energy_data_retrieval.failure_database import database_coverage, source_catalogue

coverage = database_coverage().set_index("source_id")
print(coverage[["records", "usable_records", "device_groups"]])
print(source_catalogue()[["source_id", "access"]].to_string(index=False))
plot = coverage[["usable_records"]].rename(columns={"usable_records": "Usable"})
plot["Excluded with reason"] = coverage.records - coverage.usable_records
ax = plot.plot.barh(stacked=True, figsize=(9, 4), color=["#167b89", "#e79b43"])
ax.set(
    xlabel="Source / population / failure-mode records",
    ylabel="",
    title="479 records retained, 449 evaluated",
)
plt.tight_layout()
plt.show()
