PythonSTB commited on
Commit
de1b3b9
·
verified ·
1 Parent(s): 603f8a1

Upload pyarrow/Test_PyArrow.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. pyarrow/Test_PyArrow.py +400 -0
pyarrow/Test_PyArrow.py ADDED
@@ -0,0 +1,400 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Test_PyArrow.py - on-device validation for the pyarrow 25.0.1 wheel.
3
+
4
+ Exercises arrays, tables, IPC, CSV, JSON, Feather, compute, and types.
5
+ Exit code 0 = all tests passed.
6
+
7
+ Generated by RIMI
8
+ """
9
+ import os
10
+ import sys
11
+ import tempfile
12
+
13
+ RESULTS = []
14
+
15
+
16
+ def test(name, fn):
17
+ try:
18
+ fn()
19
+ RESULTS.append(("PASS", name))
20
+ except NotImplementedError:
21
+ RESULTS.append(("SKIP", name))
22
+ except Exception as e: # noqa: BLE001
23
+ RESULTS.append(("FAIL", name, str(e)))
24
+
25
+
26
+ def section(title):
27
+ print("\n===== %s =====" % title)
28
+
29
+
30
+ def check(cond, msg="assertion failed"):
31
+ if not cond:
32
+ raise AssertionError(msg)
33
+
34
+
35
+ WORKDIR = None
36
+
37
+
38
+ def workdir():
39
+ global WORKDIR
40
+ if WORKDIR is None:
41
+ import os as _os
42
+ candidates = [_os.environ.get("TMPDIR") or "", tempfile.gettempdir(),
43
+ "/storage/emulated/0/Download", _os.getcwd()]
44
+ for base in candidates:
45
+ if not base:
46
+ continue
47
+ try:
48
+ d = os.path.join(base, "test_pyarrow_tmp")
49
+ os.makedirs(d, exist_ok=True)
50
+ with open(os.path.join(d, "_probe"), "w") as fh:
51
+ fh.write("ok")
52
+ WORKDIR = d
53
+ break
54
+ except OSError:
55
+ continue
56
+ if WORKDIR is None:
57
+ WORKDIR = "."
58
+ return WORKDIR
59
+
60
+
61
+ # ---------------------------------------------------------------------------
62
+ # 1. import / version
63
+ # ---------------------------------------------------------------------------
64
+ def test_import_pyarrow():
65
+ import pyarrow as pa
66
+ print(" pyarrow", pa.__version__)
67
+ check(pa.__version__.startswith("25."), "unexpected version: %s" % pa.__version__)
68
+
69
+
70
+ def test_import_numpy_dep():
71
+ import numpy as np
72
+ print(" numpy", np.__version__)
73
+ check(hasattr(np, "ndarray"), "numpy not functional")
74
+
75
+
76
+ def test_import_submodules():
77
+ import pyarrow.compute as pc
78
+ import pyarrow.csv
79
+ import pyarrow.json
80
+ import pyarrow.feather
81
+ import pyarrow.fs
82
+ import pyarrow.ipc
83
+ check(hasattr(pc, "cast"), "compute module incomplete")
84
+ print(" compute, csv, json, feather, fs, ipc -- all imported")
85
+
86
+
87
+ # ---------------------------------------------------------------------------
88
+ # 2. arrays
89
+ # ---------------------------------------------------------------------------
90
+ def test_array_creation():
91
+ import pyarrow as pa
92
+ a = pa.array([1, 2, 3, 4, 5])
93
+ check(a.type == pa.int64(), "type %r" % a.type)
94
+ check(len(a) == 5, "len %d" % len(a))
95
+ check(a.to_pylist() == [1, 2, 3, 4, 5], "values mismatch")
96
+ print(" int64 array:", a)
97
+
98
+
99
+ def test_array_float():
100
+ import pyarrow as pa
101
+ a = pa.array([1.0, 2.5, 3.7], type=pa.float64())
102
+ check(a.type == pa.float64(), "type %r" % a.type)
103
+ check(len(a) == 3, "len %d" % len(a))
104
+ print(" float64 array:", a)
105
+
106
+
107
+ def test_array_string():
108
+ import pyarrow as pa
109
+ a = pa.array(["hello", "world", "pyarrow"])
110
+ check(a.type == pa.string(), "type %r" % a.type)
111
+ check(a.to_pylist() == ["hello", "world", "pyarrow"])
112
+ print(" string array:", a)
113
+
114
+
115
+ def test_array_null():
116
+ import pyarrow as pa
117
+ a = pa.array([1, None, 3], type=pa.int64())
118
+ check(a.null_count == 1, "null_count %d" % a.null_count)
119
+ check(a.to_pylist() == [1, None, 3])
120
+ print(" null handling:", a)
121
+
122
+
123
+ # ---------------------------------------------------------------------------
124
+ # 3. types
125
+ # ---------------------------------------------------------------------------
126
+ def test_types():
127
+ import pyarrow as pa
128
+ t_int = pa.int64()
129
+ t_float = pa.float64()
130
+ t_str = pa.string()
131
+ t_bool = pa.bool_()
132
+ check(str(t_int) == "int64", "int64 str: %r" % str(t_int))
133
+ check(str(t_float) in ("float64", "double"), "float64 str: %r" % str(t_float))
134
+ check(str(t_str) == "string", "string str: %r" % str(t_str))
135
+ check(str(t_bool) == "bool", "bool str: %r" % str(t_bool))
136
+ check(isinstance(t_int, pa.DataType), "not DataType")
137
+ print(" types: int64, float64, string, bool -- all recognized")
138
+
139
+
140
+ # ---------------------------------------------------------------------------
141
+ # 4. tables
142
+ # ---------------------------------------------------------------------------
143
+ def test_table_creation():
144
+ import pyarrow as pa
145
+ t = pa.table({
146
+ "id": [1, 2, 3],
147
+ "name": ["alice", "bob", "charlie"],
148
+ "score": [95.5, 87.0, 92.3],
149
+ })
150
+ check(t.num_rows == 3, "rows %d" % t.num_rows)
151
+ check(t.num_columns == 3, "cols %d" % t.num_columns)
152
+ check(t.column_names == ["id", "name", "score"])
153
+ check(t.schema.field("id").type == pa.int64())
154
+ check(t.schema.field("name").type == pa.string())
155
+ check(t.schema.field("score").type == pa.float64())
156
+ print(" table: %d rows x %d cols" % (t.num_rows, t.num_columns))
157
+
158
+
159
+ def test_table_ops():
160
+ import pyarrow as pa
161
+ t = pa.table({"x": [10, 20, 30], "y": [1.0, 2.0, 3.0]})
162
+ check(t.column("x").to_pylist() == [10, 20, 30])
163
+ check(t.to_pandas().shape == (3, 2))
164
+ print(" table column access + to_pandas OK")
165
+
166
+
167
+ # ---------------------------------------------------------------------------
168
+ # 5. IPC round-trip
169
+ # ---------------------------------------------------------------------------
170
+ def test_ipc_roundtrip():
171
+ import pyarrow as pa
172
+ import pyarrow.ipc as ipc
173
+ t = pa.table({
174
+ "a": [1, 2, 3, 4, 5],
175
+ "b": ["x", "y", "z", "w", "v"],
176
+ "c": [1.1, 2.2, 3.3, 4.4, 5.5],
177
+ })
178
+ path = os.path.join(workdir(), "test_ipc.arrow")
179
+ sink = ipc.new_file(path, t.schema)
180
+ sink.write_table(t)
181
+ sink.close()
182
+ reader = ipc.open_file(path)
183
+ t2 = reader.read_all()
184
+ check(t.equals(t2), "IPC roundtrip mismatch")
185
+ os.unlink(path)
186
+ print(" IPC file: write -> read -> equals")
187
+
188
+
189
+ def test_ipc_stream():
190
+ import pyarrow as pa
191
+ import pyarrow.ipc as ipc
192
+ t = pa.table({"val": [100, 200, 300]})
193
+ path = os.path.join(workdir(), "test_ipc_stream.arrow")
194
+ sink = ipc.new_stream(path, t.schema)
195
+ sink.write_table(t)
196
+ sink.close()
197
+ reader = ipc.open_stream(path)
198
+ t2 = reader.read_all()
199
+ check(t.equals(t2), "IPC stream roundtrip mismatch")
200
+ os.unlink(path)
201
+ print(" IPC stream: write -> read -> equals")
202
+
203
+
204
+ # ---------------------------------------------------------------------------
205
+ # 6. CSV
206
+ # ---------------------------------------------------------------------------
207
+ def test_csv_write_read():
208
+ import pyarrow as pa
209
+ import pyarrow.csv as pcsv
210
+ t = pa.table({
211
+ "id": [1, 2, 3],
212
+ "name": ["alice", "bob", "charlie"],
213
+ "val": [10.5, 20.3, 30.1],
214
+ })
215
+ path = os.path.join(workdir(), "test_csv.csv")
216
+ pcsv.write_csv(t, path)
217
+ t2 = pcsv.read_csv(path)
218
+ check(t2.num_rows == 3, "rows %d" % t2.num_rows)
219
+ check(t2.num_columns == 3, "cols %d" % t2.num_columns)
220
+ os.unlink(path)
221
+ print(" CSV write -> read: %d rows x %d cols" % (t2.num_rows, t2.num_columns))
222
+
223
+
224
+ def test_csv_options():
225
+ import pyarrow as pa
226
+ import pyarrow.csv as pcsv
227
+ path = os.path.join(workdir(), "test_csv_opts.csv")
228
+ with open(path, "w") as fh:
229
+ fh.write("1,2,3\n4,5,6\n")
230
+ read_opts = pcsv.ReadOptions(column_names=["x", "y", "z"])
231
+ convert_opts = pcsv.ConvertOptions(column_types={"x": pa.int64(), "y": pa.int64(), "z": pa.int64()})
232
+ t = pcsv.read_csv(path, read_options=read_opts, convert_options=convert_opts)
233
+ check(t.column("x").to_pylist() == [1, 4])
234
+ check(t.schema.field("x").type == pa.int64())
235
+ os.unlink(path)
236
+ print(" CSV with custom options: column_names + column_types")
237
+
238
+
239
+ # ---------------------------------------------------------------------------
240
+ # 7. JSON
241
+ # ---------------------------------------------------------------------------
242
+ def test_json_read():
243
+ import pyarrow as pa
244
+ import pyarrow.json as pjson
245
+ path = os.path.join(workdir(), "test_json.json")
246
+ with open(path, "w") as fh:
247
+ fh.write('{"a": 1, "b": "hello"}\n')
248
+ fh.write('{"a": 2, "b": "world"}\n')
249
+ t = pjson.read_json(path)
250
+ check(t.num_rows == 2, "rows %d" % t.num_rows)
251
+ check("a" in t.column_names, "column 'a' missing")
252
+ check("b" in t.column_names, "column 'b' missing")
253
+ os.unlink(path)
254
+ print(" JSON read: %d rows, columns=%r" % (t.num_rows, t.column_names))
255
+
256
+
257
+ # ---------------------------------------------------------------------------
258
+ # 8. Feather round-trip
259
+ # ---------------------------------------------------------------------------
260
+ def test_feather_roundtrip():
261
+ import pyarrow as pa
262
+ import pyarrow.feather as pf
263
+ t = pa.table({
264
+ "id": [1, 2, 3, 4, 5],
265
+ "name": ["alice", "bob", "charlie", "diana", "eve"],
266
+ "score": [95.5, 87.0, 92.3, 88.8, 99.1],
267
+ })
268
+ path = os.path.join(workdir(), "test_feather.feather")
269
+ pf.write_feather(t, path)
270
+ t2 = pf.read_table(path)
271
+ check(t.equals(t2), "Feather roundtrip mismatch")
272
+ check(t2.num_rows == 5, "rows %d" % t2.num_rows)
273
+ os.unlink(path)
274
+ print(" Feather: write -> read -> equals (%d rows)" % t2.num_rows)
275
+
276
+
277
+ # ---------------------------------------------------------------------------
278
+ # 9. compute
279
+ # ---------------------------------------------------------------------------
280
+ def test_compute_basic():
281
+ import pyarrow as pa
282
+ import pyarrow.compute as pc
283
+ a = pa.array([1, 2, 3, 4, 5])
284
+ result = pc.sum(a)
285
+ check(result.as_py() == 15, "sum %r" % result)
286
+ print(" compute.sum:", result.as_py())
287
+
288
+
289
+ def test_compute_cast():
290
+ import pyarrow as pa
291
+ import pyarrow.compute as pc
292
+ a = pa.array([1, 2, 3], type=pa.int64())
293
+ b = pc.cast(a, pa.float64())
294
+ check(b.type == pa.float64(), "type %r" % b.type)
295
+ check(b.to_pylist() == [1.0, 2.0, 3.0])
296
+ print(" compute.cast int64 -> float64:", b)
297
+
298
+
299
+ def test_compute_filter():
300
+ import pyarrow as pa
301
+ import pyarrow.compute as pc
302
+ a = pa.array([10, 20, 30, 40, 50])
303
+ mask = pc.greater(a, 25)
304
+ filtered = pc.filter(a, mask)
305
+ check(filtered.to_pylist() == [30, 40, 50])
306
+ print(" compute.filter > 25:", filtered)
307
+
308
+
309
+ def test_compute_arithmetic():
310
+ import pyarrow as pa
311
+ import pyarrow.compute as pc
312
+ a = pa.array([10, 20, 30])
313
+ b = pa.array([1, 2, 3])
314
+ add_result = pc.add(a, b)
315
+ mul_result = pc.multiply(a, b)
316
+ check(add_result.to_pylist() == [11, 22, 33])
317
+ check(mul_result.to_pylist() == [10, 40, 90])
318
+ print(" compute add/multiply:", add_result, mul_result)
319
+
320
+
321
+ # ---------------------------------------------------------------------------
322
+ # 10. fs (filesystem)
323
+ # ---------------------------------------------------------------------------
324
+ def test_fs_local():
325
+ import pyarrow.fs as pfs
326
+ local = pfs.LocalFileSystem()
327
+ path = os.path.join(workdir(), "test_fs.txt")
328
+ with open(path, "w") as fh:
329
+ fh.write("filesystem test")
330
+ meta = local.get_file_info(path)
331
+ check(meta.type == pfs.FileType.File, "not a file")
332
+ check(meta.size > 0, "size %d" % meta.size)
333
+ os.unlink(path)
334
+ print(" LocalFileSystem: get_file_info OK (size=%d)" % meta.size)
335
+
336
+
337
+ # ---------------------------------------------------------------------------
338
+ # def main
339
+ # ---------------------------------------------------------------------------
340
+ def main():
341
+ section("pyarrow 25.0.1 - import / basics")
342
+ test("import pyarrow (25.x)", test_import_pyarrow)
343
+ test("import numpy (dependency)", test_import_numpy_dep)
344
+ test("import submodules (compute, csv, json, feather, fs, ipc)", test_import_submodules)
345
+
346
+ section("arrays")
347
+ test("array int64", test_array_creation)
348
+ test("array float64", test_array_float)
349
+ test("array string", test_array_string)
350
+ test("array null handling", test_array_null)
351
+
352
+ section("types")
353
+ test("types (int64, string, float64, bool)", test_types)
354
+
355
+ section("tables")
356
+ test("table creation + schema", test_table_creation)
357
+ test("table column access + to_pandas", test_table_ops)
358
+
359
+ section("IPC")
360
+ test("IPC file round-trip", test_ipc_roundtrip)
361
+ test("IPC stream round-trip", test_ipc_stream)
362
+
363
+ section("CSV")
364
+ test("CSV write / read", test_csv_write_read)
365
+ test("CSV custom options", test_csv_options)
366
+
367
+ section("JSON")
368
+ test("JSON read", test_json_read)
369
+
370
+ section("Feather")
371
+ test("Feather round-trip", test_feather_roundtrip)
372
+
373
+ section("compute")
374
+ test("compute.sum", test_compute_basic)
375
+ test("compute.cast", test_compute_cast)
376
+ test("compute.filter", test_compute_filter)
377
+ test("compute arithmetic", test_compute_arithmetic)
378
+
379
+ section("filesystem")
380
+ test("LocalFileSystem get_file_info", test_fs_local)
381
+
382
+ section("RESULT")
383
+ n_ok = n_fail = n_skip = 0
384
+ for r in RESULTS:
385
+ status = r[0]
386
+ if status == "PASS":
387
+ n_ok += 1
388
+ print(" OK %s" % r[1])
389
+ elif status == "SKIP":
390
+ n_skip += 1
391
+ print(" SKIP %s" % r[1])
392
+ else:
393
+ n_fail += 1
394
+ print(" FAIL %s: %s" % (r[1], r[2]))
395
+ print("RESULT: %d ok, %d failed, %d skipped" % (n_ok, n_fail, n_skip))
396
+ sys.exit(1 if n_fail else 0)
397
+
398
+
399
+ if __name__ == "__main__":
400
+ main()