eval_xspec_with_fixed_norm

sherpa.astro.xspec.eval_xspec_with_fixed_norm(func: Callable) Callable[source] [edit on github]

Decorator to evaluate an XSAdditiveModel with norm=1.

The norm is then applied in Python, instead of in the XSPEC code. This is a speed optimization. For a single call to an XSPEC model, there is no advantage, because the XSPEC model needs to be called anyway; it is just called with norm=1 instead of the actual norm and the output of the XSPEC model is multiplied by the actual norm in Python. The main advantage comes in fitting: Because the XSPEC model is called with the same norm (1.0) every time, is is far more likely to hit a parameter combination that is already cached (every time the optimizer changes only the norm). This results in a much higher hit rate for the cache, speeding up the fit.

This decorator is written specifically for the XSAdditiveModel class where, by construction, the last parameter is always the norm. It is applied by wrapping the calc method of the class, which then on the inside calls the calc method of the super class, which itself applies the sherpa.models.model.modelCacher1d decorator.

See https://github.com/sherpa/sherpa/issues/767 for an extensive discussion including performance benchmarks. While some XSPEC models seem to have an internal cache for the norm similar to this decorator, in other cases, we see 20% speed-ups in the fitting time. Details depend a lot on the data and models in case. At the very least, no case has been found where this decorator slows down the fitting process significantly, thus it is justified to apply it broadly to all XSAdditiveModels.

For some models, there is an additional benefit: Since it is multiplied on in Python, the norm retains the full precision of a float, while some XSPEC models use lower precision floats internally. The higher precision can reduce the number of function evaluations needed to reach the same accuracy in the fit.