def test_load_xstable_model_fails_with_dir(): """Check that the function fails with invalid input: directory The temporary directory is used for this (the test is skipped if it does not exist). """ ui.clean() assert ui.list_model_components() == [] with pytest.raises(IOError): ui.load_xstable_model('tmpdir', tmpdir) assert ui.list_model_components() == []
def test_load_table_model_fails_with_dev_null(): """Check that load_table_model fails with invalid input: /dev/null This simulates an empty file (and relies on the system containing a /dev/null file that reads in 0 bytes). """ ui.clean() assert ui.list_model_components() == [] # The error depends on the load function with pytest.raises(ValueError): ui.load_table_model('devnull', '/dev/null') assert ui.list_model_components() == []
def test_load_xstable_model_fails_with_dir(tmp_path): """Check that the function fails with invalid input: directory The temporary directory is used for this. """ tmpdir = tmp_path / 'load_xstable_model' tmpdir.mkdir() ui.clean() assert ui.list_model_components() == [] with pytest.raises(IOError): ui.load_xstable_model('tmpdir', str(tmpdir)) assert ui.list_model_components() == []
def _get_position_from_model(name): """Returns the xpos, ypos, ellip, and theta values for the given model, where name is either a model a string nameing a model Raises an ValueError if unable to retrieve a number. """ # Could try isinstance(val, sherpa.models.model.Model) # here instead, but stick with hasattr for now # if hasattr(name, "xpos") and hasattr(name, "ypos") and \ hasattr(name, "ellip") and hasattr(name, "theta"): return (name.xpos.val, name.ypos.val, name.ellip.val, name.theta.val) if isinstance(name, str): if name not in ui.list_model_components(): raise ValueError( "Name of model argument does not appear to be a model: '{0}'". format(name)) xpos = _get_parameter_value("xpos", "{0}.xpos".format(name)) ypos = _get_parameter_value("ypos", "{0}.ypos".format(name)) ellip = _get_parameter_value("xpos", "{0}.ellip".format(name)) theta = _get_parameter_value("ypos", "{0}.theta".format(name)) return (xpos, ypos, ellip, theta) raise ValueError( "Unrecognized argument to _get_position_from_model: '{0}'".format( str(name)))
def test_delete_bkg_model(clean_astro_ui): """Check we can delete a background model""" channels = np.arange(1, 5) counts = np.zeros(channels.size) d = ui.DataPHA('src', channels, counts) b = ui.DataPHA('bkg', channels, counts) d.set_background(b, id=2) ui.set_data(d) ui.set_bkg_source(ui.const1d.bmdl + ui.gauss1d.gmdl, bkg_id=2) assert ui.get_bkg_source(bkg_id=2) is not None ui.delete_bkg_model(bkg_id=2) # Expression has been removed # with pytest.raises(ModelErr) as exc: ui.get_bkg_source(bkg_id=2) assert str( exc.value) == 'background model 2 for data set 1 has not been set' # components still exist mdls = ui.list_model_components() assert set(mdls) == set(['bmdl', 'gmdl'])
def test_delete_bkg_model_with_bkgid(id, clean_astro_ui): """Check we call delete_bkg_model with non-default bkg_id""" ui.load_arrays(id, [1, 2, 3], [5, 4, 3], ui.DataPHA) bkg = ui.DataPHA('bkg', np.asarray([1, 2, 3]), [1, 1, 0]) ui.set_bkg(id, bkg, bkg_id=2) ui.set_bkg_source(id, ui.const1d.bmdl, bkg_id=2) assert ui.list_model_components() == ['bmdl'] assert ui.get_bkg_source(id, 2).name == 'const1d.bmdl' ui.delete_bkg_model(id, bkg_id=2) assert ui.list_model_components() == ['bmdl'] with pytest.raises(ModelErr) as exc: ui.get_bkg_source(id, 2) emsg = 'background model 2 for data set {} has not been set'.format(id) assert str(exc.value) == emsg
def test_load_xstable_model_fails_with_text_column(make_data_path): """Check that load_table_model fails with invalid input: text column The first column is text (and an ASCII file) so it is expected to fail. """ # Check that this file hasn't been changed (as I am re-using it for # this test) infile = make_data_path('table.txt') assert os.path.isfile(infile) ui.clean() assert ui.list_model_components() == [] # The error depends on the load function. with pytest.raises(Exception): ui.load_xstable_model('stringcol', infile) assert ui.list_model_components() == []
def save_components(filename, compIDstosave=None): if os.path.isfile(filename): raise Exception("Model already exists.") with open(filename, 'w') as f: print('Saving ' + filename) print(shp.get_model()) if compIDstosave is None: compIDstosave = [c for c in shp.list_model_components() if c not in ['emap', 'psf']] f.write('components: ' + str(len(compIDstosave)) + '\n') for compID in compIDstosave: if compID not in ['psf', 'emap']: comp = shp.get_model_component(compID) f.write(comp.name.replace('.', ' ') + ' ' + str(len(comp.pars)) + '\n') for par in comp.pars: f.write(par.name + ' ' + str(par.min) + ' ' + str(par.val) + ' ' + str(par.max) + '\n') f.close()