def test_stats(): # distributionStats # weightedMeanVar values = [0.3,0.5,2.4,6.1,5.6,3.2,1.8,5.0,2.2,-4.5,10.2,-0.5,numpy.nan] weights = [1,1,2,3,3,2,2,3,2,0,4,0,0] x = splat.distributionStats(values) assert isinstance(x,numpy.ndarray) assert_allclose(x[1],numpy.nanmedian(values)) x1 = splat.distributionStats(values,sigma=1.5) assert x[0] >= x1[0] assert x[-1] <= x1[-1] assert x[1] == x1[1] x2 = splat.distributionStats(values,q=[0.25,0.5,0.75]) assert x[0] <= x2[0] assert x[-1] >= x2[-1] assert x[1] == x2[1] x3 = splat.distributionStats(values,weights=weights) assert x[0] <= x3[0] assert x[-1] <= x3[-1] assert x[1] <= x3[1] weights0 = numpy.ones(len(weights)) x = splat.weightedMeanVar(values,weights0) assert isinstance(x,tuple) x2 = splat.weightedMeanVar(values,weights) assert x2[0] > x[0] assert x2[-1] < x[-1]
def test_mixed_array_parameters_1d_array_input(self): """ When given an array input it must be broadcastable with all the parameters. """ t = TModel_1_1([[[0.01, 0.02, 0.03], [0.04, 0.05, 0.06]], [[0.07, 0.08, 0.09], [0.10, 0.11, 0.12]]], [1, 2, 3]) y1 = t([10, 20, 30]) assert np.shape(y1) == (2, 2, 3) assert_allclose(y1, [[[11.01, 22.02, 33.03], [11.04, 22.05, 33.06]], [[11.07, 22.08, 33.09], [11.10, 22.11, 33.12]]]) y2 = t([[[[10]]], [[[20]]], [[[30]]]]) assert np.shape(y2) == (3, 2, 2, 3) assert_allclose(y2, [[[[11.01, 12.02, 13.03], [11.04, 12.05, 13.06]], [[11.07, 12.08, 13.09], [11.10, 12.11, 13.12]]], [[[21.01, 22.02, 23.03], [21.04, 22.05, 23.06]], [[21.07, 22.08, 23.09], [21.10, 22.11, 23.12]]], [[[31.01, 32.02, 33.03], [31.04, 32.05, 33.06]], [[31.07, 32.08, 33.09], [31.10, 32.11, 33.12]]]])
def test_Model_array_parameter(): m = NonFittableModel([[42, 43], [1,2]]) m = NonFittableModel([42, 43, 44, 45]) phi, theta, psi = 42, 43, 44 model = models.RotateNative2Celestial(phi, theta, psi) assert_allclose(model.param_sets, [[42], [43], [44]])
def test_process_sunshine(self): data = [-2.0, -1.0, 0, 1.0, 2.0] qual = [15, 15, 15, 15, 15] proc, invalidData = self.chain.process(data, qual) self._checkValidData(proc, invalidData) assert_allclose(proc, [-1.0, -0.5, 0, 0.5, 1])
def test_p1_nset_npset(self): """N data sets, N param_sets, Polynomial1D""" p1 = models.Polynomial1D(4, n_models=2) y1 = p1(np.array([self.x1, self.x1]).T, model_set_axis=-1) assert y1.shape == (2, 20) assert_allclose(y1[0, :], y1[1, :], atol=10 ** (-12))
def test_param_cov(self): """ Tests that the 'param_cov' fit_info entry gets the right answer for *linear* least squares, where the answer is exact """ a = 2 b = 100 with NumpyRNGContext(_RANDOM_SEED): x = np.linspace(0, 1, 100) # y scatter is amplitude ~1 to make sure covarience is # non-negligible y = x*a + b + np.random.randn(len(x)) #first compute the ordinary least squares covariance matrix X = np.matrix(np.vstack([x, np.ones(len(x))]).T) beta = np.linalg.inv(X.T * X) * X.T * np.matrix(y).T s2 = np.sum((y - (X * beta).A.ravel())**2) / (len(y) - len(beta)) olscov = np.linalg.inv(X.T * X) * s2 #now do the non-linear least squares fit mod = models.Linear1D(a, b) fitter = LevMarLSQFitter() fmod = fitter(mod, x, y) assert_allclose(fmod.parameters, beta.A.ravel()) assert_allclose(olscov, fitter.fit_info['param_cov'])
def test_binom_conf_interval(): # Test Wilson and Jeffreys interval for corner cases: # Corner cases: k = 0, k = n, conf = 0., conf = 1. n = 5 k = [0, 4, 5] for conf in [0., 0.5, 1.]: res = funcs.binom_conf_interval(k, n, conf=conf, interval='wilson') assert ((res >= 0.) & (res <= 1.)).all() res = funcs.binom_conf_interval(k, n, conf=conf, interval='jeffreys') assert ((res >= 0.) & (res <= 1.)).all() # Test Jeffreys interval accuracy against table in Brown et al. (2001). # (See `binom_conf_interval` docstring for reference.) k = [0, 1, 2, 3, 4] n = 7 conf = 0.95 result = funcs.binom_conf_interval(k, n, conf=conf, interval='jeffreys') table = np.array([[0.000, 0.016, 0.065, 0.139, 0.234], [0.292, 0.501, 0.648, 0.766, 0.861]]) assert_allclose(result, table, atol=1.e-3, rtol=0.) # Test Wald interval result = funcs.binom_conf_interval(0, 5, interval='wald') assert_allclose(result, 0.) # conf interval is [0, 0] when k = 0 result = funcs.binom_conf_interval(5, 5, interval='wald') assert_allclose(result, 1.) # conf interval is [1, 1] when k = n result = funcs.binom_conf_interval(500, 1000, conf=0.68269, interval='wald') assert_allclose(result[0], 0.5 - 0.5 / np.sqrt(1000.)) assert_allclose(result[1], 0.5 + 0.5 / np.sqrt(1000.))
def test_dimensionless_angles(): # test that the angles_dimensionless option allows one to change # by any order in radian in the unit (#1161) rad1 = u.dimensionless_angles() assert u.radian.to(1, equivalencies=rad1) == 1. assert u.deg.to(1, equivalencies=rad1) == u.deg.to(u.rad) assert u.steradian.to(1, equivalencies=rad1) == 1. assert u.dimensionless_unscaled.to(u.steradian, equivalencies=rad1) == 1. # now quantities assert (1.*u.radian).to(1, equivalencies=rad1).value == 1. assert (1.*u.deg).to(1, equivalencies=rad1).value == u.deg.to(u.rad) assert (1.*u.steradian).to(1, equivalencies=rad1).value == 1. # more complicated example I = 1.e45 * u.g * u.cm**2 Omega = u.cycle / (1.*u.s) Erot = 0.5 * I * Omega**2 # check that equivalency makes this work Erot_in_erg1 = Erot.to(u.erg, equivalencies=rad1) # and check that value is correct assert_allclose(Erot_in_erg1.value, (Erot/u.radian**2).to(u.erg).value) # test build-in equivalency in subclass class MyRad1(u.Quantity): _equivalencies = rad1 phase = MyRad1(1., u.cycle) assert phase.to(1).value == u.cycle.to(u.radian)
def test_spectraldensity3(): # Define F_nu in Jy f_nu = u.Jy # Define F_lambda in ergs / cm^2 / s / micron f_lambda = u.erg / u.cm ** 2 / u.s / u.micron # 1 GHz one_ghz = u.Quantity(1, u.GHz) # Convert to ergs / cm^2 / s / Hz assert_allclose(f_nu.to(u.erg / u.cm ** 2 / u.s / u.Hz, 1.), 1.e-23, 10) # Convert to ergs / cm^2 / s at 10 Ghz assert_allclose(f_nu.to(u.erg / u.cm ** 2 / u.s, 1., equivalencies=u.spectral_density(one_ghz * 10)), 1.e-13, 10) # Convert to F_lambda at 1 Ghz assert_allclose(f_nu.to(f_lambda, 1., equivalencies=u.spectral_density(one_ghz)), 3.335640951981521e-20, 10) # Convert to Jy at 1 Ghz assert_allclose(f_lambda.to(u.Jy, 1., equivalencies=u.spectral_density(one_ghz)), 1. / 3.335640951981521e-20, 10) # Convert to ergs / cm^2 / s at 10 microns assert_allclose(f_lambda.to(u.erg / u.cm ** 2 / u.s, 1., equivalencies=u.spectral_density(u.Quantity(10, u.micron))), 10., 10)
def test_sip_irac(): """Test forward and inverse SIP againts astropy.wcs""" test_file = get_pkg_data_filename(os.path.join('data', 'irac_sip.hdr')) hdr = fits.Header.fromtextfile(test_file) crpix1 = hdr['CRPIX1'] crpix2 = hdr['CRPIX2'] wobj = wcs.WCS(hdr) a_pars = dict(**hdr['A_*']) b_pars = dict(**hdr['B_*']) ap_pars = dict(**hdr['AP_*']) bp_pars = dict(**hdr['BP_*']) a_order = a_pars.pop('A_ORDER') b_order = b_pars.pop('B_ORDER') ap_order = ap_pars.pop('AP_ORDER') bp_order = bp_pars.pop('BP_ORDER') del a_pars['A_DMAX'] del b_pars['B_DMAX'] pix = [200, 200] rel_pix = [200 - crpix1, 200 - crpix2] sip = SIP([crpix1, crpix2], a_order, b_order, a_pars, b_pars, ap_order=ap_order, ap_coeff=ap_pars, bp_order=bp_order, bp_coeff=bp_pars) foc = wobj.sip_pix2foc([pix], 1) newpix = wobj.sip_foc2pix(foc, 1)[0] assert_allclose(sip(*pix), foc[0] - rel_pix) assert_allclose(sip.inverse(*foc[0]) + foc[0] - rel_pix, newpix - pix)
def test__length(): # testing against R CircStats package # Ref. [1] pages 6 and 125 weights = np.array([12, 1, 6, 1, 2, 1, 1]) answer = 0.766282 data = np.array([0, 3.6, 36, 72, 108, 169.2, 324])*u.deg assert_allclose(answer, _length(data, weights=weights), atol=1e-4)
def test_linear_fitter_1D(self, model_class, constraints): """Test fitting with LinearLSQFitter""" model_args = linear1d[model_class] kwargs = {} kwargs.update(model_args['kwargs']) kwargs.update(model_args['parameters']) if constraints: kwargs.update(model_args['constraints']) model = model_class(*model_args['args'], **kwargs) y1 = model(self.x1) model_lin = self.linear_fitter(model, self.x1, y1 + self.n1) if constraints: # For the constraints tests we're not checking the overall fit, # just that the constraint was maintained fixed = model_args['constraints'].get('fixed', None) if fixed: for param, value in fixed.items(): expected = model_args['parameters'][param] assert getattr(model_lin, param).value == expected else: assert_allclose(model_lin.parameters, model.parameters, atol=0.2)
def test_non_linear_fitter_2D(self, model_class, constraints): """Test fitting with non-linear LevMarLSQFitter""" model_args = linear2d[model_class] kwargs = {} kwargs.update(model_args['kwargs']) kwargs.update(model_args['parameters']) if constraints: kwargs.update(model_args['constraints']) model = model_class(*model_args['args'], **kwargs) z = model(self.x2, self.y2) model_nlin = self.non_linear_fitter(model, self.x2, self.y2, z + self.n2) if constraints: fixed = model_args['constraints'].get('fixed', None) if fixed: for param, value in fixed.items(): expected = model_args['parameters'][param] assert getattr(model_nlin, param).value == expected else: assert_allclose(model_nlin.parameters, model.parameters, atol=0.2)
def test_compound_custom_inverse(): """ Test that a compound model with a custom inverse has that inverse applied when the inverse of another model, of which it is a component, is computed. Regression test for https://github.com/astropy/astropy/issues/3542 """ poly = Polynomial1D(1, c0=1, c1=2) scale = Scale(1) shift = Shift(1) model1 = poly | scale model1.inverse = poly # model1 now has a custom inverse (the polynomial itself, ignoring the # trivial scale factor) model2 = shift | model1 assert_allclose(model2.inverse(1), (poly | shift.inverse)(1)) # Make sure an inverse is not allowed if the models were combined with the # wrong operator, or if one of the models doesn't have an inverse defined with pytest.raises(NotImplementedError): (shift + model1).inverse with pytest.raises(NotImplementedError): (model1 & poly).inverse
def test_update_mean_cov_L_lmbda_converges_to_weighted_mean_and_cov(): N_init = 10 N = 10000 D = 2 X = np.random.randn(N, D) weights = np.random.rand(N) old_mean = np.average(X[:N_init], axis=0, weights=weights[:N_init]) old_cov_L = np.linalg.cholesky(np.cov(X[:N_init].T, ddof=0)) sum_old_weights = np.sum(weights[:N_init]) lmbdas = weights_to_lmbdas(sum_old_weights, weights[N_init:]) mean, cov_L = update_mean_cov_L_lmbda(X[N_init:], old_mean, old_cov_L, lmbdas) full_mean = np.average(X, axis=0, weights=weights) # the above method uses N rather than N-1 to normalise covariance (biased) try: full_cov = np.cov(X.T, ddof=0, aweights=weights) except TypeError: raise SkipTest("Numpy's cov method does not support aweights keyword.") cov = np.dot(cov_L, cov_L.T) assert_allclose(full_mean, mean) assert_allclose(full_cov, cov, atol=1e-2)
def test_bounding_box2D(self, model_class, test_parameters): """Test bounding box evaluation""" model = create_model(model_class, test_parameters) bbox = model.bounding_box if bbox is None: pytest.skip("Bounding_box is not defined for model.") # Check for exact agreement within bounded region xlim, ylim = bbox dx = np.ceil((xlim[1] - xlim[0]) / 2) dy = np.ceil((ylim[1] - ylim[0]) / 2) x0, y0 = np.mean(bbox, axis=1).astype(int) y, x = np.mgrid[y0 - dy: y0 + dy + 1, x0 - dx: x0 + dx + 1] expected = model(x, y) actual = render_model(model) utils.assert_allclose(actual, expected, rtol=0, atol=0) # check result with no bounding box defined model.bounding_box = None actual = render_model(model, coords=[y, x]) utils.assert_allclose(actual, expected, rtol=0, atol=0)
def test_fitter1D(self, model_class, test_parameters): """ Test if the parametric model works with the fitter. """ x_lim = test_parameters['x_lim'] parameters = test_parameters['parameters'] model = create_model(model_class, test_parameters) if isinstance(parameters, dict): parameters = [parameters[name] for name in model.param_names] if "log_fit" in test_parameters: if test_parameters['log_fit']: x = np.logspace(x_lim[0], x_lim[1], self.N) else: x = np.linspace(x_lim[0], x_lim[1], self.N) np.random.seed(0) # add 10% noise to the amplitude relative_noise_amplitude = 0.01 data = ((1 + relative_noise_amplitude * np.random.randn(len(x))) * model(x)) fitter = fitting.LevMarLSQFitter() new_model = fitter(model, x, data) # Only check parameters that were free in the fit params = [getattr(new_model, name) for name in new_model.param_names] fixed = [param.fixed for param in params] expected = np.array([val for val, fixed in zip(parameters, fixed) if not fixed]) fitted = np.array([param.value for param in params if not param.fixed]) utils.assert_allclose(fitted, expected, atol=self.fit_error)
def test_poisson_conf_array_fc(): n = 7*np.ones((3,4,5)) assert_allclose(funcs.poisson_conf_interval(n, interval='frequentist-confidence'), funcs.poisson_conf_interval(n[0,0,0], interval='frequentist-confidence')[:,None,None,None]*np.ones_like(n)) n[1,2,3] = 0 assert not np.any(np.isnan(funcs.poisson_conf_interval(n, interval='frequentist-confidence')))
def test_poisson_conf_gehrels86(n): assert_allclose( funcs.poisson_conf_interval( n, interval='sherpagehrels')[1], funcs.poisson_conf_interval( n, interval='frequentist-confidence')[1], rtol = 0.02)
def test_legacy_bandpass_fftw(): try: import pyfftw except ImportError: raise nose.SkipTest("pyfftw not installed. Skipping.") lbp_fftw = legacy_bandpass_fftw(frame, 3, 11)[margin:-margin, margin:-margin] assert_allclose(lbp_fftw, bp_scipy, atol=1.1)
def test_poisson_conf_array_rootn0(): n = 7*np.ones((3,4,5)) assert_allclose(funcs.poisson_conf_interval(n, interval='root-n-0'), funcs.poisson_conf_interval(n[0,0,0], interval='root-n-0')[:,None,None,None]*np.ones_like(n)) n[1,2,3] = 0 assert not np.any(np.isnan(funcs.poisson_conf_interval(n, interval='root-n-0')))
def test_deriv_1D(self, model_class): """ Test the derivative of a model by comparing results with an estimated derivative """ x_lim = models_1D[model_class]['x_lim'] if model_class.fit_deriv is None: pytest.skip("Derivative function is not defined for model.") if issubclass(model_class, (models.PolynomialModel, models.OrthoPolynomialBase)): pytest.skip("Skip testing derivative of polynomials.") if "log_fit" in models_1D[model_class]: if models_1D[model_class]['log_fit']: x = np.logspace(x_lim[0], x_lim[1], self.N) else: x = np.linspace(x_lim[0], x_lim[1], self.N) parameters = models_1D[model_class]['parameters'] model_with_deriv = create_model(model_class, parameters, use_constraints=False) model_no_deriv = create_model(model_class, parameters, use_constraints=False) # add 10% noise to the amplitude rsn = np.random.RandomState(1234567890) n = 0.1 * parameters[0] * (rsn.rand(self.N) - 0.5) data = model_with_deriv(x) + n fitter_with_deriv = fitting.NonLinearLSQFitter() new_model_with_deriv = fitter_with_deriv(model_with_deriv, x, data) fitter_no_deriv = fitting.NonLinearLSQFitter() new_model_no_deriv = fitter_no_deriv(model_no_deriv, x, data, estimate_jacobian=True) utils.assert_allclose(new_model_with_deriv.parameters, new_model_no_deriv.parameters, atol=0.1)
def test_fitter1D(self, model_class): """ Test if the parametric model works with the fitter. """ x_lim = models_1D[model_class]['x_lim'] parameters = models_1D[model_class]['parameters'] model = create_model(model_class, parameters) if isinstance(parameters, dict): parameters = [parameters[name] for name in model.param_names] if "log_fit" in models_1D[model_class]: if models_1D[model_class]['log_fit']: x = np.logspace(x_lim[0], x_lim[1], self.N) else: x = np.linspace(x_lim[0], x_lim[1], self.N) np.random.seed(0) # add 10% noise to the amplitude relative_noise_amplitude = 0.01 data = (1 + relative_noise_amplitude * np.random.randn(len(x))) * model(x) fitter = fitting.NonLinearLSQFitter() new_model = fitter(model, x, data) # Only check parameters that were free in the fit params = [getattr(new_model, name) for name in new_model.param_names] fixed = [par.fixed for par in params] fitted_parameters = [val for (val, fixed) in zip(parameters, fixed) if not fixed] fitparams, _ = fitter._model_to_fit_params(new_model) utils.assert_allclose(fitparams, fitted_parameters, atol=self.fit_error)
def test_render_model_1d(): npix = 101 image = np.zeros(npix) coords = np.arange(npix) model = models.Gaussian1D() # test points test_pts = [0, 49.1, 49.5, 49.9, 100] # test widths test_stdv = np.arange(5.5, 6.7, .2) for x0, stdv in [(p, s) for p in test_pts for s in test_stdv]: model.mean = x0 model.stddev = stdv expected = model(coords) for x in [coords, None]: for im in [image.copy(), None]: if (im is None) & (x is None): # this case is tested in Fittable1DModelTester continue actual = model.render(out=im, coords=x) # assert images match assert_allclose(expected, actual, atol=3e-7) # assert model fully captured if (x0, stdv) == (49.5, 5.5): boxed = model.render() flux = np.sum(expected) assert ((flux - np.sum(boxed)) / flux) < 1e-7
def test_floor_divide_remainder_and_divmod(self): inch = u.Unit(0.0254 * u.m) dividend = np.array([1., 2., 3.]) * u.m divisor = np.array([3., 4., 5.]) * inch quotient = dividend // divisor remainder = dividend % divisor assert_allclose(quotient.value, [13., 19., 23.]) assert quotient.unit == u.dimensionless_unscaled assert_allclose(remainder.value, [0.0094, 0.0696, 0.079]) assert remainder.unit == dividend.unit quotient2 = np.floor_divide(dividend, divisor) remainder2 = np.remainder(dividend, divisor) assert np.all(quotient2 == quotient) assert np.all(remainder2 == remainder) quotient3, remainder3 = divmod(dividend, divisor) assert np.all(quotient3 == quotient) assert np.all(remainder3 == remainder) with pytest.raises(TypeError): divmod(dividend, u.km) with pytest.raises(TypeError): dividend // u.km with pytest.raises(TypeError): dividend % u.km if hasattr(np, 'divmod'): # not NUMPY_LT_1_13 quotient4, remainder4 = np.divmod(dividend, divisor) assert np.all(quotient4 == quotient) assert np.all(remainder4 == remainder) with pytest.raises(TypeError): np.divmod(dividend, u.km)
def test_process_replaceSequences(self): data = [1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 4.0] qual = [15, 15,15, 15, 15, 15, 15] proc, invalidData = self.chain.process(data, qual) self._checkValidData(proc, invalidData) assert_allclose(proc, [NaN, NaN, NaN, NaN, NaN, 0.5, 1.0])
def test_custom_inverse(): """Test setting a custom inverse on a model.""" p = models.Polynomial1D(1, c0=-2, c1=3) # A trivial inverse for a trivial polynomial inv = models.Polynomial1D(1, c0=(2./3.), c1=(1./3.)) with pytest.raises(NotImplementedError): p.inverse p.inverse = inv x = np.arange(100) assert_allclose(x, p(p.inverse(x))) assert_allclose(x, p.inverse(p(x))) with catch_warnings(AstropyDeprecationWarning) as w: p.inverse = None # TODO: This can be removed after Astropy v1.1 or so assert len(w) == 1 with pytest.raises(NotImplementedError): p.inverse
def test_fitter2D(self, model_class): """ Test if the parametric model works with the fitter. """ x_lim = models_2D[model_class]['x_lim'] y_lim = models_2D[model_class]['y_lim'] parameters = models_2D[model_class]['parameters'] model = create_model(model_class, parameters) if isinstance(parameters, dict): parameters = [parameters[name] for name in model.param_names] if "log_fit" in models_2D[model_class]: if models_2D[model_class]['log_fit']: x = np.logspace(x_lim[0], x_lim[1], self.N) y = np.logspace(y_lim[0], y_lim[1], self.N) else: x = np.linspace(x_lim[0], x_lim[1], self.N) y = np.linspace(y_lim[0], y_lim[1], self.N) xv, yv = np.meshgrid(x, y) np.random.seed(0) # add 10% noise to the amplitude data = model(xv, yv) + 0.1 * parameters[0] * (np.random.rand(self.N, self.N) - 0.5) fitter = fitting.NonLinearLSQFitter() new_model = fitter(model, xv, yv, data) fitparams, _ = fitter._model_to_fit_params(new_model) utils.assert_allclose(fitparams, parameters, atol=self.fit_error)
def test_process_badQuality(self): data = [-2.0, -1.0, 0, 1.0, 2.0] qual = [15, 0, self.minQuality-1, self.minQuality, self.minQuality+1] proc, invalidData = self.chain.process(data, qual) self._checkValidData(proc, invalidData) assert_allclose(proc, [-1.0, NaN, NaN, 0.5, 1])
def test_state_monitor_indexing(): # Check indexing semantics G = NeuronGroup(10, 'v:volt') G.v = np.arange(10) * volt mon = StateMonitor(G, 'v', record=[5, 6, 7]) run(2 * defaultclock.dt) assert_array_equal(mon.v, np.array([[5, 5], [6, 6], [7, 7]]) * volt) assert_array_equal(mon.v_, np.array([[5, 5], [6, 6], [7, 7]])) assert_array_equal(mon[5].v, mon.v[0]) assert_array_equal(mon[7].v, mon.v[2]) assert_array_equal(mon[[5, 7]].v, mon.v[[0, 2]]) assert_array_equal(mon[np.array([5, 7])].v, mon.v[[0, 2]]) assert_allclose(mon.t[1:], Quantity([defaultclock.dt])) assert_raises(IndexError, lambda: mon[8]) assert_raises(TypeError, lambda: mon['string']) assert_raises(TypeError, lambda: mon[5.0]) assert_raises(TypeError, lambda: mon[[5.0, 6.0]])
def test_spectral3(): a = u.nm.to(u.Hz, [1000, 2000], u.spectral()) assert_allclose(a, [2.99792458e+14, 1.49896229e+14])
def test_binom_conf_interval(): # Test Wilson and Jeffreys interval for corner cases: # Corner cases: k = 0, k = n, conf = 0., conf = 1. n = 5 k = [0, 4, 5] for conf in [0., 0.5, 1.]: res = funcs.binom_conf_interval(k, n, conf=conf, interval='wilson') assert ((res >= 0.) & (res <= 1.)).all() res = funcs.binom_conf_interval(k, n, conf=conf, interval='jeffreys') assert ((res >= 0.) & (res <= 1.)).all() # Test Jeffreys interval accuracy against table in Brown et al. (2001). # (See `binom_conf_interval` docstring for reference.) k = [0, 1, 2, 3, 4] n = 7 conf = 0.95 result = funcs.binom_conf_interval(k, n, conf=conf, interval='jeffreys') table = np.array([[0.000, 0.016, 0.065, 0.139, 0.234], [0.292, 0.501, 0.648, 0.766, 0.861]]) assert_allclose(result, table, atol=1.e-3, rtol=0.) # Test scalar version result = np.array([funcs.binom_conf_interval(kval, n, conf=conf, interval='jeffreys') for kval in k]).transpose() assert_allclose(result, table, atol=1.e-3, rtol=0.) # Test flat result = funcs.binom_conf_interval(k, n, conf=conf, interval='flat') table = np.array([[0., 0.03185, 0.08523, 0.15701, 0.24486], [0.36941, 0.52650, 0.65085, 0.75513, 0.84298]]) assert_allclose(result, table, atol=1.e-3, rtol=0.) # Test scalar version result = np.array([funcs.binom_conf_interval(kval, n, conf=conf, interval='flat') for kval in k]).transpose() assert_allclose(result, table, atol=1.e-3, rtol=0.) # Test Wald interval result = funcs.binom_conf_interval(0, 5, interval='wald') assert_allclose(result, 0.) # conf interval is [0, 0] when k = 0 result = funcs.binom_conf_interval(5, 5, interval='wald') assert_allclose(result, 1.) # conf interval is [1, 1] when k = n result = funcs.binom_conf_interval(500, 1000, conf=0.68269, interval='wald') assert_allclose(result[0], 0.5 - 0.5 / np.sqrt(1000.)) assert_allclose(result[1], 0.5 + 0.5 / np.sqrt(1000.)) # Test shapes k = 3 n = 7 for interval in ['wald', 'wilson', 'jeffreys', 'flat']: result = funcs.binom_conf_interval(k, n, interval=interval) assert result.shape == (2,) k = np.array(k) for interval in ['wald', 'wilson', 'jeffreys', 'flat']: result = funcs.binom_conf_interval(k, n, interval=interval) assert result.shape == (2,) n = np.array(n) for interval in ['wald', 'wilson', 'jeffreys', 'flat']: result = funcs.binom_conf_interval(k, n, interval=interval) assert result.shape == (2,) k = np.array([1, 3, 5]) for interval in ['wald', 'wilson', 'jeffreys', 'flat']: result = funcs.binom_conf_interval(k, n, interval=interval) assert result.shape == (2, 3) n = np.array([5, 5, 5]) for interval in ['wald', 'wilson', 'jeffreys', 'flat']: result = funcs.binom_conf_interval(k, n, interval=interval) assert result.shape == (2, 3)
def test_psf(self): psf = self.data_2fhl.psf actual = self.data_2fhl.psf.integral(100 * u.GeV, 0 * u.deg, 2 * u.deg) desired = 1.00048 assert_allclose(actual, desired, rtol=1e-4)
def test_units_conversion(): assert_allclose(u.kpc.to(u.Mpc), 0.001) assert_allclose(u.Mpc.to(u.kpc), 1000) assert_allclose(u.yr.to(u.Myr), 1.e-6) assert_allclose(u.AU.to(u.pc), 4.84813681e-6) assert_allclose(u.cycle.to(u.rad), 6.283185307179586)
def test_fast_bw_uniform(): print("Make op...") from NativeOp import FastBaumWelchOp op = FastBaumWelchOp().make_op( ) # (am_scores, edges, weights, start_end_states, float_idx, state_buffer) print("Op:", op) n_batch = 3 seq_len = 7 n_classes = 5 from Fsa import FastBwFsaShared fsa = FastBwFsaShared() for i in range(n_classes): fsa.add_edge(i, i + 1, emission_idx=i) # fwd fsa.add_edge(i + 1, i + 1, emission_idx=i) # loop assert n_classes <= seq_len fast_bw_fsa = fsa.get_fast_bw_fsa(n_batch=n_batch) print("edges:") print(fast_bw_fsa.edges) edges = fast_bw_fsa.edges.view("float32") edges_placeholder = T.fmatrix(name="edges") weights = fast_bw_fsa.weights weights_placeholder = T.fvector(name="weights") print("start_end_states:") print(fast_bw_fsa.start_end_states) start_end_states = fast_bw_fsa.start_end_states.view("float32") start_end_states_placeholder = T.fmatrix(name="start_end_states") am_scores = numpy.ones((seq_len, n_batch, n_classes), dtype="float32") * numpy.float32(1.0 / n_classes) am_scores = -numpy.log(am_scores) # in -log space am_scores_placeholder = T.ftensor3(name="am_scores") float_idx = numpy.ones((seq_len, n_batch), dtype="float32") float_idx_placeholder = T.fmatrix(name="float_idx") last_state_idx = numpy.max( fast_bw_fsa.start_end_states[1]) # see get_automata_for_batch state_buffer = numpy.zeros((2, last_state_idx + 1), dtype="float32") state_buffer_placeholder = T.fmatrix(name="state_buffer") print("Construct call...") fwdbwd, obs_scores = op(am_scores_placeholder, edges_placeholder, weights_placeholder, start_end_states_placeholder, float_idx_placeholder, state_buffer_placeholder) f = theano.function(inputs=[ am_scores_placeholder, edges_placeholder, weights_placeholder, start_end_states_placeholder, float_idx_placeholder, state_buffer_placeholder ], outputs=[fwdbwd, obs_scores]) print("Done.") print("Eval:") fwdbwd, score = f(am_scores, edges, weights, start_end_states, float_idx, state_buffer) print("score:") print(repr(score)) assert_equal(score.shape, (seq_len, n_batch)) bw = numpy.exp(-fwdbwd) print("Baum-Welch soft alignment:") print(repr(bw)) assert_equal(bw.shape, (seq_len, n_batch, n_classes)) from numpy import array, float32 if seq_len == n_classes: print("Extra check identity...") for i in range(n_batch): assert_almost_equal(numpy.identity(n_classes), bw[:, i]) if seq_len == 7 and n_classes == 5: print("Extra check ref_align (7,5)...") assert_allclose(score, 8.55801582, rtol=1e-5) # should be the same everywhere ref_align = \ array([[[1., 0., 0., 0., 0.]], [[0.33333316, 0.66666663, 0., 0., 0.]], [[0.06666669, 0.53333354, 0.40000018, 0., 0.]], [[0., 0.20000014, 0.60000014, 0.19999999, 0.]], [[0., 0., 0.39999962, 0.53333312, 0.06666663]], [[0., 0., 0., 0.66666633, 0.33333316]], [[0., 0., 0., 0., 0.99999982]]], dtype=float32) assert_equal(ref_align.shape, (seq_len, 1, n_classes)) ref_align = numpy.tile(ref_align, (1, n_batch, 1)) assert_equal(ref_align.shape, bw.shape) # print("Reference alignment:") # print(repr(ref_align)) print("mean square diff:", numpy.mean(numpy.square(ref_align - bw))) print("max square diff:", numpy.max(numpy.square(ref_align - bw))) assert_allclose(ref_align, bw, rtol=1e-5) print("Done.")
def test_spectraldensity4(): """PHOTLAM and PHOTNU conversions.""" flam = u.erg / (u.cm**2 * u.s * u.AA) fnu = u.erg / (u.cm**2 * u.s * u.Hz) photlam = u.photon / (u.cm**2 * u.s * u.AA) photnu = u.photon / (u.cm**2 * u.s * u.Hz) wave = u.Quantity([4956.8, 4959.55, 4962.3], u.AA) flux_photlam = [9.7654e-3, 1.003896e-2, 9.78473e-3] flux_photnu = [8.00335589e-14, 8.23668949e-14, 8.03700310e-14] flux_flam = [3.9135e-14, 4.0209e-14, 3.9169e-14] flux_fnu = [3.20735792e-25, 3.29903646e-25, 3.21727226e-25] flux_jy = [3.20735792e-2, 3.29903646e-2, 3.21727226e-2] # PHOTLAM <--> FLAM assert_allclose(photlam.to(flam, flux_photlam, u.spectral_density(wave)), flux_flam, rtol=1e-6) assert_allclose(flam.to(photlam, flux_flam, u.spectral_density(wave)), flux_photlam, rtol=1e-6) # PHOTLAM <--> FNU assert_allclose(photlam.to(fnu, flux_photlam, u.spectral_density(wave)), flux_fnu, rtol=1e-6) assert_allclose(fnu.to(photlam, flux_fnu, u.spectral_density(wave)), flux_photlam, rtol=1e-6) # PHOTLAM <--> Jy assert_allclose(photlam.to(u.Jy, flux_photlam, u.spectral_density(wave)), flux_jy, rtol=1e-6) assert_allclose(u.Jy.to(photlam, flux_jy, u.spectral_density(wave)), flux_photlam, rtol=1e-6) # PHOTLAM <--> PHOTNU assert_allclose(photlam.to(photnu, flux_photlam, u.spectral_density(wave)), flux_photnu, rtol=1e-6) assert_allclose(photnu.to(photlam, flux_photnu, u.spectral_density(wave)), flux_photlam, rtol=1e-6) # PHOTNU <--> FNU assert_allclose(photnu.to(fnu, flux_photnu, u.spectral_density(wave)), flux_fnu, rtol=1e-6) assert_allclose(fnu.to(photnu, flux_fnu, u.spectral_density(wave)), flux_photnu, rtol=1e-6) # PHOTNU <--> FLAM assert_allclose(photnu.to(flam, flux_photnu, u.spectral_density(wave)), flux_flam, rtol=1e-6) assert_allclose(flam.to(photnu, flux_flam, u.spectral_density(wave)), flux_photnu, rtol=1e-6)
def test_spectraldensity2(): flambda = u.erg / u.angstrom / u.cm**2 / u.s fnu = u.erg / u.Hz / u.cm**2 / u.s a = flambda.to(fnu, 1, u.spectral_density(u.Quantity(3500, u.AA))) assert_allclose(a, 4.086160166177361e-12)
def test_parallax2(): a = u.arcsecond.to(u.pc, [0.1, 2.5], u.parallax()) assert_allclose(a, [10, 0.4])
def test_Rotation2D(): model = models.Rotation2D(angle=90) x, y = model(1, 0) utils.assert_allclose([x, y], [0, 1], atol=1e-10)
def test_spectral(): a = u.AA.to(u.Hz, 1, u.spectral()) assert_allclose(a, 2.9979245799999995e+18) b = u.Hz.to(u.AA, a, u.spectral()) assert_allclose(b, 1) a = u.AA.to(u.MHz, 1, u.spectral()) assert_allclose(a, 2.9979245799999995e+12) b = u.MHz.to(u.AA, a, u.spectral()) assert_allclose(b, 1) a = u.m.to(u.Hz, 1, u.spectral()) assert_allclose(a, 2.9979245799999995e+8) b = u.Hz.to(u.m, a, u.spectral()) assert_allclose(b, 1)
def _test_roundtrip_fits(unit): s = unit.to_string('fits') a = core.Unit(s, format='fits') assert_allclose(a.decompose().scale, unit.decompose().scale, rtol=1e-2)
def test_Rotation2D_inverse(): model = models.Rotation2D(angle=234.23494) x, y = model.inverse(*model(1, 0)) utils.assert_allclose([x, y], [1, 0], atol=1e-10)
def run_test(model): wcsobj = model.meta.wcs for ch in model.meta.instrument.channel: ref_data = mrs_ref_data[ch + band_mapping[model.meta.instrument.band]] detector_to_alpha_beta = wcsobj.get_transform('detector', 'alpha_beta') ab_to_xan_yan = wcsobj.get_transform('alpha_beta', 'Xan_Yan').set_input(int(ch)) ref_alpha = ref_data['alpha'] ref_beta = ref_data['beta'] ref_lam = ref_data['lam'] #xyan_to_detector = wcsobj.get_transform('Xan_Yan', 'detector') #xin, yin = xyan_to_detector(ref_data['v2'], ref_data['v3'], # ref_data['lam']) x, y = ref_data['x'], ref_data['y'] for i, s in enumerate(ref_data['s']): sl = int(ch) * 100 + s alpha, beta, lam = detector_to_alpha_beta.set_input(sl)(x[i], y[i]) utils.assert_allclose(alpha, ref_alpha[i], atol=10**-4) utils.assert_allclose(beta, ref_beta[i], atol=10**-4) utils.assert_allclose(lam, ref_lam[i], atol=10**-4) xan, yan, lam = ab_to_xan_yan(ref_alpha, ref_beta, ref_lam) utils.assert_allclose(xan, ref_data['v2'], atol=10**-4) utils.assert_allclose(yan, ref_data['v3'], atol=10**-4) utils.assert_allclose(lam, ref_data['lam'], atol=10**-4)
def test_native_celestial_lat90(): n2c = models.RotateNative2Celestial(1, 90, 0) alpha, delta = n2c(1, 1) utils.assert_allclose(delta, 1) utils.assert_allclose(alpha, 182)
def test_poisson_conf_large(interval): n = 100 assert_allclose(funcs.poisson_conf_interval(n, interval='root-n'), funcs.poisson_conf_interval(n, interval=interval), rtol=2e-2)
def _test_roundtrip(unit): a = core.Unit(unit.to_string('generic'), format='generic') b = core.Unit(unit.decompose().to_string('generic'), format='generic') assert_allclose(a.decompose().scale, unit.decompose().scale, rtol=1e-2) assert_allclose(b.decompose().scale, unit.decompose().scale, rtol=1e-2)
def test_gaussian_sigma_to_fwhm_to_sigma(): assert_allclose( funcs.gaussian_fwhm_to_sigma * funcs.gaussian_sigma_to_fwhm, 1.0)
def test_poisson_conf_gehrels86(n): assert_allclose( funcs.poisson_conf_interval(n, interval='sherpagehrels')[1], funcs.poisson_conf_interval(n, interval='frequentist-confidence')[1], rtol=0.02)
def test_gaussian_fwhm_to_sigma(): fwhm = (2.0 * np.sqrt(2.0 * np.log(2.0))) assert_allclose(funcs.gaussian_fwhm_to_sigma * fwhm, 1.0, rtol=1.0e-6)
def test_poisson_conf_interval_rootn(): assert_allclose(funcs.poisson_conf_interval(16, interval='root-n'), (12, 20))
def test_angle_mismatched_unit(): a = Angle('+6h7m8s', unit=u.degree) assert_allclose(a.value, 91.78333333333332)
def test_gaussian_sigma_to_fwhm(): sigma = 1.0 / (2.0 * np.sqrt(2.0 * np.log(2.0))) assert_allclose(funcs.gaussian_sigma_to_fwhm * sigma, 1.0, rtol=1.0e-6)
def test_negative_sixty_dms(): # Test for DMS parser a = Angle('-00:00:60', u.deg) assert_allclose(a.degree, -1. / 60.)
def test_mad_std(): with NumpyRNGContext(12345): data = normal(5, 2, size=(100, 100)) assert_allclose(funcs.mad_std(data), 2.0, rtol=0.05)
def test_negative_fifty_nine_sixty_dms(): # Test for DMS parser a = Angle('-00:59:60', u.deg) assert_allclose(a.degree, -1.)
def test_plus_sixty_dms(): # Test for DMS parser a = Angle('+00:00:60', u.deg) assert_allclose(a.degree, 1. / 60.)
def test_negative_sixty_hm(): # Test for HM parser a = Angle('-00:60', u.hour) assert_allclose(a.hour, -1.)
def test_plus_fifty_nine_sixty_dms(): # Test for DMS parser a = Angle('+00:59:60', u.deg) assert_allclose(a.degree, 1.)
def test_negative_zero_hm(): # Test for HM parser a = Angle('-00:10', u.hour) assert_allclose(a.hour, -10. / 60.)
def test_plus_sixty_hm(): # Test for HM parser a = Angle('00:60', u.hour) assert_allclose(a.hour, 1.)