def _get_data_space(self, filter): """ Return the data space for this object. The method is called by the get_x and get_indep methods, which need an EvaluationSpace1D representation of the data space to return the appropriate data to the client. This is a hook method providing the default implementation for subclasses. Subclasses should override this method to provide alternative callback when the default implementation does not apply to the new class. At this point, this means that if you develop a subclass you should be aware of the default implementation and override it if it does not apply to your subclass. Future versions of Sherpa may implement a cleaner and more extensible API. Parameters ---------- filter : bool or string to be parsed as bool Whether the data returned should be filtered or not. Returns ------- data_space : EvaluationSpace1D An instance of the EvaluationSpace1D representing the data space for this object. """ filter = bool_cast(filter) if filter: data_x = self._x else: data_x = self.x return EvaluationSpace1D(data_x)
def test_regrid1d_error_calc_no_args(setup_1d): internal_mdl = setup_1d grid_evaluate = EvaluationSpace1D(np.arange(-10, 100, 5)) rmdl = ModelDomainRegridder1D(grid_evaluate) mdl = rmdl.apply_to(internal_mdl) with pytest.raises(ModelErr) as excinfo: pvals = [p.val for p in internal_mdl.pars] mdl.calc(p=pvals) assert ModelErr.dict['nogrid'] in str(excinfo.value)
def _test_regrid1d_interpolation(rtol, eval_incr=True, req_incr=True, method=None, setup_1d=None): """Test interpolation case. Parameters ---------- rtol : number The relative tolerance, passed through to assert_allclose. eval_incr : bool, optional Does the evaluation grid increase or decrease? req_incr : bool, optional Does the requested grid increase or decrease? method : function reference, optional The interpolator to use, which should match sherpa.utils.linear_interp's API. If None then use the default method (which is sherpa.utils.neville) """ internal_mdl = setup_1d # Shift the evaluation grid compared to the # requested grid (making sure that the evaluation # grid extends outside the requested grid on both # edges to avoid checking for edge effects here). # # grid_evaluate is x'_i, has 22 bins # grid_request is x_i, has 21 bins # grid_evaluate = np.arange(-10, 100, 5) grid_request = np.linspace(-5, 85, 21) if not eval_incr: grid_evaluate = grid_evaluate[::-1] if not req_incr: grid_request = grid_request[::-1] rmdl = ModelDomainRegridder1D(EvaluationSpace1D(grid_evaluate)) if method is not None: rmdl.method = method mdl = rmdl.apply_to(internal_mdl) yexp = internal_mdl(grid_request) ygot = mdl(grid_request) assert_allclose(ygot, yexp, atol=0, rtol=rtol)
def test_regrid1d_error_grid_mismatch_2(setup_1d): """Internal grid is points but given integrated""" internal_mdl = setup_1d grid_evaluate = EvaluationSpace1D(np.arange(-10, 100, 5)) rmdl = ModelDomainRegridder1D(grid_evaluate) mdl = rmdl.apply_to(internal_mdl) grid_run = np.arange(0, 20, 10) with pytest.raises(ModelErr) as excinfo: mdl(grid_run[:-1], grid_run[1:]) assert ModelErr.dict['needspoint'] in str(excinfo.value)
def test_regrid1d_error_grid_mismatch_1(setup_1d): """Internal grid is integrated but given points""" internal_mdl = setup_1d grid_evaluate = np.arange(-10, 100, 5) eval_space = EvaluationSpace1D(grid_evaluate[:-1], grid_evaluate[1:]) rmdl = ModelDomainRegridder1D(eval_space) mdl = rmdl.apply_to(internal_mdl) grid_run = np.arange(0, 20, 10) with pytest.raises(ModelErr) as excinfo: mdl(grid_run) assert 'An integrated grid is required for model evaluation' \ in str(excinfo.value)
def test_regrid1d_no_overlap_rev3(setup_1d): """If the two grids have no overlap, return value is the same as the model evaluated over the data space.""" internal_mdl = setup_1d eval_space = EvaluationSpace1D(np.arange(1000, 2000, 100)[::-1]) rmdl = ModelDomainRegridder1D(eval_space) mdl = rmdl.apply_to(internal_mdl) grid = np.arange(-10, 100, 5)[::-1] ygot = mdl(grid) assert_allclose(ygot, [ 0., ] * grid.size, atol=0, rtol=1e-7)
def test_regrid1d_call_twice(setup_1d): """What happens if have no evaluation (output) grid?""" internal_mdl = setup_1d eval_space = EvaluationSpace1D(np.arange(1000, 2000, 100)) rmdl = ModelDomainRegridder1D(eval_space) mdl = rmdl.apply_to(internal_mdl) # It appears that calling the model with no arguments is # the same as calling 'rmdl(internal_mdl)'. An "easy" way # to test this is to rely on the stringification (and have # other tests handle that rmdl(...) is doing the right # thing). # noargs = mdl() assert str(mdl) == str(noargs)
def test_regrid1d_identity_after_clearing_grid(setup_1d): """Ensure that the grid can be removed.""" internal_mdl = setup_1d eval_space = EvaluationSpace1D(np.arange(200, 300, 20)) rmdl = ModelDomainRegridder1D(eval_space) mdl = rmdl.apply_to(internal_mdl) grid = np.arange(-10, 100, 5) yexp = internal_mdl(grid) rmdl.grid = None ygot = mdl(grid) assert_allclose(ygot, yexp, atol=0, rtol=1e-7)
def test_regrid1d_no_overlap_int(setup_1d): """If the two grids have no overlap, return value is the same as the model evaluated over the data space.""" internal_mdl = setup_1d array = np.arange(1000, 2000, 100) eval_space = EvaluationSpace1D(array[:-1], array[1:]) rmdl = ModelDomainRegridder1D(eval_space) mdl = rmdl.apply_to(internal_mdl) grid = np.arange(-10, 100, 5) with pytest.warns(UserWarning): ygot = mdl(grid[:-1], grid[1:]) assert_allclose(ygot, [ 50., ] * (grid.size - 1), atol=0, rtol=1e-7)
def _test_regrid1d_int(rtol, eval_incr=True, req_incr=True, setup_1d=None): """Test with for integrated grids. Parameters ---------- rtol : number The relative tolerance, passed through to assert_allclose. eval_incr : bool, optional Does the evaluation grid increase or decrease? CURRENTLY UNSUPPORTED IF False. req_incr : bool, optional Does the requested grid increase or decrease? CURRENTLY UNSUPPORTED IF False. Notes ----- This is very similar to _test_regrid1d_interpolation except that it does not support the method parameter. """ # have not coded this, since need xlo[0] > xlo[1] but xhi[0] > xlo[0] # (I think). assert eval_incr assert req_incr internal_mdl = setup_1d grid_evaluate = np.arange(-10, 100, 5) grid_request = np.linspace(-5, 85, 21) eval_space = EvaluationSpace1D(grid_evaluate[:-1], grid_evaluate[1:]) rmdl = ModelDomainRegridder1D(eval_space) mdl = rmdl.apply_to(internal_mdl) yexp = internal_mdl(grid_request[:-1], grid_request[1:]) ygot = mdl(grid_request[:-1], grid_request[1:]) assert_allclose(ygot, yexp, atol=0, rtol=rtol)
def regrid(self, *args, **kwargs): """ The class RegriddableModel1D allows the user to evaluate in the requested space then interpolate onto the data space. An optional argument 'interp' enables the user to change the interpolation method. Examples -------- >>> import numpy as np >>> from sherpa.models.basic import Box1D >>> mybox = Box1D() >>> request_space = np.arange(1, 10, 0.1) >>> regrid_model = mybox.regrid(request_space, interp=linear_interp) """ valid_keys = ('interp', ) for key in kwargs.keys(): if key not in valid_keys: raise TypeError("unknown keyword argument: '%s'" % key) eval_space = EvaluationSpace1D(*args) regridder = ModelDomainRegridder1D(eval_space, **kwargs) regridder._make_and_validate_grid(args) return regridder.apply_to(self)
def _get_data_space(self, filter=False): filter = bool_cast(filter) if filter: return EvaluationSpace1D(self._lo, self._hi) return EvaluationSpace1D(self.xlo, self.xhi)
def regrid(self, *arrays): eval_space = EvaluationSpace1D(*arrays) regridder = ModelDomainRegridder1D(eval_space) return regridder.apply_to(self)