Skip to content

Commit

Permalink
Don't compute error metric if not explicitly requested in background …
Browse files Browse the repository at this point in the history
…fit.
  • Loading branch information
tritemio committed May 28, 2015
1 parent 69840c6 commit 7794cfb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
35 changes: 23 additions & 12 deletions fretbursts/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ def raw_fit(ph, clk_p=12.5e-9, residuals=False, tail_min_us=None):


def _compute_error(residuals, x_residuals, error_metrics):
assert error_metrics in ['KS', 'CM']
assert error_metrics in ['KS', 'CM', None]
error = None
if error_metrics == 'KS':
error = np.abs(residuals).max()*100
else:
elif error_metrics == 'CM':
error = np.trapz(residuals**2, x=x_residuals)
return error

Expand All @@ -65,14 +66,16 @@ def _exp_fit_generic(ph, fit_fun, tail_min_us=None, tail_min_p=0.1,
tail_min_p (int): min threshold in percentace. ONly used when
`tail_min_us` is None. Deprecated.
clk_p (float): unit of timestamps in `ph` (seconds).
error_metrics (string): Valid values are 'KS' or 'CM'.
error_metrics (string or None): Valid values are 'KS' or 'CM'.
'KS' (Kolmogorov-Smirnov statistics) computes the error as the
max of deviation of the empirical CDF from the fitted CDF.
'CM' (Crames-von Mises) uses the L^2 distance.
If None, no error metric is computed (returns None).
Returns:
Estimated background rate in cps and a quality of fit index
the lower the better according to the chosen metric.
2-Tuple: Estimated background rate in cps, and a "quality of fit"
index (the lower the better) according to the chosen metric.
If error_metrics==None, the returned "quality of fit" is None.
"""
dph = np.diff(ph)
if tail_min_us is None:
Expand All @@ -81,7 +84,6 @@ def _exp_fit_generic(ph, fit_fun, tail_min_us=None, tail_min_p=0.1,
tail_min = tail_min_us*1e-6/clk_p
Lambda, residuals, x_residuals, s_size = fit_fun(dph, s_min=tail_min)
Lambda /= clk_p
#pprint(s_size)
error = _compute_error(residuals, x_residuals, error_metrics)
return Lambda, error

Expand All @@ -99,13 +101,16 @@ def exp_fit(ph, tail_min_us=None, clk_p=12.5e-9, error_metrics='KS'):
ph (array): timestamps array from which to extract the background
tail_min_us (float): minimum waiting-time in micro-secs
clk_p (float): clock period for timestamps in `ph`
error_metrics (string): Valid values are 'KS' or 'CM'.
error_metrics (string or None): Valid values are 'KS' or 'CM'.
'KS' (Kolmogorov-Smirnov statistics) computes the error as the
max of deviation of the empirical CDF from the fitted CDF.
'CM' (Crames-von Mises) uses the L^2 distance.
If None, no error metric is computed (returns None).
Returns:
Estimated background rate in cps.
2-Tuple: Estimated background rate in cps, and a "quality of fit"
index (the lower the better) according to the chosen metric.
If error_metrics==None, the returned "quality of fit" is None.
See also:
:func:`exp_cdf_fit`, :func:`exp_hist_fit`
Expand All @@ -127,13 +132,16 @@ def exp_cdf_fit(ph, tail_min_us=None, clk_p=12.5e-9, error_metrics='KS'):
ph (array): timestamps array from which to extract the background
tail_min_us (float): minimum waiting-time in micro-secs
clk_p (float): clock period for timestamps in `ph`
error_metrics (string): Valid values are 'KS' or 'CM'.
error_metrics (string or None): Valid values are 'KS' or 'CM'.
'KS' (Kolmogorov-Smirnov statistics) computes the error as the
max of deviation of the empirical CDF from the fitted CDF.
'CM' (Crames-von Mises) uses the L^2 distance.
If None, no error metric is computed (returns None).
Returns:
Estimated background rate in cps.
2-Tuple: Estimated background rate in cps, and a "quality of fit"
index (the lower the better) according to the chosen metric.
If error_metrics==None, the returned "quality of fit" is None.
See also:
:func:`exp_fit`, :func:`exp_hist_fit`
Expand Down Expand Up @@ -161,13 +169,16 @@ def exp_hist_fit(ph, tail_min_us, binw=50e-6, clk_p=12.5e-9,
weights (None or string): if None no weights is applied.
if is 'hist_counts', each bin has a weight equal to its counts
if is 'inv_hist_counts', the weight is the inverse of the counts.
error_metrics (string): Valid values are 'KS' or 'CM'.
error_metrics (string or None): Valid values are 'KS' or 'CM'.
'KS' (Kolmogorov-Smirnov statistics) computes the error as the
max of deviation of the empirical CDF from the fitted CDF.
'CM' (Crames-von Mises) uses the L^2 distance.
If None, no error metric is computed (returns None).
Returns:
Estimated background rate in cps.
2-Tuple: Estimated background rate in cps, and a "quality of fit"
index (the lower the better) according to the chosen metric.
If error_metrics==None, the returned "quality of fit" is None.
See also:
:func:`exp_fit`, :func:`exp_cdf_fit`
Expand Down
2 changes: 1 addition & 1 deletion fretbursts/burstlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,7 @@ def _get_num_periods(self, time_s):
return int(nperiods)

def calc_bg(self, fun, time_s=60, tail_min_us=500, F_bg=2,
error_metrics='KS'):
error_metrics=None):
"""Compute time-dependent background rates for all the channels.
Compute background rates for donor, acceptor and both detectors.
Expand Down

0 comments on commit 7794cfb

Please sign in to comment.