{
"cells": [
{
"cell_type": "markdown",
"id": "f2f56541",
"metadata": {},
"source": [
"# df026_AsNumpyArrays\n",
"Read data from RDataFrame into Numpy arrays.\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Stefan Wunsch (KIT, CERN) \n",
"This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, March 19, 2024 at 07:07 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "1dacac94",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:07:14.763780Z",
"iopub.status.busy": "2024-03-19T19:07:14.763371Z",
"iopub.status.idle": "2024-03-19T19:07:17.682746Z",
"shell.execute_reply": "2024-03-19T19:07:17.673906Z"
}
},
"outputs": [],
"source": [
"import ROOT\n",
"from sys import exit"
]
},
{
"cell_type": "markdown",
"id": "924e5ed7",
"metadata": {},
"source": [
"Let's create a simple dataframe with ten rows and two columns"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "cdbc4bc0",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:07:17.719885Z",
"iopub.status.busy": "2024-03-19T19:07:17.719490Z",
"iopub.status.idle": "2024-03-19T19:07:18.666753Z",
"shell.execute_reply": "2024-03-19T19:07:18.665642Z"
}
},
"outputs": [],
"source": [
"df = ROOT.RDataFrame(10) \\\n",
" .Define(\"x\", \"(int)rdfentry_\") \\\n",
" .Define(\"y\", \"1.f/(1.f+rdfentry_)\")"
]
},
{
"cell_type": "markdown",
"id": "d2b829ae",
"metadata": {},
"source": [
"Next, we want to access the data from Python as Numpy arrays. To do so, the\n",
"content of the dataframe is converted using the AsNumpy method. The returned\n",
"object is a dictionary with the column names as keys and 1D numpy arrays with\n",
"the content as values."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5fdf2f01",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:07:18.671636Z",
"iopub.status.busy": "2024-03-19T19:07:18.671288Z",
"iopub.status.idle": "2024-03-19T19:07:25.636879Z",
"shell.execute_reply": "2024-03-19T19:07:25.633880Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Read-out of the full RDataFrame:\n",
"{'x': ndarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32), 'y': ndarray([1. , 0.5 , 0.33333334, 0.25 , 0.2 ,\n",
" 0.16666667, 0.14285715, 0.125 , 0.11111111, 0.1 ],\n",
" dtype=float32)}\n",
"\n"
]
}
],
"source": [
"npy = df.AsNumpy()\n",
"print(\"Read-out of the full RDataFrame:\\n{}\\n\".format(npy))"
]
},
{
"cell_type": "markdown",
"id": "8208f82c",
"metadata": {},
"source": [
"Since reading out data to memory is expensive, always try to read-out only what\n",
"is needed for your analysis. You can use all RDataFrame features to reduce your\n",
"dataset, e.g., the Filter transformation. Furthermore, you can can pass to the\n",
"AsNumpy method a whitelist of column names with the option `columns` or a blacklist\n",
"with column names with the option `exclude`."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "6d178f9b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:07:25.643070Z",
"iopub.status.busy": "2024-03-19T19:07:25.642501Z",
"iopub.status.idle": "2024-03-19T19:07:29.895419Z",
"shell.execute_reply": "2024-03-19T19:07:29.882226Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Read-out of the filtered RDataFrame:\n",
"{'x': ndarray([6, 7, 8, 9], dtype=int32), 'y': ndarray([0.14285715, 0.125 , 0.11111111, 0.1 ], dtype=float32)}\n",
"\n",
"Read-out of the filtered RDataFrame with the columns option:\n",
"{'x': ndarray([6, 7, 8, 9], dtype=int32)}\n",
"\n",
"Read-out of the filtered RDataFrame with the exclude option:\n",
"{'y': ndarray([0.14285715, 0.125 , 0.11111111, 0.1 ], dtype=float32)}\n",
"\n"
]
}
],
"source": [
"df2 = df.Filter(\"x>5\")\n",
"npy2 = df2.AsNumpy()\n",
"print(\"Read-out of the filtered RDataFrame:\\n{}\\n\".format(npy2))\n",
"\n",
"npy3 = df2.AsNumpy(columns=[\"x\"])\n",
"print(\"Read-out of the filtered RDataFrame with the columns option:\\n{}\\n\".format(npy3))\n",
"\n",
"npy4 = df2.AsNumpy(exclude=[\"x\"])\n",
"print(\"Read-out of the filtered RDataFrame with the exclude option:\\n{}\\n\".format(npy4))"
]
},