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 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 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_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 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 tearDown(self): ui._session.__dict__.update(self.old_state) logger.setLevel(self.old_level) if has_xspec: xspec.set_xsstate(self.old_xspec)
def setfunc(val): nstate = ostate.copy() nstate[key] = val xspec.set_xsstate(nstate)
def tearDown(self): from sherpa.astro import xspec xspec.set_xsstate(self._defaults)