-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchange_labels_events.m
More file actions
110 lines (83 loc) · 3.22 KB
/
change_labels_events.m
File metadata and controls
110 lines (83 loc) · 3.22 KB
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
% Change stim names
% Presto and Cloudy = 4 control, 4 tasks
% if CB that means first 4 tasks r control
% 3/6/35
% Shannon Kelley
clear
clc
close all
% cd '/projectnb/nphfnirs/ns/lcarlton/Homer3/' % CHANGE
% setpaths
%% Set paths and load in data
sub = '04'; % CHANGE
filePaths.baseDir = '/projectnb/nphfnirs/s/datasets/U01_ADRD/'; % CHANGE
%run_name=['sub-', sub, '_task-', task, '_run-', run]; %file name
filePaths.subDir = fullfile(filePaths.baseDir, ['sub-', sub], 'nirs');
cd(filePaths.subDir)
files = dir('*.snirf'); % Lists all .snirf files in the current folder
numFiles = length(files); % Get the number of files
for j = 1:numFiles
events = [];
filePaths.snirf = fullfile(filePaths.subDir, files(j).name); % full path 2 snirf
filePaths.events = fullfile(filePaths.subDir, [files(j).name(1:end-11), '_events.tsv']); % full path 2 events
% Load snirf file
if exist(filePaths.snirf, 'file') == 2
fprintf('\nSnirf file exists');
snirf = SnirfLoad(filePaths.snirf);
else
fprintf('\nSnirf file does not exist');
end
%%
disp(['Renaming stims for file ', files(j).name])
% load events.tsv file
if exist(filePaths.events, 'file') == 2
fileInfo = dir(filePaths.events);
if fileInfo.bytes > 0
disp('Events file exists.');
events = readtable(filePaths.events, 'FileType', 'text', 'Delimiter', '\t');
else
disp('Events file exists but is empty, creating events table to edit');
% Create events table to edit
onset = []; % Initialize empty arrays
duration = [];
amplitude = [];
trial_type = [];
% Loop through the stimuli
for i = 1:length(snirf.stim)
stim_data = snirf.stim(i).data; % Extract onset, duration, amplitude
num_trials = size(stim_data, 1);
% Append to the arrays
onset = [onset; stim_data(:,1)];
duration = [duration; stim_data(:,2)];
amplitude = [amplitude; stim_data(:,3)];
trial_type = [trial_type; repmat(str2double(snirf.stim(i).name), num_trials, 1)];
end
events = table(onset, duration, amplitude, trial_type);
% Display the table
disp(events);
end
end
%%
if ismember('Duration', events.Properties.VariableNames)
events = renamevars(events, 'Duration', 'duration');
end
%%
if ismember('Trial_type', events.Properties.VariableNames)
events = renamevars(events, 'Trial_type', 'trial_type');
end
%%
if ismember('Amplitude', events.Properties.VariableNames)
events = renamevars(events, 'Amplitude', 'value');
end
%%
if ismember('amplitude', events.Properties.VariableNames)
events = renamevars(events, 'amplitude', 'value');
end
%%
if ismember('Onset', events.Properties.VariableNames)
events = renamevars(events, 'Onset', 'onset');
end
%% save data
writetable(events, filePaths.events, 'FileType', 'text', 'Delimiter', '\t');
disp('events.tsv file created successfully.');
end