public static CollisionObject FromPyCollisionObject(PyObject obj)
        {
            var frame      = (string)PyConvert.ToClrObject(obj.GetAttr("frame_id"), typeof(string));
            var primitives = (List <CollisionPrimitive>)PyConvert.ToClrObject(obj.GetAttr("primitives"), typeof(List <CollisionPrimitive>));

            return(new CollisionObject(frame, primitives));
        }
        public static JointPath FromPyJointPath(PyObject obj)
        {
            var jointSet = (JointSet)PyConvert.ToClrObject(obj.GetAttr("joint_set"), typeof(JointSet));
            var points   = (List <JointValues>)PyConvert.ToClrObject(obj.GetAttr("points"), typeof(List <JointValues>));

            return(new JointPath(jointSet, points));
        }
        public static JointValues FromPyJointValues(PyObject obj)
        {
            var jointSet = FromPyJointSet(obj.GetAttr("joint_set"));
            var values   = (A <double>)PyConvert.ToClrObject(obj.GetAttr("values"), typeof(A <double>));

            return(new JointValues(jointSet, values));
        }
Example #4
0
        public static HttpResponseLite GetFromPythonNET(string url)
        {
            if (!PythonEngine.IsInitialized)
            {
                PythonEngine.Initialize();
            }

            var sw     = Stopwatch.StartNew();
            var result = new HttpResponseLite();

            try
            {
                // https://github.com/pythonnet/pythonnet/wiki/Threading
                var mThreadState = PythonEngine.BeginAllowThreads();
                using (Py.GIL())
                {
                    dynamic  cfscrape = Py.Import("cfscrape");
                    dynamic  scraper  = cfscrape.create_scraper();
                    PyObject response = scraper.get(url);
                    result.StatusCode = (HttpStatusCode)response.GetAttr("status_code").As <int>();
                    result.Html       = response.GetAttr("text").As <string>();
                    result.ElapsedMs  = sw.ElapsedMilliseconds;
                }

                PythonEngine.EndAllowThreads(mThreadState);
            }
            catch (Exception e)
            {
                result.ElapsedMs = sw.ElapsedMilliseconds;
                result.Exception = e;
            }

            return(result);
        }
        public static JointStates FromPyJointStates(PyObject obj)
        {
            var positions  = (JointValues)PyConvert.ToClrObject(obj.GetAttr("positions"), typeof(JointValues));
            var velocities = (JointValues)PyConvert.ToClrObject(obj.GetAttr("velocities"), typeof(JointValues));
            var efforts    = (JointValues)PyConvert.ToClrObject(obj.GetAttr("efforts"), typeof(JointValues));

            return(new JointStates(positions, velocities, efforts));
        }
        public static JointTrajectory FromPyJointTrajectory(PyObject obj)
        {
            var jointSet = (JointSet)PyConvert.ToClrObject(obj.GetAttr("joint_set"), typeof(JointSet));
            var points   = (List <JointTrajectoryPoint>)PyConvert.ToClrObject(obj.GetAttr("points"), typeof(List <JointTrajectoryPoint>));
            var valid    = (bool)PyConvert.ToClrObject(obj.GetAttr("is_valid"), typeof(bool));

            return(new JointTrajectory(jointSet, points, valid));
        }
        public static TimeSpan FromPyTimeDelta(PyObject obj)
        {
            var days         = (int)PyConvert.ToClrObject(obj.GetAttr("days"), typeof(int));
            var seconds      = (int)PyConvert.ToClrObject(obj.GetAttr("seconds"), typeof(int));
            var microseconds = (int)PyConvert.ToClrObject(obj.GetAttr("microseconds"), typeof(int));

            return(new TimeSpan((long)days * 864000000000 + (long)seconds * 10000000 + (long)microseconds * 10));
        }
        public static CollisionPrimitive FromPyCollisionPrimitive(PyObject obj)
        {
            var pyKind     = obj.GetAttr("kind");
            var kind       = (CollisionPrimitiveKind)PyConvert.ToClrObject(pyKind.GetAttr("value"), typeof(int));
            var parameters = (A <double>)PyConvert.ToClrObject(obj.GetAttr("parameters"), typeof(A <double>));
            var pose       = (Pose)PyConvert.ToClrObject(obj.GetAttr("pose"), typeof(Pose));

            return(new CollisionPrimitive(kind, parameters.ToArray(), pose));
        }
        public static JointTrajectoryPoint FromPyJoinTrajectoryPoint(PyObject obj)
        {
            var timeFromStart = (TimeSpan)PyConvert.ToClrObject(obj.GetAttr("time_from_start"), typeof(TimeSpan));
            var positions     = (JointValues)PyConvert.ToClrObject(obj.GetAttr("positions"), typeof(JointValues));
            var velocities    = (JointValues)PyConvert.ToClrObject(obj.GetAttr("velocities"), typeof(JointValues));
            var accelerations = (JointValues)PyConvert.ToClrObject(obj.GetAttr("accelerations"), typeof(JointValues));
            var efforts       = (JointValues)PyConvert.ToClrObject(obj.GetAttr("efforts"), typeof(JointValues));

            return(new JointTrajectoryPoint(timeFromStart, positions, velocities, accelerations, efforts));
        }
        public static Pose FromPyPose(PyObject obj)
        {
            var t = (A <double>)PyConvert.ToClrObject(obj.GetAttr("translation"), typeof(A <double>));
            var translationVector = new Vector3((float)t[0], (float)t[1], (float)t[2]);
            var pyQuaternion      = obj.GetAttr("quaternion");
            var q          = (A <double>)PyConvert.ToClrObject(pyQuaternion.GetAttr("elements"), typeof(A <double>));
            var quaternion = new Quaternion((float)q[1], (float)q[2], (float)q[3], (float)q[0]);
            var frame      = (string)PyConvert.ToClrObject(obj.GetAttr("frame_id"), typeof(string));

            return(new Pose(translationVector, quaternion, frame));
        }
