Generic way to detect a tuple structure? #1349
-
Is there a short and easy top-level way to check if an >>> a = ak.Array([(1, 2)])
>>> a.layout
<RecordArray length="1">
<field index="0">
<NumpyArray format="l" shape="1" data="1" at="0x55e8e613deb0"/>
</field>
<field index="1">
<NumpyArray format="l" shape="1" data="2" at="0x55e8e613ded0"/>
</field>
</RecordArray>
>>> a.layout.istuple
True but this doesn't work at higher depths: >>> b = ak.Array([[(1, 2)]])
>>> b.layout
<ListOffsetArray64>
<offsets><Index64 i="[0 1]" offset="0" length="2" at="0x55e8e613d600"/></offsets>
<content><RecordArray length="1">
<field index="0">
<NumpyArray format="l" shape="1" data="1" at="0x55e8e66aa9c0"/>
</field>
<field index="1">
<NumpyArray format="l" shape="1" data="2" at="0x55e8e667b330"/>
</field>
</RecordArray></content>
</ListOffsetArray64>
>>> b.layout.istuple
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'awkward._ext.ListOffsetArray64' object has no attribute 'istuple' In this case, you could do Am I missing a simple way to check this or does this feature not exist? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Right now, I we don't expose this information at the top-level. In For now, you can do this with a layout visitor: def is_tuple(array):
layout = ak.to_layout(array, allow_record=True)
def visitor(layout):
if isinstance(layout, (ak.layout.Record, ak.layout.RecordArray)):
return layout.istuple
elif isinstance(
layout, ak._util.listtypes + ak._util.optiontypes + ak._util.indexedtypes
):
return visitor(layout.content)
elif isinstance(layout, ak.layout.VirtualArray):
return visitor(layout.array)
elif isinstance(layout, ak._util.uniontypes):
return all(visitor(x) for x in layout.contents)
elif isinstance(layout, ak.layout.NumpyArray):
return False
else:
raise ValueError
return visitor(layout) Note that unions over records as non-tuples are a less well defined concept (are they tuples if they have different fields?). Here I'm considering a union of tuples as a tuple, even if they have differing numbers of fields. |
Beta Was this translation helpful? Give feedback.
Right now, I we don't expose this information at the top-level.
ak.Array
has thefields
attribute which returnsak.operations.describe.fields(self)
, but we don't yet have an equivalent foristuple
.In
v2
, we are a bit better at making tuples and records behave predictably with respect to numeric (albeit in string form) fields, but there is still no function to test the entire layout for any tuple. #1351 is one solution for this.For now, you can do this with a layout visitor: