-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregateData.m
202 lines (149 loc) · 6.87 KB
/
aggregateData.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
function aggregateData(source)
% Collects all data stored in various files from one participants and outputs all
% json files for that participant, one per email, containing all data
% collected, in chronological order
% source: where source files are located. If this parameter is not given
% opens a dialog asking for directory location.
%% setup
params = Params;
basedir = params.basedir;
if nargin < 1
source = uigetdir('', 'Select folder containing all source data');
end
% find all json files
mails = dir([source filesep 'MAIL_*']);
% create output dir if it doesn't exist
outdir = params.outdir;
if ~exist(basedir, 'dir')
mkdir(basedir);
end
if ~exist(outdir, 'dir')
mkdir(outdir);
end
filenum = 0;
doneNames = {};
emgData_file = struct();
emgData_file.time = [];
emgData_file.accelX = [];
emgData_file.accelY = [];
emgData_file.accelZ = [];
emgData_file.corr = [];
emgData_file.zygo = [];
gsrData_file = struct();
gsrData_file.time = [];
gsrData_file.accelX = [];
gsrData_file.accelY = [];
gsrData_file.accelZ = [];
gsrData_file.gsr = [];
gsrData_file.phasic = [];
maildir = mails(1);
% get account number and session number
spl = strsplit(maildir.name, '_');
pString = spl{2};
outdir = [outdir filesep pString];
if ~exist(outdir, 'dir')
mkdir(outdir);
end
disp(['Writing data to ' outdir]);
for maildirI = 1:numel(mails)
%% read jsons in mail folder
maildir = mails(maildirI);
% get account number and session number
spl = strsplit(maildir.name, '_');
sesString = spl{3};
sesNum = str2num(sesString(2));
accNum = str2num(spl{4}(2));
mailJsons = dir([source filesep maildir.name filesep 'MailEye_*']);
% look through all jsons and make list of startTimes and their related
% json name
jsonSortTable = struct();
for jsonI = 1:numel(mailJsons)
path = [mailJsons(jsonI).folder filesep mailJsons(jsonI).name];
text = fileread(path);
json = jsondecode(text);
jsonSortTable(jsonI).path = path;
jsonSortTable(jsonI).name = mailJsons(jsonI).name;
jsonSortTable(jsonI).startTime = json.startUnixtime;
end
jsonSortTable = struct2table(jsonSortTable);
jsonSortTable = sortrows(jsonSortTable, 'startTime');
%% prepare data
% read data all data, then for each json aggregate emg and eda
% and calculate some simple features on them
emgData_raw = csvread([maildir.folder filesep 'EMG_' pString '_' sesString '.csv' ], params.csvStart);
gsrData_raw = csvread([maildir.folder filesep 'GSR_' pString '_' sesString '.csv' ], params.csvStart);
edaData = load([maildir.folder filesep 'EDA_' pString '_' sesString '.mat' ]);
% remove discarded rows from gsrData (removedRows, if any)
if isfield(edaData, 'removedRows')
gsrData_raw(edaData.removedRows, :) = [];
end
phasicData = edaData.analysis.phasicData';
gsrRows = size(gsrData_raw, 1);
edaRows = size(phasicData, 1);
assert(gsrRows == edaRows, 'AGGR:EDAMISMATCH', ['Eda data and gsr data number of rows do not match. eda: ' num2str(edaRows) '. gsr:' num2str(gsrRows) '.']);
% normalise accelerometer and fEMG using our custom function
emgData_normalised = normalisePhysio(emgData_raw, [2:4 6 7]);
% process emg data using specs in paper (10 Hz high pass)
emgData_processed = processPhysio(emgData_raw, [6 7], 10);
% process head accel using specs in paper (0.3 Hz high pass)
emgData_processed = processPhysio(emgData_processed, 2:4, 0.3);
% calculate mean head accelerometer and process it too
emgData_meanAccel = mean(emgData_raw(:, 2:4), 2);
emgData_mean_processed = processPhysio(emgData_meanAccel, 1, 0.3);
% normalise accelerometer using our custom function
gsrData_normalised = normalisePhysio(gsrData_raw, 2:4);
% process wrist accelerometer using specs in paper (0.3 Hz high pass)
gsrData_processed = processPhysio(gsrData_raw, 2:4, 0.3);
% calculate mean head accelerometer and process it too
gsrData_meanAccel = mean(gsrData_raw(:, 2:4), 2);
gsrData_mean_processed = processPhysio(gsrData_meanAccel, 1, 0.3);
%% save raw vectors of all data to json
emgData_file.time = vertcat(emgData_file.time, emgData_raw(:, params.iTimestamp));
emgData_file.accelX = vertcat(emgData_file.accelX, emgData_raw(:, params.iaccX));
emgData_file.accelY = vertcat(emgData_file.accelY, emgData_raw(:, params.iaccY));
emgData_file.accelZ = vertcat(emgData_file.accelZ, emgData_raw(:, params.iaccZ));
emgData_file.corr = vertcat(emgData_file.corr, emgData_raw(:, params.iCorr));
emgData_file.zygo = vertcat(emgData_file.zygo, emgData_raw(:, params.iZygo));
outtext = jsonencode(emgData_file);
fid = fopen([outdir filesep 'emgData.json'], 'w');
fprintf(fid, outtext);
gsrData_file.time = vertcat(gsrData_file.time, gsrData_raw(:, params.iTimestamp));
gsrData_file.accelX = vertcat(gsrData_file.accelX, gsrData_raw(:, params.iaccX));
gsrData_file.accelY = vertcat(gsrData_file.accelY, gsrData_raw(:, params.iaccY));
gsrData_file.accelZ = vertcat(gsrData_file.accelZ, gsrData_raw(:, params.iaccZ));
gsrData_file.gsr = vertcat(gsrData_file.gsr, gsrData_raw(:, params.iGSR));
gsrData_file.phasic = vertcat(gsrData_file.phasic, phasicData);
outtext = jsonencode(gsrData_file);
fid = fopen([outdir filesep 'gsrData.json'], 'w');
fprintf(fid, outtext);
% end of raw vector save
%% one-by-one aggregation
for tableI = 1:size(jsonSortTable, 1)
name = jsonSortTable{tableI, 'name'};
% skip if this file name was already done
if any(contains(doneNames, name))
continue
end
doneNames = [doneNames{:} name];
text = fileread(jsonSortTable{tableI, 'path'}{1});
json = jsondecode(text);
json.emg = aggregate_emg(emgData_raw, emgData_normalised, emgData_processed, emgData_mean_processed, json.startUnixtime, json.endUnixtime);
json.eda = aggregate_eda(gsrData_raw, gsrData_normalised, gsrData_processed, gsrData_mean_processed, edaData, json.startUnixtime, json.endUnixtime);
% save session and account number in output json
json.session = sesNum;
json.account = accNum;
% get raw eye data (saved in Fixations_*.json)
nameSplit = strsplit(name{:}, '_');
rawEyeName = ['Fixations_' nameSplit{2}];
rawEyeFile = [source filesep maildir.name filesep rawEyeName];
text = fileread(rawEyeFile);
rawEyeData = jsondecode(text);
json.rawEyeData = rawEyeData;
outtext = jsonencode(json);
fid = fopen([outdir filesep num2str(filenum) '.json'], 'w');
fprintf(fid, outtext);
filenum = filenum + 1;
end
end
disp(['Done, wrote ' num2str(filenum) ' files.']);
end