-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b67fb1
commit e859537
Showing
3 changed files
with
165 additions
and
0 deletions.
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
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,81 @@ | ||
import json | ||
|
||
import pytest | ||
|
||
from ctapipe.core import ToolConfigurationError, run_tool | ||
from ctapipe.io import TableLoader | ||
|
||
|
||
def test_aggregate_features(dl2_shower_geometry_file_lapalma, tmp_path): | ||
from ctapipe.tools.aggregate_features import AggregateFeatures | ||
|
||
input_path = dl2_shower_geometry_file_lapalma | ||
output_path = tmp_path / "aggregated.dl1.h5" | ||
config_path = tmp_path / "config.json" | ||
|
||
with pytest.raises( | ||
ToolConfigurationError, | ||
match="No image parameters to aggregate are specified.", | ||
): | ||
run_tool( | ||
AggregateFeatures(), | ||
argv=[ | ||
f"--input={input_path}", | ||
f"--output={output_path}", | ||
], | ||
raises=True, | ||
) | ||
|
||
config = { | ||
"FeatureAggregator": { | ||
"FeatureGenerator": { | ||
"features": [("hillas_abs_skewness", "abs(hillas_skewness)")] | ||
}, | ||
"image_parameters": [ | ||
("hillas", "length"), | ||
("timing", "slope"), | ||
("HillasReconstructor", "tel_impact_distance"), | ||
("hillas", "abs_skewness"), | ||
], | ||
} | ||
} | ||
with config_path.open("w") as f: | ||
json.dump(config, f) | ||
|
||
# test "overwrite" works | ||
with pytest.raises(ToolConfigurationError, match="exists, but overwrite=False"): | ||
run_tool( | ||
AggregateFeatures(), | ||
argv=[ | ||
f"--input={input_path}", | ||
f"--output={output_path}", | ||
f"--config={config_path}", | ||
], | ||
raises=True, | ||
) | ||
|
||
ret = run_tool( | ||
AggregateFeatures(), | ||
argv=[ | ||
f"--input={input_path}", | ||
f"--output={output_path}", | ||
f"--config={config_path}", | ||
"--overwrite", | ||
], | ||
raises=True, | ||
) | ||
assert ret == 0 | ||
|
||
with TableLoader(output_path) as loader: | ||
events = loader.read_subarray_events( | ||
dl1_aggregates=True, | ||
dl2=False, | ||
simulated=False, | ||
) | ||
assert "hillas_length_max" in events.colnames | ||
assert "hillas_length_min" in events.colnames | ||
assert "hillas_length_mean" in events.colnames | ||
assert "hillas_length_std" in events.colnames | ||
assert "timing_slope_mean" in events.colnames | ||
assert "HillasReconstructor_tel_impact_distance_mean" in events.colnames | ||
assert "hillas_abs_skewness_mean" in events.colnames |
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,50 @@ | ||
import numpy as np | ||
from astropy.table import Table | ||
|
||
from ctapipe.io import TableLoader, read_table | ||
from ctapipe.io.tests.test_table_loader import check_equal_array_event_order | ||
|
||
|
||
def test_get_subarray_index(dl1_parameters_file): | ||
from ctapipe.vectorization import get_subarray_index | ||
|
||
opts = dict(simulated=False, true_parameters=False, dl2=False, pointing=False) | ||
with TableLoader(dl1_parameters_file, **opts) as loader: | ||
tel_events = loader.read_telescope_events() | ||
|
||
subarray_obs_ids, subarray_event_ids, _, _ = get_subarray_index(tel_events) | ||
trigger = read_table(dl1_parameters_file, "/dl1/event/subarray/trigger") | ||
|
||
assert len(subarray_obs_ids) == len(subarray_event_ids) | ||
assert len(subarray_obs_ids) == len(trigger) | ||
check_equal_array_event_order( | ||
Table({"obs_id": subarray_obs_ids, "event_id": subarray_event_ids}), trigger | ||
) | ||
|
||
|
||
def test_mean_std_ufunc(dl1_parameters_file): | ||
from ctapipe.vectorization import get_subarray_index, weighted_mean_std_ufunc | ||
|
||
opts = dict(simulated=False, true_parameters=False, dl2=False, pointing=False) | ||
with TableLoader(dl1_parameters_file, **opts) as loader: | ||
tel_events = loader.read_telescope_events() | ||
|
||
valid = np.isfinite(tel_events["hillas_length"]) | ||
obs_ids, event_ids, m, tel_to_subarray_idx = get_subarray_index(tel_events) | ||
n_array_events = len(obs_ids) | ||
|
||
# test only default uniform weights, | ||
# other weights are tested in test_stereo_combination | ||
mean, std = weighted_mean_std_ufunc( | ||
tel_events["hillas_length"], valid, n_array_events, tel_to_subarray_idx, m | ||
) | ||
|
||
# check if result is identical with np.mean/np.std | ||
true_mean = np.full(n_array_events, np.nan) | ||
true_std = np.full(n_array_events, np.nan) | ||
grouped = tel_events.group_by(["obs_id", "event_id"]) | ||
true_mean = grouped["hillas_length"].groups.aggregate(np.nanmean) | ||
true_std = grouped["hillas_length"].groups.aggregate(np.nanstd) | ||
|
||
assert np.allclose(mean, true_mean, equal_nan=True) | ||
assert np.allclose(std, true_std, equal_nan=True) |