I'm using pythonnet to integrate Python code into my C# program. Specifically, I'm attempting to apply a Fast Fourier Transform (FFT) on a complex array. While the code works flawlessly in Python, I encounter strange errors when attempting to use it in C#. The error message suggests that the input must be a real number and not complex, which seems contradictory.
Here's the C# code snippet:
using System.Globalization;
using System.Net.Mime;
using System.Numerics;
using Python.Runtime;
static void Initialize()
{
string pythonDll = @"C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\python39.dll";
Environment.SetEnvironmentVariable("PYTHONNET_PYDLL", pythonDll);
PythonEngine.Initialize();
}
Initialize();
Complex[] complexes = [
new Complex(1.0, 2.0),
new Complex(3.0, 4.0),
new Complex(5.0, 6.0),
new Complex(5.0, 6.0)
];
List<Complex> comlist = new List<Complex>(complexes);
using (Py.GIL())
{
dynamic lst = new PyList();
lst.append(2);
lst.append(2);
dynamic tuple = PyTuple.AsTuple(lst);
dynamic np = Py.Import("numpy");
dynamic rawData = np.array(comlist);
rawData.reshape(tuple);
dynamic k = np.reshape(a: rawData.T, newshape:tuple, order:'F');
k = np.fft.fftshift(k);
k = np.fft.fftn(k);
k = np.fft.fftshift(k);
k = np.rot90(k);
k = np.flip(k, axis : 0);
k = np.flip(np.rot90(np.fft.fftshift(np.fft.fftn(np.fft.fftshift(k)))));
Console.WriteLine(k.shape);
}
And here's the exception encountered:
Unhandled exception. Python.Runtime.PythonException: must be real number, not Complex
File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\fft\_pocketfft.py", line 74, in _raw_fft
r = pfi.execute(a, is_real, is_forward, fct)
File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\fft\_pocketfft.py", line 185, in fft
output = _raw_fft(a, n, axis, False, True, inv_norm)
File "<__array_function__ internals>", line 5, in fft
File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\fft\_pocketfft.py", line 652, in _raw_fftnd
a = function(a, n=s[ii], axis=axes[ii], norm=norm)
File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\fft\_pocketfft.py", line 755, in fftn
return _raw_fftnd(a, s, axes, fft, norm)
File "<__array_function__ internals>", line 5, in fftn
at Python.Runtime.PythonException.ThrowLastAsClrException()
at Python.Runtime.PyObject.Invoke(PyTuple args, PyDict kw)
at Python.Runtime.PyObject.InvokeMethod(String name, PyTuple args, PyDict kw)
at Python.Runtime.PyObject.TryInvokeMember(InvokeMemberBinder binder, Object[] args, Object& result)
at CallSite.Target(Closure, CallSite, Object, Object)
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
at Program.<Main>$(String[] args) in C:\Users\Mohsen\RiderProjects\ConsoleApp2\ConsoleApp2\Program.cs:line 39
Despite thorough checks, the code works smoothly in Python without any errors.
NumPy doesn't know .NET Complex type. You need to either use Python complex type or have NumPy convert a 1D array of floats.