issues: 170779798
This data as json
id | node_id | number | title | user | state | locked | assignee | milestone | comments | created_at | updated_at | closed_at | author_association | active_lock_reason | draft | pull_request | body | reactions | performed_via_github_app | state_reason | repo | type |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
170779798 | MDExOlB1bGxSZXF1ZXN0ODEwNTQ4MTQ= | 964 | New function for applying vectorized functions for unlabeled arrays to xarray objects | 1217238 | closed | 0 | 33 | 2016-08-12T01:00:06Z | 2018-12-13T16:45:07Z | 2017-01-06T00:36:05Z | MEMBER | 0 | pydata/xarray/pulls/964 | This PR creates new public facing function Note that although we use the gufunc interface here, this works for far more than gufuncs. Any function that handles broadcasting in the usual numpy way will do. See below for examples. Now that this logic is all in one place, we will even be able to (in a follow-up PR) include hooks for setting output array names and attributes based on input (e.g., to allow third party libraries to add unit support #525). Xref #770 ExamplesCalculate the vector magnitude of two arguments:
Compute the mean (
Inner product over a specific dimension:: ``` def gufunc_inner(x, y): result = np.matmul(x[..., np.newaxis, :], y[..., :, np.newaxis]) return result[..., 0, 0] def inner_product(a, b, dim): sig = ([(dim,), (dim,)], [()]) return xr.apply_ufunc(gufunc_inner, a, b, signature=sig) ``` Stack objects along a new dimension (like
Singular value decomposition: ``` def dim_shape(obj, dim): # TODO: make this unnecessary, see #921 try: return obj.dims.index(dim) except AttributeError: return obj.dims[dim] def svd(obj, dim0, dim1, new_dim='singular_values'): sig = ([(dim0, dim1)], [(dim0, new_dim), (new_dim,), (new_dim, dim1)]) K = min(dim_shape(obj, dim0), dim_shape(obj, dim1)) new_coords = [{new_dim: np.arange(K)}] * 3 return xr.apply_ufunc(np.linalg.svd, obj, signature=sig, new_coords=new_coords, kwargs={'full_matrices': False}) ``` Signature/Docstring``` apply_ufunc(func, *args, signature=None, join='inner', new_coords=None, exclude_dims=frozenset(), dataset_fill_value=None, kwargs=None, dask_array='forbidden') Apply a vectorized function for unlabeled arrays to xarray objects. The input arguments will be handled using xarray's standard rules for labeled computation, including alignment, broadcasting, looping over GroupBy/Dataset variables, and merging of coordinates. Parametersfunc : callable
Function to call like
join : {'outer', 'inner', 'left', 'right'}, optional
Method for joining the indexes of the passed objects along each
dimension, and the variables of Dataset objects with mismatched
data variables:
- 'outer': use the union of object indexes
- 'inner': use the intersection of object indexes
- 'left': use indexes from the first object with each dimension
- 'right': use indexes from the last object with each dimension
new_coords : list of dict-like, optional
New coordinates to include on each output variable. Any core dimensions
on outputs not found on the inputs must be provided here.
exclude_dims : set, optional
Dimensions to exclude from alignment and broadcasting. Any inputs
coordinates along these dimensions will be dropped. If you include
these dimensions on any outputs, you must explicit set them in
ReturnsSingle value or tuple of Dataset, DataArray, Variable, dask.array.Array or numpy.ndarray, the first type on that list to appear on an input. ``` |
{ "url": "https://api.github.com/repos/pydata/xarray/issues/964/reactions", "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
13221727 | pull |