/***************************************************************************** Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ******************************************************************************/ using NumSharp; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using NumSharp.Utilities; namespace Tensorflow { /// /// Binding utilities to mimic python functions. /// public static partial class Binding { public static T2 get(this Dictionary dict, T1 key) => key == null ? default(T2) : (dict.ContainsKey(key) ? dict[key] : default(T2)); public static void add(this IList list, T element) => list.Add(element); public static void append(this IList list, T element) => list.Add(element); public static void extend(this List list, IEnumerable elements) => list.AddRange(elements); private static string _tostring(object obj) { switch (obj) { case NDArray nd: return nd.ToString(false); case Array arr: if (arr.Rank!=1 || arr.GetType().GetElementType()?.IsArray == true) arr = Arrays.Flatten(arr); var objs = toObjectArray(arr); return $"[{string.Join(", ", objs.Select(_tostring))}]"; default: return obj?.ToString() ?? "null"; } object[] toObjectArray(Array arr) { var len = arr.LongLength; var ret = new object[len]; for (long i = 0; i < len; i++) { ret[i] = arr.GetValue(i); } return ret; } } public static void print(object obj) { Console.WriteLine(_tostring(obj)); } public static int len(object a) { switch (a) { case Array arr: return arr.Length; case IList arr: return arr.Count; case ICollection arr: return arr.Count; case NDArray ndArray: return ndArray.ndim == 0 ? 1 : ndArray.shape[0]; case IEnumerable enumerable: return enumerable.OfType().Count(); } throw new NotImplementedException("len() not implemented for type: " + a.GetType()); } public static float min(float a, float b) => Math.Min(a, b); public static T[] list(IEnumerable list) => list.ToArray(); public static IEnumerable range(int end) { return Enumerable.Range(0, end); } public static IEnumerable range(int start, int end) { return Enumerable.Range(start, end - start); } public static T New() where T : IObjectLife, new() { var instance = new T(); instance.__init__(); return instance; } [DebuggerStepThrough] [DebuggerNonUserCode()] // with "Just My Code" enabled this lets the debugger break at the origin of the exception public static void tf_with(IObjectLife py, Action action) { try { py.__enter__(); action(py); } finally { py.__exit__(); py.Dispose(); } } [DebuggerStepThrough] [DebuggerNonUserCode()] // with "Just My Code" enabled this lets the debugger break at the origin of the exception public static void tf_with(T py, Action action) where T : IObjectLife { try { py.__enter__(); action(py); } finally { py.__exit__(); py.Dispose(); } } [DebuggerStepThrough] [DebuggerNonUserCode()] // with "Just My Code" enabled this lets the debugger break at the origin of the exception public static TOut tf_with(TIn py, Func action) where TIn : IObjectLife { try { py.__enter__(); return action(py); } 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(NDArray t1, NDArray t2) where T : unmanaged { var a = t1.AsIterator(); var b = t2.AsIterator(); while (a.HasNext() && b.HasNext()) yield return (a.MoveNext(), b.MoveNext()); } public static IEnumerable<(T1, T2)> zip(IList t1, IList t2) { for (int i = 0; i < t1.Count; i++) yield return (t1[i], t2[i]); } public static IEnumerable<(T1, T2, T3)> zip(IList t1, IList t2, IList t3) { for (int i = 0; i < t1.Count; i++) yield return (t1[i], t2[i], t3[i]); } public static IEnumerable<(T1, T2)> zip(NDArray t1, NDArray t2) where T1: unmanaged where T2: unmanaged { var a = t1.AsIterator(); var b = t2.AsIterator(); while(a.HasNext() && b.HasNext()) yield return (a.MoveNext(), b.MoveNext()); } public static IEnumerable<(T1, T2)> zip(IEnumerable e1, IEnumerable e2) { return e1.Zip(e2, (t1, t2) => (t1, t2)); } public static IEnumerable<(TKey, TValue)> enumerate(Dictionary values) { foreach (var item in values) yield return (item.Key, item.Value); } public static IEnumerable<(TKey, TValue)> enumerate(KeyValuePair[] values) { var len = values.Length; for (var i = 0; i < len; i++) { var item = values[i]; yield return (item.Key, item.Value); } } public static IEnumerable<(int, T)> enumerate(IList values) { var len = values.Count; for (int i = 0; i < len; i++) yield return (i, values[i]); } [DebuggerStepThrough] public static Dictionary ConvertToDict(object dyn) { var dictionary = new Dictionary(); foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(dyn)) { object obj = propertyDescriptor.GetValue(dyn); string name = propertyDescriptor.Name; dictionary.Add(name, obj); } return dictionary; } public static bool all(IEnumerable enumerable) { foreach (var e1 in enumerable) { if (!Convert.ToBoolean(e1)) return false; } return true; } public static bool any(IEnumerable enumerable) { foreach (var e1 in enumerable) { if (Convert.ToBoolean(e1)) return true; } return false; } public static double sum(IEnumerable enumerable) { var typedef = new Type[] { typeof(double), typeof(int), typeof(float) }; var sum = 0.0d; foreach (var e1 in enumerable) { if (!typedef.Contains(e1.GetType())) throw new Exception("Numeric array expected"); sum += (double)e1; } return sum; } public static float sum(IEnumerable enumerable) => enumerable.Sum(); public static int sum(IEnumerable enumerable) => enumerable.Sum(); public static double sum(Dictionary values) { return sum(values.Keys); } public static IEnumerable slice(double start, double end, double step = 1) { for (double i = start; i < end; i += step) yield return i; } public static IEnumerable slice(float start, float end, float step = 1) { for (float i = start; i < end; i += step) yield return i; } public static IEnumerable slice(int start, int end, int step = 1) { for (int i = start; i < end; i += step) yield return i; } public static IEnumerable slice(int range) { for (int i = 0; i < range; i++) yield return i; } public static bool hasattr(object obj, string key) { var __type__ = (obj).GetType(); var __member__ = __type__.GetMembers(); var __memberobject__ = __type__.GetMember(key); return (__memberobject__.Length > 0) ? true : false; } public static IEnumerable TupleToEnumerable(object tuple) { Type t = tuple.GetType(); if (t.IsGenericType && (t.FullName.StartsWith("System.Tuple") || t.FullName.StartsWith("System.ValueTuple"))) { var flds = t.GetFields(); for (int i = 0; i < flds.Length; i++) { yield return flds[i].GetValue(tuple); } } else { throw new System.Exception("Expected Tuple."); } } public static bool isinstance(object Item1, Type Item2) { return Item1.GetType() == Item2; } public static bool isinstance(object Item1, object tuple) { foreach (var t in TupleToEnumerable(tuple)) if (isinstance(Item1, (Type) t)) return true; return false; } } }