Logo ROOT  
Reference Guide
RDataFramePyz.cxx
Go to the documentation of this file.
1// Author: Stefan Wunsch CERN 04/2019
2// Original PyROOT code by Wim Lavrijsen, LBL
3
4/*************************************************************************
5 * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "CPyCppyy.h"
13#include "CPPInstance.h"
14#include "ProxyWrappers.h"
15#include "PyROOTPythonize.h"
16#include "ROOT/RConfig.hxx"
17#include "TInterpreter.h"
18#include "CPyCppyy/API.h"
19
20#include <utility> // std::pair
21#include <sstream> // std::stringstream
22
23////////////////////////////////////////////////////////////////////////////
24/// \brief Make an RDataFrame from a dictionary of numpy arrays
25/// \param[in] self Always null, since this is a module function.
26/// \param[in] pydata Dictionary with numpy arrays
27///
28/// This function takes a dictionary of numpy arrays and creates an RDataFrame
29/// using the keys as column names and the numpy arrays as data.
31{
32 if (!pydata) {
33 PyErr_SetString(PyExc_RuntimeError, "Object not convertible: Invalid Python object.");
34 return NULL;
35 }
36
37 if (!PyDict_Check(pydata)) {
38 PyErr_SetString(PyExc_RuntimeError, "Object not convertible: Python object is not a dictionary.");
39 return NULL;
40 }
41
42 if (PyDict_Size(pydata) == 0) {
43 PyErr_SetString(PyExc_RuntimeError, "Object not convertible: Dictionary is empty.");
44 return NULL;
45 }
46
47
48 // Add PyObject (dictionary) holding RVecs to data source
49 std::stringstream code;
50 code << "ROOT::Internal::RDF::MakeNumpyDataFrame(";
51 std::stringstream pyaddress;
52 auto pyvecs = PyDict_New();
53#ifdef _MSC_VER
54 pyaddress << "0x";
55#endif
56 pyaddress << pyvecs;
57 code << "reinterpret_cast<PyObject*>(" << pyaddress.str() << "), ";
58
59 // Iterate over dictionary, convert numpy arrays to RVecs and put together interpreter code
60 PyObject *key, *value;
61 Py_ssize_t pos = 0;
62 const auto size = PyObject_Size(pydata);
63 auto counter = 0u;
64 while (PyDict_Next(pydata, &pos, &key, &value)) {
65 // Get name of key
66 if (!CPyCppyy_PyText_Check(key)) {
67 PyErr_SetString(PyExc_RuntimeError, "Object not convertible: Dictionary key is not convertible to a string.");
68 return NULL;
69 }
70 std::string keystr = CPyCppyy_PyText_AsString(key);
71
72 // Convert value to RVec and attach to dictionary
73 auto pyvec = PyROOT::AsRVec(NULL, value);
74 if (pyvec == NULL) {
75 PyErr_SetString(PyExc_RuntimeError,
76 ("Object not convertible: Dictionary entry " + keystr + " is not convertible with AsRVec.").c_str());
77 return NULL;
78 }
79 PyDict_SetItem(pyvecs, key, pyvec);
80 Py_DECREF(pyvec);
81
82 // Add pairs of column name and associated RVec to signature
83 std::string vectype = Cppyy::GetScopedFinalName(((CPyCppyy::CPPInstance*)pyvec)->ObjectIsA());
84 std::stringstream vecaddress;
85#ifdef _MSC_VER
86 vecaddress << "0x";
87#endif
88 vecaddress << ((CPyCppyy::CPPInstance*)pyvec)->GetObject();
89 code << "std::pair<std::string, " << vectype << "*>(\"" + keystr
90 << "\", reinterpret_cast<" << vectype+ "*>(" << vecaddress.str() << "))";
91 if (counter != size - 1) {
92 code << ",";
93 } else {
94 code << ");";
95 }
96 counter++;
97 }
98
99 // Create RDataFrame and build Python proxy
100 const auto err = gInterpreter->Declare("#include \"ROOT/RNumpyDS.hxx\"");
101 if (!err) {
102 PyErr_SetString(PyExc_RuntimeError, "Failed to find \"ROOT/RNumpyDS.hxx\".");
103 return NULL;
104 }
105 const auto codeStr = code.str();
106 auto address = (void*) gInterpreter->Calc(codeStr.c_str());
107 const auto pythonOwns = true;
108 auto pyobj = CPyCppyy::Instance_FromVoidPtr(address, "ROOT::RDataFrame", pythonOwns);
109
110 // Bind pyobject holding adopted memory to the RVec
111 if (PyObject_SetAttrString(pyobj, "__data__", pyvecs)) {
112 PyErr_SetString(PyExc_RuntimeError, "Object not convertible: Failed to set dictionary as attribute __data__.");
113 return NULL;
114 }
115 Py_DECREF(pyvecs);
116
117 return pyobj;
118}
#define CPyCppyy_PyText_AsString
Definition: CPyCppyy.h:97
#define CPyCppyy_PyText_Check
Definition: CPyCppyy.h:95
_object PyObject
Definition: PyMethodBase.h:43
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
#define gInterpreter
Definition: TInterpreter.h:564
CPYCPPYY_EXTERN PyObject * Instance_FromVoidPtr(void *addr, const std::string &classname, bool python_owns=false)
Definition: API.cxx:109
RPY_EXPORTED std::string GetScopedFinalName(TCppType_t type)
PyObject * MakeNumpyDataFrameImpl(PyObject *self, PyObject *obj)
Make an RDataFrame from a dictionary of numpy arrays.
PyObject * AsRVec(PyObject *self, PyObject *obj)
Adopt memory of a Python object with array interface using an RVec.
Definition: RVecPyz.cxx:29