Skip to content

NMF (GPU)

The CPU twin is bixverse.nmf.

nmf

GPU HALS NMF: one run, stabilised restarts, consensus NMF and the k sweep.

Same inputs and result classes as bixverse.nmf; only the HALS solver runs on the GPU, which caps the rank at NMF_GPU_MAX_RANK. The consensus step stays on the CPU.

nmf

nmf(
    sc: ScOrMc,
    k: int,
    *,
    cell_ids: Sequence[str] | None = None,
    gene_ids: Sequence[str] | None = None,
    preprocessing: Preprocessing = "none",
    use_second_layer: bool = True,
    nmf_hals_params: NmfHalsParams | None = None,
    seed: int = 42,
    verbose: Verbose = True,
) -> NmfResult

One HALS NMF run on the GPU.

Parameters:

Name Type Description Default
sc ScOrMc

The experiment, a subset or meta cells.

required
k int

Number of components, at most NMF_GPU_MAX_RANK.

required
cell_ids Sequence[str] | None

Cell (meta cell) ids. None for the kept cells.

None
gene_ids Sequence[str] | None

Gene ids. None for the HVGs.

None
preprocessing Preprocessing

"none", per gene "sd" or "sqrt_sd" scaling.

'none'
use_second_layer bool

Factorise the normalised rather than the raw counts.

True
nmf_hals_params NmfHalsParams | None

The solver. None uses the defaults (NNDSVD).

None
seed int

Seed for a random initialisation.

42
verbose Verbose

0 (False) silent, 1 (True) progress, 2 extra detail where the method has it.

True

Returns:

Type Description
NmfResult

The bixverse.nmf.NmfResult.

Raises:

Type Description
ValueError

On k out of range or without genes.

BixverseError

Without a GPU adapter.

stabilised_nmf

stabilised_nmf(
    sc: ScOrMc,
    k: int,
    *,
    cell_ids: Sequence[str] | None = None,
    gene_ids: Sequence[str] | None = None,
    preprocessing: Preprocessing = "none",
    use_second_layer: bool = True,
    nmf_hals_params: NmfHalsParams | None = None,
    n_runs: int = 30,
    seed: int = 42,
    verbose: Verbose = True,
) -> StabilisedNmfResult

Randomly initialised HALS NMF restarts on the GPU.

Parameters:

Name Type Description Default
sc ScOrMc

The experiment, a subset or meta cells.

required
k int

Number of components, at most NMF_GPU_MAX_RANK.

required
cell_ids Sequence[str] | None

Cell (meta cell) ids. None for the kept cells.

None
gene_ids Sequence[str] | None

Gene ids. None for the HVGs.

None
preprocessing Preprocessing

"none", per gene "sd" or "sqrt_sd" scaling.

'none'
use_second_layer bool

Factorise the normalised rather than the raw counts.

True
nmf_hals_params NmfHalsParams | None

The solver; nmf_init is ignored.

None
n_runs int

Number of restarts.

30
seed int

Base seed of the restarts.

42
verbose Verbose

0 (False) silent, 1 (True) progress, 2 extra detail where the method has it.

True

Returns:

Type Description
StabilisedNmfResult

The bixverse.nmf.StabilisedNmfResult.

Raises:

Type Description
ValueError

On k or n_runs out of range, or without genes.

BixverseError

Without a GPU adapter.

consensus_nmf

consensus_nmf(
    sc: ScOrMc,
    k: int,
    *,
    cell_ids: Sequence[str] | None = None,
    gene_ids: Sequence[str] | None = None,
    preprocessing: Preprocessing = "none",
    use_second_layer: bool = True,
    nmf_hals_params: NmfHalsParams | None = None,
    nmf_consensus_params: NmfConsensusParams | None = None,
    n_runs: int = 30,
    seed: int = 42,
    verbose: Verbose = True,
) -> ConsensusNmfResult

Consensus HALS NMF (cNMF) with the restarts on the GPU.

Parameters:

Name Type Description Default
sc ScOrMc

The experiment, a subset or meta cells.

required
k int

Number of components, 2 to NMF_GPU_MAX_RANK.

required
cell_ids Sequence[str] | None

Cell (meta cell) ids. None for the kept cells.

None
gene_ids Sequence[str] | None

Gene ids. None for the HVGs.

None
preprocessing Preprocessing

"none", per gene "sd" or "sqrt_sd" scaling.

'none'
use_second_layer bool

Factorise the normalised rather than the raw counts.

True
nmf_hals_params NmfHalsParams | None

The solver.

None
nmf_consensus_params NmfConsensusParams | None

The consensus step. None uses the defaults.

None
n_runs int

Number of restarts, at least 2.

30
seed int

Base seed of the restarts and the consensus k-means.

42
verbose Verbose

0 (False) silent, 1 (True) progress, 2 extra detail where the method has it.

True

Returns:

Type Description
ConsensusNmfResult

The bixverse.nmf.ConsensusNmfResult.

Raises:

Type Description
ValueError

On k or n_runs out of range, without genes, or when the consensus step fails.

BixverseError

Without a GPU adapter.

References

Kotliar, et al., eLife, 2019.

nmf_k_sweep

nmf_k_sweep(
    sc: ScOrMc,
    k_range: Sequence[int] | range,
    *,
    cell_ids: Sequence[str] | None = None,
    gene_ids: Sequence[str] | None = None,
    preprocessing: Preprocessing = "none",
    use_second_layer: bool = True,
    nmf_hals_params: NmfHalsParams | None = None,
    nmf_consensus_params: NmfConsensusParams | None = None,
    n_runs: int = 30,
    seed: int = 42,
    verbose: Verbose = True,
) -> NmfKSweepResult

Consensus NMF over a range of k with the restarts on the GPU.

Parameters:

Name Type Description Default
sc ScOrMc

The experiment, a subset or meta cells.

required
k_range Sequence[int] | range

The ranks, unique, 2 to NMF_GPU_MAX_RANK.

required
cell_ids Sequence[str] | None

Cell (meta cell) ids. None for the kept cells.

None
gene_ids Sequence[str] | None

Gene ids. None for the HVGs.

None
preprocessing Preprocessing

"none", per gene "sd" or "sqrt_sd" scaling.

'none'
use_second_layer bool

Factorise the normalised rather than the raw counts.

True
nmf_hals_params NmfHalsParams | None

The solver.

None
nmf_consensus_params NmfConsensusParams | None

The consensus step. None uses the defaults.

None
n_runs int

Restarts per k, at least 2.

30
seed int

Base seed of the restarts and the consensus k-means.

42
verbose Verbose

0 (False) silent, 1 (True) progress, 2 extra detail where the method has it.

True

Returns:

Type Description
NmfKSweepResult

The bixverse.nmf.NmfKSweepResult.

Raises:

Type Description
ValueError

On an invalid k_range or n_runs, or without genes.

BixverseError

Without a GPU adapter.

References

Kotliar, et al., eLife, 2019.