def _select_edge_sur(self, edges, k):
"""
Select the five cell indices surrounding each edge cell.
"""
i, j = edges[k]['k']
if k == 'n':
return ([i + 0, i + 1, i + 1, i + 1, i + 0],
[j + 1, j + 1, j + 0, j - 1, j - 1])
elif k == 'e':
return ([i - 1, i + 1, i + 1, i + 0, i - 1],
[j + 0, j + 0, j - 1, j - 1, j - 1])
elif k == 's':
return ([i - 1, i - 1, i + 0, i + 0, i - 1],
[j + 0, j + 1, j + 1, j - 1, j - 1])
elif k == 'w':
return ([i - 1, i - 1, i + 0, i + 1, i + 1],
[j + 0, j + 1, j + 1, j + 1, j + 0])
python类indices()的实例源码
def take(self, indices, axis=None, out=None, mode='raise'):
"""
"""
(_data, _mask) = (self._data, self._mask)
cls = type(self)
# Make sure the indices are not masked
maskindices = getattr(indices, '_mask', nomask)
if maskindices is not nomask:
indices = indices.filled(0)
# Get the data
if out is None:
out = _data.take(indices, axis=axis, mode=mode).view(cls)
else:
np.take(_data, indices, axis=axis, mode=mode, out=out)
# Get the mask
if isinstance(out, MaskedArray):
if _mask is nomask:
outmask = maskindices
else:
outmask = _mask.take(indices, axis=axis, mode=mode)
outmask |= maskindices
out.__setmask__(outmask)
return out
# Array methods
def put(a, indices, values, mode='raise'):
"""
Set storage-indexed locations to corresponding values.
This function is equivalent to `MaskedArray.put`, see that method
for details.
See Also
--------
MaskedArray.put
"""
# We can't use 'frommethod', the order of arguments is different
try:
return a.put(indices, values, mode=mode)
except AttributeError:
return narray(a, copy=False).put(indices, values, mode=mode)
def fixOffset(self, offset, img):
size = img.shape
finalImg = np.ndarray(size)
indices = np.indices((self.videoSize[0],self.videoSize[1])).swapaxes(0,2).swapaxes(0,1)
indices = np.around(indices, decimals=1)
indices.shape = (self.videoSize[1] * self.videoSize[0], 2)
phi = 2 * np.arctan(np.exp(indices[:, 1] / self.videoSize[1])) - 1/2 * np.pi - offset[0]
lamb = indices[:, 0] - offset[1]
x = lamb
y = np.log(np.tan(np.pi / 4 + 1/2 * phi)) * self.videoSize[1]
finalIdx = np.ndarray((self.videoSize[1] * self.videoSize[0], 2))
finalIdx = np.around(finalIdx, decimals=1).astype(int)
finalIdx[:, 1] = y % self.videoSize[1]
finalIdx[:, 0] = x % self.videoSize[0]
finalImg[indices[:,1], indices[:,0]] = img[finalIdx[:,1], finalIdx[:,0]]
return finalImg
def test_encode_data_roundtrip():
minrand, maxrand = np.sort(np.random.randint(-427, 8848, 2))
testdata = np.round((np.sum(
np.dstack(
np.indices((512, 512),
dtype=np.float64)),
axis=2) / (511. + 511.)) * maxrand, 2) + minrand
baseval = -1000
interval = 0.1
rtripped = _decode(data_to_rgb(testdata.copy(), baseval, interval), baseval, interval)
assert testdata.min() == rtripped.min()
assert testdata.max() == rtripped.max()
def _parse_mask(mask):
r"""
Interprets a string mask to return the number of coefficients to be kept
and the indices of the first and last ones in the zig-zagged flattened DCT matrix
Example: '1-44' returns 44, first=1, last=44
Parameters
----------
mask
Returns
-------
"""
tmp = mask.split('-')
first = int(tmp[0])
last = int(tmp[1])
ncoeffs = last-first+1
return ncoeffs, first, last
def diff_approx(self, fields, pars, eps=1E-8):
nvar, N = len(fields.dependent_variables), fields.size
fpars = {key: pars[key] for key in self.pars}
fpars['dx'] = (fields['x'][-1] - fields['x'][0]) / fields['x'].size
J = np.zeros((N * nvar, N * nvar))
indices = np.indices(fields.uarray.shape)
for i, (var_index, node_index) in enumerate(zip(*map(np.ravel,
indices))):
fields_plus = fields.copy()
fields_plus.uarray[var_index, node_index] += eps
fields_moins = fields.copy()
fields_moins.uarray[var_index, node_index] -= eps
Fplus = self(fields_plus, pars)
Fmoins = self(fields_moins, pars)
J[i] = (Fplus - Fmoins) / (2 * eps)
return J.T
def continuous_loss(self, y, y_hat):
if isinstance(y_hat, DocLabel):
raise ValueError("continuous loss on discrete input")
if isinstance(y_hat[0], tuple):
y_hat = y_hat[0]
prop_marg, link_marg = y_hat
y_nodes = self.prop_encoder_.transform(y.nodes)
y_links = self.link_encoder_.transform(y.links)
prop_ix = np.indices(y.nodes.shape)
link_ix = np.indices(y.links.shape)
# relies on prop_marg and link_marg summing to 1 row-wise
prop_loss = np.sum(self.prop_cw_[y_nodes] *
(1 - prop_marg[prop_ix, y_nodes]))
link_loss = np.sum(self.link_cw_[y_links] *
(1 - link_marg[link_ix, y_links]))
loss = prop_loss + link_loss
return loss
def find_beam_position_blur(z, sigma=30):
"""Estimate direct beam position by blurring the image with a large
Gaussian kernel and finding the maximum.
Parameters
----------
sigma : float
Sigma value for Gaussian blurring kernel.
Returns
-------
center : np.array
np.array containing indices of estimated direct beam positon.
"""
blurred = ndi.gaussian_filter(z, sigma)
center = np.unravel_index(blurred.argmax(), blurred.shape)
return np.array(center)
def take(self, indices, axis=None, out=None, mode='raise'):
"""
"""
(_data, _mask) = (self._data, self._mask)
cls = type(self)
# Make sure the indices are not masked
maskindices = getattr(indices, '_mask', nomask)
if maskindices is not nomask:
indices = indices.filled(0)
# Get the data
if out is None:
out = _data.take(indices, axis=axis, mode=mode).view(cls)
else:
np.take(_data, indices, axis=axis, mode=mode, out=out)
# Get the mask
if isinstance(out, MaskedArray):
if _mask is nomask:
outmask = maskindices
else:
outmask = _mask.take(indices, axis=axis, mode=mode)
outmask |= maskindices
out.__setmask__(outmask)
return out
# Array methods
def put(a, indices, values, mode='raise'):
"""
Set storage-indexed locations to corresponding values.
This function is equivalent to `MaskedArray.put`, see that method
for details.
See Also
--------
MaskedArray.put
"""
# We can't use 'frommethod', the order of arguments is different
try:
return a.put(indices, values, mode=mode)
except AttributeError:
return narray(a, copy=False).put(indices, values, mode=mode)
def _parse_output(self):
unique_ids = np.unique(self.tags)
counts = np.bincount(self.tags + 1)
sort_indices = np.argsort(self.tags)
grab_indices = np.indices(self.tags.shape).ravel()[sort_indices]
dens = self.densities[sort_indices]
cp = 0
for i in unique_ids:
cp_c = cp + counts[i + 1]
if i == -1:
cp += counts[i + 1]
continue
group_indices = grab_indices[cp:cp_c]
self._groups.append(self._halo_class(self, i, group_indices,
ptype=self.ptype))
md_i = np.argmax(dens[cp:cp_c])
px, py, pz = \
[self.particle_fields['particle_position_%s' % ax][group_indices]
for ax in 'xyz']
self._max_dens[i] = (dens[cp:cp_c][md_i], px[md_i],
py[md_i], pz[md_i])
cp += counts[i + 1]
def _parse_halolist(self, threshold_adjustment):
groups = []
max_dens = {}
hi = 0
LE, RE = self.bounds
for halo in self._groups:
this_max_dens = halo.maximum_density_location()
# if the most dense particle is in the box, keep it
if np.all((this_max_dens >= LE) & (this_max_dens <= RE)):
# Now we add the halo information to OURSELVES, taken from the
# self.hop_list
# We need to mock up the HOPHaloList thingie, so we need to
# set self._max_dens
max_dens_temp = list(self._max_dens[halo.id])[0] / \
threshold_adjustment
max_dens[hi] = [max_dens_temp] + \
list(self._max_dens[halo.id])[1:4]
groups.append(self._halo_class(self, hi, ptype=self.ptype))
groups[-1].indices = halo.indices
self.comm.claim_object(groups[-1])
hi += 1
del self._groups, self._max_dens # explicit >> implicit
self._groups = groups
self._max_dens = max_dens
def test_fill_region():
for level in range(2):
rf = 2**level
output_fields = [np.zeros((NDIM*rf,NDIM*rf,NDIM*rf), "float64")
for i in range(3)]
input_fields = [np.empty(NDIM**3, "float64")
for i in range(3)]
v = np.mgrid[0.0:1.0:NDIM*1j, 0.0:1.0:NDIM*1j, 0.0:1.0:NDIM*1j]
input_fields[0][:] = v[0].ravel()
input_fields[1][:] = v[1].ravel()
input_fields[2][:] = v[2].ravel()
left_index = np.zeros(3, "int64")
ipos = np.empty((NDIM**3, 3), dtype="int64")
ind = np.indices((NDIM,NDIM,NDIM))
ipos[:,0] = ind[0].ravel()
ipos[:,1] = ind[1].ravel()
ipos[:,2] = ind[2].ravel()
ires = np.zeros(NDIM*NDIM*NDIM, "int64")
ddims = np.array([NDIM, NDIM, NDIM], dtype="int64") * rf
fill_region(input_fields, output_fields, level,
left_index, ipos, ires, ddims,
np.array([2, 2, 2], dtype="i8"))
for r in range(level + 1):
for o, i in zip(output_fields, v):
assert_equal( o[r::rf,r::rf,r::rf], i)
def weighted_distances( dx=10, dy=10, c=(5,5)):
'''
Map with weighted distances to a point
args: Dimension maps and point
'''
a = np.zeros((dx,dy))
a[c]=1
indr = np.indices(a.shape)[0,:]
indc = np.indices(a.shape)[1,:]
difr = indr-c[0]
difc = indc-c[1]
map_diff = np.sqrt((difr**2)+(difc**2))
map_diff = 1.0 - (map_diff/ map_diff.flatten().max())
# Return inverse distance map
return map_diff
def moments(data):
total = data.sum()
X, Y = np.indices(data.shape)
x = (X*data).sum()/total
y = (Y*data).sum()/total
col = data[:, int(y)]
width_x = np.sqrt(abs((np.arange(col.size)-y)**2*col).sum()/col.sum())
row = data[int(x), :]
width_y = np.sqrt(abs((np.arange(row.size)-x)**2*row).sum()/row.sum())
height = data.max()
# Return the parameters
return height, x, y, width_x, width_y
# -----------------------------------------------------------------
def moments(data):
total = data.sum()
X, Y = np.indices(data.shape)
x = (X*data).sum()/total
y = (Y*data).sum()/total
col = data[:, int(y)]
width_x = np.sqrt(abs((np.arange(col.size)-y)**2*col).sum()/col.sum())
row = data[int(x), :]
width_y = np.sqrt(abs((np.arange(row.size)-x)**2*row).sum()/row.sum())
height = data.max()
# Return the parameters
return height, x, y, width_x, width_y
# -----------------------------------------------------------------
def compute_dt_stats(self):
self.datestack = True
print("Computing date stats")
allmask = np.ma.getmaskarray(self.ma_stack).all(axis=0)
minidx = np.argmin(np.ma.getmaskarray(self.ma_stack), axis=0)
maxidx = np.argmin(np.ma.getmaskarray(self.ma_stack[::-1]), axis=0)
dt_stack_min = np.zeros(minidx.shape, dtype=self.dtype)
dt_stack_max = np.zeros(maxidx.shape, dtype=self.dtype)
for n, dt_o in enumerate(self.date_list_o):
dt_stack_min[minidx == n] = dt_o
dt_stack_max[maxidx == (len(self.date_list_o)-1 - n)] = dt_o
self.dt_stack_min = np.ma.array(dt_stack_min, mask=allmask)
self.dt_stack_max = np.ma.array(dt_stack_max, mask=allmask)
self.dt_stack_ptp = np.ma.masked_equal((self.dt_stack_max - self.dt_stack_min), 0)
self.dt_stack_center = self.dt_stack_min + self.dt_stack_ptp.filled(0)/2.0
#Should pull out unmasked indices at each pixel along axis 0
#Take min index along axis 0
#Then create grids by pulling out corresponding value from date_list_o
core.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def take(self, indices, axis=None, out=None, mode='raise'):
"""
"""
(_data, _mask) = (self._data, self._mask)
cls = type(self)
# Make sure the indices are not masked
maskindices = getattr(indices, '_mask', nomask)
if maskindices is not nomask:
indices = indices.filled(0)
# Get the data
if out is None:
out = _data.take(indices, axis=axis, mode=mode).view(cls)
else:
np.take(_data, indices, axis=axis, mode=mode, out=out)
# Get the mask
if isinstance(out, MaskedArray):
if _mask is nomask:
outmask = maskindices
else:
outmask = _mask.take(indices, axis=axis, mode=mode)
outmask |= maskindices
out.__setmask__(outmask)
return out
# Array methods
core.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def put(a, indices, values, mode='raise'):
"""
Set storage-indexed locations to corresponding values.
This function is equivalent to `MaskedArray.put`, see that method
for details.
See Also
--------
MaskedArray.put
"""
# We can't use 'frommethod', the order of arguments is different
try:
return a.put(indices, values, mode=mode)
except AttributeError:
return narray(a, copy=False).put(indices, values, mode=mode)
def take(self, indices, axis=None, out=None, mode='raise'):
"""
"""
(_data, _mask) = (self._data, self._mask)
cls = type(self)
# Make sure the indices are not masked
maskindices = getattr(indices, '_mask', nomask)
if maskindices is not nomask:
indices = indices.filled(0)
# Get the data
if out is None:
out = _data.take(indices, axis=axis, mode=mode).view(cls)
else:
np.take(_data, indices, axis=axis, mode=mode, out=out)
# Get the mask
if isinstance(out, MaskedArray):
if _mask is nomask:
outmask = maskindices
else:
outmask = _mask.take(indices, axis=axis, mode=mode)
outmask |= maskindices
out.__setmask__(outmask)
return out
# Array methods
def put(a, indices, values, mode='raise'):
"""
Set storage-indexed locations to corresponding values.
This function is equivalent to `MaskedArray.put`, see that method
for details.
See Also
--------
MaskedArray.put
"""
# We can't use 'frommethod', the order of arguments is different
try:
return a.put(indices, values, mode=mode)
except AttributeError:
return narray(a, copy=False).put(indices, values, mode=mode)
def _parse_halolist(self, threshold_adjustment):
groups = []
max_dens = {}
hi = 0
LE, RE = self.bounds
for halo in self._groups:
this_max_dens = halo.maximum_density_location()
# if the most dense particle is in the box, keep it
if np.all((this_max_dens >= LE) & (this_max_dens <= RE)):
# Now we add the halo information to OURSELVES, taken from the
# self.hop_list
# We need to mock up the HOPHaloList thingie, so we need to
# set self._max_dens
max_dens_temp = list(self._max_dens[halo.id])[0] / \
threshold_adjustment
max_dens[hi] = [max_dens_temp] + \
list(self._max_dens[halo.id])[1:4]
groups.append(self._halo_class(self, hi, ptype=self.ptype))
groups[-1].indices = halo.indices
self.comm.claim_object(groups[-1])
hi += 1
del self._groups, self._max_dens # explicit >> implicit
self._groups = groups
self._max_dens = max_dens
def put(a, indices, values, mode='raise'):
"""
Set storage-indexed locations to corresponding values.
This function is equivalent to `MaskedArray.put`, see that method
for details.
See Also
--------
MaskedArray.put
"""
# We can't use 'frommethod', the order of arguments is different
try:
return a.put(indices, values, mode=mode)
except AttributeError:
return narray(a, copy=False).put(indices, values, mode=mode)
def put(a, indices, values, mode='raise'):
"""
Set storage-indexed locations to corresponding values.
This function is equivalent to `MaskedArray.put`, see that method
for details.
See Also
--------
MaskedArray.put
"""
# We can't use 'frommethod', the order of arguments is different
try:
return a.put(indices, values, mode=mode)
except AttributeError:
return narray(a, copy=False).put(indices, values, mode=mode)
def transform_to_2d(data, max_axis):
"""
Projects 3d data cube along one axis using maximum intensity with
preservation of the signs. Adapted from nilearn.
"""
import numpy as np
# get the shape of the array we are projecting to
new_shape = list(data.shape)
del new_shape[max_axis]
# generate a 3D indexing array that points to max abs value in the
# current projection
a1, a2 = np.indices(new_shape)
inds = [a1, a2]
inds.insert(max_axis, np.abs(data).argmax(axis=max_axis))
# take the values where the absolute value of the projection
# is the highest
maximum_intensity_data = data[inds]
return np.rot90(maximum_intensity_data)
def gaussian_image(label):
label = tf.reshape(label, [-1, 2])
indices = np.indices([368, 368])[:, ::8, ::8].astype(np.float32)
coords = tf.constant(indices)
stretch = tf.reshape(tf.to_float(label), [-1, 2, 1, 1])
stretch = tf.tile(stretch, [1, 1, 46, 46])
# pdf = 1.0/(np.sqrt(2*(sigma**2)*np.pi)) * tf.exp(-tf.pow(coords-stretch,2)/(2*sigma**2))
pdf = tf.pow(coords - stretch, 2) / (2 * sigma ** 2)
pdf = tf.reduce_sum(pdf, [1])
# pdf = tf.reduce_prod(pdf,[1])
# print debug
pdf = tf.expand_dims(pdf, 3)
debug = tf.exp(-pdf) # 1.0 / (np.sqrt(2 * (sigma ** 2) * np.pi)) *
pdf_debug_img('super', debug, sigma)
return debug
def endmembers_by_query(rast, query, gt, wkt, dd=False):
'''
Returns a list of endmember locations based on a provided query, e.g.:
> query = rast[1,...] < -25 # Band 2 should be less than -25
> endmembers_by_query(rast, query, gt, wkt)
Arguments:
rast The raster array to find endmembers within
query A NumPy boolean array representing a query in the feature space
gt The GDAL GeoTransform
wkt The GDAL WKT projection
dd True for coordinates in decimal degrees
'''
assert isinstance(rast, np.ndarray), 'Requires a NumPy array'
shp = rast.shape
idx = np.indices((shp[-2], shp[-1]))
# Execute query on the indices (pixel locations), then return the coordinates
return list(pixel_to_xy([
(x, y) for y, x in idx[:,query].T
], gt, wkt, dd=dd))
def mae(reference, predictions, idx=None, n=1):
'''
Mean absolute error (MAE) for (p x n) raster arrays, where p is the number
of bands and n is the number of pixels. Arguments:
reference Raster array of reference ("truth" or measured) data
predictions Raster array of predictions
idx Optional array of indices at which to sample the arrays
n A normalizing constant for residuals; e.g., the number
of endmembers when calculating RMSE for modeled reflectance
'''
if idx is None:
r = reference.shape[1]
residuals = reference - predictions
else:
r = len(idx)
residuals = reference[:, idx] - predictions[:, idx]
# Divide the MSE by the number of bands before taking the root
return np.apply_along_axis(lambda x: np.divide(np.abs(x).sum(), n), 0,
residuals)
def measure_background(image, Fibers, width=30, niter=3, order=3):
t = []
a,b = image.shape
ygrid,xgrid = np.indices(image.shape)
ygrid = 1. * ygrid.ravel() / a
xgrid = 1. * xgrid.ravel() / b
image = image.ravel()
s = np.arange(a*b)
for fiber in Fibers:
t.append(fiber.D*fiber.yind + fiber.xind)
t = np.hstack(t)
t = np.array(t, dtype=int)
ind = np.setdiff1d(s,t)
mask = np.zeros((a*b))
mask[ind] = 1.
mask[ind] = 1.-is_outlier(image[ind])
sel = np.where(mask==1.)[0]
for i in xrange(niter):
V = polyvander2d(xgrid[sel],ygrid[sel],[order,order])
sol = np.linalg.lstsq(V, image[sel])[0]
vals = np.dot(V,sol) - image[sel]
sel = sel[~is_outlier(vals)]
V = polyvander2d(xgrid,ygrid,[order,order])
back = np.dot(V, sol).reshape(a,b)
return back