Skip to content

Change DataPipe shape validation to rely on maxSize instead of actual size. #716

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

Merged
merged 9 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
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
31 changes: 8 additions & 23 deletions +file/fillValidators.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
elseif isa(prop, 'file.Attribute')
unitValidationStr = strjoin({unitValidationStr...
fillDtypeValidation(name, prop.dtype)...
fillDimensionValidation(prop.dtype, prop.shape)...
fillDimensionValidation(name, prop.dtype, prop.shape)...
}, newline);
else % Link
fullname = namespaceReg.getFullClassName(prop.type);
Expand Down Expand Up @@ -154,7 +154,7 @@
if isempty(prop.type)
unitValidationStr = strjoin({unitValidationStr...
fillDtypeValidation(name, prop.dtype)...
fillDimensionValidation(prop.dtype, prop.shape)...
fillDimensionValidation(name, prop.dtype, prop.shape)...
}, newline);
elseif prop.isConstrainedSet
fullname = getFullClassName(namespaceReg, prop.type, name);
Expand Down Expand Up @@ -200,7 +200,7 @@
);
end

function fdvstr = fillDimensionValidation(type, shape)
function fdvstr = fillDimensionValidation(name, type, shape)
if strcmp(type, 'any')
fdvstr = '';
return;
Expand All @@ -214,33 +214,18 @@
end
shape{i} = ['[' strjoin(shape{i}, ',') ']'];
end
shapeStr = ['{' strjoin(shape, ', ') '}'];
validShapeStr = ['{' strjoin(shape, ', ') '}'];
else
for i = 1:length(shape)
shape{i} = num2str(shape{i});
end
shapeStr = ['{[' strjoin(shape, ',') ']}'];
validShapeStr = ['{[' strjoin(shape, ',') ']}'];
end
else
shapeStr = ['{[' num2str(shape) ']}'];
validShapeStr = ['{[' num2str(shape) ']}'];
end

fdvstr = strjoin({...
'if isa(val, ''types.untyped.DataStub'')' ...
' if 1 == val.ndims' ...
' valsz = [val.dims 1];' ...
' else' ...
' valsz = val.dims;' ...
' end' ...
'elseif istable(val)' ...
' valsz = [height(val) 1];'...
'elseif ischar(val)'...
' valsz = [size(val, 1) 1];'...
'else'...
' valsz = size(val);'...
'end' ...
['validshapes = ' shapeStr ';']...
'types.util.checkDims(valsz, validshapes);'}, newline);
fdvstr = sprintf('types.util.validateShape(''%s'', %s, val)', name, validShapeStr);
end

%NOTE: can return empty strings
Expand Down Expand Up @@ -345,4 +330,4 @@
['Namespace could not be found for type `%s`.' ...
' Skipping Validation for property `%s`.'], propType, name);
end
end
end
8 changes: 7 additions & 1 deletion +io/+config/+internal/configureDataPipeFromData.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
import types.untyped.datapipe.properties.DynamicFilter

chunkSize = computeChunkSizeFromConfig(numericData, datasetConfig.chunking);
maxSize = size(numericData);
if isvector(numericData)
% If input data is vector, we use maxSize = Inf to enforce a 1D
% columnar representation of data in file.
maxSize = Inf;
else
maxSize = size(numericData);
end

dataPipeArgs = {...
"data", numericData, ...
Expand Down
23 changes: 23 additions & 0 deletions +tests/+unit/dataPipeTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,29 @@ function testOverrideBoundPipeProperties(testCase)
testCase.verifyEqual(ME.identifier, 'NWB:BoundPipe:CannotSetPipeProperty')
end
end

function testShapeValidation(testCase)
% Create a DataPipe with both maxSize and actual size that are
% valid
dataPipe = types.untyped.DataPipe( 'data', rand(50, 50, 3), 'maxSize', [50,50,inf] );
try
imageSeries = types.core.ImageSeries('data', dataPipe, 'data_unit', 'test'); %#ok<NASGU>
catch
testCase.verifyFail('Expected DataPipe with valid shape for ImageSeries to pass')
end
% Create a DataPipe where maxSize is invalid
dataPipe = types.untyped.DataPipe( 'data', rand(50, 50, 3, 4, 10), 'maxSize', [50, 50, 3, 4, inf] );
testCase.verifyError(...
@() types.core.ImageSeries('data', dataPipe, 'data_unit', 'test'), ...
'NWB:CheckDims:InvalidDimensions')

% Create a DataPipe where maxSize is valid and actual size is
% invalid
dataPipe = types.untyped.DataPipe( 'data', rand(50, 50, 3, 4, 10), 'maxSize', [50, 50, 3, inf] );
testCase.verifyWarning(...
@() types.core.ImageSeries('data', dataPipe, 'data_unit', 'test'), ...
'NWB:ValidateShape:InvalidDataPipeSize')
end
end

methods (Test, TestTags={'UsesDynamicallyLoadedFilters'})
Expand Down
64 changes: 4 additions & 60 deletions +types/+core/AbstractFeatureSeries.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,75 +90,19 @@

function val = validate_data(obj, val)
val = types.util.checkDtype('data', 'numeric', val);
if isa(val, 'types.untyped.DataStub')
if 1 == val.ndims
valsz = [val.dims 1];
else
valsz = val.dims;
end
elseif istable(val)
valsz = [height(val) 1];
elseif ischar(val)
valsz = [size(val, 1) 1];
else
valsz = size(val);
end
validshapes = {[Inf,Inf], [Inf]};
types.util.checkDims(valsz, validshapes);
types.util.validateShape('data', {[Inf,Inf], [Inf]}, val)
end
function val = validate_data_unit(obj, val)
val = types.util.checkDtype('data_unit', 'char', val);
if isa(val, 'types.untyped.DataStub')
if 1 == val.ndims
valsz = [val.dims 1];
else
valsz = val.dims;
end
elseif istable(val)
valsz = [height(val) 1];
elseif ischar(val)
valsz = [size(val, 1) 1];
else
valsz = size(val);
end
validshapes = {[1]};
types.util.checkDims(valsz, validshapes);
types.util.validateShape('data_unit', {[1]}, val)
end
function val = validate_feature_units(obj, val)
val = types.util.checkDtype('feature_units', 'char', val);
if isa(val, 'types.untyped.DataStub')
if 1 == val.ndims
valsz = [val.dims 1];
else
valsz = val.dims;
end
elseif istable(val)
valsz = [height(val) 1];
elseif ischar(val)
valsz = [size(val, 1) 1];
else
valsz = size(val);
end
validshapes = {[Inf]};
types.util.checkDims(valsz, validshapes);
types.util.validateShape('feature_units', {[Inf]}, val)
end
function val = validate_features(obj, val)
val = types.util.checkDtype('features', 'char', val);
if isa(val, 'types.untyped.DataStub')
if 1 == val.ndims
valsz = [val.dims 1];
else
valsz = val.dims;
end
elseif istable(val)
valsz = [height(val) 1];
elseif ischar(val)
valsz = [size(val, 1) 1];
else
valsz = size(val);
end
validshapes = {[Inf]};
types.util.checkDims(valsz, validshapes);
types.util.validateShape('features', {[Inf]}, val)
end
%% EXPORT
function refs = export(obj, fid, fullpath, refs)
Expand Down
16 changes: 1 addition & 15 deletions +types/+core/AnnotationSeries.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,7 @@

function val = validate_data(obj, val)
val = types.util.checkDtype('data', 'char', val);
if isa(val, 'types.untyped.DataStub')
if 1 == val.ndims
valsz = [val.dims 1];
else
valsz = val.dims;
end
elseif istable(val)
valsz = [height(val) 1];
elseif ischar(val)
valsz = [size(val, 1) 1];
else
valsz = size(val);
end
validshapes = {[Inf]};
types.util.checkDims(valsz, validshapes);
types.util.validateShape('data', {[Inf]}, val)
end
function val = validate_data_resolution(obj, val)
if isequal(val, -1)
Expand Down
48 changes: 3 additions & 45 deletions +types/+core/ClusterWaveforms.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,57 +84,15 @@
end
function val = validate_waveform_filtering(obj, val)
val = types.util.checkDtype('waveform_filtering', 'char', val);
if isa(val, 'types.untyped.DataStub')
if 1 == val.ndims
valsz = [val.dims 1];
else
valsz = val.dims;
end
elseif istable(val)
valsz = [height(val) 1];
elseif ischar(val)
valsz = [size(val, 1) 1];
else
valsz = size(val);
end
validshapes = {[1]};
types.util.checkDims(valsz, validshapes);
types.util.validateShape('waveform_filtering', {[1]}, val)
end
function val = validate_waveform_mean(obj, val)
val = types.util.checkDtype('waveform_mean', 'single', val);
if isa(val, 'types.untyped.DataStub')
if 1 == val.ndims
valsz = [val.dims 1];
else
valsz = val.dims;
end
elseif istable(val)
valsz = [height(val) 1];
elseif ischar(val)
valsz = [size(val, 1) 1];
else
valsz = size(val);
end
validshapes = {[Inf,Inf]};
types.util.checkDims(valsz, validshapes);
types.util.validateShape('waveform_mean', {[Inf,Inf]}, val)
end
function val = validate_waveform_sd(obj, val)
val = types.util.checkDtype('waveform_sd', 'single', val);
if isa(val, 'types.untyped.DataStub')
if 1 == val.ndims
valsz = [val.dims 1];
else
valsz = val.dims;
end
elseif istable(val)
valsz = [height(val) 1];
elseif ischar(val)
valsz = [size(val, 1) 1];
else
valsz = size(val);
end
validshapes = {[Inf,Inf]};
types.util.checkDims(valsz, validshapes);
types.util.validateShape('waveform_sd', {[Inf,Inf]}, val)
end
%% EXPORT
function refs = export(obj, fid, fullpath, refs)
Expand Down
64 changes: 4 additions & 60 deletions +types/+core/Clustering.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,75 +72,19 @@

function val = validate_description(obj, val)
val = types.util.checkDtype('description', 'char', val);
if isa(val, 'types.untyped.DataStub')
if 1 == val.ndims
valsz = [val.dims 1];
else
valsz = val.dims;
end
elseif istable(val)
valsz = [height(val) 1];
elseif ischar(val)
valsz = [size(val, 1) 1];
else
valsz = size(val);
end
validshapes = {[1]};
types.util.checkDims(valsz, validshapes);
types.util.validateShape('description', {[1]}, val)
end
function val = validate_num(obj, val)
val = types.util.checkDtype('num', 'int32', val);
if isa(val, 'types.untyped.DataStub')
if 1 == val.ndims
valsz = [val.dims 1];
else
valsz = val.dims;
end
elseif istable(val)
valsz = [height(val) 1];
elseif ischar(val)
valsz = [size(val, 1) 1];
else
valsz = size(val);
end
validshapes = {[Inf]};
types.util.checkDims(valsz, validshapes);
types.util.validateShape('num', {[Inf]}, val)
end
function val = validate_peak_over_rms(obj, val)
val = types.util.checkDtype('peak_over_rms', 'single', val);
if isa(val, 'types.untyped.DataStub')
if 1 == val.ndims
valsz = [val.dims 1];
else
valsz = val.dims;
end
elseif istable(val)
valsz = [height(val) 1];
elseif ischar(val)
valsz = [size(val, 1) 1];
else
valsz = size(val);
end
validshapes = {[Inf]};
types.util.checkDims(valsz, validshapes);
types.util.validateShape('peak_over_rms', {[Inf]}, val)
end
function val = validate_times(obj, val)
val = types.util.checkDtype('times', 'double', val);
if isa(val, 'types.untyped.DataStub')
if 1 == val.ndims
valsz = [val.dims 1];
else
valsz = val.dims;
end
elseif istable(val)
valsz = [height(val) 1];
elseif ischar(val)
valsz = [size(val, 1) 1];
else
valsz = size(val);
end
validshapes = {[Inf]};
types.util.checkDims(valsz, validshapes);
types.util.validateShape('times', {[Inf]}, val)
end
%% EXPORT
function refs = export(obj, fid, fullpath, refs)
Expand Down
Loading
Loading