Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify pixel log-likelihood. #1403

Merged
merged 18 commits into from
Oct 14, 2020
18 changes: 10 additions & 8 deletions ctapipe/image/pixel_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
At high signal this simplifies to a gaussian approximation.

The full and gaussian approximations are implemented, in addition to a general purpose
implementation, which tries to intellegently switch
implementation, which tries to intellegently switch
between the two. Speed tests are below:

neg_log_likelihood_approx(image, prediction, spe, ped)
Expand Down Expand Up @@ -129,7 +129,7 @@ def neg_log_likelihood_numeric(

likelihood = epsilon

ns = np.arange(*poisson(np.max(prediction)).ppf(confidence),)
ns = np.arange(*poisson(np.max(prediction)).ppf(confidence))

ns = ns[ns >= 0]

Expand Down Expand Up @@ -177,13 +177,15 @@ def neg_log_likelihood(
approx_mask = prediction > prediction_safety

neg_log_l = 0
neg_log_l += neg_log_likelihood_approx(
image[approx_mask], prediction[approx_mask], spe_width, pedestal,
)
if np.sum(approx_mask) > 0:
nbiederbeck marked this conversation as resolved.
Show resolved Hide resolved
neg_log_l += neg_log_likelihood_approx(
image[approx_mask], prediction[approx_mask], spe_width, pedestal,
)

neg_log_l += neg_log_likelihood_numeric(
image[~approx_mask], prediction[~approx_mask], spe_width, pedestal,
)
if np.sum(~approx_mask) > 0:
nbiederbeck marked this conversation as resolved.
Show resolved Hide resolved
neg_log_l += neg_log_likelihood_numeric(
image[~approx_mask], prediction[~approx_mask], spe_width, pedestal,
)

return neg_log_l

Expand Down
31 changes: 15 additions & 16 deletions ctapipe/image/tests/test_pixel_likelihood.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
from ctapipe.image import poisson_likelihood_full, poisson_likelihood_gaussian
from ctapipe.image import neg_log_likelihood, neg_log_likelihood_approx


def test_full_likelihood():
Expand All @@ -11,30 +11,29 @@ def test_full_likelihood():
spe = 0.5 # Single photo-electron width
pedestal = 1 # width of the pedestal distribution

image_small = [0, 1, 2]
expectation_small = [1, 1, 1]
image_small = np.array([0, 1, 2])
expectation_small = np.array([1, 1, 1])

full_like_small = poisson_likelihood_full(
image_small, expectation_small, spe, pedestal
full_like_small = neg_log_likelihood(image_small, expectation_small, spe, pedestal)
exp_diff = full_like_small - np.sum(
np.asarray([2.75630505, 2.62168656, 3.39248449])
)
exp_diff = full_like_small - np.asarray([2.75630505, 2.62168656, 3.39248449])
exp_diff = np.sum(np.abs(exp_diff))

# Check against known values
print(exp_diff)
assert exp_diff / np.sum(full_like_small) < 1e-4

image_large = [40, 50, 60]
expectation_large = [50, 50, 50]
full_like_large = poisson_likelihood_full(
image_large, expectation_large, spe, pedestal
)
image_large = np.array([40, 50, 60])
expectation_large = np.array([50, 50, 50])

full_like_large = neg_log_likelihood(image_large, expectation_large, spe, pedestal)
# Check against known values
exp_diff = full_like_large - np.asarray([7.45489137, 5.99305388, 7.66226007])
exp_diff = np.sum(np.abs(exp_diff))
exp_diff = full_like_large - np.sum(
np.asarray([7.45489137, 5.99305388, 7.66226007])
)

assert exp_diff / np.sum(full_like_large) < 1e-4

gaus_like_large = poisson_likelihood_gaussian(
gaus_like_large = neg_log_likelihood_approx(
image_large, expectation_large, spe, pedestal
)

Expand Down