-
Notifications
You must be signed in to change notification settings - Fork 9
/
eeg_plottfpac.m
208 lines (180 loc) · 7.01 KB
/
eeg_plottfpac.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
function hfig = eeg_plottfpac(EEG,varargin)
hfig = [];
if nargin < 1
help eeg_plotfpact;
return;
end
EEG = pop_tfpacparams(EEG, varargin{:});
params = EEG.etc.pacplotopt.tfparam;
% Check validity of parameters
options = mystruct(varargin);
options = myrmfield( options, myfieldnames(params));
g = finputcheck( options, { ...
'plotindx' 'integer' [] 1;
'pacmethod' 'string' '' '';
'plotopt' 'cell' {} {}}, ...
'eeg_plotcomodt', 'ignore');
pacstruct = EEG.etc.eegpac(g.plotindx);
% if not pacmethod provided use the first available
if isempty(g.pacmethod)
fieldnameftmp = fieldnames(pacstruct);
tmpval = find(structfun(@(x) ~isempty(x),EEG.etc.eegpac(g.plotindx)));
tmpval2 = find(tmpval>5);
g.pacmethod = fieldnameftmp{tmpval(tmpval2)};
end
% pacval may be empty
if isempty(pacstruct.(g.pacmethod).pacval)
disp('eeg_plottfpac() error: No PAC value has been computed for the input provided.');
return;
end
pacdata = pacstruct.(g.pacmethod).pacval;
freq1vals = pacstruct.params.freqs_phase;
freq2vals = pacstruct.params.freqs_amp;
if isfield( pacstruct.(g.pacmethod),'times')
timevals = pacstruct.(g.pacmethod).times;
else
disp('eeg_plotfpac() message: Unable to generate plot. No time dimension found.');
return;
end
if params.fixfreq == 1
fixfreqval = params.freqval1;
else
fixfreqval = params.freqval2;
end
% % Pvals stuff
% pvalmask = []; flag_pval =0;
% if pacstruct.(g.pacmethod).dim ==1 && g.plotsignif % Only for dim = 1 data
% if isfield(pacstruct.(g.pacmethod),'signif') && ~isempty(pacstruct.(g.pacmethod).signif.pval)
% pvalmask = pacstruct.(g.pacmethod).signif.signifmask;
% flag_pval = 1;
% else
% disp('eeg_plotcomod message: Significance mask overlay was requested but has not been computed. This input will be disregarded');
% end
% end
if length(freq1vals)==1 || length(freq2vals)==1
error('eeg_plotfpac() error: Insuficient number of frequencies for plotting');
end
% Trimming/fixing Phase frequency values
if params.fixfreq == 1
if isempty(fixfreqval), disp('eeg_plotfpac() message: A value must be defined for the Phase frequency.'); return; end
[~, indfreq1] = min(abs(freq1vals - fixfreqval));
if isempty(indfreq1), error('eeg_plotfpac() message: Frequency value out of range'); end
pacdata = pacdata(indfreq1,:,:,:);
else
if ~isempty(params.freqrange)
disp('Trimming Phase frequencies: Looking for the closest frequencies to the ones requested');
indfreq1 = find(freq1vals > params.freqrange(1) & freq1vals < params.freqrange(2));
freq1vals = freq1vals(indfreq1);
pacdata = pacdata(indfreq1,:,:,:);
% if flag_pval
% pvalmask = pvalmask(indfreq1,:,:,:);
% end
end
end
% Trimming/fixing Amplitude frequency value
if params.fixfreq == 2
if isempty(fixfreqval), disp('eeg_plotfpac() message: A value must be defined for the Phase frequency.'); return; end
[~, indfreq2] = min(abs(freq2vals - fixfreqval));
if isempty(indfreq2), error('eeg_plotfpac() message: Frequency value out of range'); end
pacdata = pacdata(:,indfreq2,:,:);
else
if ~isempty(params.freqrange)
disp('Trimming Amplitude frequencies: Looking for the closest frequencies to the ones requested');
indfreq2 = find(freq2vals > params.freqrange(1) & freq2vals < params.freqrange(2));
freq2vals = freq2vals(indfreq2);
if isempty(indfreq2), error('eeg_plotfpac() message: Frequency value out of range'); end
pacdata = pacdata(:,indfreq2,:,:);
% if flag_pval
% pvalmask = pvalmask(:,indfreq2,:,:);
% end
end
end
% Collapsing Trials
if pacstruct.(g.pacmethod).dim == 3
pacdata = mean(pacdata,3);
end
% Trimming time dimesion
timevals = [];
if pacstruct.(g.pacmethod).dim == 2 || pacstruct.(g.pacmethod).dim == 3
% Time values
if isfield(pacstruct.(g.pacmethod),'times')
timevals = pacstruct.(g.pacmethod).times;
end
% Trimming time
if ~isempty(params.timerange) && ~isempty(timevals)
try
timendx = (timevals > params.timerange(1) & timevals < params.timerange(2));
timevals = timevals(timendx);
if pacstruct.(g.pacmethod).dim == 2
pacdata = pacdata(:,:,timendx);
else
pacdata = pacdata(:,:,:,timendx);
end
catch
disp('Unable to trim time/latency values. Please check option ''timerange''');
disp('Ignoring option ''timerange'' ......');
end
end
end
% % Plotting significance
% if flag_pval
% if ~isempty(pacstruct.(g.pacmethod).signif.signifmask)
% g.plotopt(end+1:end+2) = {'signifmask', pvalmask};
% else
% disp('eegplot pac() message: Significance mask overlay was requested but has not been computed. This input will be disregarded');
% end
% end
modes = {'Phase', 'Amplitude'} ;
if params.fixfreq == 1,
indxtitle = 2;
freqvals = freq2vals;
else indxtitle = 1;
freqvals = freq1vals;
end
title = [ modes{indxtitle} ' Vs Latency (f_{' modes{params.fixfreq} '} = ' num2str(fixfreqval) 'Hz)'];
TrialAxeTitle = [modes{indxtitle} ' Frequency (Hz)'];
h = figure('Name', ['frequency-Latency PAC (' g.pacmethod ')'] ,'Units','Normalized'); hold on;
[~,~,~,~,axhndls] = erpimage(squeeze(pacdata)',[],timevals,title,0,0,'img_trialax_label',TrialAxeTitle,'cbar', 'on','cbar_title', 'Mod. Index','erp', 'on','yerplabel', 'Marginal Mod. Ind.', g.plotopt{:}) ;
set(get(axhndls{3},'Xlabel'),'String', 'Latency (ms)');
set(axhndls{1}, 'box', 'on'); set(axhndls{2}, 'box', 'on'); set(axhndls{3}, 'box', 'on');
set(axhndls{1},'YTickLabel',round(freqvals(get(axhndls{1},'YTick'))));
set(axhndls{1},'Fontsize', 20);
set(axhndls{2},'Fontsize', 15);
set(axhndls{3},'Fontsize', 20);
htmp = get(axhndls{3},'Children');
set(htmp(1),'Fontsize', 15);
%--------------------------------------------------------------------------
% remove fields and ignore fields who are absent -------> std_erspplot
% ----------------------------------------------
function s = myrmfield(s, f);
for index = 1:length(f)
if isfield(s, f{index})
s = rmfield(s, f{index});
end
end
% convert to structure (but take into account cells) -------> std_erspplot
% --------------------------------------------------
function s = mystruct(v);
for index=1:length(v)
if iscell(v{index})
v{index} = { v{index} };
end
end
try
s = struct(v{:});
catch, error('Parameter error'); end
% convert to structure (but take into account cells) -------> std_erspplot
% --------------------------------------------------
function s = myfieldnames(v);
s = fieldnames(v);
if isfield(v, 'eeglab')
s2 = fieldnames(v.eeglab);
s = { s{:} s2{:} };
end
if isfield(v, 'fieldtrip')
s3 = fieldnames(v.fieldtrip);
for index=1:length(s3)
s3{index} = [ 'fieldtrip' s3{index} ];
end
s = { s{:} s3{:} };
end