-
Notifications
You must be signed in to change notification settings - Fork 1
/
load_wrist_task_trial_data.m
111 lines (102 loc) · 4.72 KB
/
load_wrist_task_trial_data.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
function [TrialData, H] = load_wrist_task_trial_data(SUBJ, YYYY, MM, DD)
%LOAD_WRIST_TASK_TRIAL_DATA Import data from a trial .txt file
%
% Example:
% [TrialData, H] = io.load_wrist_task_trial_data("Rupert", 2021, 09, 16);
%
% MUST be run in repo that has `TaskState` enumeration class defined.
%
% See also READTABLE.
%
% Auto-generated by MATLAB on 28-Jul-2021 18:12:14
% Modified by Max Murphy on 29-Jul-2021 v0.1
% 30-Mar-2022 v1.0
% 29-May-2022 v1.1
% Repurposed to submodule and renamed by max Murphy on 05-29-2022 v1.1
if nargin == 1
f = SUBJ;
SUBJ = f.Animal;
YYYY = year(f.DateValue);
MM = month(f.DateValue);
DD = day(f.DateValue);
end
% Input handling
[YYYY, MM, DD] = utils.parse_date_args(YYYY, MM, DD, true);
txt_str = 'R:/NMLShare/raw_data/primate/%s/Behavior/%s_%s_%s_%s_Trial-Data_*.txt';
filename = sprintf(txt_str, SUBJ, SUBJ, YYYY, MM, DD);
F = dir(filename);
if isempty(F)
error('Could not find any files matching string\n\t->\t <strong>%s</strong> <-\n', filename);
end
% Set up the Import Options and import the data
% Specify column names and types
% opts.VariableNames = ["Timestamp", "Subject", "Trial_Number", "Angle", "Trial_Mode", "Last_Task_State", "Trial_Outcome", "N_Overshoot"];
% opts.VariableNames = h.var_names;
% Import the data
TrialData = [];
H = [];
for iF = 1:numel(F)
h = io.parse_wrist_task_txt_header(fullfile(F(iF).folder, F(iF).name));
opts = delimitedTextImportOptions("NumVariables", numel(h.var_names));
opts.DataLines = [h.start_line, Inf];
opts.Delimiter = "\t";
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.VariableNames = strip(h.var_names);
if h.Version.Major >= 1
opts.VariableTypes = repmat("double", 1, numel(h.var_names));
elseif (h.Version.Major >= 2) && (h.Version.Major < 5)
opts.VariableTypes = ["double", "string", "double", "double", "double", "double", "double", "double"];
elseif (h.Version.Major >= 5)
opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double", "double", "double"];
else
error('Parsing is not setup for <strong>v%d.%d</strong>!', h.Version.Major, h.Version.Minor);
end
x = readtable(fullfile(F(iF).folder, F(iF).name), opts);
% x(x.Last_Task_State=="TaskState.PRE",:) = [];
if size(x, 1) == 1
delete(fullfile(F(iF).folder, F(iF).name));
fprintf(1, 'No data in <strong>%s</strong> (file removed)\n', F(iF).name);
continue;
end
x((x.Last_Task_State==TaskState.T1_INSTRUCT_V1) | ...
(x.Last_Task_State==TaskState.T1_HOLD_1) , :) = [];
[~, name, ~] = fileparts(F(iF).name);
str_info = strsplit(name, '_');
n = size(x, 1);
x.Time = x.Timestamp * 1e-9; % Convert to seconds.
if h.Version.Major >= 1
x.Session = repmat(string(h.Session), n, 1);
x.Year = ones(n, 1) * h.Year;
x.Month = ones(n, 1) * h.Month;
x.Day = ones(n, 1) * h.Day;
x.Subject = repmat(h.Subject, n, 1);
else
x.Session = repmat(string(str_info{end}), n, 1);
x.Year = ones(n, 1) * str2double(str_info{2});
x.Month = ones(n, 1) * str2double(str_info{3});
x.Day = ones(n, 1) * str2double(str_info{4});
end
if h.Version.Major >= 1
x.Duration = [x.Time(1) - h.Start_ns*1e-9; diff(x.Time)];
else
x.Duration = [x.Time(1); diff(x.Time)];
end
TrialData = [TrialData; x]; %#ok<AGROW>
H = [H; struct2table(h)]; %#ok<AGROW>
end
% Parse Times from Timestamp column
% [TrialData.Year, TrialData.Month, TrialData.Day, TrialData.Time] = parseTimestamps(TrialData.Timestamp);
if h.Version.Major < 3
TrialData = TrialData(:, {'Subject', 'Session', 'Year', 'Month', 'Day', 'Time', 'Duration', 'Trial_Number', 'Angle', 'Trial_Outcome', 'Last_Task_State', 'Trial_Mode', 'N_Overshoot'});
elseif h.Version.Major == 3 && h.Version.Minor >= 5
TrialData = TrialData(:, {'Subject', 'Session', 'Year', 'Month', 'Day', 'Time', 'Duration', 'Trial_Number', 'Target', 'Targeted', 'Trial_Outcome', 'Last_Task_State', 'Trial_Mode', 'N_Overshoot', 'Parameters_ID'});
elseif h.Version.Major < 5
TrialData = TrialData(:, {'Subject', 'Session', 'Year', 'Month', 'Day', 'Time', 'Duration', 'Trial_Number', 'Target', 'Targeted', 'Trial_Outcome', 'Last_Task_State', 'Trial_Mode', 'N_Overshoot'});
elseif (h.Version.Major >= 5)
TrialData = TrialData(:, {'Subject', 'Session', 'Year', 'Month', 'Day', 'Time', 'Duration', 'Trial_Number', 'Target', 'Targeted', 'Trial_Outcome', 'Last_Task_State', 'Trial_Mode', 'N_Overshoot', 'Parameters_ID'});
else
error('Parsing is not setup for <strong>v%d.%d</strong>!', h.Version.Major, h.Version.Minor);
end
TrialData.Properties.UserData = H;
end