-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return a 3D object alongside 1D object in apply_ufunc #8695
Comments
If |
It's indeterministic, or it's decided inside apply_ufunc def _help_downsample(data, time, n_out):
indices = MinMaxLTTBDownsampler().downsample(time, data, n_out=n_out)
return data[indices], indices
def apply_downsample(ts_ds, n_out):
ts_ds_downsampled, indices = xr.apply_ufunc(
_help_downsample,
ts_ds["data"],
ts_ds["time"],
kwargs=dict(n_out=n_out),
input_core_dims=[["time"], ["time"]],
output_core_dims=[["time"], ["indices"]],
exclude_dims=set(("time",)),
vectorize=True,
dask="parallelized",
dask_gufunc_kwargs=dict(output_sizes={"time": n_out, "indices": n_out}),
)
ts_ds_downsampled["time"] = ts_ds["time"].isel(time=indices.values[0])
return ts_ds_downsampled.rename("data") |
If |
You're right. I suppose I misunderstood; I thought it was stacking all the channels to calculate the indices. Perhaps what I really want to do is just the following without apply_ufunc
|
I've run in to this before. There's no way to limit the broadcasting behaviour, so if there are broadcast dimensions on the input variables, those are assumed to be the broadcast dimensions on all output variables. I believe the best solution is #1618 . In the mean time if you don't need |
Is there a way to do this without a loop? import numpy as np
t0 = [0, 1, 2]
t1 = [3, 4, 5]
ts = [t0, t1]
z = np.array([[6, 7, 8], [9, 10, 11]])
ds = xr.DataArray(z, dims=["channel", "time"]).to_dataset("channel")
for i, channel in enumerate(ds.data_vars):
dim = f"time_{i}"
ds[channel] = (dim, ds[channel].values)
ds[channel] = ds[channel].assign_coords({dim: ts[i]})
ds |
If the ds = xr.DataArray(
z,
dims=["channel", "t_idx"],
coords={"time": (("channel", "t_idx"), ts)}
) |
Is your feature request related to a problem?
Currently, I have something similar to this, where the
input_lat
is transformed tonew_lat
(here, +0.25, but in real use case, it's indeterministic).Since
xarray_ufunc
doesn't return a dataset with actual coordinates values, I had to return a second output to retain new_lat to properly update the coordinate values, but this second output is shapedtime, lat, lon
so I have tods["lat"] = new_lat.isel(lon=0, time=0).values
, which I think is inefficient; I simply need it to be shapedlat
.Any ideas on how I can modify this to make it more efficient?
Describe the solution you'd like
Either be able to automatically assign the new_lat to the returned xarray object, or allow a 1D dataset to be returned
Describe alternatives you've considered
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: