qetpy.fitting package¶
Submodules¶
qetpy.fitting.fitting module¶
-
qetpy.fitting.fitting.
ofamp
(signal, template, psd, fs, withdelay=True, coupling='AC', lgcsigma=False, nconstrain=None, lgcoutsidewindow=False, integralnorm=False)¶ Function for calculating the optimum amplitude of a pulse in data. Supports optimum filtering with and without time delay.
Parameters: - signal : ndarray
The signal that we want to apply the optimum filter to (units should be Amps). Can be an array of traces.
- template : ndarray
The pulse template to be used for the optimum filter (should be normalized beforehand).
- psd : ndarray
The two-sided psd that will be used to describe the noise in the signal (in Amps^2/Hz)
- fs : float
The sample rate of the data being taken (in Hz).
- withdelay : bool, optional
Determines whether or not the optimum amplitude should be calculate with (True) or without (False) using a time delay. With the time delay, the pulse is assumed to be at any time in the trace. Without the time delay, the pulse is assumed to be directly in the middle of the trace. Default is True.
- coupling : str, optional
String that determines if the zero frequency bin of the psd should be ignored (i.e. set to infinity) when calculating the optimum amplitude. If set to ‘AC’, then ths zero frequency bin is ignored. If set to anything else, then the zero frequency bin is kept. Default is ‘AC’.
- lgcsigma : Boolean, optional
If True, the estimated optimal filter energy resolution will be calculated and returned.
- nconstrain : int, NoneType, optional
The length of the window (in bins) to constrain the possible t0 values to, centered on the unshifted trigger. If left as None, then t0 is uncontrained. If nconstrain is larger than nbins, then the function sets nconstrain to nbins, as this is the maximum number of values that t0 can vary over.
- lgcoutsidewindow : bool, optional
Boolean flag that is used to specify whether ofamp should look inside nconstrain or outside it. If False, ofamp will minimize the chi^2 in the bins specified by nconstrain, which is the default behavior. If True, then ofamp will minimize the chi^2 in the bins that do not contain the constrained window.
- integralnorm : bool, optional
If set to True, then ofamp will normalize the template to an integral of 1, and ofamp will instead return the optimal integral in units of Coulombs. If lgcsigma is set to True, then it will be returned in units of Coulombs as well. If set to False, then the usual optimal filter amplitude will be returned (in units of Amps).
Returns: - amp : float
The optimum amplitude calculated for the trace (in Amps).
- t0 : float
The time shift calculated for the pulse (in s). Set to zero if withdelay is False.
- chi2 : float
The chi^2 value calculated from the optimum filter.
- sigma : float, optional
The optimal filter energy resolution (in Amps)
-
qetpy.fitting.fitting.
ofamp_pileup
(signal, template, psd, fs, a1=None, t1=None, coupling='AC', nconstrain1=None, nconstrain2=None, lgcoutsidewindow=True)¶ Function for calculating the optimum amplitude of a pileup pulse in data. Supports inputted the values of a previously known pulse for increased computational speed, but can be used on its own.
Parameters: - signal : ndarray
The signal that we want to apply the optimum filter to (units should be Amps).
- template : ndarray
The pulse template to be used for the optimum filter (should be normalized beforehand).
- psd : ndarray
The two-sided psd that will be used to describe the noise in the signal (in Amps^2/Hz)
- fs : float
The sample rate of the data being taken (in Hz).
- a1 : float, optional
The OF amplitude (in Amps) to use for the “main” pulse, e.g. the triggered pulse. This should be calculated beforehand using ofamp. This is only used if t1 is also inputted.
- t1 : float, optional
The corresponding time offset (in seconds) to use for the “main” pulse, e.g. the triggered pulse. As with a1, this should be calculated beforehand using ofamp. This is only used if a1 is also inputted.
- coupling : str, optional
String that determines if the zero frequency bin of the psd should be ignored (i.e. set to infinity) when calculating the optimum amplitude. If set to ‘AC’, then ths zero frequency bin is ignored. If set to anything else, then the zero frequency bin is kept. Default is ‘AC’.
- nconstrain1 : int, NoneType, optional
If t1 is left as None, this is the length of the window (in bins) to constrain the possible t1 values to for the first pulse, centered on the unshifted trigger. If left as None, then t1 is uncontrained. If nconstrain1 is larger than nbins, then the function sets nconstrain1 to nbins, as this is the maximum number of values that t1 can vary over. This is only used if a1 or t1 is not given.
- nconstrain2 : int, NoneType, optional
This is the length of the window (in bins) out of which to constrain the possible t2 values to for the pileup pulse, centered on the unshifted trigger. If left as None, then t2 is uncontrained. The value of nconstrain2 should be less than nbins.
- lgcoutsidewindow : bool, optional
Boolean flag that is used to specify whether ofamp_pileup should look for the pileup pulse inside the bins specified by nconstrain2 or outside them. If True, ofamp will minimize the chi^2 in the bins ouside the range specified by nconstrain2, which is the default behavior. If False, then ofamp will minimize the chi^2 in the bins inside the constrained window specified by nconstrain2.
Returns: - a1 : float
The optimum amplitude (in Amps) calculated for the first pulse that was found, which is generally the triggered pulse.
- t1 : float
The time shift calculated for the first pulse that was found (in s)
- a2 : float
The optimum amplitude calculated for the pileup pulse (in Amps).
- t2 : float
The time shift calculated for the pileup pulse (in s)
- chi2 : float
The chi^2 value calculated for the pileup optimum filter.
-
qetpy.fitting.fitting.
ofamp_pileup_stationary
(signal, template, psd, fs, coupling='AC', nconstrain=None, lgcoutsidewindow=False)¶ Function for calculating the optimum amplitude of a pileup pulse in data, with the assumption that the triggered pulse is centered in the trace.
Parameters: - signal : ndarray
The signal that we want to apply the optimum filter to (units should be Amps).
- template : ndarray
The pulse template to be used for the optimum filter (should be normalized beforehand).
- psd : ndarray
The two-sided psd that will be used to describe the noise in the signal (in Amps^2/Hz)
- fs : float
The sample rate of the data being taken (in Hz).
- coupling : str, optional
String that determines if the zero frequency bin of the psd should be ignored (i.e. set to infinity) when calculating the optimum amplitude. If set to ‘AC’, then ths zero frequency bin is ignored. If set to anything else, then the zero frequency bin is kept. Default is ‘AC’.
- nconstrain : int, NoneType, optional
This is the length of the window (in bins) out of which to constrain the possible t2 values to for the pileup pulse, centered on the unshifted trigger. If left as None, then t2 is uncontrained. The value of nconstrain should be less than nbins.
- lgcoutsidewindow : bool, optional
Boolean flag that is used to specify whether the function should look for the pileup pulse inside the bins specified by nconstrain or outside them. If True, ofamp will minimize the chi^2 in the bins ouside the range specified by nconstrain, which is the default behavior. If False, then ofamp will minimize the chi^2 in the bins inside the constrained window specified by nconstrain.
Returns: - a1 : float
The optimum amplitude (in Amps) calculated for the first pulse that was found, which is the triggered pulse.
- a2 : float
The optimum amplitude calculated for the pileup pulse (in Amps).
- t2 : float
The time shift calculated for the pileup pulse (in s)
- chi2 : float
The reduced chi^2 value of the fit.
-
qetpy.fitting.fitting.
chi2lowfreq
(signal, template, amp, t0, psd, fs, fcutoff=10000)¶ Function for calculating the low frequency chi^2 of the optimum filter, given some cut off frequency. This function does not calculate the optimum amplitude - it requires that ofamp has been run, and the fit has been loaded to this function.
Parameters: - signal : ndarray
The signal that we want to calculate the low frequency chi^2 of (units should be Amps).
- template : ndarray
The pulse template to be used for the low frequency chi^2 calculation (should be normalized beforehand).
- amp : float
The optimum amplitude calculated for the trace (in Amps).
- t0 : float
The time shift calculated for the pulse (in s).
- psd : ndarray
The two-sided psd that will be used to describe the noise in the signal (in Amps^2/Hz).
- fs : float
The sample rate of the data being taken (in Hz).
- fcutoff : float, optional
The frequency (in Hz) that we should cut off the chi^2 when calculating the low frequency chi^2.
Returns: - chi2low : float
The low frequency chi^2 value (cut off at fcutoff) for the inputted values.
-
qetpy.fitting.fitting.
chi2_nopulse
(signal, psd, fs, coupling='AC')¶ Function for calculating the chi^2 of a trace with the assumption that there is no pulse.
Parameters: - signal : ndarray
The signal that we want to calculate the no pulse chi^2 of (units should be Amps).
- psd : ndarray
The two-sided psd that will be used to describe the noise in the signal (in Amps^2/Hz).
- fs : float
The sample rate of the data being taken (in Hz).
- coupling : str, optional
String that determines if the zero frequency bin of the psd should be ignored (i.e. set to infinity) when calculating the no pulse chi^2 . If set to ‘AC’, then the zero frequency bin is ignored. If set to anything else, then the zero frequency bin is kept. Default is ‘AC’.
Returns: - chi2_0 : float
The chi^2 value for there being no pulse.
-
class
qetpy.fitting.fitting.
OFnonlin
(psd, fs, template=None)¶ Bases:
object
This class provides the user with a non-linear optimum filter to estimate the amplitude, rise time (optional), fall time, and time offset of a pulse.
Methods
calcchi2
(model)Function to calculate the reduced chi square fit_falltimes
(pulse[, lgcdouble, errscale, …])Function to do the fit onepole
(A, tau_f, t0)Functional form of pulse in time domain with the amplitude, fall time, and time offset allowed to float, and the rise time held constant residuals
(params)Function ot calculate the weighted residuals to be minimized twopole
(A, tau_r, tau_f, t0)Functional form of pulse in frequency domain with the amplitude, rise time, fall time, and time offset allowed to float. twopoletime
(A, tau_r, tau_f, t0)Functional form of pulse in time domain with the amplitude, rise time, fall time, and time offset allowed to float -
calcchi2
(model)¶ Function to calculate the reduced chi square
Parameters: - model: ndarray
Array corresponding to pulse function (twopole or onepole) evaluated at the optimum values
Returns: - chi2: float
The reduced chi squared statistic
-
fit_falltimes
(pulse, lgcdouble=False, errscale=1, guess=None, taurise=None, lgcfullrtn=False, lgcplot=False)¶ Function to do the fit
Parameters: - pulse: ndarray
Time series traces to be fit
- lgcdouble: bool, optional
If False, the twopole fit is done, if True, the one pole fit it done. Note, if True, the user must provide the value of taurise.
- errscale: float or int, optional
A scale factor for the psd. Ex: if fitting an average, the errscale should be set to the number of traces used in the average
- guess: tuple, optional
Guess of initial values for fit, must be the same size as the model being used for fit
- taurise: float, optional
The value of the rise time of the pulse if the single pole function is being use for fit
- lgcfullrtn: bool, optional
If False, only the best fit parameters are returned. If True, the errors in the fit parameters, the covariance matrix, and chi squared statistic are returned as well.
- lgcplot: bool, optional
If True, diagnostic plots are returned.
Returns: - variables: tuple
The best fit parameters
- errors: tuple
The corresponding fit errors for the best fit parameters
- cov: ndarray
The convariance matrix returned from the fit
- chi2: float
The reduced chi squared statistic evaluated at the optimum point of the fit
Raises: - ValueError
if length of guess does not match the number of parameters needed in fit
-
onepole
(A, tau_f, t0)¶ Functional form of pulse in time domain with the amplitude, fall time, and time offset allowed to float, and the rise time held constant
Parameters: - A: float
Amplitude of pulse
- tau_f: float
Fall time of two-pole pulse
- t0: float
Time offset of two pole pulse
Returns: - pulse: ndarray, complex
Array of amplitude values as a function of freuqncy
-
residuals
(params)¶ Function ot calculate the weighted residuals to be minimized
Parameters: - params: tuple
Tuple containing fit parameters
Returns: - z1d: ndarray
Array containing residuals per frequency bin. The complex data is flatted into single array
-
twopole
(A, tau_r, tau_f, t0)¶ Functional form of pulse in frequency domain with the amplitude, rise time, fall time, and time offset allowed to float. This is meant to be a private function
Parameters: - A: float
Amplitude of pulse
- tau_r: float
Rise time of two-pole pulse
- tau_f: float
Fall time of two-pole pulse
- t0: float
Time offset of two pole pulse
Returns: - pulse: ndarray, complex
Array of amplitude values as a function of frequency
-
twopoletime
(A, tau_r, tau_f, t0)¶ Functional form of pulse in time domain with the amplitude, rise time, fall time, and time offset allowed to float
Parameters: - A: float
Amplitude of pulse
- tau_r: float
Rise time of two-pole pulse
- tau_f: float
Fall time of two-pole pulse
- t0: float
Time offset of two pole pulse
Returns: - pulse: ndarray
Array of amplitude values as a function of time
-
-
class
qetpy.fitting.fitting.
MuonTailFit
(psd, fs)¶ Bases:
object
This class provides the user with a fitting routine to estimate the thermal muon tail fall time.
Methods
calcchi2
(model)Function to calculate the chi square fitmuontail
(signal[, lgcfullrtn, errscale])Function to do the fit muontailfcn
(A, tau)Functional form of a thermal muon tail in time domain with the amplitude and fall time allowed to float. residuals
(params)Function to calculate the weighted residuals to be minimized. -
calcchi2
(model)¶ Function to calculate the chi square
Parameters: - model: ndarray
Array corresponding to pulse function evaluated at the fitted values
Returns: - chi2: float
The chi squared statistic
-
fitmuontail
(signal, lgcfullrtn=False, errscale=1)¶ Function to do the fit
Parameters: - signal: ndarray
Time series traces to be fit
- lgcfullrtn: bool, optional
If False, only the best fit parameters are returned. If True, the errors in the fit parameters, the covariance matrix, and chi squared statistic are returned as well.
- errscale: float or int, optional
A scale factor for the psd. Ex: if fitting an average, the errscale should be set to the number of traces used in the average
Returns: - variables: tuple
The best fit parameters
- errors: tuple
The corresponding fit errors for the best fit parameters
- cov: ndarray
The convariance matrix returned from the fit
- chi2: float
The chi squared statistic evaluated at the fit
-
muontailfcn
(A, tau)¶ Functional form of a thermal muon tail in time domain with the amplitude and fall time allowed to float.
Parameters: - A: float
Amplitude of pulse
- tau: float
Fall time of muon tail
Returns: - pulse: ndarray
Array of amplitude values as a function of time
-
residuals
(params)¶ Function to calculate the weighted residuals to be minimized.
Parameters: - params: tuple
Tuple containing fit parameters
Returns: - z1d: ndarray
Array containing residuals per frequency bin. The complex data is flatted into single array.
-