This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
analysis:nsb2014:week8 [2014/08/01 10:04] mvdm [Creating trial data in FieldTrip] |
analysis:nsb2014:week8 [2022/06/29 19:01] (current) mvdm |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ~~DISCUSSION~~ | ||
- | |||
- | Under construction! | ||
- | |||
===== Time-frequency analysis: spectrograms ===== | ===== Time-frequency analysis: spectrograms ===== | ||
Line 306: | Line 302: | ||
cfg.mode = 'nlx'; | cfg.mode = 'nlx'; | ||
cfg.hdr = data.hdr; | cfg.hdr = data.hdr; | ||
- | cfg.twin = [-0.5 3.5]; | + | cfg.twin = [-1 4]; |
trl = ft_maketrl(cfg); | trl = ft_maketrl(cfg); | ||
Line 315: | Line 311: | ||
</code> | </code> | ||
- | The above reorganizes the data into 4-second windows (from -0.5 to 3.5 seconds) around nosepoke times (''evt.n0'' and ''evt.n1''). ''ft_maketrl()'' converts the event times from ''LoadEvents()'' into the data indices that ''ft_redefinetrial()'' needs. | + | The above reorganizes the data into 4-second windows (from -1 to 4 seconds) around nosepoke times (''evt.n0'' and ''evt.n1''). ''ft_maketrl()'' converts the event times from ''LoadEvents()'' into the data indices that ''ft_redefinetrial()'' needs. |
☛ Inspect the ''data_trl'' structure. Notice that there are now 115 trials instead of 1, and that each trial has its own time and data vectors. Plot the 47th trial as an example to verify this. | ☛ Inspect the ''data_trl'' structure. Notice that there are now 115 trials instead of 1, and that each trial has its own time and data vectors. Plot the 47th trial as an example to verify this. | ||
Line 331: | Line 327: | ||
cfg.foi = 10:2:100; % frequencies of interest | cfg.foi = 10:2:100; % frequencies of interest | ||
cfg.t_ftimwin = ones(size(cfg.foi)).*0.5; % window size: fixed at 0.5s | cfg.t_ftimwin = ones(size(cfg.foi)).*0.5; % window size: fixed at 0.5s | ||
- | cfg.toi = -1:0.05:4; % times of interest | + | cfg.toi = -0.5:0.05:3.5; % times of interest |
TFR = ft_freqanalysis(cfg, data_trl); | TFR = ft_freqanalysis(cfg, data_trl); | ||
Line 344: | Line 340: | ||
You should get: | You should get: | ||
- | {{ :analysis:course:week6_fig4.png?600 |}} | + | {{ :analysis:nsb2014:week6_fig4.png?600 |}} |
- | This plot is an //average// spectrogram over 88 trials, with time 0 corresponding to the time of nosepoking into the reward receptacle. As with the examples using ''spectrogram()'', we can control the smoothness of the spectrogram by specifying the time and frequency steps. For the frequency steps this is accomplished the same way in FieldTrip, but for the time axis we can now simply specify a vector with the window centers instead of a window size and overlap. | + | This plot is an //average// spectrogram over 115 trials, with time 0 corresponding to the time of nosepoking into the reward receptacle. As with the examples using ''spectrogram()'', we can control the smoothness of the spectrogram by specifying the time and frequency steps. For the frequency steps this is accomplished the same way in FieldTrip, but for the time axis we can now simply specify a vector with the window centers instead of a window size and overlap. |
Looking at the results, it appears there are some beta (~20Hz) and gamma events visible in the average spectrogram. | Looking at the results, it appears there are some beta (~20Hz) and gamma events visible in the average spectrogram. | ||
- | ☛ Change the ''cfg.toi'' and ''cfg.foi'' parameters to verify you can change the resolution of the time and frequency axes. What happens if you try to extend the time axis from -2 to 5? | + | ☛ Change the ''cfg.toi'' and ''cfg.foi'' parameters to verify you can change the resolution of the time and frequency axes. What happens if you try to extend the time axis from -2 to 5? What would you do if you wanted data for that extended range? |
=== Options and parameters for FieldTrip spectrograms === | === Options and parameters for FieldTrip spectrograms === | ||
Line 379: | Line 375: | ||
You should now have a beautiful image that balances time and frequency resolution: | You should now have a beautiful image that balances time and frequency resolution: | ||
- | {{ :analysis:course:week6_fig5.png?600 |}} | + | {{ :analysis:nsb2014:ft_specgram2.png?600 |}} |
☛ What causes the whitespace at the bottom of the image? | ☛ What causes the whitespace at the bottom of the image? | ||
Line 401: | Line 397: | ||
cfg.t_ftimwin = 20./cfg.foi; % 20 cycles per time window | cfg.t_ftimwin = 20./cfg.foi; % 20 cycles per time window | ||
- | cfg.toi = -2:0.05:0; % pre-nosepoke baseline | + | cfg.toi = -1:0.05:0; % pre-nosepoke baseline |
TFR_pre = ft_freqanalysis(cfg, data_trl); | TFR_pre = ft_freqanalysis(cfg, data_trl); | ||
- | cfg.toi = 0:0.05:2; % post-nosepoke | + | cfg.toi = 0:0.05:1; % post-nosepoke |
TFR_post = ft_freqanalysis(cfg, data_trl); | TFR_post = ft_freqanalysis(cfg, data_trl); | ||
Line 447: | Line 443: | ||
<code matlab> | <code matlab> | ||
+ | %% load the data | ||
fc = FindFiles('*.ncs'); % get filenames of all LFPs recorded | fc = FindFiles('*.ncs'); % get filenames of all LFPs recorded | ||
data = ft_read_neuralynx_interp(fc); % load them all -- this will take a while | data = ft_read_neuralynx_interp(fc); % load them all -- this will take a while | ||
+ | data_all.hdr.Fs = data_all.fsample; % for some reason this is missing from the header | ||
- | % define layout | + | %% define layout for later plotting |
cfg = []; | cfg = []; | ||
cfg.layout = 'ordered'; cfg.channel = data.label; | cfg.layout = 'ordered'; cfg.channel = data.label; | ||
Line 461: | Line 459: | ||
<code matlab> | <code matlab> | ||
+ | %% | ||
cfg = []; | cfg = []; | ||
- | cfg.trialfun = 'ft_trialfun_lineartracktone2'; | + | cfg.t = cat(2,getd(evt,'n0'),getd(evt,'n1')); |
- | cfg.trialdef.hdr = data.hdr; | + | cfg.mode = 'nlx'; |
- | cfg.trialdef.pre = 2.5; | + | cfg.hdr = data_all.hdr; |
- | cfg.trialdef.post = 5; | + | cfg.twin = [-1 4]; |
- | cfg.trialdef.eventtype = 'nosepoke'; % could be 'nosepoke', 'reward', 'cue' | + | trl = ft_maketrl(cfg); |
- | cfg.trialdef.location = 'both'; % could be 'left', 'right', 'both' | + | |
- | cfg.trialdef.block = 'both'; % could be 'value', 'risk' | + | |
- | cfg.trialdef.cue = {'c1','c3','c5'}; % cell array with choice of elements {'c1','c3','c5','lo','hi'} | + | |
- | [trl, event] = ft_trialfun_lineartracktone2(cfg); | + | cfg = []; |
cfg.trl = trl; | cfg.trl = trl; | ||
- | + | data_trl = ft_redefinetrial(cfg,data_all); | |
- | data_trl = ft_redefinetrial(cfg,data); | + | |
%% | %% | ||
- | cfg = []; | + | cfg = []; % start with empty cfg |
cfg.output = 'pow'; | cfg.output = 'pow'; | ||
- | %cfg.channel = 'R016-2012-10-03-CSC04a'; | ||
cfg.method = 'mtmconvol'; | cfg.method = 'mtmconvol'; | ||
cfg.taper = 'hanning'; | cfg.taper = 'hanning'; | ||
- | cfg.foi = 1:1:100; | + | cfg.foi = 1:100; % frequencies of interest |
- | cfg.keeptrials = 'yes'; % should be needed for subsequent statistics... | + | cfg.toi = -0.5:0.05:3.5; % times of interest |
- | cfg.t_ftimwin = 20./cfg.foi; % 20 cycles per time window | + | cfg.t_ftimwin = 20./cfg.foi; |
- | cfg.toi = -1:0.05:4; | + | |
TFR = ft_freqanalysis(cfg, data_trl); | TFR = ft_freqanalysis(cfg, data_trl); | ||
</code> | </code> | ||
Line 505: | Line 498: | ||
You should get: | You should get: | ||
- | {{ :analysis:course:week6_fig6.png?600 |}} | + | {{ :analysis:nsb2014:ft_specgram3.png?600 |}} |
This shows the baseline-corrected, event-aligned spectrograms for all 16 channels in this session. There is also an average shown (the subplot on the lower right). Note that a subset of the channels, presumably located in the ventral striatum, show a very similar time-frequency pattern, whereas another subset do not show this at all. The channels that do not show this same pattern are likely located in the hippocampus. | This shows the baseline-corrected, event-aligned spectrograms for all 16 channels in this session. There is also an average shown (the subplot on the lower right). Note that a subset of the channels, presumably located in the ventral striatum, show a very similar time-frequency pattern, whereas another subset do not show this at all. The channels that do not show this same pattern are likely located in the hippocampus. | ||
- | + | ==== Exercise ==== | |
- | ==== Assignment ==== | + | |
Because the event-triggered spectrograms above show an average over trials, it is possible to lose touch with the properties of the raw data that generated it. For instance, the average may arise from a distribution of very similar looking individual trials, or it may be dominated by one atypical but extreme trial. For this reason it is important to compare the spectrogram to the raw data. | Because the event-triggered spectrograms above show an average over trials, it is possible to lose touch with the properties of the raw data that generated it. For instance, the average may arise from a distribution of very similar looking individual trials, or it may be dominated by one atypical but extreme trial. For this reason it is important to compare the spectrogram to the raw data. | ||
Line 515: | Line 507: | ||
An example of a study where this was done well is [[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3463873/ | Leventhal et al]]. Neuron 2012. [[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3463873/figure/F3/ | Figure 3]] in this paper shows a number of event-triggered LFP traces, one for each trial. | An example of a study where this was done well is [[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3463873/ | Leventhal et al]]. Neuron 2012. [[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3463873/figure/F3/ | Figure 3]] in this paper shows a number of event-triggered LFP traces, one for each trial. | ||
- | ☛ Implement a function eventLFPplot(csc,event_times,varargin) to make such a plot, as follows: | + | ☛ Implement a function eventLFPplot(cfg,csc) to make such a plot. The ''cfg'' variable should contain an ''eventTimes'' field which is required, and optionally accept a ''window'' field to override the default window of [-1 3] seconds relative to the event times. |
- | + | ||
- | <code matlab> | + | |
- | % function eventLFPplot(csc,event_times,varargin) | + | |
- | % | + | |
- | % INPUTS | + | |
- | % | + | |
- | % csc: [1 x 1] mytsd, LFP signal to be plotted | + | |
- | % event_times: [nEvents x 1] double with event times to align LFP on | + | |
- | % | + | |
- | % varargins (with defaults): | + | |
- | % | + | |
- | % t_window: [2 x 1] double indicating time window to use, e.g. [-1 3] for 1 second before to 3 seconds after event times | + | |
- | + | ||
- | </code> | + | |
You can use a skeleton like the following: | You can use a skeleton like the following: |