Example #11
0
 static PythonExtension()
 {
     using (Py.GIL())
     {
         PyObject module = Py.Import("builtins");
         PyNone      = module.GetAttr("None");
         PyTrue      = module.GetAttr("True");
         PyBoolType  = PyTrue.GetPythonType();
         PySliceType = module.GetAttr("slice");
     }
 }
Example #12
0
        private void ConfigureModules()
        {
            PyObject sequentialModule = PythonEngine.ModuleFromString("sequential", "from keras.models import Sequential");
            PyObject layersModule     = PythonEngine.ModuleFromString("layers", "from keras.layers import Dense, Dropout");
            dynamic  Sequential       = sequentialModule.GetAttr("Sequential");

            this.keras      = Py.Import("keras");
            this.dropout    = layersModule.GetAttr("Dropout");
            this.denseLayer = layersModule.GetAttr("Dense");
            this.model      = Sequential();
        }
Example #13
0
        private static object DecodeTime(PyObject py)
        {
            var result = new TimeSpan(
                py.GetAttr("hour").As <int>(),
                py.GetAttr("minute").As <int>(),
                py.GetAttr("second").As <int>());

            return(TimeSpan.FromTicks(
                       result.Ticks +
                       py.GetAttr("microsecond").As <int>() * 10));
        }
Example #14
0
        /// <summary>
        /// Get the indicator Name. If not defined, use the class name
        /// </summary>
        /// <param name="indicator">The python implementation of <see cref="IndicatorBase{IBaseDataBar}"/></param>
        /// <returns>The indicator Name.</returns>
        private static string GetIndicatorName(PyObject indicator)
        {
            using (Py.GIL())
            {
                var name = indicator.HasAttr("Name")
                    ? indicator.GetAttr("Name")
                    : indicator.GetAttr("__class__").GetAttr("__name__");

                return(name.GetAndDispose <string>());
            }
        }
