Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/cta-observatory/ctapipe i…
Browse files Browse the repository at this point in the history
…nto refactor/rename_data_writer_options
  • Loading branch information
kosack committed Jun 7, 2022
2 parents 89f7135 + d02aad7 commit 849d69f
Show file tree
Hide file tree
Showing 45 changed files with 1,889 additions and 1,825 deletions.
17 changes: 14 additions & 3 deletions ctapipe/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
from ctapipe.utils import get_dataset_path
from ctapipe.utils.filelock import FileLock

from pytest_astropy_header.display import PYTEST_HEADER_MODULES

PYTEST_HEADER_MODULES.clear()
PYTEST_HEADER_MODULES["eventio"] = "eventio"
PYTEST_HEADER_MODULES["numpy"] = "numpy"
PYTEST_HEADER_MODULES["scipy"] = "scipy"
PYTEST_HEADER_MODULES["astropy"] = "astropy"
PYTEST_HEADER_MODULES["numba"] = "numba"

# names of camera geometries available on the data server
camera_names = [
"ASTRICam",
Expand Down Expand Up @@ -44,7 +53,8 @@ def _global_example_event():

print("******************** LOAD TEST EVENT ***********************")

with SimTelEventSource(input_url=filename) as reader:
# FIXME: switch to prod5b+ file that contains effective focal length
with SimTelEventSource(input_url=filename, focal_length_choice="nominal") as reader:
event = next(iter(reader))

return event
Expand All @@ -59,7 +69,7 @@ def example_subarray():

print("******************** LOAD TEST EVENT ***********************")

with SimTelEventSource(input_url=filename) as reader:
with SimTelEventSource(input_url=filename, focal_length_choice="nominal") as reader:
return reader.subarray


Expand All @@ -84,7 +94,7 @@ def _subarray_and_event_gamma_off_axis_500_gev():

path = get_dataset_path("lst_prod3_calibration_and_mcphotons.simtel.zst")

with SimTelEventSource(path) as source:
with SimTelEventSource(path, focal_length_choice="nominal") as source:
it = iter(source)
# we want the second event, first event is a corner clipper
next(it)
Expand Down Expand Up @@ -353,6 +363,7 @@ def dl1_muon_file(dl1_tmp_path):
"--write-dl1-images",
"--DataWriter.write_dl1_parameters=False",
"--DataWriter.Contact.name=αℓℓ the äüöß",
"--SimTelEventSource.focal_length_choice=nominal",
]
assert run_tool(ProcessorTool(), argv=argv, cwd=dl1_tmp_path) == 0
return output
Expand Down
8 changes: 7 additions & 1 deletion ctapipe/coordinates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
import warnings
from .telescope_frame import TelescopeFrame
from .nominal_frame import NominalFrame
from .ground_frames import GroundFrame, TiltedGroundFrame, project_to_ground
from .ground_frames import (
GroundFrame,
TiltedGroundFrame,
project_to_ground,
EastingNorthingFrame,
)
from .camera_frame import CameraFrame, EngineeringCameraFrame


Expand All @@ -25,6 +30,7 @@
"NominalFrame",
"GroundFrame",
"TiltedGroundFrame",
"EastingNorthingFrame",
"MissingFrameAttributeWarning",
"altaz_to_righthanded_cartesian",
"project_to_ground",
Expand Down
60 changes: 57 additions & 3 deletions ctapipe/coordinates/ground_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@
- Tests Tests Tests!
"""
import astropy.units as u
from astropy.units.quantity import Quantity
import numpy as np
from astropy.coordinates import (
AltAz,
BaseCoordinateFrame,
CartesianRepresentation,
FunctionTransform,
CoordinateAttribute,
AltAz,
FunctionTransform,
RepresentationMapping,
frame_transform_graph,
AffineTransform,
)
from astropy.coordinates import frame_transform_graph
from numpy import cos, sin

__all__ = [
"GroundFrame",
"TiltedGroundFrame",
"project_to_ground",
"EastingNorthingFrame",
]


Expand All @@ -47,6 +51,25 @@ class GroundFrame(BaseCoordinateFrame):
default_representation = CartesianRepresentation


class EastingNorthingFrame(BaseCoordinateFrame):
"""GroundFrame but in standard Easting/Northing coordinates instead of
SimTel/Corsika conventions
Frame attributes: None
"""

default_representation = CartesianRepresentation

frame_specific_representation_info = {
CartesianRepresentation: [
RepresentationMapping("x", "easting"),
RepresentationMapping("y", "northing"),
RepresentationMapping("z", "height"),
]
}


class TiltedGroundFrame(BaseCoordinateFrame):
"""Tilted ground coordinate frame. The tilted ground coordinate frame
is a cartesian system describing the 2 dimensional projected
Expand Down Expand Up @@ -200,3 +223,34 @@ def project_to_ground(tilt_system):
y_projected = y_initial - trans[2][1] * z_initial / trans[2][2]

