Constructing MEG data from arrays

Hi,

I’m failing to properly construct mne-python native data objects from data arrays, and I’d appreciate advice.

I’ve used mne-python to preprocess (tSSS, artifact removal via ICA, low-pass filtering) and epoch Neuromag/Elekta data (which worked wonderfully, thanks so much for this great software and its documentation!).

I then exported the data into plain numpy arrays to try out methods for functional alignment on MEG data. It involved a bit of data wrangling, but the functionally aligned data has the same shape as the MEG data it is based on (306 sensors, in identical order).

I hoped that I would be able to transform the functionally aligned data back into a fully mne native data representation to easily perform some basic sanity checks, such as comparing plots pre and post functional alignment to check that basic signal properties are still present.
For this, I created artificial Info and Raw objects following the tutorial on generation data structures from scratch:

# recreate the channel type ordering from Elekta
ch_types = np.tile(['mag', 'grad', 'grad'], 102)
# reuse the channel names from the original Epoch object
ch_names = epochs.info['ch_names']

# create a new info object
info = mne.create_info(ch_names=ch_names,
                       sfreq=100,
                       ch_types=ch_types)
# fix the low pass filter info
info['lowpass'] = 40

# construct the raw data object. 
raw = mne.io.RawArray(data, info)

I also used the original MEG data to retrieve and set the appropriate montage:

montage = mne.channels.read_dig_fif(
        'sub-011/meg/sub-011_task-memento_cleaned_epo.fif')
raw.set_montage(montage)

At this point, raw still lacks plenty of relevant information. For example, a raw.plot_psd() call results in the Warning Channel locations not available. Disabling spatial colors and a thus greatly reduced plot, and while I can specify or find the appropriate layout (e.g., layout = mne.channels.find_layout(info)) I haven’t yet figured out where I can set this - is it possible?

Based on other people’s experiences, is there anything else I can supply the constructed data object with easily? I know that the documentation of create_info warns that

The info dictionary will be sparsely populated to enable functionality within the rest of the package. Advanced functionality such as source localization can only be obtained through substantial, proper modifications of the info structure (not recommended).

but I had hoped, because the transformations I did to the data were ultimately simple linear transformations, that “reintegrating” them into mne-python native data structures would be not completely insane.

Thanks much in advance for any advice and caveats!

1 Like

Two suggestions:

  1. use the original raw.info when creating your RawArray from your transformed data. You only need to create an Info from scratch if the original info is not suitable (e.g., if the number of channels has changed)

  2. possibly better is to use raw.apply_function(…) to directly do your transformations on the Raw object, rather than needing to extract a NumPy array and then use RawArray.

3 Likes

Thank you, this is very helpful - I will try!

1 Like