box¶
The LT box contains an assortment of plotting, histogramming and fitting routines used for data analysis. The plotting routines have been imported from the plotting module and the fitting routines have been imported from the The LT_Fit Package. For detailed information about these functions check the plotting and the the The LT_Fit Package documentation.
Normally all you should need to do is import the box and you have a box of tools.
Example:
>>> import LT.box as B
Imported functions:
- From LT.plotting:
plot_exp()
: plot experimental data points with or without error bar
plot_line()
: plot a line through a set of data points
plot_spline()
: plot a spline curve through a set of data pointsdplot_exp: (
datafile_plot_exp()
) is like plot_exp but accesses the datafile variables directly via their namesdplot_line: (
datafile_plot_theory()
) like plot_line for datafile variablesdplot_spline: (
datafile_spline_plot_theory()
) like plot_spline for datafiles variables- From LT_Fit:
linefit
: fit a straight line through a set of data points
polyfit
: fit a polynomial
gen_linfit
: general linear fit
genfit
: general, non-linear fit
Box Content:
-
LT.box.
get_file
(file, **kwargs)¶ Assume that B is the name of LT.box.
Open and read the file:
>>> md = B.get_file( 'file.data' )
-
LT.box.
get_data
(D, var)¶ Assume that B is the name of LT.box.
Get all the values of variable ‘my_var’ as a
numpy.array()
:>>> mv = B.get_data(md, 'my_var')
-
LT.box.
get_spectrum
(file, calibration=None)¶ Read an MCA spectrum and convert it to a histogram.
Example:
>>> h = B.get_spectrum('co60.spe') # read the spectrum >>> h.plot() # plot the spectrum
-
class
LT.box.
histo
(values=None, range=None, bins=None, histogram=None, bin_error=None, bin_center=None, bin_content=None, file=None, window=None, title='my histogram', xlabel='x-bin', ylabel='content', **kwargs)¶ Define a histogram based on the np.histogram class.
The various ways of defining one are:
If a is a 1D (
numpy.array()
) containing the data to be histogrammed>>> h = histo( a )
If his is the output of the
numpy.histogram()
function>>> h = histo(histogram = his)
If
bc
is a 1D array with bin center values, andbcont
contains bin content values then:>>> h = histo(bin_center = bc, bin_content = bcont)
A filename for a stored histogram is given
>>> h = histo(filename), where filename contains the pdatafile
Usually the result of a histo.save operation
Important keywords:
Keyword
Meaning
values
Array of values to be histogrammed (
numpy.array()
)range
Lower and upper limits of binning ( e.g.
range = (10.,20.)
)bins
Number of bins
histogram
Result of
numpy.histogram()
functionbin_error
Array of errors for each bin content (
numpy.array()
)bin_center
Array of bin-center values (
numpy.array()
)bin_content
Array of bin-content values (
numpy.array()
)file
Load data from file
window
Set a window (a zoom window)
title
Set the title
xlabel
Set the x-label
ylabel
Set the y-label
Additional keyword arguments are passed to the
numpy.histogram()
function-
apply_calibration
(self, cal)¶ apply x-axis calibration, new axis values are cal(xaxis)
-
clear
(self)¶ Set the content and errors to 0.
-
clear_window
(self)¶ Reset (Clear) the defined window
-
copy
(self)¶ Create a copy of the histogram:
>>>hc = h.copy()
Only the histogram values are copied, no lables and titles etc.
-
fill
(self, y, add=False, **kwargs)¶ Fill the histogram with the values stored in the
numpy.array()
y.Keyword
Meaning
add
if True add the results to the existing content
Additional keyword arguments are passed to the
numpy.histogram()
function
-
find_bin
(self, x)¶ Find the bin that would contain the value x
-
fit
(self, xmin=None, xmax=None, init=True, ignore_zeros=True)¶ Fit a gaussian on a quadratic background. You can also just fit a background or just a gaussian. All this is controlled by which parameters you want to fit. Another important part of non-linear fitting is that you need to provide reasonable guesses for the fit parameters. The parameters in
histo
are not just numbers but objects with their own properties and functions (seeParameter
). The full fit function is as follows:The (
LT.box.histo
) parameters are:Parameter
Histo Class Member
b0
b1
b2
A
mean
sigma
Which parameters are fitted is defined in
set_fit_list()
Keyword arguments are:
Keyword
Meaning
xmin
lower fit limit
xmax
upper fit limit
init
True/False (default = True) estimate initial fit parameters automatically
ignore_zeros
True/False (default = True) ignore channels with bin content zero
-
fit_func
(self, x)¶ The function fitted to the histogram data
-
fit_view
(self, init=True)¶ Fit histogram using the current display limits as fit range. This is only useful if the histogram has been plotted.
-
init_gauss
(self, xmin=None, xmax=None)¶ Calculate the initial parameter guess for a gaussian. These parameters can them be used in the call to
fit
-
load
(self, file='histo.data')¶ read the histogram data from
pdatafile
If the file does not result from a save function make sure that all the necessary data are present.
-
plot
(self, filled='True', ymin=0.0, axes=None, **kwargs)¶ Plot the histogram content:
Keyword
Meaning
filled
if True draw a filled histogram
ymin
lower limit where fill starts (horizontal line)
-
plot_exp
(self, **kwargs)¶ Plot histogram content and errors like experimental data.
-
plot_fit
(self, color='r', axes=None, **kwargs)¶ Plot the fitted curve
Keyword
Meaning
color
color of the fitted line
-
rebin
(self, n, scale=False)¶ rebin the histogram by a factor n:
>>>hc = h.rebin(2)
Keyword
Meaning
scale
- True: the original bin number is not a multiple of n
and the last bin content will be scaled
-
set_fit_list
(self, fit=['A', 'mean', 'sigma'])¶ Define which parameters are to be fitted.
The default list is
fit = [ 'A', 'mean', 'sigma']
to use all parameters:
h.set_fit_list( fit = [ 'A', 'mean', 'sigma', 'b0', 'b1, 'b2'])
-
set_window
(self, xmin=None, xmax=None)¶ Define a window into the histogram. This is similar to a zoom or a region of interest (ROI)
Keyword
Meaning
xmin
lower limit for plotting or fitting
xmax
upper limit
-
set_window_view
(self)¶ Like set_windows but uses the current display limits. This is only useful if the histogram has been plotted.
-
sum
(self, xmin=None, xmax=None)¶ Return the sum of all bins. If the limits are given, calculate the sum of all bins between the bins that contain the values xmin and xmax.
Example:
>>> s0 = h.sum() # add all bins >>> s1 = h.sum(0.5, 1.1) # add the bins between 0.5 and 1.1 >>> s2 = h.sum(xmin = 0.5, xmax = 1.1) # add the bins between 0.5 and 1.1
Keyword
Meaning
xmin
lower limit of sum of bin content
xmax
upper limit of sum
The errors are also calculated.
Histogram Operations¶
The following operations are defined for Histograms
(histo
):
In [1]: hr = h1 + h2 # addition
In [2]: hr = h1 - h2 # subtraction
In [3]: hr = 2.*h1 # mutiplication by a number
In [4]: hr = 5.5*h1 - 2.*h2 # combination of the above
- Note: When adding histograms they need to be of the same type (the
same number of bins, same bin-center values). Labels and titles are not copied.