Skip to content

combine_fooofs fails when there are "gaps" in frequencies #237

@cusinatr

Description

@cusinatr

Hi guys,

I found an error when using the above function inside the fooof.objs.utils module, if a FOOOFGroup object is created by non-consecutive frequencies (as can happen when notch-filtering for example).

A minimal code to reproduce the error is the following

import numpy as np
from fooof import FOOOFGroup
from fooof.objs.utils import combine_fooofs

freqs = np.array([1, 2, 3, 5, 6])   # missing 4
PSD = np.random.randn(5, len(freqs)) ** 2

fg = FOOOFGroup()
fg.fit(freqs, PSD)
fgs = combine_fooofs([fg])

And the error is:

Exception has occurred: ValueError
all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 6 and the array at index 1 has size 5

The problem stems in the combine_fooofs function where the frequencies and PSDs are initialized as:

n_freqs = len(gen_freqs(meta_data.freq_range, meta_data.freq_res))
temp_power_spectra = np.empty([0, n_freqs])

Hence, ending up with more frequencies than there actually are and causing the error in the np.vstack function. A possible solution could be to get rid of the initial definitions and initialize from the first object in the passed list, as:

meta_data = fooofs[0].get_meta_data()

  # Add FOOOF results from each FOOOF object to group
  for i, f_obj in enumerate(fooofs):

      # Add FOOOFGroup object
      if isinstance(f_obj, FOOOFGroup):
          fg.group_results.extend(f_obj.group_results)
          if f_obj.power_spectra is not None:
              if i == 0:
                  temp_power_spectra = f_obj.power_spectra
              else:
                  temp_power_spectra = np.vstack([temp_power_spectra, f_obj.power_spectra])

Thanks again for the wonderful tool!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions