def __sub__(self, other): """Subtract two maps. Currently does not take into account the alignment between the two maps. numpy dtype nums: 1 int8 2 uint8 3 int16 4 uint16 """ # if data is stored as unsigned, cast up (e.g. uint8 => int16) if self.dtype.kind == "u": self = self.astype(to_signed(self.dtype)) if other.dtype.kind == "u": other = other.astype(to_signed(other.dtype)) result = np.ndarray.__sub__(self, other) def norm(): mean = result.mean() std = result.std() vmin = max(result.min(), mean - 6 * std) vmax = min(result.max(), mean + 6 * std) return colors.Normalize(vmin, vmax) result.norm = norm result.cmap = cm.gray # @UndefinedVariable return result
def test_to_signed(): """ This should return a signed type that can hold uint32 and ensure that an exception is raised when attempting to convert an unsigned 64 bit integer to an integer """ assert util.to_signed(np.dtype('uint32')) == np.dtype('int64') with pytest.raises(ValueError): util.to_signed(np.dtype('uint64')) == np.dtype('int64')
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.astype(to_signed(self.dtype)) # Subtract average value from every frequency channel. tmp = (data - np.average(self, 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) return bg.reshape(self.shape[0], 1)
def auto_find_background(self, amount=0.05): # pylint: disable=E1101,E1103 data = self.astype(to_signed(self.dtype)) # Subtract average value from every frequency channel. tmp = (data - np.average(self, 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)))]
def auto_const_bg(self): """ Automatically determine background. """ # pylint: disable=E1101,E1103 data = self.astype(to_signed(self.dtype)) # Subtract average value from every frequency channel. tmp = (data - np.average(self, 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[0]), 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[:, realcand], 1) return bg.reshape(self.shape[0], 1)
def test_to_signed(): """ This should return a signed type that can hold uint32. """ assert util.to_signed(np.dtype('uint32')) == np.dtype('int64')