Skip to content

Commit

Permalink
Merge pull request #17 from fact-project/sensitivity
Browse files Browse the repository at this point in the history
Fix Sensitivity for negative signal, returns nan now
  • Loading branch information
maxnoe authored May 9, 2019
2 parents 3a5fd5a + 1f12fe9 commit b23deba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
34 changes: 23 additions & 11 deletions irf/sensitivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

@u.quantity_input(t_obs=u.hour, t_ref=u.hour)
def relative_sensitivity(
n_on,
n_off,
alpha,
t_obs,
t_ref=50*u.hour,
target_significance=5,
significance_function=li_ma_significance,
):
n_on,
n_off,
alpha,
t_obs,
t_ref=50*u.hour,
target_significance=5,
significance_function=li_ma_significance,
initial_guess=0.5,
):
'''
Calculate the relative sensitivity defined as the flux
relative to the reference source that is detectable with
Expand Down Expand Up @@ -52,6 +53,8 @@ def relative_sensitivity(
"Analysis methods for results in gamma-ray astronomy."
The Astrophysical Journal 272 (1983): 317-324.
Formula (17)
initial_guess: float
Initial guess for the root finder
'''

ratio = (t_ref / t_obs).si
Expand All @@ -64,17 +67,26 @@ def relative_sensitivity(
if np.isnan(n_on) or np.isnan(n_off):
return np.nan

if n_on == 0 or n_off == 0:
return np.nan

if n_signal <= 0:
return np.nan

def equation(relative_flux):
n_on = n_signal * relative_flux + n_background
return significance_function(n_on, n_off, alpha) - target_significance

try:
phi_rel = newton(equation, x0=1.0)
result = newton(
equation,
x0=initial_guess,
)
except RuntimeError:
warnings.warn('Could not calculate relative significance, returning nan')
phi_rel = np.nan
return np.nan

return phi_rel
return result


relative_sensitivity = np.vectorize(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='irf',
version='0.2.1',
version='0.3.0',
description='Functions to do instrument response functions for FACT',
url='http://github.com/fact-project/irf',
author='Kai Brügge, Maximilian Nöthe',
Expand Down

0 comments on commit b23deba

Please sign in to comment.