Skip to content

Commit 7622ea8

Browse files
committed
hdf5: work more broadly
1 parent 3d2e831 commit 7622ea8

File tree

10 files changed

+87
-73
lines changed

10 files changed

+87
-73
lines changed

+stdlib/auto_chunk_size.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
% * dims: proposed dataset dimensions (like size())
88

99
function csize = auto_chunk_size(dims)
10-
arguments
11-
dims (1,:) {mustBeInteger,mustBePositive}
12-
end
10+
% arguments
11+
% dims (1,:) {mustBeInteger,mustBePositive}
12+
% end
1313

1414
CHUNK_BASE = 16000; % Multiplier by which chunks are adjusted
1515
CHUNK_MIN = 8000; % lower limit: 8 kbyte

+stdlib/h5create_group.m

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
% * HDF5 file handle
88

99
function fid = h5create_group(file, hpath)
10-
arguments
11-
file (1,1)
12-
hpath (1,1) string
13-
end
10+
% arguments
11+
% file (1,1)
12+
% hpath (1,1) string
13+
% end
1414

1515
% polymorphic fid/filename
1616
if isa(file, 'H5ML.id')
@@ -25,17 +25,18 @@
2525
end
2626

2727
% are there any groups
28-
grps = split(hpath, "/");
28+
grps = split(hpath, '/');
2929
if length(grps) < 3
3030
return
3131
end
3232

3333
% recursively create groups as needed
3434
plist = 'H5P_DEFAULT';
35-
groot = H5G.open(fid, "/");
35+
groot = H5G.open(fid, '/');
3636

3737
for i = 0:length(grps) - 3
38-
n = join(grps(1:i+2), "/");
38+
n = join(grps(1:i+2), '/');
39+
n = n{1};
3940

4041
if ~H5L.exists(groot, n, plist)
4142
gid = H5G.create(fid, n, plist, plist, plist);

+stdlib/h5save.m

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,23 @@
1818
% Likewise, the type of the dataset may be explicitly specified with the "type" argument.
1919
% h5save(filename, dataset_name, dataset, type="int32")
2020

21-
function h5save(filename, varname, A, opts)
22-
arguments
23-
filename (1,1) string
24-
varname (1,1) string
25-
A {mustBeNonempty}
26-
opts.size (1,:) double {mustBeInteger,mustBeNonnegative} = []
27-
opts.type (1,1) string = ""
28-
opts.compressLevel (1,1) double {mustBeInteger,mustBeNonnegative} = 0
29-
end
21+
function h5save(filename, varname, A, varargin)
22+
% arguments
23+
% filename (1,1) string
24+
% varname (1,1) string
25+
% A {mustBeNonempty}
26+
% opts.size (1,:) double {mustBeInteger,mustBeNonnegative} = []
27+
% opts.type (1,1) string = ""
28+
% opts.compressLevel (1,1) double {mustBeInteger,mustBeNonnegative} = 0
29+
% end
30+
31+
p = inputParser;
32+
addParameter(p, 'size', []);
33+
addParameter(p, 'type', '');
34+
addParameter(p, 'compressLevel', 0, @(x) mustBeInteger(x) && mustBeNonnegative(x));
35+
parse(p, varargin{:});
36+
37+
opts = p.Results;
3038

3139
if isnumeric(A)
3240
mustBeReal(A)
@@ -42,7 +50,7 @@ function h5save(filename, varname, A, opts)
4250
h5save_exist(filename, varname, A, opts.size)
4351
catch e
4452
switch e.identifier
45-
case {'MATLAB:imagesci:hdf5io:resourceNotFound', 'MATLAB:imagesci:h5info:unableToFind', 'MATLAB:imagesci:h5info:fileOpenErr'}
53+
case {'MATLAB:imagesci:hdf5io:resourceNotFound', 'MATLAB:imagesci:h5info:unableToFind', 'MATLAB:imagesci:h5info:fileOpenErr', 'MATLAB:imagesci:h5info:libraryError'}
4654
h5save_new(filename, varname, A, opts.size, opts.compressLevel)
4755
otherwise
4856
rethrow(e)

+stdlib/h5size.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
function fsize = h5size(file, variable)
1010

11-
dsi = h5info(file, variable).Dataspace;
11+
finf = h5info(file, variable);
12+
dsi = finf.Dataspace;
1213

1314
if dsi.Type == "scalar"
1415
fsize = [];

