Skip to main content

CI License: MIT

sudregex

Version: 0.1.0

A lightweight, high-throughput pipeline for regex-driven extraction with negation and false-positive pruning—built for Substance Use Disorder (SUD) research, but flexible enough for general clinical text mining.


✨ Features

  • Negation detection – Filter matches when preceded by cues (e.g., “no”, “denies”, “not”).
  • **False-positive ** – Drop matches in noisy contexts (e.g., discharge instructions, family history).
  • Substance context window – Confirm that matches occur near a user-supplied vocabulary (e.g., opioid, alcohol terms).
  • Line-break normalization – Remove literal markers (default "$+$") and collapse whitespace.
  • Batteries included – A ready-to-use “ABC” checklist for common SUD signals.
  • CLI & Python API – Use from shell scripts or notebooks.
  • Deterministic previews – Sampling uses a fixed seed for reproducible tests.

📦 Installation

# From PyPI (enable after publish)
pip install sud-regex


# From source (dev)
git clone https://github.com/quantitativenurse/sud-regex.git
cd sud-regex
python -m venv .venv && source .venv/bin/activate
pip install -U pip
pip install -e .[dev]   # installs sudregex + black, isort, flake8, pytest, etc.
---

Usage

  • For interactive usage on notebooks refer to our tutorial

Quick Start (CLI)

sudregex --help
Run extraction (CSV with commas) using the default pruning behavior:

sudregex --extract \
  --in_file path/to/notes.csv \
  --out_file path/to/results.csv \
  --checklist path/to/checklist.py \
  --termslist path/to/termslist.py \
  --terms_active alcohol_terms,opioid_terms \
  --separator , \
  --parallel --n-workers 2

Discharge-instruction pruning

By default, sudregex excludes matches that occur in discharge-instruction contexts.

  • Default: no flag needed, or explicit:
  sudregex --extract ... --exclude-discharge-mentions

Turn pruning OFF (keep discharge-context hits):

sudregex --extract \
  --in_file path/to/notes.csv \
  --out_file path/to/results_raw.csv \
  --checklist path/to/checklist.py \
  --termslist path/to/termslist.py \
  --terms_active alcohol_terms \
  --no-exclude-discharge-mentions

Use a custom separator (example: a unique token unlikely to appear in notes):

Clinical notes often contain commas, semicolons, tabs and other common punctuation marks as part of natural language. Using these as delimiters can lead to unintended splits and parsing errors, especially when extracting structured information from note text fields. In our work, we use the custom marker |^| because:

It is highly unlikely to appear naturally in clinical documentation. It provides a clear, unambiguous boundary between segments. It avoids conflicts with commonly used punctuation, improving extraction accuracy. It simplifies line-break normalization and downstream processing.

This choice ensures that our pipeline remains robust across diverse note formats.

sudregex --extract \
  --in_file path/to/notes.txt \
  --out_file path/to/results.csv \
  --checklist path/to/checklist.py \
  --termslist path/to/termslist.py \
  --terms_active opioid_terms \
  --separator $'|^|'    # or any safe custom delimiter

Quickstart (Python API)

import sudregex as sud

# Use the packaged defaults if desired
checklist = sud.checklist_abc
terms = sud.default_termslist

# DataFrame API
df_results = sud.extract_df(
    df=my_notes_df,                  # columns: note_id, note_text (and optional grid)
    checklist=checklist,
    termslist=terms,
    terms_active="alcohol_terms,opioid_terms",
    parallel=True,                   # <— enable parallel apply (if pandarallel is installed)
    n_workers=2,                     
    include_note_text=False,
    exclude_discharge_mentions=True, # default True; set False to disable pruning
)

# File API (CSV/TSV/…)
result = sud.extract(
    in_file="notes.csv",
    out_file="results.csv",
    checklist="path/to/checklist.py",
    separator=",",
    termslist="path/to/termslist.py",
    terms_active="opioid_terms",
    parallel=True,
    n_workers=2,                      
    include_note_text=False,
    exclude_discharge_mentions=False, # keep raw matches even in discharge contexts
)

The default checklist and termslist are available using the below method.

checklist = sud.checklist_abc

checklist

termslist = sud.default_termslist

termslist


License

MIT – see LICENSE for details.

📣 Citation / Acknowledgements

If sudregex is useful in your work, please cite:

Quantitative Nurse Lab. (2025). sudregex (Version 0.1.0). GitHub. https://github.com/quantitativenurse/sud-regex

Acknowledgements:
Thanks to all contributors and collaborators for feedback and testing.

Metadata

Release files for sudregex 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sudregex 0.1.0
File Size Uploaded Provenance Actions
sudregex-0.1.0.tar.gz 24.5 kB Without provenance Details for sudregex-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sudregex 0.1.0
File Interpreter ABI Platform Actions
sudregex-0.1.0-py3-none-any.whl Python 3 none any Details for sudregex-0.1.0-py3-none-any.whl

Total release size: 47.8 kB

Release files / sudregex-0.1.0.tar.gz

Download URL sudregex-0.1.0.tar.gz
Size 24.5 kB
Tags Source
SHA-256 checksum
How to use checksums
900068a4bbbe17c9344c5a2d4fd64db512f6593653c2a946077f1bc3ef1fbe73
BLAKE2b-256 checksum
How to use checksums
1c438b87c3215c056d2ece37e79dbb21d483c98c8f3057ede5c36589d62c9a4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.4

Release files / sudregex-0.1.0-py3-none-any.whl

Download URL sudregex-0.1.0-py3-none-any.whl
Size 23.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c62585b95b4733b06ed94cbc76c1b68c9475ce7871045d590d4d71c65f121e6f
BLAKE2b-256 checksum
How to use checksums
e45cc6a69329ed2fae45b8302eb1584a0fe289ac3054cc0575a17d71ee4e6453
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.4

Security for sudregex 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page