Skip to content

LinAlgError When the data in adata contains negative values #349

@Nanguage

Description

@Nanguage

When the data in adata contains negative values, the following error occurs when running st.align.morpho_align.

---------------------------------------------------------------------------
LinAlgError                               Traceback (most recent call last)
Cell In[19], [line 1](vscode-notebook-cell:?execution_count=19&line=1)
----> [1](vscode-notebook-cell:?execution_count=19&line=1) aligned_slices, pis = st.align.morpho_align(
      [2](vscode-notebook-cell:?execution_count=19&line=2)     models=[adata_s1, adata_s2],
      [3](vscode-notebook-cell:?execution_count=19&line=3)     spatial_key=spatial_key,
      [4](vscode-notebook-cell:?execution_count=19&line=4)     key_added=key_added,
      [5](vscode-notebook-cell:?execution_count=19&line=5)     device='0',
      [6](vscode-notebook-cell:?execution_count=19&line=6)     verbose=True,
      [7](vscode-notebook-cell:?execution_count=19&line=7)     rep_layer='X_group_pca',
      [8](vscode-notebook-cell:?execution_count=19&line=8)     rep_field='obsm',
      [9](vscode-notebook-cell:?execution_count=19&line=9)     dissimilarity='cos',
     [10](vscode-notebook-cell:?execution_count=19&line=10) )

File ~/software/spateo-release/spateo/alignment/morpho_alignment.py:96, in morpho_align(models, rep_layer, rep_field, genes, spatial_key, key_added, iter_key_added, vecfld_key_added, mode, dissimilarity, max_iter, dtype, device, verbose, **kwargs)
     [77](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/morpho_alignment.py:77) modelB = align_models[i + 1]
     [79](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/morpho_alignment.py:79) morpho_model = Morpho_pairwise(
     [80](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/morpho_alignment.py:80)     sampleA=modelB,  # reverse
     [81](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/morpho_alignment.py:81)     sampleB=modelA,  # reverse
   (...)
     [94](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/morpho_alignment.py:94)     **kwargs,
     [95](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/morpho_alignment.py:95) )
---> [96](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/morpho_alignment.py:96) P = morpho_model.run()
     [97](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/morpho_alignment.py:97) modelB.obsm[f"{key_added}_rigid"] = morpho_model.optimal_RnA.copy()
     [98](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/morpho_alignment.py:98) modelB.obsm[f"{key_added}_nonrigid"] = morpho_model.XAHat.copy()

File ~/miniconda3/envs/spateo/lib/python3.10/site-packages/torch/utils/_contextlib.py:120, in context_decorator.<locals>.decorate_context(*args, **kwargs)
    [117](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/miniconda3/envs/spateo/lib/python3.10/site-packages/torch/utils/_contextlib.py:117) @functools.wraps(func)
    [118](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/miniconda3/envs/spateo/lib/python3.10/site-packages/torch/utils/_contextlib.py:118) def decorate_context(*args, **kwargs):
    [119](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/miniconda3/envs/spateo/lib/python3.10/site-packages/torch/utils/_contextlib.py:119)     with ctx_factory():
--> [120](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/miniconda3/envs/spateo/lib/python3.10/site-packages/torch/utils/_contextlib.py:120)         return func(*args, **kwargs)

File ~/software/spateo-release/spateo/alignment/methods/morpho_class.py:259, in Morpho_pairwise.run(self)
    [245](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/morpho_class.py:245) """
    [246](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/morpho_class.py:246) Run the pairwise alignment process for spatial transcriptomics data.
    [247](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/morpho_class.py:247) 
   (...)
    [256](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/morpho_class.py:256)     np.ndarray: The final cell-cell assignment matrix.
    [257](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/morpho_class.py:257) """
    [258](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/morpho_class.py:258) if self.nn_init:
