Skip to content
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

Add a trait for astropy quantities #2524

Merged
merged 6 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes/2524.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add an ``AstroQuantity`` trait which can hold any ``astropy.units.Quantity``.
97 changes: 96 additions & 1 deletion src/ctapipe/core/tests/test_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import pathlib
import tempfile
from abc import ABCMeta, abstractmethod
from subprocess import CalledProcessError
from unittest import mock

import pytest
from traitlets import CaselessStrEnum, HasTraits, Int

from ctapipe.core import Component
from ctapipe.core import Component, Tool, run_tool
from ctapipe.core.traits import (
AstroQuantity,
AstroTime,
List,
Path,
Expand Down Expand Up @@ -278,6 +280,99 @@ class NoNone(Component):
c.time = None


def test_quantity():
import astropy.units as u

class SomeComponentWithQuantityTrait(Component):
quantity = AstroQuantity()

c = SomeComponentWithQuantityTrait()
c.quantity = -1.754 * u.m / (u.s * u.deg)
assert isinstance(c.quantity, u.Quantity)
assert c.quantity.value == -1.754
assert c.quantity.unit == u.Unit("m / (deg s)")

c.quantity = "1337 erg / centimeter**2 second"
assert isinstance(c.quantity, u.Quantity)
assert c.quantity.value == 1337
assert c.quantity.unit == u.Unit("erg / (s cm2)")

with pytest.raises(TraitError):
c.quantity = "No quantity"

# Try misspelled/ non-existent unit
with pytest.raises(TraitError):
c.quantity = "5 meters"

# Test definition of physical type
class SomeComponentWithEnergyTrait(Component):
energy = AstroQuantity(physical_type=u.physical.energy)

c = SomeComponentWithEnergyTrait()

class AnotherComponentWithEnergyTrait(Component):
energy = AstroQuantity(physical_type=u.TeV)

c = AnotherComponentWithEnergyTrait()

with pytest.raises(
TraitError,
match="Given physical type must be either of type"
+ " astropy.units.PhysicalType or a subclass of"
+ f" astropy.units.UnitBase, was {type(5 * u.TeV)}.",
):

class SomeBadComponentWithEnergyTrait(Component):
energy = AstroQuantity(physical_type=5 * u.TeV)

with pytest.raises(
TraitError,
match=f"Given physical type {u.physical.energy} does not match"
+ f" physical type of the default value, {u.get_physical_type(5 * u.m)}.",
):

class AnotherBadComponentWithEnergyTrait(Component):
energy = AstroQuantity(
default_value=5 * u.m, physical_type=u.physical.energy
)


def test_quantity_tool(capsys):
import astropy.units as u

class MyTool(Tool):
energy = AstroQuantity(physical_type=u.physical.energy).tag(config=True)

tool = MyTool()
run_tool(tool, ["--MyTool.energy=5 TeV"])
assert tool.energy == 5 * u.TeV

with pytest.raises(CalledProcessError):
run_tool(tool, ["--MyTool.energy=5 m"], raises=True)

captured = capsys.readouterr()
assert (
captured.err.split(":")[-1]
== f" Given quantity is of physical type {u.get_physical_type(5 * u.m)}."
+ f" Expected {u.physical.energy}.\n"
)


def test_quantity_none():
class AllowNone(Component):
quantity = AstroQuantity(default_value=None, allow_none=True)

c = AllowNone()
assert c.quantity is None

class NoNone(Component):
quantity = AstroQuantity(default_value="5 meter", allow_none=False)

c = NoNone()
with pytest.raises(TraitError):
c.quantity = None


def test_component_name():
from ctapipe.core.traits import ComponentName, ComponentNameList

Expand Down
56 changes: 55 additions & 1 deletion src/ctapipe/core/traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pathlib
from urllib.parse import urlparse

import astropy.units as u
import traitlets
import traitlets.config
from astropy.time import Time
Expand All @@ -17,6 +18,7 @@

__all__ = [
# Implemented here
"AstroQuantity",
"AstroTime",
"BoolTelescopeParameter",
"IntTelescopeParameter",
Expand Down Expand Up @@ -72,8 +74,60 @@
flag = traitlets.config.boolean_flag


class AstroQuantity(TraitType):
"""A trait containing an ``astropy.units`` quantity."""

def __init__(self, physical_type=None, **kwargs):
super().__init__(**kwargs)
if physical_type is not None:
if isinstance(physical_type, u.PhysicalType):
self.physical_type = physical_type
elif isinstance(physical_type, u.UnitBase):
self.physical_type = u.get_physical_type(physical_type)
else:
raise TraitError(
"Given physical type must be either of type"
" astropy.units.PhysicalType or a subclass of"
f" astropy.units.UnitBase, was {type(physical_type)}."
)
else:
self.physical_type = physical_type

if self.default_value is not Undefined and self.physical_type is not None:
default_type = u.get_physical_type(self.default_value)
if default_type != self.physical_type:
raise TraitError(
f"Given physical type {self.physical_type} does not match"
f" physical type of the default value, {default_type}."
)

def info(self):
info = "An ``astropy.units.Quantity`` instance"
if self.allow_none:
info += "or None"

Check warning on line 107 in src/ctapipe/core/traits.py

View check run for this annotation

Codecov / codecov/patch

src/ctapipe/core/traits.py#L107

Added line #L107 was not covered by tests
return info

def validate(self, obj, value):
try:
quantity = u.Quantity(value)
except TypeError:
return self.error(obj, value)
except ValueError:
return self.error(obj, value)

if self.physical_type is not None:
given_type = u.get_physical_type(quantity)
if given_type != self.physical_type:
raise TraitError(
f"Given quantity is of physical type {given_type}."
f" Expected {self.physical_type}."
)

return quantity


class AstroTime(TraitType):
"""A trait representing a point in Time, as understood by `astropy.time`"""
"""A trait representing a point in Time, as understood by ``astropy.time``."""

def validate(self, obj, value):
"""try to parse and return an ISO time string"""
Expand Down