forked from NeurodataWithoutBorders/matnwb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnwbRead.m
119 lines (109 loc) · 3.59 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
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',
%
% See also GENERATECORE, GENERATEEXTENSION, NWBFILE, NWBEXPORT
ignoreCache = ~isempty(varargin) && ischar(varargin{1}) &&...
strcmp('ignorecache', varargin{1});
Blacklist = struct(...
'attributes', {{'.specloc', 'object_id'}},...
'groups', {{}});
validateattributes(filename, {'char', 'string'}, {'scalartext', 'nonempty'});
specLocation = getEmbeddedSpec(filename);
if ~isempty(specLocation)
Blacklist.groups{end+1} = specLocation;
end
if ~ignoreCache
if isempty(specLocation)
try
generateCore(util.getSchemaVersion(filename));
catch ME
if ~strcmp(ME.identifier, 'NWB:GenerateCore:MissingCoreSchema')
rethrow(ME);
end
end
else
generateSpec(filename, h5info(filename, specLocation));
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)
specNames = cell(size(specinfo.Groups));
fid = H5F.open(filename);
for i=1:length(specinfo.Groups)
location = specinfo.Groups(i).Groups(1);
namespaceName = split(specinfo.Groups(i).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 j=1:length(fileLocation)
did = H5D.open(fid, fileLocation{j});
if strcmp('namespace', sourceNames{j})
namespaceText = H5D.read(did);
else
schemaMap(sourceNames{j}) = H5D.read(did);
end
H5D.close(did);
end
Namespace = spec.generate(namespaceText, schemaMap);
spec.saveCache(Namespace);
specNames{i} = Namespace.name;
end
H5F.close(fid);
fid = [];
missingNames = cell(size(specNames));
for i = 1:length(specNames)
name = specNames{i};
try
file.writeNamespace(name);
catch ME
if strcmp(ME.identifier, 'NWB:Namespace:CacheMissing')
missingNames{i} = 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