Skip to main content

A package for survival analysis with body composition analysis data

Project description

CI Documentation PyPI Python Version

Survival Analysis Package

A Python package for analyzing survival data with a focus on body composition assessment. It was designed to utilize the results obtained by the BOA - Body and Organ Analysis workflow. In this repository we provide tools to reorganize the result of this algorithm to merge it to the patient table, add tools for data cleaning and a lifelines wrapper for automatical explorative anaylsis on survival outcomes given the Body-Composition results.

Features

  • Survival Analysis: Cox proportional hazards regression and Kaplan-Meier survival curves
  • Body Composition Analysis: Tools for processing and analyzing BCA data
  • BOA Extractor: Command-line tool for extracting measurements from BOA data
  • Data Preprocessing: Utilities for cleaning and preparing survival data
  • CLI Tools: Command-line utilities for data merging, format conversion, and PDF encryption

Installation

pip install bca-survival

Usage

Basic Survival Analysis

from bca_survival.analyzer import BCASurvivalAnalyzer

# Load your data, sharing the same identifiers
df_main = pd.read_csv('clinical_data.csv')
df_measurements = pd.read_csv('bca_measurements.csv')

# Initialize the analyzer
analyzer = BCASurvivalAnalyzer(
    df_main, df_measurements,
    main_id_col='patient_id', measurement_id_col='id',
    start_date_col='diagnosis_date', event_date_col='event_date', event_col='event_status'
)

# Perform univariate analysis
columns = ['l5::WL::imat::mean_ml', 'l5::WL::tat::mean_ml', 'age', 'gender']
results = analyzer.univariate_cox_regression(columns)

# Generate Kaplan-Meier plot
analyzer.kaplan_meier_plot('l5::WL::imat::mean_ml', split_strategy='median')

# Perform multivariate analysis
model = analyzer.multivariate_cox_regression(columns)

Command-Line Tools

The package includes several command-line tools for common data processing tasks:

BOA Extractor

Extract measurements from BOA (Body Composition Assessment) data:

boa-extract /path/to/data /path/to/output

Purpose: Processes BOA data files and extracts relevant measurements for survival analysis.

Arguments:

  • data_path: Path to the directory containing BOA data files
  • output_path: Path where extracted measurements will be saved

BCA Merger

Merge two Excel files based on ID columns:

bca-merge <first_file> <second_file> <id_column_name>

Purpose: Combines clinical data with body composition measurements by matching on ID columns.

Arguments:

  • first_file: Path to the first Excel file (e.g., clinical data)
  • second_file: Path to the second Excel file (e.g., BCA measurements)
  • id_column_name: Name of the ID column in the first file to match with 'StudyID' in the second file

Example:

bca-merge clinical_data.xlsx bca_measurements.xlsx patient_id

Output: Creates a file named {first_file}_merged.xlsx with:

  • All rows from both files (outer join)
  • Matched records combined into single rows
  • Date columns formatted as DD.MM.YYYY
  • No duplicate StudyID columns

Notes:

  • The second file must have a column named 'StudyID'
  • Uses outer merge to preserve all data from both files
  • Automatically removes duplicate ID columns

Survival Result Converter

Convert Excel files to multiple formats (PDF, CSV, TXT):

survival-result-converter [directory]

Purpose: Batch converts Excel files to multiple formats for reporting and data sharing.

Arguments:

  • directory: Directory to scan for Excel files (default: current directory)

Example:

# Convert all Excel files in current directory
survival-result-converter

# Convert Excel files in specific directory
survival-result-converter /path/to/results

Output Structure:

directory/
├── PDF/
│   ├── file1.pdf
│   └── file2.pdf
├── CSV/
│   ├── file1.csv
│   ├── file2_sheet1.csv
│   └── file2_sheet2.csv
└── TXT/
    ├── file1.txt
    └── file2.txt

Features:

  • Recursively processes all .xlsx files in the directory tree
  • Creates separate output folders (PDF, CSV, TXT)
  • For multi-sheet Excel files:
    • PDF: All sheets in single file
    • CSV: Separate file per sheet
    • TXT: All sheets in single file with separators
  • PDF generation supports two methods:
    • Windows: Uses COM automation for high-quality output
    • Cross-platform: Uses fpdf library with automatic column sizing

PDF Features:

  • Landscape orientation for better table visibility
  • Automatic column width adjustment
  • Fits tables to page width
  • Handles large tables (up to 1000 rows per sheet)
  • Text wrapping for long content

