-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathNwbFile.m
287 lines (248 loc) · 10.2 KB
/
NwbFile.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
classdef NwbFile < types.core.NWBFile
% NWBFILE - Root object representing an NWB file.
%
% Requires that core and extension NWB types have been generated
% and reside in a ``+types`` namespace on the MATLAB search path.
%
% Usage:
% Example 1 - Construct a simple NwbFile object for export::
%
% nwb = NwbFile;
% nwb.epochs = types.core.Epochs;
% nwbExport(nwb, 'epoch.nwb');
%
% See also:
% nwbRead, generateCore, generateExtension
methods
function obj = NwbFile(propValues)
% NWBFILE - Create an NWB File object
arguments
propValues.?types.core.NWBFile
propValues.nwb_version
end
nameValuePairs = namedargs2cell(propValues);
obj = [email protected](nameValuePairs{:});
if strcmp(class(obj), 'NwbFile') %#ok<STISA>
cellStringArguments = convertContainedStringsToChars(nameValuePairs(1:2:end));
types.util.checkUnset(obj, unique(cellStringArguments));
end
end
function export(obj, filename, mode)
% EXPORT - Export NWB file object
arguments
obj (1,1) NwbFile
filename (1,1) string
mode (1,1) string {mustBeMember(mode, ["edit", "overwrite"])} = "edit"
end
% add to file create date
if isa(obj.file_create_date, 'types.untyped.DataStub')
obj.file_create_date = obj.file_create_date.load();
end
current_time = {datetime('now', 'TimeZone', 'local')};
if isempty(obj.file_create_date)
obj.file_create_date = current_time;
elseif iscell(obj.file_create_date)
obj.file_create_date(end+1) = current_time;
else
% obj.file_create_date could be a datetime array
obj.file_create_date = num2cell(obj.file_create_date);
obj.file_create_date(end+1) = current_time;
end
%equate reference time to session_start_time if empty
if isempty(obj.timestamps_reference_time)
obj.timestamps_reference_time = obj.session_start_time;
end
isEditingFile = false;
if isfile(filename)
if mode == "edit"
output_file_id = H5F.open(filename, 'H5F_ACC_RDWR', 'H5P_DEFAULT');
isEditingFile = true;
elseif mode == "overwrite"
output_file_id = H5F.create(filename, 'H5F_ACC_TRUNC', 'H5P_DEFAULT', 'H5P_DEFAULT');
end
else
output_file_id = H5F.create(filename);
end
try
obj.embedSpecifications(output_file_id)
refs = [email protected](obj, output_file_id, '/', {});
obj.resolveReferences(output_file_id, refs);
H5F.close(output_file_id);
catch ME
obj.file_create_date(end) = [];
H5F.close(output_file_id);
if ~isEditingFile
delete(filename);
end
rethrow(ME);
end
end
function o = resolve(obj, path)
if ischar(path)
path = {path};
end
o = cell(size(path));
for i = 1:numel(path)
o{i} = io.resolvePath(obj, path{i});
end
if isscalar(o)
o = o{1};
end
end
function objectMap = searchFor(obj, typename, varargin)
% searchFor - Search for for a given typename within the NwbFile object
%
% Including the full namespace is optional.
%
% .. warning::
% The returned paths are resolvable but do not necessarily
% indicate a real HDF5 path. Their only function is to be resolvable.
objectMap = searchProperties(...
containers.Map,...
obj,...
'',...
typename,...
varargin{:});
end
function nwbTypeNames = listNwbTypes(obj, options)
% listNwbTypes - List all unique NWB (neurodata) types in file
arguments
obj (1,1) NwbFile
options.IncludeParentTypes (1,1) logical = false
end
objectMap = searchProperties(containers.Map, obj, '', '');
objects = objectMap.values();
objectClassNames = cellfun(@(c) string(class(c)), objects);
objectClassNames = unique(objectClassNames);
keep = startsWith(objectClassNames, "types.");
ignore = startsWith(objectClassNames, "types.untyped");
nwbTypeNames = objectClassNames(keep & ~ignore);
if options.IncludeParentTypes
includedNwbTypesWithParents = string.empty;
for i = 1:numel(nwbTypeNames)
typeHierarchy = schemes.utility.listNwbTypeHierarchy(nwbTypeNames{i});
includedNwbTypesWithParents = [includedNwbTypesWithParents, typeHierarchy]; %#ok<AGROW>
end
nwbTypeNames = includedNwbTypesWithParents;
end
end
end
%% PRIVATE
methods(Access=private)
function embedSpecifications(obj, output_file_id)
jsonSpecs = schemes.exportJson();
% Resolve the name of all types and parent types that are
% included in this file. This will be used to filter the specs
% to embed, so that only specs with used neurodata types are
% embedded.
includedNeurodataTypes = obj.listNwbTypes("IncludeParentTypes", true);
% Get the namespace names
namespaceNames = getNamespacesForDataTypes(includedNeurodataTypes);
% In the specs, the hyphen (-) is used as a word separator, while in
% matnwb the underscore (_) is used. Translate names here:
allMatlabNamespaceNames = strrep({jsonSpecs.name}, '-', '_');
[~, keepIdx] = intersect(allMatlabNamespaceNames, namespaceNames, 'stable');
jsonSpecs = jsonSpecs(keepIdx);
io.spec.writeEmbeddedSpecifications(...
output_file_id, ...
jsonSpecs);
io.spec.validateEmbeddedSpecifications(...
output_file_id, ...
strrep(namespaceNames, '_', '-'))
end
function resolveReferences(obj, fid, references)
while ~isempty(references)
resolved = false(size(references));
for iRef = 1:length(references)
refSource = references{iRef};
sourceObj = obj.resolve(refSource);
unresolvedRefs = sourceObj.export(fid, refSource, {});
exportSuccess = isempty(unresolvedRefs);
resolved(iRef) = exportSuccess;
end
errorMessage = sprintf(...
['Object(s) could not be created:\n%s\n\nThe listed '...
'object(s) above contain an ObjectView, RegionView, or ' ...
'SoftLink object that has failed to resolve itself. '...
'Please check for any references that were not assigned ' ...
'to the root NwbFile or if any of the above paths are ' ...
'incorrect.'], file.addSpaces(strjoin(references, newline), 4));
assert( ...
all(resolved), ...
'NWB:NwbFile:UnresolvedReferences', ...
errorMessage ...
)
references(resolved) = [];
end
end
end
end
function tf = metaHasType(mc, typeSuffix)
assert(isa(mc, 'meta.class'));
tf = false;
if contains(mc.Name, typeSuffix, 'IgnoreCase', true)
tf = true;
return;
end
for i = 1:length(mc.SuperclassList)
sc = mc.SuperclassList(i);
if metaHasType(sc, typeSuffix)
tf = true;
return;
end
end
end
function pathToObjectMap = searchProperties(...
pathToObjectMap,...
obj,...
basePath,...
typename,...
varargin)
assert(all(iscellstr(varargin)),...
'NWB:NwbFile:SearchProperties:InvalidVariableArguments',...
'Optional keywords for searchFor must be char arrays.');
shouldSearchSuperClasses = any(strcmpi(varargin, 'includeSubClasses'));
if isa(obj, 'types.untyped.MetaClass')
propertyNames = properties(obj);
getProperty = @(x, prop) x.(prop);
elseif isa(obj, 'types.untyped.Set')
propertyNames = obj.keys();
getProperty = @(x, prop) x.get(prop);
elseif isa(obj, 'types.untyped.Anon')
propertyNames = {obj.name};
getProperty = @(x, prop) x.value;
else
error('NWB:NwbFile:SearchProperties:InvalidType',...
'Invalid object type passed %s', class(obj));
end
searchTypename = @(obj, typename) contains(class(obj), typename, 'IgnoreCase', true);
if shouldSearchSuperClasses
searchTypename = @(obj, typename) metaHasType(metaclass(obj), typename);
end
for i = 1:length(propertyNames)
propName = propertyNames{i};
propValue = getProperty(obj, propName);
fullPath = [basePath '/' propName];
if searchTypename(propValue, typename)
pathToObjectMap(fullPath) = propValue;
end
if isa(propValue, 'types.untyped.GroupClass')...
|| isa(propValue, 'types.untyped.Set')...
|| isa(propValue, 'types.untyped.Anon')
% recursible (even if there is a match!)
searchProperties(pathToObjectMap, propValue, fullPath, typename, varargin{:});
end
end
end
function namespaceNames = getNamespacesForDataTypes(nwbTypeNames)
% getNamespacesOfTypes - Get namespace names for a list of nwb types
arguments
nwbTypeNames (1,:) string
end
namespaceNames = repmat("", size(nwbTypeNames));
pattern = '[types.]+\.(\w+)\.';
for i = 1:numel(nwbTypeNames)
namespaceNames(i) = regexp(nwbTypeNames(i), pattern, 'tokens', 'once');
end
namespaceNames = unique(namespaceNames);
end