GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — PythonInterface/PythonInterface/PythonConnection.cs at master · ncgeib/PythonInterface Expand file tree/ Copy pathPythonConnection.csMore file actions 479 lines (425 loc) · 17.5 KB / Copy pathPythonConnection.csFile metadata and controls479 lines (425 loc) · 17.5 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 using System.Collections.Generic; using System.Runtime.InteropServices; using System.Windows.Forms; using System.IO; // we need IO for file path manipulations using Python.Runtime; // this is the .NET wrapper for python namespace PythonInterface { public class PythonException : System.ApplicationException { public PythonException() { } public PythonException(string message) { } public PythonException(string message, System.Exception inner) { } // Constructor needed for serialization // when exception propagates from a remoting server to the client. protected PythonException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } } public class PythonConnection { /// <summary> /// The path to the base directory of a full python distribution (e.g. WinPython). /// This is static as one process can only obtain one embedded python interpreter. /// </summary> /// <param name="PyHome"></param> static string PythonHome; /// <summary> /// The main modules which are loaded after initialization of the interpeter. /// </summary> static dynamic Main = null; /// <summary> /// Additional paths which are searched for modules. /// </summary> private List<string> AdditionalPaths = new List<string>(); /// <summary> /// The whole paths which are searched for modules. /// </summary> private List<string> Pythonpath; /// <summary> /// The local and global dictionaries are stacks. In that way you can switch between the environments /// transparantly. However, currently only local changing is allowed. /// </summary> private Stack<PyDict> Locals = new Stack<PyDict>(); private Stack<PyDict> Globals = new Stack<PyDict>(); /// <summary> /// The reference to the numpy package if it is loaded. /// </summary> dynamic np = null; /// <summary> /// The reference to the scipy package if it is loaded. /// </summary> dynamic scipy = null; /// <summary> /// The reference to the sys package. /// </summary> dynamic sys = null; /// <summary> /// Signals if the numpy package is loaded. /// </summary> public bool NumpyLoaded = false; /// <summary> /// Signals if the scipy package is loaded. /// </summary> public bool ScipyLoaded = false; public bool MatplotlibLoaded = false; /// <summary> /// Initializes a python connection. If you leave the parameter empty you have to set the environment /// variable WINPYTHON. /// </summary> /// <param name="path">The location of the python home folder of a full python distribution (including numpy, maptlotlib, etc.)</param> public PythonConnection(string path="") { if (!PythonEngine.IsInitialized) { if (path == "") { PythonHome = System.Environment.GetEnvironmentVariable("WINPYTHON", System.EnvironmentVariableTarget.Machine); if (PythonHome == null) PythonHome = System.Environment.GetEnvironmentVariable("WINPYTHON", System.EnvironmentVariableTarget.User); if (PythonHome == null) PythonHome = System.Environment.GetEnvironmentVariable("WINPYTHON", System.EnvironmentVariableTarget.Process); if (PythonHome == null) PythonHome = ""; } else { PythonHome = path; } BuildPythonpath(false); System.Environment.SetEnvironmentVariable("PYTHONPATH", string.Join(";", Pythonpath)); PythonEngine.PythonHome = PythonHome; PythonEngine.ProgramName = "PythonRuntime"; PythonEngine.Initialize(); PythonEngine.BeginAllowThreads(); } using (Py.GIL()) { if (Main == null) Main = Py.Import("__main__"); Globals.Push(new PyDict(Main.GetAttr("__dict__"))); Locals.Push(new PyDict()); sys = Import("sys"); Import("copy"); Import("io"); Import("gc"); } } //~PythonConnection() { // PythonEngine.Shutdown(); //} public PyDict Local { get { return Locals.Peek(); } } private PyDict Global { get { return Globals.Peek(); } } /// <summary> /// Allows to set variables in the python context. Allowed types are simple types (int, double, string, etc.) and /// arrays of those types. /// Numpy must be loaded before trying to pass arrays! /// /// TODO: Implement Marshal.Copy for input of arrays... /// </summary> /// <param name="name"></param> /// <param name="content"></param> public void Set(string name, dynamic content) { using (Py.GIL()) { System.Type ValueType = content.GetType(); // Arrays are always converted to python lists if (ValueType.IsArray) { if (!NumpyLoaded) { throw new PythonException("numpy must be loaded for array conversion."); } // BlockCopy possibly multidimensional array of arbitrary type to onedimensional byte array System.Type ElementType = ValueType.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 dynamic dtype; if (ElementType == typeof(int)) dtype = np.int32; else if (ElementType == typeof(double)) dtype = np.float64; else throw new System.Exception("Datatype not supported!"); dynamic pydata = np.empty(shape, dtype); // Copy the data to that array System.IntPtr ptr = (System.IntPtr)PyInt.AsInt(pydata.__array_interface__["data"][0]).ToInt32(); Marshal.Copy(data, 0, ptr, nbytes); // Push the variable to local dictionary UpdateLocals(name, pydata); } else { if (ValueType == typeof(int)) UpdateLocals(name, new PyInt(content)); else if (ValueType == typeof(string)) UpdateLocals(name, new PyString(content)); else if (ValueType == typeof(double)) UpdateLocals(name, new PyFloat(content)); else if (ValueType == typeof(bool)) { UpdateLocals(name, new PyInt(System.Convert.ToInt32(content))); RunStringTry(name + " = (" + name + " == 1)"); } else throw new PythonException("Datatype not supported!"); } } } public dynamic Get(string name_in) { using (Py.GIL()) { dynamic result; string name = name_in; RunString("__" + name + "__type = str(type(" + name + "))"); string type = (string)Local.GetItem("__" + name + "__type").AsManagedObject(typeof(string)); if (type == "<class 'numpy.ndarray'>") { // If the array is not contiguous in memory, copy it first. // This overwrites the array (but obviously the contents stay the same). RunString( "if (" + name + ".__array_interface__['strides'] is not None):\n" + " " + name + " = copy.deepcopy(" + name + ")" ); RunString("__" + name + "__ptr = " + name + ".__array_interface__['data'][0]"); RunString("__" + name + "__nbytes = " + name + ".nbytes"); RunString("__" + name + "__shape = " + name + ".shape"); RunString("__" + name + "__dtype = str(" + name + ".dtype)"); System.IntPtr ptr = (System.IntPtr)PyInt.AsInt(Local.GetItem("__" + name + "__ptr")).ToInt32(); int nbytes = PyInt.AsInt(Local.GetItem("__" + name + "__nbytes")).ToInt32(); string dtype = (string)Local.GetItem("__" + name + "__dtype").AsManagedObject(typeof(string)); long[] shape = (long[])Local.GetItem("__" + name + "__shape").AsManagedObject(typeof(long[])); byte[] data = new byte[nbytes]; Marshal.Copy(ptr, data, 0, nbytes); System.Type result_type; if (dtype == "float64") result_type = typeof(double); else if (dtype == "int32") result_type = typeof(int); else if (dtype == "int64") result_type = typeof(long); else if (dtype == "uint8") result_type = typeof(byte); else throw new PythonException("type not supported!"); result = System.Array.CreateInstance(result_type, shape); System.Buffer.BlockCopy(data, 0, result, 0, nbytes); RunString("del __" + name + "__ptr"); RunString("del __" + name + "__nbytes"); RunString("del __" + name + "__dtype"); RunString("del __" + name + "__shape"); } else { System.Type result_type; if (type == "<class 'float'>") result_type = typeof(double); else if (type == "<class 'str'>") result_type = typeof(string); else if (type == "<class 'int'>") result_type = typeof(long); // on a 64bit-system just "int" does not work! else throw new PythonException("type not supported!"); result = Local.GetItem(name).AsManagedObject(result_type); } RunString("del __" + name + "__type"); return result; } } private PyList ArrayToPyList(dynamic arr) { using (Py.GIL()) { System.Type NestedType = arr.GetType().GetElementType(); PyList res = new PyList(); if (NestedType == typeof(int)) { foreach (int d in arr) { res.Append(d.ToPython()); } } else if (NestedType == typeof(double)) { foreach (double d in arr) { res.Append(d.ToPython()); } } else { throw new PythonException("Nested Datatype not supported!"); } return res; } } public void ClearLocals() { using (Py.GIL()) Local.Clear(); } public string[] GetLocalNames() { using (Py.GIL()) { List<string> keys = new List<string>(); foreach (PyObject k in Local.Keys()) { keys.Add((string)k.AsManagedObject(typeof(string))); } return keys.ToArray(); } } private void UpdateLocals(string name, dynamic item) { using (Py.GIL()) { PyDict update = new PyDict(); update.SetItem(name, item); Local.Update(update); } } public void RunString(string code) { using (Py.GIL()) { PyObject ret = PythonEngine.RunString(code, Global.Handle, Local.Handle); if (ret == null) { throw new PythonException("The following code produced an exception: " + code); } } } public bool RunStringTry(string code) { using (Py.GIL()) { string try_code = "resultstr = ''\n" + "try:\n " + code.Replace("\n", "\n ") + "\nexcept BaseException as e:\n" + " resultstr = str(e)\n"; PythonEngine.RunString(try_code, Global.Handle, Local.Handle); string result = Get("resultstr"); if (result != "") { MessageBox.Show("A python exception occured: " + result); return false; } return true; } } public dynamic Import(string module) { return Import(module, module); } public dynamic Import(string module, string alias) { dynamic res; using (Py.GIL()) { res = PythonEngine.ImportModule(module); if (res == null) { throw new FileNotFoundException("Module '" + module + "' could not be imported."); } Global.SetItem(alias, res); } return res; } public void ImportNumpy() { np = Import("numpy", "np"); NumpyLoaded = true; } public void ImportScipy() { scipy = Import("scipy"); ScipyLoaded = true; } public void ImportScipyStack() { ImportNumpy(); ImportScipy(); } public void ImportMatplotlib() { // Import matplotlib and set the renderer non-interactive Import("matplotlib"); RunString("matplotlib.use('agg')"); Import("matplotlib.pyplot", "plt"); } public void AddRelativePath(string path) { string CurrentDirectory = Directory.GetCurrentDirectory(); string NewPath = Path.GetFullPath(Path.Combine(CurrentDirectory, path)); AddPath(NewPath); } public void AddPath(string path) { if (!Directory.Exists(path)) { throw new System.ArgumentException("Import path does not exist: " + path); } AdditionalPaths.Add(path); BuildPythonpath(); } public void NewEnvironment() { using (Py.GIL()) Locals.Push(new PyDict()); } public void PushLocal(PyDict local) { using (Py.GIL()) Locals.Push(local); } public PyDict PopLocal() { using (Py.GIL()) return Locals.Pop(); } public void ClearPath() { AdditionalPaths.Clear(); BuildPythonpath(); } private void BuildPythonpath(bool AddToSys = true) { Pythonpath = new List<string>(); Pythonpath.Add(Path.GetFullPath(Path.Combine(PythonHome, "DLLs"))); Pythonpath.Add(Path.GetFullPath(Path.Combine(PythonHome, "Lib"))); Pythonpath.Add(Path.GetFullPath(Path.Combine(PythonHome, "Lib", "site-packages"))); foreach (string p in AdditionalPaths) { Pythonpath.Add(p); } if (AddToSys) { using (Py.GIL()) { PyObject NewPath = Pythonpath.ToPython(); sys.path = NewPath; } } } } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |