forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNumPyTests.cs
More file actions
83 lines (69 loc) · 2.37 KB
/
NumPyTests.cs
File metadata and controls
83 lines (69 loc) · 2.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
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Python.Runtime;
using Python.Runtime.Codecs;
namespace Python.EmbeddingTest
{
public class NumPyTests
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
TupleCodec<ValueTuple>.Register();
}
[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}
static PyObject GetNumPy() {
PyObject np;
try {
np = Py.Import("numpy");
} catch (PythonException) {
Assert.Inconclusive("Numpy or dependency not installed");
throw;
}
return np;
}
[Test]
public void TestReadme()
{
dynamic np = GetNumPy();
Assert.AreEqual("1.0", np.cos(np.pi * 2).ToString());
dynamic sin = np.sin;
StringAssert.StartsWith("-0.95892", sin(5).ToString());
double c = np.cos(5) + sin(5);
Assert.AreEqual(-0.675262, c, 0.01);
dynamic a = np.array(new List<float> { 1, 2, 3 });
Assert.AreEqual("float64", a.dtype.ToString());
dynamic b = np.array(new List<float> { 6, 5, 4 }, Py.kw("dtype", np.int32));
Assert.AreEqual("int32", b.dtype.ToString());
Assert.AreEqual("[ 6. 10. 12.]", (a * b).ToString().Replace(" ", " "));
}
[Test]
public void MultidimensionalNumPyArray()
{
PyObject np = GetNumPy();
var array = new[,] { { 1, 2 }, { 3, 4 } };
var ndarray = np.InvokeMethod("asarray", array.ToPython());
Assert.AreEqual((2,2), ndarray.GetAttr("shape").As<(int,int)>());
Assert.AreEqual(1, ndarray[(0, 0).ToPython()].As<int>());
Assert.AreEqual(array[1, 0], ndarray[(1, 0).ToPython()].As<int>());
}
[Test]
public void Iterate()
{
PyObject np = GetNumPy();
var size = new[] { 3, 2 };
var ndarray = np.InvokeMethod("zeros", size.ToPython());
var iterator = ndarray.GetIterator();
Assert.True(iterator.MoveNext());
Assert.True(iterator.MoveNext());
Assert.True(iterator.MoveNext());
Assert.False(iterator.MoveNext());
}
}
}