Last modified: 18 December 2023

How can I suppress the screen output from Sherpa commands? [Updated]


In order to hide the verbose screen output from Sherpa commands, e.g. load_pha, the Python logging infrastructure may be used. The logging code uses a number of levels (in order) - "DEBUG", "INFO", "WARNING", and "ERROR" - to determine the status, or severity, of a message. Setting the logging code to use the "WARNING" (or logging.WARNING) level will then stop any messages at the "INFO" level or below from being displayed.

Using SherpaVerbosity

The sherpa.utils.logging module provides the SherpaVerbosity function which lets you easily change the logging output (e.g. to hide the output). This is designed to be used as a Python context manager, and so is used to temporily change the logging output. The logging module can be used if the changes should be made to the whole program.

The load_pha call is a good example, as it can display messages at the "INFO" and "WARNING" levels:

sherpa> from sherpa.utils.logging import SherpaVerbosity
sherpa> load_pha("3c273.pi")
WARNING: systematic errors were not found in file '3c273.pi'
statistical errors were found in file '3c273.pi'
but not used; to use them, re-read with use_errors=True
read ARF file 3c273.arf
read RMF file 3c273.rmf
WARNING: systematic errors were not found in file '3c273_bg.pi'
statistical errors were found in file '3c273_bg.pi'
but not used; to use them, re-read with use_errors=True
read background file 3c273_bg.pi

Changing to the "ERROR" level will hide all messages:

sherpa> with SherpaVerbosity("ERROR"):
   ...:     load_pha("3c273.pi")

whereas using "WARNING" will show some messages:

sherpa> with SherpaVerbosity("WARNING"):
   ...:     load_pha("3c273.pi")
WARNING: systematic errors were not found in file '3c273.pi'
WARNING: systematic errors were not found in file '3c273_bg.pi'

This can be particularly useful for hiding the lengthy output of commands such as fit(), conf(), or proj().

Using the logging infrastructure

The above can also be achieved by direct use of the Python logging module:

import logging
logger = logging.getLogger("sherpa")

Then, turn off the screen output of Sherpa commands using one of

logger.setLevel(logging.WARNING)
logger.setLevel(logging.ERROR)

and turn it back on again with

logger.setLevel(logging.INFO)

The load_pha example is shown below:

sherpa> import logging
sherpa> logger = logging.getLogger("sherpa")
sherpa> logger.setLevel(logging.INFO)
sherpa> load_pha("3c273.pi")
WARNING: systematic errors were not found in file '3c273.pi'
statistical errors were found in file '3c273.pi' but not used; to use them, re-read with use_errors=True
read ARF file 3c273.arf
read RMF file 3c273.rmf
WARNING: systematic errors were not found in file '3c273_bg.pi'
statistical errors were found in file '3c273_bg.pi' but not used; to use them, re-read with use_errors=True
read background file 3c273_bg.pi
sherpa> logger.setLevel(logging.WARN)
sherpa> load_pha("3c273.pi")
WARNING: systematic errors were not found in file '3c273.pi'
WARNING: systematic errors were not found in file '3c273_bg.pi'
sherpa> logger.setLevel(logging.ERROR)
sherpa> load_pha("3c273.pi")
sherpa> logger.setLevel(logging.INFO)

See the Sherpa FAQ "How can I log the command output from my Sherpa fitting session?" to learn how to use the logging module to record the command output from a Sherpa session to a file.