本文整理匯總了Python中dask.array.map_blocks方法的典型用法代碼示例。如果您正苦於以下問題:Python array.map_blocks方法的具體用法?Python array.map_blocks怎麽用?Python array.map_blocks使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類dask.array
的用法示例。
在下文中一共展示了array.map_blocks方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: atm_variables_finder
# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import map_blocks [as 別名]
def atm_variables_finder(mus, muv, phi, height, tau, tO3, tH2O, taustep4sphalb, tO2=1.0):
tau_step = da.linspace(taustep4sphalb, MAXNUMSPHALBVALUES * taustep4sphalb, MAXNUMSPHALBVALUES,
chunks=int(MAXNUMSPHALBVALUES / 2))
sphalb0 = csalbr(tau_step)
taur = tau * da.exp(-height / SCALEHEIGHT)
rhoray, trdown, trup = chand(phi, muv, mus, taur)
if isinstance(height, xr.DataArray):
sphalb = da.map_blocks(_sphalb_index, (taur / taustep4sphalb + 0.5).astype(np.int32).data, sphalb0.compute(),
dtype=sphalb0.dtype)
else:
sphalb = sphalb0[(taur / taustep4sphalb + 0.5).astype(np.int32)]
Ttotrayu = ((2 / 3. + muv) + (2 / 3. - muv) * trup) / (4 / 3. + taur)
Ttotrayd = ((2 / 3. + mus) + (2 / 3. - mus) * trdown) / (4 / 3. + taur)
TtotraytH2O = Ttotrayu * Ttotrayd * tH2O
tOG = tO3 * tO2
return sphalb, rhoray, TtotraytH2O, tOG
示例2: interpolate_angles
# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import map_blocks [as 別名]
def interpolate_angles(self, angles, resolution):
# FIXME: interpolate in cartesian coordinates if the lons or lats are
# problematic
from geotiepoints.multilinear import MultilinearInterpolator
geocoding = self.root.find('.//Tile_Geocoding')
rows = int(geocoding.find('Size[@resolution="' + str(resolution) + '"]/NROWS').text)
cols = int(geocoding.find('Size[@resolution="' + str(resolution) + '"]/NCOLS').text)
smin = [0, 0]
smax = np.array(angles.shape) - 1
orders = angles.shape
minterp = MultilinearInterpolator(smin, smax, orders)
minterp.set_values(da.atleast_2d(angles.ravel()))
x = da.arange(rows, dtype=angles.dtype, chunks=CHUNK_SIZE) / (rows-1) * (angles.shape[0] - 1)
y = da.arange(cols, dtype=angles.dtype, chunks=CHUNK_SIZE) / (cols-1) * (angles.shape[1] - 1)
xcoord, ycoord = da.meshgrid(x, y)
return da.map_blocks(self._do_interp, minterp, xcoord, ycoord, dtype=angles.dtype, chunks=xcoord.chunks)
示例3: interpolate_xarray_linear
# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import map_blocks [as 別名]
def interpolate_xarray_linear(xpoints, ypoints, values, shape, chunks=CHUNK_SIZE):
"""Interpolate linearly, generating a dask array."""
from scipy.interpolate.interpnd import (LinearNDInterpolator,
_ndim_coords_from_arrays)
if isinstance(chunks, (list, tuple)):
vchunks, hchunks = chunks
else:
vchunks, hchunks = chunks, chunks
points = _ndim_coords_from_arrays(np.vstack((np.asarray(ypoints),
np.asarray(xpoints))).T)
interpolator = LinearNDInterpolator(points, values)
grid_x, grid_y = da.meshgrid(da.arange(shape[1], chunks=hchunks),
da.arange(shape[0], chunks=vchunks))
# workaround for non-thread-safe first call of the interpolator:
interpolator((0, 0))
res = da.map_blocks(intp, grid_x, grid_y, interpolator=interpolator)
return DataArray(res, dims=('y', 'x'))
示例4: aggregate
# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import map_blocks [as 別名]
def aggregate(d, y_size, x_size):
"""Average every 4 elements (2x2) in a 2D array."""
if d.ndim != 2:
# we can't guarantee what blocks we are getting and how
# it should be reshaped to do the averaging.
raise ValueError("Can't aggregrate (reduce) data arrays with "
"more than 2 dimensions.")
if not (x_size.is_integer() and y_size.is_integer()):
raise ValueError("Aggregation factors are not integers")
for agg_size, chunks in zip([y_size, x_size], d.chunks):
for chunk_size in chunks:
if chunk_size % agg_size != 0:
raise ValueError("Aggregation requires arrays with "
"shapes and chunks divisible by the "
"factor")
new_chunks = (tuple(int(x / y_size) for x in d.chunks[0]),
tuple(int(x / x_size) for x in d.chunks[1]))
return da.core.map_blocks(_mean, d, y_size, x_size, dtype=d.dtype, chunks=new_chunks)
示例5: detrend_wrap
# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import map_blocks [as 別名]
def detrend_wrap(detrend_func):
"""
Wrapper function for `xrft.detrendn`.
"""
def func(a, axes=None):
if len(axes) > 3:
raise ValueError("Detrending is only supported up to "
"3 dimensions.")
if axes is None:
axes = tuple(range(a.ndim))
else:
if len(set(axes)) < len(axes):
raise ValueError("Duplicate axes are not allowed.")
for each_axis in axes:
if len(a.chunks[each_axis]) != 1:
raise ValueError('The axis along the detrending is upon '
'cannot be chunked.')
if len(axes) == 1:
return dsar.map_blocks(sps.detrend, a, axis=axes[0],
chunks=a.chunks, dtype=a.dtype
)
else:
for each_axis in range(a.ndim):
if each_axis not in axes:
if len(a.chunks[each_axis]) != a.shape[each_axis]:
raise ValueError("The axes other than ones to detrend "
"over should have a chunk length of 1.")
return dsar.map_blocks(detrend_func, a, axes,
chunks=a.chunks, dtype=a.dtype
)
return func
示例6: _compute_var_image_xarray_dask
# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import map_blocks [as 別名]
def _compute_var_image_xarray_dask(src_var: xr.DataArray,
dst_src_ij_images: np.ndarray,
fill_value: Union[int, float, complex] = np.nan) -> da.Array:
"""Extract source pixels from xarray.DataArray source with dask.array.Array data"""
return da.map_blocks(_compute_var_image_xarray_dask_block,
src_var.values,
dst_src_ij_images,
fill_value,
dtype=src_var.dtype,
drop_axis=0)
示例7: HaloVelocityDispersion
# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import map_blocks [as 別名]
def HaloVelocityDispersion(mass, cosmo, redshift, mdef='vir'):
""" Compute the velocity dispersion of halo from Mass.
This is a simple model suggested by Martin White.
See http://adsabs.harvard.edu/abs/2008ApJ...672..122E
"""
mass, redshift = da.broadcast_arrays(mass, redshift)
def compute_vdisp(mass, redshift):
h = cosmo.efunc(redshift)
return 1100. * (h * mass / 1e15) ** 0.33333
return da.map_blocks(compute_vdisp, mass, redshift, dtype=mass.dtype)
示例8: wrap_map_blocks
# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import map_blocks [as 別名]
def wrap_map_blocks(self, func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
with set_backend(self._inner):
return da.map_blocks(self._wrap_current_state(func), *args, **kwargs)
return wrapped
示例9: __ua_convert__
# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import map_blocks [as 別名]
def __ua_convert__(self, value, dispatch_type, coerce):
if dispatch_type is not ufunc and value is None:
return None
if dispatch_type is ndarray:
if not coerce and not isinstance(value, da.Array):
return NotImplemented
ret = da.asarray(value)
with set_backend(self._inner):
ret = ret.map_blocks(self._wrap_current_state(unumpy.asarray))
return ret
return value
示例10: _background_removal_dog
# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import map_blocks [as 別名]
def _background_removal_dog(dask_array, **kwargs):
"""Background removal using difference of Gaussians.
Parameters
----------
dask_array : Dask array
At least 2 dimensions
min_sigma : float
max_sigma : float
Returns
-------
output_array : Dask array
Same dimensions as dask_array
Examples
--------
>>> import pyxem.utils.dask_tools as dt
>>> import dask.array as da
>>> s = pxm.dummy_data.dummy_data.get_cbed_signal()
>>> dask_array = da.from_array(s.data, chunks=(5,5,25, 25))
>>> s_rem = pxm.Diffraction2D(
... dt._background_removal_dog(dask_array))
"""
dask_array_rechunked = _rechunk_signal2d_dim_one_chunk(dask_array)
output_array = da.map_blocks(
_background_removal_chunk_dog, dask_array_rechunked, dtype=np.float32, **kwargs
)
return output_array
示例11: _background_removal_median
# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import map_blocks [as 別名]
def _background_removal_median(dask_array, **kwargs):
"""Background removal using median filter.
Parameters
----------
dask_array : Dask array
Must be at least 2 dimensions
footprint : float
Returns
-------
output_array : Dask array
Same dimensions as dask_array
Examples
--------
>>> import pyxem.utils.dask_tools as dt
>>> import dask.array as da
>>> s = pxm.dummy_data.dummy_data.get_cbed_signal()
>>> dask_array = da.from_array(s.data+1e3, chunks=(5,5,25, 25))
>>> s_rem = pxm.Diffraction2D(
... dt._background_removal_median(dask_array), footprint=20)
"""
dask_array_rechunked = _rechunk_signal2d_dim_one_chunk(dask_array)
output_array = da.map_blocks(
_background_removal_chunk_median,
dask_array_rechunked,
dtype=np.float32,
**kwargs
)
return output_array
示例12: regrid_dask
# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import map_blocks [as 別名]
def regrid_dask(self, indata):
"""See __call__()."""
extra_chunk_shape = indata.chunksize[0:-2]
output_chunk_shape = extra_chunk_shape + self.shape_out
outdata = da.map_blocks(
self.regrid_numpy,
indata,
dtype=float,
chunks=output_chunk_shape
)
return outdata
示例13: _get_indices
# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import map_blocks [as 別名]
def _get_indices(self):
"""Calculate projection indices.
Returns
-------
x_idxs : Dask array
X indices of the target grid where the data are put
y_idxs : Dask array
Y indices of the target grid where the data are put
"""
LOG.info("Determine bucket resampling indices")
# Transform source lons/lats to target projection coordinates x/y
lons = self.source_lons.ravel()
lats = self.source_lats.ravel()
result = da.map_blocks(self._get_proj_coordinates, lons, lats,
new_axis=0, chunks=(2,) + lons.chunks)
proj_x = result[0, :]
proj_y = result[1, :]
# Calculate array indices. Orient so that 0-meridian is pointing down.
adef = self.target_area
x_res, y_res = adef.resolution
x_idxs = da.floor((proj_x - adef.area_extent[0]) / x_res).astype(np.int)
y_idxs = da.floor((adef.area_extent[3] - proj_y) / y_res).astype(np.int)
# Get valid index locations
mask = ((x_idxs >= 0) & (x_idxs < adef.width) &
(y_idxs >= 0) & (y_idxs < adef.height))
self.y_idxs = da.where(mask, y_idxs, -1)
self.x_idxs = da.where(mask, x_idxs, -1)
# Convert X- and Y-indices to raveled indexing
target_shape = self.target_area.shape
self.idxs = self.y_idxs * target_shape[1] + self.x_idxs
示例14: aggregate
# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import map_blocks [as 別名]
def aggregate(self, **dims):
"""Aggregate the current swath definition by averaging.
For example, averaging over 2x2 windows:
`sd.aggregate(x=2, y=2)`
"""
import pyproj
import dask.array as da
geocent = pyproj.Proj(proj='geocent')
latlong = pyproj.Proj(proj='latlong')
res = da.map_blocks(self._do_transform, latlong, geocent,
self.lons.data, self.lats.data,
da.zeros_like(self.lons.data), new_axis=[2],
chunks=(self.lons.chunks[0], self.lons.chunks[1], 3))
res = DataArray(res, dims=['y', 'x', 'coord'], coords=self.lons.coords)
res = res.coarsen(**dims).mean()
lonlatalt = da.map_blocks(self._do_transform, geocent, latlong,
res[:, :, 0].data, res[:, :, 1].data,
res[:, :, 2].data, new_axis=[2],
chunks=res.data.chunks)
lons = DataArray(lonlatalt[:, :, 0], dims=self.lons.dims,
coords=res.coords, attrs=self.lons.attrs.copy())
lats = DataArray(lonlatalt[:, :, 1], dims=self.lons.dims,
coords=res.coords, attrs=self.lons.attrs.copy())
try:
resolution = lons.attrs['resolution'] * ((dims.get('x', 1) + dims.get('y', 1)) / 2)
lons.attrs['resolution'] = resolution
lats.attrs['resolution'] = resolution
except KeyError:
pass
return SwathDefinition(lons, lats)
示例15: expand_angle_and_nav
# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import map_blocks [as 別名]
def expand_angle_and_nav(self, arrays):
"""Expand angle and navigation datasets."""
res = []
for array in arrays:
res.append(da.map_blocks(self.expand, array[:, :, np.newaxis], self.expansion_coefs,
dtype=array.dtype, drop_axis=2, chunks=self.expansion_coefs.chunks[:-1]))
return res