home / github / issue_comments

Menu
  • Search all tables
  • GraphQL API

issue_comments: 570056960

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions performed_via_github_app issue
https://github.com/pydata/xarray/issues/3656#issuecomment-570056960 https://api.github.com/repos/pydata/xarray/issues/3656 570056960 MDEyOklzc3VlQ29tbWVudDU3MDA1Njk2MA== 14808389 2020-01-01T14:19:00Z 2020-01-01T14:19:00Z MEMBER

the reduce function needs to be a function that takes a 4D array (time x lat x lon x window in your case). It should use the elements of the window dimension (1 x 1 x 1 x 59) to calculate a time x lat x lon array.

The only thing wrong with your function is that it ignores the axis parameter: it can only work on 1D arrays (with a shape of 59 in this case).

A simple albeit horribly slow way to use your function without rewriting it would be numpy.apply_along_axis (slow because the function is called time * lat * lon == 20995200 times): python spi.rolling(time=59).reduce(lambda x, axis: np.apply_along_axis(get_grps, axis, x))

I'm not too familiar with writing numpy reduce functions so you might want to wait for the opinion of someone more experienced.

I started to rewrite your function using xarray.DataArray instead of pandas.Series, but I don't have time to finish that right now. Hopefully this points you in the right direction. python In [32]: axis = 3 ...: thresh = -1 ...: Nmin = 2 ...: # function code starts here ...: dim_names = ["time", "lat", "lon"] ...: # fails if axis == -1 but axis from reduce is always a positive number ...: dim_names.insert(axis, "window") ...: # use s = xr.DataArray(s, dims=dim_names) in the function ...: # so we can interactively write and try the function code ...: s = spi.rolling(time=59).construct("window") ...: shifted = xr.concat([s.shift({"window": i}) for i in range(Nmin)], dim="shifted") ...: thresholded = shifted <= thresh ...: # xarray does not support `ufunc.reduce`, so we need to fall back to numpy arrays ...: m = r.copy(data=np.logical_and.reduce(thresholded.values, axis=0)) ...: if Nmin > 1: ...: m = m.where(~m).ffill(limit=Nmin - 1, dim="window").fillna(False) ...: gps = m != m.shift({"window": 1}).cumsum().where(m)

{
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  543825272
Powered by Datasette · Queries took 0.619ms · About: xarray-datasette