forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython.cs
More file actions
165 lines (145 loc) · 4.46 KB
/
Python.cs
File metadata and controls
165 lines (145 loc) · 4.46 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
using NumSharp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace Tensorflow
{
/// <summary>
/// Mapping C# functions to Python
/// </summary>
public class Python
{
protected void print(object obj)
{
Console.WriteLine(obj.ToString());
}
protected int len<T>(IEnumerable<T> a)
=> a.Count();
protected IEnumerable<int> range(int end)
{
return Enumerable.Range(0, end);
}
public static T New<T>(object args) where T : IPyClass
{
var instance = Activator.CreateInstance<T>();
instance.__init__(instance, args);
return instance;
}
[DebuggerNonUserCode()] // with "Just My Code" enabled this lets the debugger break at the origin of the exception
public static void with(IPython py, Action<IPython> action)
{
try
{
py.__enter__();
action(py);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw;
}
finally
{
py.__exit__();
py.Dispose();
}
}
[DebuggerNonUserCode()] // with "Just My Code" enabled this lets the debugger break at the origin of the exception
public static void with<T>(T py, Action<T> action) where T : IPython
{
try
{
py.__enter__();
action(py);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw;
}
finally
{
py.__exit__();
py.Dispose();
}
}
[DebuggerNonUserCode()] // with "Just My Code" enabled this lets the debugger break at the origin of the exception
public static TOut with<TIn, TOut>(TIn py, Func<TIn, TOut> action) where TIn : IPython
{
try
{
py.__enter__();
return action(py);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw;
return default(TOut);
}
finally
{
py.__exit__();
py.Dispose();
}
}
public static float time()
{
return (float)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
}
public static IEnumerable<(T, T)> zip<T>(NDArray t1, NDArray t2)
{
for (int i = 0; i < t1.size; i++)
yield return (t1.Data<T>(i), t2.Data<T>(i));
}
public static IEnumerable<(T1, T2)> zip<T1, T2>(IList<T1> t1, IList<T2> t2)
{
for (int i = 0; i < t1.Count; i++)
yield return (t1[i], t2[i]);
}
public static IEnumerable<(T1, T2)> zip<T1, T2>(NDArray t1, NDArray t2)
{
for (int i = 0; i < t1.size; i++)
yield return (t1.Data<T1>(i), t2.Data<T2>(i));
}
public static IEnumerable<(T1, T2)> zip<T1, T2>(IEnumerable<T1> e1, IEnumerable<T2> e2)
{
var iter2 = e2.GetEnumerator();
foreach (var v1 in e1)
{
iter2.MoveNext();
var v2 = iter2.Current;
yield return (v1, v2);
}
}
public static IEnumerable<(int, T)> enumerate<T>(IList<T> values)
{
for (int i = 0; i < values.Count; i++)
yield return (i, values[i]);
}
public static Dictionary<string, object> ConvertToDict(object dyn)
{
var dictionary = new Dictionary<string, object>();
foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(dyn))
{
object obj = propertyDescriptor.GetValue(dyn);
string name = propertyDescriptor.Name;
dictionary.Add(name, obj);
}
return dictionary;
}
}
public interface IPython : IDisposable
{
void __enter__();
void __exit__();
}
public class PyObject<T> where T : IPyClass
{
public T Instance { get; set; }
}
}