-
Notifications
You must be signed in to change notification settings - Fork 32
/
NwbFile.m
218 lines (194 loc) · 7.56 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
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 = obj@types.core.NWBFile(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
jsonSpecs = schemes.exportJson();
io.spec.writeEmbeddedSpecifications(output_file_id, jsonSpecs);
refs = export@types.core.NWBFile(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
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
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