bpskit
Tools for studies on a BPS (Base Poblacional de Salud, Servicio Andaluz de Salud) health-records delivery, and for working with pseudonymised health-record extracts in a secure data environment generally.
- Ingest it. The people file into a universe table, and each diagnosis file into dated, ICD-10-harmonised events, with data-quality tables.
- Read the delivery. Pipe-delimited files read as text, sentinel codes for "unknown", month- and day-precision dates, streaming in fixed-size blocks so a 400-million-row file never sits in memory.
- ICD codes. Code specifications like
A15-A19, H35.3, ICD-10 chapters, and ICD-9 → ICD-10 mapping with the CMS GEM. That includes the V/E supplementary codes, which are also valid ICD-10 and can only be placed by the record's date relative to a changeover. - Person-time. Dates as days since 1970, age bands, Lexis splitting, person-sorted streaming.
- Exact tests. Poisson rate ratios (Garwood intervals), binomial tests, two-group rate contrasts with exact intervals, BH and Bonferroni.
- Indirect standardisation. Person-time and expected counts by sex, age, calendar year and prior-year contact intensity; within-person record pairs.
- An output check for leaving the secure environment.
bpskit.transferrefuses any file that contains an identifier the workflow read, or a per-person column, and fails closed when it cannot read something. - A synthetic delivery with BPS column names and planted structure, for tests and examples.
Installing
pip install bpskit
In a pixi project (pixi.toml):
[pypi-dependencies]
bpskit = "==0.4.0"
In a Snakemake rule environment (workflow/envs/*.yaml):
dependencies:
- pip
- pip:
- bpskit==0.4.0
Pin a release. Bumping the tag changes the environment file, and Snakemake's
software-env rerun trigger reruns the rules that use it. A module imported
from a sibling folder gets no such tracking.
A worked example
examples/quickstart.py runs in a few seconds with no
real data:
- it writes a 5,000-person synthetic delivery;
- it measures the incidence of type 2 diabetes (E11) in primary care in 2018-2025;
- it compares women with the whole population;
- it runs the transfer check on the result.
The core of it:
from bpskit import transfer
from bpskit.delivery import normalise_code, parse_date, read_delivery, stream_csv
from bpskit.icd import expand_spec, prefix_mask
from bpskit.persontime import day_of, follow_up, to_days
from bpskit.stats import poisson_rr
from bpskit.synthetic import make_delivery
cfg = make_delivery("work/delivery", n_persons=5_000) # files + how to read them
# people: birth and death as days since 1970 (NaN = alive)
u = cfg["universe"]
people = read_delivery(f"work/delivery/{u['file']}", [u["id"], u["sex"], u["birth"], u["death"]])
birth = to_days(parse_date(people[u["birth"]], u["date_format"], ["", "-1"], mid_month_day=15))
# first E11 record per person, streamed block by block
ap = cfg["sources"]["ap"]
for block in stream_csv(
f"work/delivery/{ap['file']}", [ap["id"], ap["date"], ap["code"]], 16, "utf-8"
):
hits = block[prefix_mask(normalise_code(block[ap["code"]]), expand_spec("E11"))]
...
# follow-up, then observed vs expected with an exact interval
entry, exit_, in_period = follow_up(birth, death, day_of("2018-01-01"), day_of("2025-12-31"))
ratio, lo, hi, p = poisson_rr(observed, expected, alpha=0.05)
# nothing leaves the machine that names a patient
n, problems = transfer.check(
["work/results/out"],
ids=set(people[u["id"]]),
strict=True,
forbidden=transfer.forbidden_columns(cfg),
)
E11 incidence 1,258 per 100,000 person-years; women vs all: 1.12 (0.98-1.27)
aggregate table: 1 file checked, 0 problem(s)
with a leaked file:
.../first_cases.csv: contains 3 patient identifier(s)
.../first_cases.csv: per-person or identifier column(s) ['patient']
The transfer check
The rule it enforces: no row carrying a patient identifier leaves the machine
that holds the delivery. It checks the bytes of every file against the
identifiers the workflow actually read, not against a guessed id format. A
workflow writes those identifiers with transfer.write_ids:
results/work/ids/universe.parquet: every id in the people file, written before anyone is dropped;results/work/ids/<source>.parquet: ids in a diagnosis file but not in the people file.
Where an id file is missing (a run that died early), the check reads the
delivery file instead. An unreadable file, an unknown suffix or an empty id set
all refuse. With strict=True (for a results folder copied off wholesale),
only csv/txt/png pass. CSVs whose header holds a per-person key, one of the
delivery's id or date columns, or an identifier-like name are refused too.
From a workflow directory on the data machine:
bpskit pullable --strict results/out # exit 0 only if results/out/ may leave
bpskit check-logs . # list the logs that may leave
bpskit synthetic data/synthetic --persons 2000
pullable and check-logs read the workflow's config/config.yaml
(--config to change it). Its universe and sources sections, in the shape
bpskit.synthetic.config() returns, name the id columns.
Developing
pixi run test # pytest
pixi run lint # ruff check + format check
pixi run example # the worked example
pixi run build # sdist + wheel, twine check
Releasing: commit messages are conventional commits
(fix: bumps the patch, feat: the minor version). On every push to main,
release-please keeps a release PR open with the next version, its
CHANGELOG.md entry and the bumped __version__ and CITATION.cff. Merging
it tags vX.Y.Z, and .github/workflows/release.yml then tests, builds and
publishes that tag to PyPI by trusted publishing.
Citing
See CITATION.cff. MIT licensed.
Metadata
Release files for bpskit 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| bpskit-0.5.0.tar.gz | 38.3 kB | With provenance | Details for bpskit-0.5.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bpskit-0.5.0-py3-none-any.whl | Python 3 | none | any | Details for bpskit-0.5.0-py3-none-any.whl |
Total release size: 75.7 kB
Release files / bpskit-0.5.0.tar.gz
| Download URL | bpskit-0.5.0.tar.gz |
|---|---|
| Size | 38.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
052ba3e91808d5706f2506490161d1f0bcc13ebaceb2782cabdddf9374ffb253
|
|
BLAKE2b-256 checksum How to use checksums |
5ffd93e25b6b38294d9b5b3edef360d29e7a31b419bfcf9d8668f2a61fc72230
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logRelease files / bpskit-0.5.0-py3-none-any.whl
| Download URL | bpskit-0.5.0-py3-none-any.whl |
|---|---|
| Size | 37.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6d74a4ed323156cb1be6defaa31202c9f5ea541209686145ee54f4f8fc8230ce
|
|
BLAKE2b-256 checksum How to use checksums |
f7c6f7d2a24c92b36108a8c928f0183b9e8c36573d4e47c434add863889dacd2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logSecurity for bpskit 0.5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for bpskit-0.5.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.