forked from open-ephys/analysis-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_session_info.m
171 lines (118 loc) · 6.37 KB
/
get_session_info.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
function info = get_session_info(directory)
% STEP 1: GET FULL PATH FOR DIRECTORY
directory = get_full_path(directory);
directory_contents = dir(directory);
%%
% STEP 2: FIND NUMBER OF RECORDING SESSIONS
settingsPattern = '^settings(_\d+)?\.xml$';
isSettingsFile = regexp({directory_contents.name}, settingsPattern, 'once');
numSessions = length(cell2mat(isSettingsFile));
for s = numSessions:-1:1 % iterate backwards to preallocate
if s == 1
sessStr = '';
else
sessStr = ['_', num2str(s)];
end
%%
% STEP 3: PARSE THE 'SETTINGS.XML' FILE
settingsFilename = ['settings' sessStr '.xml'];
DOMnode = xmlread([directory filesep settingsFilename]);
xRoot = DOMnode.getDocumentElement;
processors = cell(0);
electrodes = cell(0);
% LOOP THROUGH TOP-LEVEL NODES
for i = 1:xRoot.getChildNodes.getLength-1
if strcmp(xRoot.item(i).getNodeName, 'SIGNALCHAIN')
xSignalChain = xRoot.item(i);
processorIndex = 0;
% LOOP THROUGH PROCESSOR NODES
for j = 1:xSignalChain.getChildNodes.getLength-1
if strcmp(xSignalChain.item(j).getNodeName, 'PROCESSOR')
processorIndex = processorIndex + 1;
xProcessor = xSignalChain.item(j);
nodeId = str2double(xProcessor.getAttributes.getNamedItem('NodeId').getValue);
processorName = char(xProcessor.getAttributes.getNamedItem('name').getValue);
processors{processorIndex,1} = nodeId;
processors{processorIndex,2} = processorName;
if strcmp(processorName, 'Filters/Spike Detector')
electrodeIndex = 0;
% LOOP THROUGH ELECTRODE NODES IN SPIKE DETECTOR
for k = 1:xProcessor.getChildNodes.getLength-1
if strcmp(xProcessor.item(k).getNodeName, 'ELECTRODE')
electrodeIndex = electrodeIndex + 1;
xElectrode = xProcessor.item(k);
electrodeName = char(xElectrode.getAttributes.getNamedItem('name').getValue);
numChannels = str2double(xElectrode.getAttributes.getNamedItem('numChannels').getValue);
channels = zeros(1, numChannels);
channelIndex = 0;
% LOOP THROUGH CHANNEL NODES FOR ELECTRODE
for m = 1:xElectrode.getChildNodes.getLength-1
if strcmp(xElectrode.item(m).getNodeName, 'SUBCHANNEL')
channelIndex = channelIndex + 1;
xChannel = xElectrode.item(m);
ch = str2double(xChannel.getAttributes.getNamedItem('ch').getValue);
channels(channelIndex) = ch;
end
end
electrodes{electrodeIndex,1} = electrodeName;
electrodes{electrodeIndex,2} = channels;
end
end
end
end
end
end
end
%%
% STEP 4: PARSE CONTINUOUS_DATA.OPENEPHYS TO GET CONTINUOUS FILENAMES
DOMnode = xmlread([directory filesep 'Continuous_Data' sessStr '.openephys']);
xRoot = DOMnode.getDocumentElement;
% gather recordings
xRecordings = {};
for kR = 1:xRoot.getChildNodes.getLength - 1
if strcmp(xRoot.item(kR).getNodeName, 'RECORDING')
recNum = str2double(xRoot.item(kR).getAttributes.getNamedItem('number').getValue);
xRecordings{recNum + 1} = xRoot.item(kR);
end
end
% to hold filenames
nRecordings = length(xRecordings);
processors(:, 3) = repmat({repmat({{}}, nRecordings, 1)}, size(processors, 1), 1);
% loop over recordings
for kR = 1:nRecordings
xRecording = xRecordings{kR};
if isempty(xRecording)
continue;
end
% loop over processors
for kP = 1:xRecording.getChildNodes.getLength - 1
if ~strcmp(xRecording.item(kP).getNodeName, 'PROCESSOR')
continue;
end
xProcessor = xRecording.item(kP);
nodeId = str2double(xProcessor.getAttributes.getNamedItem('id').getValue);
procInd = find([processors{:, 1}] == nodeId, 1);
if isempty(procInd)
warning('Recorded processor number %d not found in %s', nodeId, settingsFilename);
procInd = size(processors, 1) + 1;
processors{procInd, 1} = nodeId;
processors{procInd, 2} = 'Unknown Processor';
processors{procInd, 3} = repmat({{}}, nRecordings, 1);
end
% loop through channels
for kC = 1:xProcessor.getChildNodes.getLength - 1
if ~strcmp(xProcessor.item(kC).getNodeName, 'CHANNEL')
continue;
end
xChannel = xProcessor.item(kC);
filename = char(xChannel.getAttributes.getNamedItem('filename').getValue);
processors{procInd, 3}{kR}{end+1} = filename;
end
end
end
info(s).electrodes = electrodes;
info(s).processors = processors;
info(s).events_file = ['all_channels' sessStr '.events'];
info(s).messages_file = ['messages' sessStr '.events'];
end
end