-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtZarrCreate.m
More file actions
368 lines (308 loc) · 15.4 KB
/
Copy pathtZarrCreate.m
File metadata and controls
368 lines (308 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
classdef tZarrCreate < SharedZarrTestSetup
% Tests for zarrcreate function to create Zarr files in MATLAB.
% Copyright 2025 The MathWorks, Inc.
methods(Test)
function createDefaultArray(testcase)
% Verify that zarrcreate correctly creates a Zarr array with
% all default properties
zarrcreate(testcase.ArrPathWrite,testcase.ArrSize);
expInfo.chunks = testcase.ArrSize';
expInfo.compressor = [];
expInfo.dimension_separator = '.';
expInfo.dtype = '<f8';
expInfo.fill_value = [];
expInfo.filters = [];
expInfo.order = 'C';
expInfo.shape = testcase.ArrSize';
expInfo.zarr_format = 2;
expInfo.node_type = 'array';
actInfo = zarrinfo(testcase.ArrPathWrite);
testcase.verifyEqual(actInfo, expInfo,...
'Failed to verify creating Zarr array with default properties');
end
function createIntermediateZgroups(testcase)
% Verify that zarrcreate creates zarr groups when given a
% nested path
arrayPath = fullfile(testcase.ArrPathWrite, "A", "B");
zarrcreate(arrayPath, testcase.ArrSize);
[groupPath, ~, ~] = fileparts(arrayPath);
testcase.verifyTrue(isfile(fullfile(groupPath, ".zgroup")),...
".zgroup file was not created")
grpInfo = zarrinfo(groupPath);
expFormat = '2';
expType = 'group';
testcase.verifyEqual(grpInfo.zarr_format, expFormat,...
"Unexpected Zarr group format");
testcase.verifyEqual(grpInfo.node_type, expType,...
"Unexpected Zarr group node type");
end
function createArrayRelativePath(testcase)
% Verify that the array is successfully created if a relative
% path is used. Work from a fresh temporary folder (which the
% fixture enters and cleans up) so the "../" path resolves.
import matlab.unittest.fixtures.WorkingFolderFixture
testcase.applyFixture(WorkingFolderFixture);
inpPath = fullfile('..','myGrp','myArr');
zarrcreate(inpPath,[10 10]);
arrInfo = zarrinfo(inpPath);
testcase.verifyEqual(arrInfo.zarr_format,2,'Failed to verify Zarr array format');
testcase.verifyEqual(arrInfo.node_type,'array','Unexpected Zarr array node type');
end
function invalidFilePath(testcase)
% Verify error when an invalid file path is used as an input to
% zarrcreate function.
% Empty
errID = 'MATLAB:validators:mustBeNonempty';
testcase.verifyError(@()zarrcreate('',testcase.ArrSize),errID);
% Non-scalar
errID = 'MATLAB:validators:mustBeTextScalar';
testcase.verifyError(@()zarrcreate([testcase.ArrPathWrite,testcase.ArrPathWrite], ...
testcase.ArrSize),errID);
% Non-text input
testcase.verifyError(@()zarrcreate([],testcase.ArrSize),errID);
% Invalid bucket path format
errID = 'MATLAB:Zarr:invalidS3URL';
inpPath = 'https://invalid/arr/path';
testcase.verifyError(@()zarrcreate(inpPath,[10 10]),errID);
inpPath = 'http://invalid/arr/path';
testcase.verifyError(@()zarrcreate(inpPath,[10 10]),errID);
% Non-existent bucket path
errID = 'MATLAB:Zarr:invalidPath';
inpPath = 's3://invalid/bucket/path';
testcase.verifyError(@()zarrcreate(inpPath,[10 10]),errID);
end
function invalidFilePathWindows(testcase)
% Verify error when a non-existent Windows path starting with
% "\\" is used
testcase.assumeTrue(ispc, "Windows-specific test")
errID = 'MATLAB:Zarr:invalidPath';
inpPath = '\\invalid\bucket\path';
testcase.verifyError(@()zarrcreate(inpPath,[10 10]),errID);
end
function pathContainingInvalidChars(testcase)
% Verify error when the array or group name contains
% unsupported characters.
testcase.assumeTrue(ispc,'Filtered on other platforms');
testcase.verifyError(@()zarrcreate('grp*/arr',testcase.ArrSize),testcase.PyException);
testcase.verifyError(@()zarrcreate('grp/arr*',testcase.ArrSize),testcase.PyException);
end
function chunkSizeGreaterThanArraySize(testcase)
% Verify error when the chunk size is greater than the array
% size.
errID = 'MATLAB:zarrcreate:chunkSizeGreater';
chunkSize = [30 35];
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'ChunkSize',chunkSize),errID);
end
function chunkSizeMismatch(testcase)
% Verify error when there is a mismatch between Array size and
% Chunk size.
arrSize = [10 12 5];
chunkSize = [4 5];
errID = 'MATLAB:zarrcreate:chunkDimsMismatch';
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,arrSize, ...
'ChunkSize',chunkSize),errID);
end
function invalidClevelBlosc(testcase)
% Verify error when an invalid clevel value is used with blosc
% compression. Valid values are [0 9], where 0 is for no compression.
comp.id = 'blosc';
level = {-1,10,NaN};
comp.cname = 'blosclz';
comp.shuffle = -1;
for i = 1:length(level)
comp.clevel = level{i};
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
end
end
function invalidBlockSizeBlosc(testcase)
% Verify error when an invalid blocksize value is used with blosc
% compression. Valid values for blocksize are [0 inf].
comp.id = 'blosc';
comp.level = 5;
comp.cname = 'blosclz';
comp.shuffle = -1;
blocksize = {-1,[2 2],NaN};
for i = 1:length(blocksize)
comp.blocksize = blocksize{i};
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
end
end
function invalidCnameBlosc(testcase)
% Verify error when an invalid cname value is used with blosc
% compression.
comp.id = 'blosc';
comp.level = 5;
cname = {'random','',0};
comp.shuffle = -1;
comp.blocksize = 5;
for i = 1:length(cname)
comp.cname = cname{i};
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
end
end
function invalidShuffleBlosc(testcase)
% Verify error when an invalid shuffle value is used with blosc
% compression. The valid values are -1, 0, 1, and 2.
comp.id = 'blosc';
comp.level = 5;
comp.cname = 'blosclz';
shuffle = {-2,3,inf,NaN};
comp.blocksize = 5;
for i = 1:length(shuffle)
comp.shuffle = shuffle{i};
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
end
end
function invalidChunkSize(testcase)
% Verify error when an invalid type for the chunk size is used.
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'ChunkSize',5),'MATLAB:zarrcreate:chunkDimsMismatch');
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'ChunkSize',[]),'MATLAB:zarrcreate:chunkDimsMismatch');
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'ChunkSize',[0 0]),'MATLAB:validators:mustBePositive');
end
function invalidFillValue(testcase)
% Verify error when an invalid type for the fill value is used.
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
"FillValue",[-9 -9]),testcase.PyException);
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
"FillValue","none"),'MATLAB:validators:mustBeNumericOrLogical');
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize,...
Datatype="int8", FillValue=1.4), 'MATLAB:zarrcreate:invalidFillValueType')
end
function specialFillValue(testcase)
% Verify creating Zarr arrays using special fill values like
% NaN and Inf
expData = [NaN,NaN];
zarrcreate(testcase.ArrPathWrite, [1,2], FillValue=NaN)
actData = zarrread(testcase.ArrPathWrite);
testcase.verifyEqual(expData, actData)
expData = [Inf,Inf];
zarrcreate(testcase.ArrPathWrite, [1,2], FillValue=Inf)
actData = zarrread(testcase.ArrPathWrite);
testcase.verifyEqual(expData, actData)
end
function invalidSizeInput(testcase)
% Verify error when an invalid size input is used.
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,[]), ...
'MATLAB:validators:mustBeNonempty');
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,Inf), ...
'MATLAB:validators:mustBeFinite');
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,-2), ...
'MATLAB:validators:mustBePositive');
end
function invalidDatatype(testcase)
% Verify the error when an unsupported datatype is used.
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,...
testcase.ArrSize,Datatype="bla"),...
'MATLAB:validators:mustBeMember');
end
function invalidCompressionInputType(testcase)
% Verify error when an invalid compression value is used.
%testcase.assumeTrue(false,'Filtered until the issue is fixed.');
comp.id = 'random';
errID = 'MATLAB:Zarr:invalidCompressionID';
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),errID);
comp = 'zlib';
errID = 'MATLAB:zarrcreate:invalidCompression';
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),errID);
end
function invalidCompressionMember(testcase)
% Verify error when additional compression members (cname, blocksize,
% and shuffle) are used. These members are not supported for
% compression other than blosc.
compType = {'gzip','zlib','bz2','zstd'};
comp.level = 5;
comp.cname = 'blosclz';
comp.shuffle = -1;
for i = 1:length(compType)
comp.id = compType{i};
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
end
end
function zlibInvalidCompressionLevel(testcase)
% Verify error when an invalid compression level is used.
% For zlib, valid values are [0 9]
comp.level = 5;
errID = 'MATLAB:Zarr:missingCompressionID';
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),errID);
comp.id = 'zlib';
comp.level = -1;
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
comp.level = 10;
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
comp.level = [];
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
end
function gzipInvalidCompressionLevel(testcase)
% Verify error when an invalid compression level is used.
% For gzip, valid values are [0 9]
comp.id = 'gzip';
comp.level = -1;
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
comp.level = 10;
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
comp.level = [];
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
end
function bz2InvalidCompressionLevel(testcase)
% Verify error when an invalid compression level is used.
% For zlib, valid values are [1 9]
comp.id = 'bz2';
comp.level = 0;
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
comp.level = 10;
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
comp.level = [];
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
end
function zstdInvalidCompressionLevel(testcase)
% Verify error when an invalid compression level is used.
% For zlib, valid values are [1 9]
comp.id = 'zstd';
comp.level = -131073;
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
comp.level = 23;
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
comp.level = [];
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Compression',comp),testcase.PyException);
end
function tooFewInputs(testcase)
% Verify error when too few inputs are passed to the zarrcreate
% function.
errID = 'MATLAB:minrhs';
testcase.verifyError(@()zarrcreate(),errID);
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite),errID);
end
function invalidParameter(testcase)
% Verify error when an invalid NV pair for zarrcreate is used.
errID = 'MATLAB:TooManyInputs';
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Random'),errID);
testcase.verifyError(@()zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'Random',-1),errID);
end
end
end