From 254c9e996ba2e7f973cd8e7acc73ab093002ffb4 Mon Sep 17 00:00:00 2001 From: N!no Date: Thu, 5 Aug 2021 22:40:36 -0400 Subject: [PATCH] Update HistNumbaFill.ipynb --- docs/examples/HistNumbaFill.ipynb | 150 +++++++++++++++++++++--------- 1 file changed, 105 insertions(+), 45 deletions(-) diff --git a/docs/examples/HistNumbaFill.ipynb b/docs/examples/HistNumbaFill.ipynb index d33f08ed..ee4ceffe 100644 --- a/docs/examples/HistNumbaFill.ipynb +++ b/docs/examples/HistNumbaFill.ipynb @@ -16,7 +16,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -25,7 +25,6 @@ "from hist import Hist\n", "from hist import axis\n", "\n", - "# assets\n", "array = np.random.randn(\n", " 10000,\n", ")\n", @@ -34,26 +33,53 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + "\n", + "-3\n", + "\n", + "\n", + "3\n", + "\n", + "\n", + "x-axis\n", + "\n", + "\n", + "\n", + "
\n", + "
\n", + "Regular(100, -3, 3, name='x', label='x-axis')
\n", + "
\n", + "Double() Σ=9973.0 (10000.0 with flow)\n", + "\n", + "
\n", + "
\n", + "" + ], + "text/plain": [ + "Hist(Regular(100, -3, 3, name='x', label='x-axis'), storage=Double()) # Sum: 9973.0 (10000.0 with flow)" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# python fill\n", "h.fill(array)\n", "h" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import hist\n", - "\n", - "isinstance(h.axes[0], hist.axis.Regular)" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -70,7 +96,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -99,8 +125,10 @@ "def type_hist(context):\n", " def typer(axes):\n", " for ax in axes:\n", - " # if not (isinstance(ax, types of axis)):\n", - " return typer\n", + " if not (\n", + " isinstance(ax, hist.axis.Regular)\n", + " ): # ToDo: Aassumed all are Regular axes\n", + " return typer\n", "\n", " return hist_type" ] @@ -114,7 +142,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -135,31 +163,43 @@ "class HistModel(models.StructModel):\n", " def __init__(self, dmm, fe_type):\n", " members = [\n", - " # (\"axes\", types of list of axis),\n", + " (\"bins\", types.int32),\n", + " (\"lo\", types.float64),\n", + " (\"hi\", types.float64),\n", " ]\n", " models.StructModel.__init__(self, dmm, fe_type, members)\n", "\n", "\n", "# expose attributes, porperties and constructors\n", - "make_attribute_wrapper(HistType, \"axes\", \"axes\")\n", + "make_attribute_wrapper(HistType, \"bins\", \"bins\")\n", + "make_attribute_wrapper(HistType, \"lo\", \"lo\")\n", + "make_attribute_wrapper(HistType, \"hi\", \"hi\")\n", "\n", "\n", - "@lower_builtin(Regular, types.Integer, types.Float, types.Float)\n", + "@lower_builtin(Hist, types.Integer, types.Float, types.Float)\n", "def impl_h(context, builder, sig, args):\n", - " axes = args\n", " typ = sig.return_type\n", + " lo, hi, bins = args\n", " h = cgutils.create_struct_proxy(typ)(context, builder)\n", - " h.axes = axes\n", - " return reg._getvalue()\n", + " h.lo = lo\n", + " h.hi = hi\n", + " h.bins = bins\n", + " return h._getvalue()\n", "\n", "\n", "# unbox and box\n", "@unbox(HistType)\n", "def unbox_h(typ, obj, c):\n", - " axes_obj = c.pyapi.object_getattr_string(obj, \"axes\")\n", + " bins_obj = c.pyapi.object_getattr_string(obj, \"bins\")\n", + " lo_obj = c.pyapi.object_getattr_string(obj, \"lo\")\n", + " hi_obj = c.pyapi.object_getattr_string(obj, \"hi\")\n", " h = cgutils.create_struct_proxy(typ)(c.context, c.builder)\n", - " # h.axes = c.pyapi.float_as_double(axes_obj)\n", - " c.pyapi.decref(axes_obj)\n", + " h.bins = c.pyapi.float_as_double(bins_obj)\n", + " h.lo = c.pyapi.float_as_double(lo_obj)\n", + " h.hi = c.pyapi.float_as_double(hi_obj)\n", + " c.pyapi.decref(bins_obj)\n", + " c.pyapi.decref(lo_obj)\n", + " c.pyapi.decref(hi_obj)\n", " is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred())\n", " return NativeValue(h._getvalue(), is_error=is_error)\n", "\n", @@ -167,29 +207,53 @@ "@box(HistType)\n", "def box_h(typ, val, c):\n", " h = cgutils.create_struct_proxy(typ)(c.context, c.builder, value=val)\n", - " axes_obj = c.pyapi.float_from_double(h.axes)\n", + " bins_obj = c.pyapi.float_from_double(h.bins)\n", + " lo_obj = c.pyapi.float_from_double(h.lo)\n", + " hi_obj = c.pyapi.float_from_double(h.hi)\n", " class_obj = c.pyapi.unserialize(c.pyapi.serialize_object(Hist))\n", - " res = c.pyapi.call_function_objargs(class_obj, (axes_obj))\n", - " c.pyapi.decref(axes_obj)\n", + " res = c.pyapi.call_function_objargs(class_obj, (bins_obj, lo_obj, hi_obj))\n", + " c.pyapi.decref(bins_obj)\n", + " c.pyapi.decref(lo_obj)\n", + " c.pyapi.decref(hi_obj)\n", " c.pyapi.decref(class_obj)\n", " return res" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ - "reg_ax = axis.Regular(10, 0, 1)\n", - "\n", - "\n", - "@nb.jit\n", - "def nb_create_Hist():\n", - " Hist(reg_ax)\n", - "\n", + "@nb.njit\n", + "def nb_fill_hist(h):\n", + " h.fill(np.random.randn(10))" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "ename": "TypingError", + "evalue": "Failed in nopython mode pipeline (step: nopython frontend)\nUnknown attribute 'fill' of type Hist\n\nFile \"\", line 3:\ndef nb_fill_hist(h):\n h.fill(np.random.randn(10))\n ^\n\nDuring: typing of get attribute at (3)\n\nFile \"\", line 3:\ndef nb_fill_hist(h):\n h.fill(np.random.randn(10))\n ^\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypingError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mh1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mHist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreg_ax\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mnb_fill_hist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mh1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m~/anaconda3/envs/hist/lib/python3.9/site-packages/numba/core/dispatcher.py\u001b[0m in \u001b[0;36m_compile_for_args\u001b[0;34m(self, *args, **kws)\u001b[0m\n\u001b[1;32m 418\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpatch_message\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 419\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 420\u001b[0;31m \u001b[0merror_rewrite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'typing'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 421\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0merrors\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mUnsupportedError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 422\u001b[0m \u001b[0;31m# Something unsupported is present in the user code, add help info\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m~/anaconda3/envs/hist/lib/python3.9/site-packages/numba/core/dispatcher.py\u001b[0m in \u001b[0;36merror_rewrite\u001b[0;34m(e, issue_type)\u001b[0m\n\u001b[1;32m 359\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 360\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 361\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwith_traceback\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 362\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 363\u001b[0m \u001b[0margtypes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypingError\u001b[0m: Failed in nopython mode pipeline (step: nopython frontend)\nUnknown attribute 'fill' of type Hist\n\nFile \"\", line 3:\ndef nb_fill_hist(h):\n h.fill(np.random.randn(10))\n ^\n\nDuring: typing of get attribute at (3)\n\nFile \"\", line 3:\ndef nb_fill_hist(h):\n h.fill(np.random.randn(10))\n ^\n" + ] + } + ], + "source": [ + "reg_ax = axis.Regular(10, -3, 3)\n", + "h1 = Hist(reg_ax)\n", "\n", - "nb_create_Hist()" + "nb_fill_hist(h1)" ] }, { @@ -197,11 +261,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Numba fill\n", - "# nb_fill(h, array)\n", - "# h" - ] + "source": [] } ], "metadata": {