Skip to main content

SNU_DHC Package

Project description

snu_dhc.tables

DemoTable generates baseline characteristic tables for medical studies and exports them to Excel. It supports continuous and categorical variables, grouping, and automatic p-value calculation.

Parameters:

  • df: Input pandas DataFrame.
  • table_name: Title for the Excel sheet.
  • variables: List of dictionaries defining the variables to display.
  • group_variable: Optional column to group comparisons.
  • group_labels: Display labels for group values.
  • show_total: Whether to show the total column.
  • show_missing: Whether to show missing counts for categorical variables.
  • percent_decimals: Decimal places for percentages.
  • thousands_sep: Use comma separator for each thousand numbers.
  • show_p_values: Whether to include a p-value column.
  • p_value_decimals: Fixed or automatic formatting for p-values.

Test selection logic (for p-values):

  • Continuous (mean):
    • 2 groups: Welch's t-test (with checks and warnings for normality)
    • 3+ groups: ANOVA (with checks and warnings for normality and variance)
  • Continuous (median):
    • 2 groups: Mann-Whitney U test
    • 3+ groups: Kruskal-Wallis test
  • Categorical:
    • Chi-square test by default
    • Fisher's exact test if 2x2 and <5 cell counts

Usage Example

from snu_dhc.tables import DemoTable

variables_config = [
    {"var": "age", "name": "Age", "type": "continuous", "stat": "median", "decimals": 0},
    {"var": "sex", "name": "Sex", "type": "categorical", "class_labels": {1: "Male", 0: "Female"}},
    {"var": "dm", "name": "Diabetes mellitus", "type": "categorical", "class_labels": {1: ""}},
    {"var": "init_rhythm", "name": "Initial rhythm", "type": "categorical", "class_labels": {1: "VF/VT", 2: "PEA", 3: "Asystole"}},
    {"var": "rti", "name": "RTI", "type": "continuous", "stat": "median", "decimals": 0},
]

table = DemoTable(
    df=ohca_group,
    table_name="Table 1. Baseline characteristics of study patients",
    variables=variables_config,
    group_variable="group",
    group_labels={"train": "Train", "val": "Validation", "test": "Test"}, 
    show_total=True,
    show_missing=True,
    percent_decimals=1,
    thousands_sep=True,
    show_p_values=True,
    p_value_decimals="auto"
)

table.save("table1.xlsx")

snu_dhc.statistics

This module includes tools to evaluate binary classifiers with confidence intervals and visualization utilities.

binary_outcome_analysis(true, **probs)

  • true: Array-like of true binary labels.
  • **probs: Dictionary of model names and their predicted probabilities. Methods:
    • .auc(): Print AUC and 95% CI for each model.
    • .roc_auc(**styles): Plot ROC curves and display AUC values. Customize plot style per model.
    • .classification(**cutoffs): Print classification metrics using thresholds like fixed float, 'youden', 'sen90', 'spe80', etc.

Example: binary_outcome_analysis

from snu_dhc.statistics import binary_outcome_analysis

probs = {
    'XGBoost': prob0,
    'Random Forest': prob1,
    'Logistic Regression': prob2
}
result = binary_outcome_analysis(true, **probs)

# Plot ROC curves
styles = {
    'XGBoost': {'color': 'C0', 'linestyle': '-'},
    'Random Forest': {'color': 'C1', 'linestyle': '-.'},
    'Logistic Regression': {'color': 'C2', 'linestyle': '--'}
}
result.roc_auc(**styles)

# Show classification metrics with thresholds
cutoffs = {
    'XGBoost': 0.5,
    'Random Forest': 'sen90',
    'Logistic Regression': 'spe80'
}
result.classification(**cutoffs)

auc_score(true, prob, method='delong', alpha=0.95, n_iterations=1000, show=False)

  • Get AUC and 95% CI using either DeLong's method or bootstrap. method can be 'delong' or 'bootstrap'.

Returns: [auc, [ci_lower, ci_upper]]

classification_metrics(true, pred, method='wilson', ...)

  • Returns sensitivity, specificity, PPV, NPV, and F1-score with confidence intervals. method can be 'wilson' or 'bootstrap'.

Returns: Dictionary like {'sen': [...], 'spe': [...], 'ppv': [...], 'npv': [...], 'f1': [...]}

youden_metrics(true, prob, ...)

  • Computes metrics using Youden's index as the threshold.

Returns: (metrics_dict, youden_threshold)

bootstrap_ci(true, pred, fx, n_iterations=1000, alpha=0.95)

  • Computes confidence interval for any function fx using bootstrap.

Returns: [estimate, [ci_lower, ci_upper]]

overview_df(df)

  • Provides column-wise overview of a DataFrame.

Returns: DataFrame with columns for dtype, #unique, #missing, #zeros, etc.

multiclass_bootstrap_metrics(true, pred, n_iter=1000, alpha=0.95)

  • Computes sensitivity, specificity, PPV, NPV, and F1-score with 95% confidence intervals for each class using bootstrapping.

  • true: Array-like true labels.

  • pred: Array-like predicted labels (e.g., argmax of softmax outputs).

  • n_iter: Number of bootstrap iterations (default=1000).

  • alpha: Confidence level (default=0.95).

Returns: Dictionary of the form:

{
  0: {'sen': [...], 'spe': [...], 'ppv': [...], 'npv': [...], 'f1': [...]},
  1: {...},
  ...
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snu_dhc-0.1.8.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

snu_dhc-0.1.8-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file snu_dhc-0.1.8.tar.gz.

File metadata

  • Download URL: snu_dhc-0.1.8.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for snu_dhc-0.1.8.tar.gz
Algorithm Hash digest
SHA256 0c784ec36e5f25f57aa136786e892780ffae6c6a477f368f570b076a8702bb6a
MD5 69254551cabfc4eb818f9989ff462445
BLAKE2b-256 99efd26964a6536cd455c9fe50fa240d3b54b0853fa083567f2ce80b383715dc

See more details on using hashes here.

File details

Details for the file snu_dhc-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: snu_dhc-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for snu_dhc-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 5ca2c2ae5f783e13f81a5e9a2fcfc6b9e3bb5a4836da48f2ba4357931151f072
MD5 e7be31b01ff8c0efc1fd7948295aa5b0
BLAKE2b-256 c59bd1ba1afe7ffccfe2d02049e3c64baea48fc6bd6c476f577c5a7d4e2ebb90

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page