+stdlib/h5variables.m

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,11 @@
99
% * names: variable names
1010

1111
function names = h5variables(file, group)
12-
arguments
13-
file
14-
group (1,1) string = ""
12+
if nargin < 2
13+
group = '/';
1514
end
1615

17-
if stdlib.strempty(group)
18-
finf = h5info(file);
19-
else
20-
finf = h5info(file, group);
21-
end
16+
finf = h5info(file, group);
2217

2318
ds = finf.Datasets;
2419

+stdlib/private/coerce_ds.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
function A = coerce_ds(A, dtype)
22
% used by h5save and ncsave
3-
arguments
4-
A
5-
dtype (1,1) string
6-
end
3+
% arguments
4+
% A
5+
% dtype (1,1) string
6+
% end
77

88
if ischar(A)
99
A = string(A);

+stdlib/private/h5save_exist.m

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
% normally users use h5save() instead of this function
33

44
function h5save_exist(filename, varname, A, sizeA)
5-
arguments
6-
filename
7-
varname
8-
A
9-
sizeA (1,:) double {mustBeInteger,mustBeNonnegative} = []
5+
% arguments
6+
% filename
7+
% varname
8+
% A
9+
% sizeA (1,:) double {mustBeInteger,mustBeNonnegative} = []
10+
% end
11+
if nargin < 4
12+
sizeA = [];
1013
end
1114

1215
diskshape = stdlib.h5size(filename, varname);

+stdlib/private/h5save_new.m

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22
% normally users will use h5save() instead of this function
33

44
function h5save_new(filename, varname, A, sizeA, compressLevel)
5-
arguments
6-
filename
7-
varname
8-
A
9-
sizeA (1,:) double {mustBeInteger,mustBeNonnegative} = []
10-
compressLevel (1,1) double {mustBeInteger,mustBeNonnegative} = 0
5+
% arguments
6+
% filename
7+
% varname
8+
% A
9+
% sizeA (1,:) double {mustBeInteger,mustBeNonnegative} = []
10+
% compressLevel (1,1) double {mustBeInteger,mustBeNonnegative} = 0
11+
% end
12+
if nargin < 4
13+
sizeA = [];
1114
end
15+
if nargin < 5
16+
compressLevel = 0;
17+
end
18+
1219

1320
if isempty(sizeA)
1421
sizeA = defaultSize(A);

+stdlib/private/h5save_scalar.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
function h5save_scalar(filename, hpath, A)
22
%% write HDF5 scalar as a scalar
33
% h5create doesn't support scalars
4-
arguments
5-
filename (1,1) string
6-
hpath (1,1) string
7-
A (1,1)
8-
end
9-
% filename must be a scalar string--char does not work
4+
% arguments
5+
% filename (1,1) string
6+
% hpath (1,1) string
7+
% A (1,1)
8+
% end
9+
1010

1111
dcpl = 'H5P_DEFAULT';
1212

test/TestHDF5.m

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
properties (TestParameter)
1818
str = {"string", 'char'}
19-
type = {"single", "double", ...
20-
"int8", "int16", "int32", "int64", ...
21-
"uint8", "uint16", "uint32", "uint64", "string"}
19+
type = {'single', 'double', ...
20+
'int8', 'int16', 'int32', 'int64', ...
21+
'uint8', 'uint16', 'uint32', 'uint64', 'string'}
2222
end
2323

2424
methods(TestClassSetup)
@@ -34,9 +34,9 @@ function setupData(tc)
3434
methods(TestMethodSetup)
3535

3636
function setup_file(tc)
37-
tc.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture())
37+
tc.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture());
3838

39-
tc.file = fullfile(pwd(), class(tc) + ".h5");
39+
tc.file = [pwd(), class(tc), '.h5'];
4040

4141
% create test data first, so that parallel tests works
4242
stdlib.h5save(tc.file, '/A0', tc.A0)
@@ -57,13 +57,12 @@ function setup_file(tc)
5757
stdlib.h5save(tc.file, '/t/y', 13)
5858
stdlib.h5save(tc.file, '/j/a/b', 6)
5959

60-
tc.assertThat(tc.file, matlab.unittest.constraints.IsFile)
6160
end
6261

6362
end
6463

6564

66-
methods (Test, TestTags = {'R2019b'})
65+
methods (Test, TestTags = {'R2017b'})
6766

6867
function test_auto_chunk_size(tc)
6968

