Ingestion
ingestion
Data ingestion adapters, validation gates, and normalization utilities.
This module provides standard connectors and validation gates for feeding raw client-side or
server-side datasets (such as SQL tables or CSVs) into the xpyrment experimental workflow.
| CLASS | DESCRIPTION |
|---|---|
DuckDBIngester |
High-performance out-of-core data ingestion and computation adapter using DuckDB. |
| FUNCTION | DESCRIPTION |
|---|---|
load_from_sql |
Loads experimental telemetry and assignment logs from an external relational SQL database. |
ingest_dataframe |
Ingests, validates, and copies an in-memory pandas DataFrame into the xpyrment lifecycle. |
ingest_chunks |
Ingests and yields an iterable of pandas DataFrames (chunks) for out-of-core processing. |
ingest_dask_dataframe |
Ingests, validates, and sets up a computation graph for a Dask DataFrame. |
DuckDBIngester
High-performance out-of-core data ingestion and computation adapter using DuckDB.
Provides streaming, memory-efficient statistical computations on parquet files/folders, such as covariate balance checks and Welch's t-test statistics, completely avoiding loading entire massive datasets into RAM.
Mathematical Specifications
- Standardized Mean Difference (SMD) for continuous/numeric covariates: Let \(\bar{X}_1\) and \(\bar{X}_0\) be the sample means of a covariate \(X\) in the treatment and control groups, and let \(s_1^2\) and \(s_0^2\) be their sample variances. $$ \text{SMD} = \frac{\bar{X}_1 - \bar{X}_0}{\sqrt{\frac{s_1^2 + s_0^2}{2}}} $$
- Welch's \(t\)-test for unequal variances: Let \(N_0, N_1\) be the sample sizes, \(\bar{X}_0, \bar{X}_1\) be the sample means, and \(s_0^2, s_1^2\) be the sample variances. $$ t = \frac{\bar{X}1 - \bar{X}_0}{\sqrt{\frac{s_0^2}{N_0} + \frac{s_1^2}{N_1}}} $$ The Welch-Satterthwaite degrees of freedom \(\nu\) is calculated as: $$ \nu = \frac{\left( \frac{s_0^2}{N_0} + \frac{s_1^2}{N_1} \right)^2}{\frac{\left( \frac{s_0^2}{N_0} \right)^2}{N_0 - 1} + \frac{\left( \frac{s_1^2}{N_1} \right)^2}{N_1 - 1}} $$ The two-sided \(p\)-value is: $$ p = 2 \times \left(1 - F{t, \nu}(|t|)\right) $$ where \(F_{t, \nu}\) is the cumulative distribution function (CDF) of the Student's \(t\)-distribution with \(\nu\) degrees of freedom.
- Pearson \(\chi^2\) Test of Independence for categorical covariates: $$ \chi^2 = \sum_{i=1}^R \sum_{j=1}^C \frac{(O_{i,j} - E_{i,j})^2}{E_{i,j}} $$ with degrees of freedom: $$ \text{df} = (R - 1)(C - 1) $$ where \(O_{i,j}\) and \(E_{i,j}\) are the observed and expected frequency counts, respectively.
| PARAMETER | DESCRIPTION |
|---|---|
db_path
|
The file path to a persistent DuckDB database, or ':memory:' for transient sessions.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ImportError
|
If duckdb or pyarrow are not installed. |
| METHOD | DESCRIPTION |
|---|---|
close |
Closes the underlying DuckDB connection if open to release locks. |
query |
Executes a raw SQL query against DuckDB and returns the result as a pandas DataFrame. |
compute_covariate_balance |
Computes covariate balance statistics out-of-core using DuckDB. |
compute_welch_statistics |
Computes Welch's t-test statistics for experimental metrics out-of-core using DuckDB. |
Source code in src\xpyrment\run\ingestion.py
close
Closes the underlying DuckDB connection if open to release locks.
query
Executes a raw SQL query against DuckDB and returns the result as a pandas DataFrame.
| PARAMETER | DESCRIPTION |
|---|---|
sql_query
|
A standard SQL query.
TYPE:
|
params
|
Parameters to pass into the query safely.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
pd.DataFrame: The queried records. |
Source code in src\xpyrment\run\ingestion.py
compute_covariate_balance
compute_covariate_balance(
parquet_path: str,
treatment_col: str,
covariate_cols: list,
control_group: str = None,
treatment_group: str = None,
) -> dict
Computes covariate balance statistics out-of-core using DuckDB.
Performs streaming, out-of-core scans to calculate Standardized Mean Difference (SMD) for continuous/numeric covariates and Pearson Chi-Square tests of independence for categorical covariates.
| PARAMETER | DESCRIPTION |
|---|---|
parquet_path
|
Absolute or relative path to the Parquet file or directory.
TYPE:
|
treatment_col
|
Column name identifying experimental groups/arms.
TYPE:
|
covariate_cols
|
List of column names representing categorical or continuous pre-experiment covariates.
TYPE:
|
control_group
|
The label/value of the control group. Defaults to None.
TYPE:
|
treatment_group
|
The label/value of the treatment group. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
A dictionary mapping each covariate to its balance statistics: - For numeric: {"type": "numeric", "smd": float, "p_value": float} - For categorical: {"type": "categorical", "p_value": float}
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If the parquet path does not exist. |
KeyError
|
If columns are not found in the Parquet schema. |
ValueError
|
If the dataset is empty, has fewer than 2 distinct treatment arms, or contains invalid/degenerate data. |
Source code in src\xpyrment\run\ingestion.py
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 | |
compute_welch_statistics
compute_welch_statistics(
parquet_path: str,
treatment_col: str,
metric_cols: list,
control_group: str = None,
treatment_group: str = None,
alpha: float = 0.05,
) -> dict
Computes Welch's t-test statistics for experimental metrics out-of-core using DuckDB.
Calculates means, sample variances, sample sizes, standard errors, Welch's t-statistic, degrees of freedom, p-value, confidence intervals, and statistical significance.
| PARAMETER | DESCRIPTION |
|---|---|
parquet_path
|
Absolute or relative path to the Parquet file or directory.
TYPE:
|
treatment_col
|
Column name identifying experimental groups/arms.
TYPE:
|
metric_cols
|
List of continuous metric column names.
TYPE:
|
control_group
|
The label/value of the control group. Defaults to None.
TYPE:
|
treatment_group
|
The label/value of the treatment group. Defaults to None.
TYPE:
|
alpha
|
Significance level for confidence interval calculation. Defaults to 0.05.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
A dictionary mapping each metric name to a sub-dictionary of Welch's statistics.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If the parquet path does not exist. |
KeyError
|
If columns are not found in the Parquet schema. |
ValueError
|
If the dataset is empty, has fewer than 2 distinct treatment arms, or contains zero-variance/degenerate data in treatment arms. |
Source code in src\xpyrment\run\ingestion.py
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 | |
load_from_sql
Loads experimental telemetry and assignment logs from an external relational SQL database.
Fetches exposure matrices, pre-period metrics, and covariate vectors using high-performance database adapters.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
The SQL retrieval query (e.g.,
TYPE:
|
connection_string
|
The database connection URI.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
pd.DataFrame: A cleaned pandas DataFrame containing the queried records. |
Source code in src\xpyrment\run\ingestion.py
ingest_dataframe
ingest_dataframe(
df: DataFrame,
unit_id_col: Optional[str] = None,
time_col: Optional[str] = None,
metric_cols: Optional[List[str]] = None,
categorical_cols: Optional[List[str]] = None,
schema=None,
) -> DataFrame
Ingests, validates, and copies an in-memory pandas DataFrame into the xpyrment lifecycle.
Performs localized validation checks on the pandas DataFrame, ensuring all required column signatures are mapped correctly.
| PARAMETER | DESCRIPTION |
|---|---|
df
|
The raw source DataFrame.
TYPE:
|
unit_id_col
|
Column representing unit identifiers (nulls will be dropped).
TYPE:
|
time_col
|
Column representing event timestamps (will be parsed to datetime).
TYPE:
|
metric_cols
|
Continuous metric columns (nulls will be imputed to 0.0).
TYPE:
|
categorical_cols
|
Categorical covariate columns (nulls will be imputed to "UNKNOWN").
TYPE:
|
schema
|
A user-provided Pandera schema to validate against. If None, a schema is built dynamically based on the provided columns.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
pd.DataFrame: An audited, isolated copy of the DataFrame ready for downstream operations. |
Source code in src\xpyrment\run\ingestion.py
ingest_chunks
ingest_chunks(
chunks: Iterable[DataFrame],
unit_id_col: str = None,
time_col: str = None,
metric_cols: list = None,
categorical_cols: list = None,
schema=None,
) -> Iterator[DataFrame]
Ingests and yields an iterable of pandas DataFrames (chunks) for out-of-core processing.
Applies the same localized validation and imputation checks as ingest_dataframe to each chunk.
This is highly memory efficient for massive datasets when used with e.g. pd.read_csv(..., chunksize=N).
| PARAMETER | DESCRIPTION |
|---|---|
chunks
|
An iterable or generator of raw pandas DataFrames.
TYPE:
|
unit_id_col
|
Column representing unit identifiers (nulls will be dropped).
TYPE:
|
time_col
|
Column representing event timestamps (will be parsed to datetime).
TYPE:
|
metric_cols
|
Continuous metric columns (nulls will be imputed to 0.0).
TYPE:
|
categorical_cols
|
Categorical covariate columns (nulls will be imputed to "UNKNOWN").
TYPE:
|
schema
|
A user-provided Pandera schema to validate against.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
DataFrame
|
pd.DataFrame: An audited, isolated chunk of the dataset ready for downstream operations. |
Source code in src\xpyrment\run\ingestion.py
ingest_dask_dataframe
ingest_dask_dataframe(
ddf: Any,
unit_id_col: Optional[str] = None,
time_col: Optional[str] = None,
metric_cols: Optional[List[str]] = None,
categorical_cols: Optional[List[str]] = None,
schema=None,
) -> Any
Ingests, validates, and sets up a computation graph for a Dask DataFrame.
Performs localized validation checks on the Dask DataFrame, similar to ingest_dataframe,
using lazy Dask operations without triggering computation.
| PARAMETER | DESCRIPTION |
|---|---|
ddf
|
The raw source Dask DataFrame.
TYPE:
|
unit_id_col
|
Column representing unit identifiers (nulls will be dropped).
TYPE:
|
time_col
|
Column representing event timestamps (will be parsed to datetime).
TYPE:
|
metric_cols
|
Continuous metric columns (nulls will be imputed to 0.0).
TYPE:
|
categorical_cols
|
Categorical covariate columns (nulls will be imputed to "UNKNOWN").
TYPE:
|
schema
|
A user-provided Pandera schema to validate against.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
dask.dataframe.DataFrame: A lazy Dask DataFrame with data cleaning operations appended to its graph. |
| RAISES | DESCRIPTION |
|---|---|
ImportError
|
If the 'dask' library is not installed. |
TypeError
|
If the input is not a dask.dataframe.DataFrame. |