File size: 19,297 Bytes
d7989aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
%% Step2: PostProcessing data for Post-Correct and Post-Error trials: ERP analysis; Time-Frequncy analaysis, ITPC analysis

clear; clc
datalocation='D:\Project_EEG_CC\CC_Results_step1\';   % Data are here

savedir = 'D:\Project_EEG_CC\CC_PD_Figures_Manuscript\CC_Manuscript\Manuscript_Scripts_PREDICT\Data\PostError\'; % save data here

cd(savedir);



load('D:\Project_EEG_CC\mFiles\ONOFF.mat','ONOFF')

load('D:\Project_EEG_CC\mFiles\BV_Chanlocs_60.mat');



[num_cc,txt_cc,raw_cc]=xlsread('D:\Project_EEG_CC\CC_ICAs.xlsx');



PDsx=[801:811,813:823,825:829];      % 824 S2 CC is bad (mange in Step 3)

CTLsx=[8010,8070,8060,890:914];  % 911 S1 CC is bad (mange in Step 3)

%%%%%%%%% or run 824 afterwards since session 2 is bad OR use sessiosn 1

%%%%%%%%% for both OFF and ON 



%%



for subj= 801%[CTLsx(end:-1:1),PDsx(end:-1:1)] 

    for session=1:2

        if (subj>850 && session==1) || subj<850  % If not ctl, do session 2

            if 1 % exist([num2str(subj),'_Session_',num2str(session),'_PDDys_CC.mat'])~=2;

                

                % ----------------   GET PD and Control DATA ----------------   ----------------  ----------------

                % &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&                   % &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 

                disp([num2str(subj),'_Session_',num2str(session),'_PDDys_CC.mat'])

                load([num2str(subj),'_Session_',num2str(session),'_PDDys_CC.mat'],'EEG','bad_chans','bad_epochs','bad_ICAs');

                

                % Get Subj Info

                temp1=cell2mat(raw_cc(find(num_cc(:,1)==subj),session+1));

                if isnumeric(temp1)

                    bad_ICAs_To_Remove=temp1;

                elseif strmatch('NaN',temp1)

                    bad_ICAs_To_Remove=NaN;

                else

                    bad_ICAs_To_Remove=str2num(temp1);

                end

                clear temp1;

                

                % Remove the (presumptive) bad ICAs:

                if ~(isnan(bad_ICAs_To_Remove))

                    EEG = pop_subcomp( EEG, bad_ICAs_To_Remove, 0);

                end

                clear bad_ICAs_To_Remove;

                

               

                %% %%%%%%%%%%          &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& GET Epochs  % &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 



                CONGRU=[111,112,113,114,211,212,213,214];

                INCONGRU=[121,122,123,124,221,222,223,224];

                CORRECT=[101,102];

                ERROR=[103,104];

                REW=8;

                PUN=9;

                

% It will added 3 column at the EEG.epochs with TYPE, RESP and RT...this code will find STIM CONG and INCONG and Response and latency of Response



                % &&&&&&&&& Get the good info out of the epochs &&&&&&&&&&

                for aai=1:size(EEG.epoch,2)                                              % look for total size of epoch..how many epochs are there

                    EEG.epoch(aai).TYPE=NaN; EEG.epoch(aai).RESP=NaN; EEG.epoch(aai).RT=NaN; % add TYPE; RESP and RT column at the end of epoch

                    RESP_VECTOR(aai,1:2)=NaN;                                                % create variable "RESP_VECTOR" for all epochs for Correct and Error resp

                    for bbi=1:size(EEG.epoch(aai).eventlatency,2)                            % look the size of each epoch via eventlatency..

                        %%% Get STIMTYPE

                        if EEG.epoch(aai).eventlatency{bbi}==0 % If this bi is the event     % if the eventlatency shows "0"in each epoch..means data were epchoed ("0") at stim...then

                            %%% Get StimType

                            FullName=EEG.epoch(aai).eventtype{bbi};                          % look the name for the eventtype for each event/stimuli for each epoch

                            %%% IF TRN CUE  (Training Cue) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%looking for the TRAINING CUE                 

                            if any(str2num(FullName(2:end))==[CONGRU,INCONGRU])              % if any Fullname of stim match to Cong and Incong stim..then

                                EEG.epoch(aai).TYPE=str2num(FullName(2:end)) ;               % mention the full name in TYPE coulmn...ELSE..send to them in VECTOR variable

                                if any(str2num(FullName(2:end))==CONGRU)                     % if any Fullname stim match to Cong then add 5 in Vector

                                    VECTOR(aai)=55;                             % CONGRUENT

                                elseif any(str2num(FullName(2:end))==INCONGRU)               % if any Fullname stim match to Incong then add 6 in Vector

                                    VECTOR(aai)=66;                             % INCONGRUENT

                                end

%                                 %%% If anything is next

                                if  size(EEG.epoch(aai).eventlatency,2)>= bbi                 % if the size of eventtype is greater or equal to size of latency for each epoch then 

                                    %%% If RESP  &&& Simplely look for RESP after the STIM that's why eventtype+1..if there is resp after stim                            
                                    tempName=EEG.epoch(aai).eventtype{bbi+1};                 % tempname would be +1 to evenettype
                                    if any(str2num(tempName(2:end))==[CORRECT,ERROR])         % if tempname is equal to Corrcet and Error
                                        EEG.epoch(aai).RESP=str2num(tempName(2:end)) ;        % Add name for Correct and Error response
                                        EEG.epoch(aai).RT=EEG.epoch(aai).eventlatency{bbi+1}; % Add latency for Corrcet and Error Response
                                        RESP_VECTOR(aai,1)=str2num(tempName(2:end));          % Add Correct and Error stim
                                        RESP_VECTOR(aai,2)=EEG.epoch(aai).eventlatency{bbi+1};% Add latency for Correct and Erroe Response
                                    end
                                end
                            else
                                EEG.epoch(aai).TYPE=str2num(FullName(2:end)) ;             % if the epoch type is not Cong and Incong stim then
                                VECTOR(aai)=str2num(FullName(2:end));                      % create VECTOR and add the info here..if Cong (5) and Incong (6) else other stim type
                            end
                            clear FullName tempName
                        end
                    end
                end
                
   % &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
                % %% get the VECTOR here with all stimuli
                
                for aai=1:size(EEG.epoch,2)
                    EEG.epoch(aai).resp_num = 0;
                    EEG.epoch(aai).VECTOR = VECTOR(aai);
                    if any(EEG.epoch(aai).RESP==CORRECT)
                        EEG.epoch(aai).resp_num = 1;
                    elseif any(EEG.epoch(aai).RESP==ERROR)
                       EEG.epoch(aai).resp_num = 2;
                    end
                    EEG.epoch(aai).VECTOR2= EEG.epoch(aai).VECTOR;
                end
                
                % %%% find poet-error Cue: Cong and Incong
                       
                for aai=1:length(EEG.epoch)
                    %%% get post correct Cue all
                    if any(EEG.epoch(aai).resp_num == 1) 
                        if EEG.epoch(aai).VECTOR2 == 55          % cong
                            EEG.epoch(aai).VECTOR2 = 5;                          %%%% All correct Cong Cue   = 5
                        elseif  EEG.epoch(aai).VECTOR2 == 66     % Incong
                            EEG.epoch(aai).VECTOR2 = 5;                          %%%% All correct Incong Cue = 5
                        end
                    end 
                        %%%%%%%% to get post -error Cue all                
                    if any(EEG.epoch(aai).resp_num == 2) && any(EEG.epoch(aai+1).resp_num == 1)
                        if EEG.epoch(aai+1).VECTOR2 == 55          % cong
                            EEG.epoch(aai+1).VECTOR2 = 6;                       %%%% post-error Cong Cue   = 6
                        elseif  EEG.epoch(aai+1).VECTOR2 == 66     % Incong
                            EEG.epoch(aai+1).VECTOR2 = 6;                       %%%% post-error Incong Cue = 6
                        end
                    end
                    
                end
               
                VECTOR_PE= [EEG.epoch.VECTOR2]; 
                                     
       % %%%%%%%%%&&&&&&&&&&& Aggregate accelerometer data &&&&&&&&&&&&&&
                EEG.X=EEG.X-repmat(mean(EEG.X),3250,1);
                EEG.Y=EEG.Y-repmat(mean(EEG.Y),3250,1);
                EEG.Z=EEG.Z-repmat(mean(EEG.Z),3250,1);
                % Add to EEG.data as 61st channel - but not the rejected trials
                if subj==824 && session==2, clear bad_epochs; bad_epochs{1}=zeros(1,size(EEG.data,3)); end  % B/c 824 S2 is bad - hack this
                EEG.data(61,:,:)=(EEG.X(:,bad_epochs{1}~=1).^2)+(EEG.Y(:,bad_epochs{1}~=1).^2)+(EEG.Z(:,bad_epochs{1}~=1).^2);
                dims=size(EEG.data);
                
