Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ix1, ix2 = np.mgrid[-winw:winw + 1, -winw:winw + 1]
Win = np.exp(-(ix1 ** 2 * Cinv[0, 0] + ix2 ** 2 * Cinv[1, 1] + 2 * Cinv[1, 0] * ix1 * ix2) / 2)
Win /= np.sum(Win)
logging.debug('time 2D binning and bandwidth: %s ; bins: %s', time.time() - start, fine_bins_2D)
start = time.time()
cache = {}
convolvesize = xsize + 2 * winw + Win.shape[0]
bins2D = convolve2D(histbins, Win, 'same', largest_size=convolvesize, cache=cache)
if meanlikes:
bin2Dlikes = convolve2D(finebinlikes, Win, 'same', largest_size=convolvesize, cache=cache, cache_args=[2])
if mult_bias_correction_order:
ix = bin2Dlikes > 0
finebinlikes[ix] /= bin2Dlikes[ix]
likes2 = convolve2D(finebinlikes, Win, 'same', largest_size=convolvesize, cache=cache, cache_args=[2])
likes2[ix] *= bin2Dlikes[ix]
bin2Dlikes = likes2
del finebinlikes
mx = 1e-4 * np.max(bins2D)
bin2Dlikes[bins2D > mx] /= bins2D[bins2D > mx]
bin2Dlikes[bins2D <= mx] = 0
else:
bin2Dlikes = None
if has_prior and boundary_correction_order >= 0:
# Correct for edge effects
prior_mask = np.ones((ysize + 2 * winw, xsize + 2 * winw))
self._setEdgeMask2D(parx, pary, prior_mask, winw)
a00 = convolve2D(prior_mask, Win, 'valid', largest_size=convolvesize, cache=cache)
ix = a00 * bins2D > np.max(bins2D) * 1e-8
a00 = a00[ix]
self._setEdgeMask2D(parx, pary, prior_mask, winw)
a00 = convolve2D(prior_mask, Win, 'valid', largest_size=convolvesize, cache=cache)
ix = a00 * bins2D > np.max(bins2D) * 1e-8
a00 = a00[ix]
normed = bins2D[ix] / a00
if boundary_correction_order == 1:
# linear boundary correction
indexes = np.arange(-winw, winw + 1)
y = np.empty(Win.shape)
for i in range(Win.shape[0]):
y[:, i] = indexes
winx = Win * indexes
winy = Win * y
a10 = convolve2D(prior_mask, winx, 'valid', largest_size=convolvesize, cache=cache)[ix]
a01 = convolve2D(prior_mask, winy, 'valid', largest_size=convolvesize, cache=cache)[ix]
a20 = convolve2D(prior_mask, winx * indexes, 'valid', largest_size=convolvesize, cache=cache,
cache_args=[1])[ix]
a02 = convolve2D(prior_mask, winy * y, 'valid', largest_size=convolvesize, cache=cache,
cache_args=[1])[ix]
a11 = convolve2D(prior_mask, winy * indexes, 'valid', largest_size=convolvesize, cache=cache,
cache_args=[1])[ix]
xP = convolve2D(histbins, winx, 'same', largest_size=convolvesize, cache=cache)[ix]
yP = convolve2D(histbins, winy, 'same', largest_size=convolvesize, cache=cache)[ix]
denom = (a20 * a01 ** 2 + a10 ** 2 * a02 - a00 * a02 * a20 + a11 ** 2 * a00 - 2 * a01 * a10 * a11)
A = a11 ** 2 - a02 * a20
Ax = a10 * a02 - a01 * a11
Ay = a01 * a20 - a10 * a11
corrected = (bins2D[ix] * A + xP * Ax + yP * Ay) / denom
bins2D[ix] = normed * np.exp(np.minimum(corrected / normed, 4) - 1)
elif boundary_correction_order == 0:
# simple boundary correction by normalization
bins2D[ix] = normed
winw = int(round(2.5 * smooth_scale))
Cinv = np.linalg.inv(np.array([[ry ** 2, rx * ry * corr], [rx * ry * corr, rx ** 2]]))
ix1, ix2 = np.mgrid[-winw:winw + 1, -winw:winw + 1]
Win = np.exp(-(ix1 ** 2 * Cinv[0, 0] + ix2 ** 2 * Cinv[1, 1] + 2 * Cinv[1, 0] * ix1 * ix2) / 2)
Win /= np.sum(Win)
logging.debug('time 2D binning and bandwidth: %s ; bins: %s', time.time() - start, fine_bins_2D)
start = time.time()
cache = {}
convolvesize = xsize + 2 * winw + Win.shape[0]
bins2D = convolve2D(histbins, Win, 'same', largest_size=convolvesize, cache=cache)
if meanlikes:
bin2Dlikes = convolve2D(finebinlikes, Win, 'same', largest_size=convolvesize, cache=cache, cache_args=[2])
if mult_bias_correction_order:
ix = bin2Dlikes > 0
finebinlikes[ix] /= bin2Dlikes[ix]
likes2 = convolve2D(finebinlikes, Win, 'same', largest_size=convolvesize, cache=cache, cache_args=[2])
likes2[ix] *= bin2Dlikes[ix]
bin2Dlikes = likes2
del finebinlikes
mx = 1e-4 * np.max(bins2D)
bin2Dlikes[bins2D > mx] /= bins2D[bins2D > mx]
bin2Dlikes[bins2D <= mx] = 0
else:
bin2Dlikes = None
if has_prior and boundary_correction_order >= 0:
# Correct for edge effects
prior_mask = np.ones((ysize + 2 * winw, xsize + 2 * winw))
# Correct for edge effects
prior_mask = np.ones((ysize + 2 * winw, xsize + 2 * winw))
self._setEdgeMask2D(parx, pary, prior_mask, winw)
a00 = convolve2D(prior_mask, Win, 'valid', largest_size=convolvesize, cache=cache)
ix = a00 * bins2D > np.max(bins2D) * 1e-8
a00 = a00[ix]
normed = bins2D[ix] / a00
if boundary_correction_order == 1:
# linear boundary correction
indexes = np.arange(-winw, winw + 1)
y = np.empty(Win.shape)
for i in range(Win.shape[0]):
y[:, i] = indexes
winx = Win * indexes
winy = Win * y
a10 = convolve2D(prior_mask, winx, 'valid', largest_size=convolvesize, cache=cache)[ix]
a01 = convolve2D(prior_mask, winy, 'valid', largest_size=convolvesize, cache=cache)[ix]
a20 = convolve2D(prior_mask, winx * indexes, 'valid', largest_size=convolvesize, cache=cache,
cache_args=[1])[ix]
a02 = convolve2D(prior_mask, winy * y, 'valid', largest_size=convolvesize, cache=cache,
cache_args=[1])[ix]
a11 = convolve2D(prior_mask, winy * indexes, 'valid', largest_size=convolvesize, cache=cache,
cache_args=[1])[ix]
xP = convolve2D(histbins, winx, 'same', largest_size=convolvesize, cache=cache)[ix]
yP = convolve2D(histbins, winy, 'same', largest_size=convolvesize, cache=cache)[ix]
denom = (a20 * a01 ** 2 + a10 ** 2 * a02 - a00 * a02 * a20 + a11 ** 2 * a00 - 2 * a01 * a10 * a11)
A = a11 ** 2 - a02 * a20
Ax = a10 * a02 - a01 * a11
Ay = a01 * a20 - a10 * a11
corrected = (bins2D[ix] * A + xP * Ax + yP * Ay) / denom
bins2D[ix] = normed * np.exp(np.minimum(corrected / normed, 4) - 1)
elif boundary_correction_order == 0:
indexes = np.arange(-winw, winw + 1)
y = np.empty(Win.shape)
for i in range(Win.shape[0]):
y[:, i] = indexes
winx = Win * indexes
winy = Win * y
a10 = convolve2D(prior_mask, winx, 'valid', largest_size=convolvesize, cache=cache)[ix]
a01 = convolve2D(prior_mask, winy, 'valid', largest_size=convolvesize, cache=cache)[ix]
a20 = convolve2D(prior_mask, winx * indexes, 'valid', largest_size=convolvesize, cache=cache,
cache_args=[1])[ix]
a02 = convolve2D(prior_mask, winy * y, 'valid', largest_size=convolvesize, cache=cache,
cache_args=[1])[ix]
a11 = convolve2D(prior_mask, winy * indexes, 'valid', largest_size=convolvesize, cache=cache,
cache_args=[1])[ix]
xP = convolve2D(histbins, winx, 'same', largest_size=convolvesize, cache=cache)[ix]
yP = convolve2D(histbins, winy, 'same', largest_size=convolvesize, cache=cache)[ix]
denom = (a20 * a01 ** 2 + a10 ** 2 * a02 - a00 * a02 * a20 + a11 ** 2 * a00 - 2 * a01 * a10 * a11)
A = a11 ** 2 - a02 * a20
Ax = a10 * a02 - a01 * a11
Ay = a01 * a20 - a10 * a11
corrected = (bins2D[ix] * A + xP * Ax + yP * Ay) / denom
bins2D[ix] = normed * np.exp(np.minimum(corrected / normed, 4) - 1)
elif boundary_correction_order == 0:
# simple boundary correction by normalization
bins2D[ix] = normed
else:
raise SettingError('unknown boundary_correction_order (expected 0 or 1)')
if mult_bias_correction_order:
prior_mask = np.ones((ysize + 2 * winw, xsize + 2 * winw))
self._setEdgeMask2D(parx, pary, prior_mask, winw, alledge=True)
a00 = convolve2D(prior_mask, Win, 'valid', largest_size=convolvesize, cache=cache, cache_args=[2])
prior_mask = np.ones((ysize + 2 * winw, xsize + 2 * winw))
self._setEdgeMask2D(parx, pary, prior_mask, winw)
a00 = convolve2D(prior_mask, Win, 'valid', largest_size=convolvesize, cache=cache)
ix = a00 * bins2D > np.max(bins2D) * 1e-8
a00 = a00[ix]
normed = bins2D[ix] / a00
if boundary_correction_order == 1:
# linear boundary correction
indexes = np.arange(-winw, winw + 1)
y = np.empty(Win.shape)
for i in range(Win.shape[0]):
y[:, i] = indexes
winx = Win * indexes
winy = Win * y
a10 = convolve2D(prior_mask, winx, 'valid', largest_size=convolvesize, cache=cache)[ix]
a01 = convolve2D(prior_mask, winy, 'valid', largest_size=convolvesize, cache=cache)[ix]
a20 = convolve2D(prior_mask, winx * indexes, 'valid', largest_size=convolvesize, cache=cache,
cache_args=[1])[ix]
a02 = convolve2D(prior_mask, winy * y, 'valid', largest_size=convolvesize, cache=cache,
cache_args=[1])[ix]
a11 = convolve2D(prior_mask, winy * indexes, 'valid', largest_size=convolvesize, cache=cache,
cache_args=[1])[ix]
xP = convolve2D(histbins, winx, 'same', largest_size=convolvesize, cache=cache)[ix]
yP = convolve2D(histbins, winy, 'same', largest_size=convolvesize, cache=cache)[ix]
denom = (a20 * a01 ** 2 + a10 ** 2 * a02 - a00 * a02 * a20 + a11 ** 2 * a00 - 2 * a01 * a10 * a11)
A = a11 ** 2 - a02 * a20
Ax = a10 * a02 - a01 * a11
Ay = a01 * a20 - a10 * a11
corrected = (bins2D[ix] * A + xP * Ax + yP * Ay) / denom
bins2D[ix] = normed * np.exp(np.minimum(corrected / normed, 4) - 1)
elif boundary_correction_order == 0:
# simple boundary correction by normalization
bins2D[ix] = normed * np.exp(np.minimum(corrected / normed, 4) - 1)
elif boundary_correction_order == 0:
# simple boundary correction by normalization
bins2D[ix] = normed
else:
raise SettingError('unknown boundary_correction_order (expected 0 or 1)')
if mult_bias_correction_order:
prior_mask = np.ones((ysize + 2 * winw, xsize + 2 * winw))
self._setEdgeMask2D(parx, pary, prior_mask, winw, alledge=True)
a00 = convolve2D(prior_mask, Win, 'valid', largest_size=convolvesize, cache=cache, cache_args=[2])
for _ in range(mult_bias_correction_order):
box = histbins.copy()
ix2 = bins2D > np.max(bins2D) * 1e-8
box[ix2] /= bins2D[ix2]
bins2D *= convolve2D(box, Win, 'same', largest_size=convolvesize, cache=cache, cache_args=[2])
bins2D /= a00
x = np.linspace(xbinmin, xbinmax, xsize)
y = np.linspace(ybinmin, ybinmax, ysize)
density = Density2D(x, y, bins2D,
view_ranges=[(parx.range_min, parx.range_max), (pary.range_min, pary.range_max)])
density.normalize('max', in_place=True)
if get_density:
return density
ncontours = len(self.contours)
if num_plot_contours: ncontours = min(num_plot_contours, ncontours)
contours = self.contours[:ncontours]
logging.debug('time 2D convolutions: %s', time.time() - start)
denom = (a20 * a01 ** 2 + a10 ** 2 * a02 - a00 * a02 * a20 + a11 ** 2 * a00 - 2 * a01 * a10 * a11)
A = a11 ** 2 - a02 * a20
Ax = a10 * a02 - a01 * a11
Ay = a01 * a20 - a10 * a11
corrected = (bins2D[ix] * A + xP * Ax + yP * Ay) / denom
bins2D[ix] = normed * np.exp(np.minimum(corrected / normed, 4) - 1)
elif boundary_correction_order == 0:
# simple boundary correction by normalization
bins2D[ix] = normed
else:
raise SettingError('unknown boundary_correction_order (expected 0 or 1)')
if mult_bias_correction_order:
prior_mask = np.ones((ysize + 2 * winw, xsize + 2 * winw))
self._setEdgeMask2D(parx, pary, prior_mask, winw, alledge=True)
a00 = convolve2D(prior_mask, Win, 'valid', largest_size=convolvesize, cache=cache, cache_args=[2])
for _ in range(mult_bias_correction_order):
box = histbins.copy()
ix2 = bins2D > np.max(bins2D) * 1e-8
box[ix2] /= bins2D[ix2]
bins2D *= convolve2D(box, Win, 'same', largest_size=convolvesize, cache=cache, cache_args=[2])
bins2D /= a00
x = np.linspace(xbinmin, xbinmax, xsize)
y = np.linspace(ybinmin, ybinmax, ysize)
density = Density2D(x, y, bins2D,
view_ranges=[(parx.range_min, parx.range_max), (pary.range_min, pary.range_max)])
density.normalize('max', in_place=True)
if get_density:
return density
ncontours = len(self.contours)