Permalink
Cannot retrieve contributors at this time
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?
test/TET_logfile_bids.m
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
131 lines (95 sloc)
4.14 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Hariri Logfile Extraction / Number of scans | |
% | |
% transfers logfiles into BIDS tsv format | |
% | |
% file history: | |
% created A. Kuehnel 18.12.2017 | |
function TET_logfile_bids(BIDS) | |
subject_path = BIDS.output.dir; | |
log = dir([subject_path,'/func/*tet*.log']); | |
logfile=readtext([subject_path,'/func/',log.name],'\t'); | |
dummies = 0; %number of volumes discarded for analysis | |
% Find last row with relevant information | |
last_row=find(strcmp(logfile(:,1),'Event Type'))-3; | |
% Convert to table | |
logfile_table = cell2table(logfile(6:last_row,1:8)); | |
Var_Names = strrep(logfile(4,1:8),' ',''); | |
logfile_table.Properties.VariableNames = Var_Names; | |
% Find zerotime | |
logfile_table.Time=logfile_table.Time/10000; | |
logfile_table.TTime=logfile_table.TTime/10000; | |
tmp = cellfun(@isempty,logfile_table.Duration); | |
logfile_table.Duration(tmp)={NaN}; | |
logfile_table.Duration=cell2mat(logfile_table.Duration)/10000; | |
Pulse_index = strcmp(cellstr(logfile_table.EventType),'Pulse'); | |
Response_index = strcmp(cellstr(logfile_table.EventType),'Response'); | |
Dummy_index = find(Pulse_index,dummies+1,'first'); | |
zero_time = logfile_table.Time(Dummy_index(end)+1); | |
logfile_table.Time=logfile_table.Time-zero_time; | |
% Find Indexes for different event types | |
temp = cellfun(@num2str, logfile_table.Code, 'UniformOutput', false); | |
Ende_index = ~cellfun(@isempty,(regexp(temp,'END'))); | |
if sum(Ende_index)==0; error('Incomplete Logfile!'); end | |
Est_index = ~cellfun(@isempty,(regexp(temp,'est'))); | |
Fix_index = ~cellfun(@isempty,(regexp(temp,'fix'))); | |
FB_correct = ~cellfun(@isempty,(regexp(temp,'correct'))); | |
FB_false = ~cellfun(@isempty,(regexp(temp,'false'))); | |
FB_uncertain = ~cellfun(@isempty,(regexp(temp,'uncert'))); | |
ITI_index = ~cellfun(@isempty,(regexp(temp,'ITI'))); | |
logfile_table.event=string(nan(size(temp))); | |
logfile_table.event(Fix_index)='fix'; | |
logfile_table.event(Est_index)='estimation'; | |
logfile_table.event(FB_correct)='fb_correct'; | |
logfile_table.event(FB_false)='fb_false'; | |
logfile_table.event(FB_uncertain)='fb_uncertain'; | |
logfile_table.event(ITI_index)='ITI'; | |
logfile_table.event(Response_index)='Motor'; | |
logfile_table.fb=nan(size(temp)); | |
logfile_table.fb(FB_correct)= 1; | |
logfile_table.fb(FB_false)= 2; | |
logfile_table.fb(FB_uncertain)= 3; | |
all_events_index=logical(Est_index+Fix_index+FB_correct+FB_false+FB_uncertain+ITI_index+Response_index); | |
% tsv/csv files for bids | |
% without Pulses and responses | |
events=logfile_table.event(all_events_index); | |
event_onsets = logfile_table.Time(all_events_index); | |
event_durations = logfile_table.Duration(all_events_index); | |
ev_ind = find(events=="estimation"); | |
% Calculate Reaction Times | |
RT = nan(length(events),1); | |
if sum(Est_index)==sum(Response_index) %If one response per trial | |
RT(events=="estimation")=logfile_table.TTime(Response_index); | |
else %In case of more responses | |
ind_est = find(Est_index); | |
ind_RT = find(Response_index); | |
for i_trial = 1:sum(Est_index) | |
RT_trial = find(ind_RT > ind_est(i_trial),1,'first'); | |
RT(ev_ind(i_trial))=logfile_table.TTime(ind_RT(RT_trial)); | |
end | |
end | |
feedback = nan(length(events),1); | |
%Get feedbacks, sometimes not all trials have feedback events. | |
if sum(Est_index)==sum(FB_correct+FB_false+FB_uncertain) | |
feedback(events=="estimation")=logfile_table.fb(logical(FB_correct+FB_false+FB_uncertain)); | |
else | |
ind_est = find(Est_index); | |
ind_est(end+1)=find(Ende_index); | |
ind_FB = find(FB_correct+FB_false+FB_uncertain); | |
for i_trial = 1:sum(Est_index) | |
fb_trial = find(ind_FB > ind_est(i_trial) & ind_FB < ind_est(i_trial+1)); | |
if ~isempty(fb_trial) | |
feedback(ev_ind(i_trial))=logfile_table.fb(ind_FB(fb_trial)); | |
end | |
end | |
end | |
log_tab=table(events,event_onsets,event_durations,RT,feedback); | |
%writetable(log, [subject_path,'/func/sub-',subject_path(end-6:end),'_task-hariri_events.csv'],'Delimiter','\t'); | |
log_cell=[log_tab.Properties.VariableNames;table2cell(log_tab)]; | |
fid = fopen([subject_path,'/func/sub-',subject_path(end-11:end-5),'_task-tet_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 | |