Example #15
0
 public void Load()
 {
     if (Lock == null)
     {
         return;
     }
     using (Py.GIL())
     {
         Plugin = PythonEngine.ImportModule(File);
         Plugin.InvokeMethod("Load");
         Name        = Plugin.GetAttr("Name").As <string>();
         Explanation = Plugin.GetAttr("Explanation").As <string>();
     }
 }
        /// <summary>
        /// <see cref = "AlgorithmPythonWrapper"/> constructor.
        /// Creates and wraps the algorithm written in python.
        /// </summary>
        /// <param name="module">Python module with the algorithm written in Python</param>
        public AlgorithmPythonWrapper(PyObject module)
        {
            _algorithm = null;

            try
            {
                using (Py.GIL())
                {
                    if (!module.HasAttr("QCAlgorithm"))
                    {
                        return;
                    }

                    var baseClass = module.GetAttr("QCAlgorithm");

                    // Load module with util methods
                    _util = ImportUtil();

                    var moduleName = module.Repr().Split('\'')[1];

                    foreach (var name in module.Dir())
                    {
                        var attr = module.GetAttr(name.ToString());

                        if (attr.IsSubclass(baseClass) && attr.Repr().Contains(moduleName))
                        {
                            attr.SetAttr("OnPythonData", _util.GetAttr("OnPythonData"));

                            _algorithm = attr.Invoke();

                            // QCAlgorithm reference for LEAN internal C# calls (without going from C# to Python and back)
                            _baseAlgorithm = (QCAlgorithm)_algorithm;

                            // write events such that when the base handles an event it
                            // will also invoke event handlers defined on this instance
                            _baseAlgorithm.AlphasGenerated += AlphasGenerated;

                            // Set pandas
                            _baseAlgorithm.SetPandasConverter();

                            return;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logging.Log.Error(e);
            }
        }
Example #17
0
        private static object DecodeDateTime(PyObject py)
        {
            var utcDelta = TimeSpan.Zero;
            var kind     = DateTimeKind.Local;

            var tz = py.GetAttr("tzinfo");

            if (tz.HasAttr("utcoffset"))
            {
                kind     = DateTimeKind.Utc;
                utcDelta = py.InvokeMethod("utcoffset")
                           .DecodeTimeDelta();
            }

            var result = new DateTime(
                py.GetAttr("year").As <int>(),
                py.GetAttr("month").As <int>(),
                py.GetAttr("day").As <int>(),
                py.GetAttr("hour").As <int>(),
                py.GetAttr("minute").As <int>(),
                py.GetAttr("second").As <int>(),
                kind);

            return(result.AddTicks(-utcDelta.Ticks + py.GetAttr("microsecond").As <int>() * 10)
                   .ToLocalTime());
        }
Example #18
0
        private void ConfigureModules()
        {
            PyObject SequentialModule = PythonEngine.ModuleFromString("sequential", "from keras.models import Sequential");
            PyObject layersModule     = PythonEngine.ModuleFromString("standardLayers", "from keras.layers import Dense, Dropout, Flatten");
            PyObject layersCnnModule  = PythonEngine.ModuleFromString("conEvoLayers", "from keras.layers import Conv2D, MaxPooling2D");

            dynamic sequential = SequentialModule.GetAttr("Sequential");

            this.model                = sequential();
            this.denseLayer           = layersModule.GetAttr("Dense");
            this.dropoutLayer         = layersModule.GetAttr("Dropout");
            this.flattenLayer         = layersModule.GetAttr("Flatten");
            this.convolutional2DLayer = layersCnnModule.GetAttr("Conv2D");
            this.maxPooling2DLayer    = layersCnnModule.GetAttr("MaxPooling2D");
        }
Example #19
0
        /// <summary>
        /// Sets the python implementation of the indicator
        /// </summary>
        /// <param name="indicator">The python implementation of <see cref="IndicatorBase{IBaseDataBar}"/></param>
        public void SetIndicator(PyObject indicator)
        {
            using (Py.GIL())
            {
                foreach (var attributeName in new[] { "IsReady", "Update", "Value" })
                {
                    if (!indicator.HasAttr(attributeName))
                    {
                        var name = indicator.GetAttr("__class__").GetAttr("__name__");

                        var message = $"Indicator.{attributeName} must be implemented. " +
                                      $"Please implement this missing method in {name}";

                        if (attributeName == "IsReady")
                        {
                            message += " or use PythonIndicator as base:" +
                                       $"{Environment.NewLine}class {name}(PythonIndicator):";
                        }

                        throw new NotImplementedException(message);
                    }
                }
            }

            _indicator = indicator;
        }
Example #20
0
            public bool TryDecode <T>(PyObject pyObj, out T value)
            {
                var message = pyObj.GetAttr("args")[0].As <string>();

                value = (T)(object)new ValueErrorWrapper(message);
                return(true);
            }
Example #21
0
        public static PyObject _load_clr_module(PyObject spec)
        {
            using var modname = spec.GetAttr("name");
            string name = modname.As <string?>() ?? throw new ArgumentException("name must not be None");
            var    mod  = ImportHook.Import(name);

            return(mod);
        }
Example #22
0
        private PyList GetSysPaths()
        {
            // set sys paths
            PyObject sys         = PythonEngine.ImportModule("sys");
            PyObject sysPathsObj = sys.GetAttr("path");

            return(PyList.AsList(sysPathsObj));
        }
Example #23
0
 /// <summary>
 /// This method calls back into the CPython runtime - tests
 /// call this from Python to check that we don't hang on
 /// nested transitions from managed to Python code and back.
 /// </summary>
 public static string CallEchoString(string arg)
 {
     using (Py.GIL())
     {
         if (module == null)
         {
             module = PyModule.FromString("tt", testmod);
         }
         PyObject func   = module.GetAttr("echostring");
         var      parg   = new PyString(arg);
         PyObject temp   = func.Invoke(parg);
         var      result = (string)temp.AsManagedObject(typeof(string));
         func.Dispose();
         parg.Dispose();
         temp.Dispose();
         return(result);
     }
 }
Example #24
0
        public static IEnumerable <PyType> GetDecorators(PyObject func)
        {
            while (func.HasAttr("__func__"))
            {
                yield return(func.GetPythonType() as PyType);

                func = func.GetAttr("__func__");
            }
        }
Example #25
0
        /// <summary>
        /// <see cref = "AlgorithmPythonWrapper"/> constructor.
        /// Creates and wraps the algorithm written in python.
        /// </summary>
        /// <param name="module">Python module with the algorithm written in Python</param>
        public AlgorithmPythonWrapper(PyObject module)
        {
            _algorithm = null;

            try
            {
                using (Py.GIL())
                {
                    if (!module.HasAttr("QCAlgorithm"))
                    {
                        return;
                    }

                    var baseClass = module.GetAttr("QCAlgorithm");

                    // Load module with util methods
                    var onPythonData = Py.Import("AlgorithmPythonUtil").GetAttr("OnPythonData");

                    var moduleName = module.Repr().Split('\'')[1];

                    foreach (var name in module.Dir())
                    {
                        var attr = module.GetAttr(name.ToString());

                        if (attr.IsSubclass(baseClass) && attr.Repr().Contains(moduleName))
                        {
                            attr.SetAttr("OnPythonData", onPythonData);

                            _pyAlgorithm = attr.Invoke();
                            _algorithm   = Impromptu.ActLike <IAlgorithm>(_pyAlgorithm);

                            // QCAlgorithm reference for LEAN internal C# calls (without going from C# to Python and back)
                            _baseAlgorithm = _algorithm.UndoActLike();

                            return;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logging.Log.Error(e);
            }
        }
Example #26
0
        // This method calls back into the CPython runtime - tests
        // call this from Python to check that we don't hang on
        // nested transitions from managed to Python code and back.

        public static string CallEchoString(string arg)
        {
            IntPtr gs = PythonEngine.AcquireLock();

            if (module == null)
            {
                module = PythonEngine.ModuleFromString("tt", testmod);
            }
            PyObject func   = module.GetAttr("echostring");
            PyString parg   = new PyString(arg);
            PyObject temp   = func.Invoke(parg);
            string   result = (string)temp.AsManagedObject(typeof(String));

            func.Dispose();
            parg.Dispose();
            temp.Dispose();
            PythonEngine.ReleaseLock(gs);
            return(result);
        }
Example #27
0
        // from https://stackoverflow.com/questions/4601373/better-way-to-shuffle-two-numpy-arrays-in-unison
        static void Shuffle <T1, T2>(ndarray <T1> array1, ndarray <T2> array2)
        {
            using var _ = Py.GIL();
            var random      = numPy.GetAttr("random");
            var randomState = random.InvokeMethod("get_state");

            random.InvokeMethod("shuffle", array1.PythonObject);
            random.InvokeMethod("set_state", randomState);
            random.InvokeMethod("shuffle", array2.PythonObject);
        }
Example #28
0
 /// <summary>
 /// Returns the type nam eof a Python object as a string.
 /// </summary>
 /// <param name="handle">Handle to the Pyhton object.</param>
 /// <returns>Name of the Python object.</returns>
 private static string GetPythonTypeName(IntPtr handle)
 {
     using (var excType = new PyObject(handle))
     {
         using (var typeName = excType.GetAttr("__name__"))
         {
             return(typeName.ToString());
         }
     }
 }
Example #29
0
        public static PySignature GetSignature(PyScope scope, string functionName)
        {
            var s = new PySignature();

            PyObject fn = scope.Get(functionName);

            var inspect    = Py.Import("inspect");
            var signature  = inspect.InvokeMethod("signature", fn);
            var parameters = signature.GetAttr("parameters");

            var keys          = parameters.InvokeMethod("keys");
            var argumentNames = new List <string>();

            foreach (var key in keys.OfType <PyObject>())
            {
                var p    = parameters[key];
                var name = p.GetAttr("name").As <string>();
                argumentNames.Add(name);
            }

            PyDict annotations = new PyDict(fn.GetAttr("__annotations__"));

            foreach (var argumentName in argumentNames)
            {
                Type type;
                if (annotations.HasKey(argumentName))
                {
                    var pyType = annotations.GetItem(argumentName);
                    type = ToClrType(pyType);
                }
                else
                {
                    type = typeof(object);
                }
                s.Arguments.Add(new PyArgument {
                    Name = argumentName, Type = type
                });
            }

            Type returnType;

            if (annotations.HasKey("return"))
            {
                var returnPyType = annotations.GetItem("return");
                returnType = ToClrType(returnPyType);
            }
            else
            {
                returnType = typeof(object);
            }

            s.ReturnType = returnType;

            return(s);
        }
Example #30
0
        public void InheritedFromInheritedClassIsSelf()
        {
            using var scope = Py.CreateScope();
            scope.Exec($"from {typeof(Inherited).Namespace} import {nameof(Inherited)}");
            scope.Exec($"class B({nameof(Inherited)}): pass");
            PyObject b              = scope.Eval("B");
            PyObject bInstance      = b.Invoke();
            PyObject bInstanceClass = bInstance.GetAttr("__class__");

            Assert.IsTrue(PythonReferenceComparer.Instance.Equals(b, bInstanceClass));
        }