Skip to content

Commit 1256138

Browse files
authored
Improve shape validation of DataPipe objects (#724)
* Update validateShape.m * Make comments wrt enforceScalarSize flag clearer
1 parent 148eb7b commit 1256138

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

+types/+util/checkDims.m

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@ function checkDims(valueSize, validSizes, enforceScalarSize)
55
% of valid sizes, validates that the value size matches at least one of them.
66
%
77
% types.util.CHECKDIMS(valsize, validSizes, enforceScalarSize) optionally
8-
% enforces stricter validation for vectors. By default, MATLAB vectors
9-
% (column: [n,1], row: [1,n]) pass validation against a 1D size (Inf),
10-
% because vector data is typically treated as 1D when written to file.
11-
% However, DataPipe may write vector data as 2D. To prevent 2D sizes from
12-
% being accepted as 1D, use the enforceScalarSize flag.
8+
% enforces stricter validation for scalar/1D shapes. By default, MATLAB
9+
% vectors with shape [n,1] (column) or [1,n] (row) will pass validation
10+
% against a 1D size specification of [Inf], because vector data is typically
11+
% treated as 1D when written to file. However, in some cases (e.g., DataPipe
12+
% maxSize), the 2D shape [n,1] or [1,n] determines the actual shape in the
13+
% exported file. Set enforceScalarSize to true to reject 2D vector shapes
14+
% when a 1D shape like [Inf] is specified for validSize.
15+
%
16+
% Example:
17+
% % Without strict enforcement, [5,1] matches [Inf]:
18+
% types.util.checkDims([5,1], {Inf}, false); % OK
19+
%
20+
% % With strict enforcement, [5,1] does NOT match [Inf]:
21+
% types.util.checkDims([5,1], {Inf}, true); % Error
22+
% types.util.checkDims(5, {Inf}, true); % OK
1323

1424
if nargin < 3 % Set default value for enforceScalarSize if not provided
1525
enforceScalarSize = false;

+types/+util/validateShape.m

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ function validateShape(propertyName, validShapes, value)
1616
end
1717
elseif isa(value, 'types.untyped.DataPipe')
1818
valueShape = value.internal.maxSize;
19-
% For data pipes, vectors can be exported to HDF5 files as 2D arrays
20-
% (columnar (n,1) or row (1,n)). The types.util.checkDims function allows
21-
% this, even if the valid shape specifies that the data should be 1D.
22-
% Use 'enforceScalarShape' to ensure that 2D-like vectors do not
23-
% pass validation when the valid shape specifies 1D data.
19+
% For DataPipe objects, vectors can be exported to HDF5 files as 2D arrays
20+
% with shape [n,1] (column) or [1,n] (row). By default, types.util.checkDims
21+
% allows these 2D shapes to pass validation even when the valid shape
22+
% specifies 1D data (e.g., [Inf]). However, for DataPipe, the maxSize
23+
% property determines the actual shape in the exported file, so we need
24+
% stricter validation. Set 'enforceScalarShape' to true to ensure that
25+
% shapes like [n,1] or [1,n] are not accepted when [Inf] is specified.
2426
enforceScalarShape = true;
2527
elseif istable(value)
2628
valueShape = [height(value) 1];
@@ -49,7 +51,17 @@ function validateShape(propertyName, validShapes, value)
4951
% Check actual size of DataPipe and warn if it is not valid
5052
if isa(value, 'types.untyped.DataPipe')
5153
try
54+
if enforceScalarShape
55+
% Reset enforceScalarShape to false when validating the current
56+
% data size. Strict validation is only needed for maxSize, which
57+
% determines the final shape of the dataset in the exported file.
58+
% The current data size is allowed to be more flexible (e.g.,
59+
% [n,1] can match [Inf]).
60+
enforceScalarShape = false;
61+
end
62+
5263
valueShape = size(value);
64+
5365
types.util.checkDims(valueShape, validShapes, enforceScalarShape);
5466
catch ME
5567
warning(...

0 commit comments

Comments
 (0)