pytfce
Fast probabilistic Threshold-Free Cluster Enhancement in Python.
Overview
pytfce is a pure-Python package for probabilistic TFCE (pTFCE) that provides analytical inference on neuroimaging statistical maps without permutation testing. It implements a baseline pTFCE faithful to the original R package (Spisák et al. 2019) and a novel hybrid eTFCE–GRF that combines union-find cluster retrieval with analytical GRF p-values. On real brain data (~2M voxels), pytfce is 73× faster than the R pTFCE package and produces concordant results.
Installation
pip install pytfce
# development install
git clone https://github.com/Don-Yin/pytfce.git
cd pytfce
pip install -e .
Dependencies: NumPy, SciPy, connected-components-3d (installed automatically).
Quick start
import nibabel as nib
from pytfce import ptfce_baseline, ptfce_exact
img = nib.load("zstat1.nii.gz")
Z = img.get_fdata()
mask = Z != 0 # or load a proper brain mask
# --- baseline pTFCE (fast, matches R pTFCE) ---
result = ptfce_baseline(Z, mask)
# --- hybrid eTFCE–GRF (exact cluster retrieval) ---
result = ptfce_exact(Z, mask)
# result keys:
# result["p"] — enhanced p-values (3-D array)
# result["logp"] — −log10(p) for visualization
# result["Z_enhanced"] — enhanced Z-scores
# result["smoothness"] — estimated smoothness dict
Variants
| Variant | Method | Speed (2M vox) | Best for |
|---|---|---|---|
ptfce_baseline |
pTFCE (threshold grid + CCL) | ~5 s | Standard use, small–medium volumes |
ptfce_exact |
eTFCE–GRF (union-find + GRF) | ~84 s | Large volumes, finer threshold grids |
Both variants use identical GRF p-values and aggregation. The baseline sweeps a threshold grid with connected-component labelling at each level; the hybrid uses a single union-find sweep, which eliminates discretisation error at higher constant computational cost.
Validation
Spatial detection. Phantom study (64³ volume, 80 subjects, 3 embedded blobs). All pTFCE variants achieve Dice = 1.0:
Runtime. Log-scale comparison across five methods (emulated phantom). Py pTFCE runs in 0.34 s vs 21.7 s for R pTFCE (64× speedup):
API reference
| Function | Description |
|---|---|
ptfce_baseline(Z, mask, ...) |
Baseline pTFCE with LUT-accelerated GRF p-values |
ptfce_exact(Z, mask, ...) |
Hybrid eTFCE–GRF via union-find + analytical GRF |
estimate_smoothness(Z, mask) |
Estimate image smoothness (FWHM) from a Z-score map |
estimate_smoothness_from_residuals(Y, X, mask) |
Estimate smoothness from GLM residuals |
fwer_z_threshold(n_resels, alpha) |
GRF Euler-characteristic FWER Z-threshold |
pvox_clust(V, Rd, c, h) |
P(Z ≥ h | cluster_size = c) via GRF Bayes' rule |
aggregate_logpvals_vec(s, delta) |
Vectorised Q-function for log-probability aggregation |
Citation
If you use pytfce in your research, please cite:
@article{pytfce2026,
author = {Yin, Don and Chen, Hao},
title = {pytfce: {Fast} probabilistic {Threshold-Free Cluster Enhancement}
in {Python}},
journal = {Journal of Open Source Software},
year = {2026},
doi = {10.21105/joss.XXXXX}
}
License
MIT
Metadata
Release files for pytfce 0.1.1
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 |
|---|---|---|---|---|
| pytfce-0.1.1.tar.gz | 33.6 kB | With provenance | Details for pytfce-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytfce-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for pytfce-0.1.1-py3-none-any.whl |
Total release size: 68.4 kB
Release files / pytfce-0.1.1.tar.gz
| Download URL | pytfce-0.1.1.tar.gz |
|---|---|
| Size | 33.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8a4bc94b8d334676e6024be541208c3f302df90d02959b3118e308aad1bb68e8
|
|
BLAKE2b-256 checksum How to use checksums |
5059d656b10518f53994715558ce5f4829e5e75c1dd1b5347038c858d8784135
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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 Mar 10, 2026.
Transparency logRelease files / pytfce-0.1.1-py3-none-any.whl
| Download URL | pytfce-0.1.1-py3-none-any.whl |
|---|---|
| Size | 34.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6888323d5563ca6da85de1c420201f7436bf7ea6883483dfbd6f8ed3e92c90e7
|
|
BLAKE2b-256 checksum How to use checksums |
a62ad432d82be9a355925fdfe80b72d53177d09c804b045f9d4dc5a30bca3312
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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 Mar 10, 2026.
Transparency logSecurity for pytfce 0.1.1
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
All 2 files in this release have been uploaded with provenance metadata.
Attestation for pytfce-0.1.1.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 Mar 10, 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.