BaseTable¶
clifpy.tables.base_table.BaseTable
¶
BaseTable(data_directory, filetype, timezone, output_directory=None, data=None, clif_version=DEFAULT_CLIF_VERSION)
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
|
clif_version
|
str
|
CLIF schema version to validate against (e.g. "2.1", "3.0"). Defaults to the package default (2.1). |
DEFAULT_CLIF_VERSION
|
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
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 | |
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
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 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 | |
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, clif_version=None)
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
|
clif_version
|
str
|
CLIF schema version to validate against. Overrides any |
None
|
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
181 182 183 184 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 267 268 269 270 | |
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
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 | |
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)