-
Notifications
You must be signed in to change notification settings - Fork 272
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
Hillas psi uncertainty ruo #2629
Open
ruoyushang
wants to merge
9
commits into
main
Choose a base branch
from
hillas_psi_uncertainty_ruo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
29ad794
Implement uncertainty on hillas psi
maxnoe 1e31e69
add code for testing hillas psi angle uncertainty using toy model
715f037
add uncertainty for b parameter
70896cd
fix bug: ctapipe.core.container.FieldValidationError: HillasParameter…
53b9545
change name: transverse_cog_uncertainty
adcc86c
adding a readable file that expalins the changes
1452f66
updated after pre-commit run
619d2ef
update docs/changes/2629.feature.rst
523d2cd
test_hillas_psi_unc_toy_model.py was deleted
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -301,6 +301,10 @@ class CameraHillasParametersContainer(BaseHillasParametersContainer): | |
width = Field(nan * u.m, "standard spread along the minor-axis", unit=u.m) | ||
width_uncertainty = Field(nan * u.m, "uncertainty of width", unit=u.m) | ||
psi = Field(nan * u.deg, "rotation angle of ellipse", unit=u.deg) | ||
psi_uncertainty = Field(nan * u.deg, "uncertainty of psi", unit=u.deg) | ||
b_uncertainty = Field( | ||
nan * u.m, "rotated centroid y coordinate uncertainty", unit=u.m | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a hard time understanding how "rotated centroid y coordinate uncertainty" and "uncertainty on the center of gravity along the transverse axis of the image" can mean the same thing, and I think the definition you give in the changelog is much clearer. |
||
) | ||
|
||
|
||
class HillasParametersContainer(BaseHillasParametersContainer): | ||
|
@@ -329,6 +333,10 @@ class HillasParametersContainer(BaseHillasParametersContainer): | |
width = Field(nan * u.deg, "standard spread along the minor-axis", unit=u.deg) | ||
width_uncertainty = Field(nan * u.deg, "uncertainty of width", unit=u.deg) | ||
psi = Field(nan * u.deg, "rotation angle of ellipse", unit=u.deg) | ||
psi_uncertainty = Field(nan * u.deg, "uncertainty of psi", unit=u.deg) | ||
b_uncertainty = Field( | ||
nan * u.deg, "rotated centroid y coordinate uncertainty", unit=u.deg | ||
) | ||
|
||
|
||
class LeakageContainer(Container): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import os | ||
|
||
import astropy.units as u | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
from scipy.stats import norm | ||
from tqdm.auto import tqdm | ||
|
||
from ctapipe.image import hillas_parameters | ||
from ctapipe.image.cleaning import tailcuts_clean | ||
from ctapipe.image.toymodel import Gaussian | ||
from ctapipe.instrument import CameraGeometry | ||
|
||
ctapipe_output = os.environ.get("CTAPIPE_OUTPUT_PATH") | ||
ctapipe_input = os.environ.get("CTAPIPE_SVC_PATH") | ||
|
||
rng = np.random.default_rng(0) | ||
|
||
# cam = CameraGeometry.from_name("LSTCam") | ||
cam = CameraGeometry.from_name("NectarCam") | ||
# cam = CameraGeometry.from_name("SCTCam") | ||
|
||
true_width = 0.05 * u.m | ||
true_length = 0.3 * u.m | ||
true_psi = 45 * u.deg | ||
true_x = 0.5 * u.m | ||
true_y = -0.2 * u.m | ||
|
||
image_intensity = 1000 | ||
test_nsb_level_pe = 3 | ||
# test_nsb_level_pe = 5 | ||
|
||
# n_sample = 10 | ||
n_sample = 1000 | ||
|
||
model = Gaussian(true_x, true_y, true_length, true_width, true_psi) | ||
|
||
|
||
def sample_no_noise_no_cleaning(): | ||
_, signal, _ = model.generate_image( | ||
cam, intensity=image_intensity, nsb_level_pe=0, rng=rng | ||
) | ||
h = hillas_parameters(cam, signal) | ||
return h | ||
|
||
|
||
def sample_no_noise_with_cleaning(): | ||
_, signal, _ = model.generate_image( | ||
cam, intensity=image_intensity, nsb_level_pe=0, rng=rng | ||
) | ||
|
||
mask = tailcuts_clean( | ||
cam, | ||
signal, | ||
3.0 * test_nsb_level_pe, | ||
2.0 * test_nsb_level_pe, | ||
min_number_picture_neighbors=2, | ||
) | ||
|
||
image_clean = np.zeros_like(signal) | ||
for pix in range(0, len(signal)): | ||
image_clean[pix] = max(0.0, signal[pix] - 2.0 * test_nsb_level_pe) | ||
|
||
h = hillas_parameters(cam[mask], image_clean[mask]) | ||
return h | ||
|
||
|
||
def sample_noise_with_cleaning(): | ||
image, _, _ = model.generate_image( | ||
cam, intensity=image_intensity, nsb_level_pe=test_nsb_level_pe, rng=rng | ||
) | ||
|
||
mask = tailcuts_clean( | ||
cam, | ||
image, | ||
3.0 * test_nsb_level_pe, | ||
2.0 * test_nsb_level_pe, | ||
min_number_picture_neighbors=2, | ||
) | ||
|
||
image_clean = np.zeros_like(image) | ||
for pix in range(0, len(image)): | ||
image_clean[pix] = max(0.0, image[pix] - 2.0 * test_nsb_level_pe) | ||
|
||
h = hillas_parameters(cam[mask], image_clean[mask]) | ||
return h | ||
|
||
|
||
trials_no_noise_no_cleaning = [ | ||
sample_no_noise_no_cleaning() for _ in tqdm(range(n_sample)) | ||
] | ||
trials_no_noise_with_cleaning = [ | ||
sample_no_noise_with_cleaning() for _ in tqdm(range(n_sample)) | ||
] | ||
trials_noise_cleaning = [sample_noise_with_cleaning() for _ in tqdm(range(n_sample))] | ||
|
||
titles = [ | ||
"No Noise, all Pixels", | ||
f"No Noise, Tailcuts({test_nsb_level_pe*3}, {test_nsb_level_pe*2})", | ||
f"With Noise ({test_nsb_level_pe} p.e.), Tailcuts({test_nsb_level_pe*3}, {test_nsb_level_pe*2})", | ||
] | ||
values = [ | ||
trials_no_noise_no_cleaning, | ||
trials_no_noise_with_cleaning, | ||
trials_noise_cleaning, | ||
] | ||
|
||
fig, axs = plt.subplots(3, 1, constrained_layout=True, sharex=True) | ||
for ax, trials, title in zip(axs, values, titles): | ||
pred = np.array([t.psi.to_value(u.rad) for t in trials]) | ||
unc = np.array([t.psi_uncertainty.to_value(u.rad) for t in trials]) | ||
limits = np.quantile(pred, [0.001, 0.999]) | ||
hist, edges, plot = ax.hist(pred, bins=51, range=limits, density=True) | ||
x = np.linspace(edges[0], edges[-1], 500) | ||
ax.plot(x, norm.pdf(x, pred.mean(), pred.std())) | ||
ax.plot(x, norm.pdf(x, pred.mean(), unc.mean())) | ||
ax.set_title(title) | ||
axs[2].set_xlabel("Psi / rad") | ||
fig.savefig(f"{ctapipe_output}/output_plots/hillas_uncertainties.png", dpi=300) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs a better name. Also, for what is this additional uncertainty needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The axis of the image is modeled with the function y = ax + b. After rotating the image (so the axis is horizontal), the uncertainty of a = psi uncertainty. However, the axis uncertainty along the transverse direction should be the combination of the psi uncertainty and the b uncertainty, i.e. transverse uncertainty sigma_t = pow( pow(dsigma_psi,2) + pow(sigma_b,2) , 0.5), where d is the longitudinal coordinate of the test point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about "transverse_cog_uncertainty" ?