forked from yagweb/pythonnetLab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumpy.cs
More file actions
168 lines (147 loc) · 6.03 KB
/
Copy pathNumpy.cs
File metadata and controls
168 lines (147 loc) · 6.03 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Python.Runtime
{
public class NumpyException : System.ApplicationException
{
public NumpyException() { }
public NumpyException(string message) { }
public NumpyException(string message, System.Exception inner) { }
}
public class Numpy
{
public class NumpyArrayInterface
{
public NumpyArrayInterface(PyObject o)
{
if (o.GetPythonType().Handle != NumpyArrayType)
{
throw new Exception("object is not a numpy array");
}
var meta = o.GetAttr("__array_interface__");
IsCStyleContiguous = meta["strides"] == null;
Address = new System.IntPtr((long)meta["data"][0].As<long>());
var typestr = meta["typestr"].As<string>();
var dtype = typestr.Substring(1);
switch(dtype)
{
case "b1":
DataType = typeof(bool);
break;
case "f4":
DataType = typeof(float);
break;
case "f8":
DataType = typeof(double);
break;
case "i2":
DataType = typeof(short);
break;
case "i4":
DataType = typeof(int);
break;
case "i8":
DataType = typeof(long);
break;
case "u1":
DataType = typeof(byte);
break;
case "u2":
DataType = typeof(ushort);
break;
case "u4":
DataType = typeof(uint);
break;
case "u8":
DataType = typeof(ulong);
break;
default:
throw new NumpyException($"type '{dtype}' not supported");
}
Shape = o.GetAttr("shape").As<long[]>();
NBytes = o.GetAttr("nbytes").As<int>();
}
public readonly IntPtr Address;
public readonly System.Type DataType;
public readonly long[] Shape;
public readonly int NBytes;
public readonly bool IsCStyleContiguous;
}
public static void Initialize()
{
np = Py.Import("numpy");
NumpyArrayType = np.GetAttr("ndarray").Handle;
np_dtypes.Clear();
np_dtypes.Add(typeof(byte), np.GetAttr("uint8"));
np_dtypes.Add(typeof(short), np.GetAttr("int16"));
np_dtypes.Add(typeof(int), np.GetAttr("int32"));
np_dtypes.Add(typeof(long), np.GetAttr("int64"));
np_dtypes.Add(typeof(ushort), np.GetAttr("uint16"));
np_dtypes.Add(typeof(uint), np.GetAttr("uint32"));
np_dtypes.Add(typeof(ulong), np.GetAttr("uint64"));
np_dtypes.Add(typeof(float), np.GetAttr("float"));
np_dtypes.Add(typeof(double), np.GetAttr("float64"));
var copy = Py.Import("copy");
deepcopy = copy.GetAttr("deepcopy");
}
/// <summary>
/// numpy Module
/// </summary>
internal static PyObject np;
internal static Dictionary<Type, PyObject> np_dtypes = new Dictionary<Type, PyObject>();
public static PyObject GetNumpyDataType(Type type)
{
PyObject dtype;
np_dtypes.TryGetValue(type, out dtype);
if(dtype == null)
{
throw new NumpyException($"type '{type}' not supported.");
}
return dtype;
}
internal static PyObject deepcopy;
internal static IntPtr NumpyArrayType;
public static PyObject NewArray(Array content)
{
// BlockCopy possibly multidimensional array of arbitrary type to onedimensional byte array
System.Type ElementType = content.GetType().GetElementType();
int nbytes = content.Length * Marshal.SizeOf(ElementType);
byte[] data = new byte[nbytes];
System.Buffer.BlockCopy(content, 0, data, 0, nbytes);
// Create an python tuple with the dimensions of the input array
PyObject[] lengths = new PyObject[content.Rank];
for (int i = 0; i < content.Rank; i++)
lengths[i] = new PyInt(content.GetLength(i));
PyTuple shape = new PyTuple(lengths);
// Create an empty numpy array in correct shape and datatype
var dtype = GetNumpyDataType(ElementType);
var arr = np.InvokeMethod("empty", shape, dtype);
var meta = arr.GetAttr("__array_interface__");
var address = new System.IntPtr((long)meta["data"][0].As<long>());
// Copy the data to that array
Marshal.Copy(data, 0, address, nbytes);
return arr;
}
public static Array ToArray(PyObject array)
{
var info = new NumpyArrayInterface(array);
// If the array is not contiguous in memory, copy it first.
// This overwrites the array (but obviously the contents stay the same).
PyObject arr = array;
if(!info.IsCStyleContiguous)
{
arr = deepcopy.Invoke(array);
}
byte[] data = new byte[info.NBytes];
Marshal.Copy(info.Address, data, 0, info.NBytes);
if (info.DataType == typeof(byte) && info.Shape.Length == 1)
{
return data;
}
var result = System.Array.CreateInstance(info.DataType, info.Shape);
System.Buffer.BlockCopy(data, 0, result, 0, info.NBytes);
return result;
}
}
}