Commit 8059ad4e authored by Martin Reinecke's avatar Martin Reinecke

cleanup 2/n

parent a76ff674
Pipeline #18034 passed with stage
in 4 minutes and 57 seconds
...@@ -46,7 +46,7 @@ class DirectSmoothingOperator(EndomorphicOperator): ...@@ -46,7 +46,7 @@ class DirectSmoothingOperator(EndomorphicOperator):
# ---Added properties and methods--- # ---Added properties and methods---
def _precompute(self, x, sigma, dxmax=None): def _precompute(self, x):
""" Does precomputations for Gaussian smoothing on a 1D irregular grid. """ Does precomputations for Gaussian smoothing on a 1D irregular grid.
Parameters Parameters
...@@ -54,10 +54,6 @@ class DirectSmoothingOperator(EndomorphicOperator): ...@@ -54,10 +54,6 @@ class DirectSmoothingOperator(EndomorphicOperator):
x: 1D floating point array or list containing the individual grid x: 1D floating point array or list containing the individual grid
positions. Points must be given in ascending order. positions. Points must be given in ascending order.
sigma: The sigma of the Gaussian with which the function living on x
should be smoothed, in the same units as x.
dxmax: (optional) The maximum distance up to which smoothing is
performed, in the same units as x. Default is 3.01*sigma.
Returns Returns
------- -------
...@@ -72,8 +68,7 @@ class DirectSmoothingOperator(EndomorphicOperator): ...@@ -72,8 +68,7 @@ class DirectSmoothingOperator(EndomorphicOperator):
normalized smoothing weights. normalized smoothing weights.
""" """
if dxmax is None: dxmax = self._effective_smoothing_width*self._sigma
dxmax = self._effective_smoothing_width*sigma
x = np.asarray(x) x = np.asarray(x)
...@@ -81,7 +76,7 @@ class DirectSmoothingOperator(EndomorphicOperator): ...@@ -81,7 +76,7 @@ class DirectSmoothingOperator(EndomorphicOperator):
nval = np.searchsorted(x, x+dxmax) - ibegin nval = np.searchsorted(x, x+dxmax) - ibegin
wgt = [] wgt = []
expfac = 1. / (2. * sigma*sigma) expfac = 1. / (2. * self._sigma*self._sigma)
for i in range(x.size): for i in range(x.size):
if nval[i] > 0: if nval[i] > 0:
t = x[ibegin[i]:ibegin[i]+nval[i]]-x[i] t = x[ibegin[i]:ibegin[i]+nval[i]]-x[i]
...@@ -94,12 +89,8 @@ class DirectSmoothingOperator(EndomorphicOperator): ...@@ -94,12 +89,8 @@ class DirectSmoothingOperator(EndomorphicOperator):
return ibegin, nval, wgt return ibegin, nval, wgt
def _apply_kernel_along_array(self, power, startindex, endindex, def _apply_kernel_along_array(self, power, startindex, endindex,
distances, smooth_length, smoothing_width,
ibegin, nval, wgt): ibegin, nval, wgt):
if smooth_length == 0.0:
return power[startindex:endindex]
p_smooth = np.zeros(endindex-startindex, dtype=power.dtype) p_smooth = np.zeros(endindex-startindex, dtype=power.dtype)
for i in range(startindex, endindex): for i in range(startindex, endindex):
imin = max(startindex, ibegin[i]) imin = max(startindex, ibegin[i])
...@@ -109,12 +100,10 @@ class DirectSmoothingOperator(EndomorphicOperator): ...@@ -109,12 +100,10 @@ class DirectSmoothingOperator(EndomorphicOperator):
return p_smooth return p_smooth
def _apply_along_axis(self, axis, arr, startindex, endindex, distances, def _apply_along_axis(self, axis, arr, startindex, endindex, distances):
smooth_length, smoothing_width):
nd = arr.ndim nd = arr.ndim
ibegin, nval, wgt = self._precompute( ibegin, nval, wgt = self._precompute(distances)
distances, smooth_length, smooth_length*smoothing_width)
ind = np.zeros(nd-1, dtype=np.int) ind = np.zeros(nd-1, dtype=np.int)
i = np.zeros(nd, dtype=object) i = np.zeros(nd, dtype=object)
...@@ -130,8 +119,7 @@ class DirectSmoothingOperator(EndomorphicOperator): ...@@ -130,8 +119,7 @@ class DirectSmoothingOperator(EndomorphicOperator):
holdshape = outshape holdshape = outshape
slicedArr = arr[tuple(i.tolist())] slicedArr = arr[tuple(i.tolist())]
res = self._apply_kernel_along_array( res = self._apply_kernel_along_array(
slicedArr, startindex, endindex, distances, slicedArr, startindex, endindex, ibegin, nval, wgt)
smooth_length, smoothing_width, ibegin, nval, wgt)
outshape = np.asarray(arr.shape) outshape = np.asarray(arr.shape)
outshape[axis] = endindex - startindex outshape[axis] = endindex - startindex
...@@ -149,8 +137,7 @@ class DirectSmoothingOperator(EndomorphicOperator): ...@@ -149,8 +137,7 @@ class DirectSmoothingOperator(EndomorphicOperator):
i.put(indlist, ind) i.put(indlist, ind)
slicedArr = arr[tuple(i.tolist())] slicedArr = arr[tuple(i.tolist())]
res = self._apply_kernel_along_array( res = self._apply_kernel_along_array(
slicedArr, startindex, endindex, distances, slicedArr, startindex, endindex, ibegin, nval, wgt)
smooth_length, smoothing_width, ibegin, nval, wgt)
outarr[tuple(i.tolist())] = res outarr[tuple(i.tolist())] = res
k += 1 k += 1
...@@ -158,9 +145,9 @@ class DirectSmoothingOperator(EndomorphicOperator): ...@@ -158,9 +145,9 @@ class DirectSmoothingOperator(EndomorphicOperator):
def _smooth(self, x, spaces): def _smooth(self, x, spaces):
# infer affected axes # infer affected axes
# we rely on the knowledge, that `spaces` is a tuple with length 1. # we rely on the knowledge that `spaces` is a tuple with length 1.
affected_axes = x.domain_axes[spaces[0]] affected_axes = x.domain_axes[spaces[0]]
if len(affected_axes) > 1: if len(affected_axes) != 1:
raise ValueError("By this implementation only one-dimensional " raise ValueError("By this implementation only one-dimensional "
"spaces can be smoothed directly.") "spaces can be smoothed directly.")
affected_axis = affected_axes[0] affected_axis = affected_axes[0]
...@@ -174,6 +161,4 @@ class DirectSmoothingOperator(EndomorphicOperator): ...@@ -174,6 +161,4 @@ class DirectSmoothingOperator(EndomorphicOperator):
x.val, x.val,
startindex=0, startindex=0,
endindex=x.shape[affected_axis], endindex=x.shape[affected_axis],
distances=distance_array, distances=distance_array)
smooth_length=self._sigma,
smoothing_width=self._effective_smoothing_width)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment