NNPDF / pineappl

PineAPPL is not an extension of APPLgrid
https://nnpdf.github.io/pineappl/
GNU General Public License v3.0
13 stars 3 forks source link

Add `Grid` slicing #166

Open cschwan opened 2 years ago

cschwan commented 2 years ago

Sometimes we'd like to work with a slice of a Grid, which means a subset of the contained orders, luminosities and/or bins.

We can already do this using the {order,bin,lumi}_mask parameters of Grid::convolute, but we have to remember these parameters when calling other methods of Grid. Instead it would be useful to add a new method Grid::slice, passing it the desired orders, bins and lumis, which then returns a GridSlice object containing the reference of a Grid object and the corresponding masks. This would allow

Furthermore, Grid itself is a GridSlice, so GridSlice would be a trait and Grid::slice would return a struct implementing the trait.

alecandido commented 2 years ago

I foresee ownership issues...

If you want to avoid data duplication, there should be a single owner, and multiple references around (one for each slice). But then it would be complicated to have a mutable slice.

A possible solution might be to make the grid owning data through a reference counted Rc, and then make the slices own a Weak reference, to upgrade only when you call some slice method.

This is a solution that came to my mind, but I just wanted to point out the problem.

cschwan commented 2 years ago

This won't be a problem, we'll have to model that similar to ndarray's ArrayBase::slice which uses explicit lifetime parameters. The Python interface will be more difficult, but in the first instance we can leave the API as is.

alecandido commented 2 years ago

Ok, I learnt myself that explicit lifetimes are difficult (easy to fall down a path with a dead-end). But the moment there is a working example, following it is definitely a sensible choice.

I'll leave it up to you, and the moment we'll have the Rust implementation, we'll think about how to wrap it in Python. But I'm confident we'll find a suitable way to do it :)