darkhistory.spec.spectra.Spectra¶
-
class
darkhistory.spec.spectra.
Spectra
(spec_arr, eng=None, in_eng=None, rs=None, spec_type='dNdE', rebin_eng=None)¶ Structure for a collection of
Spectrum
objects.Spectra
should be viewed as a collection of spectra of particles at different redshifts, or as a collection of spectra produced by injected particles over a range of energies.Individual
Spectrum
objects can be accessed by taking slices of theSpectra
object.Parameters: - spec_arr : list of Spectrum or ndarray of length N
List of
Spectrum
objects or arrays to be stored together.- spec_type : {‘N’, ‘dNdE’}, optional
The type of entries. Default is ‘dNdE’.
- eng : ndarray of length M, optional
Array of energy abscissa of each spectrum. Specify only if spec_arr is an ndarray.
- in_eng : ndarray of length N, optional
Array of injection energies corresponding to each spectrum, if relevant.
- rs : ndarray of length N, optional
Array of redshifts corresponding to each spectrum, if relevant.
- rebin_eng : ndarray, optional
New abscissa to rebin all of the spectra into, if specified.
See also
Examples
Indexing into
Spectra
to obtain aSpectrum
object:>>> from darkhistory.spec.spectrum import Spectrum >>> eng = np.array([1, 10, 100, 1000]) >>> spec_arr = [Spectrum(eng, np.ones(4)*i, spec_type='N') for i in np.arange(9)] >>> test_spectra = Spectra(spec_arr) >>> test_spectra[3].N array([3., 3., 3., 3.])
Attributes: - eng : ndarray of length M
Array of energy abscissa of each spectrum.
- in_eng : ndarray of length N
Array of injection energies corresponding to each spectrum.
- rs : ndarray of length N
Array of redshifts corresponding to each spectrum.
- grid_vals : ndarray of shape (N,M)
2D array of the raw data, indexed by either (rs, eng) or (in_eng, eng).
- spec_type : {‘N’, ‘dNdE’}
The type of values stored.
- N_underflow : ndarray
Array of underflow particles for each
Spectrum
.- eng_underflow : ndarray
Array of underflow particles for each
Spectrum
.
Methods
append
(spec)Appends a new Spectrum. at_rs
(new_rs[, interp_type, bounds_err, …])Interpolates the transfer function at a new redshift. integrate_each_spec
([weight])Sums over each individual spectrum with some weight. plot
(ax[, ind, step, indtype, fac])Plots the contained Spectrum objects. rebin
(out_eng)Re-bins all Spectrum objects according to a new abscissa. redshift
(rs_arr)Redshifts the stored spectra. sum_specs
([weight])Sums all of spectra with some weight. switch_spec_type
([target])Switches between the type of values to be stored. totN
([bound_type, bound_arr])Returns the total number of particles in part of the spectra. toteng
([bound_type, bound_arr])Returns the total energy of particles in part of the spectra. -
__init__
(spec_arr, eng=None, in_eng=None, rs=None, spec_type='dNdE', rebin_eng=None)¶ Initialize self. See help(type(self)) for accurate signature.
-
__add__
(other)¶ Adds two
Spectra
instances together, or an array to theSpectra
. TheSpectra
is on the left.The returned
Spectra
will have its underflow reset to zero if other is not aSpectrum
object.Parameters: - other : Spectra or ndarray
The object to add to the current
Spectra
object.
Returns: - Spectra
New
Spectra
instance which has the summed spectra.
See also
Notes
This special function, together with
Spectra.__radd__()
, allows the use of the symbol+
to addSpectra
objects together.
-
__init__
(spec_arr, eng=None, in_eng=None, rs=None, spec_type='dNdE', rebin_eng=None) Initialize self. See help(type(self)) for accurate signature.
-
__mul__
(other)¶ Takes a product with the spectra with a
Spectra
object, array or number.The
Spectra
object is on the left.Parameters: - other : Spectra, int, float or ndarray
The object to multiply to the current
Spectra
object.
Returns: - Spectra
New
Spectra
instance with the multiplied spectra.
See also
Notes
This special function, together with
Spectra.__rmul__()
, allows the use of the symbol*
to multiply objects with a Spectra object.The returned
Spectra
object as underflow set to zero.
-
__radd__
(other)¶ Adds two
Spectra
instances together, or an array to the spectra. TheSpectra
object is on the right.The returned
Spectra
will have its underflow reset to zero if other is not aSpectrum
object.Parameters: - other : Spectra or ndarray
The object to add to the current
Spectra
object.
Returns: - Spectra
New
Spectra
instance which has the summed spectra.
See also
Notes
This special function, together with Spectra.__add__, allows the use of the symbol
+
to add twoSpectra
together.
-
__rmul__
(other)¶ Takes a product with the spectra with a
Spectra
object, array or number.The
Spectra
object is on the right.Parameters: - other : Spectra, int, float or ndarray
The object to multiply to the current
Spectra
object.
Returns: - Spectra
New
Spectra
instance with the multiplied spectra.
See also
Notes
This special function, together with
Spectra.__mul__()
, allows the use of the symbol*
to multiply objects with a Spectra object.The returned
Spectra
object as underflow set to zero.
-
__rsub__
(other)¶ Subtracts this
Spectra
from another or an array.Parameters: - other : Spectra or ndarray
The object from which to subtract the current
Spectra
object.
Returns: - Spectra
New
Spectra
instance which has the subtracted spectrum.
See also
Notes
This special function, together with
Spectra.__rsub__()
, allows the use of the symbol-
to subtract or subtract from Spectra objects.
-
__rtruediv__
(other)¶ Divides an object by the spectra.
Parameters: - other : ndarray, float, int, list or Spectra
Returns: - Spectra
New
Spectra
instance which has the divided spectra.
See also
Notes
This special function, together with
Spectra.__truediv__()
, allows the use fo the symbol/
to divideSpectra
objects.
-
__sub__
(other)¶ Subtracts a
Spectra
or array from thisSpectra
.Parameters: - other : Spectra or ndarray
The object to subtract from the current
Spectra
object.
Returns: - Spectra
New
Spectra
instance which has the subtracted spectrum.
See also
Notes
This special function, together with
Spectra.__rsub__()
, allows the use of the symbol-
to subtract or subtract fromSpectra
objects.
-
__truediv__
(other)¶ Divides the spectra by another object.
Parameters: - other : ndarray, float, int, list or Spectra
Returns: - Spectra
New
Spectra
instance which has the divided spectra.
See also
Notes
This special function, together with
Spectra.__rtruediv__()
, allows the use fo the symbol/
to divideSpectra
objects.
-
__weakref__
¶ list of weak references to the object (if defined)
-
append
(spec)¶ Appends a new Spectrum.
Parameters: - spec : Spectrum
The new spectrum to append.
-
at_rs
(new_rs, interp_type='val', bounds_err=None, fill_value=nan)¶ Interpolates the transfer function at a new redshift.
Interpolation is logarithmic.
Parameters: - new_rs : ndarray
The redshifts or redshift bin indices at which to interpolate.
- interp_type : {‘val’, ‘bin’}
The type of interpolation. ‘bin’ uses bin index, while ‘val’ uses the actual redshift.
- bounds_err : bool, optional
Whether to return an error if outside of the bounds for the interpolation.
-
integrate_each_spec
(weight=None)¶ Sums over each individual spectrum with some weight.
The weight is over each energy bin, and has the same length as self.eng.
Parameters: - weight : ndarray, optional
The weight in each energy bin, with weight of 1 for every bin if not specified.
Returns: - ndarray
An array of weighted sums, one for each spectrum in this Spectra.
-
plot
(ax, ind=None, step=1, indtype='ind', fac=1, **kwargs)¶ Plots the contained Spectrum objects.
Parameters: - ax : matplotlib.axes.Axes
The axis handle of the figure to show the plot in.
- ind : int, float, tuple or ndarray, optional.
Index or redshift of Spectrum to plot, or a tuple of indices or redshifts providing a range of Spectrum to plot, or a list of indices or redshifts of Spectrum to plot.
- step : int, optional
The number of steps to take before choosing one Spectrum to plot.
- indtype : {‘ind’, ‘rs’}, optional
Specifies whether ind is an index or an abscissa value.
- abs_plot : bool, optional
Plots the absolute value if true.
- fac : ndarray, optional
Factor to multiply the array by.
- **kwargs : optional
All additional keyword arguments to pass to matplotlib.plt.plot.
Returns: - matplotlib.figure
-
rebin
(out_eng)¶ Re-bins all Spectrum objects according to a new abscissa.
Rebinning conserves total number and total energy.
Parameters: - out_eng : ndarray
The new abscissa to bin into. If self.eng has values that are smaller than out_eng[0], then the new underflow will be filled. If self.eng has values that exceed out_eng[-1], then an error is returned.
- rebin_type : {‘1D’, ‘2D’}, optional
Whether to rebin each Spectrum separately (‘1D’), or the whole Spectra object at once (‘2D’). Default is ‘2D’.
See also
spec.spectools.rebin_N_2D_arr
-
redshift
(rs_arr)¶ Redshifts the stored spectra.
Parameters: - rs_arr : ndarray
Array of redshifts (1+z) to redshift each spectrum to.
Returns: - None
Examples
>>> from darkhistory.spec.spectrum import Spectrum >>> eng = np.array([1, 10, 100, 1000]) >>> spec_arr = [Spectrum(eng, np.ones(4)*i, rs=100, spec_type='N') for i in np.arange(4)] >>> test_spectra = Spectra(spec_arr) >>> test_spectra.redshift(np.array([0.01, 0.1, 1, 10])) >>> print(test_spectra.grid_vals) [[0. 0. 0. 0.] [1. 0. 0. 0.] [2. 2. 0. 0.] [3. 3. 3. 0.]] >>> print(test_spectra.N_underflow) [0. 3. 4. 3.] >>> print(test_spectra.eng_underflow) [0. 0.111 0.22 0.3 ]
-
sum_specs
(weight=None)¶ Sums all of spectra with some weight.
The weight is over each spectrum, and has the same length as self.in_eng and self.rs.
Parameters: - weight : ndarray or Spectrum, optional
The weight in each redshift bin, with weight of 1 for every bin if not specified.
Returns: - Spectrum
A Spectrum of the weighted sum of the spectra.
-
switch_spec_type
(target=None)¶ Switches between the type of values to be stored.
Parameters: - target : {‘N’, ‘dNdE’}
The target type to switch to.
-
totN
(bound_type=None, bound_arr=None)¶ Returns the total number of particles in part of the spectra.
The part of the spectra can be specified in two ways, and is specified by bound_type. Multiple totals can be obtained through bound_arr.
Parameters: - bound_type : {‘bin’, ‘eng’, None}
The type of bounds to use. Bound values do not have to be within the [0:eng.size] for ‘bin’ or within the abscissa for ‘eng’. None should only be used when computing the total particle number in the spectrum.
Specifying
bound_type='bin'
without bound_arr returns the number of particles in each bin.- bound_arr : ndarray of length N, optional
An array of boundaries (bin or energy), between which the total number of particles will be computed. If bound_arr is None, but bound_type is specified, the total number of particles in each bin is computed. If both bound_type and bound_arr are None, then the total number of particles in the spectrum is computed.
For ‘bin’, bounds are specified as the bin boundary, with 0 being the left most boundary, 1 the right-hand of the first bin and so on. This is equivalent to integrating over a histogram. For ‘eng’, bounds are specified by energy values.
These boundaries need not be integer values for ‘bin’: specifying
np.array([0.5, 1.5])
for example will include half of the first bin and half of the second.
Returns: - ndarray of shape (self.rs.size, N-1) or length N-1
Total number of particles in the spectra or between the specified boundaries.
See also
Examples
>>> from darkhistory.spec.spectrum import Spectrum >>> eng = np.array([1, 10, 100, 1000]) >>> spec_arr = [Spectrum(eng, np.arange(4) + 4*i, rs=100, spec_type='N') for i in np.arange(4)] >>> test_spectra = Spectra(spec_arr) >>> test_spectra.totN() array([ 6., 22., 38., 54.]) >>> test_spectra.totN('bin', np.array([1, 3])) array([[ 3., 11., 19., 27.]]) >>> test_spectra.totN('eng', np.array([10, 1e4])) array([[ 5.5, 15.5, 25.5, 35.5]])
-
toteng
(bound_type=None, bound_arr=None)¶ Returns the total energy of particles in part of the spectra.
The part of the Spectrum objects to find the total energy of particles can be specified in two ways, and is specified by bound_type. Multiple totals can be obtained through bound_arr.
Parameters: - bound_type : {‘bin’, ‘eng’, None}
The type of bounds to use. Bound values do not have to be within the [0:eng.size] for ‘bin’ or within the abscissa for ‘eng’. None should only be used when computing the total particle number in the spectrum.
Specifying
bound_type=='bin'
without bound_arr gives the total energy in each bin.- bound_arr : ndarray of length N, optional
An array of boundaries (bin or energy), between which the total number of particles will be computed. If unspecified, the total number of particles in the whole spectrum is computed.
For ‘bin’, bounds are specified as the bin boundary, with 0 being the left most boundary, 1 the right-hand of the first bin and so on. This is equivalent to integrating over a histogram. For ‘eng’, bounds are specified by energy values.
These boundaries need not be integer values for ‘bin’: specifying np.array([0.5, 1.5]) for example will include half of the first bin and half of the second.
Returns: - ndarray of shape (self.rs.size, N-1) or length N-1
Total number of particles in the spectra or between the specified boundaries.
See also
Examples
>>> from darkhistory.spec.spectrum import Spectrum >>> eng = np.array([1, 10, 100, 1000]) >>> spec_arr = [Spectrum(eng, np.arange(4) + 4*i, rs=100, spec_type='N') for i in np.arange(4)] >>> test_spectra = Spectra(spec_arr) >>> test_spectra.toteng() array([ 3210., 7654., 12098., 16542.]) >>> test_spectra.toteng('bin', np.array([1, 3])) array([[ 210., 650., 1090., 1530.]]) >>> test_spectra.toteng('eng', np.array([10, 1e4])) array([[ 3205., 7625., 12045., 16465.]])