| |
- Mca.Mca
-
- Med
class Med(Mca.Mca) |
|
The MED class is basically a collection of Mca objects.
This class is device-independent.
Its methods generally simply apply the Mca class methods to each Mca object
in the collection. The Med class itself is most commonly used for reading
data from disk files. More importantly, this class is the superclass of the
epicsMed class. |
|
Methods defined here:
- __init__(self, n_detectors=16, file=None)
- Initialization code for creating a new Med object.
Keywords:
n_detectors:
The number of detectors (Mca objects) in the Med.
file:
The name of a disk file to read into the Med after it is created.
The number of detectors in the Med will be changed if the number of
Mca objects in the disk file is different from the Med.
- add_roi(self, roi, energy=0)
- This procedure adds an ROI to each Mca in the Med.
Inputs:
roi:
A single McaROI to be added.
- copy_rois(self, source_mca=0, energy=0)
- This procedure copies the ROIs defined for one Mca in the Med to all of
the other Mcas.
Inputs:
source_mca:
The index number of the Mca from which the ROIs are to
be copied. This number ranges from 0 to self.n_detectors-1.
The default is the first Mca (index=0).
Keywords:
energy:
Set this keyword if the ROIs should be copied by their position
in energy rather than in channels. This is very useful when
copying ROIs when the calibration parameters for each Mca in
the Med are not identical.
- delete_roi(self, index)
- This procedure deletes the ROI at position "index" from each Mca in the
Med.
Inputs:
index: The index number of the ROI to be deleted.
- final_calibration(self, peaks)
- Performs a final energy calibration for each Mca in the Med.
Inputs:
peaks:
A list of McaPeak objects. This list is typically read from a
disk file with function Mca.read_peaks().
See the documentation for Mca.final_calibration() for more information.
- get_calibration(self)
- Returns a list of McaCalibration objects, one for each Mca in the Med.
- get_data(self, total=0, align=0)
- Returns the data from each Mca in the Med as a 2-D Numeric array
Keywords:
total:
Set this keyword to return the sum of the spectra from all
of the Mcas as a 1-D Numeric array.
align:
Set this keyword to return spectra which have been shifted and
and stretched to match the energy calibration parameters of the
first detector. This permits doing arithmetic on a
"channel-by-channel" basis. This keyword can be used alone
or together with the TOTAL keyword, in which case the data
are aligned before summing.
Outputs:
By default this function returns a long 2-D array of counts dimensioned
[nchans, self.n_detectors]
If the "total" keyword is set then the function returns a long 1-D
array dimensioned [nchans].
- get_elapsed(self)
- Returns the elapsed parameters for the Med.
The elapsed information is contained in a list of structures of type
McaElapsed.
Outputs:
Returns a list of structures of type McaElapsed.
Procedure:
This function simply invokes Mca.get_elapsed for each Mca in the Med
and stores the results in the returned list.
- get_energy(self)
- Returns a list of energy arrays, one array for each Mca in the Med.
See the documentation for Mca.get_energy() for more information.
- get_mcas(self)
- Returns a list of Mca objects from the Med.
- get_presets(self)
- Returns the preset parameters for the Med.
The preset information is contained in a list of objects of type
McaPresets.
Outputs:
Returns a list of structures of type McaPresets.
Procedure:
This function simply invokes Mca.get_presets() for each Mca in the Med
and stores the results in the returned list.
- get_roi_counts(self, background_width=1)
- Returns the net and total counts for each Roi in each Mca in the Med.
Outputs:
Returns a tuple (total, net). total and net are lists of lists
containing the total and net counts in each ROI. The length of the
outer list is self.n_detectors, the length of the total and net lists
list for each Mca is the number of ROIs defined for that Mca.
- get_rois(self)
- Returns the region-of-interest information for each Mca in the Med.
Outputs:
Returns a list of list of lists of McaRoi objects.
The length of the outer list is self.n_detectors, the length of the
list for each Mca is the number of ROIs defined for that Mca.
- initial_calibration(self, energy)
- Performs an initial energy calibration for each Mca in the Med.
Inputs:
energy:
The energy of the largest peak in the spectrum.
See the documentation for Mca.initial_calibration() for more information.
- read_file(self, file, netcdf=0)
- Reads a disk file into an Med object. The file contains the information
from the Med object which it makes sense to store permanently, but does
not contain all of the internal state information for the Med.
Inputs:
file:
The name of the disk file to read.
- set_calibration(self, calibration)
- This procedure sets the calibration parameters for the Med.
The calibration information is contained in an object or list of
objects of type McaCalibration.
Inputs:
calibration:
A single object or a list of objects of type McaCalibration
containing the calibration parameters for each Mca.
If a single object is passed then this is written to each Mca.
If a list of objects is passed then calibration[i] is written to
Mca[i].
- set_elapsed(self, elapsed)
- Sets the elapsed parameters for the Med.
The elapsed information is contained in an object or list of
objects of type McaElapsed.
Inputs:
elapsed:
A single structure or a list of structures of type McaElapsed
containing the elapsed parameters for each Mca.
If a single object is passed then this is written to each Mca.
If a list of objects is passed then elapsed[i] is written to Mca[i].
- set_presets(self, presets)
- This procedure set the preset parameters for the Med.
The elapsed information is contained in an object or list of
objects of type McaPresets.
Inputs:
presets:
A single object or a list of objects of type McaPresets containing
the preset parameters for each Mca.
If a single object is passed then this is written to each Mca.
If a list of objects is passed then presets[i] is written to Mca[i].
- set_rois(self, rois, energy=0)
- This procedure sets the ROIs for the Med.
The elapsed information is contained in a list of McaRoi objects,
or list of such lists.
Inputs:
rois:
A single list or a nested list of objects McaROI objects.
If a single list is passed then this is written to each Mca.
If a list of lists is passed then rois[i][*] is written to Mca[i].
Data and non-method functions defined here:
- __doc__ = '\n The MED class is basically a collection of M...s is the superclass of the\n epicsMed class.\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__ = 'Med'
- str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
Methods inherited from Mca.Mca:
- __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.
- 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)
- 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_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
- set_data(self, data)
- Copies an array of data (counts) to the Mca.
Inputs:
data:
A Numeric array of data (counts).
- 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
- 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')
| |