--> [259](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/morpho_class.py:259)     self._coarse_rigid_alignment()
    [261](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/morpho_class.py:261) self._initialize_variational_variables()
    [263](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/morpho_class.py:263) # calculate the representation(s) pairwise distance matrix if pre_compute_dist is True or not in SVI mode
    [264](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/morpho_class.py:264) # this will reduce the runtime but consume more GPU memory

File ~/software/spateo-release/spateo/alignment/methods/morpho_class.py:1004, in Morpho_pairwise._coarse_rigid_alignment(self, n_sampling)
   [1001](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/morpho_class.py:1001) train_x, train_y = coordsA[NN[:, 1], :], coordsB[NN[:, 0], :]
   [1003](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/morpho_class.py:1003) # coarse alignment core function
-> [1004](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/morpho_class.py:1004) P, R, t, init_weight, sigma2, gamma = inlier_from_NN(train_x, train_y, distance[:, None])
   [1006](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/morpho_class.py:1006) # if allow_filp, then try to flip the data
   [1007](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/morpho_class.py:1007) if self.allow_flip:

File ~/software/spateo-release/spateo/alignment/methods/utils.py:1253, in inlier_from_NN(train_x, train_y, distance)
   [1251](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/utils.py:1251) X_mu, Y_mu = train_x - mu_x, train_y - mu_y
   [1252](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/utils.py:1252) A = np.dot(Y_mu.T, np.multiply(X_mu, P))
-> [1253](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/utils.py:1253) svdU, svdS, svdV = np.linalg.svd(A)
   [1254](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/utils.py:1254) C = np.eye(D)
   [1255](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/software/spateo-release/spateo/alignment/methods/utils.py:1255) C[-1, -1] = np.linalg.det(np.dot(svdU, svdV))

File ~/miniconda3/envs/spateo/lib/python3.10/site-packages/numpy/linalg/_linalg.py:1839, in svd(a, full_matrices, compute_uv, hermitian)
   [1835](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/miniconda3/envs/spateo/lib/python3.10/site-packages/numpy/linalg/_linalg.py:1835) signature = 'D->DdD' if isComplexType(t) else 'd->ddd'
   [1836](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/miniconda3/envs/spateo/lib/python3.10/site-packages/numpy/linalg/_linalg.py:1836) with errstate(call=_raise_linalgerror_svd_nonconvergence,
   [1837](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/miniconda3/envs/spateo/lib/python3.10/site-packages/numpy/linalg/_linalg.py:1837)               invalid='call', over='ignore', divide='ignore',
   [1838](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/miniconda3/envs/spateo/lib/python3.10/site-packages/numpy/linalg/_linalg.py:1838)               under='ignore'):
-> [1839](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/miniconda3/envs/spateo/lib/python3.10/site-packages/numpy/linalg/_linalg.py:1839)     u, s, vh = gufunc(a, signature=signature)
   [1840](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/miniconda3/envs/spateo/lib/python3.10/site-packages/numpy/linalg/_linalg.py:1840) u = u.astype(result_t, copy=False)
   [1841](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/miniconda3/envs/spateo/lib/python3.10/site-packages/numpy/linalg/_linalg.py:1841) s = s.astype(_realType(result_t), copy=False)

File ~/miniconda3/envs/spateo/lib/python3.10/site-packages/numpy/linalg/_linalg.py:113, in _raise_linalgerror_svd_nonconvergence(err, flag)
    [112](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/miniconda3/envs/spateo/lib/python3.10/site-packages/numpy/linalg/_linalg.py:112) def _raise_linalgerror_svd_nonconvergence(err, flag):
--> [113](https://vscode-remote+ssh-002dremote-002blenovo-002dtag308-002dfrp.vscode-resource.vscode-cdn.net/home/weize/analysis/openst-2510/~/miniconda3/envs/spateo/lib/python3.10/site-packages/numpy/linalg/_linalg.py:113)     raise LinAlgError("SVD did not converge")

LinAlgError: SVD did not converge

Spateo version: 1.1.0.dev70+622a20f.dirty

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions