hpc4cmb / toast

Time Ordered Astrophysics Scalable Tools
Other
44 stars 39 forks source link

Add convenience method for getting slices from a view #596

Open tskisner opened 2 years ago

tskisner commented 2 years ago

This issue was spawned by discussion in #593

Originally, the "view" concept (i.e. returning a list of detdata / shared views) seemed like a nice shorthand way of accessing the underlying data- especially when working interactively. However, depending on the underlying data, there can be a performance penalty looping over views first and then detectors within a view (since this is jumping around in memory). The operators where we have ported the internals to C++ / OpenMP or Jax now always loop over detectors and then elements of the intervals list (which is now just a numpy array with a custom dtype). This keeps the memory access contiguous.

As suggested in the other thread, it would be convenient to have a quick way of getting the sample slices associated with an interval list / view. The intervals object is the plain numpy array of intervals, and the view object would not only provide the current list of detdata / shared views (which it implements via Sequence.__getitem__), but also have a method that returns a list of actual slice objects.

So the pattern would look something like:

for det in dets:
    for vslice in ob.view["intervals_name"].slices:
        nsample = vslice.stop - vslice.start
        ob.detdata["signal"][det, vslice] += 1.0

Instead of

for det in dets:
    for intrvl in ob.intervals["intervals_name"]:
        vslice = slice(intrvl.first, intrvl.last + 1)
        nsample = intrvl.last - intrvl.first + 1
        ob.detdata["signal"][det, vslice] += 1.0