smoother.models.impute ====================== .. py:module:: smoother.models.impute .. autoapi-nested-parse:: Impute spatial feature of interest with spatial loss Classes ------- .. autoapisummary:: smoother.models.impute.ImputeModel smoother.models.impute.ImputeTorch smoother.models.impute.ImputeConvex Module Contents --------------- .. py:class:: ImputeModel(y_obs, spatial_loss_all: smoother.losses.SpatialLoss, fixed_obs=True, nonneg=False, **kwargs) Class for different implementations of spatial imputation. This class is designed for coorperative inheritance (mixin). .. attribute:: y_obs Observed spatial feature matrix, n_obs x n_feature. :type: 2D tensor .. attribute:: spatial_loss_all Spatial loss object build from the combined spatial coordinates containing both observed (the first n_obs rows) and missing spots (the rest rows). :type: SpatialLoss .. attribute:: fixed_obs Whether to allow updates in the observed data. :type: bool .. attribute:: nonneg Whether to enforce nonnegativity on spatial features. :type: bool .. attribute:: n_feature Number of spatial features to process. :type: int .. attribute:: n_all Number of total spots. n_all = n_obs + n_missing. :type: int .. attribute:: n_obs Number of observed spots. :type: int .. attribute:: n_missing Number of missing spots. :type: int .. attribute:: var_obs Observed spatial feature matrix, n_obs x n_feature. .. attribute:: var_missing Missing spatial feature matrix to impute, n_missing x n_feature. .. attribute:: var_all Concatenated observed and missing spatial feature matrix, n_all x n_feature. .. attribute:: impute_configs Dictionary of imputation configurations. :type: dict .. attribute:: impute_time Time spent on deconvolution. :type: float .. attribute:: impute_flag Whether the imputation was successful. :type: bool .. py:attribute:: y_obs :value: None .. py:attribute:: spatial_loss_all :value: None .. py:attribute:: fixed_obs :value: None .. py:attribute:: nonneg :value: None .. py:attribute:: n_feature :value: None .. py:attribute:: n_obs :value: None .. py:attribute:: n_all :value: None .. py:attribute:: n_missing :value: None .. py:attribute:: var_obs :value: None .. py:attribute:: var_missing :value: None .. py:attribute:: var_all :value: None .. py:attribute:: impute_configs :value: None .. py:attribute:: impute_time :value: 0 .. py:method:: set_params(y_obs, spatial_loss_all, fixed_obs=True, nonneg=False) Set configuration parameters and initialize variables. .. py:method:: set_variables() :abstractmethod: Set variables to impute. .. py:method:: get_recon_loss() :abstractmethod: Get reconstruction loss. .. py:method:: get_sp_loss() :abstractmethod: Get spatial loss (weighted by lambda_spatial_loss). .. py:method:: get_results() :abstractmethod: Get spatial features after imputation. .. py:class:: ImputeTorch(y_obs, spatial_loss_all: smoother.losses.SpatialLoss, fixed_obs=True, nonneg=False, **kwargs) Bases: :py:obj:`ImputeModel`, :py:obj:`torch.nn.Module` Imputation solver implemented using pytorch. .. attribute:: var_obs Observed spatial feature matrix, n_obs x n_feature. :type: 2D tensor .. attribute:: var_missing Missing spatial feature matrix to impute, n_missing x n_feature. :type: 2D tensor .. attribute:: var_all Concatenated observed and missing spatial feature matrix, n_all x n_feature. :type: 2D tensor .. attribute:: See super class attributes for additional arguments. .. py:attribute:: impute_flag :value: False .. py:method:: set_variables() Initialize torch parameters to estimate. .. py:method:: forward(y_obs, **kwargs) Run imputation for a new set of spatial features. .. py:method:: get_recon_loss() Calculate loss of deviation from observation. If the observed data is not fixed, then the loss is used to make sure data does not deviate too much from the observed value after imputation. .. py:method:: get_sp_loss() Calculate loss of spatial smoothness. .. py:method:: get_results() Get spatial features of all spots after imputation. The first n_obs spots are the observed spots and the rest are the missing ones. If fixed_obs is True, then the returned observed data is not updated. .. py:method:: final_sanity_check() .. py:method:: impute(lambda_spatial_loss=1.0, lr=0.001, max_epochs=1000, patience=10, tol=1e-05, verbose=True, quiet=False) -> bool Run imputation algorithm. :param lambda_spatial_loss: Specifies the strength of the spatial smoothing. :type lambda_spatial_loss: float :param lr: Learning rate. :type lr: float :param max_epochs: Maximum number of training epochs. If -1, iterate until convergence (`d_loss` < tol). :type max_epochs: int :param patient: Number of epochs to wait for the loss to decrease before stopping. :type patient: int :param tol: Tolerance of loss convergence. :type tol: float :param verbose: If True, print out loss while training. :type verbose: bool :param quiet: If True, no output printed. :type quiet: bool :returns: True if imputation is successful. :rtype: bool .. py:class:: ImputeConvex(y_obs, spatial_loss_all: smoother.losses.SpatialLoss, fixed_obs=True, nonneg=False, **kwargs) Bases: :py:obj:`ImputeModel` Imputation solver implemented using cvxpy. .. attribute:: var_obs Observed spatial feature matrix, n_obs x n_feature. .. attribute:: var_missing Missing spatial feature matrix to impute, n_missing x n_feature. .. attribute:: var_all Concatenated observed and missing spatial feature matrix, n_all x n_feature. .. attribute:: recon_loss_exp Reconstruction loss expression. :type: cp.Expression .. attribute:: sp_loss_exp Spatial loss expression. :type: cp.Expression .. attribute:: See super class attributes for additional arguments. .. py:attribute:: recon_loss_exp :value: None .. py:attribute:: spatial_loss_exp :value: None .. py:attribute:: impute_flag :value: False .. py:method:: set_variables() Initialize parameters to estimate. .. py:method:: set_recon_loss() Set loss expression of deviation from observation. If the observed data is not fixed, then the loss is used to make sure data does not deviate too much from the observed value after imputation. .. py:method:: set_sp_loss() Set spatial loss expression (before weighted by lambda_spatial_loss). .. py:method:: get_recon_loss() Calculate loss of deviation from observation. If the observed data is not fixed, then the loss is used to make sure data does not deviate too much from the observed value after imputation. .. py:method:: get_sp_loss() Calculate loss of spatial smoothness (weighted by lambda_spatial_loss). .. py:method:: get_results() Get spatial features of all spots after imputation. The first n_obs spots are the observed spots and the rest are the missing ones. If fixed_obs is True, then the returned observed data is not updated. .. py:method:: impute(lambda_spatial_loss=1.0, verbose=False, quiet=False, solver=None, **kwargs) -> bool Run imputation algorithm. :param lambda_spatial_loss: Specifies the strength of the spatial smoothing. :type lambda_spatial_loss: float :param verbose: If True, print out loss while training. :type verbose: bool :param quiet: If True, no output printed. :type quiet: bool :returns: True if imputation is successful. :rtype: bool