additionally, when I try to specify the output_core_dims argument to be [['time', 'x', 'y']] to correct this, I get an error, it looks like you can't have a dimension be both an input core dimension and an output core dimension[67]bin_arr = np.linspace(configs.rmin, configs.rmax, 50)blues = t_series['reflectance'].sel(band=1).chunk({'time':-1})result = xr.apply_ufunc(partial(np.digitize, bins=bin_arr), blues, input_core_dims=[['time']], output_core_dims=[['time','x','y']], dask="parallelized", output_dtypes=[blues.dtype])---------------------------------------------------------------------------ValueError Traceback (most recent call last) in 5 bin_arr = np.linspace(configs.rmin, configs.rmax, 50) 6 blues = t_series['reflectance'].sel(band=1).chunk({'time':-1})----> 7 result = xr.apply_ufunc(partial(np.digitize, bins=bin_arr), blues, input_core_dims=[['time']], output_core_dims=[['time','x','y']], dask="parallelized", output_dtypes=[blues.dtype])~/miniconda3/envs/pyatsa/lib/python3.7/site-packages/xarray/core/computation.py in apply_ufunc(func, input_core_dims, output_core_dims, exclude_dims, vectorize, join, dataset_join, dataset_fill_value, keep_attrs, kwargs, dask, output_dtypes, output_sizes, *args) 967 join=join, 968 exclude_dims=exclude_dims,--> 969 keep_attrs=keep_attrs) 970 elif any(isinstance(a, Variable) for a in args): 971 return variables_vfunc(*args)~/miniconda3/envs/pyatsa/lib/python3.7/site-packages/xarray/core/computation.py in apply_dataarray_vfunc(func, signature, join, exclude_dims, keep_attrs, *args) 215 216 data_vars = [getattr(a, 'variable', a) for a in args]--> 217 result_var = func(*data_vars) 218 219 if signature.num_outputs > 1:~/miniconda3/envs/pyatsa/lib/python3.7/site-packages/xarray/core/computation.py in apply_variable_ufunc(func, signature, exclude_dims, dask, output_dtypes, output_sizes, keep_attrs, *args) 539 if isinstance(arg, Variable) 540 else arg--> 541 for arg, core_dims in zip(args, signature.input_core_dims)] 542 543 if any(isinstance(array, dask_array_type) for array in input_data):~/miniconda3/envs/pyatsa/lib/python3.7/site-packages/xarray/core/computation.py in (.0) 539 if isinstance(arg, Variable) 540 else arg--> 541 for arg, core_dims in zip(args, signature.input_core_dims)] 542 543 if any(isinstance(array, dask_array_type) for array in input_data):~/miniconda3/envs/pyatsa/lib/python3.7/site-packages/xarray/core/computation.py in broadcast_compat_data(variable, broadcast_dims, core_dims) 493 'dimensions %r on an input variable: these are core ' 494 'dimensions on other input or output variables'--> 495 % unexpected_dims) 496 497 # for consistency with numpy, keep broadcast dimensions to the leftValueError: operand to apply_ufunc encountered unexpected dimensions ['y', 'x'] on an input variable: these are core dimensions on other input or output variables我们非常感谢您的帮助,我想了解我是如何滥用input_core_dim和output_core_dim参数的.Any help is greatly appreciated, I'd like to understand how I'm misusing the input_core_dim and output_core_dim arguments.推荐答案您要逐点应用digitize.这是apply_ufunc的最简单的用例.不需要特殊参数.You want to apply digitize on a point-by-point basis. This is the easiest possible use case for apply_ufunc. No special arguments are required.import numpy as npimport xarray as xrny, nx = 100, 100nt = 44data = xr.DataArray(np.random.randn(nt,ny,nx), dims=['time', 'y', 'x'], name='blue reflectance')rmin, rmax, nbins = -4, 4, 50bins = np.linspace(rmin, rmax, nbins)data_digitized = xr.apply_ufunc(np.digitize, data, bins)这将返回一个类似的DataArray This returns a DataArray like<xarray.DataArray 'blue reflectance' (time: 44, y: 100, x: 100)>array([[[34, 17, ..., 27, 15], .... [21, 24, ..., 23, 29]]])Dimensions without coordinates: time, y, x其中的值是bin索引,根据 numpy.digitize 文档.where the values are the bin indexes, according to the conventions described in the numpy.digitize docs.要使它懒散地在dask数组上运行,您有两个选择To make this operate lazily on dask arrays, you have two options# create chunked dask version of datadata_chunked = data.chunk({'time': 1})# use dask's version of digitizeimport dask.array as daxr.apply_ufunc(da.digitize, data_chunked, bins, dask='allowed')# use xarray's built-in `parallelized` option on the numpy function# (I needed to define a wrapper function to make this work,# but I don't fully understand why.)def wrap_digitize(data): return np.digitize(data, bins)xr.apply_ufunc(wrap_digitize, data_chunked, dask='parallelized', output_dtypes=['i8']) 这篇关于如何在xarray.DataArray的时间维度上对每个图像使用apply_ufunc和numpy.digitize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 10:15