def test_poisson_noise(self): out = utils.poisson_noise(1000) self.assertEqual(type(out), SherpaFloat) self.assertGreater(out, 0.0) for x in (-1000, 0): out = utils.poisson_noise(x) self.assertEqual(type(out), SherpaFloat) self.assertEqual(out, 0.0) out = utils.poisson_noise([1001, 1002, 0.0, 1003, -1004]) self.assertEqual(type(out), numpy.ndarray) self.assertEqual(out.dtype.type, SherpaFloat) ans = numpy.flatnonzero(out > 0.0) assert_equal(ans, numpy.array([0, 1, 3])) self.assertRaises(ValueError, utils.poisson_noise, 'ham') self.assertRaises(TypeError, utils.poisson_noise, [1, 2, 'ham'])
def test_poisson_noise(self): out = utils.poisson_noise(1000) self.assertEqual(type(out), SherpaFloat) self.assertGreater(out, 0.0) for x in (-1000, 0): out = utils.poisson_noise(x) self.assertEqual(type(out), SherpaFloat) self.assertEqual(out, 0.0) out = utils.poisson_noise([1001, 1002, 0.0, 1003, -1004]) self.assertEqual(type(out), numpy.ndarray) self.assertEqual(out.dtype.type, SherpaFloat) ans = numpy.flatnonzero(out > 0.0) assert_equal(ans, numpy.array([0, 1, 3])) self.assertRaises(ValueError, utils.poisson_noise, 'ham') self.assertRaises(TypeError, utils.poisson_noise, [1, 2, 'ham'])
def calculate(nullfit, altfit, proposal, null_vals, alt_vals): # FIXME: only null perturbed? nullfit.model.thawedpars = proposal # Fake using poisson_noise with null fake = poisson_noise(nullfit.data.eval_model(nullfit.model)) # Set faked data for both nullfit and altfit nullfit.data.set_dep(fake) # Start the faked fit at initial null best-fit values #nullfit.model.thawedpars = null_vals # Fit with null model nullfr = nullfit.fit() debug(nullfr.format()) null_stat = nullfr.statval debug("statistic null = " + repr(null_stat)) # nullfit and altfit BOTH point to same faked dataset assert ( id(nullfit.data) == id(altfit.data) ) assert (nullfit.data.get_dep() == altfit.data.get_dep()).all() # Start the faked fit at the initial alt best-fit values #altfit.model.thawedpars = alt_vals debug("proposal: " + repr(proposal)) debug("alt model") debug(str(altfit.model)) # Set alt model and fit altfr = altfit.fit() debug(altfr.format()) debug(str(altfit.model)) alt_stat = altfr.statval debug("statistic alt = " + repr(alt_stat)) LR = -(alt_stat - null_stat) debug("LR = " + repr(LR)) return [null_stat, alt_stat, LR]
def calculate(nullfit, altfit, proposal, null_vals, alt_vals): # FIXME: only null perturbed? nullfit.model.thawedpars = proposal # Fake using poisson_noise with null fake = poisson_noise(nullfit.data.eval_model(nullfit.model)) # Set faked data for both nullfit and altfit nullfit.data.set_dep(fake) # Start the faked fit at initial null best-fit values #nullfit.model.thawedpars = null_vals # Fit with null model nullfr = nullfit.fit() debug(nullfr.format()) null_stat = nullfr.statval debug("statistic null = " + repr(null_stat)) # nullfit and altfit BOTH point to same faked dataset assert (id(nullfit.data) == id(altfit.data)) assert (nullfit.data.get_dep() == altfit.data.get_dep()).all() # Start the faked fit at the initial alt best-fit values #altfit.model.thawedpars = alt_vals debug("proposal: " + repr(proposal)) debug("alt model") debug(str(altfit.model)) # Set alt model and fit altfr = altfit.fit() debug(altfr.format()) debug(str(altfit.model)) alt_stat = altfr.statval debug("statistic alt = " + repr(alt_stat)) LR = -(alt_stat - null_stat) debug("LR = " + repr(LR)) return [null_stat, alt_stat, LR]
def setup_data1d_fit(): """Create a 1D dataset for fitting a gaussian-like profile. This sets the random seed to a fixed value, so use the reset_seed fixture. """ x = np.linspace(2300, 2400, 51) cpt = ui.voigt1d('cpt') cpt.pos = 2345 cpt.fwhm_g = 20 cpt.fwhm_l = 15 cpt.ampl = 480 np.random.seed(72383) y = poisson_noise(cpt(x)) ui.load_arrays(1, x, y, ui.Data1D) ui.set_stat('leastsq') ui.set_method('simplex') ui.delete_model_component('cpt')
def test_poisson_noise(): out = utils.poisson_noise(1000) assert type(out) == SherpaFloat assert out > 0.0 for x in (-1000, 0): out = utils.poisson_noise(x) assert type(out) == SherpaFloat assert out == 0.0 out = utils.poisson_noise([1001, 1002, 0.0, 1003, -1004]) assert type(out) == numpy.ndarray assert out.dtype.type == SherpaFloat ans = numpy.flatnonzero(out > 0.0) assert (ans == numpy.array([0, 1, 3])).all() with pytest.raises(ValueError): utils.poisson_noise('ham') with pytest.raises(TypeError): utils.poisson_noise(1, 2, 'ham')