-
Notifications
You must be signed in to change notification settings - Fork 278
Minimal initial commit of FreePACT code #2920
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
Draft
ParsonsRD
wants to merge
50
commits into
cta-observatory:main
Choose a base branch
from
ParsonsRD:freepact_repeat
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.
Draft
Changes from 19 commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
f3d441d
Include template paths as telescope parameters
gschwefer cd6e775
Move template set up to reconstructor init
gschwefer 5daf0cd
Make spe and pedestal width telescope parameters
gschwefer 37c6bd4
Add tel_type key to dummy templates
gschwefer 5beb81d
Make tests pass gain
gschwefer 8fbb6fc
Remove dummy reconstructor option
gschwefer c4bdb2b
Update shower processor config to work with ImPACT
gschwefer f87bf31
Add template paths to reconstruction tests
gschwefer 17bb07f
Add changelog
gschwefer f37c75b
Make template bounds a global variable
gschwefer a2542cc
Commit minimal version of FreePACT code
ParsonsRD 438e307
Merge branch 'cta-observatory:main' into freepact_repeat
ParsonsRD c03acdc
Minor commit to change to new container structure.
ParsonsRD cf85a0d
Merge branch 'cta-observatory:main' into freepact_repeat
ParsonsRD a81c146
Merge branch 'cta-observatory:main' into freepact_repeat
ParsonsRD 4514e78
Add tensorflow as optional dependency
ParsonsRD 4b34c72
Fix dependency versions in pyproject.toml
ParsonsRD c2226a3
Fixing CI build
ParsonsRD 0250307
Reformat of file
ParsonsRD d039bf3
Fixing CI build... again
ParsonsRD ee0e95b
Making import of tensorflow optional
ParsonsRD 64b92f2
Fix messed up merge
ParsonsRD ffd8d55
Fixing optional dependency
ParsonsRD 35b22ad
Fixed failing unit tests
ParsonsRD 2f468ec
Removed pedestal and spe default table
ParsonsRD 22e1300
Reverted change in iminuit requirement
ParsonsRD fde474c
Fixin TF exceptions
ParsonsRD 250d0ac
Merge branch 'cta-observatory:main' into freepact_repeat
ParsonsRD 46c97aa
Added FreePACT tests and minor fixes to impact
ParsonsRD 53007eb
Fixing import error as minor doc changes
ParsonsRD e8e162f
Again fixing optional dependencies for tensorflow.
ParsonsRD aa90a60
Remove masked array copy function no longer supported
ParsonsRD da3af8f
Remove masked array copy function no longer supported
ParsonsRD 61e9c60
Merge branch 'cta-observatory:main' into freepact_repeat
ParsonsRD c702573
Add filter for tensorflow warnings
ParsonsRD a396795
Filter for pyparsing warnings.
ParsonsRD 31ceb96
fix pyparsing warning
ParsonsRD 1fb8b0e
Again parsin warning fix
ParsonsRD 25c0b89
Fix filterwarnings
maxnoe d47afd1
Remove impact extra from minimal-dependencies CI job
maxnoe 6683ef3
sonarqube error fixes
ParsonsRD 64d93ca
Fix sonarqube complaints
ParsonsRD 7e57590
First FreePACT trainer commit
ParsonsRD 85a2b96
Reordered parameters in the interpolator
ParsonsRD 5b30303
General refactor of training script to work with DL1 exported data
ParsonsRD 5753282
Merge branch 'main' into freepact_repeat
ParsonsRD 5f766d6
Add optional dependency check and fixed loading of subarray from hdf …
ParsonsRD f1b81e7
Made tf import optional
ParsonsRD b477ae5
Some options added to FreePACT trainer, include trainer as command li…
ParsonsRD a050b63
Merge branch 'cta-observatory:main' into freepact_repeat
ParsonsRD 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 @@ | ||
| Make template paths and likelihood parameters TelecopeParameters in ImPACT |
This file contains hidden or 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 hidden or 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 hidden or 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,158 @@ | ||
| """FreePACT Reconstructor for ctapipe | ||
| This module implements the FreePACT reconstructor, which is a subclass of ImPACTReconstructor. | ||
| It uses a neural network to predict th likelihood camera images based on the shower parameters.""" | ||
|
|
||
| import numpy as np | ||
| import numpy.ma as ma | ||
|
|
||
| from ctapipe.core import traits | ||
| from ctapipe.core.telescope_component import TelescopeParameter | ||
| from ctapipe.reco.impact import ImPACTReconstructor | ||
| from ctapipe.utils.template_network_interpolator import FreePACTInterpolator | ||
|
|
||
| from ..compat import COPY_IF_NEEDED | ||
| from .impact_utilities import rotate_translate | ||
|
|
||
| __all__ = ["FreePACTReconstructor"] | ||
|
|
||
|
|
||
| class FreePACTReconstructor(ImPACTReconstructor): | ||
| """ | ||
| FreePACT Reconstructor | ||
| This class implements the FreePACT reconstructor, which uses a neural network | ||
| to predict the likelihood of camera images based on shower parameters. | ||
| """ | ||
|
|
||
| image_template_path = TelescopeParameter( | ||
| trait=traits.Path(exists=True, directory_ok=True, allow_none=False), | ||
| allow_none=False, | ||
| help=("Path to the image templates to be used in the reconstruction"), | ||
| ).tag(config=True) | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
|
maxnoe marked this conversation as resolved.
Outdated
|
||
| super().__init__(*args, **kwargs) | ||
|
|
||
| def set_up_templates(self): | ||
| """Set up the templates for the FreePACT reconstructor.""" | ||
| template_sort_dict = {} | ||
|
|
||
| for tel_id in self.subarray.tel_ids: | ||
| if self.image_template_path.tel[tel_id] not in template_sort_dict.keys(): | ||
| template_sort_dict[self.image_template_path.tel[tel_id]] = [tel_id] | ||
| else: | ||
| template_sort_dict[self.image_template_path.tel[tel_id]].append(tel_id) | ||
|
|
||
| for template_path, tel_ids in template_sort_dict.items(): | ||
| net_interpolator = FreePACTInterpolator(template_path) | ||
|
|
||
| self.prediction[tuple(tel_ids)] = net_interpolator | ||
|
|
||
| def get_likelihood( | ||
| self, | ||
| source_x, | ||
| source_y, | ||
| core_x, | ||
| core_y, | ||
| energy, | ||
| x_max_scale, | ||
| goodness_of_fit=False, | ||
| ): | ||
| """Get the likelihood that the image predicted at the given test | ||
| position matches the camera image. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| source_x: float | ||
| Source position of shower in the nominal system (in deg) | ||
| source_y: float | ||
| Source position of shower in the nominal system (in deg) | ||
| core_x: float | ||
| Core position of shower in tilted telescope system (in m) | ||
| core_y: float | ||
| Core position of shower in tilted telescope system (in m) | ||
| energy: float | ||
| Shower energy (in TeV) | ||
| x_max_scale: float | ||
| Scaling factor applied to geometrically calculated Xmax | ||
|
|
||
| Returns | ||
| ------- | ||
| float: Likelihood the model represents the camera image at this position | ||
|
|
||
| """ | ||
| if np.isnan(source_x) or np.isnan(source_y): | ||
| return 1e8 | ||
|
|
||
| # First we add units back onto everything. Currently not | ||
| # handled very well, maybe in future we could just put | ||
| # everything in the correct units when loading in the class | ||
| # and ignore them from then on | ||
|
|
||
| zenith = self.zenith | ||
| azimuth = self.azimuth | ||
|
|
||
| # Geometrically calculate the depth of maximum given this test position | ||
| x_max_guess = self.get_shower_max(source_x, source_y, core_x, core_y, zenith) | ||
| x_max_guess *= x_max_scale | ||
|
|
||
| # Convert to binning of Xmax | ||
| x_max_diff = x_max_guess | ||
|
|
||
| # Calculate impact distance for all telescopes | ||
| impact = np.sqrt( | ||
| (self.tel_pos_x - core_x) ** 2 + (self.tel_pos_y - core_y) ** 2 | ||
| ) | ||
| # And the expected rotation angle | ||
| phi = np.arctan2(self.tel_pos_y - core_y, self.tel_pos_x - core_x) | ||
|
|
||
| # Rotate and translate all pixels such that they match the | ||
| # template orientation | ||
| # numba does not support masked arrays, work on underlying array and add mask back | ||
| pix_x_rot, pix_y_rot = rotate_translate( | ||
| self.pixel_y.data, self.pixel_x.data, source_y, source_x, -phi | ||
| ) | ||
| pix_x_rot = np.ma.array(pix_x_rot, mask=self.pixel_x.mask, copy=COPY_IF_NEEDED) | ||
| pix_y_rot = np.ma.array(pix_y_rot, mask=self.pixel_y.mask, copy=COPY_IF_NEEDED) | ||
|
|
||
| # In the interpolator class we can gain speed advantages by using masked arrays | ||
| # so we need to make sure here everything is masked | ||
| likelihood = ma.zeros(self.image.shape) | ||
| likelihood.mask = ma.getmask(self.image) | ||
|
|
||
| for tel_ids, template in self.prediction.items(): | ||
| template_mask = self.template_masks[tel_ids] | ||
| if np.any(template_mask): | ||
| likelihood[template_mask] = template( | ||
| np.rad2deg(zenith), | ||
| azimuth, | ||
| energy * np.ones_like(impact[template_mask]), | ||
| impact[template_mask], | ||
| x_max_diff * np.ones_like(impact[template_mask]), | ||
| np.rad2deg(pix_x_rot[template_mask]), | ||
| np.rad2deg(pix_y_rot[template_mask]), | ||
| self.image[template_mask], | ||
| ) | ||
| if goodness_of_fit: | ||
| # return -2 * np.sum(likelihood[self.image>5]) / np.sum(self.image>5) | ||
| # print(ma.getmask(self.image)) | ||
| return -2 * ma.sum(likelihood) / np.sum(~ma.getmask(self.image)) | ||
|
|
||
| likelihood = ma.sum(likelihood) * -2 | ||
|
|
||
| return likelihood | ||
|
|
||
|
|
||
| class FreePACTProtonReconstructor(FreePACTReconstructor): | ||
| """FreePACT Proton Reconstructor | ||
|
maxnoe marked this conversation as resolved.
Outdated
|
||
| This class implements the FreePACT reconstructor for proton showers. | ||
| It uses a neural network to predict the likelihood of camera images based on shower parameters. | ||
| """ | ||
|
|
||
| image_template_path = TelescopeParameter( | ||
| trait=traits.Path(exists=True, directory_ok=True, allow_none=False), | ||
| allow_none=False, | ||
| help=("Path to the image templates to be used in the reconstruction"), | ||
| ).tag(config=True) | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.