Skip to content

Commit

Permalink
fix: all AwkwardForth Forms now agree with awkward_form method output. (
Browse files Browse the repository at this point in the history
#790)

* fix: all AwkwardForth Forms now agree with awkward_form method output.

* Oops; don't forget this test.
  • Loading branch information
jpivarski authored Nov 30, 2022
1 parent 000f31f commit 150cf6c
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 29 deletions.
7 changes: 3 additions & 4 deletions src/uproot/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,9 @@ def awkward_form(self, file, context):
index_format (str): Format to use for indexes of the
``awkward.forms.Form``; may be ``"i32"``, ``"u32"``, or
``"i64"``.
header (bool): If True, include headers in the Form's ``"uproot"``
parameters.
tobject_header (bool): If True, include headers for ``TObject``
classes in the Form's ``"uproot"`` parameters.
header (bool): If True, include header fields of each C++ class.
tobject_header (bool): If True, include header fields of each ``TObject``
base class.
breadcrumbs (tuple of class objects): Used to check for recursion.
Types that contain themselves cannot be Awkward Arrays because the
depth of instances is unknown.
Expand Down
9 changes: 4 additions & 5 deletions src/uproot/interpretation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def awkward_form(
context=None,
index_format="i64",
header=False,
tobject_header=True,
tobject_header=False,
breadcrumbs=(),
):
"""
Expand All @@ -87,10 +87,9 @@ def awkward_form(
index_format (str): Format to use for indexes of the
``awkward.forms.Form``; may be ``"i32"``, ``"u32"``, or
``"i64"``.
header (bool): If True, include headers in the Form's ``"uproot"``
parameters.
tobject_header (bool): If True, include headers for ``TObject``
classes in the Form's ``"uproot"`` parameters.
header (bool): If True, include header fields of each C++ class.
tobject_header (bool): If True, include header fields of each ``TObject``
base class.
breadcrumbs (tuple of class objects): Used to check for recursion.
Types that contain themselves cannot be Awkward Arrays because the
depth of instances is unknown.
Expand Down
2 changes: 1 addition & 1 deletion src/uproot/interpretation/grouped.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def awkward_form(
context=None,
index_format="i64",
header=False,
tobject_header=True,
tobject_header=False,
breadcrumbs=(),
):
context = self._make_context(
Expand Down
2 changes: 1 addition & 1 deletion src/uproot/interpretation/identify.py
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ def awkward_form(
context=None,
index_format="i64",
header=False,
tobject_header=True,
tobject_header=False,
breadcrumbs=(),
):
raise self
Expand Down
2 changes: 1 addition & 1 deletion src/uproot/interpretation/jagged.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def awkward_form(
context=None,
index_format="i64",
header=False,
tobject_header=True,
tobject_header=False,
breadcrumbs=(),
):
context = self._make_context(
Expand Down
14 changes: 10 additions & 4 deletions src/uproot/interpretation/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,18 @@ def _awkward_offsets(awkward, form, array):

def _awkward_json_to_array(awkward, form, array):
if form["class"] == "NumpyArray":
form = awkward.forms.from_json(json.dumps(form))
dtype = awkward.types.numpytype.primitive_to_dtype(form.primitive)
if isinstance(array, awkward.contents.EmptyArray):
form = awkward.forms.from_json(json.dumps(form))
dtype = awkward.types.numpytype.primitive_to_dtype(form.primitive)
return awkward.contents.NumpyArray(numpy.empty(0, dtype=dtype))
return awkward.contents.NumpyArray(
numpy.empty(0, dtype=dtype),
parameters=form.parameters,
)
else:
return array
return awkward.contents.NumpyArray(
numpy.asarray(array.data, dtype=dtype),
parameters=form.parameters,
)

elif form["class"] == "RecordArray":
contents = []
Expand Down
6 changes: 3 additions & 3 deletions src/uproot/interpretation/numerical.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def awkward_form(
context=None,
index_format="i64",
header=False,
tobject_header=True,
tobject_header=False,
breadcrumbs=(),
):
context = self._make_context(
Expand Down Expand Up @@ -668,7 +668,7 @@ def awkward_form(
context=None,
index_format="i64",
header=False,
tobject_header=True,
tobject_header=False,
breadcrumbs=(),
):
context = self._make_context(
Expand Down Expand Up @@ -726,7 +726,7 @@ def awkward_form(
context=None,
index_format="i64",
header=False,
tobject_header=True,
tobject_header=False,
breadcrumbs=(),
):
context = self._make_context(
Expand Down
4 changes: 2 additions & 2 deletions src/uproot/interpretation/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def awkward_form(
context=None,
index_format="i64",
header=False,
tobject_header=True,
tobject_header=False,
breadcrumbs=(),
):
context = self._make_context(
Expand Down Expand Up @@ -609,7 +609,7 @@ def awkward_form(
context=None,
index_format="i64",
header=False,
tobject_header=True,
tobject_header=False,
breadcrumbs=(),
):
context = self._make_context(
Expand Down
2 changes: 1 addition & 1 deletion src/uproot/interpretation/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def awkward_form(
context=None,
index_format="i64",
header=False,
tobject_header=True,
tobject_header=False,
breadcrumbs=(),
):
context = self._make_context(
Expand Down
7 changes: 3 additions & 4 deletions src/uproot/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,10 +679,9 @@ def awkward_form(cls, file, context):
index_format (str): Format to use for indexes of the
``awkward.forms.Form``; may be ``"i32"``, ``"u32"``, or
``"i64"``.
header (bool): If True, include headers in the Form's ``"uproot"``
parameters.
tobject_header (bool): If True, include headers for ``TObject``
classes in the Form's ``"uproot"`` parameters.
header (bool): If True, include header fields of each C++ class.
tobject_header (bool): If True, include header fields of each ``TObject``
base class.
breadcrumbs (tuple of class objects): Used to check for recursion.
Types that contain themselves cannot be Awkward Arrays because the
depth of instances is unknown.
Expand Down
4 changes: 2 additions & 2 deletions src/uproot/models/TRef.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ def read_members(self, chunk, cursor, context, file):
if forth_obj.should_add_form():
for elem in keys:
forth_obj.add_form_key(elem)
temp_aform = f'{{"class": "RecordArray", "contents": {{"fName": {{"class": "ListOffsetArray", "offsets": "i64", "content": {{"class": "NumpyArray", "primitive": "uint8", "inner_shape": [], "parameters": {{"__array__": "char"}}, "form_key": "node{form_keys[2]}"}}, "parameters": {{}}, "form_key": "node{form_keys[1]}"}}, "fSize": {{"class": "NumpyArray", "primitive": "int64", "inner_shape": [], "parameters": {{}}, "form_key": "node{form_keys[3]}"}}, "refs": {{"class": "ListOffsetArray", "offsets": "i64", "content": {{"class": "NumpyArray", "primitive": "int64", "inner_shape": [], "parameters": {{}}, "form_key": "node{form_keys[5]}"}}, "parameters": {{}}, "form_key": "node{form_keys[4]}"}}}}, "parameters": {{}}, "form_key": "node{form_keys[0]}"}}'
temp_aform = f'{{"class": "RecordArray", "contents": {{"fName": {{"class": "ListOffsetArray", "offsets": "i64", "content": {{"class": "NumpyArray", "primitive": "uint8", "inner_shape": [], "parameters": {{"__array__": "char"}}, "form_key": "node{form_keys[2]}"}}, "parameters": {{"__array__": "string"}}, "form_key": "node{form_keys[1]}"}}, "fSize": {{"class": "NumpyArray", "primitive": "int32", "inner_shape": [], "parameters": {{}}, "form_key": "node{form_keys[3]}"}}, "refs": {{"class": "ListOffsetArray", "offsets": "i64", "content": {{"class": "NumpyArray", "primitive": "int32", "inner_shape": [], "parameters": {{}}, "form_key": "node{form_keys[5]}"}}, "parameters": {{}}, "form_key": "node{form_keys[4]}"}}}}, "parameters": {{"__record__": "TRefArray"}}, "form_key": "node{form_keys[0]}"}}'
forth_obj.add_form(json.loads(temp_aform))
forth_stash.add_to_header(
f"output node{form_keys[1]}-offsets int64\noutput node{form_keys[2]}-data uint8\noutput node{form_keys[3]}-data int64\noutput node{form_keys[4]}-offsets int64\noutput node{form_keys[5]}-data int64\n"
f"output node{form_keys[1]}-offsets int64\noutput node{form_keys[2]}-data uint8\noutput node{form_keys[3]}-data int32\noutput node{form_keys[4]}-offsets int64\noutput node{form_keys[5]}-data int32\n"
)
forth_stash.add_to_init(
f"0 node{form_keys[1]}-offsets <- stack\n0 node{form_keys[4]}-offsets <- stack\n"
Expand Down
2 changes: 1 addition & 1 deletion src/uproot/streamers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ def class_code(
f' forth_stash.add_to_header(f"output node{{key}}-data {{uproot._awkward_forth.convert_dtype(uproot._awkward_forth.symbol_dict[self._dtype{len(dtypes)}])}}\\n")',
' forth_stash.add_to_header(f"output node{key2}-offsets int64\\n")',
' forth_stash.add_to_init(f"0 node{key2}-offsets <- stack\\n")',
f' content[{self.name!r}] = {{"class": "ListOffsetArray", "offsets": "i64", "content": {{ "class": "NumpyArray", "primitive": f"{{uproot._awkward_forth.convert_dtype(uproot._awkward_forth.symbol_dict[self._dtype{len(dtypes)}])}}", "inner_shape": [], "parameters": {{}}, "form_key": f"node{{key}}"}}, "form_key": f"node{{key2}}"}}',
f' content[{self.name!r}] = {{"class": "RegularArray", "size": {self.array_length}, "content": {{ "class": "NumpyArray", "primitive": f"{{uproot._awkward_forth.convert_dtype(uproot._awkward_forth.symbol_dict[self._dtype{len(dtypes)}])}}", "inner_shape": [], "parameters": {{}}, "form_key": f"node{{key}}"}}, "form_key": f"node{{key2}}"}}',
f' forth_stash.add_to_pre(f"{self.array_length} dup node{{key2}}-offsets +<- stack \\n stream #!{{uproot._awkward_forth.symbol_dict[self._dtype{len(dtypes)}]}}-> node{{key}}-data\\n")\n',
" if forth_obj.should_add_form():",
" forth_obj.add_form_key(form_key)",
Expand Down
Loading

0 comments on commit 150cf6c

Please sign in to comment.