Skip to main content

Compress clinical text without losing (or flipping) clinical meaning.

Project description

clinicalcompress

Compress clinical text without flipping a negation.

"Patient denies chest pain."  →  a naive compressor drops "denies"  →  "Patient chest pain."

That single dropped word turns a normal finding into a medical emergency. clinicalcompress is a deterministic-first Python library that compresses clinical text for LLM context windows, storage, or transmission — while guaranteeing that negations, values, allergies, laterality, and temporal status markers can never be silently lost or reversed.

No API key required. The protection and verification layers are fully deterministic; LLM-assisted compression is an optional add-on.

License: Apache 2.0 Python 3.9+

Install

pip install clinicalcompress

Quickstart

from clinicalcompress import compress

note = "Patient denies chest pain. No history of MI. BP 120/80. Allergic to penicillin."
result = compress(note, target_reduction=0.3)

print(result.compressed_text)
print(result.safety.all_passed)   # True — nothing clinically load-bearing was lost
print(result.reduction_pct)       # best-effort token reduction, %

Run the built-in demo (no API key needed):

python examples/quickstart.py

It compresses a sample note, then deliberately shows a naive, unsafe compression dropping a negation — and verify() catching the flip.

The five protected categories

clinicalcompress never drops or alters text in these categories, no matter how aggressive the compression target is:

Category Example
Negation "denies", "no", "ruled out", "without"
Value "BP 120/80", "HR 88 bpm", "10 mg"
Allergy "Allergic to penicillin", "NKDA"
Laterality "left knee", "bilateral", "R) shoulder"
Temporal "history of", "chronic", "resolved"

The verification guarantee

This is the whole point of the library. compress(text, strict=True) (the default) runs deterministic safety checks after every compression:

  • negation_preserved — every negation cue in the source is still present and still adjacent to its governed clinical term.
  • values_intact — every numeric value + unit is present, unchanged.
  • allergies_preserved — every allergy statement is present.
  • laterality_preserved — every left/right/bilateral marker is present with its referent.
  • no_meaning_reversal — no term that was negated in the source appears as an unqualified positive assertion in the output.

If any check fails, the orchestrator reverts the affected content to the source text and re-verifies. With strict=True, the result can never contain a lost negation or a reversed clinical meaning. This is a guarantee the test suite enforces directly (see tests/test_verify.py and tests/test_api.py).

Architecture

protect()  →  compress_deterministic() [+ optional compress_llm()]  →  verify()  →  (revert if unsafe)
  • Protect (deterministic): scan the source text and mark every clinically load-bearing span before any compression happens.
  • Compress (deterministic by default): remove redundancy — repeated whitespace, duplicate sentences, boilerplate filler, low-information sentences — from everything outside protected spans. LLM-assisted compression is optional, only ever touches unprotected spans, and silently no-ops without an API key.
  • Verify (deterministic): confirm every protected span's meaning survived. Revert on any failure.

CLI reference

# Compress a note and print the safety report
clinicalcompress run --file note.txt --reduction 0.4

# Show what would be protected, without compressing (good for demos)
clinicalcompress check --file note.txt

# Optionally enable LLM-assisted compression of unprotected spans
clinicalcompress run --file note.txt --use-llm

API example

from clinicalcompress import compress
from clinicalcompress.protect import protect

text = "Denies fever. History of asthma. Left knee pain. BP 118/76."

spans = protect(text)
for span in spans:
    print(span.category, span.text, span.governs)

result = compress(text, target_reduction=0.4, use_llm=False, strict=True)
print(result.compressed_text)
print(result.safety.checks)

Disclaimer

clinicalcompress is a text-compression utility, not a medical device. It is not clinically validated and must not be used as the sole basis for clinical decisions. Always review compressed output before use in any workflow that affects patient care.

Live demo

A hosted demo showing the safe compression side-by-side against a deliberately unsafe naive baseline lives at versionone.health/tools/clinicalcompress. The same UI can be run locally — see webui/README.md.

Contributing

See CONTRIBUTING.md for how to add a new protected category, add a detector, and the project's no-PHI rule.

License

Apache 2.0. See LICENSE. Copyright 2026 Anurag Chatterjee.

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

clinicalcompress-0.1.1.tar.gz (27.2 kB view details)

Uploaded Source

Built Distribution

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

clinicalcompress-0.1.1-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file clinicalcompress-0.1.1.tar.gz.

File metadata

  • Download URL: clinicalcompress-0.1.1.tar.gz
  • Upload date:
  • Size: 27.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for clinicalcompress-0.1.1.tar.gz
Algorithm Hash digest
SHA256 49d66561c45ddac7154c8c1fd18a849d59430b414f37a71f2d42eff141f56eea
MD5 0eeef12be1b3f2b5e3c86919ce3d6456
BLAKE2b-256 d7592b030a4b1fe7834321f7de365252c28978d4cbfa9c81b2ddfe6e142a2e8c

See more details on using hashes here.

Provenance

The following attestation bundles were made for clinicalcompress-0.1.1.tar.gz:

Publisher: publish.yml on VersionOne-Health/clinicalcompress

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

File details

Details for the file clinicalcompress-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for clinicalcompress-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 baff2849b6456d5a084e54c3145aadf8772a5ef84b597357d56f20e26860f0a0
MD5 257a413f44b74a31c3a1647a38f43d8a
BLAKE2b-256 9b18a6c72c732542c22e899c5ee1b5ffc429a6b5c7a4524d5b04fd06f9142cd4

See more details on using hashes here.

Provenance

The following attestation bundles were made for clinicalcompress-0.1.1-py3-none-any.whl:

Publisher: publish.yml on VersionOne-Health/clinicalcompress

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