You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
is there any way to convert NumSharp.NDArray to Numpy.NDarray? #383
Me and @henon have collaborated just two days ago on a solution for that,
The trick is to create a python scope, load a script that takes in a pointer-length and returns an np.ndarray
Remember! you have to keep reference to the original NDArray, otherwise the NDArray will be disposed and will release the memory. (@henon is there a tag field we can use in NDarray?)
Note: the code below is psuedo, the rest of the code can be found here: ConsoleApp9.zip
usingnumsharp_np=NumSharp.np;usingnumpy_np=Numpy.np;staticvoidMain(string[]args){varnumsharp_nd=numsharp_np.arange(10);varnumpy_nd=numsharp_nd.ToNumpyNET();numpy_nd[1]=(NDarray)5;Debug.Assert(numsharp_nd[1].array_equal(5));Console.WriteLine("numsharp_nd[1].array_equal(5) is indeed true!");}
Remember! you have to keep reference to the original NDArray, otherwise the NDArray will be disposed and will release the memory. (@henon is there a tag field we can use in NDarray?)
Not yet, but I will add one in Numpy.NET and support this.
@lelelemonade@davidvct, the manual pointer-scope trick from this thread is superseded. The new NumSharp.Interop.pythonnet package (0.70.0, #628) crosses both ways zero-copy, with GC-safe leases (no more "keep a reference or it gets disposed").
Set up the Python engine once at startup - either point pythonnet at an installed Python, or deploy one from NuGet with Python.Included (no system install needed):
usingNumSharp;usingNumSharp.Interop.PythonNet;usingPython.Runtime;// Option A - existing Python: the PYTHONNET_PYDLL env var, or the DLL directlyRuntime.PythonDLL="python312.dll";// or Environment.GetEnvironmentVariable("PYTHONNET_PYDLL")PythonEngine.Initialize();// Option B - deploy an embedded Python from NuGet (dotnet add package Python.Included)awaitPython.Included.Installer.SetupPython();awaitPython.Included.Installer.PipInstallModule("numpy");PythonEngine.Initialize();
Implicit (register the codec once, then NumSharp arrays auto-convert):
NDArrayPythonInterop.RegisterCodec();// once per processvarnd=np.arange(6).reshape(2,3);using(Py.GIL()){dynamicnumpy=Py.Import("numpy");Console.WriteLine(numpy.sum(nd));// 15 (nd auto-encoded by the codec)}
Activity
Nucs commentedon Jan 6, 2020
Me and @henon have collaborated just two days ago on a solution for that,
The trick is to create a python scope, load a script that takes in a pointer-length and returns an np.ndarray
Remember! you have to keep reference to the original NDArray, otherwise the NDArray will be disposed and will release the memory. (@henon is there a tag field we can use in NDarray?)
Note: the code below is psuedo, the rest of the code can be found here: ConsoleApp9.zip
numpy_interop.py as embedded resource
PythonExtensions.cs
main.cs
Nucs commentedon Jan 6, 2020
Added some changes that will make sure the NDarray will hold reference to the NDArray as long as the PyObject doesnt change internally.
ConsoleApp9.zip
henon commentedon Jan 6, 2020
Not yet, but I will add one in Numpy.NET and support this.
davidvct commentedon Feb 24, 2023
Hi, could you provide instruction on how to use the ConsoleApp9.zip? And does it able to convert Numpy array back to NumSharp array?
henon commentedon Feb 24, 2023
@davidvct, if I remember correctly I have added support for copying data to and from Numpy.NET like @Nucs did in this solution. Read this to see how: https://github.com/SciSharp/Numpy.NET#create-a-numpy-array-from-a-c-array-and-vice-versa
Nucs commentedon Sep 7, 2026
@lelelemonade @davidvct, the manual pointer-scope trick from this thread is superseded. The new NumSharp.Interop.pythonnet package (0.70.0, #628) crosses both ways zero-copy, with GC-safe leases (no more "keep a reference or it gets disposed").
Set up the Python engine once at startup - either point pythonnet at an installed Python, or deploy one from NuGet with Python.Included (no system install needed):
Explicit:
Implicit (register the codec once, then NumSharp arrays auto-convert):
Requires NumSharp.Interop.pythonnet (
dotnet add package NumSharp.Interop.pythonnet). Closing.