sunkit-spex icon indicating copy to clipboard operation
sunkit-spex copied to clipboard

add support for using pyxspec models in XSPEC

Open elastufka opened this issue 2 years ago • 0 comments

Describe the feature

Solar models computed by this package can be easily modified to be compatible with pyXspec.

From the documentation:

You can also write model functions in Python and insert them into the XSPEC library with the AllModels addPyMod method. You simply define a function with 3 arguments for energies, parameters, and flux. XSPEC will pass tuples containing the energy and parameter values to your function. For the flux array, it will pass a list pre-sized to nE-1, where nE is the size of the energies array. Your model function should fill in this list with the proper flux values.

Further details for the requirements can be found here but for the particular case of the thick/thin-target models they are summarized below. (also, the XSPEC ability to use table models from FITS files might be a good way to go from sunxspex thermal_emission() plus custom abundances directly to a usable model)

The main difference between the existing thick/thin-target bremsstrahlung models and those required by XSPEC are:

  • flux is returned integrated over the energy bin instead of at bin edges, ie, output units must be: photons/s/cm^2 instead of photons/s/cm^2/keV
  • the flux list must be modified in-place
  • electron flux parameter a0 must be included either in the model, or in the XSPEC construction of the complete model as, for example: XPSEC> model='const+thick2'
  • default parameter values and ranges must be defined
  • Python errors will be caught by pyXspec as an Exception, with no further detail. Therefore the ValueError raised if, for example, eebrk > eehigh, will cause XSPEC to not complete the fitting or other task using the model, and the user won't be able to easily tell where or why this happened. (XSPEC prints to the terminal that there was an error in the model function, and pyXspec only returns a generic Exception) Logging Python errors could provide a solution; however, it will be best to define a behaviour of the function upon such an error, preferably one that forces the next iteration of the fit to consider parameters that fulfill the necessary conditions. As parameters cannot be modified from the Python side (as far as I know) once the fitting in XSPEC has started, the simplest way might be to return an answer that is clearly wrong. How this affects the fitting likely depends not only on the loss function but also on the fitting algorithm and how it converges to a local/global minimum.

Such an implementation is relatively low-effort. It would provide both a complement to the existing/planned fitting infrastructure by leveraging the extensive XSPEC capabilities as well as a temporary solution to enabling solar spectral fitting while such infrastructure is tested or improved.

Proposed solution

No response

elastufka avatar Apr 04 '22 08:04 elastufka