Download evaluation_code/utils.py from ibm-research/dp-bench: direct link, hf CLI and curl.
- Browser
- Download file 3.29 kB
-
https://huggingface.co/datasets/ibm-research/dp-bench/resolve/main/evaluation_code/utils.py
- Command line
-
hf download hf://datasets/ibm-research/dp-bench/evaluation_code/utils.py
-
curl -L -o utils.py https://huggingface.co/datasets/ibm-research/dp-bench/resolve/main/evaluation_code/utils.py
3.29 kB
| from collections import defaultdict | |
| from dp_bench_classes import * | |
| import os | |
| from pathlib import Path | |
| ROOT_DIR = Path(os.path.dirname(os.path.realpath(__file__))).parent | |
| DATA_DIR = Path.resolve(ROOT_DIR / "data").resolve() | |
| SCHEMA_DIR = (DATA_DIR / "bird_elt_dp/bird_db_schema/").resolve() | |
| BENCH_DIR = (DATA_DIR / "dp-bench/").resolve() | |
| SQLITE_DB_DIR = (DATA_DIR / "bird_data/").resolve() | |
| def extract_xml(txt, target_xml): | |
| xmlstart = f"<{target_xml}>" | |
| xmlend = f"</{target_xml}>" | |
| if xmlstart not in txt or xmlend not in txt: | |
| return [] | |
| outputs = [] | |
| while xmlstart in txt: | |
| txt = txt[txt.find(xmlstart)+len(xmlstart):] | |
| outputs.append(txt[:txt.find(xmlend)]) | |
| txt = txt[txt.find(xmlend)+len(xmlend):] | |
| return outputs | |
| def gt_to_db(dbname, raw_gt_data_product): | |
| tables = {} | |
| derived_columns = [] | |
| for table, columns in raw_gt_data_product['dp_tables'].items(): | |
| col_types = {} | |
| real_cols = set() | |
| for col in columns: | |
| if col["is_derived"]: | |
| # assert len(col['provenance_cols']) == 1 | |
| derived_columns.append(DerivedColumn(name=col['name'].lower(), provenance_sql=col['provenance'], | |
| provenance_tables={ | |
| ptab:Table(name=ptab, columns=set([c.lower() for c in pcols])) | |
| for ptab,pcols in col['provenance_cols'].items() | |
| })) | |
| else: | |
| real_cols.add(col['name'].lower()) | |
| tables[table] = Table(name=table, columns=real_cols) | |
| return DataBase(name=dbname, tables=tables, derived_columns=derived_columns) | |
| def viewgen_pred_to_db(dbname, pred_content): | |
| tables = {} | |
| derived_cols = [] | |
| if pred_content is not None: | |
| for k,v in pred_content.items(): | |
| if k.lower() == 'derived_columns': | |
| for dv in v: | |
| derived_cols.append(DerivedColumn(name=dv['name'], | |
| provenance_sql=dv['SQL'], | |
| provenance_tables={ | |
| tab:Table(name=tab, columns=set([c.lower() for c in cols])) | |
| for tab,cols in dv['source_columns'].items() | |
| })) | |
| else: | |
| try: | |
| tables[k.lower()] = Table(name=k.lower(), columns=set(col.lower() for col in v)) | |
| except: | |
| print(k, v) | |
| return DataBase(name=dbname, tables=tables, derived_columns=derived_cols) | |
| def get_pred_cols(tabcol, base_schema, all_relevant_cols=False): | |
| pred = defaultdict(lambda: set()) | |
| for table, columns in tabcol.items(): | |
| table = table.lower() | |
| if not all_relevant_cols and table == "derived_columns": | |
| continue | |
| if table not in base_schema: | |
| continue | |
| for c in columns: | |
| c = c.lower() | |
| if c in base_schema[table]: | |
| pred[table].add(c) | |
| else: | |
| pass | |
| return pred |