FOOOF fg.get_params('peak_params') ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (20,) + inhomogeneous part.

  • MNE version: 1.5.1_0
  • operating system: macOS 14.5

Hello, iā€™m trying to apply a FOOOFGroup method on a matrix of 20 PSD with 201 point of frequencies.
When iā€™m runing the ā€˜fg.group_resultsā€™ iā€™v this kind of message - that i also have when i run in details the ā€˜get_paramsā€™ function ā€˜peak_paramsā€™. Thatā€™s not occurs with the ā€˜aperiodic_paramsā€™ and ā€˜exponentā€™.

File ~/Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/fooof/objs/group.py:379 in get_params
out = np.array([np.insert(getattr(data, name), 3, index, axis=1)

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (20,) + inhomogeneous part.

I think the probleme should be in my datas arrangement or in the function get_params() but iā€™m begginer in python and affraid to modify a code writed by professionals :slight_smile:

Thanks you very much for your attention to my problem ! :slight_smile:

Hi,

Based on your description of the data and the error message it seems like you donā€™t have the same frequency dimensions across all your PSDs.

Could you share the code you are using (and add some diagnostics about the data dimensions if you donā€™t have any)?

Cheers,

Hi, Thanks you very much for your answer.
My PSD array have a dimension of (20,201). And for the frequencies iā€™m using an array of float64 (201,).

There is the code :

#FOOOFGroup accross a group of Power Spectra (eg : PSD_DCZ)
#Initialize a FOOOFGroup object, specifying some parameters
fg = FOOOFGroup(peak_width_limits=[1.0, 12], max_n_peaks=8)
#Fit FOOOF model across the matrix of power spectra
fg.fit(freq, PSD_DCZ, freq_range)
fg.print_results() #probleme display here

#Create and save out a report summarizing the results across the group of power spectra
fg.save_report()
#Save out FOOOF results for further analysis later
fg.save(file_name=ā€˜fooof_group_resultsā€™, save_results=True)
fg.plot()

#Check why the FOOOFGroup Peak-centerfrequ doesnā€™t work :
print(fg.group_results)
test = fg.group_results #check what group_result contain

#Extract aperiodic parameters
aps = fg.get_params(ā€˜aperiodic_paramsā€™) #ok
exps = fg.get_params(ā€˜aperiodic_paramsā€™, ā€˜exponentā€™) #ok

#Extract peak parameters
peaks = ft.get_params(ā€˜peak_paramsā€™)# out = np.array([np.insert(getattr(data, name), 3, index, axis=1) ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (20,) + inhomogeneous part.
cfs = fg.get_params(ā€˜peak_paramsā€™, ā€˜CFā€™)# same error

#Extract goodness-of-fit metrics
errors = fg.get_params(ā€˜errorā€™) #ok
r2s = fg.get_params(ā€˜r_squaredā€™) #ok

And there is the code of the function get_params() :

def get_params(self, name, col=None):
if not self.has_model:
raise NoModelError(ā€œNo model fit results are available, can not proceed.ā€)

    # Allow for shortcut alias, without adding `_params`
    if name in ['aperiodic', 'peak', 'gaussian']:
        name = name + '_params'

    # If col specified as string, get mapping back to integer
    if isinstance(col, str):
        col = get_indices(self.aperiodic_mode)[col]
    elif isinstance(col, int):
        if col not in [0, 1, 2]:
            raise ValueError("Input value for `col` not valid.")

    # Pull out the requested data field from the group data
    # As a special case, peak_params are pulled out in a way that appends
    #  an extra column, indicating which FOOOF run each peak comes from
    if name in ('peak_params', 'gaussian_params'):
        
        out = np.array([np.insert(getattr(data, name), 3, index, axis=1)
                        for index, data in enumerate(self.group_results)])
        # This updates index to grab selected column, and the last column
        #  This last column is the 'index' column (FOOOF object source)
        if col is not None:
            col = [col, -1]
    else:
        out = np.array([getattr(data, name) for data in self.group_results])

    # Some data can end up as a list of separate arrays
    #   If so, concatenate it all into one 2d array
    if isinstance(out[0], np.ndarray):
        out = np.concatenate([arr.reshape(1, len(arr)) \
            if arr.ndim == 1 else arr for arr in out], 0)

    # Select out a specific column, if requested
    if col is not None:
        out = out[:, col]

    return out

So, you are actually referencing the FOOOF code here? Sorry, I didnā€™t get it before. You shouldnā€™t try to change that code on your own.

If you are confident that your PSDs are ok, then I donā€™t think that your problem is related to MNE at all and neither to your data (although you havenā€™t shared any code on data processing). This should be a bug in the FOOOF toolbox and you should consider filing a bug report there. Thereā€™s already at least one related bug, and a workaround is listed in the discussion. Maybe it will for you.

Please also consider better formatting your code - itā€™s not very easy to read in its current state.

Cheers,

Okay thank you iā€™m not usually using github and forums and didnā€™t know about the Issues tools on the FOOOF github.

Thanks a lot for your help !

1 Like