def validate_xspec_state_setting(key, newval, altval): """Check we can change an XSPEC setting via the state mechanism Parameters ---------- key : string The name of the setting (e.g. 'abund' or 'xsect'). newval, altval The value to use (newval) and an alternative (altval) if the current setting is already at newval (this is perhaps a bit excessive but it avoids issues if other tests have changed things). """ from sherpa.astro import xspec ostate = xspec.get_xsstate() def getfunc(): return xspec.get_xsstate()[key] def setfunc(val): nstate = ostate.copy() nstate[key] = val xspec.set_xsstate(nstate) validate_xspec_setting(getfunc, setfunc, newval, altval) assert xspec.get_xsstate() == ostate
def test_set_xsstate_missing_key(): """Check set_xsstate does nothing if required key is missing. """ from sherpa.astro import xspec ostate = xspec.get_xsstate() for val in ostate.values(): assert val is not None # paths is not a required key # req_keys = ["abund", "chatter", "cosmo", "xsect", "modelstrings"] fake = {'abund': ostate['abund'] + '_copy', 'xsect': ostate['xsect'] + '_copy', 'chatter': -10, 'cosmo': (0.0, 0.0), # two elements will cause a failure 'modelstrings': {'foo': 2, 'bar': None}, 'paths': {'manager': '/dev/null'}} for key in req_keys: copy = fake.copy() del copy[key] xspec.set_xsstate(copy) nstate = xspec.get_xsstate() assert nstate == ostate
def test_xset_change(): """Can we change the xset setting. """ from sherpa.astro import xspec def getfunc(): return xspec.get_xsxset(DEFAULT_XSET_NAME) def setfunc(val): xspec.set_xsxset(DEFAULT_XSET_NAME.lower(), val) val1 = 'dummy value' val2 = 'a different setting' validate_xspec_setting(getfunc, setfunc, val1, val2) # A separate part of the XSET interface is that the settings # are recorded in the XSPEC state maintained by the xspec # module, so check that the stored value is included in this. # modelvals = xspec.get_xsstate()['modelstrings'] assert DEFAULT_XSET_NAME in modelvals # Is it worth changing the code so we know which to check for? assert modelvals[DEFAULT_XSET_NAME] in [val1, val2]
def restore_xspec_settings(): """Fixture to ensure that XSPEC settings are restored after the test. The aim is to allow the test to change XSPEC settings but to ensure they are restored to their default values after the test. The logic for what should and should not be reset is left to xspec.get_state/set_state. There are known issues with trying to reset "XSET" values (e.g. you can only set them to "", not "delete" them, and it is up to the model implementation to note the value has changed). """ try: from sherpa.astro import xspec except ImportError: # can I return from here safely? return # grab initial values # state = xspec.get_xsstate() # return control to test yield # clean up after test xspec.set_xsstate(state)
def clean_astro_ui(): """Ensure sherpa.astro.ui.clean is called before AND after the test. This also resets the XSPEC settings (if XSPEC support is provided). See Also -------- clean_ui Notes ----- It does NOT change the logging level; perhaps it should, but the screen output is useful for debugging at this time. """ from sherpa.astro import ui if has_xspec: old_xspec = xspec.get_xsstate() else: old_xspec = None ui.clean() yield ui.clean() if old_xspec is not None: xspec.set_xsstate(old_xspec)
def test_set_xsstate_xset(): """Check set_xsstate works for an xset command. """ from sherpa.astro import xspec ostate = xspec.get_xsstate() key = 'a-test-keyword' val = '/foo/bar/baz.pha' while key in ostate['modelstrings']: key += "a" ukey = key.upper() # There should be no value for this key (since it isn't # in modelstrings by construction). # assert key not in xspec.modelstrings assert xspec.get_xsxset(key) == '' nstate = copy.deepcopy(ostate) nstate['modelstrings'][key] = val xspec.set_xsstate(nstate) assert xspec.get_xsxset(key) == val assert ukey in xspec.modelstrings assert xspec.modelstrings[ukey] == val xspec.set_xsstate(ostate) # Unfortunately, due to there being no attempt at clearing out the # XSET settings (e.g. removing existing settings before restoring # the state), the following tests fail. # # TODO: the code should probably be updated to fix this # # assert xspec.get_xsxset(key) == '' # assert xspec.get_xsstate() == ostate xspec.set_xsxset(key, '') del xspec.modelstrings[ukey] assert xspec.get_xsstate() == ostate
def test_set_xsstate_missing_key(): """Check set_xsstate does nothing if required key is missing. """ from sherpa.astro import xspec ostate = xspec.get_xsstate() for val in ostate.values(): assert val is not None # paths is not a required key # req_keys = ["abund", "chatter", "cosmo", "xsect", "modelstrings"] fake = { 'abund': ostate['abund'] + '_copy', 'xsect': ostate['xsect'] + '_copy', 'chatter': -10, 'cosmo': (0.0, 0.0), # two elements will cause a failure 'modelstrings': { 'foo': 2, 'bar': None }, 'paths': { 'manager': '/dev/null' } } for key in req_keys: copy = fake.copy() del copy[key] xspec.set_xsstate(copy) nstate = xspec.get_xsstate() assert nstate == ostate
def test_get_xsstate_keys(): """Check get_xsstate returns the expected keys. Checking the values here are hard, unless we save/restore the state in the requires_xspec decorator or essentially replicate the implementation of get_xsstate. """ from sherpa.astro import xspec ostate = xspec.get_xsstate() assert isinstance(ostate, dict) for key in ["abund", "chatter", "cosmo", "xsect", "modelstrings", "paths"]: assert key in ostate
def setUp(self): self.is_crates_io = False try: import sherpa.astro.io if ("sherpa.astro.io.crates_backend" == sherpa.astro.io.backend.__name__): self.is_crates_io = True except: self.is_crates_io = False self.old_state = ui._session.__dict__.copy() self.old_level = logger.getEffectiveLevel() logger.setLevel(logging.CRITICAL) # Store XSPEC settings, if applicable if has_xspec: self.old_xspec = xspec.get_xsstate()
def test_set_xsstate_path_manager(): """Check set_xsstate works for the manager path """ from sherpa.astro import xspec ostate = xspec.get_xsstate() opath = xspec.get_xspath_manager() spath = ostate['paths'].get('manager', None) # This is just an internal validation check if spath is not None: assert spath == opath if opath == 'b/a': npath = 'a/b' else: npath = 'b/a' nstate = copy.deepcopy(ostate) nstate['paths']['manager'] = npath xspec.set_xsstate(nstate) assert xspec.get_xspath_manager() == npath xspec.set_xsstate(ostate) # Similar to the state xset tests, using an empty # dictionary for paths does not clear out/reset the # manager path. In this case it's not obvious what # should be done (as there's no obvious default value # to use, unless we fall back to the FNINIT-created # value, which is not ideal since there's no guarantee # that we will notice any changes to that logic). # # This is an edge case. # # assert xspec.get_xspath_manager() == opath # assert xspec.get_xsstate() == ostate xspec.set_xspath_manager(opath)
def setUp(self): self.is_crates_io = False try: import sherpa.astro.io if "sherpa.astro.io.crates_backend" == sherpa.astro.io.backend.__name__: self.is_crates_io = True except ImportError: self.is_crates_io = False self.old_state = ui._session.__dict__.copy() self.old_level = logger.getEffectiveLevel() logger.setLevel(logging.CRITICAL) # Store XSPEC settings, if applicable if has_xspec: self.old_xspec = xspec.get_xsstate() # As of XSPEC 12.10.1, it is safest to explicitly set # the state to a known value. For now just pick the # "easy" settings. # xspec.set_xsabund('angr') xspec.set_xsxsect('bcmc')
def setUp(self): from sherpa.astro import xspec ui.clean() self._defaults = xspec.get_xsstate()
def getfunc(): return xspec.get_xsstate()[key]