-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor: Add arguments block to main MatNWB api functions #619
Changes from all commits
4a305d0
0bec70d
895fa1f
39bb147
1c1d7ec
2a31310
ff62e74
1b3da95
ef2c592
0c0671f
a2ac9f0
cd21682
ef99eb9
a78a239
29a3021
efafb53
5429e56
d92dacb
ffdc5b8
79f2603
d7b9edd
7469fa5
0282719
2f30b4d
6fc75ce
53057ca
ac3b990
3aaed8f
0749a6a
3e2fa00
2b12acb
befd8fe
a6c61d4
00d1623
f97b6a9
b18424a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplify |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extracted code from nwbRead and added to function |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function specLocation = readEmbeddedSpecLocation(fid, specLocAttributeName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the difference between readEmbeddedSpecLocaation and getEmbeddedSpecLocation? Can you add a comment here indicating the distinction? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
arguments | ||
fid (1,1) H5ML.id | ||
specLocAttributeName (1,1) string = '.specloc' | ||
end | ||
|
||
specLocation = ''; | ||
try % Check .specloc | ||
attributeId = H5A.open(fid, specLocAttributeName); | ||
attributeCleanup = onCleanup(@(id) H5A.close(attributeId)); | ||
referenceRawData = H5A.read(attributeId); | ||
specLocation = H5R.get_name(attributeId, 'H5R_OBJECT', referenceRawData); | ||
catch ME | ||
if ~strcmp(ME.identifier, 'MATLAB:imagesci:hdf5lib:libraryError') | ||
rethrow(ME); | ||
end % don't error if the attribute doesn't exist. | ||
end | ||
end |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extracted code from |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function specLocation = getEmbeddedSpecLocation(filename, options) | ||
% getEmbeddedSpecLocation - Get location of embedded specs in NWB file | ||
% | ||
% Note: Returns an empty string if the spec location does not exist | ||
% | ||
% See also io.spec.internal.readEmbeddedSpecLocation | ||
|
||
arguments | ||
filename (1,1) string {matnwb.common.mustBeNwbFile} | ||
options.SpecLocAttributeName (1,1) string = '.specloc' | ||
end | ||
|
||
fid = H5F.open(filename); | ||
fileCleanup = onCleanup(@(id) H5F.close(fid) ); | ||
specLocation = io.spec.internal.readEmbeddedSpecLocation(fid, options.SpecLocAttributeName); | ||
end |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extracted code from |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
function specs = readEmbeddedSpecifications(filename, specLocation) | ||
% readEmbeddedSpecifications - Read embedded specs from an NWB file | ||
% | ||
% specs = io.spec.readEmbeddedSpecifications(filename, specLocation) read | ||
% embedded specs from the specLocation in an NWB file | ||
% | ||
% Inputs: | ||
% filename (string) : Absolute path of an nwb file | ||
% specLocation (string) : h5 path for the location of specs inside the NWB file | ||
% | ||
% Outputs | ||
% specs cell: A cell array of structs with one element for each embedded | ||
% specification. Each struct has two fields: | ||
% | ||
% - namespaceName (char) : Name of the namespace for a specification | ||
% - namespaceText (char) : The namespace declaration for a specification | ||
% - schemaMap (containers.Map): A set of schema specifications for the namespace | ||
|
||
arguments | ||
filename (1,1) string {matnwb.common.mustBeNwbFile} | ||
specLocation (1,1) string | ||
end | ||
|
||
specInfo = h5info(filename, specLocation); | ||
specs = deal( cell(size(specInfo.Groups)) ); | ||
|
||
fid = H5F.open(filename); | ||
fileCleanup = onCleanup(@(id) H5F.close(fid) ); | ||
|
||
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 | ||
|
||
specs{iGroup}.namespaceName = namespaceName; | ||
specs{iGroup}.namespaceText = namespaceText; | ||
specs{iGroup}.schemaMap = schemaMap; | ||
end | ||
end |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extracted function from |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
function writeEmbeddedSpecifications(fid, jsonSpecs) | ||
specLocation = io.spec.internal.readEmbeddedSpecLocation(fid); | ||
|
||
if isempty(specLocation) | ||
specLocation = '/specifications'; | ||
io.writeGroup(fid, specLocation); | ||
specView = types.untyped.ObjectView(specLocation); | ||
io.writeAttribute(fid, '/.specloc', specView); | ||
end | ||
|
||
for iJson = 1:length(jsonSpecs) | ||
JsonDatum = jsonSpecs(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 | ||
|
||
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 |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved rehash statement to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fileread
instead offopen
,fread
,fclose
sequence