Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LinAlgError: SVD did not converge #307

Open
Li-ZhiD opened this issue Jan 16, 2025 · 1 comment
Open

LinAlgError: SVD did not converge #307

Li-ZhiD opened this issue Jan 16, 2025 · 1 comment

Comments

@Li-ZhiD
Copy link

Li-ZhiD commented Jan 16, 2025

Run morpho_align_transformation after harmony:

transformation = st.align.morpho_align_transformation(
    models=adatas,
    spatial_key='spatial',
    key_added='spateo_spatial',
    device='cpu',
    verbose=False,
    rep_layer='X_pca_harmony',
    rep_field='obsm',
    dissimilarity='kl'
)

error:

---------------------------------------------------------------------------
LinAlgError                               Traceback (most recent call last)
Cell In[84], line 1
----> 1 transformation = st.align.morpho_align_transformation(
      2     models=adatas,
      3     spatial_key='spatial',
      4     key_added='spateo_spatial',
      5     device='cpu',
      6     verbose=False,
      7     rep_layer='X_pca_harmony',
      8     rep_field='obsm',
      9     dissimilarity='kl'
     10 )

File /mnt/data1/aaa/software/miniconda3/envs/spateo/lib/python3.9/site-packages/spateo/alignment/morpho_alignment.py:206, in morpho_align_transformation(models, models_path, save_transformation, transformation_path, resume, rep_layer, rep_field, genes, spatial_key, key_added, iter_key_added, vecfld_key_added, dissimilarity, max_iter, dtype, device, verbose, **kwargs)
    187     modelB = models[i + 1]
    189 morpho_model = Morpho_pairwise(
    190     sampleA=modelB,  # reverse
    191     sampleB=modelA,  # reverse
   (...)
    204     **kwargs,
    205 )
--> 206 _ = morpho_model.run()
    208 optimal_R, optimal_t = solve_RT_by_correspondence(
    209     morpho_model.optimal_RnA[:, :2], modelB.obsm[spatial_key][:, :2]
    210 )
    211 cur_transformation = {"Rotation": optimal_R, "Translation": optimal_t}

File /mnt/data1/aaa/software/miniconda3/envs/spateo/lib/python3.9/site-packages/torch/utils/_contextlib.py:116, in context_decorator.<locals>.decorate_context(*args, **kwargs)
    113 @functools.wraps(func)
    114 def decorate_context(*args, **kwargs):
    115     with ctx_factory():
--> 116         return func(*args, **kwargs)

File /mnt/data1/aaa/software/miniconda3/envs/spateo/lib/python3.9/site-packages/spateo/alignment/methods/morpho_class.py:291, in Morpho_pairwise.run(self)
    289     self.nonrigid_flag = True
    290     self._update_nonrigid()
--> 291 self._update_rigid()
    292 self.XAHat = self.VnA + self.RnA
    293 self._update_sigma2(iter=iter)

File /mnt/data1/aaa/software/miniconda3/envs/spateo/lib/python3.9/site-packages/spateo/alignment/methods/morpho_class.py:1369, in Morpho_pairwise._update_rigid(self)
   1364 if self.nn_init:
   1365     A -= (self.sigma2 * self.nn_init_weight * self.Sp / self.nx.sum(self.inlier_P)) * self.nx.dot(
   1366         (inlier_A_hat * self.inlier_P).T, -inlier_B_hat
   1367     ).T
-> 1369 svdU, svdS, svdV = _linalg(self.nx).svd(A)
   1370 self.C[-1, -1] = _linalg(self.nx).det(self.nx.dot(svdU, svdV))
   1372 R = self.nx.dot(self.nx.dot(svdU, self.C), svdV)

File /mnt/data1/aaa/software/miniconda3/envs/spateo/lib/python3.9/site-packages/numpy/linalg/linalg.py:1681, in svd(a, full_matrices, compute_uv, hermitian)
   1678         gufunc = _umath_linalg.svd_n_s
   1680 signature = 'D->DdD' if isComplexType(t) else 'd->ddd'
-> 1681 u, s, vh = gufunc(a, signature=signature, extobj=extobj)
   1682 u = u.astype(result_t, copy=False)
   1683 s = s.astype(_realType(result_t), copy=False)

File /mnt/data1/aaa/software/miniconda3/envs/spateo/lib/python3.9/site-packages/numpy/linalg/linalg.py:121, in _raise_linalgerror_svd_nonconvergence(err, flag)
    120 def _raise_linalgerror_svd_nonconvergence(err, flag):
--> 121     raise LinAlgError("SVD did not converge")

LinAlgError: SVD did not converge
@YifanLu2000
Copy link
Contributor

Hi @Li-ZhiD, I suspect the issue is caused by using KL divergence, as "X_pca_harmony" contains negative values. I suggest setting dissimilarity to "cos" instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants