Skip to content

Commit

Permalink
Merge pull request #2440 from cta-observatory/fix_additional_disp_col…
Browse files Browse the repository at this point in the history
…umns

Fix unwanted default columns in disp output
  • Loading branch information
kosack authored Oct 31, 2023
2 parents 42e35c0 + 4788832 commit 1164e6c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
12 changes: 8 additions & 4 deletions ctapipe/reco/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def predict_table(self, key, table: Table) -> Dict[ReconstructionProperty, Table
result,
ReconstructedEnergyContainer,
prefix=self.prefix,
stereo=False,
add_tel_prefix=True,
)
return {ReconstructionProperty.ENERGY: result}

Expand Down Expand Up @@ -480,7 +480,10 @@ def predict_table(self, key, table: Table) -> Dict[ReconstructionProperty, Table
}
)
add_defaults_and_meta(
result, ParticleClassificationContainer, prefix=self.prefix, stereo=False
result,
ParticleClassificationContainer,
prefix=self.prefix,
add_tel_prefix=True,
)
return {ReconstructionProperty.PARTICLE_TYPE: result}

Expand Down Expand Up @@ -770,7 +773,8 @@ def predict_table(self, key, table: Table) -> Dict[ReconstructionProperty, Table
disp_result,
DispContainer,
prefix=f"{self.prefix}_parameter",
stereo=False,
# disp is always per telescope, so no need to add the prefix
add_tel_prefix=False,
)

psi = table["hillas_psi"].quantity.to_value(u.rad)
Expand All @@ -797,7 +801,7 @@ def predict_table(self, key, table: Table) -> Dict[ReconstructionProperty, Table
altaz_result,
ReconstructedGeometryContainer,
prefix=self.prefix,
stereo=False,
add_tel_prefix=True,
)

return {
Expand Down
14 changes: 7 additions & 7 deletions ctapipe/reco/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def add_defaults_and_meta(table, container, prefix=None, stereo=True):
def add_defaults_and_meta(table, container, prefix=None, add_tel_prefix=False):
"""
Fill column descriptions and default values into table for container
Expand All @@ -10,21 +10,21 @@ def add_defaults_and_meta(table, container, prefix=None, stereo=True):
the container class to add columns and descriptions to the table
prefix : str
prefix for the column names
stereo : bool
If False, add a ``tel_`` prefix to the column names to signal it's
add_tel_prefix : bool
If True, add a ``tel_`` prefix to the column names to signal it's
telescope-wise quantity
"""
if prefix is None:
prefix = container.default_prefix

for name, field in container.fields.items():
if not stereo and name == "telescopes":
if add_tel_prefix and name == "telescopes":
continue

if stereo:
colname = f"{prefix}_{name}"
else:
if add_tel_prefix:
colname = f"{prefix}_tel_{name}"
else:
colname = f"{prefix}_{name}"

if colname not in table.colnames and field.default is not None:
table[colname] = field.default
Expand Down
2 changes: 1 addition & 1 deletion ctapipe/tools/tests/test_apply_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ def test_apply_all(
energy_regressor_path,
particle_classifier_path,
disp_reconstructor_path,
dl2_shower_geometry_file_lapalma,
tmp_path,
):
from ctapipe.tools.apply_models import ApplyModels
Expand Down Expand Up @@ -211,6 +210,7 @@ def test_apply_all(
assert f"{prefix_disp}_tel_is_valid" in tel_events.colnames
assert f"{prefix_disp}_parameter_norm" in tel_events.colnames
assert f"{prefix_disp}_parameter_is_valid" in tel_events.colnames
assert f"{prefix_disp}_parameter_tel_is_valid" not in tel_events.colnames

# check that the "--no-dl1-parameters" option worked
assert "hillas_intensity" not in tel_events.colnames
Expand Down
1 change: 1 addition & 0 deletions docs/changes/2440.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix additional, unwanted columns being written into disp prediction output.
4 changes: 4 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,14 @@ def setup(app):
("py:class", "traitlets.config.application.Application"),
("py:class", "traitlets.utils.sentinel.Sentinel"),
("py:class", "traitlets.traitlets.ObserveHandler"),
("py:class", "dict[K, V]"),
("py:class", "G"),
("py:class", "t.Sequence"),
("py:class", "StrDict"),
("py:class", "ClassesType"),
("py:obj", "traitlets.traitlets.G"),
("py:obj", "traitlets.traitlets.S"),
("py:obj", "traitlets.traitlets.T"),
("py:class", "traitlets.traitlets.T"),
("py:class", "re.Pattern[t.Any]"),
("py:class", "Sentinel"),
Expand Down

0 comments on commit 1164e6c

Please sign in to comment.