1-way repeated-measures ANOVA on source data: issue with the factor declaration

  • MNE-Python version: 0.21.0
  • operating system: Windows 11 Home

Hi,

I am trying to do rm-ANOVA with 1 factor that has three levels.

My function:

def computeStatistic(*args):

    print('comparing 3 groups')
        
    stats_array = np.zeros((3,n_vertices_fsave))
    pval_array = np.zeros((3,n_vertices_fsave))

    stats, pval= f_mway_rm(np.swapaxes(args, 1, 0), factor_levels=[3],
                     effects='A', return_pvals=True)#subjectx x condition x observations 
    stats_array = stats
    pval_array = pval
    
    return stats_array, pval_array

However, when I call it I get an error with the dot function, as it understands my factors as (3,3) instead of just 3 or (1,3):

stc_fsave_all_real_avg = np.mean(X, axis=2) 
a=np.swapaxes(stc_fsave_all_real_avg, 1, 0)

for i in range(n_vertices_fsave):
        #print('vertex ind: ', i)
    fvalues_r, pvalues_r = computeStatistic(stc_fsave_all_real_avg[:,:,i])

My data is in shape (21, 3, 20484), meaning subjects (21) x conditions (3) x vertices (20484), and the error I get is:

--> 398         y = np.dot(data, c_)
    399         b = np.mean(y, axis=1)[:, np.newaxis, :]
    400         ss = np.sum(np.sum(y * b, axis=2), axis=1)

<__array_function__ internals> in dot(*args, **kwargs)

ValueError: shapes (21,3,1) and (3,3) not aligned: 1 (dim 2) != 3 (dim 0)

Any clue what is going on there, or if I’m declaring my factors in a wrong way?

Thanks a lot!

:point_right: :point_right: :point_right: Please edit or remove the above text before submitting your posting. :point_left: :point_left: :point_left:

Hi @ruxt,

could you post the full error you get (the whole exception traceback)?

So far, based on what I see I have a few comments:

  • although the data shape you report seems to be correct for f_mway_rm you also perform several swapaxes operations. If your shape is correct you don’t need these.
  • in your computeStatistic function you first create two arrays (stats and pval) and then ignore these variables (by assigning different values to the same variable names). You don’t need to create these arrays, you can use the data returned by f_mway_rm.
  • you perform a loop across vertices, you don’t need this - f_mway_rm should work for multidimensional spaces.

thanks @mmagnuski for the very quick reply, here’s the full traceback:
Computing the stats…
Average across time points
(3, 21, 20484)
(21, 3, 20484)
comparing 3 groups

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-106-5ae732f93355> in <module>
     30 for i in range(n_vertices_fsave):
     31         #print('vertex ind: ', i)
---> 32     fvalues_r, pvalues_r = computeStatistic(stc_fsave_all_real_avg[:,:,i])
     33 
     34 

<ipython-input-105-53e7edfa8f5c> in computeStatistic(*args)
      7 
      8     stats, pval= f_mway_rm(np.swapaxes(args, 1, 0), factor_levels=[3],
----> 9                      effects='A', return_pvals=True)#subjectx x condition x observations 
     10     stats_array = stats
     11     pval_array = pval

~\anaconda3\envs\sourceloc\lib\site-packages\mne\stats\parametric.py in f_mway_rm(data, factor_levels, effects, correction, return_pvals)
    396     for c_, df1, df2 in _iter_contrasts(n_replications, factor_levels,
    397                                         effect_picks):
--> 398         y = np.dot(data, c_)
    399         b = np.mean(y, axis=1)[:, np.newaxis, :]
    400         ss = np.sum(np.sum(y * b, axis=2), axis=1)

<__array_function__ internals> in dot(*args, **kwargs)

ValueError: shapes (21,3,1) and (3,3) not aligned: 1 (dim 2) != 3 (dim 0)

If I run it for all the vertices I get:

ValueError: shapes (430164,3,1) and (3,3) not aligned: 1 (dim 2) != 3 (dim 0)

as expected.

You must be reshaping your data wrong at some stage, notice that if I create the data in the shape you report your data to be, I don’t get any errors:

import numpy as np
from mne.stats import f_mway_rm

data = np.random.random((21, 3, 20484))
fval, pval = f_mway_rm(data, factor_levels=[3], effects='A')

Indeed, there was a problem with the swapaxes inside the function. I swiped the axes before putting the data into the function (I had to, as my data came in shape subjects x conditions x vertices (3, 21, 20484) instead of conditions x subjects x vertices. Anyway, thanks a lot for the quick check, we ended up staring at the source code and not understanding what was going on, but never thought to use a simulated array… :crazy_face:

No problem, happy to help!