BrainSpace icon indicating copy to clipboard operation
BrainSpace copied to clipboard

How to use my own atlas?ValueError: Array shape is not valid.

Open AstonshisL opened this issue 5 months ago • 1 comments

Many thanks to the authors for creating brainspace, which helped me. But I was curious, how do you plot gradients using atlas and brain templates other than examples?

Here is my code:

surf_labels_lh = nb.freesurfer.read_annot('E:\Papers\Datasets\MCI diagnosis\BN_Atals\label/lh.BN_Atlas.annot')[0]
surf_labels_rh = nb.freesurfer.read_annot( 'E:\Papers\Datasets\MCI diagnosis\BN_Atals\label/rh.BN_Atlas.annot')[0]

labeling = np.concatenate([surf_labels_lh, surf_labels_rh])
mask = labeling != 0
surf_lh = brainspace.mesh.mesh_io.read_surface('E:\Papers\Datasets\MCI diagnosis\BN_Atals\surf/fsaverage.L.pial.32k_fs_LR.surf.gii')
surf_rh = brainspace.mesh.mesh_io.read_surface('E:\Papers\Datasets\MCI diagnosis\BN_Atals\surf/fsaverage.R.pial.32k_fs_LR.surf.gii')

correlation_matrix = scipy.io.loadmat('E:\Papers\Datasets\MCI diagnosis\MCI/MCI_subject_1_matrix.mat')
input_matrix = correlation_matrix['subject_1_matrix']
gm = GradientMaps(n_components=2, random_state=0)
gm.fit(input_matrix)
grad = [None] * 2

for i in range(2):
    gradients = gradient_zscore(gm.gradients_[:, i])
    grad[i] = map_to_labels(gradients, labeling, mask=mask)
plot_hemispheres(surf_lh, surf_rh, array_name=grad, size=(1200, 400), cmap='viridis_r',
                 color_bar=True,label_text=['Grad1', 'Grad2'], zoom=1.55)`
The dimension of the input matrix is 248*248.
But in the last step:```
plot_hemispheres(surf_lh, surf_rh, array_name=grad, size=(1200, 400), cmap='viridis_r',
                 color_bar=True,label_text=['Grad1', 'Grad2'], zoom=1.55)

An error has occurred:

C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\brainspace\gradient\embedding.py:70: UserWarning: Affinity is not symmetric. Making symmetric.
  warnings.warn('Affinity is not symmetric. Making symmetric.')
Traceback (most recent call last):
  File "E:\Papers\Code\mycode_IDSCN\OpenBUB test\Code\MCI_gradients.py", line 91, in <module>
    plot_hemispheres(surf_lh, surf_rh, array_name=grad, size=(1200, 400), cmap='viridis_r',
  File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\brainspace\vtk_interface\decorators.py", line 41, in _wrapper_wrap
    data = func(*args, **kwds)
  File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\brainspace\plotting\surface_plotting.py", line 557, in plot_hemispheres
    surf_rh.append_array(an[n_pts_lh:], name=name, at='p')
  File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\brainspace\vtk_interface\wrappers\data_object.py", line 214, in append_array
    _array_overwrite(self.PointData, to_point)
  File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\brainspace\vtk_interface\wrappers\data_object.py", line 211, in _array_overwrite
    raise ValueError('Array shape is not valid.')
ValueError: Array shape is not valid.

How should I modify my code? Please help me, this problem has been bothering me for a long time!

AstonshisL avatar Jan 23 '24 09:01 AstonshisL