Home > . > nwbRead.m

nwbRead

PURPOSE ^

NWBREAD Reads an NWB file.

SYNOPSIS ^

function nwb = nwbRead(filename, varargin)

DESCRIPTION ^

NWBREAD Reads an NWB file.
  nwb = nwbRead(filename) Reads the nwb file at filename and returns an
  NWBFile object representing its contents.

  Requires that core and extension NWB types have been generated
  and reside in a 'types' package on the matlab path.

  Example:
    %Generate Matlab code for the NWB objects from the core schema.
    %This only needs to be done once.
    generateCore('schema\core\nwb.namespace.yaml');
    %Now we can read nwb files!
    nwb=nwbRead('data.nwb');

  See also GENERATECORE, GENERATEEXTENSION, NWBFILE, NWBEXPORT

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function nwb = nwbRead(filename, varargin)
0002 %NWBREAD Reads an NWB file.
0003 %  nwb = nwbRead(filename) Reads the nwb file at filename and returns an
0004 %  NWBFile object representing its contents.
0005 %
0006 %  Requires that core and extension NWB types have been generated
0007 %  and reside in a 'types' package on the matlab path.
0008 %
0009 %  Example:
0010 %    %Generate Matlab code for the NWB objects from the core schema.
0011 %    %This only needs to be done once.
0012 %    generateCore('schema\core\nwb.namespace.yaml');
0013 %    %Now we can read nwb files!
0014 %    nwb=nwbRead('data.nwb');
0015 %
0016 %  See also GENERATECORE, GENERATEEXTENSION, NWBFILE, NWBEXPORT
0017 ignorecache = ~isempty(varargin) && ischar(varargin{1}) &&...
0018     strcmp('ignorecache', varargin{1});
0019 if ischar(filename)
0020     validateattributes(filename, {'char'}, {'scalartext', 'nonempty'});
0021     info = h5info(filename);
0022     try
0023         %check for .specloc
0024         fid = H5F.open(filename);
0025         attr_id = H5A.open(fid, '.specloc');
0026         ref_data = H5A.read(attr_id);
0027         blacklist = H5R.get_name(attr_id, 'H5R_OBJECT', ref_data);
0028         if ~ignorecache
0029             generateSpec(fid, h5info(filename, blacklist));
0030             rehash(); %required if we want parseGroup to read the right files.
0031         end
0032         info.Attributes(strcmp('.specloc', {info.Attributes.Name})) = [];
0033         H5A.close(attr_id);
0034         H5F.close(fid);
0035     catch ME
0036         if ~strcmp(ME.identifier, 'MATLAB:imagesci:hdf5lib:libraryError')
0037             rethrow(ME);
0038         end
0039         blacklist = '';
0040     end
0041     nwb = io.parseGroup(filename, info, blacklist);
0042     return;
0043 elseif isstring(filename)
0044     validateattributes(filename, {'string'}, {'nonempty'});
0045 else
0046     validateattributes(filename, {'cell'}, {'nonempty'});
0047     assert(iscellstr(filename));
0048 end
0049 nwb = NwbFile.empty(length(filename), 0);
0050 isStringArray = isstring(filename);
0051 for i=1:length(filename)
0052     if isStringArray
0053         fnm = filename(i);
0054     else
0055         fnm = filename{i};
0056     end
0057     info = h5info(fnm);
0058     nwb(i) = io.parseGroup(fnm, info);
0059 end
0060 end
0061 
0062 function generateSpec(fid, specinfo)
0063 schema = spec.loadSchema();
0064 
0065 for i=1:length(specinfo.Groups)
0066     location = specinfo.Groups(i).Groups(1);
0067     
0068     namespace_name = split(specinfo.Groups(i).Name, '/');
0069     namespace_name = namespace_name{end};
0070     
0071     filenames = {location.Datasets.Name};
0072     if ~any(strcmp('namespace', filenames))
0073         warning('MATNWB:INVALIDCACHE',...
0074         'Couldn''t find a `namespace` in namespace `%s`.  Skipping cache generation.',...
0075         namespace_name);
0076         return;
0077     end
0078     source_names = {location.Datasets.Name};
0079     file_loc = strcat(location.Name, '/', source_names);
0080     schema_map = containers.Map;
0081     for j=1:length(file_loc)
0082         did = H5D.open(fid, file_loc{j});
0083         if strcmp('namespace', source_names{j})
0084             namespace_map = schema.read(H5D.read(did));
0085         else
0086             schema_map(source_names{j}) = H5D.read(did);    
0087         end
0088         H5D.close(did);
0089     end
0090     
0091     spec.generate(namespace_map, schema_map);
0092 end
0093 end

Generated on Fri 09-Aug-2019 14:27:49 by m2html © 2005