Skip to content
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

Change: Only embed specs/namespaces for types that are included in NWB file on export #615

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions NwbFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ function export(obj, filename)
typename,...
varargin{:});
end

function nwbTypeNames = listNwbTypes(obj)
% listNwbTypes - List all unique NWB types in file
objectMap = searchProperties(containers.Map, obj, '', '');

objects = objectMap.values();
objectClassNames = cellfun(@(c) string(class(c)), objects);
objectClassNames = unique(objectClassNames);

keep = startsWith(objectClassNames, "types.");
ignore = startsWith(objectClassNames, "types.untyped");

nwbTypeNames = objectClassNames(keep & ~ignore);
end
end

%% PRIVATE
Expand Down Expand Up @@ -131,7 +145,7 @@ function resolveReferences(obj, fid, references)
end
end

function embedSpecifications(~, fid)
function embedSpecifications(obj, fid)
try
attrId = H5A.open(fid, '/.specloc');
specLocation = H5R.get_name(fid, 'H5R_OBJECT', H5A.read(attrId));
Expand All @@ -144,6 +158,15 @@ function embedSpecifications(~, fid)
end

JsonData = schemes.exportJson();

% Only embed namespaces for types that are included in the file
includedNwbTypes = obj.listNwbTypes();
namespaceNames = getNamespacesOfTypes(includedNwbTypes);

allMatlabNamespaceNames = strrep({JsonData.name}, '-', '_');
[~, keepIdx] = intersect(allMatlabNamespaceNames, namespaceNames, 'stable');
JsonData = JsonData(keepIdx);

for iJson = 1:length(JsonData)
JsonDatum = JsonData(iJson);
schemaNamespaceLocation = strjoin({specLocation, JsonDatum.name}, '/');
Expand Down Expand Up @@ -244,4 +267,19 @@ function embedSpecifications(~, fid)
searchProperties(pathToObjectMap, propValue, fullPath, typename, varargin{:});
end
end
end
end

function namespaceNames = getNamespacesOfTypes(nwbTypeNames)
% getNamespacesOfTypes - Get namespace names for a list of nwb types
arguments
nwbTypeNames (1,:) string
end

namespaceNames = repmat("", size(nwbTypeNames));
pattern = '[types.]+\.(\w+)\.';

for i = 1:numel(nwbTypeNames)
namespaceNames(i) = regexp(nwbTypeNames(i), pattern, 'tokens', 'once');
end
namespaceNames = unique(namespaceNames);
end