BaseTable¶
clifpy.tables.base_table.BaseTable
¶
Base class for all pyCLIF table classes.
Provides common functionality for loading data, running validations, and generating reports. All table-specific classes should inherit from this.
Attributes:
| Name | Type | Description |
|---|---|---|
data_directory |
str
|
Path to the directory containing data files |
filetype |
str
|
Type of data file (csv, parquet, etc.) |
timezone |
str
|
Timezone for datetime columns |
output_directory |
str
|
Directory for saving output files and logs |
table_name |
str
|
Name of the table (from class name) |
df |
DataFrame
|
The loaded data |
schema |
dict
|
The YAML schema for this table |
errors |
List[dict]
|
Validation errors from last validation run |
logger |
Logger
|
Logger for this table |
Initialize the BaseTable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_directory
|
str
|
Path to the directory containing data files |
required |
filetype
|
str
|
Type of data file (csv, parquet, etc.) |
required |
timezone
|
str
|
Timezone for datetime columns |
required |
output_directory
|
str
|
Directory for saving output files and logs. If not provided, creates an 'output' directory in the current working directory. |
None
|
data
|
DataFrame
|
Pre-loaded data to use instead of loading from file |
None
|
Source code in clifpy/tables/base_table.py
analyze_categorical_distributions
¶
Analyze distributions of categorical variables.
For each categorical variable, returns the distribution of categories based on unique hospitalization_id (or patient_id if hospitalization_id is not present).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save
|
bool
|
If True, saves distribution data to CSV files in the output directory. |
True
|
Returns:
| Type | Description |
|---|---|
Dict[str, DataFrame]
|
Dictionary where keys are categorical column names and values are DataFrames with category distributions (unique ID counts and %). |
Source code in clifpy/tables/base_table.py
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 | |
calculate_stratified_ecdf
¶
Calculate ECDF for a continuous variable stratified by categories using loaded DataFrame (self.df).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value_column
|
str
|
Name of the continuous/numeric column to calculate ECDF for. |
required |
category_column
|
str
|
Name of the categorical column to stratify by. |
required |
category_values
|
List[str]
|
Specific category values to include. If None, uses permissible values from schema, or all unique values in the data if schema doesn't specify permissible values. |
None
|
save
|
bool
|
If True, saves stratified ECDF data to CSV files (one per category). |
True
|
Returns:
| Type | Description |
|---|---|
List[DataFrame] or None
|
List of DataFrames (one per category), each with x-values and their corresponding cumulative probabilities. If save=True, saves the resulting DataFrame to CSV. |
Source code in clifpy/tables/base_table.py
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 | |
from_file
classmethod
¶
from_file(data_directory=None, filetype=None, timezone=None, config_path=None, output_directory=None, sample_size=None, columns=None, filters=None, verbose=False)
Load data from file and create a table instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_directory
|
str
|
Path to the directory containing data files |
None
|
filetype
|
str
|
Type of data file (csv, parquet, etc.) |
None
|
timezone
|
str
|
Timezone for datetime columns |
None
|
config_path
|
str
|
Path to configuration JSON file |
None
|
output_directory
|
str
|
Directory for saving output files and logs |
None
|
sample_size
|
int
|
Number of rows to load |
None
|
columns
|
List[str]
|
Specific columns to load |
None
|
filters
|
Dict
|
Filters to apply when loading |
None
|
verbose
|
bool
|
If True, show detailed loading messages. Default is False |
False
|
Notes
Loading priority: 1. If all required params provided → use them 2. If config_path provided → load from that path, allow param overrides 3. If no params and no config_path → auto-detect config.json 4. Parameters override config file values when both are provided
Returns:
| Type | Description |
|---|---|
BaseTable
|
Instance of the table class with loaded data |
Source code in clifpy/tables/base_table.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
get_summary
¶
Get a summary of the table data.
Returns: dict: Summary statistics and information about the table
Source code in clifpy/tables/base_table.py
isvalid
¶
Check if the data is valid based on the last validation run.
Returns: bool: True if validation has been run and no errors were found, False if validation found errors or hasn't been run yet
Source code in clifpy/tables/base_table.py
plot_categorical_distributions
¶
Create bar plots for categorical variable distributions.
Counts unique hospitalization_id (or patient_id if hospitalization_id is not present) for each category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
List[str]
|
Specific categorical columns to plot. If None, plots all categorical columns. |
None
|
figsize
|
Tuple[int, int]
|
Figure size for each plot (width, height). |
(10, 6)
|
save
|
bool
|
If True, saves plots to output directory as PNG files. |
True
|
dpi
|
int
|
Resolution for saved plots (dots per inch). |
300
|
Returns:
| Type | Description |
|---|---|
Dict[str, Figure]
|
Dictionary where keys are categorical column names and values are matplotlib Figure objects. |
Source code in clifpy/tables/base_table.py
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 688 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 | |
save_summary
¶
Save table summary to a JSON file.
Source code in clifpy/tables/base_table.py
validate
¶
Run comprehensive validation on the data.
This method runs all validation checks including:
- Schema validation (required columns, data types, categories)
- Missing data analysis
- Duplicate checking
- Statistical analysis
- Table-specific validations (if overridden in child class)