-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathnumpy_conversion.swift
48 lines (38 loc) · 1.52 KB
/
numpy_conversion.swift
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
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
//
// `numpy.ndarray` conversion tests.
import Python
import StdlibUnittest
var NumpyConversionTests = TestSuite("NumpyConversion")
let numpyModule = try? Python.attemptImport("numpy")
NumpyConversionTests.test("array-conversion") {
guard let np = numpyModule else { return }
let numpyArrayEmpty = np.array([] as [Float], dtype: np.float32)
if let array = expectNotNil(Array<Float>(numpy: numpyArrayEmpty)) {
expectEqual([], array)
}
let numpyArrayBool = np.array([true, false, false, true])
if let array = expectNotNil(Array<Bool>(numpy: numpyArrayBool)) {
expectEqual([true, false, false, true], array)
}
let numpyArrayFloat = np.ones([6], dtype: np.float32)
if let array = expectNotNil(Array<Float>(numpy: numpyArrayFloat)) {
expectEqual(Array(repeating: 1, count: 6), array)
}
let numpyArrayInt32 = np.array([-1, 4, 25, 2018], dtype: np.int32)
if let array = expectNotNil(Array<Int32>(numpy: numpyArrayInt32)) {
expectEqual([-1, 4, 25, 2018], array)
}
let numpyArray2D = np.ones([2, 3])
expectNil(Array<Float>(numpy: numpyArray2D))
let numpyArrayStrided = np.array([[1, 2], [1, 2]], dtype: np.int32)[
Python.slice(Python.None), 1]
// Assert that the array has a stride, so that we're certainly testing a
// strided array.
expectNotEqual(numpyArrayStrided.__array_interface__["strides"], Python.None)
if let array = expectNotNil(Array<Int32>(numpy: numpyArrayStrided)) {
expectEqual([2, 2], array)
}
}
runAllTests()