@@ -89,19 +88,19 @@ function test_get_variables(tc)
8988
tc.verifyEqual(sort(v), k)
9089

9190
% 1-level group
92-
v = stdlib.h5variables(tc.file, "/t");
91+
v = stdlib.h5variables(tc.file, '/t');
9392
tc.verifyEqual(sort(v), ["x", "y"])
9493

9594
% traversal
96-
tc.verifyEmpty(stdlib.h5variables(tc.file, "/j") )
95+
tc.verifyEmpty(stdlib.h5variables(tc.file, '/j') )
9796

98-
tc.verifyEqual(stdlib.h5variables(tc.file, "/j/a") , "b")
97+
tc.verifyEqual(stdlib.h5variables(tc.file, '/j/a') , "b")
9998

10099
end
101100

102101

103102
function test_exists(tc)
104-
e = stdlib.h5exists(tc.file, "/A0");
103+
e = stdlib.h5exists(tc.file, '/A0');
105104

106105
tc.verifyThat(e, matlab.unittest.constraints.IsScalar)
107106
tc.verifyTrue(e);
@@ -178,11 +177,11 @@ function test_read(tc)
178177

179178
function test_shape(tc)
180179

181-
stdlib.h5save(tc.file, "/vector1", 34, "size", 1)
180+
stdlib.h5save(tc.file, '/vector1', 34, "size", 1)
182181
s = stdlib.h5size(tc.file, '/vector1');
183182
tc.verifyEqual(s, 1);
184183

185-
stdlib.h5save(tc.file, "/scalar", 34, "size", 0)
184+
stdlib.h5save(tc.file, '/scalar', 34, "size", 0)
186185
s = stdlib.h5size(tc.file, '/scalar');
187186
tc.verifyEmpty(s);
188187

@@ -195,14 +194,14 @@ function test_coerce(tc, type)
195194
tc.assumeFalse(stdlib.matlabOlderThan("R2020b"))
196195
end
197196

198-
stdlib.h5save(tc.file, "/" + type, 0, "type", type)
197+
stdlib.h5save(tc.file, ['/', type], 0, "type", type)
199198

200199
switch type
201200
case "string", vt = 'char';
202201
otherwise, vt = type;
203202
end
204203

205-
tc.verifyClass(h5read(tc.file, "/"+type), vt)
204+
tc.verifyClass(h5read(tc.file, ['/', type]), vt)
206205

207206
end
208207

@@ -216,33 +215,33 @@ function test_rewrite(tc)
216215

217216
function test_int8(tc)
218217

219-
stdlib.h5save(tc.file, "/i1", int8(127))
218+
stdlib.h5save(tc.file, '/i1', int8(127))
220219

221-
a = h5read(tc.file, "/i1");
220+
a = h5read(tc.file, '/i1');
222221
tc.verifyEqual(a, int8(127))
223222

224223
% test rewrite
225-
stdlib.h5save(tc.file, "/i1", int8(-128))
224+
stdlib.h5save(tc.file, '/i1', int8(-128))
226225

227-
a = h5read(tc.file, "/i1");
226+
a = h5read(tc.file, '/i1');
228227
tc.verifyEqual(a, int8(-128))
229228

230229
% test int8 array
231-
stdlib.h5save(tc.file, "/Ai1", int8([1, 2]))
232-
a = h5read(tc.file, "/Ai1");
230+
stdlib.h5save(tc.file, '/Ai1', int8([1, 2]))
231+
a = h5read(tc.file, '/Ai1');
233232
tc.verifyEqual(a, int8([1;2]))
234233
end
235234

236235
function test_real_only(tc)
237236

238-
tc.verifyError(@() stdlib.h5save(tc.file, "/bad_imag", 1j), 'MATLAB:validators:mustBeReal')
237+
tc.verifyError(@() stdlib.h5save(tc.file, '/bad_imag', 1j), 'MATLAB:validators:mustBeReal')
239238
tc.verifyError(@() stdlib.h5variables(tc.file, '/nothere'), 'MATLAB:imagesci:h5info:unableToFind')
240239
end
241240

242241
end
243242

244243

245-
methods (Test, TestTags=["R2020b", "hdf5"])
244+
methods (Test, TestTags={'R2020b'})
246245

247246
function test_string(tc, str)
248247
tc.assumeFalse(stdlib.matlabOlderThan("R2020b"))

0 commit comments

Comments
 (0)