Skip to content

Commit

Permalink
Only include namespaces for types that are included in NWB file on ex…
Browse files Browse the repository at this point in the history
…port (Issue #607)
  • Loading branch information
ehennestad committed Nov 4, 2024
1 parent 95b5e5e commit a458d62
Showing 1 changed file with 40 additions and 2 deletions.
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

0 comments on commit a458d62

Please sign in to comment.