fabian-sp / GGLasso

A Python package for General Graphical Lasso computation
MIT License
30 stars 14 forks source link

AssertionError: input X is not symmetric, but all the input matrices are symmetric #45

Closed myxa closed 8 months ago

myxa commented 8 months ago

Trying to run the following code:

P = glasso_problem(fc_cl, len(fc_cl), reg_params={'lambda1': 0.005, 'lambda2': 0.001}, latent=False, do_scaling=False)

I checked all the input data to be symmetric, but still this error appears.

AssertionError                            Traceback (most recent call last)
Cell In[31], line 1
----> 1 P.solve(verbose=True)

File ~/projects/OpenCloseProject/nilearn_env/lib/python3.10/site-packages/gglasso/problem.py:440, in glasso_problem.solve(self, Omega_0, solver_params, tol, rtol, solver, verbose)
    436         info = {}
    439 elif self.conforming:         
--> 440     sol, info = ADMM_MGL(S = self.S, lambda1 = self.reg_params['lambda1'], lambda2 = self.reg_params['lambda2'], reg = self.reg,\\
    441              Omega_0 = self.Omega_0, latent = self.latent, mu1 = self.reg_params['mu1'],\\
    442                  tol = self.tol, rtol = self.rtol, **self.solver_params)
    445 else:
    446     sol, info = ext_ADMM_MGL(S = self.S, lambda1 = self.reg_params['lambda1'], lambda2 = self.reg_params['lambda2'], reg = self.reg,\\
    447                              Omega_0 = self.Omega_0, G = self.G, tol = self.tol, rtol = self.rtol,\\
    448                                  latent = self.latent, mu1 = self.reg_params['mu1'], **self.solver_params)

File ~/projects/OpenCloseProject/nilearn_env/lib/python3.10/site-packages/gglasso/solver/admm_solver.py:172, in ADMM_MGL(S, lambda1, lambda2, reg, Omega_0, Theta_0, X_0, n_samples, tol, rtol, stopping_criterion, update_rho, rho, max_iter, verbose, measure, latent, mu1)
    169     Omega_t[k,:,:] = phiplus(beta = nk[k,0,0]/rho, D = eigD[k,:], Q = eigQ[k,:,:])
    171 # Theta Update
--> 172 Theta_t = prox_p(Omega_t + L_t + X_t, (1/rho)*lambda1, (1/rho)*lambda2, reg)
    174 #L Update
    175 if latent:

File ~/projects/OpenCloseProject/nilearn_env/lib/python3.10/site-packages/gglasso/solver/ggl_helper.py:195, in prox_p()
    192 @njit()
    193 def prox_p(X, l1, l2, reg):
    194     #X is always symmetric and hence we only calculate upper diagonals
--> 195     assert np.abs(X - trp(X)).max() <= 1e-5, \"input X is not symmetric\"
    196     assert np.minimum(l1,l2) > 0, \"lambda 1 and lambda2 have to be positive\"
    198     (K,p,p) = X.shape

AssertionError: input X is not symmetric
fabian-sp commented 8 months ago

Hi, from the error message it seems, that it calls the solver for multiple Graphical Lasso problems (ADMM_MGL). In that case, fc_cl should be a (K,p,p) array, where each fc_cl[k,:,:] should be a symmetric matrix. Did you verify this? What is K and p in your example? Also I noticed that the second argument should be the number of samples, which is in general not the same as len(fc_cl). Note that fc_cl should already be a covariance matrix (that is, not a data array of shape [num_samples, p]).

myxa commented 8 months ago

Thank you for your answer! Here fc_cl is an array of covariance matrices, every matrix is symmetric, and shape of fc_cl is [84, 116, 116]. But now I'm confused now about term "num of samples". Here I have data for 84 people, and therefore I have 84 samples?

In addition, mentioned error is raised on 50th iteration only.

fabian-sp commented 8 months ago

Ok, got it. So the second argument is the number of samples you had for computing the covariance matrix. If, for example, for each of the 84 arrays the matrix was computed from 100 samples, then set N=100 (if the number of samples is different for each of the 84, you can also input an array for N.

Regarding the symmetry: this can happen due to rounding issues I guess; this assertion might be too overcautious. I would recommend to either try to increase the tolerance (that is, set 1e-5 to a higher number in the line that throws the error), or comment out that line entirely. Let me know what happens.

myxa commented 8 months ago

If I change tolerance, it fails, but at different iterations. If the line is commented out, algorithm runs till termination. The problem is in my data, the matrices are symmetric with certain tolerance too, which is not enough i guess. Thank you for the help!