| |
- Mca
- McaBackground
- McaCalibration
- McaElapsed
- McaEnvironment
- McaFit
- McaPeak
- McaPresets
- McaROI
class Mca |
|
Device-independent MultiChannel Analyzer (MCA) class |
|
Methods defined here:
- __copy__(self)
- Makes a "shallow" copy of an Mca instance, using copy.copy() on all of
the attributes of the Mca instance. The .rois and .environment attributes
will still point to the same values, because they are lists.
- __deepcopy__(self, visit)
- Makes a "deep" copy of an Mca instance, using copy.deepcopy() on all of
the attributes of the Mca instance. All of the attribute will point to
new objects.
- __init__(self, file=None, **filekw)
- Creates new Mca object. The data are initially all zeros, and the number
of channels is 2048.
Keywords:
file:
Name of a file to read into the new Mca object with read_file()
Example:
m = Mca('my_spectrum.dat')
- add_roi(self, roi, energy=0)
- This procedure adds a new region-of-interest to the MCA.
Inputs:
roi: An object of type mcaROI.
Kyetwords:
energy:
Set this flag to 1 to indicate that the .left and .right
fields of roi are in units of energy rather than channel
number.
Example:
mca = Mca('mca.001')
roi = McaROI()
roi.left = 500
roi.right = 600
roi.label = 'Fe Ka'
mca,add_roi(roi)
- channel_to_d(self, channels)
- Converts channels to "d-spacing" using the current calibration values for
the Mca. This routine can convert a single channel number or an array of
channel numbers. Users are strongly encouraged to use this function
rather than implement the conversion calculation themselves, since it
will be updated if additional calibration parameters are added. This
routine is useful for energy dispersive diffraction experiments. It uses
both the energy calibration parameters and the "two-theta" calibration
parameter.
Inputs:
channels:
The channel numbers to be converted to "d-spacing".
This can be a single number or a list of channel numbers.
Outputs:
This function returns the equivalent "d-spacing" for the input channels.
The output units are in Angstroms.
Restrictions:
This function assumes that the units of the energy calibration are keV
and that the units of "two-theta" are degrees.
Example:
mca = Mca('mca.001')
channels = [100,200,300]
d = mca.channel_to_d(channels) # Get the "d-spacing" of these
- channel_to_energy(self, channels)
- Converts channels to energy using the current calibration values for the
Mca. This routine can convert a single channel number or an array of
channel numbers. Users are strongly encouraged to use this function
rather than implement the conversion calculation themselves, since it
will be updated if additional calibration parameters (cubic, etc.) are
added.
Inputs:
channels:
The channel numbers to be converted to energy. This can be
a single number or a sequence of channel numbers.
Outputs:
This function returns the equivalent energy for the input channels.
Example:
mca = Mca('mca.001')
channels = [100, 200, 300]
energy = mca.channel_to_energy(channels) # Get the energy of these
- d_to_channel(self, d, clip=0)
- Converts "d-spacing" to channels using the current calibration values
for the Mca. This routine can convert a single "d-spacing" or an array
of "d-spacings". Users are strongly encouraged to use this function
rather than implement the conversion calculation themselves, since it
will be updated if additional calibration parameters are added.
This routine is useful for energy dispersive diffraction experiments.
It uses both the energy calibration parameters and the "two-theta"
calibration parameter.
Inputs:
d:
The "d-spacing" values to be converted to channels.
This can be a single number or an array of values.
Keywords:
clip:
Set this flag to 1 to clip the returned values to be between
0 and nchans-1. The default is not to clip.
Outputs:
This function returns the closest equivalent channel for the input
"d-spacing". Note that it does not generate an error if the channel
number is outside the range 0 to (nchans-1), which will happen if the
"d-spacing" is outside the range for the calibration values of the Mca.
Example:
mca = Mca('mca.001')
channel = mca.d_to_chan(1.598)
- delete_roi(self, index)
- This procedure deletes the specified region-of-interest from the MCA.
Inputs:
index: The index of the ROI to be deleted, range 0 to len(mca.rois)
Example:
mca = Mca('mca.001')
mca.delete_roi(2)
- energy_to_channel(self, energy, clip=0)
- Converts energy to channels using the current calibration values for the
Mca. This routine can convert a single energy or an array of energy
values. Users are strongly encouraged to use this function rather than
implement the conversion calculation themselves, since it will be updated
if additional calibration parameters are added.
Inputs:
energy:
The energy values to be converted to channels. This can be a
single number or a sequence energy values.
Keywords:
clip:
Set this flag to 1 to clip the returned values to be between
0 and nchans-1. The default is not to clip.
Outputs:
This function returns the closest equivalent channel for the input
energy. Note that it does not generate an error if the channel number
is outside the range 0 to (nchans-1), which will happen if the energy
is outside the range for the calibration values of the Mca.
Example:
mca = Mca('mca.001')
channel = mca.energy_to_channel(5.985)
- find_roi(self, left, right, energy=0)
- This procedure finds the index number of the ROI with a specified
left and right channel number.
Inputs:
left:
Left channel number (or energy) of this ROI
right:
Right channel number (or energy) of this ROI
Keywords:
energy:
Set this flag to 1 to indicate that Left and Right are in units
of energy rather than channel number.
Output:
Returns the index of the specified ROI, -1 if the ROI was not found.
Example:
mca = Mca('mca.001')
index = mca.find_roi(100, 200)
- fit_background(self, bottom_width=4.0, top_width=0.0, exponent=2, tangent=0, compress=4)
- This function fits a background to an MCA spectrum. The background is
fitted using an enhanced version of the algorithm published by
Kajfosz, J. and Kwiatek, W .M. (1987) "Non-polynomial approximation of
background in x-ray spectra." Nucl. Instrum. Methods B22, 78-81.
Keywords:
top_width:
Specifies the width of the polynomials which are concave upward.
The top_width is the full width in energy units at which the
magnitude of the polynomial is 100 counts. The default is 0, which
means that concave upward polynomials are not used.
bottom_width:
Specifies the width of the polynomials which are concave downward.
The bottom_width is the full width in energy units at which the
magnitude of the polynomial is 100 counts. The default is 4.
exponent:
Specifies the power of polynomial which is used. The power must be
an integer. The default is 2, i.e. parabolas. Higher exponents,
for example EXPONENT=4, results in polynomials with flatter tops
and steeper sides, which can better fit spectra with steeply
sloping backgrounds.
tangent:
Specifies that the polynomials are to be tangent to the slope of the
spectrum. The default is vertical polynomials. This option works
best on steeply sloping spectra. It has trouble in spectra with
big peaks because the polynomials are very tilted up inside the
peaks.
compress:
Compression factor to apply before fitting the background.
Default=4, which means, for example, that a 2048 channel spectrum
will be rebinned to 512 channels before fitting.
The compression is done on a temporary copy of the input spectrum,
so the input spectrum itself is unchanged.
The algorithm works best if the spectrum is compressed before it
is fitted. There are two reasons for this. First, the background
is constrained to never be larger than the data itself. If the
spectrum has negative noise spikes they will cause the fit to be
too low. Compression will smooth out such noise spikes.
Second, the algorithm requires about 3*N^2 operations, so the time
required grows rapidly with the size of the input spectrum. On a
200 MHz Pentium it takes about 3 seconds to fit a 2048 channel
spectrum with COMPRESS=1 (no compression), but only 0.2 seconds
with COMPRESS=4 (the default).
Procedure:
1) At each channel "i" an n'th degree polynomial which is concave up
is fitted. Its equation is
n
(e(i) - e(j))
f(j,i) = y(i) + --------------
n
top_width
where f(j,i) is the fitted counts in channel j for the polynomial
centered in channel i. y(i) is the input counts in channel "i", e(i) is
the energy of channel i, e(j) is the energy of channel j, and
"top_width" and "n" are user-specified parameters. The background count
in channel "j", b(j) is defined as
b(j) = min ((f(j,i), y(j))
i
b(j) is thus the smallest fitted polynomial in channel j, or the raw
data, whichever is smaller.
2) After the concave up polynomials have been fitted, a series of
concave down polynomials are constructed. At each channel "i" an n'th
degree polynomial which is concave up is fitted. The polynomial is slid
up from below until it "just touches" some channel of the spectrum. Call
this channel "i". The maximum height of the polynomial is thus
n
(e(i) - e(j))
height(j) = max ( b(j) + -------------- )
i n
bottom_width
where bottom_width is a user_specified parameter.
3) Once the value of height(i) is known the polynomial is fitted. The
background counts in each channel are then determined from:
n
(e(i) - e(j))
bgd(j) = max ( height(i) + --------------
i n
bottom_width
bgd(j) is thus the maximum counts for any of the concave down
polynomials passing though channel j.
Before the concave-down polynomials are fitted the spectrum at each
channel it is possible to subtract out a straight line which is
tangent to the spectrum at that channel. Use the /TANGENT qualifier to
do this. This is equivalent to fitting a "tilted" polynomial whose
apex is tangent to the spectrum at that channel. By fitting
polynomials which are tangent rather than vertical the background fit
is much improved on spectra with steep slopes.
Outputs:
This function returns an MCA object which is identical to the calling
object, except that the data have been replaced by the background fit.
Example:
mca = Mca()
mca.read_file('mca.001')
bgd = mca.fit_background(mca, bottom=6, exponent=4)
- fit_peaks(self, peaks, fit=None, background=None, output='', spreadsheet=None, append=1, **background_kw)
- Fits the peaks in the MCA spectrum. It provides a convenient interface to
the fitPeaks() function.
Inputs:
peaks: A list of McaPeak objects. See fitPeaks() and read_peaks() for
more information.
Keywords:
fit:
An object of type McaFit which can be used to control the
peak fitting. If this keyword is omitted then the fit structure
is created with McaFit()
background:
An Mca object containing the fitted background. If this keyword
is omitted then this function will call Mca.fit_background() before
calling fitPeaks().
output:
The name of an output file to receive the ASCII printout of the
fit results. This keyword is simply passed to fit_peaks_report().
spreadsheet:
The name of an output file to receive the ASCII output of the
fit results in spreadsheet format. This keyword is simply passed to
fit_peaks_report().
append:
Flag indicating whether the output and spreadsheet files should be
appended to or overwritten. This keyword is simply passed to
fit_peaks_report9).
In addition to these keywords, all keywords accepted by the
fit_background() function are accepted if the background keyword is
not present, i.e. if this function will be calling fit_background().
Outputs:
This function returns an Mca object which is identical to the calling
object, except that the data have been replaced by the peak fit.
Procedure:
The function does the following:
- Creates the Fit structure with mca->FIT_INITIALIZE() if Fit
was not passed as a keyword parameter.
- Fits the background using fit_background() if background
was not passed as a keyword parameter.
- Extracts the data from the input spectrum and the background
spectrum.
- Calls fitPeaks() with the background subtracted data.
- Calls fit_peaks_report()
- Creates a new Mca object using Mca.deepcopy and stores the output
of fitPeaks() in this new object with set_data(). It then
returns this new MCA object as the function return value.
Example:
mca = Mca()
mca.read_file('mca.001')
peaks = read_peaks('mypeaks.pks')
fit = mca.fit_peaks(peaks, bottom=6, exponent=4)
- fit_peaks_report(self, fit, peaks, background, output='', spreadsheet=None, append=1, time=None)
- Prints out the results from <A HREF="mca_utility_routines.html#FIT_PEAKS">FIT_PEAKS</A>
Inputs:
fit:
An McaFit object with the global fitting parameters.
peaks:
A list of McaPeak objects with the fit results for each peak.
See fit_peaks for more information on fit and peaks.
background:
An Mca object containing the fitted background spectrum.
Keywords:
output:
The name of an output file to receive the ASCII printout of the
fit results. If this keyword is omitted then the output will be
written to stdout, i.e. the IDL output window. If the Output file
already exists then the new information will (by default) be appended
to the file.
spreadsheet:
The name of an output file to receive the ASCII output of the
fit results in a format easily imported into a spreadsheet. If this
keyword is omitted then no spreadsheet output will be generated.
written to stdout, i.e. the IDL output window.
If the spreadhseet file already exists then the new information will
(by default) be appended to the file.
append:
Set this keyword to 0 to overwrite the output and spreadsheet files
rather than to append to them, which is the default behavior.
Example:
mca = Mca(file='mca.001')
peaks = read_peaks('mypeaks.pks')
[fit, peaks, predicted] = mca.fit_peaks(peaks, fit,
bottom=6, exponent=4)
mca.fit_peaks_report(fit, peaks, background, output='mca.001_out')
- get_calibration(self)
- Returns the Mca calibration, as an McaCalibration object
- get_data(self)
- Returns the data (counts) from the Mca
- get_elapsed(self)
- Returns the Mca elapsed parameters, as an McaElapsed object
- get_energy(self)
- Returns a list containing the energy of each channel in the MCA spectrum.
Procedure:
Simply returns mca.channel_to_energy() for each channel
Example:
from Mca import *
mca = Mca('mca.001')
energy = mca.get_energy()
- get_environment(self)
- Returns a list of McaEnvironment objects that contain the environment
parameters of the Mca.
- get_name(self)
- Returns the Mca name as a string
- get_presets(self)
- Returns the Mca presets, as an McaCPresets object
- get_roi_counts(self, background_width=1)
- Returns a tuple (total, net) containing the total and net counts of
each region-of-interest in the MCA.
Kyetwords:
background_width:
Set this keyword to set the width of the background region on either
side of the peaks when computing net counts. The default is 1.
Outputs:
total: The total counts in each ROI.
net: The net counts in each ROI.
The dimension of each list is NROIS, where NROIS
is the number of currently defined ROIs for this MCA. It returns
and empty list for both if NROIS is zero.
Example:
mca = Mca('mca.001')
total, net = mca.get_roi_counts(background_width=3)
print 'Net counts = ', net
- get_rois(self, energy=0)
- Returns the Mca ROIS, as a list of McaROI objects
- initial_calibration(self, energy)
- Performs an initial coarse energy calibration of the Mca, setting only
the slope, and setting the offset parameter to 0.
Inputs:
energy: The energy of the biggest peak in the MCA spectrum.
Procedure:
This routine does the following:
1) Sets the offset coefficient to 0.0
2) Sets the quadratic coefficient to 0.0
3) Determines which channel contains the most counts, PEAK_CHAN
4) Sets the slope equal to the input energy divided by PEAK_CHAN
Example:
from Mca import *
mca = Mca('mca.001')
mca.initial_calibration(20.1)
- read_file(self, file, netcdf=0, detector=0)
- Reads a disk file into an MCA object. If the netcdf=1 flag is set it
reads a netcdf file, else it assumes the file is ASCII.
If the data file has multiple detectors then the detector keyword can be
used to specify which detector data to return.
Inputs:
file:
The name of the disk file to read.
Keywords:
netcdf:
Set this flag to read files written in netCDF format, otherwise
the routine assumes that the file is in ASCII format.
See the documentation for Mca.write_ascii_file and
Mca.write_netcdf_file for information on the formats.
detector:
Specifies which detector to read if the file has multiple detectors.
Example:
mca = Mca()
mca.read_file('mca.001')
- set_calibration(self, calibration)
- Sets the Mca calibration.
Inputs:
calibration:
An McaCalibration object
- set_data(self, data)
- Copies an array of data (counts) to the Mca.
Inputs:
data:
A Numeric array of data (counts).
- set_elapsed(self, elapsed)
- Sets the Mca elapsed parameters.
Inputs:
elapsed:
An McaElapsed object
- set_environment(self, environment)
- Copies a list of McaEnvironment objects to the Mca object.
Inputs:
environment:
A list of McaEnvironment objects.
- set_name(self, name)
- Sets the Mca name.
Inputs:
name:
A string
- set_presets(self, presets)
- Sets the Mca presets.
Inputs:
presets:
An McaPresets object
- set_rois(self, rois, energy=0)
- Sets the region-of-interest parameters for the MCA.
The rois information is contained in an object of class McaRoi.
This routine is not needed if the information in the McaRoi instance
is already in channel units. It is needed if the information in the
.left and .right fields is in terms of energy.
Inputs:
rois:
A list of objects of type McaROI
Keywords:
energy:
Set this flag to indicate that the .left and .right fields
of rois are in units of energy rather than channel number.
Example:
mca = Mca('mca.001')
r1 = McaROI()
r1.left = 5.4
r1.right = 5.6
r2 = McaROI()
r2.left = 6.1
r2.right = 6.2
mca.set_rois([r1,r2], energy=1)
- write_file(self, file, netcdf=0)
- Writes Mca or Med objects to a disk file.
It calls Mca.write_netcdf_file if the netcdf keyword flg is set,
Note that users who want to read such files with Python are strongly
encouraged to use Mca.read_file()
Inputs:
file:
The name of the disk file to write.
Keywords:
netcdf:
Set this flag to write the file in netCDF format, otherwise
the file is written in ASCII format. See the documentation
for Mca.write_ascii_file and Mca.write_netcdf_file for
information on the formats.
Example:
mca = Mca()
mca.write_file('mca.001')
Data and non-method functions defined here:
- __doc__ = ' Device-independent MultiChannel Analyzer (MCA) class '
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
- __module__ = 'Mca'
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
|
class McaBackground |
|
Defines the parameters for fitting backgrounds in Mca objects.
Fields and default values:
.exponent = 2
.top_width = 0.
.bottom_width = 4.
.tangent = 0
.compress = 4
See the documentation on fit_background() for information on the meaning
of these fields. |
|
Methods defined here:
- __init__(self)
Data and non-method functions defined here:
- __doc__ = '\n Defines the parameters for fitting backgroun...nformation on the meaning\n of these fields.\n '
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
- __module__ = 'Mca'
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
|
class McaCalibration |
|
Class defining an Mca calibration. The calibration equation is
energy = .offset + .slope*channel + .quad*channel**2
where the first channel is channel 0, and thus the energy of the first
channel is .offset.
Fields:
.offset # Offset
.slope # Slope
.quad # Quadratic
.units # Calibration units, a string
.two_theta # 2-theta of this Mca for energy-dispersive diffraction |
|
Methods defined here:
- __init__(self, offset=0.0, slope=1.0, quad=0.0, units='keV', two_theta=10.0)
- There is a keyword with the same name as each field, so the object can
be initialized when it is created.
Data and non-method functions defined here:
- __doc__ = '\n Class defining an Mca calibration. The cali...of this Mca for energy-dispersive diffraction\n '
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
- __module__ = 'Mca'
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
|
class McaElapsed |
|
The elapsed time and counts for an Mca.
Fields:
.start_time # Start time and date, a string
.live_time # Elapsed live time in seconds
.real_time # Elapsed real time in seconds
.read_time # Time that the Mca was last read in seconds
.total_counts # Total counts between the preset start and stop channels |
|
Methods defined here:
- __init__(self, start_time='', live_time=0.0, real_time=0.0, read_time=0.0, total_counts=0.0)
Data and non-method functions defined here:
- __doc__ = '\n The elapsed time and counts for an Mca.\n \n...ts between the preset start and stop channels\n '
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
- __module__ = 'Mca'
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
|
class McaEnvironment |
|
The "environment" or related parameters for an Mca. These might include
things like motor positions, temperature, anything that describes the
experiment.
An Mca object has an associated list of McaEnvironment objects, since there
are typically many such parameters required to describe an experiment.
Fields:
.name # A string name of this parameter, e.g. "13IDD:m1"
.value # A string value of this parameter, e.g. "14.223"
.description # A string description of this parameter, e.g. "X stage" |
|
Methods defined here:
- __init__(self, name='', value='', description='')
Data and non-method functions defined here:
- __doc__ = '\n The "environment" or related parameters for ...description of this parameter, e.g. "X stage"\n '
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
- __module__ = 'Mca'
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
|
class McaFit |
|
Class used for the global input and output parameters for fit_peaks. |
|
Methods defined here:
- __init__(self, mca=None)
- update(self, mca)
- Updates the McaFit object to be consistent with the Mca object.
The calibration offset and slope are copied to the initial values,
and the number of channels is set.
Inputs:
mca:
An Mca object to copy the calibration from
Data and non-method functions defined here:
- __doc__ = '\n Class used for the global input and output parameters for fit_peaks.\n '
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
- __module__ = 'Mca'
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
|
class McaPeak |
|
Class for definiing the input and output parameters for each peak in
fit_peaks().
Input fields set bfore calling fit_peaks(), defaults and descriptions
.label = "" # Peak label
.self.energy_flag = 0 # Flag for fitting energy
# 0 = Fix energy
# 1 = Optimize energy
.fwhm_flag = 0 # Flag for fitting FWHM
# 0 = Fix FWHM to global curve
# 1 = Optimize FWHM
# 2 = Fix FWHM to input value
.ampl_factor = 0. # Fixed amplitude ratio to previous peak
# 0. = Optimize amplitude of this peak
# >0. = Fix amplitude to this value relative
# to amplitude of previous free peak
# -1.0 = Fix amplitude at 0.0
.initial_energy = 0. # Peak energy
.initial_fwhm = 0. # Peak FWHM
.initial_ampl = 0. # Peak amplitude
Output fields returned by fit_peaks(), defaults and descriptions
.energy = 0. # Peak energy
.fwhm = 0. # Peak FWHM
.ampl = 0. # Peak amplitude
.area = 0. # Area of peak
.bgd = 0. # Background under peak |
|
Methods defined here:
- __init__(self)
Data and non-method functions defined here:
- __doc__ = '\n Class for definiing the input and output par...bgd = 0. # Background under peak\n '
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
- __module__ = 'Mca'
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
|
class McaPresets |
|
The preset time and counts for an Mca.
Fields:
.live_time # Preset live time in seconds
.real_time # Preset real time in seconds
.read_time # Time that the Mca was last read in seconds
.total_counts # Preset total counts between the preset
# start and stop channels
.start_channel # Start channel for preset counts
.end_channel # End channel for preset counts
.dwell # Dwell time per channel for MCS devices
.channel_advance # Channel advance source for MCS hardware:
# 0=internal, 1=external
.prescale # Prescaling setting for MCS hardware |
|
Methods defined here:
- __init__(self)
Data and non-method functions defined here:
- __doc__ = '\n The preset time and counts for an Mca.\n \n ... # Prescaling setting for MCS hardware\n '
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
- __module__ = 'Mca'
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
|
class McaROI |
|
Class that defines a Region-Of-Interest (ROI)
Fields
.left # Left channel or energy
.right # Right channel or energy
.centroid # Centroid channel or energy
.fwhm # Width
.bgd_width # Number of channels to use for background subtraction
.use # Flag: should the ROI should be used for energy calibration
.preset # Is this ROI controlling preset acquisition
.label # Name of the ROI
.d_spacing # Lattice spacing if a diffraction peak
.energy # Energy of the centroid for energy calibration |
|
Methods defined here:
- __cmp__(self, other)
- Comparison operator. The .left field is used to define ROI ordering
- __init__(self, left=0.0, right=0.0, centroid=0.0, fwhm=0.0, bgd_width=0, use=1, preset=0, label='', d_spacing=0.0, energy=0.0)
- Keywords:
There is a keyword with the same name as each attribute that can be
used to initialize the ROI when it is created.
Data and non-method functions defined here:
- __doc__ = '\n Class that defines a Region-Of-Interest (ROI...Energy of the centroid for energy calibration\n '
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
- __module__ = 'Mca'
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
| |