Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
% logfile extraction script for FOR/FOK cases of I.S.T.
% - only for cases without the flip/logfile extraction issue
% - also saves .tsv file
% - split time bug fixed IE 08/2017
% adapted from A. Kuehnel
% M. Czisch 25.1.2018
function IST_logfile_flipped_bids(BIDS)
subject_path = BIDS.input.dir;
log = dir([subject_path,'/LOG/*IST_final*.log']);
%clearvars -except m root subs tracksep1 tracksep2
logfile=readtext([subject_path,'/LOG/',log.name],'\t');
string_logfile = cellfun(@num2str, logfile(:,4), 'UniformOutput', 0);
ende = find(strcmp(string_logfile, 'startpause'));
%Check if logfile is complete
ende_ind = find(strcmp(string_logfile, 'last_frame'));
if isempty(ende_ind); error('Logfile incomplete!');end
f = cell2table(logfile(6:ende(end)+2,1:8));
Var_Names = strrep(logfile(4,1:8),' ','');
f.Properties.VariableNames = Var_Names;
f.Code = string(f.Code);
% reduce table to relevant columns and remove "pulse" code
t1 = f(:,{'Code','Time'});
t1.Time = t1.Time/10000;
% Start of first pulse is zerotime -> substracted from all further events
%tmp = t1{t1.Code == "miniblock",2}(1);
tmp1 = t1{t1.Code == "10",2};
tmp = tmp1(1);
t1{:,2} = t1{:,2} - tmp;
pulses = t1{t1.Code == "10",2}; % for major separator action
t1 = t1(f.Code ~= "10",:); % now remove 'pulse' codes
% extract onsets: onst1 = calculus; onst2 = motor; onst3 = fb_correct; onst4 =
% fb_false; onst5 = fb_timeout
t11 = t1(t1.Code ~= "1" & t1.Code ~= "2" & t1.Code ~= "3",:);
tmp1 = find(t11.Code == "startpause") -1;
tmp2 = find(t11.Code == "verbf_long") -1;
tmp3 = [tmp1;tmp2];
tmp3 = sort(tmp3);
tmp = t11.Code == "fb_correct";
tmp(tmp3) = 0;
onst11 = t11{tmp,2} + 6000 ;
tmp = t11.Code == "fb_false";
tmp(tmp3) = 0;
onst12 = t11{tmp,2} + 6000;
tmp = t11.Code == "fb_timeout";
tmp(tmp3) = 0;
onst13 = t11{tmp,2} + 6000;
tmp = t11.Code == "miniblock";
tmp = t11{tmp,2} + 1666;
onst1 = [onst11; onst12; onst13; tmp];
onst1 = sort(onst1);
nr_calc = repmat({'calc'},length(onst1),1);
% Calculate drt1
for i = 1:numel(onst1)
drt1(i) = min(t1.Time(t1.Time > onst1(i))) - onst1(i);
end
% motor onsets are a bit tricky: because multiple buttons and variable
% ammount of hits and sometimes motoractivity soutside of calculus answer
% subission
onst2 = zeros(size(t1.Code,1),1);
tmp = find(strncmp(t1.Code, "fb_", 3)) + 1;
onst2(tmp) = 1;
rm1 = strncmp(t1.Code, "antic_", 6);
rm2 = t1.Code == "startpause";
rm3 = strncmp(f.Code, "verbf", 5);
rm4 = find(t1.Code == "startpause") - 1;
onst2(rm1) = 0;
onst2(rm2) = 0;
onst2(rm3) = 0;
onst2(rm4) = 0;
onst2 = t1{onst2,2};
tmp = t1{strncmp(t1.Code, "antic_", 6),2};
for i = 1:numel(onst2)
drt2(i) = min(tmp(tmp > onst2(i))) - onst2(i);
end
nr_motor = repmat({'Motor'},length(onst2),1);
% % motor onsets are a bit tricky: because multiple buttons and variable
% % amount of hits and sometimes motor activity outside the calculus answer
% % submission
%
% tmp = find(tmp == 1)+1; % what ever follows calculus presentation is a first motor event
% tmp1 = find(strncmp(t1.Code, "antic", 5)); % unless no motor response was given, then it is a "antic_" event
%
% ind = tmp ~= tmp1; % determine which events following a calculus event are not antic events
% tmp = tmp(ind); % and exclude all others
%
% onst2 = t1{tmp,2};
% antic and feedback events will be concatinated and treated as one
% for designfiles
onst3 = t1{t1.Code == 'antic_correct', 2};
onst4 = t1{t1.Code == 'antic_false', 2};
onst5 = t1{t1.Code == 'antic_timeout', 2};
a_correct = ones(length(onst3),1);
nr_antic_corr = repmat({'antic_correct'},length(onst3),1);
a_false = repmat(2,length(onst4),1);
nr_antic_false = repmat({'antic_false'},length(onst4),1);
a_timeout = repmat(3,length(onst5),1);
nr_antic_timeout = repmat({'antic_timeout'},length(onst5),1);
tmp = find(strncmp(t1.Code, "verbf", 5));
onst9 = t1.Time(tmp);
nr_vfb = repmat({'vb_feedback'},3,1);
%separate feedback events for .tsv
onst6 = t1{t1.Code == 'fb_correct', 2};
onst7 = t1{t1.Code == 'fb_false', 2};
onst8 = t1{t1.Code == 'fb_timeout', 2};
nr_fb_corr = repmat({'fb_correct'},length(onst6),1);
nr_fb_false = repmat({'fb_false'},length(onst7),1);
nr_fb_timeout = repmat({'fb_timeout'},length(onst8),1);
% extract durations
% calculus durations are defined as begin calculus until first motor response
tmp = strncmp(t1.Code, "calculus", 8);
drt1 = t1{find(tmp==1) +1,2} - t1{find(tmp==1),2};
% define drt2 as any 'antic_*' event - whatever followed the preceding calculus event
tmp1 = strncmp(t1.Code, "antic", 5);
drt2 = t1{find(tmp1==1),2} - t1{find(tmp==1) +1,2};
drt2 = drt2(ind); % use indicator of missing motorevents to remove 0 durations
% define drt3/4/5 as respective 'fb' event - preceeding 'antic' event
tmp2 = t1.Code == "antic_correct";
tmp3 = t1.Code == "fb_correct";
drt3 = t1{find(tmp3==1),2} - t1{find(tmp2==1),2};
drt6 = t1{find(tmp3==1)+1,2} - t1{find(tmp3==1),2};
drt3_des = t1{find(tmp3==1)+1,2} - t1{find(tmp2==1),2}; %concetaned antic+feedback for design
% drt3tst = t1{find(tmp3==1) +1,2} - t1{find(tmp3==1),2};
tmp2 = t1.Code == "antic_false";
tmp3 = t1.Code == "fb_false";
drt4 = t1{find(tmp3==1),2} - t1{find(tmp2==1),2};
drt7 = t1{find(tmp3==1)+1,2} - t1{find(tmp3==1),2};
drt4_des = t1{find(tmp3==1)+1,2} - t1{find(tmp2==1),2};
% drt4tst = t1{find(tmp3==1) +1,2} - t1{find(tmp3==1),2};
tmp2 = t1.Code == "antic_timeout";
tmp3 = t1.Code == "fb_timeout";
drt5 = t1{find(tmp3==1),2} - t1{find(tmp2==1),2};
drt8 = t1{find(tmp3==1)+1,2} - t1{find(tmp3==1),2};
drt5_des = t1{find(tmp3==1)+1,2} - t1{find(tmp2==1),2};
% duration of verbal feedback is 10 or 15 seconds
tmp4 = strncmp(t1.Code, "verbf", 5);
drt9(1) = 100000; drt9(2) = 150000; drt9(3) = 150000;
% build tsv file inclusive feedback vector (1=correct 2=false 3=timeout)
[event_onsets,idx] = sort([onst1;onst2;onst3;onst4;onst5;onst6;onst7;onst8;onst9]);
event_durations = [drt1;drt2;drt3;drt4;drt5;drt6;drt7;drt8;drt9'];
events = [nr_calc;nr_motor;nr_antic_corr;nr_antic_false;nr_antic_timeout;nr_fb_corr;nr_fb_false;nr_fb_timeout;nr_vfb];
event_durations = event_durations(idx);
events = events(idx);
log_table=table(events,event_onsets,event_durations);
log_table.events=string(log_table.events);
[~,idx1]=sort([onst3;onst4;onst5]);
fb = [a_correct;a_false;a_timeout];
fb = fb(idx1);
feedback = nan(size(log_table,1),1);
feedback(log_table.events=="calc")=fb;
RT = nan(size(log_table,1),1);
RT(feedback==1|feedback==2,1)=event_durations(feedback==1|feedback==2);
log_table = [log_table,table(RT,feedback)];
log_cell=[log_table.Properties.VariableNames;table2cell(log_table)];
fid = fopen([subject_path,'/func/sub-',subject_path(end-6:end),'_task-ist_events.tsv'],'wt');
if fid>0
fprintf(fid, '%s\t%s\t%s\t%s\t%s\n',log_cell{1,:});
for k=2:size(log_cell,1)
fprintf(fid,'%s\t%f\t%f\t%f\t%f\n',log_cell{k,:});
end
fclose(fid);
end
end