Skip to content
All projects
Core engineering libraryData Manager

data_utilities.py — Shared DataFrame Toolbox

BCHPR · imported by 22 pipeline scripts · 2023 – present

The dependency root of the library — 9,746 lines that import nothing else in the codebase and that everything else imports. 84 public callables covering REDCap activity-log parsing, data-dictionary recoding, checkbox column expansion, export diffing, and file I/O that survives a SharePoint sync.

Highlights

  • RedcapLogVectorOps is a third of the file and holds one rule: no row-wise Python loops, so it stays usable at production log volumes.
  • Three implementations of duplicate-filling, and a router that measures the actual duplicate ratio to choose between them — sampling was rejected in a comment as statistically biased.
  • Polars on the hot path with a pandas fallback, but deliberately reversed for compressed CSVs: single-pass decompress-and-parse holds peak memory at 1× file size where Polars would need 2×.
  • Encoding fallback ordered utf-8 → cp1252 → latin-1, because latin-1 decodes every byte sequence and would silently mojibake Windows-1252 files if it came earlier.
  • Atomic writes with a real OS file lock across platforms: hidden temp file, fsync, then os.replace.
  • Worker count capped at 4 — more parallel CSV loads means more files in RAM at once, the primary cause of OOM under WSL.
  • Log statements refuse to print row content, because the rows are medical records and scheduled-task logs retain stdout.