forked from m053m716/-io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_tmsi_channel.m
266 lines (250 loc) · 9.77 KB
/
load_tmsi_channel.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
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
function data = load_tmsi_channel(SUBJ, YYYY, MM, DD, ARRAY, BLOCK, TYPE, INDEX, varargin)
%LOAD_TMSI_CHANNEL Loads TMSi channel if it has been parsed
%
% Syntax:
% data = io.load_tmsi_channel(SUBJ, YYYY, MM, DD, ARRAY, BLOCK, TYPE, INDEX);
%
% Example:
% data = io.load_tmsi_channel('Frank', 2021, 12, 9, "B", 155, "UNI", 21);
% -> Returns data struct for unipolar channel 21 (UNI21).
%
% Inputs:
% SUBJ - Subject name (char array or string)
% YYYY - Year (4-digit year char array or string, or double)
% MM - Month (2-digit month char array or string, or double)
% DD - Day (2-digit day char array or string, or double)
% ARRAY - "A", "B", or ["A, "B"]
% BLOCK - Numeric double scalar or vector of recording block keys.
% --> If ARRAY or BLOCK are nonscalar, then `offset` is returned as a
% cell array, with dimensions numel(BLOCK) x numel(ARRAY)
% TYPE - "UNI" | "BIP" | "TRIGGERS" | "STATUS" | "EMG"
% -> "UNI" (requires INDEX 1-64) -- HD-EMG array
% -> "BIP" (requires INDEX 1-4) -- Surface bipolar
% -> "TRIGGERS" -- "Sync" data on DB26 16-bit interface
% -> "STATUS" -- Device status indicator (see TMSi docs for bit
% encodings)
% -> "COUNTER" -- Device sample counter (for sanity-checks)
% -> "POT" | "POTENTIOMETER" | "AUX" | "ISO" -- For
% potentiometers collected using ISO-AUX 2-channel sensor.
% -> "EMG" -- All HD-EMG and Bipolar channels (no INDEX)
% INDEX - (Only needed for "UNI" or "BIP") -- 1-indexed scalar value (or
% array to return multiple channels). Alternatively, can give
% the "muscle" name (from Muscle-Map file) e.g. "R_EDC" or
% "ED45" etc. In this case, any channels containing that
% string in the Muscle-Map name will be returned.
% varargin - (Optional) 'Name',value input argument pairs.
%
% Output:
% data - Struct or array of structs with channel data
%
% See also: Contents, io.load_tmsi_triggers, io.load_tmsi_raw
if nargin < 8
if strcmpi(TYPE, "UNI") || strcmpi(TYPE, "BIP")
error("LoadChannel:MissingArgument", ...
"If loading channel of type UNI or BIP, must specify channel INDEX argument (1-indexed).");
end
end
if numel(varargin) > 0
if isstruct(varargin{1})
pars = varargin{1};
varargin(1) = [];
else
pars = struct;
[pars.raw_data_folder, pars.generated_data_folder] = parameters('raw_data_folder', 'generated_data_folder');
end
pars = utils.parse_parameters(pars, varargin{:});
else
pars = struct;
[pars.raw_data_folder, pars.generated_data_folder] = parameters('raw_data_folder', 'generated_data_folder'); % Reference to LOCAL parameters function (in repo with this submodule)
end
% Iterate over ARRAY and BLOCK elements if any are non-scalar.
if (numel(ARRAY) > 1) || (numel(BLOCK) > 1)
data = cell(numel(BLOCK));
for iA = 1:numel(ARRAY)
data{iA} = io.load_tmsi_channel(SUBJ, YYYY, MM, DD, ARRAY{iA}, BLOCK(iB), TYPE, INDEX, pars);
end
return;
end
if ischar(INDEX)
INDEX = string(INDEX);
end
if numel(INDEX) > 1
data = init_data(numel(INDEX));
for ii = 1:numel(data)
data(ii) = io.load_tmsi_channel(SUBJ, YYYY, MM, DD, ARRAY, BLOCK, TYPE, INDEX(ii), pars);
end
return;
end
% Get path info
[f, args] = utils.get_block_name(SUBJ, YYYY, MM, DD, ARRAY, BLOCK, ...
'rootdir_raw', pars.raw_data_folder, ...
'rootdir_gen', pars.generated_data_folder);
if exist(f.Generated.Meta, 'file')==0
error(...
"LoadChannels:MissingData", ...
"The Metadata file <strong>%s</strong> is missing.\n\t->\tThis is most-likely because data has not yet been exported.", ...
f.Generated.Meta ...
);
else
channels = getfield(load(f.Generated.Meta, 'channels'), 'channels');
end
if isstring(INDEX)
M = io.load_muscle_map(args{:}, pars.raw_data_folder);
if isempty(M)
error(...
"LoadChannels:MissingData", ...
"The Muscle-Map file <strong>%s</strong> is missing.\n\t->\tThis is most-likely because it has not yet been created using the Muscle-Map Exporter app.", ...
f.Raw.Map ...
);
end
end
% Handle different types of channel requests.
switch upper(TYPE)
case "UNI"
if isstring(INDEX)
INDEX = handle_mapped_channel(M, INDEX, "UNI");
if numel(INDEX) > 1
data = init_data(numel(INDEX));
for ii = 1:numel(data)
data(ii) = io.load_tmsi_channel(SUBJ, YYYY, MM, DD, ARRAY, BLOCK, TYPE, INDEX(ii), pars);
end
return;
end
else
if (INDEX < 1) || (INDEX > 64)
error("LoadChannels:InvalidIndex", ...
"INDEX == %d is out of range for TYPE == 'UNI'", ...
INDEX);
end
end
case "BIP"
if isstring(INDEX)
INDEX = handle_mapped_channel(M, INDEX, "BIP");
if numel(INDEX) > 1
data = init_data(numel(INDEX));
for ii = 1:numel(data)
data(ii) = io.load_tmsi_channel(SUBJ, YYYY, MM, DD, ARRAY, BLOCK, TYPE, INDEX(ii), pars);
end
return;
end
else
if (INDEX < 1) || (INDEX > 4)
error("LoadChannels:InvalidIndex", ...
"INDEX == %d is out of range for TYPE == 'BIP'", ...
INDEX);
end
end
case "EMG"
iUni = find(strcmpi(channels.type, "UNI"));
nUni = numel(iUni);
iBip = find(strcmpi(channels.type, "BIP"));
nBip = numel(iBip);
n = nUni + nBip;
data = init_data(n);
for ii = 1:nUni
data(ii) = io.load_tmsi_channel(SUBJ, YYYY, MM, DD, ARRAY, BLOCK, "UNI", iUni(ii), pars);
end
for ii = 1:nBip
data(nUni+ii) = io.load_tmsi_channel(SUBJ, YYYY, MM, DD, ARRAY, BLOCK, "BIP", iBip(ii), pars);
end
return;
case "COUNTER"
INDEX = find(contains(channel.type, "COUNTER"), 1, 'first');
if isempty(INDEX)
error("LoadChannels:MissingType", ...
"No match for TYPE == '%s' in this data record", ...
upper(TYPE));
end
case "STATUS"
INDEX = find(contains(channel.type, "STATUS"), 1, 'first');
if isempty(INDEX)
error("LoadChannels:MissingType", ...
"No match for TYPE == '%s' in this data record", ...
upper(TYPE));
end
case {"POT", "POTENTIOMETER", "ISO", "AUX"}
INDEX = find(contains(channel.type, "ISO"));
if isempty(INDEX)
error("LoadChannels:MissingType", ...
"No match for TYPE == '%s' in this data record", ...
upper(TYPE));
else
data = init_data(numel(INDEX));
end
for ii = 1:numel(INDEX)
data(ii) = load(fullfile(f.Generated.Channels, ...
sprintf("%s_RAW_%d.mat", f.Block, INDEX)), ...
'fs', 'name', 'samples', 'start_time', 'units');
end
return;
case {"TRIGGER", "TRIGGERS"}
INDEX = find(contains(channels.type, "TRIGGER"), 1, 'first');
if isempty(INDEX)
error("LoadChannels:MissingType", ...
"No match for TYPE == '%s' in this data record", ...
upper(TYPE));
end
otherwise
error("LoadChannel:InvalidType", ...
"TYPE == '%s' is not currently handled.", upper(TYPE));
end
data = load(fullfile(f.Generated.Channels, ...
sprintf("%s_RAW_%d.mat", f.Block, INDEX)), ...
'fs', 'name', 'samples', 'start_time', 'units');
function data = init_data(n)
%INIT_DATA Initialize data struct array if multiple channel indices.
%
% Syntax:
% data = init_data(n);
%
% Inputs:
% n - Number of data array elements to initialize
%
% Output:
% data - (n x 1) struct array with fields
% 'fs', 'name', 'samples', 'start_time', and 'units'
data = struct(...
'fs', cell(n,1), ...
'name', cell(n,1), ...
'samples', cell(n,1), ...
'start_time', cell(n,1), ...
'units', cell(n,1) ...
);
end
function idx = handle_mapped_channel(m, s_idx, emg_type)
%HANDLE_MAPPED_CHANNEL Convert channel from string/char to 1-indexed scalar or array.
%
% Syntax:
% idx = handle_mapped_channel(m, s_idx, emg_type)
%
% Inputs:
% m - Muscle-Map io.JSON object
% s_idx - "String" index (the name of channel or channel subset)
% emg_type - "UNI" | "BIP"
%
% Output:
% idx - Scalar or array of 1-indexed channels
switch upper(emg_type)
case "UNI"
ch = strings(64,1);
for iEMG = 1:64
ch(iEMG) = string(m.Muscles.(sprintf("UNI%02d", iEMG)));
end
case "BIP"
ch = strings(4,1);
for iEMG = 1:4
ch(iEMG) = string(m.Muscles.(sprintf("BIP%02d", iEMG)));
end
otherwise
error("LoadChannels:InvalidType", ...
"TYPE == '%s' is not currently handled.", upper(emg_type));
end
idx = find(contains(ch, s_idx, 'IgnoreCase', true));
if isempty(idx)
disp(unique(ch));
error("LoadChannels:InvalidIndex", ...
"No match for INDEX == '%s' in Muscle-Map. See above-list for valid values for TYPE == '%s'.", ...
s_idx, upper(emg_type));
end
end
end