Skip to content

Commit

Permalink
Merge pull request #247 from NeurodataWithoutBorders/242-schema-selector
Browse files Browse the repository at this point in the history
Schema Selector
  • Loading branch information
lawrence-mbf authored Dec 24, 2020
2 parents ac737d0 + de4adbc commit e283f26
Show file tree
Hide file tree
Showing 29 changed files with 96 additions and 2,815 deletions.
8 changes: 2 additions & 6 deletions +tests/+system/NwbTestInterface.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ function setupClass(testCase)

methods (TestMethodSetup)
function setupMethod(testCase)
testCase.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture);
schemaPath = fullfile(testCase.root, 'nwb-schema');

generateCore(...
fullfile(schemaPath, 'hdmf-common-schema', 'common', 'namespace.yaml'),...
fullfile(schemaPath, 'core', 'nwb.namespace.yaml'));
testCase.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture);
generateCore();
testCase.file = NwbFile( ...
'session_description', 'a test NWB File', ...
'identifier', 'TEST123', ...
Expand Down
18 changes: 18 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[submodule "nwb-schema/2.2.5"]
path = nwb-schema/2.2.5
url = https://github.com/NeurodataWithoutBorders/nwb-schema
[submodule "nwb-schema/2.2.4"]
path = nwb-schema/2.2.4
url = https://github.com/NeurodataWithoutBorders/nwb-schema
[submodule "nwb-schema/2.2.3"]
path = nwb-schema/2.2.3
url = https://github.com/NeurodataWithoutBorders/nwb-schema
[submodule "nwb-schema/2.2.2"]
path = nwb-schema/2.2.2
url = https://github.com/NeurodataWithoutBorders/nwb-schema
[submodule "nwb-schema/2.2.1"]
path = nwb-schema/2.2.1
url = https://github.com/NeurodataWithoutBorders/nwb-schema
[submodule "nwb-schema/2.2.0"]
path = nwb-schema/2.2.0
url = https://github.com/NeurodataWithoutBorders/nwb-schema
27 changes: 11 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A Matlab interface for reading and writing Neurodata Without Borders (NWB) 2.0 f
Download the current release of MatNWB from the [MatNWB releases page](https://github.com/NeurodataWithoutBorders/matnwb/releases) or from the [![View NeurodataWithoutBorders/matnwb on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://www.mathworks.com/matlabcentral/fileexchange/67741-neurodatawithoutborders-matnwb). You can also check out the latest development version via

```bash
git clone https://github.com/NeurodataWithoutBorders/matnwb.git
git clone --recurse-submodules https://github.com/NeurodataWithoutBorders/matnwb.git
```

### Step 2: Generate the API
Expand All @@ -19,37 +19,32 @@ From the Matlab command line, add MatNWB to the Matlab path and generate the cor
```matlab
cd matnwb
addpath(genpath(pwd));
generateCore(); % generate core namespace located in the repository.
generateCore(); % generate the most recent nwb-schema release.
```

### Optional: Generate MatNWB code for extensions

The `generateCore` command also takes variable arguments from any extensions.
The `generateExtension` command generates extension classes given a file path to the extension's namespace.

```matlab
generateCore('schema/core/nwb.namespace.yaml', '.../my_extensions1.namespace.yaml',...);
```

You can also generate extensions without generating the core classes in this way:

```matlab
generateExtension('my_extension.namespace.yaml');
generateExtension('schema/core/nwb.namespace.yaml', '.../my_extensions1.namespace.yaml',...);
```

Generated Matlab code will be put a `+types` subdirectory. This is a Matlab package. When the `+types` folder is accessible to the Matlab path, the generated code will be used for reading NWBFiles.

```matlab
nwb=nwbRead('data.nwb');
nwb = nwbRead('data.nwb');
```

### Optional: Download the NWB format schema
### Optional: Generate an older core schema

Download the current release of the NWB format schema from the [nwb-schema releases page](https://github.com/NeurodataWithoutBorders/nwb-schema/releases) or check out the latest development via
The `generateCore` command can generate older versions of the nwb schema.

```bash
git clone --recursive https://github.com/NeurodataWithoutBorders/nwb-schema.git
```matlab
generateCore('2.2.1'); % generate schema 2.2.1
```
Then re-run `generateCore()` to update the API from the updated schema.

Currently, only schemas 2.2.x are supported (2.1.0 and 2.0.1 are not supported at this time).

## Tutorials

Expand Down
47 changes: 26 additions & 21 deletions generateCore.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function generateCore(varargin)
function generateCore(version)
% GENERATECORE Generate Matlab classes from NWB core schema files
% GENERATECORE() Generate classes (Matlab m-files) from the
% NWB:N core namespace file.
% NWB:N core namespace file. By default, generates off of the most recent nwb-schema
% release.
%
% GENERATECORE(core_or_extension_paths,...) Generate classes for the
% core namespace as well as one or more extenstions. Each input filename
% should be an NWB namespace file.
% GENERATECORE(version) Generate classes for the
% core namespace of that version
%
% A cache of schema data is generated in the 'namespaces' subdirectory in
% the current working directory. This is for allowing cross-referencing
Expand All @@ -16,25 +16,30 @@ function generateCore(varargin)
%
% Example:
% generateCore();
% generateCore('schema/core/nwb.namespace.yaml');
% generateCore('schema/my_ext/myext.namespace.yaml');
% generateCore('2.2.3');
%
% See also GENERATEEXTENSION

if nargin == 0
[nwbLocation, ~, ~] = fileparts(mfilename('fullpath'));
schemaPath = fullfile(nwbLocation, 'nwb-schema');
corePath = fullfile(schemaPath, 'core', 'nwb.namespace.yaml');
commonPath = fullfile(schemaPath, 'hdmf-common-schema', ...
'common', 'namespace.yaml');

if 2 == exist(commonPath, 'file')
generateExtension(commonPath);
end

generateExtension(corePath);
version = '2.2.5';
else
for i=1:length(varargin)
generateExtension(varargin{i});
end
validateattributes(version, {'char'}, {'scalartext'});
end

[nwbLocation, ~, ~] = fileparts(mfilename('fullpath'));
schemaPath = fullfile(nwbLocation, 'nwb-schema', version);
corePath = fullfile(schemaPath, 'core', 'nwb.namespace.yaml');
commonPath = fullfile(schemaPath,...
'hdmf-common-schema', ...
'common',...
'namespace.yaml');
assert(2 == exist(corePath, 'file'),...
'MATNWB:nwbread:missingSpec',...
'Cannot find suitable core namespace for schema version `%s`',...
version);

if 2 == exist(commonPath, 'file')
generateExtension(commonPath);
end
generateExtension(corePath);
end
33 changes: 18 additions & 15 deletions generateExtension.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function generateExtension(source)
function generateExtension(varargin)
% GENERATEEXTENSION Generate Matlab classes from NWB extension schema file
% GENERATECORE(extension_path...) Generate classes
% GENERATEEXTENSION(extension_path...) Generate classes
% (Matlab m-files) from one or more NWB:N schema extension namespace
% files. A registry of already generated core types is used to resolve
% dependent types.
Expand All @@ -13,19 +13,22 @@ function generateExtension(source)
% current working directory.
%
% Example:
% generateCore('schema\core\nwb.namespace.yaml');
% generateExtension('schema\myext\myextension.namespace.yaml')
% generateExtension('schema\myext\myextension.namespace.yaml', 'schema\myext2\myext2.namespace.yaml');
%
% See also GENERATECORE
validateattributes(source, {'char', 'string'}, {'scalartext'});

[localpath, ~, ~] = fileparts(source);
assert(2 == exist(source, 'file'),...
'MATNWB:FILE', 'Path to file `%s` could not be found.', source);
fid = fopen(source);
namespaceText = fread(fid, '*char') .';
fclose(fid);

Namespace = spec.generate(namespaceText, localpath);
file.writeNamespace(Namespace.name);
for i = 1:length(varargin)
source = varargin{i};
validateattributes(source, {'char', 'string'}, {'scalartext'});

[localpath, ~, ~] = fileparts(source);
assert(2 == exist(source, 'file'),...
'MATNWB:FILE', 'Path to file `%s` could not be found.', source);
fid = fopen(source);
namespaceText = fread(fid, '*char') .';
fclose(fid);

Namespace = spec.generate(namespaceText, localpath);
file.writeNamespace(Namespace.name);
rehash();
end
end
1 change: 1 addition & 0 deletions nwb-schema/2.2.0
Submodule 2.2.0 added at 62c734
1 change: 1 addition & 0 deletions nwb-schema/2.2.1
Submodule 2.2.1 added at 5d6160
1 change: 1 addition & 0 deletions nwb-schema/2.2.2
Submodule 2.2.2 added at 7931e5
1 change: 1 addition & 0 deletions nwb-schema/2.2.3
Submodule 2.2.3 added at 6b2ca9
1 change: 1 addition & 0 deletions nwb-schema/2.2.4
Submodule 2.2.4 added at 24fba6
1 change: 1 addition & 0 deletions nwb-schema/2.2.5
Submodule 2.2.5 added at aa7702
19 changes: 0 additions & 19 deletions nwb-schema/core/UpdateFromUpstream.sh

This file was deleted.

Loading

0 comments on commit e283f26

Please sign in to comment.