Skip to main content

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

Project description

bea-tools

Tools made by, and for, Bea

A Python toolkit for data sampling and analysis workflows.

Installation

pip install bea-tools

Features

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
  • Single-per-entity sampling: Ensure unique entities (e.g., one exam per patient)

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
        }
    }]
)

Requirements

  • Python 3.10+
  • pandas >= 2.2

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.4.tar.gz (13.3 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.4-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: bea_tools-0.0.4.tar.gz
  • Upload date:
  • Size: 13.3 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.4.tar.gz
Algorithm Hash digest
SHA256 e09f49834698d0b603f9ca117e6dea0528e2cba8ea38629672c34f32df0a5328
MD5 d0f651ee999b9e0997dd0e8046d41143
BLAKE2b-256 dbe9404e14f3cdfed73b49e3a560e413097b89cdde20f5b3a1ccdb25fb3a0c4e

See more details on using hashes here.

Provenance

The following attestation bundles were made for bea_tools-0.0.4.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.4-py3-none-any.whl.

File metadata

  • Download URL: bea_tools-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 12.5 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 de3ef00c73e85db8ba498d02ecb98b2a8ec6471d952b5475e23f30100387c54b
MD5 c651ecd02b2c0197fd8458db1ac54239
BLAKE2b-256 d422862b3b9c5b7dfd1f08dcee8a18d7c9e196fa413c726471e14dbda58cf0a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for bea_tools-0.0.4-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