- Notifications
You must be signed in to change notification settings - Fork749
Implements buffer interface for .NET arrays of primitive types#1511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
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(); | ||
} | ||
[Test] | ||
public void TestReadme() | ||
{ | ||
dynamic np; | ||
try | ||
{ | ||
np = Py.Import("numpy"); | ||
} | ||
catch (PythonException) | ||
{ | ||
Assert.Inconclusive("Numpy or dependency not installed"); | ||
return; | ||
} | ||
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; | ||
try { | ||
np = Py.Import("numpy"); | ||
} catch (PythonException) { | ||
Assert.Inconclusive("Numpy or dependency not installed"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. We should probably make that | ||
return; | ||
} | ||
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()].InvokeMethod("__int__").As<int>()); | ||
Assert.AreEqual(array[1, 0], ndarray[(1, 0).ToPython()].InvokeMethod("__int__").As<int>()); | ||
} | ||
[Test] | ||
public void Int64Array() | ||
{ | ||
PyObject np; | ||
try | ||
{ | ||
np = Py.Import("numpy"); | ||
} | ||
catch (PythonException) | ||
{ | ||
Assert.Inconclusive("Numpy or dependency not installed"); | ||
return; | ||
} | ||
var array = new long[,] { { 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()].InvokeMethod("__int__").As<long>()); | ||
Assert.AreEqual(array[1, 0], ndarray[(1, 0).ToPython()].InvokeMethod("__int__").As<long>()); | ||
} | ||
} | ||
} |
This file was deleted.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.