-
Notifications
You must be signed in to change notification settings - Fork 32
/
NwbFile.m
247 lines (224 loc) · 9.24 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
classdef NwbFile < types.core.NWBFile
% NWBFILE Root object representing data read from an NWB file.
%
% Requires that core and extension NWB types have been generated
% and reside in a 'types' package on the matlab path.
%
% Example. Construct an object from scratch for export:
% nwb = NwbFile;
% nwb.epochs = types.core.Epochs;
% nwbExport(nwb, 'epoch.nwb');
%
% See also NWBREAD, GENERATECORE, GENERATEEXTENSION
methods
function obj = NwbFile(varargin)
obj = [email protected](varargin{:});
if strcmp(class(obj), 'NwbFile')
cellStringArguments = convertContainedStringsToChars(varargin(1:2:end));
types.util.checkUnset(obj, unique(cellStringArguments));
end
end
function export(obj, filename)
%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
try
output_file_id = H5F.create(filename);
isEditingFile = false;
catch ME % if file exists, open and edit
if verLessThan('matlab', '9.9') % < 2020b
isEditingFile = strcmp(ME.identifier, 'MATLAB:imagesci:hdf5lib:libraryError')...
&& contains(ME.message, '''File exists''');
else
isEditingFile = strcmp(ME.identifier, 'MATLAB:imagesci:hdf5io:resourceAlreadyExists');
end
if isEditingFile
output_file_id = H5F.open(filename, 'H5F_ACC_RDWR', 'H5P_DEFAULT');
else
rethrow(ME);
end
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)
% Searches this NwbFile object for a given typename
% 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
end
%% PRIVATE
methods(Access=private)
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
if ~any(resolved)
errorFormat = ['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.'];
unresolvedRefs = strjoin(references, newline);
error('NWB:NwbFile:UnresolvedReferences',...
errorFormat, file.addSpaces(unresolvedRefs, 4));
end
references(resolved) = [];
end
end
function embedSpecifications(~, fid)
try
attrId = H5A.open(fid, '/.specloc');
specLocation = H5R.get_name(fid, 'H5R_OBJECT', H5A.read(attrId));
H5A.close(attrId);
catch
specLocation = '/specifications';
io.writeGroup(fid, specLocation);
specView = types.untyped.ObjectView(specLocation);
io.writeAttribute(fid, '/.specloc', specView);
end
JsonData = schemes.exportJson();
for iJson = 1:length(JsonData)
JsonDatum = JsonData(iJson);
schemaNamespaceLocation = strjoin({specLocation, JsonDatum.name}, '/');
namespaceExists = io.writeGroup(fid, schemaNamespaceLocation);
if namespaceExists
namespaceGroupId = H5G.open(fid, schemaNamespaceLocation);
names = getVersionNames(namespaceGroupId);
H5G.close(namespaceGroupId);
for iNames = 1:length(names)
H5L.delete(fid, [schemaNamespaceLocation '/' names{iNames}],...
'H5P_DEFAULT');
end
end
schemaLocation =...
strjoin({schemaNamespaceLocation, JsonDatum.version}, '/');
io.writeGroup(fid, schemaLocation);
Json = JsonDatum.json;
schemeNames = keys(Json);
for iScheme = 1:length(schemeNames)
name = schemeNames{iScheme};
path = [schemaLocation '/' name];
io.writeDataset(fid, path, Json(name));
end
end
end
end
end
function versionNames = getVersionNames(namespaceGroupId)
[~, ~, versionNames] = H5L.iterate(namespaceGroupId,...
'H5_INDEX_NAME', 'H5_ITER_NATIVE',...
0, @removeGroups, {});
function [status, versionNames] = removeGroups(~, name, versionNames)
versionNames{end+1} = name;
status = 0;
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