-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy patha_ophakker.m
104 lines (93 loc) · 4.03 KB
/
a_ophakker.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
% this demo code is part of Titta, a toolbox providing convenient access to
% eye tracking functionality using Tobii eye trackers
%
% Titta can be found at https://github.com/dcnieho/Titta. Check there for
% the latest version.
% When using Titta, please cite the following paper:
%
% Niehorster, D.C., Andersson, R. & Nystrom, M., (2020). Titta: A toolbox
% for creating Psychtoolbox and Psychopy experiments with Tobii eye
% trackers. Behavior Research Methods.
% doi: https://doi.org/10.3758/s13428-020-01358-8
clear variables; clear global; clear mex; close all; fclose('all'); clc
dbstop if error % for debugging: trigger a debug point when an error occurs
% setup directories
myDir = fileparts(mfilename('fullpath'));
cd(myDir);
cd data; dirs.data = cd;
cd mat; dirs.mat = cd; % directory where mat files with data generated by the readme files should be placed
cd ..;
if ~isdir('msgs_ophak') %#ok<*ISDIR>
mkdir(fullfile(cd,'msgs_ophak'));
end
cd msgs_ophak; dirs.msgsO = cd;
cd ..;
if ~isdir('samples_ophak')
mkdir(fullfile(cd,'samples_ophak'));
end
cd samples_ophak; dirs.samplesO = cd;
cd ..;
cd ..; cd function_library; dirs.funclib = cd;
cd ..;
cd stimuli; dirs.stims = cd;
cd ..;
addpath(genpath(dirs.funclib)); % add dirs to path
%%% cut up the data file into tasks
[files,nfiles] = FileFromFolder(dirs.mat,[],'mat');
if 0
% filter so we only get data that matches the filter. uses regexp
filtstr = '^(?!01|02|03).*$';
results = regexpi({files.name}.',filtstr,'start');
files = files(~cellfun(@isempty,results));
nfiles = length(files);
end
for p=1:nfiles
disp(files(p).name)
% read msgs and data
dat = load(fullfile(dirs.mat,files(p).name));
scrRes = dat.expt.resolution;
ts = dat.data.gaze.systemTimeStamp;
% the Pro SDK does not guarantee invalid data is nan. Set to nan if
% invalid
dat.data.gaze. left.gazePoint.onDisplayArea(:,~dat.data.gaze. left.gazePoint.valid) = nan;
dat.data.gaze.right.gazePoint.onDisplayArea(:,~dat.data.gaze.right.gazePoint.valid) = nan;
dat.data.gaze. left.pupil.diameter(~dat.data.gaze. left.pupil.valid) = nan;
dat.data.gaze.right.pupil.diameter(~dat.data.gaze.right.pupil.valid) = nan;
% collect data from the file, and turn gaze positions from normalized
% coordinates into pixels
samp = [bsxfun(@times,dat.data.gaze.left.gazePoint.onDisplayArea,scrRes.'); bsxfun(@times,dat.data.gaze.right.gazePoint.onDisplayArea,scrRes.'); dat.data.gaze.left.pupil.diameter; dat.data.gaze.right.pupil.diameter];
header = {'t','gaze_point_LX','gaze_point_LY','gaze_point_RX','gaze_point_RY','pupil_diameter_L','pupil_diameter_R'};
[timest,what,msgs] = parseMsgs(dat.messages);
% split up per task and write
for q=1:length(timest.fix)
fname = sprintf('%s_R%03d.txt',files(p).fname,q);
fprintf('%s\n',fname);
% msgs
fid = fopen(fullfile(dirs.msgsO,fname),'wt');
t = msgs{q}.';
fprintf(fid,'%d\t%s\n',t{:});
fclose(fid);
% data
fid = fopen(fullfile(dirs.samplesO,fname),'wt');
fmt = repmat('%s\t',1,length(header));
fmt(end) = 'n';
% header
fprintf(fid,fmt,header{:});
% data
fmt = ['%ld\t' repmat('%.2f\t',1,length(header)-3) repmat('%.4f\t',1,2)];
fmt(end) = 'n';
qSel = ts>=timest.fix(q) & ts<=timest.end(q);
data = [num2cell(ts(qSel)); num2cell(samp(:,qSel))];
fprintf(fid,fmt,data{:});
fclose(fid);
% copy stimuli, if needed
fInfo = [dat.expt.stim.fInfo];
qWhich= strcmp({fInfo.name},what{q});
imgFile = fullfile(dat.expt.stim(qWhich).fInfo.folder,what{q});
imgFileOut = fullfile(dirs.stims,what{q});
if exist(imgFile,'file') && ~exist(imgFileOut,'file')
copyfile(imgFile,imgFileOut,'f');
end
end
end
rmpath(genpath(dirs.funclib)); % cleanup path