Skip to main content

Tools made by, and for, Bea - a Python toolkit for data sampling and analysis workflows

Project description

bea-tools

🐝 𝓉𝑜𝑜𝓁𝓈 𝓂𝒶𝒹𝑒 𝒷𝓎, 𝒶𝓃𝒹 𝒻𝑜𝓇, 𝒷𝑒𝒶 🐝

A Python package of random functions and tools that I use regularly. Data science/analysis focused since I'm a data scientist c:

Installation

pip install bea-tools

Features

Utility Functions

Helper functions for formatting and displaying output in a clean, readable way.

divider()

Creates formatted divider lines with optional text alignment.

from bea_tools.utility import divider

# Simple divider
print(divider(line_width=50))  # --------------------...

# Divider with centered text
print(divider("Section Header", "=", line_width=50, align="center"))
# ============ Section Header ============

# Divider with left-aligned text
print(divider("Results", "-", align="left"))
# Results -------------------------

aligned()

Formats items within a frame, which is then aligned within a line. Perfect for creating clean output displays.

from bea_tools.utility import aligned

# Center two items within a frame
print(aligned("Label:", "Value", frame_width=30, line_width=50))

# Multiple items distributed across frame
print(aligned("A", "B", "C", frame_width=40))

Pandas Extensions

Series.bea.value_counts()

Enhanced value counts with custom sorting, normalization, and formatted string output.

import pandas as pd

df = pd.DataFrame({'category': ['A', 'B', 'A', 'C', 'B', 'A']})

# Get counts with proportions as formatted strings
counts = df['category'].bea.value_counts(with_proportion=True, output=True)
# Returns: {'A': '3 (50.0%)', 'B': '2 (33.3%)', 'C': '1 (16.7%)'}

# Custom sort order
counts = df['category'].bea.value_counts(sort=['A', 'B', 'C'], output=True)

# Display directly (Jupyter-friendly)
df['category'].bea.value_counts(with_proportion=True)

TreeSampler

A hierarchical stratified sampling tool for pandas DataFrames. Designed for scenarios where you need to sample data while maintaining specific proportions across multiple categorical dimensions, with intelligent handling of capacity constraints.

Key capabilities:

  • Multi-dimensional stratification: Define sampling targets across multiple features (e.g., gender, age group, category)
  • Hierarchical spillover: When a stratum lacks sufficient data, excess quota automatically redistributes to sibling strata
  • Flexible matching: Match values using equals, contains, or between strategies
  • Conditional weights: Define weights that vary based on the path through the sampling tree
  • Strict mode: Lock specific strata to prevent them from absorbing spillover
  • Balanced sampling: Equal distribution across levels regardless of population proportions
  • Single-per-entity sampling: Ensure unique entities (e.g., one exam per patient) with optional sorting control

DicomComparer

A tool for comparing two DICOM files at the attribute level. Identifies which attributes are shared between files, which are exclusive to each, and whether shared attributes have matching or conflicting values.

Key capabilities:

  • Attribute overlap analysis: Identify which DICOM tags exist in both files vs. exclusive to one
  • Value comparison: For shared attributes, detect matches and conflicts
  • Flexible input: Pass files directly or as a labeled dictionary
  • Summary output: Generate formatted comparison reports
import pydicom
from bea_tools._dicom.dicomp import DicomComparer

dcm1 = pydicom.dcmread("path/to/first.dcm")
dcm2 = pydicom.dcmread("path/to/second.dcm")

comparer = DicomComparer(dcm1, dcm2)
comparison = comparer.compare()

# Print summary statistics
comparison.summary()

# Access specific conflicts
for conflict in comparison.intersection.comparison.conflicts:
    print(conflict)

Quick Start

from bea_tools import TreeSampler
from bea_tools._pandas.sampler import Feature

import pandas as pd

# Sample data
df = pd.DataFrame({
    'patient_id': ['P001', 'P002', 'P003', 'P004', 'P005', 'P006'],
    'gender': ['M', 'M', 'F', 'F', 'M', 'F'],
    'age': [25, 45, 35, 55, 30, 40],
    'studydate_anon': pd.date_range('2020-01-01', periods=6)
})

