User class#
The SPEX user models are used to enable external models to be used inside SPEX. The
script acts as an interface between the SPEX user model (user, cusr, and musr)
and the external model.
For users who want to use the SPEX user models, we have a Python module available
to make the interaction with SPEX easier. The User class can be imported into
the Python script (prepared by the user) that the SPEX user model will execute.
Example script#
The user model takes a parameter called exec, which holds the path to the
user script that calculates the (external) model. This can be a Python script,
or a program in other compatible languages (must be able to take command-line
arguments and read text files).
The script below is also available in the file python/examples/usermodel.py
in the SPEX source directory:
import sys
from pyspex import User
# from scipy import signal # For cusr model example
def main():
"""This is an example script for the SPEX user models. Users can copy
this script and alter it to compute their desired model. Please
select the required user model from the User class (either
'user' (additive model), 'musr' (multiplicative model), or
'cusr' (convolutional model).
Notes for the different models:
Musr: The user.spectrum array needs to be filled with the transmission
as a function of energy. SPEX will multiply this transmission with the
spectrum internally.
Cusr: The spectrum is already available in the user.spectrum array.
Users can replace this array with the convolved spectrum, provided
that the energy grid remains the same.
"""
# Read input filename from command line (first entry)
input_file = sys.argv[1]
# Read output filename from command file (second entry)
output_file = sys.argv[2]
# Load the User class for interaction with the SPEX user models
user = User('user')
#user = User('musr') # use for multiplicative user model
#user = User('cusr') # use for convolution user model
# Read input from the SPEX ASCII file
user.read_from_spex(input_file)
# Calculate an emission spectrum (user), 'user.spectrum',
# for each bin (the spectrum weights are kept at 0 in this example)
user.spectrum = user.model_param[0] * user.e_centroid**(-user.model_param[1])
# Calculate a transmission for a multiplicative model (musr).
# In this case the user.spectrum variable contains the transmission:
# user.spectrum = 1. - user.model_param[0] * numpy.exp(-user.model_param[1] * user.e_centroid)
# Calculate a convolution for a convolution model (cusr).
# gauss_kernel = signal.windows.gaussian(50, user.model_param[0])
# user.spectrum = signal.fftconvolve(user.spectrum,gauss_kernel,'same')
# Write the result to the output file in the proper format
user.write_to_spex(output_file)
if __name__ == "__main__":
main()
User class#
The example script makes use of the User class. This is a class to read and
write the SPEX input and output files.
- class pyspex.user.user.User(mode: str)#
This class handles the interaction between the SPEX user models (user, musr, cusr) and python models that users may have.
- Variables:
n_model_param (int) – Number of parameters of the model (read from the user model ‘npar’)
model_param (numpy.ndarray) – Array containing the parameters of the model (read from the user model)
n_energy_bins (int) – Number of bins in the energy grid.
e_upper_boundary (numpy.ndarray) – Energy bin upper boundaries (in keV). Called ‘egb’ in SPEX.
e_centroid (numpy.ndarray) – Energy bin centroids (in keV). Called ‘eg’ in SPEX.
e_width (numpy.ndarray) – Energy bin widths (in keV). Called ‘deg’ in SPEX.
spectrum (numpy.ndarray) – Spectrum in (ph/s/bin). In the case of the ‘musr’ model, this contains the transmission of the model. This variable is called ‘sener’ in SPEX.
spectrum_weigths – Weigths of the spectrum (usually 0., only use when you know what to do). Called ‘wener’ in SPEX.
mode (str) – Which model is being used? (user/musr/cusr)
- read_from_spex(inputfile)#
Read the file generated by the SPEX user model.
- Parameters:
inputfile (str) – Input file name
- write_to_spex(outputfile)#
Write the calculated spectrum to file to input back into the SPEX user model.
- Parameters:
outputfile (str) – Output file name