Datasets:
Download scripts/features.py from NovaeonStudio/p2p-payment-block-benchmark: direct link, hf CLI and curl.
- Browser
- Download file 4.66 kB
-
https://huggingface.co/datasets/NovaeonStudio/p2p-payment-block-benchmark/resolve/main/scripts/features.py
- Command line
-
hf download hf://datasets/NovaeonStudio/p2p-payment-block-benchmark/scripts/features.py
-
curl -L -o features.py https://huggingface.co/datasets/NovaeonStudio/p2p-payment-block-benchmark/resolve/main/scripts/features.py
4.66 kB
| """Feature-Tabelle je Fall (Bestellposition) zum Vorhersagezeitpunkt T0 = erste Rechnungserfassung. | |
| Label "haengt": keine "Clear Invoice" innerhalb von 60 Tagen nach T0. | |
| Features nur aus Events mit Zeitstempel strikt vor T0 plus Fall-Stammdaten (bei Bestellanlage bekannt). | |
| Quelle: BPI Challenge 2019 (van Dongen 2019, 4TU.ResearchData, CC BY 4.0). | |
| """ | |
| import sys, json | |
| import numpy as np, pandas as pd | |
| # Input: BPI Challenge 2019 event log converted to parquet (see xes_to_parquet.py in NovaeonStudio/p2p-process-models-bpmn) | |
| SRC = sys.argv[1] if len(sys.argv) > 1 else "../process/out/bpic2019.parquet" | |
| OUT = sys.argv[2] if len(sys.argv) > 2 else "out/features.parquet" | |
| HORIZONT_TAGE = 60 | |
| T0_VON, T0_BIS = pd.Timestamp("2018-01-01", tz="UTC"), pd.Timestamp("2018-11-01", tz="UTC") | |
| CASE_COLS = ["case:Company", "case:Document Type", "case:Item Type", "case:Item Category", | |
| "case:Spend area text", "case:Sub spend area text", "case:Spend classification text", | |
| "case:GR-Based Inv. Verif.", "case:Goods Receipt", "case:Vendor"] | |
| ZAEHL = ["Record Goods Receipt", "Change Price", "Change Quantity", "Vendor creates invoice", | |
| "Vendor creates debit memo", "Cancel Invoice Receipt", "Set Payment Block", "Remove Payment Block", | |
| "Cancel Goods Receipt", "Change Approval for Purchase Order", "Receive Order Confirmation", | |
| "Record Service Entry Sheet", "Delete Purchase Order Item", "Change Delivery Indicator", | |
| "Record Invoice Receipt"] | |
| ev = pd.read_parquet(SRC, columns=["case:concept:name", "concept:name", "time:timestamp", "User", | |
| "Cumulative net worth (EUR)"] + CASE_COLS) | |
| ev = ev.rename(columns={"case:concept:name": "case", "concept:name": "act", "time:timestamp": "ts", | |
| "Cumulative net worth (EUR)": "netto"}) | |
| log_ende = ev["ts"].quantile(0.9999) | |
| print("Events", len(ev), "Log-Ende (99,99%-Quantil)", log_ende) | |
| t0 = ev.loc[ev.act == "Record Invoice Receipt"].groupby("case").ts.min().rename("t0") | |
| t0 = t0[(t0 >= T0_VON) & (t0 < T0_BIS)] | |
| ev = ev[ev["case"].isin(t0.index)].merge(t0, left_on="case", right_index=True) | |
| print("Faelle mit T0 im Fenster", len(t0)) | |
| # Label: erste Clear Invoice am/nach T0 | |
| clr = ev[(ev.act == "Clear Invoice") & (ev.ts >= ev.t0)].groupby("case").ts.min() | |
| tage_bis_clear = ((clr - t0).dt.total_seconds() / 86400).reindex(t0.index) | |
| label = (tage_bis_clear.isna() | (tage_bis_clear > HORIZONT_TAGE)).astype(int).rename("haengt") | |
| rpb = ev[(ev.act == "Remove Payment Block") & (ev.ts >= ev.t0)].groupby("case").ts.min() | |
| tage_bis_rpb = ((rpb - t0).dt.total_seconds() / 86400).reindex(t0.index) | |
| label_b = ((tage_bis_rpb.notna()) & (tage_bis_rpb <= HORIZONT_TAGE)).astype(int).rename("sperre_aufheben") | |
| vor = ev[ev.ts < ev.t0] # strikt vor T0: kein Leakage | |
| g = vor.groupby("case") | |
| F = pd.DataFrame(index=t0.index) | |
| F["n_events_vorher"] = g.size() | |
| F["n_aktivitaeten_vorher"] = g.act.nunique() | |
| F["anteil_manuell_vorher"] = g.User.apply(lambda s: (~s.fillna("").str.startswith("batch")).mean()) | |
| cnt = vor.groupby(["case", "act"]).size().unstack(fill_value=0) | |
| for a in ZAEHL: | |
| F["n_" + a.lower().replace(" ", "_")] = cnt[a] if a in cnt else 0 | |
| first = vor.groupby(["case", "act"]).ts.min().unstack() | |
| last = vor.groupby(["case", "act"]).ts.max().unstack() | |
| def tage(s): | |
| return ((t0 - s.reindex(t0.index)).dt.total_seconds() / 86400) | |
| F["tage_seit_bestellung"] = tage(first.get("Create Purchase Order Item", pd.Series(dtype="datetime64[ns, UTC]"))) | |
| F["tage_seit_rechnungsdatum"] = tage(first.get("Vendor creates invoice", pd.Series(dtype="datetime64[ns, UTC]"))) | |
| F["tage_seit_letztem_we"] = tage(last.get("Record Goods Receipt", pd.Series(dtype="datetime64[ns, UTC]"))) | |
| F["netto_eur_log"] = np.log1p(vor.dropna(subset=["netto"]).sort_values("ts").groupby("case").netto.last().clip(lower=0)) | |
| F["t0_wochentag"] = t0.dt.dayofweek | |
| F["t0_tag_im_monat"] = t0.dt.day | |
| stamm = ev.groupby("case")[CASE_COLS].first() | |
| stamm.columns = [c.replace("case:", "").replace(" ", "_").replace(".", "").replace("-", "_") for c in stamm.columns] | |
| F = F.join(stamm) | |
| F = F.fillna({c: 0 for c in F.columns if c.startswith("n_")}) | |
| F["t0"] = t0 | |
| F["tage_bis_clear"] = tage_bis_clear | |
| F["haengt"] = label | |
| F["sperre_aufheben"] = label_b | |
| F.index.name = "fall" | |
| F.to_parquet(OUT) | |
| info = {"faelle": int(len(F)), "anteil_haengt": float(F.haengt.mean()), "anteil_sperre_aufheben": float(F.sperre_aufheben.mean()), "horizont_tage": HORIZONT_TAGE, | |
| "log_ende": str(log_ende), "spalten": list(F.columns)} | |
| json.dump(info, open(OUT.replace(".parquet", "_info.json"), "w"), indent=1, ensure_ascii=False) | |
| print(json.dumps(info, indent=1, ensure_ascii=False)) | |