# Define stratification features
features = [
    Feature(
        name='gender',
        match_type='equals',
        levels=['M', 'F'],
        weights=[0.5, 0.5]  # Target 50/50 split
    )
]

# Create sampler and extract stratified sample
sampler = TreeSampler(
    n=4,                          # Target sample size
    features=features,
    seed=42,                      # For reproducibility
    count_col='patient_id',       # Column for unique entity identification
    single_per_patient=True       # One row per patient
)

result = sampler.sample_data(df)

Advanced Usage

Age Brackets with Between Matching

age_feature = Feature(
    name='age',
    match_type='between',
    levels=[(0, 30), (30, 50), (50, 100)],
    weights=[0.3, 0.4, 0.3],
    labels=['Young', 'Middle', 'Senior'],
    label_col='age_group'
)

Strict Strata (No Spillover)

# This stratum will maintain exact proportions, never absorbing excess
feature = Feature(
    name='category',
    match_type='equals',
    levels=['A', 'B'],
    weights=[0.7, 0.3],
    strict=True  # Prevents spillover absorption
)

Conditional Weights

Define weights that depend on parent feature values:

category_feature = Feature(
    name='category',
    match_type='equals',
    levels=['X', 'Y'],
    conditional_weights=[{
        'feature': 'gender',
        'weights': {
            'M': [0.6, 0.4],  # When gender=M: 60% X, 40% Y
            'F': [0.4, 0.6]   # When gender=F: 40% X, 60% Y
        }
    }]
)

Balanced Sampling

Ensure equal representation across levels, ignoring the underlying population distribution:

# This feature will have exactly equal samples from each level
feature = Feature(
    name='modality',
    match_type='equals',
    levels=['CT', 'MRI', 'Xray', 'US'],
    balanced=True  # Distributes samples equally across all 4 levels
)

Optional Sorting for Single-Per-Patient

Control whether to use a sort column when selecting one row per patient:

# With sorting (e.g., earliest study date per patient)
sampler = TreeSampler(
    n=100,
    features=features,
    sort_col='studydate_anon',  # Default
    single_per_patient=True
)

# Without sorting (arbitrary selection, faster)
sampler = TreeSampler(
    n=100,
    features=features,
    sort_col=None,  # Disables sorting
    single_per_patient=True
)

Requirements

  • Python 3.10+
  • pandas >= 2.2
  • pydicom (for DICOM utilities)

License

MIT

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

bea_tools-0.0.7.tar.gz (36.9 kB view details)

Uploaded Source

Built Distribution

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

bea_tools-0.0.7-py3-none-any.whl (27.8 kB view details)

Uploaded Python 3

File details

Details for the file bea_tools-0.0.7.tar.gz.

File metadata

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

File hashes

Hashes for bea_tools-0.0.7.tar.gz
Algorithm Hash digest
SHA256 5e6887cefbdf7cef247e21752d3ca4bf23b7529638e914a730b5e378217beb80
MD5 f76b7e49d1fbdfec8fac7b136d0ec68b
BLAKE2b-256 c5db05729021fbed722b94c736bc0cca9116f9c5090dba55ff20bb4f8cf66fb7

See more details on using hashes here.

Provenance

The following attestation bundles were made for bea_tools-0.0.7.tar.gz:

Publisher: publish.yml on beatrice-b-m/bea-tools

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

File details

Details for the file bea_tools-0.0.7-py3-none-any.whl.

File metadata

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

File hashes

Hashes for bea_tools-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 2b2269ab39eca1a9af5ac778c2bc0824c7ff3a3ec7f5bf73d3ce6f0cf5c8819d
MD5 9c2ef9d718aed4e56b3794eb006ac9c8
BLAKE2b-256 21f7ee4894265d57d080f8c121a06d6546b10825e0edbeedcfbb0f90a2a806ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for bea_tools-0.0.7-py3-none-any.whl:

Publisher: publish.yml on beatrice-b-m/bea-tools

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