Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def randomized_auto_const_bg(self, amount):
""" Automatically determine background. Only consider a randomly
chosen subset of the image.
Parameters
----------
amount : int
Size of random sample that is considered for calculation of
the background.
"""
cols = [randint(0, self.shape[1] - 1) for _ in xrange(amount)]
# pylint: disable=E1101,E1103
data = self.data.astype(to_signed(self.dtype))
# Subtract average value from every frequency channel.
tmp = (data - np.average(self.data, 1).reshape(self.shape[0], 1))
# Get standard deviation at every point of time.
# Need to convert because otherwise this class's __getitem__
# is used which assumes two-dimensionality.
tmp = tmp[:, cols]
sdevs = np.asarray(np.std(tmp, 0))
# Get indices of values with lowest standard deviation.
cand = sorted(xrange(amount), key=lambda y: sdevs[y])
# Only consider the best 5 %.
realcand = cand[:max(1, int(0.05 * len(cand)))]
# Average the best 5 %
bg = np.average(self[:, [cols[r] for r in realcand]], 1)
def auto_find_background(self, amount=0.05):
# pylint: disable=E1101,E1103
data = self.data.astype(to_signed(self.dtype))
# Subtract average value from every frequency channel.
tmp = (data - np.average(self.data, 1).reshape(self.shape[0], 1))
# Get standard deviation at every point of time.
# Need to convert because otherwise this class's __getitem__
# is used which assumes two-dimensionality.
sdevs = np.asarray(np.std(tmp, 0))
# Get indices of values with lowest standard deviation.
cand = sorted(xrange(self.shape[1]), key=lambda y: sdevs[y])
# Only consider the best 5 %.
return cand[:max(1, int(amount * len(cand)))]