return GroundFrame(x=x_projected * unit, y=y_projected * unit, z=0 * unit)


@frame_transform_graph.transform(FunctionTransform, GroundFrame, GroundFrame)
def ground_to_ground(ground_coords, ground_frame):
"""Null transform for going from ground to ground, since there are no
attributes of the GroundSystem"""
return ground_coords


# Matrices for transforming between GroundFrame and EastingNorthingFrame
NO_OFFSET = CartesianRepresentation(Quantity([0, 0, 0], u.m))
GROUND_TO_EASTNORTH = np.asarray([[0, -1, 0], [1, 0, 0], [0, 0, 1]])


@frame_transform_graph.transform(AffineTransform, GroundFrame, EastingNorthingFrame)
def ground_to_easting_northing(ground_coords, eastnorth_frame):
"""
convert GroundFrame points into eastings/northings for plotting purposes
"""

return GROUND_TO_EASTNORTH, NO_OFFSET


@frame_transform_graph.transform(AffineTransform, EastingNorthingFrame, GroundFrame)
def easting_northing_to_ground(eastnorth_coords, ground_frame):
"""
convert eastings/northings back to GroundFrame
"""
return GROUND_TO_EASTNORTH.T, NO_OFFSET
28 changes: 24 additions & 4 deletions ctapipe/coordinates/tests/test_coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_telescope_separation():
tel1 = SkyCoord(fov_lon=0 * u.deg, fov_lat=0 * u.deg, frame=telescope_frame)
tel2 = SkyCoord(fov_lon=0 * u.deg, fov_lat=1 * u.deg, frame=telescope_frame)

assert tel1.separation(tel2) == u.Quantity(1, u.deg)
assert u.isclose(tel1.separation(tel2), 1 * u.deg)


def test_separation_is_the_same():
Expand Down Expand Up @@ -267,6 +267,7 @@ def test_camera_focal_length_array():


def test_ground_frame_roundtrip():
"""test transform from sky to ground roundtrip"""
from ctapipe.coordinates import GroundFrame, TiltedGroundFrame

normal = SkyCoord(alt=70 * u.deg, az=0 * u.deg, frame=AltAz())
Expand All @@ -275,6 +276,25 @@ def test_ground_frame_roundtrip():

back = tilted.transform_to(GroundFrame())

assert u.isclose(coord.x, back.x)
assert u.isclose(coord.y, back.y)
assert u.isclose(coord.z, back.z)
assert u.isclose(coord.x, back.x, atol=1e-12 * u.m)
assert u.isclose(coord.y, back.y, atol=1e-12 * u.m)
assert u.isclose(coord.z, back.z, atol=1e-12 * u.m)


def test_ground_to_eastnorth_roundtrip():
"""Check Ground to EastingNorthing and the round-trip"""
from ctapipe.coordinates import GroundFrame, EastingNorthingFrame

ground = SkyCoord(
x=[1, 2, 3] * u.m, y=[-2, 5, 2] * u.m, z=[1, -1, 2] * u.m, frame=GroundFrame()
)
eastnorth = ground.transform_to(EastingNorthingFrame())
ground2 = eastnorth.transform_to(GroundFrame())

assert u.isclose(eastnorth.easting, [2, -5, -2] * u.m).all()
assert u.isclose(eastnorth.northing, [1, 2, 3] * u.m).all()
assert u.isclose(eastnorth.height, [1, -1, 2] * u.m).all()

assert u.isclose(ground.x, ground2.x).all()
assert u.isclose(ground.y, ground2.y).all()
assert u.isclose(ground.z, ground2.z).all()
33 changes: 29 additions & 4 deletions ctapipe/core/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from abc import ABCMeta
from inspect import isabstract
from logging import getLogger
from docutils.core import publish_parts
from inspect import cleandoc

from traitlets import TraitError
from traitlets.config import Configurable
Expand Down Expand Up @@ -219,22 +221,45 @@ def _repr_html_(self):
"""nice HTML rep, with blue for non-default values"""
traits = self.traits()
name = self.__class__.__name__
docstring = (
publish_parts(cleandoc(self.__class__.__doc__), writer_name="html")[
"html_body"
]
or "Undocumented"
)
lines = [
"<div style='border:1px solid black; max-width: 700px; padding:2em'; word-wrap:break-word;>",
f"<b>{name}</b>",
f"<p> {self.__class__.__doc__ or 'Undocumented!'} </p>",
f"<p> {docstring} </p>",
"<table>",
" <colgroup>",
" <col span='1' style=' '>",
" <col span='1' style='width: 20em;'>",
" <col span='1' >",
" </colgroup>",
" <tbody>",
]
for key, val in self.get_current_config()[name].items():
htmlval = (
str(val).replace("/", "/<wbr>").replace("_", "_<wbr>")
) # allow breaking at boundary

