You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I don't have experience using isel with boolean indexing. (Although the docs on positional indexing claim it is supported.) My guess is that that the time is being spent aligning the indexer with the array, which is unnecessary since you know they are already aligned. Probably not the most efficient pattern for xarray.
Here's how I would recommend writing the query using label-based selection:
I am sorry @rabernat and @maxim-lian ,
the variable's name time and the simple example with the greater than filter are misleading. In general, it is about using a boolean mask via isel and that it is very slow. In my code, I am not able to use your workaround since my boolean mask is more complex.
Another part of the matrix of possibilities. Takes about half the time if you pass time_filter.values (numpy array) rather than the time_filter DataArray:
>>> %timeit ds.a.isel(time=time_filter)
1 loop, best of 3: 906 ms per loop
>>> %timeit ds.a.isel(time=time_filter.values)
1 loop, best of 3: 447 ms per loop
>>> %timeit ds.a.values[time_filter]
10 loops, best of 3: 169 ms per loop
Given the size of this gap, I suspect this could be improved with some investigation and profiling, but there is certainly an upper-limit on the possible performance gain.
One simple example is that indexing the dataset needs to index both 'a' and 'time', so it's going to be at least twice as slow as only indexing 'a'. So the second indexing expression ds.a.isel(time=time_filter.values) is only 447/(169*2) = 1.32 times slower than the best case scenario.
I am looking into a similar performance issue with isel, but it seems that the issue is that it is creating arrays that are much bigger than needed. For my multidimensional case (time/x/y/window), what should end up only taking a few hundred MB is spiking up to 10's of GB of used RAM. Don't know if this might be a possible source of performance issues.
Huh, strange... I just tried a simplified version of what I was doing (particularly, no dask arrays), and everything worked fine. I'll have to investigate further.
Yeah, it looks like if da is backed by a dask array, and you do a .isel(win=window.compute()) because otherwise isel barfs on dask indexers, it seems, then the memory usage shoots through the roof. Note that in my case, the dask chunks are (1, 3000, 7000). If I do a window.load() prior to window.isel(), then the memory usage is perfectly reasonable.
No, it does not make a difference. The example above peaks at around 5GB of memory (a bit much, but manageable). And it peaks similarly if we chunk it like you suggested.
Yes, I'm seeing similar numbers, about 10x slower indexing in a DataArray. This seems to have gotten slower over time. It would be good to track this down and add a benchmark!
%timeit ds.a.values[time_filter]
158 ms ± 1.14 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit ds.a.isel(time=time_filter.values)
2.57 s ± 3.65 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit ds.a.isel(time=time_filter)
3.12 s ± 37.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit ds.a.values[time_filter]
158 ms ± 2.2 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit ds.a.isel(time=time_filter.values)
665 ms ± 6.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit ds.a.isel(time=time_filter)
1.15 s ± 1.55 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Can we short-circuit the special case where the index of the array used for slicing is the same object as the index being sliced, so no alignment is needed?
Yes, align checks index.equals(other) first, which has a shortcut for the same object.
The real mystery here is why time_filter.indexes['time'] and ds.indexes['time'] are not the same object. I guess this is likely due to lazy initialization of indexes, and should be fixed eventually by the explicit indexes refactor.
Hi, I'd like to understand how isel works exactly in conjunction with dask arrays.
As it seems, #3481 propagates the isel operation onto each dask chunk for lazy evaluation. Is this correct?
I don't know much about indexing but that PR propagates a "new" indexes property as part of #1603 (work towards enabling more flexible indexing), it doesn't change anything about "indexing". I think the dask docs may be more relevant to what you may be asking about: https://docs.dask.org/en/latest/array-slicing.html
@dschwoerer are you sure that you are actually calculating the same thing in both cases? What exactly do the values of slc[d] look like? I would test thing on smaller inputs to verify. My guess is that you are inadvertently calculating something different, recalling that Xarray's broadcasting rules differ slightly from NumPy's.
I see, they are not the same - the slow one is still a dask array, the other one is not:
Sn (r, theta, phi, sampling) float64 dask.array<chunksize=(14, 52, 2, 10), meta=np.ndarray>,
Sn (r, theta, phi, sampling) float64 nan nan nan nan ... nan nan nan
Otherwise they are the same, so this might be dask related ...
A reproducible example would help but indexing with dask arrays is a bit limited.
On #5873 it's possible it will raise an error and ask you to compute the indexer. Also see dask/dask#4156
EDIT: your slowdown is probably because it's compuing Sn multiple times. You could speed it up by calling compute once and passing a numpy array to isel
Activity
rabernat commentedon Jun 12, 2018
I don't have experience using
iselwith boolean indexing. (Although the docs on positional indexing claim it is supported.) My guess is that that the time is being spent aligning the indexer with the array, which is unnecessary since you know they are already aligned. Probably not the most efficient pattern for xarray.Here's how I would recommend writing the query using label-based selection:
max-sixty commentedon Jun 12, 2018
@rabernat that's a good solution where it's a slice
When is a time that it needs to align a bool array? If you try and pass an array of unequal length, it doesn't work anyway:
JohnMrziglod commentedon Jun 12, 2018
I am sorry @rabernat and @maxim-lian ,
the variable's name time and the simple example with the greater than filter are misleading. In general, it is about using a boolean mask via
iseland that it is very slow. In my code, I am not able to use your workaround since my boolean mask is more complex.rabernat commentedon Jun 12, 2018
Another part of the matrix of possibilities. Takes about half the time if you pass
time_filter.values(numpy array) rather than thetime_filterDataArray:shoyer commentedon Jun 12, 2018
My measurements:
Given the size of this gap, I suspect this could be improved with some investigation and profiling, but there is certainly an upper-limit on the possible performance gain.
One simple example is that indexing the dataset needs to index both
'a'and'time', so it's going to be at least twice as slow as only indexing'a'. So the second indexing expressionds.a.isel(time=time_filter.values)is only447/(169*2) = 1.32times slower than the best case scenario.WeatherGod commentedon Sep 25, 2018
I am looking into a similar performance issue with isel, but it seems that the issue is that it is creating arrays that are much bigger than needed. For my multidimensional case (time/x/y/window), what should end up only taking a few hundred MB is spiking up to 10's of GB of used RAM. Don't know if this might be a possible source of performance issues.
max-sixty commentedon Sep 25, 2018
@WeatherGod do you have a reproducible example? I'm happy to have a look
WeatherGod commentedon Sep 25, 2018
Huh, strange... I just tried a simplified version of what I was doing (particularly, no dask arrays), and everything worked fine. I'll have to investigate further.
WeatherGod commentedon Sep 25, 2018
Just for posterity, though, here is my simplified (working!) example:
WeatherGod commentedon Sep 25, 2018
Yeah, it looks like if
dais backed by a dask array, and you do a.isel(win=window.compute())because otherwise isel barfs on dask indexers, it seems, then the memory usage shoots through the roof. Note that in my case, the dask chunks are (1, 3000, 7000). If I do awindow.load()prior towindow.isel(), then the memory usage is perfectly reasonable.shoyer commentedon Sep 26, 2018
@WeatherGod does adding something like
da = da.chunk({'time': 1})reproduce this with your example?WeatherGod commentedon Sep 26, 2018
No, it does not make a difference. The example above peaks at around 5GB of memory (a bit much, but manageable). And it peaks similarly if we chunk it like you suggested.
jhamman commentedon Sep 27, 2018
@WeatherGod - are you reading data from netCDF files by chance?
If so, can you share the compression/chunk layout for those (
ncdump -h -s file.nc)?WeatherGod commentedon Sep 27, 2018
It would be ten files opened via xr.open_mfdataset() concatenated across a time dimension, each one looking like:
4 remaining items
shoyer commentedon Sep 18, 2019
Yes, I'm seeing similar numbers, about 10x slower indexing in a DataArray. This seems to have gotten slower over time. It would be good to track this down and add a benchmark!
shoyer commentedon Sep 18, 2019
#3319 gives us about a 2x performance boost. It could likely be much faster, but at least this fixes the regression.
crusaderky commentedon Sep 19, 2019
Before #3319:
After #3319:
Good job!
crusaderky commentedon Sep 19, 2019
Can we short-circuit the special case where the index of the array used for slicing is the same object as the index being sliced, so no alignment is needed?
the time spent on that align call could be zero!
dcherian commentedon Sep 19, 2019
I think align tries to optimize that case, so maybe something's also possible there?
shoyer commentedon Sep 19, 2019
Yes, align checks
index.equals(other)first, which has a shortcut for the same object.The real mystery here is why
time_filter.indexes['time']andds.indexes['time']are not the same object. I guess this is likely due to lazy initialization of indexes, and should be fixed eventually by the explicit indexes refactor.Propagate indexes in DataArray binary operations.
Propagate indexes in DataArray binary operations. (#3481)
Hoeze commentedon Nov 26, 2019
Hi, I'd like to understand how
iselworks exactly in conjunction with dask arrays.As it seems, #3481 propagates the
iseloperation onto each dask chunk for lazy evaluation. Is this correct?dcherian commentedon Nov 26, 2019
I don't know much about indexing but that PR propagates a "new" indexes property as part of #1603 (work towards enabling more flexible indexing), it doesn't change anything about "indexing". I think the dask docs may be more relevant to what you may be asking about: https://docs.dask.org/en/latest/array-slicing.html
dschwoerer commentedon Mar 10, 2023
I just changed
to:
And that changed the runtime of my code from (unknown, still running after 3 hours) to around 10 seconds.
ds[k]is a 3 dimensional arrayslc[d]are 7-d numpy array of integersshoyer commentedon Mar 10, 2023
@dschwoerer are you sure that you are actually calculating the same thing in both cases? What exactly do the values of
slc[d]look like? I would test thing on smaller inputs to verify. My guess is that you are inadvertently calculating something different, recalling that Xarray's broadcasting rules differ slightly from NumPy's.dschwoerer commentedon Mar 14, 2023
I see, they are not the same - the slow one is still a dask array, the other one is not:
Otherwise they are the same, so this might be dask related ...
dcherian commentedon Mar 14, 2023
A reproducible example would help but indexing with dask arrays is a bit limited.
On #5873 it's possible it will raise an error and ask you to compute the indexer. Also see dask/dask#4156
EDIT: your slowdown is probably because it's compuing
Snmultiple times. You could speed it up by calling compute once and passing a numpy array toiselJASMIN: optimized ERA5 extraction with netCDF4+numpy (140× speedup)