-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdateJSONWithEvents.m
More file actions
93 lines (78 loc) · 2.96 KB
/
Copy pathupdateJSONWithEvents.m
File metadata and controls
93 lines (78 loc) · 2.96 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
function jsonData = updateJSONWithEvents(jsonInput, events, srate)
% updateJSONWithEvents(jsonInput, events, srate)
%
% Updates JSON data with event information, counting occurrences of each label.
%
% Inputs:
% jsonInput: Either a path to a JSON file (string) or a JSON structure.
% events: A struct array or table containing event data. Required fields:
% - start (in samples)
% - duration (in seconds)
% - key (string, 'A', 'F1', 'F2', ..., 'F12')
% - label (string, event label, e.g., 'Artifact', 'Spindle')
% srate: Sampling rate of the data (samples per second).
%
% The JSON data should have the structure provided in the prompt.
% Load or use the JSON data
if ischar(jsonInput)
jsonData = jsondecode(fileread(jsonInput));
elseif isstruct(jsonInput)
jsonData = jsonInput;
else
error('jsonInput must be a string (file path) or a struct.');
end
% Extract epoch duration
epochDuration = jsonData{1}(1).end - jsonData{1}(1).start; % Assuming all epochs have same duration
% Extract epoch start times
epochStartTimes = [jsonData{1}.start];
epochEndTimes = epochStartTimes + epochDuration;
% Initialize counters for each event label
labelCounters = containers.Map();
% Loop through events
for ievent = 1:length(events)
event = events(ievent);
% Name of event
eventLabel = event.label;
% Determine digit based on key
eventKey = event.key;
if strcmp(eventKey, 'A')
eventDigit = 0;
else
eventDigit = str2double(eventKey(2:end)); % Extract number from 'F1', 'F2', etc.
end
% Convert sample start to time start
eventStart = event.start / srate;
% Calculate time end
eventEnd = eventStart + event.duration;
% Determine epochs
eventEpochs = find(eventStart >= epochStartTimes & eventStart < epochEndTimes) : find(eventEnd > epochStartTimes & eventEnd <= epochEndTimes);
% Get or initialize counter for this label
if isKey(labelCounters, eventLabel)
counter = labelCounters(eventLabel) + 1;
else
counter = 1;
end
% Update counter for this label
labelCounters(eventLabel) = counter;
% Create event struct
newEvent = struct( ...
'key', eventKey, ...
'event', eventLabel, ...
'digit', eventDigit, ...
'counter', counter, ...
'epoch', eventEpochs, ...
'start', eventStart, ...
'end', eventEnd ...
);
% Append to JSON data
jsonData{2}(end + 1) = newEvent;
end
%
% % Update name
% [basename] = fileparts(jsonFilePath)
%
% % Write updated JSON file
% fid = fopen([basename '_withEvents.json'], 'w');
% fprintf(fid, '%s', jsonencode(jsonData, 'PrettyPrint', true));
% fclose(fid);
end