# traits of the current component
if key in traits:
thehelp = f"{traits[key].help} (default: {traits[key].default_value})"
lines.append(f"<tr><th>{key}</th>")
if val != traits[key].default_value:
lines.append(f"<td><span style='color:blue'>{val}</span></td>")
lines.append(
f"<td style='text-align: left;'><span style='color:blue; max-width:30em;'>{htmlval}</span></td>"
)
else:
lines.append(f"<td>{val}</td>")
lines.append(f'<td style="text-align:left"><i>{thehelp}</i></td></tr>')
lines.append(f"<td style='text-align: left;'>{htmlval}</td>")
lines.append(
f"<td style='text-align: left;'><i>{thehelp}</i></td></tr>"
)
lines.append(" </tbody>")
lines.append("</table>")
lines.append("</div>")
return "\n".join(lines)


Expand Down
6 changes: 4 additions & 2 deletions ctapipe/core/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def __repr__(self):
if self.ndim is not None:
desc += f" as a {self.ndim}-D array"
if self.dtype is not None:
desc += f" with type {self.dtype}"
desc += f" with dtype {self.dtype}"
if self.type is not None:
desc += f" with type {self.type}"

return desc

Expand Down Expand Up @@ -143,7 +145,7 @@ def validate(self, value):


class DeprecatedField(Field):
""" used to mark which fields may be removed in next version """
"""used to mark which fields may be removed in next version"""

def __init__(self, default, description="", unit=None, ucd=None, reason=""):
super().__init__(default=default, description=description, unit=unit, ucd=ucd)
Expand Down
51 changes: 35 additions & 16 deletions ctapipe/core/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from abc import abstractmethod
from typing import Union
import yaml
from docutils.core import publish_parts
from inspect import cleandoc

try:
import tomli as toml
Expand Down Expand Up @@ -401,30 +403,47 @@ def _repr_html_(self):
"""nice HTML rep, with blue for non-default values"""
traits = self.traits()
name = self.__class__.__name__
docstring = (
publish_parts(
cleandoc(self.__class__.__doc__ or self.description), writer_name="html"
)["html_body"]
or "Undocumented"
)
lines = [
f"<b>{name}</b>",
f"<p> {self.__class__.__doc__ or self.description} </p>",
f"<p> {docstring} </p>",
"<table>",
" <colgroup>",
" <col span='1' style=' '>",
" <col span='1' style='width: 20em;'>",
" <col span='1' >",
" </colgroup>",
" <tbody>",
]
for key, val in self.get_current_config()[name].items():
# after running setup, also the subcomponents are in the current config
# which are not in traits
if key not in traits:
continue

default = traits[key].default_value
thehelp = f"{traits[key].help} (default: {default})"
lines.append(f"<tr><th>{key}</th>")
if val != default:
lines.append(f"<td><span style='color:blue'>{val}</span></td>")
else:
lines.append(f"<td>{val}</td>")
lines.append(f'<td style="text-align:left"><i>{thehelp}</i></td></tr>')

htmlval = (
str(val).replace("/", "/<wbr>").replace("_", "_<wbr>")
) # allow breaking at boundary

# traits of the current component
if key in traits:
thehelp = f"{traits[key].help} (default: {traits[key].default_value})"
lines.append(f"<tr><th>{key}</th>")
if val != traits[key].default_value:
lines.append(
f"<td style='text-align: left;'><span style='color:blue; max-width:30em;'>{htmlval}</span></td>"
)
else:
lines.append(f"<td style='text-align: left;'>{htmlval}</td>")
lines.append(
f"<td style='text-align: left;'><i>{thehelp}</i></td></tr>"
)
lines.append(" </tbody>")
lines.append("</table>")
lines.append("<p><i>Components:</i>")
lines.append("<p><b>Components:</b>")
lines.append(", ".join([x.__name__ for x in self.classes]))
lines.append("</p>")
lines.append("</div>")

return "\n".join(lines)

Expand Down
4 changes: 2 additions & 2 deletions ctapipe/image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
TwoPassWindowSum,
extract_around_peak,
extract_sliding_window,
neighbor_average_waveform,
neighbor_average_maximum,
subtract_baseline,
integration_correction,
)
Expand Down Expand Up @@ -110,7 +110,7 @@
"TwoPassWindowSum",
"extract_around_peak",
"extract_sliding_window",
"neighbor_average_waveform",
"neighbor_average_maximum",
"subtract_baseline",
"integration_correction",
"DataVolumeReducer",
Expand Down
Loading

0 comments on commit 849d69f

Please sign in to comment.