scQUEST.abnormality module

Summary

Classes:

Abnormality

Estimator to quantify the abnormality of a cell's expression profile.

AbnormalityLitModule

pytorch_lightning Abnormality module

DefaultAE

Default AE as implemented in [Wagner2019]

Reference

class DefaultAE(n_in, hidden=(10, 2, 10), bias=True, activation=ReLU(), activation_last=Sigmoid(), seed=None)[source]

Bases: torch.nn.modules.module.Module

Default AE as implemented in [Wagner2019]

__init__(n_in, hidden=(10, 2, 10), bias=True, activation=ReLU(), activation_last=Sigmoid(), seed=None)[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

__doc__ = '\n    Default AE as implemented in [Wagner2019]_\n    '
__module__ = 'scQUEST.abnormality'
training: bool
_is_full_backward_hook: Optional[bool]
class AbnormalityLitModule(*args, **kwargs)[source]

Bases: scQUEST.utils.LitModule

pytorch_lightning Abnormality module

__init__(*args, **kwargs)[source]
forward(X, mode=None)[source]

return the reconstruction error

Return type

int

__doc__ = 'pytorch_lightning Abnormality module'
__module__ = 'scQUEST.abnormality'
training: bool
_is_full_backward_hook: Optional[bool]
class Abnormality(n_in=None, model=None, loss_fn=None, metrics=None, seed=None)[source]

Bases: scQUEST.utils.Estimator

Estimator to quantify the abnormality of a cell’s expression profile. Abnormality is defined as the average reconstruction error of the autoencoder trained on a reference (normal) cell population.

Parameters
  • n_in – number of feature for estimator

  • model – Model used to train estimator torch.Module or pytorch_lightning.Module

  • loss_fn – Loss function used for optimization

  • metrics – Metrics tracked during test time

Note

The abnormality model (Abn.model) predicts the abnormality a.k.a. reconstruction error, i.e. \(X-F(X)\) where \(F\) is the autoencoder (saved to ad.layers[‘abnormality’]). On the other hand, the base torch model (Abn.model.model) predicts the reconstruction, i.e. \(F(X)\).

__init__(n_in=None, model=None, loss_fn=None, metrics=None, seed=None)[source]

Base estimator class

Parameters
  • n_in – number of feature for estimator

  • model – Model used to train estimator torch.Module or pytorch_lightning.Module

  • loss_fn – Loss function used for optimization

  • metrics – Metrics tracked during test time

  • seed – Seed for model weight initialisation

fit(ad=None, layer=None, datamodule=None, max_epochs=100, callbacks=None, seed=None, **kwargs)[source]

Fit abnormality estimator (autoencoder). Given the cell-expression profile given in ad.X or ad.layer[layer], an autoencoder is fitted. By default the given data is randomly split 90/10 in training and test set. If you wish to customize training provide a datamodule with the given train/validation/test splits.

Parameters
  • ad (Optional[AnnData]) – AnnData object to fit

  • layer (Optional[str]) – layer in ad.layers to use instead of ad.X

  • datamodule (Optional[LightningDataModule]) – pytorch lightning data module with custom configurations of train, val and test splits

  • preprocessing – list of processors (Preprocessor) that should be applied to the dataset

  • early_stopping – configured EarlyStopping class

  • max_epochs (int) – maximum epochs for which the model is trained

  • callbacks (Optional[list]) – additional pytorch_lightning callbacks

  • seed (Optional[int]) – Seed for data split

Return type

None

Returns

None

predict(ad, layer=None, inplace=True)[source]

Predict abnormality of each cell-feature as the difference between target and reconstruction (y-pred).

Parameters
  • ad (AnnData) – AnnData object to fit

  • layer (Optional[str]) – AnnData.X layer to use for prediction

  • inplace – whether to manipulate the AnnData object inplace or return a copy

Return type

AnnData

Returns

None or AnnData depending on inplace.

_predict_step(X)[source]
static aggregate(ad, agg_fun='mse', key='abnormality', layer='abnormality')[source]

Aggregate the high-dimensional (number of features) reconstruction error of each cell.

Parameters
  • ad – AnnData object

  • agg_fun (Union[str, Callable]) – mse or function used to aggregate the observed reconstruciton errors

  • key – key under which the results should be stored in ad.obs

  • layer – layer in X used to compute the aggregation

_default_model(*args, **kwargs)[source]
Return type

Module

_default_loss()[source]
_default_metric()[source]
_default_litModule()[source]
_configure_anndata_class()[source]
__doc__ = "Estimator to quantify the abnormality of a cell's expression profile. Abnormality is defined as the average\n    reconstruction error of the autoencoder trained on a reference (normal) cell population.\n\n    Args:\n        n_in: number of feature for estimator\n        model: Model used to train estimator :class:`.torch.Module` or :class:`.pytorch_lightning.Module`\n        loss_fn: Loss function used for optimization\n        metrics: Metrics tracked during test time\n\n    Note:\n        The abnormality model (`Abn.model`) predicts the abnormality a.k.a. reconstruction error, i.e. :math:`X-F(X)`\n        where :math:`F` is the autoencoder (saved to `ad.layers['abnormality']`). On the other hand, the base torch\n        model (`Abn.model.model`) predicts the reconstruction, i.e. :math:`F(X)`.\n\n    "
__module__ = 'scQUEST.abnormality'