def test_gauss2D_sym(self): """Testing if the symmetrical case is a sub case of the full one""" coefs_full = [12, 20, 70, 20, 20, 0, 2] full_data = Gauss2D.gauss2D((self.xx, self.yy), *coefs_full) coefs_sym = coefs_full[:4] + [coefs_full[-1]] sym_data = Gauss2D.gauss2D_sym((self.xx, self.yy), *coefs_sym) assert np.all(sym_data == full_data)
def test_gauss2D_norot(self): """Testing if the no rotation case is a sub case of the full one""" coefs_full = [12, 20, 70, 20, 30, 0, 2] full_data = Gauss2D.gauss2D((self.xx, self.yy), *coefs_full) coefs_norot = coefs_full[:5] + [coefs_full[-1]] norot_data = Gauss2D.gauss2D_norot((self.xx, self.yy), *coefs_norot) assert np.all(norot_data == full_data)
def test_unequal_range(self): """Making sure that a ValueError is thrown for unequal ranges""" x = np.arange(64) y = np.arange(64) xx, yy = np.meshgrid(x, y) coefs_full = [12, 20, 70, 20, 30, 0.5, 2] with pytest.raises(RuntimeError): Gauss2D.gauss2D((self.xx, yy), *coefs_full) with pytest.raises(RuntimeError): Gauss2D.gauss2D((xx, self.yy), *coefs_full)
def _test(self): # pull proper data and coefs coefs = self.coefs[modeltype] data = self.data[modeltype] # add noise if snr: if fittype == "ls": amp = coefs[0] noise_data = (amp / snr) * RNG.normal(size=data.shape) noisy_data = data + noise_data # this is a heuristic and should be replaced with a better # reasone parameter rtol = 1 / snr elif fittype == "mle": noisy_data = RNG.poisson(data) rtol = 1 / coefs[0] else: noisy_data = data rtol = 1e-7 # build object to test test_g = Gauss2D(noisy_data) # speficify guesses if not guess: guess_coefs = None else: guess_coefs = coefs.copy() # grab optimized coefs test_coefs = test_g.optimize_params(guess_params=guess_coefs, modeltype=modeltype, fittype=fittype) # do the actual test assert_allclose(test_coefs, coefs, rtol=rtol)
def setUp(self): """Set up testing fixtures, they'll be generated on the fly because this is a small model""" amp, x0, y0, sigma_x, sigma_y, rho, offset = self._make_coefs() # make full gt_coefs_full = np.array((amp, x0, y0, sigma_x, sigma_y, rho, offset)) gt_full = Gauss2D.model((self.xx, self.yy), *gt_coefs_full) # make norot gt_coefs_norot = np.array((amp, x0, y0, sigma_x, sigma_y, offset)) gt_norot = Gauss2D.model((self.xx, self.yy), *gt_coefs_norot) # make sym gt_coefs_sym = np.array((amp, x0, y0, sigma_x, offset)) gt_sym = Gauss2D.model((self.xx, self.yy), *gt_coefs_sym) # make internal dictionaries for tests to use. self.coefs = dict(sym=gt_coefs_sym, norot=gt_coefs_norot, full=gt_coefs_full) self.data = dict(sym=gt_sym, norot=gt_norot, full=gt_full)
def _test(self): area = self.gauss.area() model = self.gauss.fit_model numeric_area = model.sum() assert_allclose(area, numeric_area, err_msg="Failed with GT params") assert_allclose( Gauss2D(model).area(guess_params=self.gauss._popt), numeric_area, err_msg="Failed when optimizing params", )
def test_model(self): """Test model classmethod in Gauss2D class""" # first test acceptable values for goodness funcs = (Gauss2D.gauss2D, Gauss2D.gauss2D_norot, Gauss2D.gauss2D_sym) coefs = ([12, 20, 70, 20, 30, 0.5, 2], [12, 20, 70, 20, 30, 2], [12, 20, 70, 20, 2]) for func, coef in zip(funcs, coefs): data = func((self.xx, self.yy), *coef) model = Gauss2D.model((self.xx, self.yy), *coef) assert np.all(model == data) # now test edge cases with pytest.raises(ValueError): Gauss2D.model(1) with pytest.raises(ValueError): Gauss2D.model(*np.arange(10))
def test_gen_model(self): """Test model""" params = np.array([1, 70, 25, 20, 10, 0.5, 1]) myg = Gauss2D.gen_model(self.noisy_data, *params) assert_allclose(self.raw_data, myg)
def setUp(self): # make x, y ranges self.x = np.arange(64) self.y = np.arange(128) # make meshgrid self.xx, self.yy = np.meshgrid(self.x, self.y) self.noisy_data = tif.imread( os.path.join(os.path.dirname(__file__), "..", "fixtures", "noisy_data.tif")) self.raw_data = tif.imread( os.path.join(os.path.dirname(__file__), "..", "fixtures", "raw_data.tif")) self.myg = Gauss2D(self.noisy_data)
def test_model(self): """Test model classmethod in Gauss2D class""" # first test acceptable values for goodness funcs = (Gauss2D.gauss2D, Gauss2D.gauss2D_norot, Gauss2D.gauss2D_sym) coefs = ([12, 20, 70, 20, 30, 0.5, 2], [12, 20, 70, 20, 30, 2], [12, 20, 70, 20, 2]) for func, coef in zip(funcs, coefs): data = func((self.xx, self.yy), *coef) model = Gauss2D.model((self.xx, self.yy), *coef) assert np.all(model == data) # now test edge cases assert_raises(ValueError, Gauss2D.model, 1) assert_raises(ValueError, Gauss2D.model, *np.arange(10))
def setUp(self): """Basic set up""" data = np.zeros((2001, 2001)) self.gauss = Gauss2D(data)