Utility routines
There are a number of utility routines provided by Sherpa that may be useful. Unfortunately it is not always obvious whether a routine is for use with the Object-Oriented API or the Session API.
The Sherpa resource file
The sherpa.get_config routine returns the location of the default
resource file used to configure Sherpa. The location of this file is
taken from (in order):
the
SHERPARCenvironment file,the file
.sherpa.rcin the user’s home directory,and the file returned by
get_config.
Taking advantage of multiple CPUs
The sherpa.utils.parallel module provides code to run code on
multiple CPUs. There are two settings in the Sherpa resource file
that can be changed:
parallel.numcoresmultiprocessing.multiprocessing_start_method
If parallel.numcores is set to 1 then the code will run in serial
and not parallel.
The multiprocessing.multiprocessing_start_method field controls
how the parallel code is run. Using methods other than “fork” are
unlikely to provide any benefit for Sherpa, and it is probably better to set
parallel.numcores to 1 if the “fork” method can not be used.
The Python documentation on multiprocessing
explains the methods that Python offers and discusses some pitfalls.
Sherpa relies on mutable state, in particular for handling parameter values, and so is unlikely to benefit from using threads.
Controlling the verbosity of Sherpa
Sherpa uses Python logging for most messages. This allows the user to redirected the output to a file or suppress it by setting the logging level. The following example will globally change the level for all sherpa modules, such that debug and informational messages are no longer displayed:
>>> import logging
>>> sherpalog = logging.getLogger('sherpa')
>>> sherpalog.setLevel('WARNING')
Sherpa also provides a context manager -
SherpaVerbosity - to change the
logging level only for a specific portion of the code. This can be
used, e.g., to hide the long default output printed after fitting a
model:
>>> from sherpa.utils.logging import SherpaVerbosity
>>> import numpy as np
>>> from sherpa.astro import ui
>>> ui.load_arrays("mydata", np.arange(5), np.ones(5))
>>> ui.set_model("mydata", "polynom1d.poly")
>>> with SherpaVerbosity('WARNING'):
... ui.fit("mydata")
Random Numbers
Sherpa uses random numbers
in an optimisation routine (
montecarloused byMonCar);when evaluating a MCMC chain such as
MetropolisMH;estimating the likelihood ratio of two models (
sherpa.sim.simulate);when sampling distributions using routines from
sherpa.sim.sample;
The primary way to control the random numbers used by Sherpa is to
create a NumPy Random Generator and
pass it to routines either with the rng parameter or, for users of
the Session class, the set_rng method.
However, for the optimiser code, which uses a C++ implementation
of the Mersenne Twister random number generator,
the important value is the seed argument (although this can be
derived from the random number generator if the seed value is
set to None).
Note
Prior to Sherpa 4.16.0 the random numbers were controlled by a
combination of the legacy NumPy random-number API, that is,
calling numpy.random.seed() , and - in a few places, the
Python random module,
as well as the seed argument for the optimiser code.
Reference/API
- The sherpa module
- The sherpa.utils.err module
- The sherpa.utils.logging module
- The sherpa.utils.parallel module
- The sherpa.utils.random module
- The sherpa.utils.guess module
- The sherpa.utils module
- Knuth_close
- _guess_ampl_scale
- apache_muller
- bisection
- bool_cast
- calc_ftest
- calc_mlr
- calc_total_error
- create_expr
- dataspace1d
- dataspace2d
- demuller
- erf
- export_method
- extract_kernel
- filter_bins
- gamma
- get_error_estimates
- get_fwhm
- get_keyword_defaults
- get_keyword_names
- get_midpoint
- get_num_args
- get_peak
- get_position
- get_valley
- guess_amplitude
- guess_amplitude2d
- guess_amplitude_at_ref
- guess_bounds
- guess_fwhm
- guess_position
- guess_radius
- guess_reference
- histogram1d
- histogram2d
- igam
- igamc
- incbet
- interpolate
- is_binary_file
- lgam
- linear_interp
- multinormal_pdf
- multit_pdf
- nearest_interp
- neville
- neville2d
- new_muller
- normalize
- pad_bounding_box
- parallel_map
- param_apply_limits
- parse_expr
- poisson_noise
- print_fields
- quantile
- rebin
- sao_arange
- sao_fcmp
- set_origin
- sum_intervals
- zeroin
- NoNewAttributesAfterInit
- The sherpa.utils.testing module
- The sherpa.io module
- The sherpa.astro module
- The sherpa.astro.io module
- The sherpa.astro.io.types module
- The sherpa.astro.io.wcs module
- The sherpa.astro.io.xstable module
- The sherpa.astro.utils module
- The sherpa.astro.utils.xspec module