-
I'm working on a routine that accepts only a specific type of Array, e.g. >>> ak.Array([[[1, 2], [3]], [[7], []], [[9], [3, 8, 5]]])
<Array [[[1, 2], [3]], ..., [[9], [3, ...]]] type='3 * var * var * int64'> strictly characterized by:
each of these features must be present at each axis. I'm looking for a way to check the array sanity (i.e. the presence of all these characteristics) at all depths at the same time. I presume one needs to recursively walk the array structure? If yes, what is the recommended way to do so? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Awkward Arrays have three levels of type information:
It sounds like you only care about the first one, We would write this kind of test using recursion, e.g. import awkward as ak
def assert_valid(type_):
if isinstance(type_, ak.types.ArrayType):
return assert_valid(type_.content)
elif isinstance(type_, ak.types.ScalarType):
raise TypeError("Expected ArrayType or its content")
assert not isinstance(
type_,
(
ak.types.RegularType,
ak.types.OptionType,
ak.types.UnionType,
ak.types.UnknownType,
ak.types.RecordType,
),
)
if isinstance(type_, ak.types.ListType):
assert_valid(type_.content)
else:
assert isinstance(type_, ak.types.NumpyType)
array = ak.Array([[1, 2, 3]])
assert_valid(array.type) These type objects form a closed set, so we can exhaustively test each one. You should take a look at the type object of an array to see what its properties are! Does this enforce the rules you care about? |
Beta Was this translation helpful? Give feedback.
Awkward Arrays have three levels of type information:
It sounds like you only care about the first one,
ak.Array.type
.We would write this kind of test using recursion, e.g.