-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from NeurodataWithoutBorders/todo/file-amend
Allow Amending NWB Files
- Loading branch information
Showing
14 changed files
with
353 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,40 @@ | ||
function writeDataset(fid, fullpath, data, varargin) | ||
assert(isempty(varargin) || iscellstr(varargin),... | ||
'options should be character arrays.'); | ||
[tid, sid, data] = io.mapData2H5(fid, data, varargin{:}); | ||
did = H5D.create(fid, fullpath, tid, sid, 'H5P_DEFAULT'); | ||
[~, dims, ~] = H5S.get_simple_extent_dims(sid); | ||
try | ||
dcpl = H5P.create('H5P_DATASET_CREATE'); | ||
if any(strcmp('forceChunking', varargin)) | ||
H5P.set_chunk(dcpl, dims) | ||
end | ||
did = H5D.create(fid, fullpath, tid, sid, dcpl); | ||
H5P.close(dcpl); | ||
catch ME | ||
if contains(ME.message, 'name already exists') | ||
did = H5D.open(fid, fullpath); | ||
create_plist = H5D.get_create_plist(did); | ||
edit_sid = H5D.get_space(did); | ||
[~, edit_dims, ~] = H5S.get_simple_extent_dims(edit_sid); | ||
layout = H5P.get_layout(create_plist); | ||
is_chunked = layout == H5ML.get_constant_value('H5D_CHUNKED'); | ||
is_same_dims = all(edit_dims == dims); | ||
if ~is_same_dims && is_chunked | ||
H5D.set_extent(did, dims); | ||
elseif ~is_same_dims | ||
warning('Attempted to change size of continuous dataset `%s`. Skipping.',... | ||
fullpath); | ||
end | ||
H5P.close(create_plist); | ||
H5S.close(edit_sid); | ||
else | ||
rethrow(ME); | ||
end | ||
end | ||
H5D.write(did, tid, sid, sid, 'H5P_DEFAULT', data); | ||
H5D.close(did); | ||
if isa(tid, 'H5ML.id') | ||
H5T.close(tid); | ||
end | ||
H5S.close(sid); | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
classdef AmendTest < tests.system.NwbTestInterface | ||
methods (Test) | ||
function testAmend(testCase) | ||
filename = ['MatNWB.' testCase.className() '.testRoundTrip.nwb']; | ||
nwbExport(testCase.file, filename); | ||
testCase.appendContainer(testCase.file); | ||
nwbExport(testCase.file, filename); | ||
|
||
writeContainer = testCase.getContainer(testCase.file); | ||
readFile = nwbRead(filename); | ||
readContainer = testCase.getContainer(readFile); | ||
testCase.verifyContainerEqual(readContainer, writeContainer); | ||
end | ||
end | ||
|
||
methods (Abstract) | ||
appendContainer(testCase, file); | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
classdef NWBFileIOTest < tests.system.PyNWBIOTest | ||
methods | ||
function addContainer(testCase, file) %#ok<INUSL> | ||
ts = types.core.TimeSeries(... | ||
'data', int32(100:10:190) .', ... | ||
'data_unit', 'SIunit', ... | ||
'timestamps', (0:9) .', ... | ||
'data_resolution', 0.1); | ||
file.acquisition.set('test_timeseries', ts); | ||
clust = types.core.Clustering( ... | ||
'description', 'A fake Clustering interface', ... | ||
'num', [0, 1, 2, 0, 1, 2] .', ... | ||
'peak_over_rms', [100, 101, 102] .', ... | ||
'times', (10:10:60) .'); | ||
mod = types.core.ProcessingModule( ... | ||
'description', 'a test module', ... | ||
'Clustering', clust); | ||
file.processing.set('test_module', mod); | ||
methods | ||
function addContainer(testCase, file) %#ok<INUSL> | ||
ts = types.core.TimeSeries(... | ||
'data', int32(100:10:190) .', ... | ||
'data_unit', 'SIunit', ... | ||
'timestamps', (0:9) .', ... | ||
'data_resolution', 0.1); | ||
file.acquisition.set('test_timeseries', ts); | ||
clust = types.core.Clustering( ... | ||
'description', 'A fake Clustering interface', ... | ||
'num', [0, 1, 2, 0, 1, 2] .', ... | ||
'peak_over_rms', [100, 101, 102] .', ... | ||
'times', (10:10:60) .'); | ||
mod = types.core.ProcessingModule( ... | ||
'description', 'a test module', ... | ||
'Clustering', clust); | ||
file.processing.set('test_module', mod); | ||
end | ||
|
||
function c = getContainer(testCase, file) %#ok<INUSL> | ||
c = file; | ||
end | ||
end | ||
|
||
function c = getContainer(testCase, file) %#ok<INUSL> | ||
c = file; | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
classdef NwbTestInterface < matlab.unittest.TestCase | ||
properties | ||
% registry | ||
file | ||
root; | ||
end | ||
|
||
methods (TestClassSetup) | ||
function setupClass(testCase) | ||
rootPath = fullfile(fileparts(mfilename('fullpath')), '..', '..'); | ||
testCase.applyFixture(matlab.unittest.fixtures.PathFixture(rootPath)); | ||
testCase.root = rootPath; | ||
end | ||
end | ||
|
||
methods (TestMethodSetup) | ||
function setupMethod(testCase) | ||
testCase.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture); | ||
generateCore(fullfile(testCase.root, ... | ||
'schema', 'core', 'nwb.namespace.yaml')); | ||
testCase.file = NwbFile( ... | ||
'session_description', 'a test NWB File', ... | ||
'identifier', 'TEST123', ... | ||
'session_start_time', '2018-12-02T12:57:27.371444-08:00', ... | ||
'file_create_date', datestr([2017, 4, 15, 12, 0, 0], 'yyyy-mm-dd HH:MM:SS'),... | ||
'timestamps_reference_time', '2018-12-02T12:57:27.371444-08:00'); | ||
testCase.addContainer(testCase.file); | ||
end | ||
end | ||
|
||
methods | ||
function n = className(testCase) | ||
classSplit = strsplit(class(testCase), '.'); | ||
n = classSplit{end}; | ||
end | ||
|
||
function verifyContainerEqual(testCase, actual, expected) | ||
testCase.verifyEqual(class(actual), class(expected)); | ||
props = properties(actual); | ||
for i = 1:numel(props) | ||
prop = props{i}; | ||
if strcmp(prop, 'file_create_date') | ||
continue; | ||
end | ||
val1 = actual.(prop); | ||
val2 = expected.(prop); | ||
failmsg = ['Values for property ''' prop ''' are not equal']; | ||
if startsWith(class(val1), 'types.core.') | ||
verifyContainerEqual(testCase, val1, val2); | ||
elseif isa(val1, 'types.untyped.Set') | ||
verifySetEqual(testCase, val1, val2, failmsg); | ||
elseif isdatetime(val1) | ||
testCase.verifyEqual(char(val1), char(val2)); | ||
else | ||
if isa(val1, 'types.untyped.DataStub') | ||
trueval = val1.load(); | ||
else | ||
trueval = val1; | ||
end | ||
|
||
if isvector(val2) && isvector(trueval) && numel(val2) == numel(trueval) | ||
trueval = reshape(trueval, size(val2)); | ||
end | ||
testCase.verifyEqual(trueval, val2, failmsg); | ||
end | ||
end | ||
end | ||
|
||
function verifySetEqual(testCase, actual, expected, failmsg) | ||
testCase.verifyEqual(class(actual), class(expected)); | ||
ak = actual.keys(); | ||
ek = expected.keys(); | ||
verifyTrue(testCase, isempty(setxor(ak, ek)), failmsg); | ||
for i=1:numel(ak) | ||
key = ak{i}; | ||
verifyContainerEqual(testCase, actual.get(key), ... | ||
expected.get(key)); | ||
end | ||
end | ||
|
||
function verifyUntypedEqual(testCase, actual, expected) | ||
testCase.verifyEqual(class(actual), class(expected)); | ||
props = properties(actual); | ||
for i = 1:numel(props) | ||
prop = props{i}; | ||
val1 = actual.(prop); | ||
val2 = expected.(prop); | ||
if isa(val1, 'types.core.NWBContainer') || isa(val1, 'types.core.NWBData') | ||
verifyContainerEqual(testCase, val1, val2); | ||
else | ||
testCase.verifyEqual(val1, val2, ... | ||
['Values for property ''' prop ''' are not equal']); | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
||
methods(Abstract) | ||
addContainer(testCase, file); | ||
c = getContainer(testCase, file); | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.