-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
119 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
""" | ||
Quality Control module for ADTL, report submodule | ||
""" | ||
|
||
import json | ||
import sqlite3 | ||
from string import Template | ||
from pathlib import Path | ||
from typing import List, Any, Dict | ||
|
||
import pandas as pd | ||
|
||
from . import get_result_from_insertion | ||
|
||
RULES_SUBFOLDER = "r" | ||
DATASET_SUBFOLDER = "d" | ||
TEMPLATES = Path(__file__).parent / "templates" | ||
STYLE = TEMPLATES / "style.css" | ||
INDEX = "index.html" | ||
|
||
|
||
def render_result(result: Dict[str, Any]) -> str: | ||
result = get_result_from_insertion(result) # type: ignore | ||
tmpl = "<li><tt>[{ success } / { total }}]</tt> { dataset } / { file }" | ||
if result["fail_data"] is not None: | ||
fail_data = pd.DataFrame(json.loads(result["fail_data"])) | ||
tmpl += """ | ||
<details> | ||
<summary>Failed rows</summary> | ||
<pre>{ log }</p> | ||
</details></li>""".format( | ||
log=str(fail_data) | ||
) | ||
else: | ||
tmpl += "</li>" | ||
return tmpl.format(**result) | ||
|
||
|
||
def render_index(rules: List[Dict[str, Any]], datasets: List[str]) -> str: | ||
dataset_index = "\n".join( | ||
f"""<li><a href="d/{dataset}.html">{dataset}</a></li>""" | ||
for dataset in datasets | ||
) | ||
rule_index = "\n".join( | ||
f"""<li><a href="r/{r["name"]}.html">{r["description"]}</a></li>""" | ||
for r in rules | ||
) | ||
return Template((TEMPLATES / "index.html").read_text()).substitute( | ||
dict(dataset_index=dataset_index, rule_index=rule_index) | ||
) | ||
|
||
|
||
def read_sql( | ||
conn: sqlite3.Connection, sql: str, columns: List[str] | ||
) -> List[Dict[str, Any]]: | ||
cur = conn.cursor() | ||
res = cur.execute(sql) | ||
return [dict(zip(columns, r)) for r in res.fetchall()] | ||
|
||
|
||
def make_report(store_database: str, output_folder: Path = Path("qc_report")): | ||
"Makes report from results database" | ||
|
||
if not output_folder.exists(): | ||
output_folder.mkdir() | ||
conn = sqlite3.connect(store_database) | ||
datasets = read_sql(conn, "SELECT DISTINCT dataset FROM results", ["dataset"]) | ||
datasets = [n["dataset"] if n["dataset"] else "unlabelled" for n in datasets] | ||
rules = read_sql( | ||
conn, | ||
"SELECT name, description, long_description FROM rules", | ||
["name", "description", "long_description"], | ||
) | ||
print(rules) | ||
(output_folder / "style.css").write_text(STYLE.read_text()) | ||
(output_folder / INDEX).write_text(render_index(rules, datasets)) | ||
results = read_sql( | ||
conn, | ||
"SELECT * from results", | ||
[ | ||
"rule", | ||
"dataset", | ||
"file", | ||
"rows_success", | ||
"rows_fail", | ||
"ratio_success", | ||
"rows_fail_idx", | ||
"success", | ||
"mostly", | ||
"fail_data", | ||
], | ||
) | ||
print(results) | ||
|
||
|
||
if __name__ == "__main__": | ||
make_report("adtl-qc.db") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters