$ show jmschrei/tangermeme
Tangermeme is a PyTorch library for post-training interpretability of sequence-based deep learning models in regulatory genomics. It provides a comprehensive suite of functions for analyzing what a trained model has learned about cis-regulatory elements, including attribution methods (DeepLIFT/SHAP, saturation mutagenesis, PISA), sequence perturbation experiments (marginalization, ablation, variant effect, spacing analysis), motif discovery via seqlet calling (recursive and TF-MoDISco-style), and sequence design (greedy/beam search substitution). The library is designed to be assumption-free, extensible to any alphabet, and works with any PyTorch model that operates on one-hot encoded sequences. Key entry points are the `deep_lift_shap`, `predict`, `ersatz`, `marginalize`, `ablate`, `seqlet`, `annotate`, and `design` subpackages, all exposed through a consistent API that accepts a `func=` argument for composability. The library includes a built-in Claude Code skill installer for AI-assisted development, extensive unit tests with toy models, and documentation with tutorials and vignettes. Tangermeme is actively developed (semantic versioning, regular releases) and targets bioinformaticians working with models like BPNet, Enformer, Beluga, or ChromBPNet.
The LLM identified 5 files worth highlighting for reuse:
Provides `_resolve_device` for automatic CPU/CUDA fallback, `_autocast_supported` for mixed-precision compatibility detection, and `_preserve_model_state` context manager that safely stores and restores model training mode and device placement. This isolation pattern is reusable for any PyTorch inference pipeline that needs to temporarily modify model state without side effects.
Defines `PerturbationResult`, `SpaceResult`, `AttributionReferencesResult` as NamedTuples, enabling both tuple unpacking and attribute access (e.g., `y_before`, `y_after`). This pattern enforces consistent return types across perturbation functions and can be adopted in any library where multiple experimental results need a standardized, self-documenting structure.
Contains `_validate_input` that checks tensor shape, dtype, one-hot encoding correctness, and value bounds; `one_hot_encode` and `random_one_hot` for sequence tensor creation; and `reverse_complement` for DNA manipulation. The validation function is imported and used across almost all core modules, ensuring input consistency. This pattern of centralized defensive validation is reusable for any library processing one-hot encoded categorical data.
Implements fundamental sequence operations (`substitute`, `insert`, `delete`, `shuffle`, `dinucleotide_shuffle`, `randomize`) on one-hot encoded tensors. Each function validates inputs, handles batch dimensions, and returns modified tensors without side effects. The modular, composable design (each operation is independent) allows easy extraction and use in other genomic analysis projects that need in-silico perturbation of sequences.
Provides a batched prediction function that moves the model and inputs to the specified device, handles dtype casting, supports tqdm progress bars, and restores original model state after inference. The function accepts arbitrary `args` and `kwargs` passed to the model, enabling flexible usage. This pattern is directly reusable for any PyTorch model requiring memory-efficient batch inference with device management.