Lets say I have the simple array:
data = [1,1,0,0,1,1,1]
I can apply labeling to this data with the scipy ndimage module with:
groups, _ = sp.ndimage.label(data)
Resulting in
In [68]: print(groups)
[1 1 0 0 2 2 2]
Now, I would like to do the same labeling function on a xarray DataArray.
xr_data = xr.DataArray([1,1,0,0,1,1,1], coords = [("x", [0,1,2,3,4,5,6])])
I know I could call the same function as before on the xr_data
, but the output of doing this call is a numpy array, which in my actual dataset, is too large to fit in memory.
It seems like the xr.apply_ufunc
function is what I need. However, I am having trouble getting it to work.
def xr_label(arr):
return xr.apply_ufunc(sp.ndimage.label, arr)
xr_groups, _ = xr_label(xr_data)
This results in: "ValueError: applied function returned data with unexpected number of dimensions. Received 0 dimension(s) but expected 1 dimensions with names: ('x',)"
I'm finding the documentation on the apply_ufunc method difficult to interpret. Can someone help me out with this?