forked from NeurodataWithoutBorders/matnwb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nwbRead.m
176 lines (158 loc) · 6.37 KB
/
nwbRead.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
function nwb = nwbRead(filename, varargin)
%NWBREAD Reads an NWB file.
% nwb = NWBREAD(filename) Reads the nwb file at filename and returns an
% NWBFile object representing its contents.
% nwb = nwbRead(filename, 'ignorecache') Reads the nwb file without generating classes
% off of the cached schema if one exists.
%
% nwb = NWBREAD(filename, options)
%
% Requires that core and extension NWB types have been generated
% and reside in a 'types' package on the matlab path.
%
% Example:
% nwb = nwbRead('data.nwb');
% nwb = nwbRead('data.nwb', 'ignorecache');
% nwb = nwbRead('data.nwb', 'savedir', '.');
%
% See also GENERATECORE, GENERATEEXTENSION, NWBFILE, NWBEXPORT
for iOption = 1:length(varargin)
option = varargin{iOption};
if isstring(option)
option = char(option);
end
assert(ischar(option), 'NWB:Read:InvalidParameter' ...
, 'Invalid optional parameter in argument position %u', 1 + iOption);
varargin{iOption} = option;
end
ignoreCache = any(strcmpi(varargin, 'ignorecache'));
saveDirMask = strcmpi(varargin, 'savedir');
assert(isempty(saveDirMask) || ~saveDirMask(end), 'NWB:NWBRead:InvalidSaveDir',...
'`savedir` is a key value pair requiring a directory string as a value.');
if any(saveDirMask)
saveDir = varargin{find(saveDirMask, 1, 'last') + 1};
else
saveDir = misc.getMatnwbDir();
end
Blacklist = struct(...
'attributes', {{'.specloc', 'object_id'}},...
'groups', {{}});
validateattributes(filename, {'char', 'string'}, {'scalartext', 'nonempty'} ...
, 'nwbRead', 'filename', 1);
filename = char(filename);
specLocation = getEmbeddedSpec(filename);
schemaVersion = util.getSchemaVersion(filename);
if ~isempty(specLocation)
Blacklist.groups{end+1} = specLocation;
end
% validate supported schema version
Schemas = dir(fullfile(misc.getMatnwbDir(), 'nwb-schema'));
supportedSchemas = setdiff({Schemas.name}, {'.', '..'});
if ~any(strcmp(schemaVersion, supportedSchemas))
warning('NWB:Read:UnsupportedSchema' ...
, ['NWB schema version %s is not support by this version of MatNWB. ' ...
'This file is not guaranteed to be supported.'] ...
, schemaVersion);
end
if ~ignoreCache
if isempty(specLocation)
try
generateCore(schemaVersion, 'savedir', saveDir);
catch ME
if ~strcmp(ME.identifier, 'NWB:GenerateCore:MissingCoreSchema')
rethrow(ME);
end
end
else
generateSpec(filename, h5info(filename, specLocation), 'savedir', saveDir);
end
rehash();
end
nwb = io.parseGroup(filename, h5info(filename), Blacklist);
end
function specLocation = getEmbeddedSpec(filename)
specLocation = '';
fid = H5F.open(filename);
try
%check for .specloc
attributeId = H5A.open(fid, '.specloc');
referenceRawData = H5A.read(attributeId);
specLocation = H5R.get_name(attributeId, 'H5R_OBJECT', referenceRawData);
H5A.close(attributeId);
catch ME
if ~strcmp(ME.identifier, 'MATLAB:imagesci:hdf5lib:libraryError')
rethrow(ME);
end % don't error if the attribute doesn't exist.
end
H5F.close(fid);
end
function generateSpec(filename, specinfo, varargin)
saveDirMask = strcmp(varargin, 'savedir');
if any(saveDirMask)
assert(~saveDirMask(end),...
'NWB:Read:InvalidParameter',...
'savedir must be paired with the desired save directory.');
saveDir = varargin{find(saveDirMask, 1, 'last') + 1};
else
saveDir = misc.getMatnwbDir();
end
specNames = cell(size(specinfo.Groups));
fid = H5F.open(filename);
for iGroup = 1:length(specinfo.Groups)
location = specinfo.Groups(iGroup).Groups(1);
namespaceName = split(specinfo.Groups(iGroup).Name, '/');
namespaceName = namespaceName{end};
filenames = {location.Datasets.Name};
if ~any(strcmp('namespace', filenames))
warning('NWB:Read:GenerateSpec:CacheInvalid',...
'Couldn''t find a `namespace` in namespace `%s`. Skipping cache generation.',...
namespaceName);
return;
end
sourceNames = {location.Datasets.Name};
fileLocation = strcat(location.Name, '/', sourceNames);
schemaMap = containers.Map;
for iFileLocation = 1:length(fileLocation)
did = H5D.open(fid, fileLocation{iFileLocation});
if strcmp('namespace', sourceNames{iFileLocation})
namespaceText = H5D.read(did);
else
schemaMap(sourceNames{iFileLocation}) = H5D.read(did);
end
H5D.close(did);
end
Namespaces = spec.generate(namespaceText, schemaMap);
% Handle embedded namespaces.
Namespace = Namespaces(strcmp({Namespaces.name}, namespaceName));
if isempty(Namespace)
% legacy checks in case namespaceName is using the old underscore
% conversion name.
namespaceName = strrep(namespaceName, '_', '-');
Namespace = Namespaces(strcmp({Namespaces.name}, namespaceName));
end
assert(~isempty(Namespace), ...
'NWB:Namespace:NameNotFound', ...
'Namespace %s not found in schema. Perhaps an extension should be generated?', ...
namespaceName);
spec.saveCache(Namespace, saveDir);
specNames{iGroup} = Namespace.name;
end
H5F.close(fid);
missingNames = cell(size(specNames));
for iName = 1:length(specNames)
name = specNames{iName};
try
file.writeNamespace(name, saveDir);
catch ME
if strcmp(ME.identifier, 'NWB:Namespace:CacheMissing')
missingNames{iName} = name;
else
rethrow(ME);
end
end
end
missingNames(cellfun('isempty', missingNames)) = [];
assert(isempty(missingNames), 'NWB:Namespace:DependencyMissing',...
'Missing generated caches and dependent caches for the following namespaces:\n%s',...
misc.cellPrettyPrint(missingNames));
end