def test_zn(self): """Test pulse phase calculation, frequency only.""" ph = np.array([0, 1]) np.testing.assert_array_almost_equal(z_n(ph, 2), 8) ph = np.array([]) np.testing.assert_array_almost_equal(z_n(ph, 2), 0) ph = np.array([0.2, 0.7]) ph2 = np.array([0, 0.5]) np.testing.assert_array_almost_equal(z_n(ph, 2), z_n(ph2, 2))
def test_zn_2(self): with pytest.warns(DeprecationWarning) as record: np.testing.assert_almost_equal(z_n(np.arange(1), n=1, norm=1), 2) np.testing.assert_almost_equal(z_n(np.arange(1), n=2, norm=1), 4) np.testing.assert_almost_equal(z_n(np.arange(2), n=2, norm=1), 8) np.testing.assert_almost_equal( z_n(np.arange(2) + 0.5, n=2, norm=1), 8) assert np.any([ "The use of ``z_n(phase, norm=profile)``" in r.message.args[0] for r in record ])
def test_deprecated_norm_use(self): prof512, bins = self.prof512, self.bins with pytest.warns(DeprecationWarning) as record: z = z_n(prof512, n=3, datatype="binned") z_dep = z_n(np.zeros(prof512.size), norm=prof512, n=3, datatype="binned") np.testing.assert_almost_equal(z, z_dep) assert np.any([ "The use of ``z_n(phase, norm=profile)``" in r.message.args[0] for r in record ])
def test_zn_poisson(self): phases = self.phases prof512, bins = self.prof512, self.bins ks, ze = z_n_events_all(phases, nmax=10) ksp, zp = z_n_binned_events_all(prof512, nmax=10) assert np.allclose(ze, zp, rtol=0.01) m, h = htest(prof512, datatype="binned", nmax=10) assert np.isclose(h + 4 * m - 4, z_n(prof512, n=m, datatype="binned")) assert np.isclose(h + 4 * m - 4, zp[m - 1])
def test_zn_gauss(self): nbin = 512 dph = 1 / nbin err = 0.1 ph = np.arange(-0.5 + dph / 2, 0.5, dph) prof = np.random.normal(np.exp(-ph**2 / 2 / 0.1**2), err) prof_poiss = poissonize_gaussian_profile(prof, err) ksp, zp = z_n_binned_events_all(prof_poiss, nmax=10) ksg, zg = z_n_gauss_all(prof, err, nmax=10) assert np.allclose(zg, zp, rtol=0.01) mg, hg = htest(prof, err=err, datatype="gauss", nmax=10) mp, hp = htest(prof_poiss, datatype="binned", nmax=10) assert np.isclose(hg, hp) assert np.isclose(mg, mp) assert np.isclose(hg + 4 * mg - 4, z_n(prof, n=mg, err=err, datatype="gauss"))
def test_wrong_args_Z_gauss_noerr(self): with pytest.raises(ValueError) as excinfo: z_n([1], 2, datatype="gauss") assert "If datatype='gauss', you need to " in str(excinfo.value)
def test_wrong_args_Z_datatype(self): with pytest.raises(ValueError) as excinfo: z_n([1], 2, datatype="gibberish") assert "Unknown datatype requested for Z_n (gibberish)" in str( excinfo.value)
def test_zn_events(self): phases = self.phases ks, ze = z_n_events_all(phases, nmax=10) m, h = htest(phases, nmax=10, datatype="events") assert np.isclose(h + 4 * m - 4, z_n(phases, n=m, datatype="events")) assert np.isclose(h + 4 * m - 4, ze[m - 1])