
{
"cell_type": "markdown",
"id": "a5004dae",
"metadata": {},
"source": [
"You can read-out all objects from ROOT files since these are wrapped by PyROOT\n",
"in the Python world. However, be aware that objects other than fundamental types,\n",
"such as complex C++ objects and not int or float, are costly to read-out."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "17e97d58",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:07:29.915212Z",
"iopub.status.busy": "2024-03-19T19:07:29.914795Z",
"iopub.status.idle": "2024-03-19T19:07:32.900785Z",
"shell.execute_reply": "2024-03-19T19:07:32.899518Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Read-out of C++ objects:\n",
"[\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" ]\n",
"\n",
"Access to all methods and data members of the C++ object:\n",
"Object: \n",
"Access data member: custom_object.x = 42\n",
"\n"
]
}
],
"source": [
"ROOT.gInterpreter.Declare(\"\"\"\n",
"// Inject the C++ class CustomObject in the C++ runtime.\n",
"class CustomObject {\n",
"public:\n",
" int x = 42;\n",
"};\n",
"// Create a function that returns such an object. This is called to fill the dataframe.\n",
"CustomObject fill_object() { return CustomObject(); }\n",
"\"\"\")\n",
"\n",
"df3 = df.Define(\"custom_object\", \"fill_object()\")\n",
"npy5 = df3.AsNumpy()\n",
"print(\"Read-out of C++ objects:\\n{}\\n\".format(npy5[\"custom_object\"]))\n",
"print(\"Access to all methods and data members of the C++ object:\\nObject: {}\\nAccess data member: custom_object.x = {}\\n\".format(\n",
" repr(npy5[\"custom_object\"][0]), npy5[\"custom_object\"][0].x))"
]
},
{
"cell_type": "markdown",
"id": "1211951a",
"metadata": {},
"source": [
"Note that you can pass the object returned by AsNumpy directly to pandas.DataFrame\n",
"including any complex C++ object that may be read-out."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f3634278",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:07:32.906134Z",
"iopub.status.busy": "2024-03-19T19:07:32.905773Z",
"iopub.status.idle": "2024-03-19T19:07:33.789564Z",
"shell.execute_reply": "2024-03-19T19:07:33.782933Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Content of the ROOT.RDataFrame as pandas.DataFrame:\n",
" custom_object x y\n",
"0 0 1.000000\n",
"1 1 0.500000\n",
"2 2 0.333333\n",
"3 3 0.250000\n",
"4 4 0.200000\n",
"5 5 0.166667\n",
"6 6 0.142857\n",
"7 7 0.125000\n",
"8 8 0.111111\n",
"9 9 0.100000\n",
"\n"
]
}
],
"source": [
"try:\n",
" import pandas\n",
"except:\n",
" print(\"Please install the pandas package to run this section of the tutorial.\")\n",
" exit(1)\n",
"\n",
"df = pandas.DataFrame(npy5)\n",
"print(\"Content of the ROOT.RDataFrame as pandas.DataFrame:\\n{}\\n\".format(df))"
]
},
{
"cell_type": "markdown",
"id": "e1e7a2c5",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "71b45331",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:07:33.799432Z",
"iopub.status.busy": "2024-03-19T19:07:33.794237Z",
"iopub.status.idle": "2024-03-19T19:07:33.939657Z",
"shell.execute_reply": "2024-03-19T19:07:33.932202Z"
}
},
"outputs": [],
"source": [
"from ROOT import gROOT \n",
"gROOT.GetListOfCanvases().Draw()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}