PDF Report Extractor

Encrypt and organize PDF files from a directory tree:

pdf-report-extractor <input_path> <output_path> <password>

Purpose: Finds PDF files in a directory structure, copies them with standardized names, and encrypts them for secure distribution.

Arguments:

  • input_path: Root directory to search for PDF files
  • output_path: Destination directory for encrypted PDFs
  • password: Password to encrypt the PDFs with

Example:

pdf-report-extractor /data/patient_reports /encrypted_reports MySecureP@ss123

Behavior:

  • Recursively searches for all .pdf files
  • Copies files to destination with naming pattern: encrypted_{parent_folder_name}.pdf
  • Encrypts each file using user password protection
  • Requires pdftk to be installed

Check pdftk Installation:

pdf-report-extractor --check-pdftk

Installing pdftk:

  • Ubuntu/Debian: sudo apt-get install pdftk
  • macOS: brew install pdftk-java
  • Windows: Download from PDFtk website

Output Summary:

Processing: /data/patient_reports/folder1/report.pdf
  -> /encrypted_reports/encrypted_folder1.pdf
  -> Encrypted successfully

Processing complete:
  - Files processed successfully: 15
  - Errors: 0

Notes:

  • Original files remain unchanged
  • If encryption fails, the unencrypted copy is removed from destination
  • Parent folder name is used for output filename (one level up from the PDF)

Documentation

Refer to the documentation in the docs/ directory for detailed information:

  1. Install the package with documentation dependencies:
   pip install -e ".[docs]"
  1. Build the documentation on Windows:
   cd docs
   make.bat html

Or on Linux/macOS:

   cd docs
   make html
  1. Open docs/build/html/index.html in your browser

Development

Clone the repository and install in development mode:

git clone https://gitlab.com/your-group/survival-analysis.git
cd survival-analysis
pip install -e ".[dev]"

Requirements

Core Dependencies

  • pandas
  • openpyxl (for Excel file handling)
  • lifelines (for survival analysis)

Optional Dependencies

  • For PDF conversion (survival-result-converter):
    • Windows: pywin32
    • Cross-platform: fpdf, openpyxl
  • For PDF encryption (pdf-report-extractor):
    • pdftk (external dependency)

Common Workflows

Workflow 1: Complete Data Processing Pipeline

# 1. Merge clinical and BCA data
bca-merge clinical.xlsx measurements.xlsx PatientID

# 2. Perform survival analysis (Python)
# ... (use BCASurvivalAnalyzer)

# 3. Convert results to multiple formats
survival-result-converter ./results

# 4. Encrypt PDF reports for distribution
pdf-report-extractor ./results/PDF ./encrypted_reports SecurePassword123

Workflow 2: Quick Data Conversion

# Convert a directory of Excel results to PDF
survival-result-converter /path/to/results

# PDFs are created in /path/to/results/PDF/

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

bca_survival-0.2.6.tar.gz (42.6 kB view details)

Uploaded Source

Built Distribution

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

bca_survival-0.2.6-py3-none-any.whl (30.8 kB view details)

Uploaded Python 3

File details

Details for the file bca_survival-0.2.6.tar.gz.

File metadata

  • Download URL: bca_survival-0.2.6.tar.gz
  • Upload date:
  • Size: 42.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bca_survival-0.2.6.tar.gz
Algorithm Hash digest
SHA256 0169bcf11c5f95e270000b97942b90cfe1e3ff630a5f63dacd41b3f6cd07c569
MD5 6b9acecd6f2a38daa33ea4d207c4706f
BLAKE2b-256 9cf51450b022e5c6d7d5708164d579092e816b7fb1d37d8e949fcd1ca5f7eb86

See more details on using hashes here.

Provenance

The following attestation bundles were made for bca_survival-0.2.6.tar.gz:

Publisher: release.yml on eFroD/bca-survival-analyzer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bca_survival-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: bca_survival-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 30.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bca_survival-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 5fd7700bb71d6f19e8f1e98cf3ba261a25da5abd5af7f3ba21771172512f585f
MD5 1c4c6fecbdec4c387aae5e5901303e6b
BLAKE2b-256 ca40706f140cd24cf717bb95a5f19017f1e0f7561e6698dd6f0de7a7f4710eac

See more details on using hashes here.

Provenance

The following attestation bundles were made for bca_survival-0.2.6-py3-none-any.whl:

Publisher: release.yml on eFroD/bca-survival-analyzer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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