def test_create_xspec_multiplicative_model(make_data_path): """Can we load multiplicative table models? """ from sherpa.astro import xspec path = make_data_path('testpcfabs.mod') tbl = xspec.read_xstable_model('bar', path) assert tbl.name == 'bar' assert isinstance(tbl, xspec.XSTableModel) assert not tbl.addmodel # Apparently we lose the case of the parameter names; # should investigate # assert len(tbl.pars) == 2 assert tbl.pars[0].name == 'nh' assert tbl.pars[1].name == 'fract' assert tbl.nh.val == pytest.approx(1) assert tbl.nh.min == pytest.approx(0) assert tbl.nh.max == pytest.approx(1000) assert tbl.fract.val == pytest.approx(0.5) assert tbl.fract.min == pytest.approx(0) assert tbl.fract.max == pytest.approx(1) for p in tbl.pars: assert not (p.frozen)
def test_evaluate_xspec_additive_model_beyond_grid(make_data_path): """Can we extend an additive table model beyond its grid? XSPEC 12.10.0 (if not manually patched) will crash if an XSPEC table model is evaluated beyond its grid (this is only an issue for programs like Sherpa that use XSPEC as a "library"). """ from sherpa.astro import xspec if xspec.get_xsversion().startswith('12.10.0'): pytest.skip('Test known to crash XSPEC 12.10.0') path = make_data_path('xspec-tablemodel-RCS.mod') tbl = xspec.read_xstable_model('bar', path) egrid = np.arange(0.1, 11, 0.01) y = tbl(egrid) # Several simple regression tests. assert y[0] == pytest.approx(0.27216572) assert y.max() == pytest.approx(0.3047457) assert y.min() == 0.0 # Is the following worth it? minval = 1.2102469e-11 assert y[y > 0].min() == pytest.approx(minval) assert y[967] == pytest.approx(minval) zeros = np.where(y <= 0) assert (zeros[0] == np.arange(968, 1090)).all()
def test_create_xspec_multiplicative_model(make_data_path): """Can we load multiplicative table models? """ from sherpa.astro import xspec path = make_data_path('testpcfabs.mod') tbl = xspec.read_xstable_model('bar', path) assert tbl.name == 'bar' assert isinstance(tbl, xspec.XSTableModel) assert not tbl.addmodel # Apparently we lose the case of the parameter names; # should investigate # assert len(tbl.pars) == 2 assert tbl.pars[0].name == 'nh' assert tbl.pars[1].name == 'fract' assert tbl.nh.val == pytest.approx(1) assert tbl.nh.min == pytest.approx(0) assert tbl.nh.max == pytest.approx(1000) assert tbl.fract.val == pytest.approx(0.5) assert tbl.fract.min == pytest.approx(0) assert tbl.fract.max == pytest.approx(1) for p in tbl.pars: assert not(p.frozen)
def test_evaluate_xspec_multiplicative_model(make_data_path): """Can we evaluate multiplicative table models? This is a limited test - in that it does not attempt to test the full set of grid inputs that we do with additive table models (and other XSPEC models) - as it is assumed that this logic hsa been tested. """ from sherpa.astro import xspec if xspec.get_xsversion().startswith('12.10.0'): pytest.skip('Test known to crash XSPEC 12.10.0') path = make_data_path('testpcfabs.mod') tbl = xspec.read_xstable_model('bar', path) # This extends beyond the range of the model grid egrid = np.arange(0.1, 17, 1.0) # The expected values, evaluated with XSPEC 12.10.1b using # C++ code (i.e. not the Sherpa interface). # # It appears that the -1 is 1 in earlier versions. # yexp = np.asarray([0.511674, 0.730111, 0.898625, 0.95572, 0.977472, 0.987328, 0.992138, 0.990245, 0.992846, 0.994674, 0.995997, 0.996945, 0.997616, 0.998104, 0.998454, -1, 0]) # Note, xspec 12.10.0 should not be seen here as explicitly # excluded above. xver = xspec.get_xsversion() if xver.startswith('12.9.'): yexp[-2] = 1.0 y = tbl(egrid) assert_almost_equal(y, yexp, decimal=6)
def test_xspec_tablemodel_requires_bin_edges(make_data_path, clean_astro_ui): """Check we can not call a table model with a single grid. This used to be supported in Sherpa 4.13 and before. """ from sherpa.astro import xspec path = make_data_path('xspec-tablemodel-RCS.mod') tbl = xspec.read_xstable_model('bar', path) emsg = r'calc\(\) requires pars,lo,hi arguments, sent 2 arguments' with pytest.warns(FutureWarning, match=emsg): tbl([0.1, 0.2, 0.3, 0.4])
def test_xstbl_link_parameter_evaluation(make_data_path): """See also sherpa/models/test_parameter::test_link_parameter_setting This is meant to replicate issue #742, where we want to ensure that parameter limits are respected, otherwise it is likely that the model evaluation will crash (since the table-model code will likely be indexing into unalocated memory). DJB has checked that this code causes a segfault on linux without a fix for #742. """ from sherpa.astro import xspec path = make_data_path('xspec-tablemodel-RCS.mod') tbl = xspec.read_xstable_model('bar', path) # The tau parameter (first one) has a range of 1 to 10 # - safety check that this still holds, so we know # that we are violating this limit when we set lmdl.c0 # to 20 # assert tbl.tau.min == pytest.approx(1) assert tbl.tau.max == pytest.approx(10) lmdl = Const1D() grid = np.arange(1, 6) glo = grid[:-1] ghi = grid[1:] tbl.tau = lmdl.c0 lmdl.c0 = 2 # just a safety check that we can change the parameter via # a link and run the model assert tbl.tau.val == pytest.approx(2) y2 = tbl(glo, ghi) assert (y2 > 0).all() # Test the fix for #742 lmdl.c0 = 20 emsg = 'parameter bar.tau has a maximum of 10' with pytest.raises(ParameterErr, match=emsg): tbl(glo, ghi)
def test_evaluate_xspec_multiplicative_model(make_data_path): """Can we evaluate multiplicative table models? This is a limited test - in that it does not attempt to test the full set of grid inputs that we do with additive table models (and other XSPEC models) - as it is assumed that this logic hsa been tested. """ from sherpa.astro import xspec if xspec.get_xsversion().startswith('12.10.0'): pytest.skip('Test known to crash XSPEC 12.10.0') path = make_data_path('testpcfabs.mod') tbl = xspec.read_xstable_model('bar', path) # This extends beyond the range of the model grid egrid = np.arange(0.1, 17, 1.0) elo = egrid[:-1] ehi = egrid[1:] # The expected values, evaluated with XSPEC 12.10.1b using # C++ code (i.e. not the Sherpa interface). # # It appears that the -1 is 1 in earlier versions. # yexp = np.asarray([ 0.511674, 0.730111, 0.898625, 0.95572, 0.977472, 0.987328, 0.992138, 0.990245, 0.992846, 0.994674, 0.995997, 0.996945, 0.997616, 0.998104, 0.998454, -1 ]) # Note, xspec 12.10.0 should not be seen here as explicitly # excluded above. xver = xspec.get_xsversion() if xver.startswith('12.9.'): yexp[-1] = 1.0 y = tbl(elo, ehi) assert_almost_equal(y, yexp, decimal=6)
def test_read_xstable_model(make_data_path): """Limited test (only one file). Evaluation tests using this model are in sherpa.astro.xspec.tests.test_xspec. """ from sherpa.astro import xspec path = make_data_path('xspec-tablemodel-RCS.mod') tbl = xspec.read_xstable_model('bar', path) assert tbl.name == 'bar' assert isinstance(tbl, xspec.XSTableModel) assert tbl.addmodel assert tbl.integrate assert len(tbl.pars) == 4 assert tbl.pars[0].name == 'tau' assert tbl.pars[1].name == 'beta' assert tbl.pars[2].name == 't' assert tbl.pars[3].name == 'norm' assert_almost_equal(tbl.tau.val, 1) assert_almost_equal(tbl.tau.min, 1) assert_almost_equal(tbl.tau.max, 10) assert_almost_equal(tbl.beta.val, 0.1) assert_almost_equal(tbl.beta.min, 0.1) assert_almost_equal(tbl.beta.max, 0.5) assert_almost_equal(tbl.t.val, 0.1) assert_almost_equal(tbl.t.min, 0.1) assert_almost_equal(tbl.t.max, 1.3) assert_almost_equal(tbl.norm.val, 1) assert_almost_equal(tbl.norm.min, 0) assert_almost_equal(tbl.norm.max, 1e24) for p in tbl.pars: assert not (p.frozen)
def test_read_xstable_model(make_data_path): """Limited test (only one file). Evaluation tests using this model are in sherpa.astro.xspec.tests.test_xspec. """ from sherpa.astro import xspec path = make_data_path('xspec-tablemodel-RCS.mod') tbl = xspec.read_xstable_model('bar', path) assert tbl.name == 'bar' assert isinstance(tbl, xspec.XSTableModel) assert tbl.addmodel assert len(tbl.pars) == 4 assert tbl.pars[0].name == 'tau' assert tbl.pars[1].name == 'beta' assert tbl.pars[2].name == 't' assert tbl.pars[3].name == 'norm' assert_almost_equal(tbl.tau.val, 1) assert_almost_equal(tbl.tau.min, 1) assert_almost_equal(tbl.tau.max, 10) assert_almost_equal(tbl.beta.val, 0.1) assert_almost_equal(tbl.beta.min, 0.1) assert_almost_equal(tbl.beta.max, 0.5) assert_almost_equal(tbl.t.val, 0.1) assert_almost_equal(tbl.t.min, 0.1) assert_almost_equal(tbl.t.max, 1.3) assert_almost_equal(tbl.norm.val, 1) assert_almost_equal(tbl.norm.min, 0) assert_almost_equal(tbl.norm.max, 1e24) for p in tbl.pars: assert not(p.frozen)