Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

14eeb833-a86c-49a0-8bb5-5382ea74a803 9.2 KB
Raw

You have to be logged in to leave a comment. Sign In
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
  1. import re
  2. from unittest import mock
  3. import numpy as np
  4. import pytest
  5. from zarr.core import Array
  6. from zarr.util import (
  7. ConstantMap,
  8. all_equal,
  9. flatten,
  10. guess_chunks,
  11. human_readable_size,
  12. info_html_report,
  13. info_text_report,
  14. is_total_slice,
  15. json_dumps,
  16. normalize_chunks,
  17. normalize_dimension_separator,
  18. normalize_fill_value,
  19. normalize_order,
  20. normalize_resize_args,
  21. normalize_shape,
  22. retry_call,
  23. tree_array_icon,
  24. tree_group_icon,
  25. tree_get_icon,
  26. tree_widget,
  27. )
  28. def test_normalize_dimension_separator():
  29. assert None is normalize_dimension_separator(None)
  30. assert "/" == normalize_dimension_separator("/")
  31. assert "." == normalize_dimension_separator(".")
  32. with pytest.raises(ValueError):
  33. normalize_dimension_separator("X")
  34. def test_normalize_shape():
  35. assert (100,) == normalize_shape((100,))
  36. assert (100,) == normalize_shape([100])
  37. assert (100,) == normalize_shape(100)
  38. with pytest.raises(TypeError):
  39. normalize_shape(None)
  40. with pytest.raises(ValueError):
  41. normalize_shape("foo")
  42. def test_normalize_chunks():
  43. assert (10,) == normalize_chunks((10,), (100,), 1)
  44. assert (10,) == normalize_chunks([10], (100,), 1)
  45. assert (10,) == normalize_chunks(10, (100,), 1)
  46. assert (10, 10) == normalize_chunks((10, 10), (100, 10), 1)
  47. assert (10, 10) == normalize_chunks(10, (100, 10), 1)
  48. assert (10, 10) == normalize_chunks((10, None), (100, 10), 1)
  49. assert (30, 30, 30) == normalize_chunks(30, (100, 20, 10), 1)
  50. assert (30, 20, 10) == normalize_chunks((30,), (100, 20, 10), 1)
  51. assert (30, 20, 10) == normalize_chunks((30, None), (100, 20, 10), 1)
  52. assert (30, 20, 10) == normalize_chunks((30, None, None), (100, 20, 10), 1)
  53. assert (30, 20, 10) == normalize_chunks((30, 20, None), (100, 20, 10), 1)
  54. assert (30, 20, 10) == normalize_chunks((30, 20, 10), (100, 20, 10), 1)
  55. with pytest.raises(ValueError):
  56. normalize_chunks("foo", (100,), 1)
  57. with pytest.raises(ValueError):
  58. normalize_chunks((100, 10), (100,), 1)
  59. # test auto-chunking
  60. assert (100,) == normalize_chunks(None, (100,), 1)
  61. assert (100,) == normalize_chunks(-1, (100,), 1)
  62. assert (30, 20, 10) == normalize_chunks((30, -1, None), (100, 20, 10), 1)
  63. def test_is_total_slice():
  64. # 1D
  65. assert is_total_slice(Ellipsis, (100,))
  66. assert is_total_slice(slice(None), (100,))
  67. assert is_total_slice(slice(0, 100), (100,))
  68. assert not is_total_slice(slice(0, 50), (100,))
  69. assert not is_total_slice(slice(0, 100, 2), (100,))
  70. # 2D
  71. assert is_total_slice(Ellipsis, (100, 100))
  72. assert is_total_slice(slice(None), (100, 100))
  73. assert is_total_slice((slice(None), slice(None)), (100, 100))
  74. assert is_total_slice((slice(0, 100), slice(0, 100)), (100, 100))
  75. assert not is_total_slice((slice(0, 100), slice(0, 50)), (100, 100))
  76. assert not is_total_slice((slice(0, 50), slice(0, 100)), (100, 100))
  77. assert not is_total_slice((slice(0, 50), slice(0, 50)), (100, 100))
  78. assert not is_total_slice((slice(0, 100, 2), slice(0, 100)), (100, 100))
  79. with pytest.raises(TypeError):
  80. is_total_slice("foo", (100,))
  81. def test_normalize_resize_args():
  82. # 1D
  83. assert (200,) == normalize_resize_args((100,), 200)
  84. assert (200,) == normalize_resize_args((100,), (200,))
  85. # 2D
  86. assert (200, 100) == normalize_resize_args((100, 100), (200, 100))
  87. assert (200, 100) == normalize_resize_args((100, 100), (200, None))
  88. assert (200, 100) == normalize_resize_args((100, 100), 200, 100)
  89. assert (200, 100) == normalize_resize_args((100, 100), 200, None)
  90. with pytest.raises(ValueError):
  91. normalize_resize_args((100,), (200, 100))
  92. def test_human_readable_size():
  93. assert "100" == human_readable_size(100)
  94. assert "1.0K" == human_readable_size(2**10)
  95. assert "1.0M" == human_readable_size(2**20)
  96. assert "1.0G" == human_readable_size(2**30)
  97. assert "1.0T" == human_readable_size(2**40)
  98. assert "1.0P" == human_readable_size(2**50)
  99. def test_normalize_order():
  100. assert "F" == normalize_order("F")
  101. assert "C" == normalize_order("C")
  102. assert "F" == normalize_order("f")
  103. assert "C" == normalize_order("c")
  104. with pytest.raises(ValueError):
  105. normalize_order("foo")
  106. def test_normalize_fill_value():
  107. assert b"" == normalize_fill_value(0, dtype=np.dtype("S1"))
  108. structured_dtype = np.dtype([("foo", "S3"), ("bar", "i4"), ("baz", "f8")])
  109. expect = np.array((b"", 0, 0.0), dtype=structured_dtype)[()]
  110. assert expect == normalize_fill_value(0, dtype=structured_dtype)
  111. assert expect == normalize_fill_value(expect, dtype=structured_dtype)
  112. assert "" == normalize_fill_value(0, dtype=np.dtype("U1"))
  113. def test_guess_chunks():
  114. shapes = (
  115. (100,),
  116. (100, 100),
  117. (1000000,),
  118. (1000000000,),
  119. (10000000000000000000000,),
  120. (10000, 10000),
  121. (10000000, 1000),
  122. (1000, 10000000),
  123. (10000000, 1000, 2),
  124. (1000, 10000000, 2),
  125. (10000, 10000, 10000),
  126. (100000, 100000, 100000),
  127. (1000000000, 1000000000, 1000000000),
  128. (0,),
  129. (0, 0),
  130. (10, 0),
  131. (0, 10),
  132. (1, 2, 0, 4, 5),
  133. )
  134. for shape in shapes:
  135. chunks = guess_chunks(shape, 1)
  136. assert isinstance(chunks, tuple)
  137. assert len(chunks) == len(shape)
  138. # doesn't make any sense to allow chunks to have zero length dimension
  139. assert all(0 < c <= max(s, 1) for c, s in zip(chunks, shape))
  140. # ludicrous itemsize
  141. chunks = guess_chunks((1000000,), 40000000000)
  142. assert isinstance(chunks, tuple)
  143. assert (1,) == chunks
  144. def test_info_text_report():
  145. items = [("foo", "bar"), ("baz", "qux")]
  146. expect = "foo : bar\nbaz : qux\n"
  147. assert expect == info_text_report(items)
  148. def test_info_html_report():
  149. items = [("foo", "bar"), ("baz", "qux")]
  150. actual = info_html_report(items)
  151. assert "<table" == actual[:6]
  152. assert "</table>" == actual[-8:]
  153. def test_tree_get_icon():
  154. assert tree_get_icon("Array") == tree_array_icon
  155. assert tree_get_icon("Group") == tree_group_icon
  156. with pytest.raises(ValueError):
  157. tree_get_icon("Baz")
  158. @mock.patch.dict("sys.modules", {"ipytree": None})
  159. def test_tree_widget_missing_ipytree():
  160. pattern = (
  161. "Run `pip install zarr[jupyter]` or `conda install ipytree`"
  162. "to get the required ipytree dependency for displaying the tree "
  163. "widget. If using jupyterlab<3, you also need to run "
  164. "`jupyter labextension install ipytree`"
  165. )
  166. with pytest.raises(ImportError, match=re.escape(pattern)):
  167. tree_widget(None, None, None)
  168. def test_retry_call():
  169. class Fixture:
  170. def __init__(self, pass_on=1):
  171. self.c = 0
  172. self.pass_on = pass_on
  173. def __call__(self):
  174. self.c += 1
  175. if self.c != self.pass_on:
  176. raise PermissionError()
  177. for x in range(1, 11):
  178. # Any number of failures less than 10 will be accepted.
  179. fixture = Fixture(pass_on=x)
  180. retry_call(fixture, exceptions=(PermissionError,), wait=0)
  181. assert fixture.c == x
  182. def fail(x):
  183. # Failures after 10 will cause an error to be raised.
  184. retry_call(Fixture(pass_on=x), exceptions=(Exception,), wait=0)
  185. for x in range(11, 15):
  186. pytest.raises(PermissionError, fail, x)
  187. def test_flatten():
  188. assert list(
  189. flatten(
  190. [
  191. "0",
  192. [
  193. "1",
  194. [
  195. "2",
  196. [
  197. "3",
  198. [
  199. 4,
  200. ],
  201. ],
  202. ],
  203. ],
  204. ]
  205. )
  206. ) == ["0", "1", "2", "3", 4]
  207. assert list(flatten("foo")) == ["f", "o", "o"]
  208. assert list(flatten(["foo"])) == ["foo"]
  209. def test_all_equal():
  210. assert all_equal(0, np.zeros((10, 10, 10)))
  211. assert not all_equal(1, np.zeros((10, 10, 10)))
  212. assert all_equal(1, np.ones((10, 10, 10)))
  213. assert not all_equal(1, 1 + np.ones((10, 10, 10)))
  214. assert all_equal(np.nan, np.array([np.nan, np.nan]))
  215. assert not all_equal(np.nan, np.array([np.nan, 1.0]))
  216. assert all_equal({"a": -1}, np.array([{"a": -1}, {"a": -1}], dtype="object"))
  217. assert not all_equal({"a": -1}, np.array([{"a": -1}, {"a": 2}], dtype="object"))
  218. assert all_equal(np.timedelta64(999, "D"), np.array([999, 999], dtype="timedelta64[D]"))
  219. assert not all_equal(np.timedelta64(999, "D"), np.array([999, 998], dtype="timedelta64[D]"))
  220. # all_equal(None, *) always returns False
  221. assert not all_equal(None, np.array([None, None]))
  222. assert not all_equal(None, np.array([None, 10]))
  223. def test_json_dumps_numpy_dtype():
  224. assert json_dumps(np.int64(0)) == json_dumps(0)
  225. assert json_dumps(np.float32(0)) == json_dumps(float(0))
  226. # Check that we raise the error of the superclass for unsupported object
  227. with pytest.raises(TypeError):
  228. json_dumps(Array)
  229. def test_constant_map():
  230. val = object()
  231. m = ConstantMap(keys=[1, 2], constant=val)
  232. assert len(m) == 2
  233. assert m[1] is val
  234. assert m[2] is val
  235. assert 1 in m
  236. assert 0 not in m
  237. with pytest.raises(KeyError):
  238. m[0]
  239. assert repr(m) == repr({1: val, 2: val})
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...