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 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
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, orbetweenstrategies - 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)
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
}
}]
)
Requirements
- Python 3.10+
- pandas >= 2.2
- pydicom (for DICOM utilities)
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bea_tools-0.0.5.tar.gz.
File metadata
- Download URL: bea_tools-0.0.5.tar.gz
- Upload date:
- Size: 24.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2654547daf5692afe000373cd89faed92e0b85138a583778ff216d36e5cb9a75
|
|
| MD5 |
787125a97085dd9c85be086e4919d6d2
|
|
| BLAKE2b-256 |
2b07ab50b220cc3fc0bb12cb89d6acd4ee67bb1e049ea16179234a5ba69f0705
|
Provenance
The following attestation bundles were made for bea_tools-0.0.5.tar.gz:
Publisher:
publish.yml on beatrice-b-m/bea-tools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bea_tools-0.0.5.tar.gz -
Subject digest:
2654547daf5692afe000373cd89faed92e0b85138a583778ff216d36e5cb9a75 - Sigstore transparency entry: 814212857
- Sigstore integration time:
-
Permalink:
beatrice-b-m/bea-tools@8ea149686db85cfc7ca0fd1b09d09060717c1092 -
Branch / Tag:
refs/tags/v0.0.5 - Owner: https://github.com/beatrice-b-m
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8ea149686db85cfc7ca0fd1b09d09060717c1092 -
Trigger Event:
release
-
Statement type:
File details
Details for the file bea_tools-0.0.5-py3-none-any.whl.
File metadata
- Download URL: bea_tools-0.0.5-py3-none-any.whl
- Upload date:
- Size: 19.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a8d800a8eb0aeb32ef5d582fbc2646891b95c9506614b807b532aa5aae524a3
|
|
| MD5 |
0a69ab03aa79d7c19ae2a1bb331087c6
|
|
| BLAKE2b-256 |
77c3d6d3a940425c331d3cd9c022d44e0632ae7480c072e6ea54227de30fe155
|
Provenance
The following attestation bundles were made for bea_tools-0.0.5-py3-none-any.whl:
Publisher:
publish.yml on beatrice-b-m/bea-tools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bea_tools-0.0.5-py3-none-any.whl -
Subject digest:
0a8d800a8eb0aeb32ef5d582fbc2646891b95c9506614b807b532aa5aae524a3 - Sigstore transparency entry: 814212862
- Sigstore integration time:
-
Permalink:
beatrice-b-m/bea-tools@8ea149686db85cfc7ca0fd1b09d09060717c1092 -
Branch / Tag:
refs/tags/v0.0.5 - Owner: https://github.com/beatrice-b-m
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8ea149686db85cfc7ca0fd1b09d09060717c1092 -
Trigger Event:
release
-
Statement type: