FieldTrip

From MEG Wiki
Jump to navigation Jump to search

Introduction

FieldTrip is a Free and Open Source MATLAB based toolbox for MEEG analysis. It is developed by the Donders Institute for Brain, Cognition and Behaviour. FieldTrip can read and work with all commercial MEG data formats. It can do all parts of analyses, preprocessing, artifact removal, averaging, source analyses and much more.

Importing Neuromag(*.fif) in FieldTrip

Fieldtrip can import neuromag data but there is a special way to do this. Follow the following commands to read and import your Neuromag306 data in FieldTrip and work on it as a Matlab data structure.

 hdr = ft_read_header('cu1_AudioRun1_raw.fif')
 dat = ft_read_data('cu1_AudioRun1_raw.fif')
 Fevents = ft_read_event('cu1_AudioRun1_raw.fif')
 cfg.headerfile = 'cu1_AudioRun1_raw.fif'
 cfg.inputfile = 'cu1_AudioRun1_raw.fif'
 cfg.dataset = 'cu1_AudioRun1_raw.fif'
 cfg = ft_definetrial(cfg)
 To define the trials
   F = []
   [len, ~] = size(Fevents);
   for i = 1:len
      %disp(Fevents(i).sample);
      F = [F, Fevents(i).sample];
   end
   F = F';
   endS = F(:,1);
   begS = F(:,1);
   endS = endS(2:len);
   begS = begS(1:len-1);
   offset = zeros(1,len-1)';
   samples = horzcat(begS, endS, offset)
 cfg.trl = samples

Artifact Removal

ICA Artifact Removal

See the ICA for ECG page for information on how to apply ICA projection on your raw data to remove ECG - heart beat artifact.
Useful mailists answers: http://mailman.science.ru.nl/pipermail/fieldtrip/2012-April/005011.html

Converting FieldTrip to Neuromag Raw FIFF

Once you are satisfied with the necessary data processing steps in the FieldTrip environment in MATLAB, you can write the raw data back to the Neuromag FIFF structure that can be read by mne.

Steps to Follow

  • Open Matlab
  • Have the data struct ready after you are done with FT processing(eg: ft_preprocessing, ft_resampledata etc.,)
  • ft_defaults
  • fifffile = [pathstr filesep name '.fif'];
  • data = ft_checkdata(data, 'datatype', {'raw', 'timelock'}, 'feedback', 'yes');
  • info = data.hdr.orig;
  • [outfid, cals] = fiff_start_writing_raw(fifffile, info);
  • fiff_write_raw_buffer(outfid, data.trial, cals);
    • NOTE that the channel data structures must match the buffer and the calibration struc obtained from the data.hdr struct
    • Make sure you are writing all the trials into the raw fiff file. For this purpose, you can set one sample to hold the entire time period.
  • fiff_finish_writing_raw(outfid);

Time Lock analysis

When you have pre-processed your data and have run the ICA analysis to clean the data you are now ready to proceed to the sensor level analysis. This is done by calling the ft_timelockanalysis and ft_timelockgrandaverage commands in FieldTrip. Follow the commands below for computing the average across runs within a subject and for computing the average of a run for a specified trial condition.

Within Subject Average

cfg = []
cfg.channel = meg
cfg.vartrllength = 1 % Accept variable length trials
tl_run1 = ft_timelockanalysis(cfg, run1.data_clean)
tl_run2 = ft_timelockanalysis(cfg, run2.data_clean)
cfg = []
cfg.keepindividual = 'no'
cfg.channel = meg
cfg.method = 'within'
cfg.parameter = 'avg'
[tl_all] = ft_timelockgrandaverage(cfg, tl_run1, tl_run2)

Plotting Averages

cfg = []
cfg.showoutline = 'yes'
cfg.layout = 'neuromag306mag.lay'
cfg.xlim = [0.0 0.5] You can edit the time window here
ft_multiplotER(cfg, tl_all)

Source Analyses

Preparing Anatomical and Spatial Information

ft_defaults
[mri] = ft_read_mri('MPRAGE.nii')

Common Issues

1) ICA on Neuromag MEG: runica error

Problem: surf, X, Y, Z, S cant be complex
Solution: Do not include the Reference channel in the cfg or the other option is to specify the components to be 1 less than the number of channels you would like in the component analysis. Read the links below for more detailed info and for commands to included in your script.
http://mailman.science.ru.nl/pipermail/fieldtrip/2013-October/007067.html
http://fieldtrip.fcdonders.nl/faq/why_does_my_ica_output_contain_complex_numbers

2) fiff_start_file: Permission denied

See here: http://mailman.science.ru.nl/pipermail/fieldtrip/2014-March/007748.html