Skip to content

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

git clone git@github.com:vchz/pfi.git
cd pfi
pip install -e .

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:

from pfi.utils.data import X_from_snapshots

X = X_from_snapshots(snaps, times)

load_data accepts either:

  • a local .h5ad / .h5ad.gz path
  • 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:

The flow stage is exposed through FlowEstimator. The main solver names are:

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:

The flow-stage methods are:

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), and GaussianOT, GaussianDebiasedSinkhorn for 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: