-
Notifications
You must be signed in to change notification settings - Fork 2
3-telescope XGB #328
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
Merged
Merged
3-telescope XGB #328
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
40583bd
add 3-telescope selection
GernotMaier bfd767e
add shellcheck
GernotMaier 6158ef0
filter does not remove
GernotMaier 2b2fb2e
logger
GernotMaier 3dc8fb8
tests
GernotMaier f8e4bdd
Merge branch 'main' into v492-dev9
GernotMaier 96c2c71
args
GernotMaier 004cdeb
default values
GernotMaier 847cc03
Update python/applyXGBoostforDirection.py
GernotMaier c6764b3
postion alignement
GernotMaier f5c3c1b
args
GernotMaier 46098e2
args
GernotMaier a4327f7
3-tel
GernotMaier 99a300e
more efficient 3-telescope event selection
GernotMaier 52e75e7
filter 2
GernotMaier 2eed3fe
keep original disp tel list
GernotMaier c08f2d4
3-tel with padding
GernotMaier 9ffb754
ignore commit to branch
GernotMaier 5d5b856
process in chunks
GernotMaier 8fb1162
logger
GernotMaier 16510cf
docstrings
GernotMaier 7eeacbf
fix tests
GernotMaier 7856112
tests
GernotMaier 9f55368
Merge pull request #329 from VERITAS-Observatory/v492-dev9-in-chunks
GernotMaier 8f14c66
Merge branch 'main' into v492-dev9
GernotMaier 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 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,2 @@ | ||
| [pytest] | ||
| pythonpath = python |
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,100 @@ | ||
| import applyXGBoostforDirection as mod | ||
| import numpy as np | ||
| import pandas as pd | ||
|
|
||
|
|
||
| def test_parse_image_selection_indices(): | ||
| indices = mod.parse_image_selection("1,2,3") | ||
| assert indices == [1, 2, 3] | ||
|
|
||
|
|
||
| def test_parse_image_selection_bits(): | ||
| indices = mod.parse_image_selection("14") # 0b1110 -> [1,2,3] | ||
| assert indices == [1, 2, 3] | ||
|
|
||
|
|
||
| def test_filter_by_telescope_selection_returns_mask_and_preserves_length(): | ||
| df = pd.DataFrame( | ||
| { | ||
| "DispTelList_T": [ | ||
| [1, 2, 3], # has 1,2,3 | ||
| [1, 3], # missing 2 | ||
| [0, 1, 2, 3], # 4 telescopes -> always included | ||
| [0, 2], # missing 1,3 | ||
| ] | ||
| } | ||
| ) | ||
| selected = [1, 2, 3] | ||
| mask = mod.filter_by_telescope_selection(df, selected) | ||
| assert isinstance(mask, pd.Series) | ||
| assert len(mask) == len(df) | ||
| # Expect True for rows 0 and 2 | ||
| assert mask.tolist() == [True, False, True, False] | ||
|
|
||
|
|
||
| class DummyModel: | ||
| def __init__(self, out_val=(0.0, 0.0)): | ||
| self.out_val = np.array(out_val, dtype=float) | ||
|
|
||
| def predict(self, X): | ||
| # Return shape (n_rows, 2) filled with out_val | ||
| n = len(X) | ||
| return np.tile(self.out_val, (n, 1)) | ||
|
|
||
|
|
||
| def test_apply_models_with_selection_mask(monkeypatch): | ||
| # Build a minimal DataFrame with required columns | ||
| df = pd.DataFrame( | ||
| { | ||
| "DispNImages": [3, 3, 4, 2, 3], | ||
| "DispTelList_T": [ | ||
| [1, 2, 3], # selected | ||
| [1, 3], # unselected | ||
| [0, 1, 2, 3], # 4 telescopes -> selected | ||
| [0, 1], # unselected | ||
| [1, 2, 3], # selected | ||
| ], | ||
| # Truth-related columns used downstream; values not relevant for this test | ||
| "Xoff": [0, 0, 0, 0, 0], | ||
| "Yoff": [0, 0, 0, 0, 0], | ||
| "Xoff_intersect": [0, 0, 0, 0, 0], | ||
| "Yoff_intersect": [0, 0, 0, 0, 0], | ||
| } | ||
| ) | ||
|
|
||
| # Selection: require telescopes 1,2,3 or len==4 | ||
| selection_mask = mod.filter_by_telescope_selection(df, [1, 2, 3]) | ||
|
|
||
| # Monkeypatch model loading and existence checks | ||
| monkeypatch.setattr(mod.os.path, "exists", lambda p: True) | ||
|
|
||
| # Always return a DummyModel; value differentiates by n_tel if desired | ||
| def fake_load(path): | ||
| if "ntel4" in path: | ||
| return DummyModel(out_val=(4.0, -4.0)) | ||
| elif "ntel3" in path: | ||
| return DummyModel(out_val=(3.0, -3.0)) | ||
| else: | ||
| return DummyModel(out_val=(2.0, -2.0)) | ||
|
|
||
| monkeypatch.setattr(mod.joblib, "load", fake_load) | ||
|
|
||
| # Monkeypatch flattening to avoid complex array inputs | ||
| def fake_flatten(group_df, n_tel, training_vars): | ||
| # Provide a simple feature column not in excluded list | ||
| return pd.DataFrame({"feature": np.zeros(len(group_df))}, index=group_df.index) | ||
|
|
||
| monkeypatch.setattr(mod, "flatten_data_vectorized", fake_flatten) | ||
|
|
||
| pred_x, pred_y = mod.apply_models(df, "dummy_models_dir", selection_mask) | ||
|
|
||
| # Output length must match input length | ||
| assert len(pred_x) == len(df) | ||
| assert len(pred_y) == len(df) | ||
|
|
||
| # Unselected rows must be -999; selected rows come from model values | ||
| # From selection_mask above: rows 0,2,4 are selected; 1,3 are unselected | ||
| expected_x = [3.0, -999.0, 4.0, -999.0, 3.0] | ||
| expected_y = [-3.0, -999.0, -4.0, -999.0, -3.0] | ||
| assert np.allclose(pred_x, expected_x, equal_nan=False) | ||
| assert np.allclose(pred_y, expected_y, equal_nan=False) |
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
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.