%      *******************************************************************************************************************************************************           
                
                % Lock to Response, Stim, and Cue
                respct=1;
                for ai=1:size(EEG.epoch,2)
                    if any(RESP_VECTOR(ai,1)==[CORRECT,ERROR])                                                         % look for the Correct and Error resp in Response vector
                        Cue_to_Resp=RESP_VECTOR(ai,2) ./ (1000/EEG.srate);   % convert latency into data points cueTOresp (eg. RT=770 ms; 1000 ms=500samples; 2ms=1sample; 770ms = 385 samples )
                        if isnan(Cue_to_Resp), Cue_to_Resp=1; end
                        EEG.resp(:,:,respct)=[squeeze(EEG.data(:,Cue_to_Resp:end,ai)),zeros(dims(1),(Cue_to_Resp-1))]; % get the All Cahnnles (61) X fulllength epoch (3250 smaples) X epoch numbers Resp-locked  
                        % EEG.resp is locked to Resp: see the epoch size(EEG.data(all chan :,Cue_to_Resp in time to: end of the epoch, look in all epochs in "for" loop )
                        % size of EEG.resp is different so add Zeros at the end to make size equal to EEG.data. 
                        if any(RESP_VECTOR(ai,1)==CORRECT)                                                             % if RESP_VECTOR has Correct Resp then..creat VECTOR_resp
                            VECTOR_resp(respct,1)=1; VECTOR_resp(respct,2)=Cue_to_Resp;                                % VECTOR_resp: 1st column = 1 ; 2nd column = Cue to Correct Resp in samples
                        elseif any(RESP_VECTOR(ai,1)==ERROR)                                                           % if RESP_VECTOR has Error Resp then
                            VECTOR_resp(respct,1)=2; VECTOR_resp(respct,2)=Cue_to_Resp;                                % VECTOR_resp: 1st column = 2 ; 2nd column = Cue to Error Resp in samples
                        end
                        respct=respct+1;
                        clear Cue_to_Resp;
                    end
                end
                clear RESP_VECTOR;
                
% %%%%%%%% Look for the Post-error response: only Correct Response                
  RespID = zeros(length(VECTOR_resp),3); 
  RespID(:,1) = VECTOR_resp(:,1);
  RespID(:,2) = VECTOR_resp(:,2).*2;  % convert in RT

%%%%% find post-error Cue: Cong and Incong
  for rpi=1:length(RespID(:,1))-1
      if any(RespID(rpi,1)==1) && any(RespID(rpi+1,1)==1)
           RespID(rpi+1,3)= 1;   %%%% post-correct response
      elseif  any(RespID(rpi,1)==2) && any(RespID(rpi+1,1)==1)
           RespID(rpi+1,3)= 2;   %%%% post-error response
     end
  end    
                
     VECTOR_respPE = [RespID(:,3) RespID(:,2)];
                
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                 
                % Set Times
                tx=-1500:2:4998;
                b1=find(tx==-500);  b2=find(tx==-200);  %% original 
                t1=find(tx==-500);  t2=find(tx==1000);
                tx2disp=-500:2:1000;
                
                % ------------------------ Get the goods
                X_CUE{1}=5;       % post- Correct CUE
                X_CUE{2}=6;       % post-error   CUE
                X_RESP{1}=1;      % POST-CORRECT RESP
                X_RESP{2}=2;      % POST-ERROR   RESP (correct)
                                
%% %% ###################################################################################################################################
                % ---------- % ---------- % ----------
                % ---------- TF stuff
                % ---------- % ---------- % ----------
                
                % Setup Wavelet Params
                num_freqs=50;
                frex=logspace(.01,1.7,num_freqs);
                s=logspace(log10(3),log10(10),num_freqs)./(2*pi*frex);
                t=-2:1/EEG.srate:2;
                
                % Definte Convolution Parameters
                n_wavelet = length(t);
                half_of_wavelet_size = (n_wavelet-1)/2; clear dims
                % -------- cue/fb
                dims{1} = size(EEG.data); n_data{1} = dims{1}(2)*dims{1}(3);   
                n_convolution{1} = n_wavelet+n_data{1}-1;   n_conv_pow2{1} = pow2(nextpow2(n_convolution{1}));
                % -------- resp
                dims{2} = size(EEG.resp); n_data{2} = dims{2}(2)*dims{2}(3);   
                n_convolution{2} = n_wavelet+n_data{2}-1;   n_conv_pow2{2} = pow2(nextpow2(n_convolution{2}));
                                                
                CHANS= (1:60); % ALL Channels
                for chani=1:60 
                                        
                    % get FFT of data
                    EEG_fft{1} = fft(reshape(EEG.data(CHANS(chani),:,:),1,n_data{1}),n_conv_pow2{1});
                    EEG_fft{2} = fft(reshape(EEG.resp(CHANS(chani),:,:),1,n_data{2}),n_conv_pow2{2});
                                       
                    for fi=1:num_freqs
                        
                        wavelet{1} = fft( exp(2*1i*pi*frex(fi).*t) .* exp(-t.^2./(2*(s(fi)^2))) , n_conv_pow2{1} );   
                        wavelet{2} = fft( exp(2*1i*pi*frex(fi).*t) .* exp(-t.^2./(2*(s(fi)^2))) , n_conv_pow2{2} );   
                       
                        % convolution
                        for convo=1:2
                            EEG_conv = ifft(wavelet{convo}.*EEG_fft{convo});
                            EEG_conv = EEG_conv(1:n_convolution{convo});
                            EEG_conv = EEG_conv(half_of_wavelet_size+1:end-half_of_wavelet_size);
                            EEG_multi_conv{convo} = reshape(EEG_conv,dims{convo}(2),dims{convo}(3)); clear EEG_conv;
                            temp_POWER{convo} = abs(EEG_multi_conv{convo}(t1:t2,:)).^2;  %%% -500 to 1000 ms
                        end

                        % Baseline from pre-cue  {1}
                        BASE = mean(mean(abs( EEG_multi_conv{1}(b1:b2,VECTOR_PE<7)).^2));
                       
                        % Average FIRST 
                        for condi=1:2
                            temp_POWER_avg(:,condi,1) = mean(temp_POWER{1}(:,VECTOR_PE==X_CUE{condi}),2); 
                            temp_POWER_avg(:,condi,2) = mean(temp_POWER{2}(:,VECTOR_respPE==X_RESP{condi}),2);
                            % -------------------
                            ITPC(chani,fi,:,condi,1) = abs(mean(exp(1i*(  angle(EEG_multi_conv{1}(t1:t2,VECTOR_PE==X_CUE{condi}))  )),2));
                            ITPC(chani,fi,:,condi,2) = abs(mean(exp(1i*(  angle(EEG_multi_conv{2}(t1:t2,VECTOR_respPE==X_RESP{condi}))  )),2));
                            
                        end
                         
                        % dB correct power by base
                        for condi=1:2
                            for event=1:2
                                POWER(chani,fi,:,condi,event) = 10*( log10(temp_POWER_avg(:,condi,event)) - log10(repmat(BASE,size(temp_POWER_avg(:,condi,event),1),1)) );
                            end
                        end
                        
                        % Actually, save these for later
                        Baselines(chani,fi,1)=BASE;
                                            
                        clear temp* EEG_multi_conv wavelet BASE PE;
                        
                    end
                    clear *_fft;
                end
                
%% ##############################################################################################################                
                % ---------- % ---------- % ----------
                % ---------- ERP stuff
                % ---------- % ---------- % ----------  
                
                % Filter for ERPs
                dims=size(EEG.data);
                EEG.data=eegfilt(EEG.data,500,[],20); EEG.data=reshape(EEG.data,dims(1),dims(2),dims(3));
                
                dims=size(EEG.resp);
                EEG.resp=eegfilt(EEG.resp,500,[],20); EEG.resp=reshape(EEG.resp,dims(1),dims(2),dims(3));

                % Basecor your ERPs here so they are pretty ------------>
                BASE1=squeeze(  mean(EEG.data(:,b1:b2,:),2)  );
                BASE2=squeeze(  mean(EEG.resp(:,b1:b2,:),2)  );
                
                for chani=1:dims(1)   %% for all channels 
                    EEG.data(chani,:,:)=squeeze(EEG.data(chani,:,:))-repmat( BASE1(chani,:),dims(2),1 );
                    EEG.resp(chani,:,:)=squeeze(EEG.resp(chani,:,:))-repmat( BASE2(chani,:),dims(2),1 );
                end
               
                for condi=1:2   %% for both conditions or conflicts
                    % Mean for ERPs
                    ERPs(1:60,:,condi,1)=mean(EEG.data(1:60,t1:t2, VECTOR_PE==X_CUE{condi} ),3);         %%% PreCue-locked   ERPs
                    ERPs(1:60,:,condi,2)=mean(EEG.resp(1:60,t1:t2, VECTOR_respPE==X_RESP{condi} ),3);    %%% Response-locked ERPs
                    
                end
                %%%% save data                                           
                save([savedir,num2str(subj),'_Session_',num2str(session),'_PDDys_CC_ALL_GOODS.mat'],...
                    'ERPs','VECTOR','VECTOR_PE','VECTOR_resp','VECTOR_respPE','POWER','ITPC','Baselines');
                
                clearvars -except datalocation ONOFF PDsx CTLsx session subj num_cc txt_cc raw_cc savedir
                
            end
        end
    end
end