MICA-MNI / BrainSpace

BrainSpace is an open-access toolbox that allows for the identification and analysis of gradients from neuroimaging and connectomics datasets | available in both Python and Matlab |
http://brainspace.readthedocs.io
BSD 3-Clause "New" or "Revised" License
185 stars 75 forks source link

Use diffusion_mapping in MATLAB and got complex number in the eigvalue #98

Open tw70455 opened 1 year ago

tw70455 commented 1 year ago

Hello,

I would like to use diffusion_map to obtain the embedding and eigenvalues. I provided a symmetric cosine similarity matrix (no negative value) as input data with an alpha value of 0.5 and a diffusion time of 0. Although I obtained both the embedding and eigenvalues, I noticed that the eigenvalues are not sorted. Upon reviewing the code, I discovered that the eigval is a complex number ([eigvec, eigval] = eig(M)). As a result, the sorting ([eigval, idx] = sort(eigval,'descend');) did not work. I am wondering if this is because I used the wrong matrix for input? Thank you.

ReinderVosDeWael commented 1 year ago

Could you try running your matrix through the standard GradientMaps object with the kernel set to 'none', and other parameters set appropriately? This would be the recommended approach, and already handles some standard error cases. Does this yield the same outcome?

tw70455 commented 1 year ago

Thank you for your reply. I noticed that there is a slight difference between the MATLAB and Python code for diffusion_map estimation.

In MATLAB code, 'data' is the input matrix, and 'M' is the matrix for eigenvalue/eigenvector estimation. 'd = sum(data,2) .^ -alpha; L = data . (dd.');

d2 = sum(L,2) .^ -1; M = bsxfun(@times, L, d2);'

In Python code, 'adj' is the input matrix and also for the eigenvalue/eigenvector estimation. ' if alpha > 0: if use_sparse: d = np.power(adj.sum(axis=1).A1, -alpha) adj.data = d[adj.row] adj.data = d[adj.col] else: d = adj.sum(axis=1, keepdims=True) d = np.power(d, -alpha) adj = d.T adj = d'

It looks like the matrix in MATLAB will be asymmetry for the eigenvalue estimation, and it won't happen in the Python code.