-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtZarrWrite.m
More file actions
153 lines (128 loc) · 6.37 KB
/
Copy pathtZarrWrite.m
File metadata and controls
153 lines (128 loc) · 6.37 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
classdef tZarrWrite < SharedZarrTestSetup
% Tests for zarrwrite function to write data to Zarr files in MATLAB.
% Copyright 2025 The MathWorks, Inc.
properties(TestParameter)
DataType = {'double','single','int8','uint8','int16','uint16', ...
'int32','uint32','int64','uint64','logical'}
CompId = {'zlib','gzip','bz2','zstd'}
ArrSizeWrite = {10, [1 10],[20 25],[10 12 5]}
end
methods(Test)
function createArrayLocalDefaultSyntax(testcase,ArrSizeWrite)
% Verify the data when creating and writing to arrays of different
% dimensions using zarrcreate and zarrwrite locally. The default
% datatype is double.;
zarrcreate(testcase.ArrPathWrite,ArrSizeWrite);
if isscalar(ArrSizeWrite)
expData = (1:ArrSizeWrite)*pi;
else
expData = rand(ArrSizeWrite);
end
zarrwrite(testcase.ArrPathWrite,expData);
actData = zarrread(testcase.ArrPathWrite);
testcase.verifyEqual(actData,expData,'Failed to verify array data');
end
function createArrayLocalUserDefinedSyntax(testcase,DataType,CompId)
% Verify the data when creating and writing to arrays with
% user-defined properties using zarrcreate and zarrwrite locally.
comp.level = 5;
fillValue = cast(-9, DataType);
expData = cast(ones(testcase.ArrSize),DataType);
comp.id = CompId;
zarrcreate(testcase.ArrPathWrite,testcase.ArrSize,'ChunkSize',testcase.ChunkSize, ...
'Compression',comp,'FillValue',fillValue,'Datatype',DataType);
zarrwrite(testcase.ArrPathWrite,expData);
actData = zarrread(testcase.ArrPathWrite);
testcase.verifyEqual(actData,expData,['Failed to verify data for ' DataType ' datatype' ...
' with ' CompId ' compression.']);
end
function createArrayWithDefaultBloscConfig(testcase)
% Verify data when creating and writing to a Zarr array using
% a default blosc compression configuration.
comp.id = 'blosc';
expData = randn(testcase.ArrSize);
zarrcreate(testcase.ArrPathWrite,testcase.ArrSize,'ChunkSize', ...
testcase.ChunkSize,'Compression',comp);
zarrwrite(testcase.ArrPathWrite,expData);
actData = zarrread(testcase.ArrPathWrite);
testcase.verifyEqual(actData,expData,'Failed to verify data.');
end
function createArrayWithCustomBloscConfig(testcase)
% Verify data when creating and writing to a Zarr array using
% custom blosc compression configuration.
comp.id = 'blosc';
comp.clevel = 5;
cname = {'blosclz','lz4','lz4hc','zlib','zstd','snappy'};
comp.shuffle = -1;
expData = randn(testcase.ArrSize);
for i = 1:length(cname)
comp.cname = cname{i};
zarrcreate(testcase.ArrPathWrite,testcase.ArrSize,'ChunkSize', ...
testcase.ChunkSize,'Compression',comp);
zarrwrite(testcase.ArrPathWrite,expData);
actData = zarrread(testcase.ArrPathWrite);
testcase.verifyEqual(actData,expData,['Failed to verify data for ' cname(i)]);
end
end
function createArrayWithDefaultCompConfig(testcase)
% Verify data when creating and writing to a Zarr array using
% a default compression (other than Blosc) configuration.
compType = {'zlib','gzip','bz2','zstd'};
expData = randn(testcase.ArrSize);
for i = 1:length(compType)
comp.id = compType{i};
zarrcreate(testcase.ArrPathWrite,testcase.ArrSize,'ChunkSize', ...
testcase.ChunkSize,'Compression',comp);
zarrwrite(testcase.ArrPathWrite,expData);
actData = zarrread(testcase.ArrPathWrite);
testcase.verifyEqual(actData,expData,'Failed to verify data.');
end
end
function tooFewInputs(testcase)
% Verify error when too few inputs to zarrwrite are passed.
errID = 'MATLAB:minrhs';
testcase.verifyError(@()zarrwrite(testcase.ArrPathWrite),errID);
end
function invalidFilePath(testcase)
% Verify error when an invalid file path is used.
errID = 'MATLAB:validators:mustBeNonzeroLengthText';
data = ones(10,10);
testcase.verifyError(@()zarrwrite('',data),errID);
end
function dataDatatypeMismatch(testcase)
% Verify error for mismatch between datatype value and datatype
% of data to be written with zarrwrite.
errID = 'MATLAB:Python:PyException';
zarrcreate(testcase.ArrPathWrite,testcase.ArrSize,"Datatype",'int8');
data = ones(testcase.ArrSize);
testcase.verifyError(@()zarrwrite(testcase.ArrPathWrite,data),errID);
end
function dataDimensionMismatch(testcase)
% Verify error when there is a dimension mismatch at the time of
% writing to the array.
errID = 'MATLAB:Zarr:sizeMismatch';
zarrcreate(testcase.ArrPathWrite,testcase.ArrSize);
data = ones(30,30);
testcase.verifyError(@()zarrwrite(testcase.ArrPathWrite,data),errID);
end
function overwriteArray(testcase)
% Verify data after the array data is overwritten with new
% data.
zarrcreate(testcase.ArrPathWrite,testcase.ArrSize, ...
'ChunkSize',testcase.ChunkSize);
data = ones(testcase.ArrSize);
zarrwrite(testcase.ArrPathWrite,data);
% Create new data
expData = rand(testcase.ArrSize);
zarrwrite(testcase.ArrPathWrite,expData);
actData = zarrread(testcase.ArrPathWrite);
testcase.verifyEqual(actData,expData,'Failed to verify array data')
end
function writeToNonExistentArray(testcase)
% Try writing to a Zarr array which has not been created yet
errID = 'MATLAB:zarrinfo:invalidZarrObject';
data = rand(10);
testcase.verifyError(@()zarrwrite('nonExistentArray.zarr',data),errID);
end
end
end