-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtZarrRead.m
More file actions
202 lines (166 loc) · 8.18 KB
/
Copy pathtZarrRead.m
File metadata and controls
202 lines (166 loc) · 8.18 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
classdef tZarrRead < SharedZarrTestSetup
% Tests for zarrread function to read data from Zarr files in MATLAB.
% Copyright 2025 The MathWorks, Inc.
properties(Constant)
% Paths for read functions. SharedZarrTestSetup copies the contents
% of dataFiles/ into the working folder, so fixtures are at the
% working folder root (grp_v2/..., grp_v3/...).
GrpPathRead = "grp_v2"
ArrPathRead = "grp_v2/arr_v2"
ArrPathReadSmall = "grp_v2/smallArr"
ArrPathReadVector = "grp_v2/vectorData"
ArrPathReadScalar = "grp_v2/scalarData"
ArrPathReadV3 = "grp_v3/arr_v3"
% Loaded at class-load time, while pwd is still the test folder.
ExpData = load(fullfile(pwd,"dataFiles","expZarrArrData.mat"))
end
methods(Test)
function verifyArrayData(testcase)
% Verify array data using zarrread function.
actArrData = zarrread(testcase.ArrPathRead);
expArrData = testcase.ExpData.arr_v2;
testcase.verifyEqual(actArrData,expArrData,'Failed to verify array data.');
end
function verifyPartialArrayData(testcase)
% Verify array data using zarrread function with Start/Stride/Count.
% The full data in the small array is
%
% 1 4 7 10
% 2 5 8 11
% 3 6 9 12
zpath = testcase.ArrPathReadSmall;
% Start
actData = zarrread(zpath, Start=[2, 3]);
expData = [8, 11; 9, 12];
testcase.verifyEqual(actData,expData,...
'Failed to verify reading with Start.');
% Count
actData = zarrread(zpath, Count=[2, 1]);
expData = [1;2];
testcase.verifyEqual(actData,expData,...
'Failed to verify reading with Count.');
% Stride
actData = zarrread(zpath, Stride=[3, 2]);
expData = [1, 7];
testcase.verifyEqual(actData,expData,...
'Failed to verify reading with Stride.');
% Start, Stride, and Count
actData = zarrread(zpath,...
Start=[2, 1], Stride=[1, 2], Count=[1, 2]);
expData = [2, 8];
testcase.verifyEqual(actData,expData,...
'Failed to verify reading with Start, Stride, and Count.');
end
function verifyPartialVectorData(testcase)
% Verify that specifying a scalar value for Start/Stride/Count
% for vector datasets works as expected
zpath = testcase.ArrPathReadVector; % data is 1:10
expData = [2,5];
actData = zarrread(zpath, Start=2, Stride=3, Count=2);
testcase.verifyEqual(actData,expData,...
'Failed to verify using scalar Start, Stride, and Count.');
end
function verifyReadScalarData(testcase)
zpath = testcase.ArrPathReadScalar; % data is 0
expectedData = 0;
actualData = testcase.verifyWarningFree(@()zarrread(zpath));
testcase.verifyEqual(actualData, expectedData)
end
function verifyArrayDataRelativePath(testcase)
% Verify array data if the input is using a relative path to the
% array. Read from a subfolder using a "../" prefixed path.
import matlab.unittest.fixtures.CurrentFolderFixture
testcase.applyFixture(CurrentFolderFixture("grp_v2"));
inpPath = fullfile('..', testcase.ArrPathRead);
actArrData = zarrread(inpPath);
expArrData = testcase.ExpData.arr_v2;
testcase.verifyEqual(actArrData,expArrData,['Failed to verify array ' ...
'data with relative path.']);
end
function verifyGroupInpError(testcase)
% Verify error if a user tries to pass the group as input to
% zarrread function.
errID = 'MATLAB:Zarr:invalidZarrObject';
testcase.verifyError(@()zarrread(testcase.GrpPathRead),errID);
end
function verifyArrReadV3(testcase)
% Verify error when a user tries to read a zarr format v3
% array.
errID = 'MATLAB:Zarr:invalidZarrObject';
testcase.verifyError(@()zarrread(testcase.ArrPathReadV3),errID);
end
function nonExistentArray(testcase)
% Verify zarrread error when a user tries to read a non-existent
% file.
errID = 'MATLAB:Zarr:invalidZarrObject';
testcase.verifyError(@()zarrread('nonexistent/'),errID);
end
function tooBigArray(testcase)
% Verify zarrread error when a user tries to read data that is
% too large
% Lower the array size limit so the ~37 GiB array below always
% exceeds it, regardless of how much RAM this machine has.
s = settings;
limitEnabled = s.matlab.desktop.workspace.ArraySizeLimitEnabled;
limitPercent = s.matlab.desktop.workspace.ArraySizeLimit;
testcase.addTeardown(@() clearTemporaryValue(limitEnabled));
testcase.addTeardown(@() clearTemporaryValue(limitPercent));
limitEnabled.TemporaryValue = true;
limitPercent.TemporaryValue = 1;
bigDataPath = "bigData/myzarr";
zarrcreate(bigDataPath, [100000,100000], Datatype='single');
errID = 'MATLAB:Zarr:OutOfMemory';
testcase.verifyError(@()zarrread(bigDataPath),errID);
end
function invalidFilePath(testcase)
% Verify zarrread error when an invalid file path is used.
% Using a cell input with a valid array path
errID = 'MATLAB:validators:mustBeTextScalar';
testcase.verifyError(@()zarrread({testcase.ArrPathRead}),errID);
% Empty cell or double
testcase.verifyError(@()zarrread({}),errID);
testcase.verifyError(@()zarrread([]),errID);
% Non-scalar input
testcase.verifyError(@()zarrread([testcase.ArrPathRead,testcase.ArrPathRead]), ...
errID);
% Empty char
errID = 'MATLAB:validators:mustBeNonzeroLengthText';
testcase.verifyError(@()zarrread(''),errID);
% Non-existent bucket
inpPath = 's3://invalid/bucket/path';
errID = 'MATLAB:Zarr:invalidZarrObject';
testcase.verifyError(@()zarrread(inpPath),errID);
end
function invalidPartialReadParams(testcase)
% Verify zarrread errors when invalid partial read
% Start/Stride/Count are used.
zpath = testcase.ArrPathReadSmall; % a 2D array, 3x4
% Wrong number of dimensions in comparison to the array
errID = 'MATLAB:Zarr:badPartialReadDimensions';
wrongDims = [1,1,1];
testcase.verifyError(@()zarrread(zpath,Start=wrongDims),errID);
testcase.verifyError(@()zarrread(zpath,Stride=wrongDims),errID);
testcase.verifyError(@()zarrread(zpath,Count=wrongDims),errID);
% Invalid type
errID = 'MATLAB:validators:mustBeNumeric';
testcase.verifyError(@()zarrread(zpath,"Start",""),errID);
testcase.verifyError(@()zarrread(zpath,"Stride",""),errID);
testcase.verifyError(@()zarrread(zpath,"Count",""),errID);
% Negative values
inpVal = [-1 1];
errID = 'MATLAB:validators:mustBePositive';
testcase.verifyError(@()zarrread(zpath,"Start",inpVal),errID);
testcase.verifyError(@()zarrread(zpath,"Stride",inpVal),errID);
testcase.verifyError(@()zarrread(zpath,"Count",inpVal),errID);
% Parameters out of bounds
inpVal = [100 200];
errID = 'MATLAB:Zarr:PartialReadOutOfBounds';
testcase.verifyError(@()zarrread(zpath,"Start",inpVal),errID);
testcase.verifyError(@()zarrread(zpath,"Stride",inpVal),errID);
testcase.verifyError(@()zarrread(zpath,"Count",inpVal),errID);
% Combination of parameters out of bounds
testcase.verifyError(...
@()zarrread(zpath,Start=[3 4],Count=[2 2]),errID)
end
end
end