scanvi_explainer.scanvi_bootstrapper.SCANVIBoostrapper#

class scanvi_explainer.scanvi_bootstrapper.SCANVIBoostrapper(model: SCANVI, n_bootstraps: int = 25)#

Methods table#

estimate(func_fn, shap_values)

Calculate measurement for each boostrap run.

feature_plot(shap_values[, n_features, ...])

Feature plot for bootstrapping

run([train_size, batch_size])

Runner

save(shap_values, filename)

Save results to feather format.

Methods#

SCANVIBoostrapper.estimate(func_fn: Callable[[...], _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]], shap_values: list[_SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]]) ndarray#

Calculate measurement for each boostrap run.

Parameters:
  • func_fn (Callable[..., ArrayLike]) – Stat function to call, i.e.: np.mean, np.median. The function must contain axis parameter.

  • shap_values (list[ArrayLike]) – SHAP values

Returns:

Array of metric in format (classifier code, n_features, n_bootstraps)

Return type:

np.ndarray

SCANVIBoostrapper.feature_plot(shap_values: list, n_features: int = 10, metric_fn: ~collections.abc.Callable[[...], ~numpy._typing._array_like._SupportsArray[~numpy.dtype[~typing.Any]] | ~numpy._typing._nested_sequence._NestedSequence[~numpy._typing._array_like._SupportsArray[~numpy.dtype[~typing.Any]]] | bool | int | float | complex | str | bytes | ~numpy._typing._nested_sequence._NestedSequence[bool | int | float | complex | str | bytes]] = <function mean>, kind: ~typing.Literal['boxplot', 'barplot'] = 'boxplot', gene_symbols: None | str = None, n_cols: int = 3, figsize: tuple[int, int] = (20, 20), return_fig: bool = False) Figure | None#

Feature plot for bootstrapping

Parameters:
  • shap_values (list) – SHAP values

  • n_features (int) – Number of features to subset, by default 10

  • metric (Callable[..., ArrayLike]) – Statistical measurement of each boostrap, by default np.mean

  • kind (Literal[&quot;boxplot&quot;, &quot;barplot&quot;]) – Type of plot, by default “boxplot”

  • gene_symbols (None | str = None) – Column name in var for gene symbols

  • n_cols (int) – Number of columns for subplots, by default 3

  • figsize (tuple[int, int]) – Figure size, by default [20, 20]

  • return_fig (bool) – Flag to return figure object, by default False

Returns:

Either plot or return figure object

Return type:

Figure | None

Raises:

ValueError – When specified plot kind is not supported

SCANVIBoostrapper.run(train_size: float = 0.8, batch_size: int = 128) list[ndarray]#

Runner

Parameters:
  • train_sizefloat Training size (background), by default 0.8

  • batch_sizeint Number of cells used from each group, by default 128 To ignore the batch_size subsetting, set batch_size=-1

Returns:

List of SHAP bootstrap values

Return type:

list[np.ndarray]

SCANVIBoostrapper.save(shap_values: list[ndarray], filename: str)#

Save results to feather format.

Parameters:
  • shap_values (list[np.ndarray]) – SHAP values

  • filename (str) – Path to filename containing .feather extension

Examples

>>> lvae = scvi.model.SCANVI.load("...")
>>> bootstrapper = SCANVIBoostrapper(lvae, n_bootstraps=10)
>>> shap_values = bootstrapper.run(train_size=0.8, batch_size=64)
>>> bootstrapper.save(shap_values, "./bootstrapped_shaps.feather")