Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

Commit

Permalink
_normalize_branches with callable
Browse files Browse the repository at this point in the history
  • Loading branch information
jpivarski committed Jun 5, 2019
1 parent 77ebf29 commit 003fba1
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 8 deletions.
119 changes: 112 additions & 7 deletions binder/new-tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2060,7 +2060,7 @@
"source": [
"# Changing the output container type\n",
"\n",
"When we ask for [TTreeMethods.arrays](https://uproot.readthedocs.io/en/latest/ttree-handling.html#arrays), [TTreeMethods.iterate](https://uproot.readthedocs.io/en/latest/ttree-handling.html#iterate), or [uproot.iterate](https://uproot.readthedocs.io/en/latest/opening-files.html#uproot-iterate) (plural), we get a Python dict mapping branch names to arrays. (As a reminder, **namedecode=\"utf-8\"** makes those branch names Python strings, rather than bytestrings.) Sometimes, we want a different kind of container.\n",
"When we ask for [TTreeMethods.arrays](https://uproot.readthedocs.io/en/latest/ttree-handling.html#arrays) (plural), [TTreeMethods.iterate](https://uproot.readthedocs.io/en/latest/ttree-handling.html#iterate), or [uproot.iterate](https://uproot.readthedocs.io/en/latest/opening-files.html#uproot-iterate), we get a Python dict mapping branch names to arrays. (As a reminder, **namedecode=\"utf-8\"** makes those branch names Python strings, rather than bytestrings.) Sometimes, we want a different kind of container.\n",
"\n",
" * **outputtype:** the _type_ of the container to hold the output arrays.\n",
"\n",
Expand Down Expand Up @@ -2097,6 +2097,13 @@
"px"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using `tuple` as an **outputtype** in [TTreeMethods.iterate](https://uproot.readthedocs.io/en/latest/ttree-handling.html#iterate) and [uproot.iterate](https://uproot.readthedocs.io/en/latest/opening-files.html#uproot-iterate) lets us unpack the arrays in Python's for statement."
]
},
{
"cell_type": "code",
"execution_count": 67,
Expand All @@ -2111,7 +2118,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Another is `collections.namedtuple`, which packs everything into a single object, but the fields are accessible by name."
"Another useful type is `collections.namedtuple`, which packs everything into a single object, but the fields are accessible by name."
]
},
{
Expand Down Expand Up @@ -2150,7 +2157,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"You can define your own classes."
"You can also use your own classes."
]
},
{
Expand Down Expand Up @@ -3450,7 +3457,7 @@
}
],
"source": [
"events.arrays(\"p[xyz]?\").keys()"
"events.arrays(\"p[xyz]?\").keys() # using wildcards"
]
},
{
Expand All @@ -3470,13 +3477,111 @@
}
],
"source": [
"events.arrays(\"/p[x-z].?/\").keys()"
"events.arrays(\"/p[x-z].?/\").keys() # using regular expressions"
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"source": []
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ERROR:root:Internal Python error in the inspect module.\n",
"Below is the traceback from this internal error.\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Traceback (most recent call last):\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py\", line 3267, in run_code\n",
" exec(code_obj, self.user_global_ns, self.user_ns)\n",
" File \"<ipython-input-94-a0c91ad06364>\", line 1, in <module>\n",
" events.arrays(lambda branch: branch.compressionratio() > 3).keys()\n",
" File \"/home/pivarski/diana/uproot/binder/uproot/tree.py\", line 501, in arrays\n",
" branches = list(self._normalize_branches(branches, awkward))\n",
" File \"/home/pivarski/diana/uproot/binder/uproot/tree.py\", line 762, in _normalize_branches\n",
" yield branch, branch._normalize_dtype(result, awkward)\n",
" File \"/home/pivarski/diana/uproot/binder/uproot/tree.py\", line 1055, in _normalize_dtype\n",
" raise TypeError(\"branch interpretation must be an Interpretation, not {0} (type {1})\".format(interpretation, type(interpretation)))\n",
"TypeError: branch interpretation must be an Interpretation, not False (type <class 'bool'>)\n",
"\n",
"During handling of the above exception, another exception occurred:\n",
"\n",
"Traceback (most recent call last):\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py\", line 2018, in showtraceback\n",
" stb = value._render_traceback_()\n",
"AttributeError: 'TypeError' object has no attribute '_render_traceback_'\n",
"\n",
"During handling of the above exception, another exception occurred:\n",
"\n",
"Traceback (most recent call last):\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/IPython/core/ultratb.py\", line 1095, in get_records\n",
" return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/IPython/core/ultratb.py\", line 313, in wrapped\n",
" return f(*args, **kwargs)\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/IPython/core/ultratb.py\", line 347, in _fixed_getinnerframes\n",
" records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/inspect.py\", line 1502, in getinnerframes\n",
" frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/inspect.py\", line 1460, in getframeinfo\n",
" filename = getsourcefile(frame) or getfile(frame)\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/inspect.py\", line 696, in getsourcefile\n",
" if getattr(getmodule(object, filename), '__loader__', None) is not None:\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/inspect.py\", line 733, in getmodule\n",
" if ismodule(module) and hasattr(module, '__file__'):\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/py/_vendored_packages/apipkg.py\", line 195, in __getattribute__\n",
" return getattr(getmod(), name)\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/py/_vendored_packages/apipkg.py\", line 179, in getmod\n",
" x = importobj(modpath, None)\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/py/_vendored_packages/apipkg.py\", line 69, in importobj\n",
" module = __import__(modpath, None, None, ['__doc__'])\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/pytest.py\", line 8, in <module>\n",
" from _pytest.config import cmdline\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/_pytest/config/__init__.py\", line 17, in <module>\n",
" from pkg_resources import parse_version\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/pkg_resources/__init__.py\", line 3126, in <module>\n",
" @_call_aside\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/pkg_resources/__init__.py\", line 3110, in _call_aside\n",
" f(*args, **kwargs)\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/pkg_resources/__init__.py\", line 3139, in _initialize_master_working_set\n",
" working_set = WorkingSet._build_master()\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/pkg_resources/__init__.py\", line 572, in _build_master\n",
" ws = cls()\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/pkg_resources/__init__.py\", line 565, in __init__\n",
" self.add_entry(entry)\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/pkg_resources/__init__.py\", line 621, in add_entry\n",
" for dist in find_distributions(entry, True):\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/pkg_resources/__init__.py\", line 1886, in find_eggs_in_zip\n",
" if metadata.has_metadata('PKG-INFO'):\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/pkg_resources/__init__.py\", line 1405, in has_metadata\n",
" return self.egg_info and self._has(self._fn(self.egg_info, name))\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/pkg_resources/__init__.py\", line 1760, in _has\n",
" return zip_path in self.zipinfo or zip_path in self._index()\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/pkg_resources/__init__.py\", line 1637, in zipinfo\n",
" return self._zip_manifests.load(self.loader.archive)\n",
" File \"/home/pivarski/miniconda3/lib/python3.7/site-packages/pkg_resources/__init__.py\", line 1594, in load\n",
" mtime = os.stat(path).st_mtime\n",
"FileNotFoundError: [Errno 2] No such file or directory: '/home/pivarski/miniconda3/lib/python3.7/site-packages/uproot-3.6.3-py3.7.egg'\n"
]
},
{
"ename": "TypeError",
"evalue": "branch interpretation must be an Interpretation, not False (type <class 'bool'>)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m"
]
}
],
"source": [
"events.arrays(lambda branch: branch.compressionratio() > 3).keys()"
]
},
{
"cell_type": "markdown",
Expand Down
2 changes: 1 addition & 1 deletion uproot/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ def _normalize_branches(self, arg, awkward, allownone=True, allowcallable=True,
elif allowcallable and callable(arg):
for branch in self.allvalues():
result = arg(branch)
if result is None:
if result is None or result is False:
pass
elif result is True: # function is a filter
interpretation = interpret(branch, awkward)
Expand Down

0 comments on commit 003fba1

Please sign in to comment.