Last modified: 12 December 2024

How can I log the command output from my Sherpa fitting session?


In order to create an automatically updated log file containing the output of a Sherpa fitting session - e.g., all of the fit results from a script which you have executed - you can take advantage of the Python logging infrastructure used by Sherpa. The Python logging module may be imported either in a separate script or within the Sherpa interactive session.

unix% more my_sherpa_script.py

import logging
logger = logging.getLogger("sherpa")
logging.basicConfig(level=logging.INFO, filename="fit.log", filemode="w")

load_image("subimage_bin50.fits")
set_source(gauss2d.core + beta2d.beta + conts2d.bg)
set_method("simplex")
set_stat("cstat")
subimage_xpos = 228
subimage_ypos = 191
core.fwhm = 0.1
core.xpos = subimage_xpos
core.ypos = subimage_ypos
core.ellip= 0
core.theta= 0
freeze(core.fwhm, core.xpos, core.ypos, core.ellip, core.theta)
fit()
...
quit()

unix% ciao
unix% sherpa my_sherpa_script.py

In the example Python script above, the logging module is imported and configured to have the output from the commands in the script written to the log file 'fit.log'. The verbosity level of command output which gets written to the log file is set to 'logging.INFO', which means that all output from the Sherpa commands gets logged (in this example, only output from 'fit()'). Setting the verbosity level to something other than 'logging.INFO', e.g. 'logging.WARN', would log only the command messages considered severe, i.e. WARNING and ERROR messages; setting the level to 'logging.ERROR' would log only the most severe ERROR messages. Note also that since the 'filemode' of the log file is set to 'w' for 'write' (as opposed to 'a' for 'append'), the file 'fit.log' would be overwritten each time the script is run. See the Python logging tutorial, or other Python references, for more information.