private IList <string> GetLoadedModuleNames() { var sys = PythonEngine.ImportModule("sys"); var modules = new PyDict(sys.GetAttr("modules")); return(modules.Keys().OfType <PyObject>().Select(x => x.As <string>()).ToList()); }
public static Dictionary <string, T> ToClr <T>(PyDict dict) { Dictionary <string, T> result = new Dictionary <string, T>(); string[] keys = dict.Keys().As <string[]>(); foreach (var item in keys) { result[item] = dict[item].As <T>(); } return(result); }
public static Dictionary <string, NDArray> ToStrNDArray(PyDict dict) { Dictionary <string, NDArray> result = new Dictionary <string, NDArray>(); string[] keys = dict.Keys().As <string[]>(); foreach (var item in keys) { result.Add(item, new NDArray(dict[item])); } return(result); }
public static Dictionary <string, Shape> ToStrShape(PyDict dict) { Dictionary <string, Shape> result = new Dictionary <string, Shape>(); string[] keys = dict.Keys().As <string[]>(); foreach (var item in keys) { result.Add(item, new Shape(dict[item])); } return(result); }
/// <summary> /// Gets the index of the word. /// </summary> /// <param name="path">The path.</param> /// <returns></returns> public static Dictionary <string, int> GetWordIndex(string path = "imdb_word_index.json") { Dictionary <string, int> result = new Dictionary <string, int>(); PyDict py = new PyDict((PyObject)Instance.keras.datasets.imdb.get_word_index(path: path)); string[] keys = py.Keys().As <string[]>(); foreach (var item in keys) { result[item] = py[item].As <int>(); } return(result); }
private void ReloadModules() { int count = 0; var sw = new Stopwatch(); sw.Start(); var sys = PythonEngine.ImportModule("sys"); var importLib = PythonEngine.ImportModule("importlib"); var modules = new PyDict(sys.GetAttr("modules")); foreach (var key in modules.Keys().OfType <PyObject>()) { try { var name = key.As <string>(); if (modulesExcludedFormReload.Contains(name)) { continue; } var module = modules.GetItem(key); if (!name.Contains("importlib")) { if (module.HasAttr("__file__") && module.HasAttr("__name__")) { var path = module.GetAttr("__file__").As <string>(); importLib.InvokeMethod("reload", module); count += 1; } } } catch (PythonException) { } } sw.Stop(); logger.LogInformation($"[PythonModules] Reloading {count} Python modules took {sw.ElapsedMilliseconds}ms."); }
/// <summary> /// Converts a <see cref="PyDict"/> of string to pandas.Series in a pandas.DataFrame /// </summary> /// <param name="pyDict"><see cref="PyDict"/> of string to pandas.Series</param> /// <returns><see cref="PyObject"/> containing a pandas.DataFrame</returns> private PyObject MakeIndicatorDataFrame(PyDict pyDict) { return(_pandas.DataFrame(pyDict, columns: pyDict.Keys().Select(x => x.As <string>().ToLowerInvariant()).OrderBy(x => x))); }
public static object ToClrObject(PyObject obj, Type expectedType) { if (obj == null) { return(null); } var pyType = obj.GetPythonType(); var typeName = pyType.GetAttr("__name__").ToString(); if (typeName == "NoneType") { return(null); } else if (typeName == "str") { return(obj.As <string>()); } else if (typeName == "int") { return(obj.As <int>()); } else if (typeName == "float") { return(obj.As <double>()); } else if (typeName == "bool") { return(obj.As <bool>()); } else if (NumpyHelper.IsNumpyPrimitive(typeName)) { return(NumpyHelper.ToPrimitiveType(typeName, obj)); } else if (typeName == "ndarray") { return(NumpyHelper.ToA(obj)); } else if (typeName == "tuple") { int length = obj.Length(); if (length == 0) { return(null); } var pyItems = new List <PyObject>(length); for (int i = 0; i < length; i += 1) { PyObject item = obj[i]; pyItems.Add(item); } object[] clrItems; object tuple; if (typeof(ITuple).IsAssignableFrom(expectedType)) { var types = TypeHelpers.GetTupleTypes(expectedType); clrItems = pyItems.Select((x, i) => ToClrObject(x, types[i])).ToArray(); tuple = Activator.CreateInstance(expectedType, clrItems); } else if (expectedType == typeof(object)) { var types = new Type[length]; Array.Fill(types, typeof(object)); var tupleType = TypeHelpers.CreateTupleType(types); clrItems = pyItems.Select(x => ToClrObject(x, typeof(object))).ToArray(); tuple = Activator.CreateInstance(tupleType, clrItems); } else { throw new Exception($"Target type {expectedType.Name} cannot be assigned from python tuple."); } return(tuple); } else if (typeName == "list" || typeName == "deque") { Type elementType; Type listType = TypeHelpers.GetGenericTypeBase(expectedType, typeof(IList <>)); IList list; if (listType != null) { elementType = listType.GetGenericArguments()[0]; listType = typeof(List <>).MakeGenericType(elementType); list = (IList)Activator.CreateInstance(listType); } else if (expectedType == typeof(object)) { elementType = typeof(object); list = new List <object>(); } else { throw new Exception($"Target type {expectedType.Name} cannot be assigned from python list."); } int length = obj.Length(); for (int i = 0; i < length; i += 1) { PyObject item = obj[i]; list.Add(ToClrObject(item, elementType)); } return(list); } else if (typeName == "dict") { Type keyType; Type valueType; Type dictionaryType = TypeHelpers.GetGenericTypeBase(expectedType, typeof(IDictionary <,>)); IDictionary dictionary; if (dictionaryType != null) { var genericArgs = dictionaryType.GetGenericArguments(); keyType = genericArgs[0]; valueType = genericArgs[1]; dictionaryType = typeof(Dictionary <,>).MakeGenericType(keyType, valueType); dictionary = (IDictionary)Activator.CreateInstance(dictionaryType); } else if (expectedType == typeof(object)) { keyType = typeof(string); valueType = typeof(object); dictionary = new Dictionary <string, object>(); } else { throw new Exception($"Target type {expectedType.Name} cannot be assigned from python dict."); } var dict = new PyDict(obj); foreach (PyObject key in dict.Keys()) { PyObject value = dict[key]; dictionary.Add(ToClrObject(key, keyType), ToClrObject(value, valueType)); } return(dictionary); } else if (typeName == "JointSet") { return(RoboticsTypesExtensions.FromPyJointSet(obj)); } else if (typeName == "JointValues") { return(RoboticsTypesExtensions.FromPyJointValues(obj)); } else if (typeName == "Pose") { return(RoboticsTypesExtensions.FromPyPose(obj)); } else if (typeName == "timedelta") { return(RoboticsTypesExtensions.FromPyTimeDelta(obj)); } else if (typeName == "JointTrajectoryPoint") { return(RoboticsTypesExtensions.FromPyJoinTrajectoryPoint(obj)); } else if (typeName == "JointTrajectory") { return(RoboticsTypesExtensions.FromPyJointTrajectory(obj)); } else if (typeName == "JointStates") { return(RoboticsTypesExtensions.FromPyJointStates(obj)); } else if (typeName == "JointPath") { return(RoboticsTypesExtensions.FromPyJointPath(obj)); } else if (typeName == "CartesianPath") { return(RoboticsTypesExtensions.FromPyCartesianPath(obj)); } else if (typeName == "PlanParameters") { return(RoboticsTypesExtensions.FromPyPlanParameters(obj)); } else if (typeName == "CollisionPrimitive") { return(RoboticsTypesExtensions.FromPyCollisionPrimitive(obj)); } else if (typeName == "CollisionObject") { return(RoboticsTypesExtensions.FromPyCollisionObject(obj)); } else { throw new Exception($"Encountered unsupported type annotation '{pyType.ToString()}'."); } }