-
Hi, To give an example which behavior I would expect, I will mask an Array first: import awkward as ak
array1 = ak.Array(
[
[53.1, 30.2],
[],
[91.7, 88.6, 20.3],
]
)
print("array1:")
print(array1)
mask1 = array1 > 40
print("mask1:")
print(mask1)
print()
print("slicing array1 with mask1:")
print(array1[mask1])
print("masking array1 with mask1:")
print(array1.mask[mask1])
print(ak.mask(array1, mask1)) The output is the following: array1:
[[53.1, 30.2], [], [91.7, 88.6, 20.3]]
mask1:
[[True, False], [], [True, True, False]]
slicing array1 with mask1:
[[53.1], [], [91.7, 88.6]]
masking array1 with mask1:
[[53.1, None], [], [91.7, 88.6, None]]
[[53.1, None], [], [91.7, 88.6, None]] This is exactly what I was expecting to get. Now if I do the same thing with a RecordArray: array2 = ak.Array(
[
{
'Muon_pt': [53.1, 30.2],
},
{
'Muon_pt': [],
},
{
'Muon_pt': [91.7, 88.6, 20.3],
}
]
)
print("array2:")
print(array2)
print(array2.tolist())
mask2 = array2.Muon_pt > 40
print("mask2:")
print(mask2)
print()
print("slicing array2 with mask2:")
print(array2[mask2])
print("masking array2 with mask2:")
print(array2.mask[mask2])
print(ak.mask(array2, mask2))
print()
print("to list:")
print()
print("slicing array2 with mask2:")
print(array2[mask2].tolist())
print("masking array2 with mask2:")
print(array2.mask[mask2].tolist())
print(ak.mask(array2, mask2).tolist()) The output is: array2:
[{Muon_pt: [53.1, 30.2]}, {Muon_pt: []}, {Muon_pt: [91.7, 88.6, 20.3]}]
[{'Muon_pt': [53.1, 30.2]}, {'Muon_pt': []}, {'Muon_pt': [91.7, 88.6, 20.3]}]
mask2:
[[True, False], [], [True, True, False]]
slicing array2 with mask2:
[{Muon_pt: [53.1]}, {Muon_pt: []}, {Muon_pt: [91.7, 88.6]}]
masking array2 with mask2:
[[{Muon_pt: [53.1, 30.2]}, None], ... 20.3]}, {Muon_pt: [91.7, 88.6, 20.3]}, None]]
[[{Muon_pt: [53.1, 30.2]}, None], ... 20.3]}, {Muon_pt: [91.7, 88.6, 20.3]}, None]]
to list:
slicing array2 with mask2:
[{'Muon_pt': [53.1]}, {'Muon_pt': []}, {'Muon_pt': [91.7, 88.6]}]
masking array2 with mask2:
[[{'Muon_pt': [53.1, 30.2]}, None], [], [{'Muon_pt': [91.7, 88.6, 20.3]}, {'Muon_pt': [91.7, 88.6, 20.3]}, None]]
[[{'Muon_pt': [53.1, 30.2]}, None], [], [{'Muon_pt': [91.7, 88.6, 20.3]}, {'Muon_pt': [91.7, 88.6, 20.3]}, None]] But what I was expecting was: [{'Muon_pt': [53.1, None]}, {'Muon_pt': []}, {'Muon_pt': [91.7, 88.6, None]}] What I see is, that the shape of array2 changed and is now the shape of mask2. I would expect ak.mask to preserve the shape of the initial array. I don't know if this is the normal behavior of RecordArrays but I would be happy to find a way to get the expected output. Thanks for all comments and suggestions. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 15 replies
-
What's happening here is that the mask and the array are first broadcast together. In this case, this changes the type of the array: >>> array2.type
3 * {"Muon_pt": var * float64}
>>> mask2.type
3 * var * bool
>>> ak.broadcast_arrays(array2,mask2)
[<Array [[{Muon_pt: [53.1, 30.2, ... 20.3]}]] type='3 * var * {"Muon_pt": var * f...'>,
<Array [[{Muon_pt: [True, True, ... False]}]] type='3 * var * {"Muon_pt": var * ...'>] It's not so obvious why More specifically, you need to provide Awkward with your mask in a RecordArray, e.g. mask2 = ak.zip({
"Muon_pt": array2.Muon_pt > 40
}, depth_limit=1) This will ensure that the broadcast result matches what you expect to happen. Whether this is the right approach for your problem, though, I can't say! :) |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot for the quick response. When I saw the shape of I also tried it with more than one field, like array2 = ak.Array(
[
{
'Muon_pt': [53.1, 30.2],
'Muon_eta': [1.1, 0.2],
},
{
'Muon_pt': [],
'Muon_eta': [],
},
{
'Muon_pt': [91.7, 88.6, 20.3],
'Muon_eta': [0.6, -0.5],
}
]
) I tried to keep the first example simple, because it seems to yield the same result: array2:
[{Muon_pt: [53.1, 30.2], Muon_eta: [1.1, 0.2], ... 20.3], Muon_eta: [0.6, -0.5]}]
[{'Muon_pt': [53.1, 30.2], 'Muon_eta': [1.1, 0.2]}, {'Muon_pt': [], 'Muon_eta': []}, {'Muon_pt': [91.7, 88.6, 20.3], 'Muon_eta': [0.6, -0.5]}]
mask2:
[[True, False], [], [True, True, False]]
slicing array2 with mask2:
[{Muon_pt: [53.1], Muon_eta: [1.1]}, ... 91.7, 88.6], Muon_eta: [0.6, -0.5]}]
masking array2 with mask2:
[[{Muon_pt: [53.1, 30.2], Muon_eta: [1.1, 0.2], ... Muon_eta: [0.6, -0.5]}, None]]
[[{Muon_pt: [53.1, 30.2], Muon_eta: [1.1, 0.2], ... Muon_eta: [0.6, -0.5]}, None]]
to list:
slicing array2 with mask2:
[{'Muon_pt': [53.1], 'Muon_eta': [1.1]}, {'Muon_pt': [], 'Muon_eta': []}, {'Muon_pt': [91.7, 88.6], 'Muon_eta': [0.6, -0.5]}]
masking array2 with mask2:
[[{'Muon_pt': [53.1, 30.2], 'Muon_eta': [1.1, 0.2]}, None], [], [{'Muon_pt': [91.7, 88.6, 20.3], 'Muon_eta': [0.6, -0.5]}, {'Muon_pt': [91.7, 88.6, 20.3], 'Muon_eta': [0.6, -0.5]}, None]]
[[{'Muon_pt': [53.1, 30.2], 'Muon_eta': [1.1, 0.2]}, None], [], [{'Muon_pt': [91.7, 88.6, 20.3], 'Muon_eta': [0.6, -0.5]}, {'Muon_pt': [91.7, 88.6, 20.3], 'Muon_eta': [0.6, -0.5]}, None]] But ultimately, I have a bunch of fields in that RecordArray. Thanks a lot for the example using I am also wondering if I really need to have duplicates of this |
Beta Was this translation helpful? Give feedback.
Thanks a lot for the quick response.
When I saw the shape of
mask2
inarray2
I already suspected something happening when broadcasting, that I didn't expect.I also tried it with more than one field, like
I tried to keep the first example simple, because it seems to yield the same result: