def test_grouped_ciao4_5(self): self.run_thread('grouped_ciao4.5') self.assertEqualWithinTol(ui.get_fit_results().statval, 18.8316, 1e-4) self.assertEqual(ui.get_fit_results().numpoints, 46) self.assertEqualWithinTol(self.locals['aa'].gamma.val, 1.83906, 1e-4) self.assertEqualWithinTol(self.locals['aa'].ampl.val, 0.000301258, 1e-4)
def test_pha_intro(self): self.run_thread('pha_intro') # astro.ui imported as ui, instead of # being in global namespace fit_results = ui.get_fit_results() covarerr = sqrt(fit_results.extra_output['covar'].diagonal()) assert covarerr[0] == approx(0.0790393, rel=1e-4) assert covarerr[1] == approx(1.4564e-05, rel=1e-4) assert fit_results.statval == approx(37.9079, rel=1e-4) assert fit_results.rstat == approx(0.902569, rel=1e-4) assert fit_results.qval == approx(0.651155, rel=1e-4) assert self.locals['p1'].gamma.val == approx(2.15852, rel=1e-4) assert self.locals['p1'].ampl.val == approx(0.00022484, rel=1e-4) assert ui.calc_photon_flux() == approx(0.000469964, rel=1e-4) assert ui.calc_energy_flux() == approx(9.614847e-13, rel=1e-4) assert ui.calc_data_sum() == approx(706.85714092, rel=1e-4) assert ui.calc_model_sum() == approx(638.45693377, rel=1e-4) assert ui.calc_source_sum() == approx(0.046996409, rel=1e-4) calc = ui.eqwidth(self.locals['p1'], ui.get_source()) assert calc == approx(-0.57731725, rel=1e-4) calc = ui.calc_kcorr([1, 1.2, 1.4, 1.6, 1.8, 2], 0.5, 2) expected = [0.93341286, 0.93752836, 0.94325233, 0.94990140, 0.95678054, 0.96393515] assert calc == approx(expected, rel=1e-4) self.assertEqual(ui.get_fit_results().nfev, 22) self.assertEqual(ui.get_fit_results().numpoints, 44) self.assertEqual(ui.get_fit_results().dof, 42)
def test_linepro(self): def cmp_linepro(fit_results, mylocals, covarerr): b1 = mylocals['b1'] assert covarerr[0] == approx(0.176282, rel=1e-4) assert covarerr[1] == approx(0.0019578, rel=1e-4) assert covarerr[2] == approx(0.495889, rel=1e-4) assert fit_results.statval == approx(203.34, rel=1e-4) assert b1.r0.val == approx(4.25557, rel=1e-4) assert b1.beta.val == approx(0.492232, rel=1e-4) assert b1.ampl.val == approx(11.8129, rel=1e-4) self.assertEqual(fit_results.nfev, 17) self.assertEqual(fit_results.numpoints, 75) self.assertEqual(fit_results.dof, 72) self.run_thread('linepro') fit_results = ui.get_fit_results() covarerr = sqrt(fit_results.extra_output['covar'].diagonal()) cmp_linepro(fit_results, self.locals, covarerr) self.assertEqual(0, fit_results.extra_output['num_parallel_map']) self.run_thread('linepro_ncpus') fit_results = ui.get_fit_results() covarerr = sqrt(fit_results.extra_output['covar'].diagonal()) cmp_linepro(fit_results, self.locals, covarerr) if _ncpus != 1: assert fit_results.extra_output['num_parallel_map'] > 0 else: assert fit_results.extra_output['num_parallel_map'] == 0
def test_radpro(self): def cmp_radpro(fit_results, mylocals, covarerr): src = mylocals['src'] assert covarerr[0] == approx(9.37345, rel=1e-4) assert covarerr[1] == approx(0.512596, rel=1e-4) assert covarerr[2] == approx(0.0691102, rel=1e-4) assert fit_results.statval == approx(217.450, rel=1e-4) assert fit_results.rstat == approx(6.21287, rel=1e-4) assert fit_results.qval == approx(0.0, rel=1e-4) assert src.r0.val == approx(125.829, rel=1e-4) assert src.beta.val == approx(4.1633, rel=1e-4) assert src.xpos.val == approx(0.0, rel=1e-4) assert src.ampl.val == approx(4.42821, rel=1e-4) self.assertEqual(fit_results.nfev, 92) self.assertEqual(fit_results.numpoints, 38) self.assertEqual(fit_results.dof, 35) self.run_thread('radpro') fit_results = ui.get_fit_results() covarerr = sqrt(fit_results.extra_output['covar'].diagonal()) cmp_radpro(fit_results, self.locals, covarerr) self.assertEqual(0, fit_results.extra_output['num_parallel_map']) self.run_thread('radpro_ncpus') fit_results = ui.get_fit_results() covarerr = sqrt(fit_results.extra_output['covar'].diagonal()) cmp_radpro(fit_results, self.locals, covarerr) if _ncpus != 1: assert fit_results.extra_output['num_parallel_map'] > 0 else: assert fit_results.extra_output['num_parallel_map'] == 0
def test_sourceandbg(self): def cmp_sourceanddbg(fit_results, mylocals, covarerr): a1 = mylocals['a1'] b1 = mylocals['b1'] b2 = mylocals['b2'] assert covarerr[0] == approx(0.012097, rel=1e-3) assert covarerr[1] == approx(0, rel=1e-3) assert covarerr[2] == approx(0.000280678, rel=1e-3) assert covarerr[3] == approx(0.00990783, rel=1e-3) assert covarerr[4] == approx(2.25746e-07, rel=1e-3) assert fit_results.statval == approx(947.5, rel=1e-4) assert fit_results.rstat == approx(0.715094, rel=1e-4) assert fit_results.qval == approx(1, rel=1e-4) assert a1.nh.val == approx(0.0342266, rel=1e-2) assert b1.kt.val == approx(20, rel=1e-2) self.assertEqualWithinTol(b1.norm.val, 0.00953809, 1e-2) self.assertEqualWithinTol(b2.kt.val, 0.563109, 1e-2) self.assertEqualWithinTol(b2.norm.val, 1.16118e-05, 1e-2) self.assertEqual(fit_results.numpoints, 1330) self.assertEqual(fit_results.dof, 1325) self.run_thread('sourceandbg') fit_results = ui.get_fit_results() covarerr = sqrt(fit_results.extra_output['covar'].diagonal()) cmp_sourceanddbg(fit_results, self.locals, covarerr) self.assertEqual(0, fit_results.extra_output['num_parallel_map']) self.run_thread('sourceandbg_ncpus') fit_results = ui.get_fit_results() covarerr = sqrt(fit_results.extra_output['covar'].diagonal()) cmp_sourceanddbg(fit_results, self.locals, covarerr) if _ncpus != 1: fit_results.extra_output['num_parallel_map'] > 0 else: assert fit_results.extra_output['num_parallel_map'] == 0
def test_kernel(self): def cmp_kernel(fit_results, mylocals, covarerr): b1 = mylocals['b1'] assert covarerr[0] == approx(0.210895, rel=1e-4) assert covarerr[1] == approx(0.00154839, rel=1e-4) assert covarerr[2] == approx(0.0223859, rel=1e-4) assert fit_results.statval == approx(98.5793, rel=1e-4) assert b1.r0.val == approx(19.2278, rel=1e-4) assert b1.beta.val == approx(0.555464, rel=1e-4) assert b1.ampl.val == approx(1.93706, rel=1e-4) self.assertEqual(fit_results.nfev, 21) self.assertEqual(fit_results.numpoints, 75) self.assertEqual(fit_results.dof, 72) self.run_thread('kernel') fit_results = ui.get_fit_results() covarerr = sqrt(fit_results.extra_output['covar'].diagonal()) cmp_kernel(fit_results, self.locals, covarerr) self.assertEqual(0, fit_results.extra_output['num_parallel_map']) self.run_thread('kernel_ncpus') fit_results = ui.get_fit_results() covarerr = sqrt(fit_results.extra_output['covar'].diagonal()) cmp_kernel(fit_results, self.locals, covarerr) if _ncpus != 1: assert fit_results.extra_output['num_parallel_map'] > 0 else: assert fit_results.extra_output['num_parallel_map'] == 0
def test_xmm(self): def cmp_xmm(fres, mylocals, covarerr): intrin = mylocals['intrin'] phard = mylocals['phard'] assert covarerr[0] == approx(0.954993, rel=1e-3) assert covarerr[1] == approx(0.142357, rel=1e-3) assert covarerr[2] == approx(0.00038775, rel=1e-3) assert fres.statval == approx(118.085, rel=1e-4) self.assertEqualWithinTol(intrin.nh.val, 11.0769, 1e-2) self.assertEqualWithinTol(phard.phoindex.val, 1.49055, 1e-2) self.assertEqualWithinTol(phard.norm.val, 0.00140301, 1e-2) self.assertEqual(fres.nfev, 95) self.assertEqual(fres.numpoints, 162) self.assertEqual(fres.dof, 159) self.run_thread('xmm') fres = ui.get_fit_results() covarerr = sqrt(fres.extra_output['covar'].diagonal()) cmp_xmm(fres, self.locals, covarerr) self.assertEqual(0, fres.extra_output['num_parallel_map']) self.run_thread('xmm_ncpus') fres = ui.get_fit_results() covarerr = sqrt(fres.extra_output['covar'].diagonal()) cmp_xmm(fres, self.locals, covarerr) if _ncpus != 1: assert fres.extra_output['num_parallel_map'] > 0 else: assert fres.extra_output['num_parallel_map'] == 0
def test_spectrum(self): def cmp_spectrum(fres, mylocals, covarerr): abs2 = mylocals['abs2'] mek1 = mylocals['mek1'] mek2 = mylocals['mek2'] assert covarerr[0] == approx(0.00148391, rel=1e-3) assert covarerr[1] == approx(0.0011518, rel=1e-3) assert covarerr[2] == approx(0.00377755, rel=1e-3) assert covarerr[3] == approx(0.00370543, rel=1e-3) assert covarerr[4] == approx(0.0016608, rel=1e-3) assert fres.statval == approx(0.0496819, rel=1e-4) assert abs2.nh.val == approx(1.1015, rel=1e-4) assert mek1.kt.val == approx(0.841025, rel=1e-4) assert mek1.norm.val == approx(0.699761, rel=1e-4) assert mek2.kt.val == approx(2.35845, rel=1e-4) assert mek2.norm.val == approx(1.03724, rel=1e-4) self.assertEqual(fres.numpoints, 446) self.assertEqual(fres.dof, 441) self.run_thread('spectrum') fres = ui.get_fit_results() covarerr = sqrt(fres.extra_output['covar'].diagonal()) cmp_spectrum(fres, self.locals, covarerr) self.assertEqual(0, fres.extra_output['num_parallel_map']) self.run_thread('spectrum_ncpus') fres = ui.get_fit_results() covarerr = sqrt(fres.extra_output['covar'].diagonal()) cmp_spectrum(fres, self.locals, covarerr) if _ncpus != 1: assert fres.extra_output['num_parallel_map'] > 0 else: assert fres.extra_output['num_parallel_map'] == 0
def test_grouped_ciao4_5(self): def cmp_grouped_ciao4_5(fres, mylocals, covarerr): assert covarerr[0] == approx(0.104838, rel=1e-4) assert covarerr[1] == approx(2.43937e-05, rel=1e-4) assert fres.statval == approx(18.8316, rel=1e-4) self.assertEqual(fres.numpoints, 46) aamdl = mylocals['aa'] assert aamdl.gamma.val == approx(1.83906, rel=1e-4) assert aamdl.ampl.val == approx(0.000301258, rel=1e-4) self.run_thread('grouped_ciao4.5') fres = ui.get_fit_results() covarerr = sqrt(fres.extra_output['covar'].diagonal()) cmp_grouped_ciao4_5(fres, self.locals, covarerr) self.assertEqual(0, fres.extra_output['num_parallel_map']) self.run_thread('grouped_ciao4.5_ncpus') fres = ui.get_fit_results() covarerr = sqrt(fres.extra_output['covar'].diagonal()) cmp_grouped_ciao4_5(fres, self.locals, covarerr) if _ncpus != 1: assert fres.extra_output['num_parallel_map'] > 0 else: assert fres.extra_output['num_parallel_map'] == 0
def test_proj_bubble(self): def cmp_proj_bubble(mylocals, convarerr, proj, conf): mek1 = mylocals['mek1'] assert covarerr[0] == approx(0, rel=1e-4) assert covarerr[1] == approx(8.74608e-07, rel=1e-3) assert mek1.kt.val == approx(17.8849, rel=1e-2) assert mek1.norm.val == approx(4.15418e-06, rel=1e-2) # Proj -- Upper bound of kT can't be found # assert proj.parmins[0] == approx(-12.048069, rel=0.01) assert proj.parmins[1] == approx(-9.510913e-07, rel=0.01) assert proj.parmaxes[1] == approx(2.403640e-06, rel=0.01) assert proj.parmaxes[0] is None assert conf.parmins[0] == approx(-12.1073, rel=0.01) assert conf.parmaxes[0] == approx(62.0585, rel=0.01) assert conf.parmins[1] == approx(-9.5568e-07, rel=0.01) assert conf.parmaxes[1] == approx(2.39937e-06, rel=0.01) self.run_thread('proj_bubble') fit_results = ui.get_fit_results() covarerr = sqrt(fit_results.extra_output['covar'].diagonal()) proj = ui.get_proj_results() conf = ui.get_conf_results() cmp_proj_bubble(self.locals, covarerr, proj, conf) self.run_thread('proj_bubble_ncpus') fit_results = ui.get_fit_results() covarerr = sqrt(fit_results.extra_output['covar'].diagonal()) proj = ui.get_proj_results() conf = ui.get_conf_results() cmp_proj_bubble(self.locals, covarerr, proj, conf)
def test_simultaneous(self): def cmp_simultaneous(fit_results, mylocals, covarerr): abs1 = mylocals['abs1'] pl1 = mylocals['pl1'] pl2 = mylocals['pl2'] assert covarerr[0] == approx(0.397769, rel=1e-3) assert covarerr[1] == approx(0.486058, rel=1e-3) assert covarerr[2] == approx(1.48213e-05, rel=1e-3) assert covarerr[3] == approx(1.54245e-05, rel=1e-3) assert fit_results.statval == approx(7.4429, rel=1e-4) assert fit_results.rstat == approx(0.531636, rel=1e-4) assert fit_results.qval == approx(0.916288, rel=1e-4) assert abs1.nh.val == approx(0.898162, rel=1e-2) assert pl1.gamma.val == approx(1.645, rel=1e-4) self.assertEqualWithinTol(pl1.ampl.val, 2.28323e-05, 1e-3) self.assertEqualWithinTol(pl2.ampl.val, 2.44585e-05, 1e-3) self.assertEqual(fit_results.numpoints, 18) self.assertEqual(fit_results.dof, 14) self.run_thread('simultaneous') fit_results = ui.get_fit_results() covarerr = sqrt(fit_results.extra_output['covar'].diagonal()) cmp_simultaneous(fit_results, self.locals, covarerr) self.assertEqual(0, fit_results.extra_output['num_parallel_map']) self.run_thread('simultaneous_ncpus') fit_results = ui.get_fit_results() covarerr = sqrt(fit_results.extra_output['covar'].diagonal()) cmp_simultaneous(fit_results, self.locals, covarerr) if _ncpus != 1: assert fit_results.extra_output['num_parallel_map'] > 0 else: assert fit_results.extra_output['num_parallel_map'] == 0
def test_xmm(self): self.run_thread('xmm') self.assertEqualWithinTol(ui.get_fit_results().statval, 118.085, 1e-4) self.assertEqualWithinTol(self.locals['intrin'].nh.val, 11.0769, 1e-2) self.assertEqualWithinTol(self.locals['phard'].phoindex.val, 1.49055, 1e-2) self.assertEqualWithinTol(self.locals['phard'].norm.val, 0.00140301, 1e-2) self.assertEqual(ui.get_fit_results().nfev, 95) self.assertEqual(ui.get_fit_results().numpoints, 162) self.assertEqual(ui.get_fit_results().dof, 159)
def test_kernel(self): self.run_thread('kernel') self.assertEqualWithinTol(ui.get_fit_results().statval, 98.5793, 1e-4) self.assertEqualWithinTol(self.locals['b1'].r0.val, 19.2278, 1e-4) self.assertEqualWithinTol(self.locals['b1'].beta.val, 0.555464, 1e-4) self.assertEqualWithinTol(self.locals['b1'].ampl.val, 1.93706, 1e-4) self.assertEqual(ui.get_fit_results().nfev,21) self.assertEqual(ui.get_fit_results().numpoints,75) self.assertEqual(ui.get_fit_results().dof,72)
def test_linepro(self): self.run_thread('linepro') self.assertEqualWithinTol(ui.get_fit_results().statval, 203.34, 1e-4) self.assertEqualWithinTol(self.locals['b1'].r0.val, 4.25557, 1e-4) self.assertEqualWithinTol(self.locals['b1'].beta.val, 0.492232, 1e-4) self.assertEqualWithinTol(self.locals['b1'].ampl.val, 11.8129, 1e-4) self.assertEqual(ui.get_fit_results().nfev,17) self.assertEqual(ui.get_fit_results().numpoints,75) self.assertEqual(ui.get_fit_results().dof,72)
def test_radpro_psf(self): self.run_thread('radpro_psf') self.assertEqualWithinTol(ui.get_fit_results().statval, 200.949, 1e-4) self.assertEqualWithinTol(self.locals['src'].r0.val, 83.0997, 1e-4) self.assertEqualWithinTol(self.locals['src'].beta.val, 2.97737, 1e-4) self.assertEqualWithinTol(self.locals['src'].ampl.val, 5.27604, 1e-4) self.assertEqual(ui.get_fit_results().nfev,48) self.assertEqual(ui.get_fit_results().numpoints,38) self.assertEqual(ui.get_fit_results().dof,35)
def test_histo(self): self.run_thread('histo') assert ui.get_fit_results().statval == approx(14.7264, rel=1e-4) assert self.locals['g1'].fwhm.val == approx(0.0232473, rel=1e-4) assert self.locals['g1'].pos.val == approx(1.26713, rel=1e-4) assert self.locals['g1'].ampl.val == approx(40.4503, rel=1e-4) # self.assertEqual(ui.get_fit_results().nfev, 19) self.assertEqual(ui.get_fit_results().numpoints, 50) self.assertEqual(ui.get_fit_results().dof, 47)
def test_kernel(self): self.run_thread('kernel') self.assertEqualWithinTol(ui.get_fit_results().statval, 98.5793, 1e-4) self.assertEqualWithinTol(self.locals['b1'].r0.val, 19.2278, 1e-4) self.assertEqualWithinTol(self.locals['b1'].beta.val, 0.555464, 1e-4) self.assertEqualWithinTol(self.locals['b1'].ampl.val, 1.93706, 1e-4) self.assertEqual(ui.get_fit_results().nfev, 21) self.assertEqual(ui.get_fit_results().numpoints, 75) self.assertEqual(ui.get_fit_results().dof, 72)
def test_radpro_psf(self): self.run_thread('radpro_psf') assert ui.get_fit_results().statval == approx(200.949, rel=1e-4) assert self.locals['src'].r0.val == approx(83.0997, rel=1e-4) assert self.locals['src'].beta.val == approx(2.97737, rel=1e-4) assert self.locals['src'].ampl.val == approx(5.27604, rel=1e-4) self.assertEqual(ui.get_fit_results().nfev, 48) self.assertEqual(ui.get_fit_results().numpoints, 38) self.assertEqual(ui.get_fit_results().dof, 35)
def test_radpro_psf(self): self.run_thread('radpro_psf') self.assertEqualWithinTol(ui.get_fit_results().statval, 200.949, 1e-4) self.assertEqualWithinTol(self.locals['src'].r0.val, 83.0997, 1e-4) self.assertEqualWithinTol(self.locals['src'].beta.val, 2.97737, 1e-4) self.assertEqualWithinTol(self.locals['src'].ampl.val, 5.27604, 1e-4) self.assertEqual(ui.get_fit_results().nfev, 48) self.assertEqual(ui.get_fit_results().numpoints, 38) self.assertEqual(ui.get_fit_results().dof, 35)
def test_xmm(self): self.run_thread('xmm') self.assertEqualWithinTol(ui.get_fit_results().statval, 118.085, 1e-4) self.assertEqualWithinTol(self.locals['intrin'].nh.val, 11.0769, 1e-2) self.assertEqualWithinTol(self.locals['phard'].phoindex.val, 1.49055, 1e-2) self.assertEqualWithinTol(self.locals['phard'].norm.val, 0.00140301, 1e-2) self.assertEqual(ui.get_fit_results().nfev,95) self.assertEqual(ui.get_fit_results().numpoints,162) self.assertEqual(ui.get_fit_results().dof,159)
def test_histo(self): self.run_thread('histo') self.assertEqualWithinTol(ui.get_fit_results().statval, 14.7264, 1e-4) self.assertEqualWithinTol(self.locals['g1'].fwhm.val, 0.0232473, 1e-4) self.assertEqualWithinTol(self.locals['g1'].pos.val, 1.26713, 1e-4) self.assertEqualWithinTol(self.locals['g1'].ampl.val, 40.4503, 1e-4) # self.assertEqual(ui.get_fit_results().nfev,19) self.assertEqual(ui.get_fit_results().numpoints, 50) self.assertEqual(ui.get_fit_results().dof, 47)
def test_histo(self): self.run_thread('histo') self.assertEqualWithinTol(ui.get_fit_results().statval, 14.7264, 1e-4) self.assertEqualWithinTol(self.locals['g1'].fwhm.val, 0.0232473, 1e-4) self.assertEqualWithinTol(self.locals['g1'].pos.val, 1.26713, 1e-4) self.assertEqualWithinTol(self.locals['g1'].ampl.val, 40.4503, 1e-4) #self.assertEqual(ui.get_fit_results().nfev,19) self.assertEqual(ui.get_fit_results().numpoints,50) self.assertEqual(ui.get_fit_results().dof,47)
def test_linepro(self): self.run_thread('linepro') self.assertEqualWithinTol(ui.get_fit_results().statval, 203.34, 1e-4) self.assertEqualWithinTol(self.locals['b1'].r0.val, 4.25557, 1e-4) self.assertEqualWithinTol(self.locals['b1'].beta.val, 0.492232, 1e-4) self.assertEqualWithinTol(self.locals['b1'].ampl.val, 11.8129, 1e-4) self.assertEqual(ui.get_fit_results().nfev, 17) self.assertEqual(ui.get_fit_results().numpoints, 75) self.assertEqual(ui.get_fit_results().dof, 72)
def test_spectrum(self): self.run_thread('spectrum') self.assertEqualWithinTol(ui.get_fit_results().statval, 0.0496819, 1e-4) self.assertEqualWithinTol(self.locals['abs2'].nh.val, 1.1015, 1e-4) self.assertEqualWithinTol(self.locals['mek1'].kt.val, 0.841025, 1e-4) self.assertEqualWithinTol(self.locals['mek1'].norm.val, 0.699761, 1e-4) self.assertEqualWithinTol(self.locals['mek2'].kt.val, 2.35845, 1e-4) self.assertEqualWithinTol(self.locals['mek2'].norm.val, 1.03724, 1e-4) self.assertEqual(ui.get_fit_results().numpoints,446) self.assertEqual(ui.get_fit_results().dof,441)
def test_psf2d(self): self.run_thread('psf') self.assertEqualWithinTol(ui.get_fit_results().statval, 4066.78, 1e-4) self.assertEqualWithinTol(self.locals['g1'].fwhm.val, 2.80117, 1e-2) self.assertEqualWithinTol(self.locals['g1'].ypos.val, 77.2271, 1e-2) self.assertEqualWithinTol(self.locals['g1'].xpos.val, 88.661, 1e-2) self.assertEqualWithinTol(self.locals['g1'].ampl.val, 166.649, 1e-2) # self.assertEqual(ui.get_fit_results().nfev,342) self.assertEqual(ui.get_fit_results().numpoints, 4899) self.assertEqual(ui.get_fit_results().dof, 4895)
def test_spectrum(self): self.run_thread('spectrum') self.assertEqualWithinTol(ui.get_fit_results().statval, 0.0496819, 1e-4) self.assertEqualWithinTol(self.locals['abs2'].nh.val, 1.1015, 1e-4) self.assertEqualWithinTol(self.locals['mek1'].kt.val, 0.841025, 1e-4) self.assertEqualWithinTol(self.locals['mek1'].norm.val, 0.699761, 1e-4) self.assertEqualWithinTol(self.locals['mek2'].kt.val, 2.35845, 1e-4) self.assertEqualWithinTol(self.locals['mek2'].norm.val, 1.03724, 1e-4) self.assertEqual(ui.get_fit_results().numpoints, 446) self.assertEqual(ui.get_fit_results().dof, 441)
def test_psf2d(self): self.run_thread('psf') self.assertEqualWithinTol(ui.get_fit_results().statval, 4066.78, 1e-4) self.assertEqualWithinTol(self.locals['g1'].fwhm.val, 2.80117, 1e-2) self.assertEqualWithinTol(self.locals['g1'].ypos.val, 77.2271, 1e-2) self.assertEqualWithinTol(self.locals['g1'].xpos.val, 88.661, 1e-2) self.assertEqualWithinTol(self.locals['g1'].ampl.val, 166.649, 1e-2) #self.assertEqual(ui.get_fit_results().nfev,342) self.assertEqual(ui.get_fit_results().numpoints,4899) self.assertEqual(ui.get_fit_results().dof,4895)
def test_psf2d(self): self.run_thread('psf') assert ui.get_fit_results().statval == approx(4066.78, rel=1e-4) assert self.locals['g1'].fwhm.val == approx(2.80117, rel=1e-2) assert self.locals['g1'].ypos.val == approx(77.2271, rel=1e-2) assert self.locals['g1'].xpos.val == approx(88.661, rel=1e-2) assert self.locals['g1'].ampl.val == approx(166.649, rel=1e-2) # self.assertEqual(ui.get_fit_results().nfev, 342) self.assertEqual(ui.get_fit_results().numpoints, 4899) self.assertEqual(ui.get_fit_results().dof, 4895)
def test_simultaneous(self): self.run_thread('simultaneous') self.assertEqualWithinTol(ui.get_fit_results().statval, 7.4429, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 0.531636, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 0.916288, 1e-4) self.assertEqualWithinTol(self.locals['abs1'].nh.val, 0.898162, 1e-2) self.assertEqualWithinTol(self.locals['pl1'].gamma.val, 1.645, 1e-4) self.assertEqualWithinTol(self.locals['pl1'].ampl.val, 2.28323e-05, 1e-3) self.assertEqualWithinTol(self.locals['pl2'].ampl.val, 2.44585e-05, 1e-3) self.assertEqual(ui.get_fit_results().numpoints, 18) self.assertEqual(ui.get_fit_results().dof, 14)
def test_lev3fft(self): self.run_thread('lev3fft', scriptname='bar.py') self.assertEqualWithinTol(self.locals['src'].fwhm.val, 0.04418584, 1e-4) self.assertEqualWithinTol(self.locals['src'].xpos.val, 150.016, 1e-4) self.assertEqualWithinTol(self.locals['src'].ypos.val, 2.66493839, 1e-4) self.assertEqualWithinTol(self.locals['src'].ampl.val, 1.56090546, 1e-4) self.assertEqualWithinTol(self.locals['bkg'].c0.val, -1.513700715, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().istatval, 19496.3, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().statval, 592.32647, 1e-4) self.assertEqual(ui.get_fit_results().numpoints, 3307) self.assertEqual(ui.get_fit_results().dof, 3302)
def test_simultaneous(self): self.run_thread('simultaneous') self.assertEqualWithinTol(ui.get_fit_results().statval, 7.4429, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 0.531636, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 0.916288, 1e-4) self.assertEqualWithinTol(self.locals['abs1'].nh.val, 0.898162, 1e-2) self.assertEqualWithinTol(self.locals['pl1'].gamma.val, 1.645, 1e-4) self.assertEqualWithinTol(self.locals['pl1'].ampl.val, 2.28323e-05, 1e-3) self.assertEqualWithinTol(self.locals['pl2'].ampl.val, 2.44585e-05, 1e-3) self.assertEqual(ui.get_fit_results().numpoints,18) self.assertEqual(ui.get_fit_results().dof,14)
def test_pileup(self): self.run_thread('pileup') self.assertEqualWithinTol(ui.get_fit_results().statval, 53.6112, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 1.44895, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 0.0379417, 1e-4) self.assertEqualWithinTol(self.locals['jdp'].alpha.val, 0.522593, 1e-1) self.assertEqualWithinTol(self.locals['jdp'].f.val, 0.913458, 1e-2) self.assertEqualWithinTol(self.locals['abs1'].nh.val, 6.12101, 1e-2) self.assertEqualWithinTol(self.locals['power'].gamma.val, 1.41887, 1e-2) self.assertEqualWithinTol(self.locals['power'].ampl.val, 0.00199457, 1e-2) self.assertEqual(ui.get_fit_results().numpoints,42) self.assertEqual(ui.get_fit_results().dof,37)
def test_sourceandbg(self): self.run_thread('sourceandbg') self.assertEqualWithinTol(ui.get_fit_results().statval, 947.5, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 0.715094, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 1, 1e-4) self.assertEqualWithinTol(self.locals['a1'].nh.val, 0.0342266, 1e-2) self.assertEqualWithinTol(self.locals['b1'].kt.val, 20, 1e-2) self.assertEqualWithinTol(self.locals['b1'].norm.val, 0.00953809, 1e-2) self.assertEqualWithinTol(self.locals['b2'].kt.val, 0.563109, 1e-2) self.assertEqualWithinTol(self.locals['b2'].norm.val, 1.16118e-05, 1e-2) self.assertEqual(ui.get_fit_results().numpoints,1330) self.assertEqual(ui.get_fit_results().dof,1325)
def test_sourceandbg(self): self.run_thread('sourceandbg') self.assertEqualWithinTol(ui.get_fit_results().statval, 947.5, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 0.715094, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 1, 1e-4) self.assertEqualWithinTol(self.locals['a1'].nh.val, 0.0342266, 1e-2) self.assertEqualWithinTol(self.locals['b1'].kt.val, 20, 1e-2) self.assertEqualWithinTol(self.locals['b1'].norm.val, 0.00953809, 1e-2) self.assertEqualWithinTol(self.locals['b2'].kt.val, 0.563109, 1e-2) self.assertEqualWithinTol(self.locals['b2'].norm.val, 1.16118e-05, 1e-2) self.assertEqual(ui.get_fit_results().numpoints, 1330) self.assertEqual(ui.get_fit_results().dof, 1325)
def test_pileup(self): self.run_thread('pileup') self.assertEqualWithinTol(ui.get_fit_results().statval, 53.6112, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 1.44895, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 0.0379417, 1e-4) self.assertEqualWithinTol(self.locals['jdp'].alpha.val, 0.522593, 1e-1) self.assertEqualWithinTol(self.locals['jdp'].f.val, 0.913458, 1e-2) self.assertEqualWithinTol(self.locals['abs1'].nh.val, 6.12101, 1e-2) self.assertEqualWithinTol(self.locals['power'].gamma.val, 1.41887, 1e-2) self.assertEqualWithinTol(self.locals['power'].ampl.val, 0.00199457, 1e-2) self.assertEqual(ui.get_fit_results().numpoints, 42) self.assertEqual(ui.get_fit_results().dof, 37)
def test_spatial(self): self.run_thread('spatial') self.assertEqualWithinTol(ui.get_fit_results().statval, -59229.749441, 1e-4) self.assertEqualWithinTol(self.locals['g1'].fwhm.val, 61.5615, 1e-4) self.assertEqualWithinTol(self.locals['g1'].xpos.val, 4070.45, 1e-4) self.assertEqualWithinTol(self.locals['g1'].ypos.val, 4251.35, 1e-4) self.assertEqualWithinTol(self.locals['g1'].ampl.val, 22.1269, 1e-4) self.assertEqualWithinTol(self.locals['g2'].fwhm.val, 6.20409, 1e-4) self.assertEqualWithinTol(self.locals['g2'].xpos.val, 4070.78, 1e-4) self.assertEqualWithinTol(self.locals['g2'].ypos.val, 4249.33, 1e-4) self.assertEqualWithinTol(self.locals['g2'].ampl.val, 226.563, 1e-4) #self.assertEqual(ui.get_fit_results().nfev,371) self.assertEqual(ui.get_fit_results().numpoints,4881) self.assertEqual(ui.get_fit_results().dof,4877)
def test_spatial(self): self.run_thread('spatial') self.assertEqualWithinTol(ui.get_fit_results().statval, -59229.749441, 1e-4) self.assertEqualWithinTol(self.locals['g1'].fwhm.val, 61.5615, 1e-4) self.assertEqualWithinTol(self.locals['g1'].xpos.val, 4070.45, 1e-4) self.assertEqualWithinTol(self.locals['g1'].ypos.val, 4251.35, 1e-4) self.assertEqualWithinTol(self.locals['g1'].ampl.val, 22.1269, 1e-4) self.assertEqualWithinTol(self.locals['g2'].fwhm.val, 6.20409, 1e-4) self.assertEqualWithinTol(self.locals['g2'].xpos.val, 4070.78, 1e-4) self.assertEqualWithinTol(self.locals['g2'].ypos.val, 4249.33, 1e-4) self.assertEqualWithinTol(self.locals['g2'].ampl.val, 226.563, 1e-4) # self.assertEqual(ui.get_fit_results().nfev,371) self.assertEqual(ui.get_fit_results().numpoints, 4881) self.assertEqual(ui.get_fit_results().dof, 4877)
def test_fpsf2d(self): self.run_thread('fpsf') self.assertEqualWithinTol(ui.get_fit_results().statval, -4053.6635, 1e-4) # self.assertEqualWithinTol(self.locals['b1'].xlow.val, -4.70832, 1e-4) # self.assertEqualWithinTol(self.locals['b1'].xhi.val, 164.687, 1e-4) # self.assertEqualWithinTol(self.locals['b1'].ylow.val, 0.83626, 1e-4) # self.assertEqualWithinTol(self.locals['b1'].yhi.val, 142.603, 1e-4) # self.assertEqualWithinTol(self.locals['b1'].ampl.val, 0.956766, 1e-4) self.assertEqualWithinTol(self.locals['g1'].fwhm.val, 6.420237, 1e-4) self.assertEqualWithinTol(self.locals['g1'].xpos.val, 88.940712, 1e-4) self.assertEqualWithinTol(self.locals['g1'].ypos.val, 76.577265, 1e-4) self.assertEqualWithinTol(self.locals['g1'].ampl.val, 36344.48324, 1e-4) #self.assertEqual(ui.get_fit_results().nfev,978) self.assertEqual(ui.get_fit_results().numpoints,4899) self.assertEqual(ui.get_fit_results().dof,4895)
def test_fpsf2d(self): self.run_thread('fpsf') self.assertEqualWithinTol(ui.get_fit_results().statval, -4053.6635, 1e-4) # self.assertEqualWithinTol(self.locals['b1'].xlow.val, -4.70832, 1e-4) # self.assertEqualWithinTol(self.locals['b1'].xhi.val, 164.687, 1e-4) # self.assertEqualWithinTol(self.locals['b1'].ylow.val, 0.83626, 1e-4) # self.assertEqualWithinTol(self.locals['b1'].yhi.val, 142.603, 1e-4) # self.assertEqualWithinTol(self.locals['b1'].ampl.val, 0.956766, 1e-4) self.assertEqualWithinTol(self.locals['g1'].fwhm.val, 6.420237, 1e-4) self.assertEqualWithinTol(self.locals['g1'].xpos.val, 88.940712, 1e-4) self.assertEqualWithinTol(self.locals['g1'].ypos.val, 76.577265, 1e-4) self.assertEqualWithinTol(self.locals['g1'].ampl.val, 36344.48324, 1e-4) # self.assertEqual(ui.get_fit_results().nfev,978) self.assertEqual(ui.get_fit_results().numpoints, 4899) self.assertEqual(ui.get_fit_results().dof, 4895)
def test_eqwith_err1(make_data_path, restore_xspec_settings): def check1(e0, e1, e2): assert e0 == approx(0.028335201547206704, rel=1.0e-3) assert e1 == approx(-0.00744118799274448756, rel=1.0e-3) assert e2 == approx(0.0706249544851336, rel=1.0e-3) ui.set_xsabund('angr') ui.set_xsxsect('bcmc') ui.load_pha(make_data_path('3c273.pi')) ui.notice(0.5, 7.0) ui.set_stat("chi2datavar") ui.set_method("simplex") ui.set_model('powlaw1d.p1+gauss1d.g1') g1.fwhm = 0.1 g1.pos = 2.0 ui.freeze(g1.pos, g1.fwhm) ui.fit() numpy.random.seed(2345) e = ui.eqwidth(p1, p1 + g1, error=True, niter=100) check1(e[0], e[1], e[2]) params = e[3] numpy.random.seed(2345) e = ui.eqwidth(p1, p1 + g1, error=True, params=params, niter=100) check1(e[0], e[1], e[2]) parvals = ui.get_fit_results().parvals assert parvals[0] == approx(1.9055272902160334, rel=1.0e-3) assert parvals[1] == approx(0.00017387966749772638, rel=1.0e-3) assert parvals[2] == approx(1.279415076070516e-05, rel=1.0e-3)
def test_ARFModelPHA(self): from sherpa.astro import ui ui.load_pha(self.make_path("3c120_meg_1.pha")) # remove the RMF to ensure this is an ARF-only analysis # (which is what is needed to trigger the bug that lead to #699) ui.get_data().set_rmf(None) ui.group_counts(20) ui.notice(0.5, 6) ui.subtract() ui.set_model(ui.xsphabs.abs1 * (ui.xsapec.bubble + ui.powlaw1d.p1)) ui.set_xsabund('angr') ui.set_xsxsect('vern') abs1.nh = 0.163 abs1.nh.freeze() p1.ampl = 0.017 p1.gamma = 1.9 bubble.kt = 0.5 bubble.norm = 4.2e-5 tol = 1.0e-2 ui.set_method_opt('ftol', tol) ui.fit() result = ui.get_fit_results() assert result.numpoints == self._fit_using_ARFModelPHA['numpoints'] assert result.dof == self._fit_using_ARFModelPHA['dof']
def test_radpro_dm(self): # This test is completely redundant to test_radpro above. # The only difference is that here I test if using DM syntax # in the file name returns the correct arrays from CRATES. # Since any other I/O backend would not understand DM syntax, # I make this test a no-op unless CRATES is used as the I/O # backend. SMD 05/23/13 if self.is_crates_io: self.run_thread('radpro_dm') fres = ui.get_fit_results() covarerr = sqrt(fres.extra_output['covar'].diagonal()) assert covarerr[0] == approx(9.37344776, rel=1e-4) assert covarerr[1] == approx(0.51259645, rel=1e-4) assert covarerr[2] == approx(0.06911017, rel=1e-4) assert fres.statval == approx(217.450, rel=1e-4) assert fres.rstat == approx(6.21287, rel=1e-4) assert fres.qval == approx(0.0, rel=1e-4) srcmdl = self.locals['src'] assert srcmdl.r0.val == approx(125.829, rel=1e-4) assert srcmdl.beta.val == approx(4.1633, rel=1e-4) assert srcmdl.xpos.val == approx(0.0, rel=1e-4) assert srcmdl.ampl.val == approx(4.42821, rel=1e-4) self.assertEqual(fres.nfev, 92) self.assertEqual(fres.numpoints, 38) self.assertEqual(fres.dof, 35)
def test_user_stat_unit(): given_stat_error = [1.1, 2.2, 3.3] given_sys_error = [10.1, 10.2, 10.3] def calc_stat(data, _model, staterror, syserror=None, weight=None): # Make sure values are being injected correctly np.testing.assert_array_equal(given_stat_error, staterror) np.testing.assert_array_equal(given_sys_error, syserror) return 3.235, np.ones_like(data) xdata = [1, 2, 3] ydata = xdata ui.load_arrays(1, xdata, ydata, None, given_sys_error, Data1D) ui.set_model(1, 'polynom1d.p') ui.load_user_stat('customstat', calc_stat, lambda x: given_stat_error) ui.set_stat(eval('customstat')) try: ui.fit(1) except StatErr: pytest.fail("Call should not be throwing any exception (bug #341)") # Test the result is what we made the user stat return assert 3.235 == ui.get_fit_results().statval
def fit(self, conf = False): ui.ignore(None, None) ui.notice(self.start, self.stop) self.set_source() ui.fit(1) if conf: ui.conf() res = ui.get_fit_results() for line in (self.H2lines + self.nonH2lines): sourcename = line['source'].split('.')[1] print sourcename for p in ['pos', 'fwhm', 'ampl']: n = '{0}.{1}'.format(sourcename, p) _place_val(line, p, ui.get_par(n).val) self.const = ui.get_par('c1.c0').val self.redchi2 = res.rstat if conf: res = ui.get_conf_results() for line in (self.H2lines + self.nonH2lines): sourcename = line['source'].split('.')[1] for p in ['pos', 'fwhm', 'ampl']: n = '{0}.{1}'.format(sourcename, p) parmin, parmax = _parminmax(res, n) line[p+'_max'] = parmax line[p+'_min'] = parmin # deal with error on const parmin, parmax = _parminmax(res, 'c1.c0') self.const_min = parmin self.const_max = parmax
def test_background(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) kT_sim = 1.0 Z_sim = 0.0 norm_sim = 4.0e-2 nH_sim = 0.04 redshift = 0.01 exp_time = (200., "ks") area = (1000., "cm**2") fov = (10.0, "arcmin") prng = 24 agen = ApecGenerator(0.05, 12.0, 5000, broadening=False) spec = agen.get_spectrum(kT_sim, Z_sim, redshift, norm_sim) spec.apply_foreground_absorption(norm_sim) events = make_background(area, exp_time, fov, (30.0, 45.0), spec, prng=prng) events.write_simput_file("bkgnd", overwrite=True) instrument_simulator("bkgnd_simput.fits", "bkgnd_evt.fits", exp_time, "sq_acisi_cy19", [30.0, 45.0], overwrite=True, foreground=False, ptsrc_bkgnd=False, instr_bkgnd=False, prng=prng) write_spectrum("bkgnd_evt.fits", "background_evt.pi", overwrite=True) os.system("cp %s %s ." % (arf.filename, rmf.filename)) load_user_model(mymodel, "wapec") add_user_pars("wapec", ["nH", "kT", "metallicity", "redshift", "norm"], [0.01, 4.0, 0.2, redshift, norm_sim*0.8], parmins=[0.0, 0.1, 0.0, -20.0, 0.0], parmaxs=[10.0, 20.0, 10.0, 20.0, 1.0e9], parfrozen=[False, False, False, True, False]) load_pha("background_evt.pi") set_stat("cstat") set_method("simplex") ignore(":0.5, 8.0:") set_model("wapec") fit() res = get_fit_results() assert np.abs(res.parvals[0]-nH_sim)/nH_sim < 0.1 assert np.abs(res.parvals[1]-kT_sim)/kT_sim < 0.05 assert np.abs(res.parvals[2]-Z_sim) < 0.05 assert np.abs(res.parvals[3]-norm_sim)/norm_sim < 0.05 os.chdir(curdir) shutil.rmtree(tmpdir)
def test_point_source(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) nH_sim = 0.02 norm_sim = 1.0e-4 alpha_sim = 0.95 redshift = 0.02 exp_time = (100., "ks") area = (3000., "cm**2") spec = Spectrum.from_powerlaw(alpha_sim, redshift, norm_sim, emin=0.1, emax=11.5, nbins=2000) spec.apply_foreground_absorption(nH_sim, model="tbabs") positions = [(30.01, 45.0)] events = make_point_sources(area, exp_time, positions, (30.0, 45.0), spec, prng=prng) events.write_simput_file("ptsrc", overwrite=True) instrument_simulator("ptsrc_simput.fits", "ptsrc_evt.fits", exp_time, "sq_aciss_cy19", [30.0, 45.0], overwrite=True, foreground=False, ptsrc_bkgnd=False, instr_bkgnd=False, prng=prng) write_spectrum("ptsrc_evt.fits", "point_source_evt.pi", overwrite=True) os.system("cp %s %s ." % (arf.filename, rmf.filename)) load_user_model(mymodel, "tplaw") add_user_pars("tplaw", ["nH", "norm", "redshift", "alpha"], [0.02, norm_sim*0.8, redshift, 0.9], parmins=[0.0, 0.0, 0.0, 0.1], parmaxs=[10.0, 1.0e9, 10.0, 10.0], parfrozen=[True, False, True, False]) load_pha("point_source_evt.pi") set_stat("cstat") set_method("simplex") ignore(":0.4, 9.0:") set_model("tplaw") fit() res = get_fit_results() assert np.abs(res.parvals[0]-norm_sim)/norm_sim < 0.05 assert np.abs(res.parvals[1]-alpha_sim)/alpha_sim < 0.05 os.chdir(curdir) shutil.rmtree(tmpdir)
def print_fit(): res = sau.get_fit_results() print("Fit success : \t", res.succeeded) print("Fit results:") print("Numpoints :\t", res.numpoints) print("dof :\t\t", res.dof) print("Final statistic value : \t", res.statval) print("Fitted parameters:") for index, parn in enumerate(res.parnames): print(parn, "\t\t{0:.3e}".format(res.parvals[index]))
def test_lev3fft(self): self.run_thread("lev3fft", scriptname="bar.py") # self.assertEqualWithinTol(ui.get_source().lhs.fwhm.val, 0.0034705, 1e-4) # self.assertEqualWithinTol(ui.get_source().lhs.xpos.val, 150.016, 1e-4) # self.assertEqualWithinTol(ui.get_source().lhs.ypos.val, 2.66587, 1e-4) # self.assertEqualWithinTol(ui.get_source().lhs.ampl.val, 0.0117411, 1e-4) # self.assertEqualWithinTol(ui.get_source().rhs.c0.val, 0.0190897, 1e-4) self.assertEqualWithinTol(self.locals["src"].fwhm.val, 0.0034705, 1e-4) self.assertEqualWithinTol(self.locals["src"].xpos.val, 150.016, 1e-4) self.assertEqualWithinTol(self.locals["src"].ypos.val, 2.66587, 1e-4) self.assertEqualWithinTol(self.locals["src"].ampl.val, 0.0117411, 1e-4) self.assertEqualWithinTol(self.locals["bkg"].c0.val, 0.0190897, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().istatval, 19496.3, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().statval, 607.09, 1e-4) self.assertEqual(ui.get_fit_results().numpoints, 3307) self.assertEqual(ui.get_fit_results().dof, 3302)
def fit_adjuster_set(coeffs, adj_idxs, method='simplex'): """ Find best fit parameters for an arbitrary subset of adjustors specified by the array ``adj_idxs``. The input ``coeffs`` are the best-fit adjustor coefficients for the last iteration. """ import sherpa.astro.ui as ui dummy_data = np.zeros(100) dummy_times = np.arange(100) ui.load_arrays(1, dummy_times, dummy_data) ui.set_method(method) ui.get_method().config.update(SHERPA_CONFIGS.get(method, {})) calc_model = CalcModel() ui.load_user_model(calc_model, 'axo_mod') # sets global axo_mod parnames = [] for adj_idx in adj_idxs: parnames.append('adj_{}'.format(adj_idx)) ui.add_user_pars('axo_mod', parnames) ui.set_model(1, 'axo_mod') coeffs = coeffs.copy() # Don't modify input coeffs coeffs[coeffs < 0] = 0 # Don't allow negative coeffs # Set frozen, min, and max attributes for each axo_mod parameter for adj_idx, par in zip(adj_idxs, axo_mod.pars): par.min = -1000 par.max = 1000 par.val = coeffs[adj_idx] print 'Setting {} to {}'.format(adj_idx, par.val) # Compute base adjusted displacements assuming all the fitted actuators # have zero drive level. coeffs[adj_idxs] = 0 base_adj_displ = M_2d.dot(coeffs) m_2d = M_2d[:, adj_idxs].copy() print m_2d.shape calc_stat = CalcStat(base_adj_displ, m_2d, DISPL_X) ui.load_user_stat('axo_stat', calc_stat, lambda x: np.ones_like(x)) ui.set_stat(axo_stat) calc_model.set_calc_stat(calc_stat) ui.fit(1) # Update coeffs with the values determined in fitting for adj_idx, par in zip(adj_idxs, axo_mod.pars): coeffs[adj_idx] = abs(par.val) return coeffs, ui.get_fit_results()
def test_pha_intro(self): self.run_thread("pha_intro") # astro.ui imported as ui, instead of # being in global namespace self.assertEqualWithinTol(ui.get_fit_results().statval, 37.9079, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 0.902569, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 0.651155, 1e-4) self.assertEqualWithinTol(self.locals["p1"].gamma.val, 2.15852, 1e-4) self.assertEqualWithinTol(self.locals["p1"].ampl.val, 0.00022484, 1e-4) self.assertEqualWithinTol(ui.calc_photon_flux(), 0.000469964, 1e-4) self.assertEqualWithinTol(ui.calc_energy_flux(), 9.614847e-13, 1e-4) self.assertEqualWithinTol(ui.calc_data_sum(), 706.85714092, 1e-4) self.assertEqualWithinTol(ui.calc_model_sum(), 638.45693377, 1e-4) self.assertEqualWithinTol(ui.calc_source_sum(), 0.046996409, 1e-4) self.assertEqualWithinTol(ui.eqwidth(self.locals["p1"], ui.get_source()), -0.57731725, 1e-4) self.assertEqualWithinTol( ui.calc_kcorr([1, 1.2, 1.4, 1.6, 1.8, 2], 0.5, 2), [0.93341286, 0.93752836, 0.94325233, 0.94990140, 0.95678054, 0.96393515], 1e-4, ) self.assertEqual(ui.get_fit_results().nfev, 22) self.assertEqual(ui.get_fit_results().numpoints, 44) self.assertEqual(ui.get_fit_results().dof, 42)
def check_chi2(): """Execute this function after fitting to see if the best-fit chi2 reported matches the formula coded here""" import sherpa.astro.ui as sau chi2 = sau.get_fit_results().statval print('chi2 from fit: {0}'.format(chi2)) data = sau.get_dep() model = sau.get_model_plot().y error = np.where(model > data, sau.get_syserror(), sau.get_staterror()) chi = ((data - model) / error) # Chi per bin chi2 = chi ** 2 # Chi^2 per bin print('chi2 re-computed: {0}'.format(chi2.sum()))
def test_radpro(self): self.run_thread('radpro') self.assertEqualWithinTol(ui.get_fit_results().statval, 217.450, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 6.21287, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 0.0, 1e-4) self.assertEqualWithinTol(self.locals['src'].r0.val, 125.829, 1e-4) self.assertEqualWithinTol(self.locals['src'].beta.val, 4.1633, 1e-4) self.assertEqualWithinTol(self.locals['src'].xpos.val, 0.0, 1e-4) self.assertEqualWithinTol(self.locals['src'].ampl.val, 4.42821, 1e-4) self.assertEqual(ui.get_fit_results().nfev,92) self.assertEqual(ui.get_fit_results().numpoints,38) self.assertEqual(ui.get_fit_results().dof,35)
def plot_chips(self, filename=''): ui.plot_fit_delchi(1) fig = plt.gcf() ch.add_label(0.5, 0.97, self.name + '_'+filename, ["coordsys", ch.FRAME_NORM]) res = ui.get_fit_results() ch.add_label(0.2,0.92, '\\chi^2_{{red}} = {0:3.1f}'.format(res.rstat), ["coordsys", ch.FRAME_NORM]) for i in range(len(res.parnames)): ch.add_label(0.2,0.88-i*0.02, '{0} = {1:8.5g}'.format(res.parnames[i], res.parvals[i]), ["coordsys", ch.FRAME_NORM]) ch.print_window(os.path.join(self.plotpath, self.name + filename + self.printfiletype), {"clobber": True, 'dpi': 300})
def test_basic(self): self.run_thread("basic") self.assertEqualWithinTol(ui.get_fit_results().statval, 151.827, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 16.8697, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 3.68798e-28, 1e-4) self.assertEqualWithinTol(self.locals["m1"].c0.val, 1.49843, 1e-4) self.assertEqualWithinTol(self.locals["m1"].c1.val, 0.1447, 1e-4) self.assertEqualWithinTol(self.locals["m1"].c2.val, 0.0322936, 1e-4) self.assertEqualWithinTol(self.locals["m1"].c3.val, -0.00277729, 1e-4) self.assertEqualWithinTol(self.locals["m2"].c0.val, 1.75548, 1e-4) self.assertEqualWithinTol(self.locals["m2"].c1.val, 0.198455, 1e-4) self.assertEqual(ui.get_fit_results().nfev, 9) self.assertEqual(ui.get_fit_results().numpoints, 11) self.assertEqual(ui.get_fit_results().dof, 9)
def test_eqwith_err(make_data_path, restore_xspec_settings): def check(a0, a1, a2): assert a0 == approx(0.16443033244310976, rel=1e-3) assert a1 == approx(0.09205564216156815, rel=1e-3) assert a2 == approx(0.23933118287470895, rel=1e-3) ui.set_method('neldermead') ui.set_stat('cstat') ui.set_xsabund('angr') ui.set_xsxsect('bcmc') ui.load_data(make_data_path('12845.pi')) ui.notice(0.5, 7) ui.set_model("xsphabs.gal*xszphabs.zabs*(powlaw1d.p1+xszgauss.g1)") ui.set_par(gal.nh, 0.08) ui.freeze(gal) ui.set_par(zabs.redshift, 0.518) ui.set_par(g1.redshift, 0.518) ui.set_par(g1.Sigma, 0.01) ui.freeze(g1.Sigma) ui.set_par(g1.LineE, min=6.0, max=7.0) ui.fit() numpy.random.seed(12345) result = ui.eqwidth(p1, p1 + g1, error=True, niter=100) check(result[0], result[1], result[2]) params = result[3] numpy.random.seed(12345) result = ui.eqwidth(p1, p1 + g1, error=True, params=params, niter=100) check(result[0], result[1], result[2]) parvals = ui.get_fit_results().parvals assert parvals[0] == approx(0.6111340686157877, rel=1.0e-3) assert parvals[1] == approx(1.6409785803466297, rel=1.0e-3) assert parvals[2] == approx(8.960926761312153e-05, rel=1.0e-3) assert parvals[3] == approx(6.620017726014523, rel=1.0e-3) assert parvals[4] == approx(1.9279114810359657e-06, rel=1.0e-3)
def test_basic(self): # In data1.dat for this test, there is a comment with one # word at the beginning -- deliberately would break when reading # with DM ASCII kernel, but passes because we have Sherpa code # to bypass that. self.run_thread('basic') self.assertEqualWithinTol(ui.get_fit_results().statval, 151.827, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 16.8697, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 3.68798e-28, 1e-4) self.assertEqualWithinTol(self.locals['m1'].c0.val, 1.49843, 1e-4) self.assertEqualWithinTol(self.locals['m1'].c1.val, 0.1447, 1e-4) self.assertEqualWithinTol(self.locals['m1'].c2.val, 0.0322936, 1e-4) self.assertEqualWithinTol(self.locals['m1'].c3.val, -0.00277729, 1e-4) self.assertEqualWithinTol(self.locals['m2'].c0.val, 1.75548, 1e-4) self.assertEqualWithinTol(self.locals['m2'].c1.val, 0.198455, 1e-4) self.assertEqual(ui.get_fit_results().nfev,9) self.assertEqual(ui.get_fit_results().numpoints,11) self.assertEqual(ui.get_fit_results().dof,9)
def test_radpro_dm(self): # This test is completely redundant to test_radpro above. # The only difference is that here I test if using DM syntax # in the file name returns the correct arrays from CRATES. # Since any other I/O backend would not understand DM syntax, # I make this test a no-op unless CRATES is used as the I/O # backend. SMD 05/23/13 if (True == self.is_crates_io): self.run_thread('radpro_dm') self.assertEqualWithinTol(ui.get_fit_results().statval, 217.450, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 6.21287, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 0.0, 1e-4) self.assertEqualWithinTol(self.locals['src'].r0.val, 125.829, 1e-4) self.assertEqualWithinTol(self.locals['src'].beta.val, 4.1633, 1e-4) self.assertEqualWithinTol(self.locals['src'].xpos.val, 0.0, 1e-4) self.assertEqualWithinTol(self.locals['src'].ampl.val, 4.42821, 1e-4) self.assertEqual(ui.get_fit_results().nfev,92) self.assertEqual(ui.get_fit_results().numpoints,38) self.assertEqual(ui.get_fit_results().dof,35)
b1, b2 = pars return BoxBOD.model(x, b1, b2) ui.load_user_model(boxbod_func, "boxbod") ui.add_user_pars("boxbod", ["b1", "b2"]) bb = boxbod ui.set_model(bb) bb.b1, bb.b2 = BoxBOD.p0[0], BoxBOD.p0[1] # Perform fit ui.set_stat('chi2datavar') #ui.set_method('levmar') # ['levmar', 'moncar', 'neldermead', 'simplex'] ui.set_method_opt('xtol', 1e-10) ui.fit() # Compute best-fit parameters ui.set_covar_opt('eps', 1e-5) # @todo: Why does this parameter have no effect ui.covariance() # Compute covariance matrix (i.e. errors) #ui.conf() # Compute profile errors #ui.show_all() # Print a very nice summary of your session to less # Report results fr = ui.get_fit_results() cr = ui.get_covar_results() # Report results (we have to apply the s factor ourselves) popt = np.array(fr.parvals) chi2 = fr.statval s_factor = np.sqrt(chi2 / fr.dof) perr = s_factor * np.array(cr.parmaxes) report_results('sherpa', popt, perr, chi2)