PFI Documentation
PFI (Probability Flow Inference) provides estimators to learn a score model and a probability-flow model from time-resolved cross-sectional data. The package is organized around a high-level composite estimator API, and a lower-level API for users who want to control the score stage, the flow stage, or the model classes directly.
Package structure
| Module | Content |
|---|---|
pfi |
make_pfi_estimator and the composite PFI estimator (score stage, then flow stage). |
pfi.score |
ScoreEstimator; score models in pfi.score.models (DSMScore, OUScore); the denoising score matching solver DSM_ in pfi.score.solvers. |
pfi.flow |
FlowEstimator; flow and growth models in pfi.flow.models (CLEFlow, MultiplicativeFlow, AdditiveFlow, AutonomousODEFlow, ODEFlow, OUFlow, AutonomousGrowth, ...); interpolants in pfi.flow.interpolants; path ensembles in pfi.flow.paths; solvers in pfi.flow.solvers. |
pfi.base |
Model, the base class of every score, flow and growth model, and its mixins (network architecture, input normalization, output perturbation). |
pfi.utils |
data loading and conversion (pfi.utils.data), simulators of reference processes (pfi.utils.simulations: toggle switch, Ornstein-Uhlenbeck), and neural-network building blocks (pfi.utils.nns). |
Installation
Main dependencies include numpy, torch, tqdm, POT, geomloss, torchcubicspline, and scanpy.
Quick Start (composite estimator)
Data format and loading
All estimators expect a matrix X with shape (n_samples_total, ndim + 1):
- columns
0..ndim-1: state variables - last column: time
If you already have snapshots grouped by time, convert them with X_from_snapshots:
load_data accepts either:
- a local
.h5ad/.h5ad.gzpath - the aliases
"natcomm"or"kaggle"
For these aliases, the data is fetched from the Zenodo repository associated with the package: https://doi.org/10.5281/zenodo.19237707. Downloaded files are cached in pfi.utils.data.PFI_DATA_FOLDER, which defaults to ~/pfi_data.
For the exact function signature and outputs, see load_data.
Using a composite estimator
The main high-level entry point is make_pfi_estimator. It creates a composite PFI object from a score estimator and a flow estimator. The fitting order is fixed: score first, then flow.
import torch
from pfi import make_pfi_estimator
from pfi.flow.models import CLEFlow
from pfi.score.models import DSMScore
device = "cuda" if torch.cuda.is_available() else "cpu"
score_model = DSMScore(
sizes=[128, 128, 128, 128],
batch_norm="zscore",
)
flow_model = CLEFlow(
score=None,
sizes=[128, 128, 128],
lx=1.5,
)
est = make_pfi_estimator(
score_model=score_model,
flow_model=flow_model,
s_solver="dsm",
f_solver="pfm",
s_solver_kwargs={"L": 5, "lr": 5e-4, "n_epochs": 4000},
f_solver_kwargs={"fac": 2, "lr": 1e-3, "n_epochs": 2000},
device=device,
random_state=0,
)
est.fit(X)
This is the standard high-level usage pattern. For the complete signature, see make_pfi_estimator. The resulting object is documented in PFI.
Quick note on the solvers
The score stage is exposed through ScoreEstimator. The solver currently supported there is:
dsm: denoising score matching, taken from: https://doi.org/10.1162/NECO_a_00142, https://doi.org/10.48550/arXiv.1907.05600. The noise kernel is Gaussian by default;solver_kwargs={"kernel": "laplace"}uses a Laplace kernel, whose score tends to a constant away from the data instead of growing linearly.
The flow stage is exposed through FlowEstimator. The main solver names are:
pfm: the flow-matching solver, please cite: https://doi.org/10.48550/arXiv.2604.25062.pfm-linear: the same procedure, solved in closed form for the linearOUFlowmodel.upfi: the neural-ODE-style UPFI solver, please cite: https://doi.org/10.48550/arXiv.2505.13197, https://doi.org/10.1073/pnas.2420621122.future.ufm_ot,future.ufm_uot: experimental unbalanced flow-matching solvers.external.deepruotv2: wrapper of an external solver, for comparison.
Low level usage
If you want finer control, work with the two stages separately using ScoreEstimator and FlowEstimator directly.
By default ScoreEstimator uses DSMScore, but you can provide your own instantiated score model. FlowEstimator accepts an instantiated flow model and an optional growth model.
from pfi.score import ScoreEstimator
from pfi.flow import FlowEstimator
from pfi.flow.models import CLEFlow, AutonomousGrowth
score_est = ScoreEstimator(
solver="dsm",
solver_kwargs={"L": 5, "lr": 5e-4, "n_epochs": 4000},
device=device,
)
score_est.fit(X)
flow_est = FlowEstimator(
flow=CLEFlow(score=score_est.model_, lx=0.3),
growth=AutonomousGrowth(),
solver="pfm",
solver_kwargs={"fac": 2, "lr": 1e-3, "n_epochs": 2000},
device=device,
)
flow_est.fit(X)
The score-stage methods are:
ScoreEstimator.fit: fit the score model from(x, t)dataScoreEstimator.predict: evaluate the score on time-augmented inputsScoreEstimator.sample: generate samples at the times present in the inputScoreEstimator.score: compare generated samples to observed samples, time by time
The flow-stage methods are:
FlowEstimator.fit: fit the flow model, and optionally a growth modelFlowEstimator.predict: evaluate the learned driftFlowEstimator.sample: simulate trajectories from initial statesFlowEstimator.score: push source samples to the next later target time and compare them to target samples
Trajectories: path ensembles and iterated coupling
Flow matching regresses the model velocity on trajectories through the snapshots. Their construction is set by the paths solver argument, an object from pfi.flow.paths, used like the interp argument for the interpolant:
- plan-based paths, random paths through the observed cells drawn from transport plans between consecutive snapshots:
Independent,ExactOT(the default),Sinkhorn,UnbalancedSinkhorn; - map-based paths, a cloud of nodes moved from one snapshot to the next by a transport map:
DebiasedSinkhorn(debiased entropic map), andGaussianOT,GaussianDebiasedSinkhornfor Gaussian snapshots.
With n_rounds > 1, the coupling is iterated: every round builds the paths again with the current model's flow as reference dynamics, and retrains the model.
from pfi.flow.interpolants import ChebyshevInterpolant
from pfi.flow.paths import DebiasedSinkhorn
flow_est = FlowEstimator(
flow=CLEFlow(score=score_est.model_, lx=1.0),
solver="pfm",
solver_kwargs={
"interp": ChebyshevInterpolant(),
"paths": DebiasedSinkhorn(reg=1.0),
"n_rounds": 5,
},
device=device,
)
For concrete runnable workflows, use the notebooks in the examples folder (https://github.com/vchz/pfi/tree/main/examples):
ornstein_uhlenbeck.ipynb: inference of a linear Ornstein-Uhlenbeck process;toggle_switch.ipynb: a two-gene toggle switch with growth;interpolants.ipynb: the interpolants on OT trajectories.
For the full public API reference, browse: