forked from SciSharp/Numpy.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonObject.gen.cs
More file actions
151 lines (142 loc) · 6.19 KB
/
PythonObject.gen.cs
File metadata and controls
151 lines (142 loc) · 6.19 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
// Copyright (c) 2020 by Meinrad Recheis (Member of SciSharp)
// Code generated by CodeMinion: https://github.com/SciSharp/CodeMinion
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Python.Runtime;
using Numpy.Models;
#if PYTHON_INCLUDED
using Python.Included;
#endif
namespace Numpy
{
public partial class PythonObject
{
//auto-generated
public PyTuple ToTuple(Array input)
{
var array = new PyObject[input.Length];
for (int i = 0; i < input.Length; i++)
{
array[i]=ToPython(input.GetValue(i));
}
return new PyTuple(array);
}
//auto-generated
public PyObject ToPython(object obj)
{
if (obj == null) return Runtime.None;
switch (obj)
{
// basic types
case int o: return new PyInt(o);
case long o: return new PyInt(o);
case float o: return new PyFloat(o);
case double o: return new PyFloat(o);
case string o: return new PyString(o);
case bool o: return ConverterExtension.ToPython(o);
case PyObject o: return o;
// sequence types
case Array o: return ToTuple(o);
// special types from 'ToPythonConversions'
case Axis o: return o.Axes==null ? null : ToTuple(o.Axes);
case Shape o: return ToTuple(o.Dimensions);
case Slice o: return o.ToPython();
case PythonObject o: return o.PyObject;
case Dictionary<string, NDarray> o: return ToDict(o);
default: throw new NotImplementedException($"Type is not yet supported: { obj.GetType().Name}. Add it to 'ToPythonConversions'");
}
}
//auto-generated
public T ToCsharp<T>(dynamic pyobj)
{
switch (typeof(T).Name)
{
// types from 'ToCsharpConversions'
case "Dtype": return (T)(object)new Dtype(pyobj);
case "NDarray": return (T)(object)new NDarray(pyobj);
case "NDarray`1":
switch (typeof(T).GenericTypeArguments[0].Name)
{
case "Byte": return (T)(object)new NDarray<byte>(pyobj);
case "Short": return (T)(object)new NDarray<short>(pyobj);
case "Boolean": return (T)(object)new NDarray<bool>(pyobj);
case "Int32": return (T)(object)new NDarray<int>(pyobj);
case "Int64": return (T)(object)new NDarray<long>(pyobj);
case "Single": return (T)(object)new NDarray<float>(pyobj);
case "Double": return (T)(object)new NDarray<double>(pyobj);
default: throw new NotImplementedException($"Type NDarray<{typeof(T).GenericTypeArguments[0].Name}> missing. Add it to 'ToCsharpConversions'");
}
break;
case "NDarray[]":
var po = pyobj as PyObject;
var len = po.Length();
var rv = new NDarray[len];
for (int i = 0; i < len; i++)
rv[i] = ToCsharp<NDarray>(po[i]);
return (T) (object) rv;
case "Matrix": return (T)(object)new Matrix(pyobj);
default:
var pyClass = $"{pyobj.__class__}";
if (pyClass == "<class 'str'>")
{
return (T)(object)pyobj.ToString();
}
if (pyClass.StartsWith("<class 'numpy"))
{
return (pyobj.item() as PyObject).As<T>();
}
try
{
return pyobj.As<T>();
}
catch (Exception e)
{
throw new NotImplementedException($"conversion from {pyobj.__class__} to {typeof(T).Name} not implemented", e);
return default(T);
}
}
}
//auto-generated
public T SharpToSharp<T>(object obj)
{
if (obj == null) return default(T);
switch (obj)
{
// from 'SharpToSharpConversions':
case Array a:
if (typeof(T)==typeof(NDarray)) return (T)(object)ConvertArrayToNDarray(a);
break;
}
throw new NotImplementedException($"Type is not yet supported: { obj.GetType().Name}. Add it to 'SharpToSharpConversions'");
}
//auto-generated: SpecialConversions
private static NDarray ConvertArrayToNDarray(Array a)
{
switch(a)
{
case bool[] arr: return np.array(arr);
case int[] arr: return np.array(arr);
case float[] arr: return np.array(arr);
case double[] arr: return np.array(arr);
case int[,] arr: return np.array(arr.Cast<int>().ToArray()).reshape(arr.GetLength(0), arr.GetLength(1));
case float[,] arr: return np.array(arr.Cast<float>().ToArray()).reshape(arr.GetLength(0), arr.GetLength(1));
case double[,] arr: return np.array(arr.Cast<double>().ToArray()).reshape(arr.GetLength(0), arr.GetLength(1));
case bool[,] arr: return np.array(arr.Cast<bool>().ToArray()).reshape(arr.GetLength(0), arr.GetLength(1));
default: throw new NotImplementedException($"Type {a.GetType()} not supported yet in ConvertArrayToNDarray.");
}
}
//auto-generated: SpecialConversions
private static PyDict ToDict(Dictionary<string, NDarray> d)
{
var dict = new PyDict();
foreach (var pair in d)
dict[new PyString(pair.Key)] = pair.Value.self;
return dict;
}
}
}