OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Accessing elements of an awkward array that are *not* a passed in index?

  • Thread starter Thread starter Matt Bellis
  • Start date Start date
M

Matt Bellis

Guest
Hi awkward users/developers!

I have a bit of an odd use-case for awkward. I'm trying to access the elements of an awkward array that do not correspond to some particular set of indices. I've drawn up an example that, while not exactly what I'm doing, might be familiar to awkward users.

Suppose I have some jets in my event(s) and those jets contain some number of leptons. I have a separate array of all my leptons and I keep track of which leptons are in each jet with an index.

Code:
In the following example, I have 3 events in total with one jet per event and some number of leptons. Each lepton has a particular flag associated with it.For each jet, I keep track of the indices of the leptons in that jet.

Code:
jet_lepton_indices = ak.Array([[0, 2], [1], [2,3]])
print(f'jet_lepton_indices\n{jet_lepton_indices}\n')

lepton_flags = ak.Array([[0, 10, 20, 30], [0, 10, 20, 30], [0, 10, 20, 30, 40]])
print(f'lepton_flags\n{lepton_flags}\n')

And the output is

Code:
jet_lepton_indices
[[0, 2], [1], [2, 3]]

lepton_flags
[[0, 10, 20, 30], [0, 10, 20, 30], [0, 10, 20, 30, 40]]

If I want the flags of the leptons that are in each jet, I can just do the following:

Code:
lepton_flags[jet_lepton_indices]

And get

Code:
[[0, 20],
 [10],
 [20, 30]]

Easy peasy. :)

But what I also need is to access all the lepton flags associated with leptons that are not in the jets. I'd like to be able to produce

Code:
[[10, 30],
 [0, 20, 30],
 [0, 10, 40]]

I thought maybe I could do lepton_flags[~jet_lepton_indices] but that has some sort of behavior I don't understand. :)

I thought there might be a way to flatten/unflatten but I can't figure that out either.

Any help is greatly appreciated! Thanks!

Matt
<p>Hi awkward users/developers!</p>
<p>I have a bit of an odd use-case for awkward. I'm trying to access the elements of an awkward array that do <em>not</em> correspond to some particular set of indices. I've drawn up an example that, while not exactly what I'm doing, might be familiar to awkward users.</p>
<p>Suppose I have some jets in my event(s) and those jets contain some number of leptons. I have a separate array of all my leptons and I keep track of which leptons are in each jet with an index.</p>
<pre><code>In the following example, I have 3 events in total with one jet per event and some number of leptons. Each lepton has a particular flag associated with it.For each jet, I keep track of the indices of the leptons in that jet.
</code></pre>
<pre><code>jet_lepton_indices = ak.Array([[0, 2], [1], [2,3]])
print(f'jet_lepton_indices\n{jet_lepton_indices}\n')

lepton_flags = ak.Array([[0, 10, 20, 30], [0, 10, 20, 30], [0, 10, 20, 30, 40]])
print(f'lepton_flags\n{lepton_flags}\n')
</code></pre>
<p>And the output is</p>
<pre><code>jet_lepton_indices
[[0, 2], [1], [2, 3]]

lepton_flags
[[0, 10, 20, 30], [0, 10, 20, 30], [0, 10, 20, 30, 40]]
</code></pre>
<p>If I want the flags of the leptons that are in each jet, I can just do the following:</p>
<pre><code>lepton_flags[jet_lepton_indices]
</code></pre>
<p>And get</p>
<pre><code>[[0, 20],
[10],
[20, 30]]
</code></pre>
<p>Easy peasy. :)</p>
<p>But what I also need is to access all the lepton flags associated with leptons that are <em>not</em> in the jets. I'd like to be able to produce</p>
<pre><code>[[10, 30],
[0, 20, 30],
[0, 10, 40]]
</code></pre>
<p>I thought maybe I could do <code>lepton_flags[~jet_lepton_indices]</code> but that has some sort of behavior I don't understand. :)</p>
<p>I thought there might be a way to flatten/unflatten but I can't figure that out either.</p>
<p>Any help is greatly appreciated! Thanks!</p>
<p>Matt</p>
 
Top