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 support for GeneralSersic2D #221

Closed
wants to merge 11 commits into from
23 changes: 23 additions & 0 deletions .github/workflows/compare_asdf_support.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Compare ASDF support

on:
workflow_dispatch:
push:
branches:
- main
- '*.x'
tags:
- '*'
pull_request:

# Only cancel in-progress jobs or runs for the current workflow
# This cancels the already triggered workflows for a specific PR without canceling
# other instances of this workflow (other PRs, scheduled triggers, etc) when something
# within that PR re-triggers this CI
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
compare_asdf_support:
uses: braingram/asdf-actions/.github/workflows/compare_asdf_support.yml@main
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.6.0 (unreleased)
------------------

- Add support for GeneralSersic2D transform. [#221]

0.5.0 (2023-11-15)
------------------

Expand Down
2 changes: 1 addition & 1 deletion asdf_astropy/converters/coordinates/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_legacy_icrs_deseialize():
unit: deg"""
truth = ICRS(ra=Longitude(25, unit=u.deg), dec=Latitude(45, unit=u.deg))

buff = yaml_to_asdf(f"example: {example.strip()}")
buff = yaml_to_asdf(f"example: {example.strip()}", version="1.5.0")
with asdf.AsdfFile() as af:
af._open_impl(af, buff, mode="rw")
assert_frame_equal(af["example"], truth)
2 changes: 1 addition & 1 deletion asdf_astropy/converters/fits/tests/test_fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_asdf_tag():
- [BUNIT, DN, Units of the error array]
"""

buff = yaml_to_asdf(yaml)
buff = yaml_to_asdf(yaml, version="1.5.0")
with asdf.open(buff) as af:
hdul = af["hdul"]
assert len(hdul) == 3 # noqa: PLR2004
Expand Down
10 changes: 8 additions & 2 deletions asdf_astropy/converters/table/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@


class ColumnConverter(Converter):
tags = ("tag:stsci.edu:asdf/core/column-*",)
tags = (
"tag:stsci.edu:asdf/core/column-*",
"tag:stsci.edu:asdf/table/column-*",
)
types = (
"astropy.table.column.Column",
"astropy.table.column.MaskedColumn",
Expand Down Expand Up @@ -45,7 +48,10 @@ def from_yaml_tree(self, node, tag, ctx):


class AsdfTableConverter(Converter):
tags = ("tag:stsci.edu:asdf/core/table-*",)
tags = (
"tag:stsci.edu:asdf/core/table-*",
"tag:stsci.edu:asdf/table/table-*",
)
types = ()

def to_yaml_tree(self, obj, tag, ctx):
Expand Down
4 changes: 2 additions & 2 deletions asdf_astropy/converters/table/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def test_mismatched_columns():
colnames: [a, b]
"""

buff = yaml_to_asdf(yaml)
buff = yaml_to_asdf(yaml, version="1.5.0")

with pytest.raises(ValueError, match="Inconsistent data column lengths"), asdf.open(buff):
pass
Expand Down Expand Up @@ -251,7 +251,7 @@ def test_asdf_table():
description: The target name
name: c
"""
buff = yaml_to_asdf(yaml)
buff = yaml_to_asdf(yaml, version="1.5.0")

with asdf.open(buff) as af:
table = af["table"]
Expand Down
6 changes: 5 additions & 1 deletion asdf_astropy/converters/time/tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def create_examples():

@pytest.mark.parametrize("example", create_examples())
def test_read_examples(example):
buff = yaml_to_asdf(f"example: {example['example'].strip()}")
buff = yaml_to_asdf(f"example: {example['example'].strip()}", version="1.5.0")
with asdf.AsdfFile() as af:
af._open_impl(af, buff, mode="rw")
assert np.all(af["example"] == example["truth"])
Expand All @@ -133,6 +133,10 @@ def create_formats():

formats = []
for format_ in TIME_FORMATS:
if format_ == "stardate":
# stardate is not a documented format for astropy
# https://docs.astropy.org/en/latest/time/index.html#time-format
continue
new = Time("B2000.0")
new.format = format_
formats.append(new)
Expand Down
4 changes: 2 additions & 2 deletions asdf_astropy/converters/transform/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class ModelBoundingBoxConverter(Converter):
tags = ("tag:stsci.edu:asdf/transform/property/bounding_box-1.0.0",)
tags = ("tag:stsci.edu:asdf/transform/property/bounding_box-*",)
types = ("astropy.modeling.bounding_box.ModelBoundingBox",)

def to_yaml_tree(self, bbox, tag, ctx):
Expand Down Expand Up @@ -40,7 +40,7 @@ def create_bounding_box(model, cbbox=None):


class CompoundBoundingBoxConverter(Converter):
tags = ("tag:stsci.edu:asdf/transform/property/compound_bounding_box-1.0.0",)
tags = ("tag:stsci.edu:asdf/transform/property/compound_bounding_box-*",)
types = ("astropy.modeling.bounding_box.CompoundBoundingBox",)

def to_yaml_tree(self, cbbox, tag, ctx):
Expand Down
11 changes: 7 additions & 4 deletions asdf_astropy/converters/transform/tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def create_single_models(): # noqa: PLR0915
astropy_models.Scale(3.4),
astropy_models.Sersic1D(amplitude=10.0, r_eff=1.0, n=4.0),
astropy_models.Sersic2D(amplitude=10.0, r_eff=1.0, n=4.0, x_0=0.5, y_0=1.5, ellip=0.0, theta=0.0),
astropy_models.GeneralSersic2D(amplitude=10.0, r_eff=1.0, n=4.0, x_0=0.5, y_0=1.5, ellip=0.0, theta=0.0, c=0.5),
astropy_models.Shift(2.0),
astropy_models.Shift(2.0 * u.deg),
astropy_models.Scale(3.4 * u.deg),
Expand Down Expand Up @@ -485,13 +486,14 @@ def create_single_models(): # noqa: PLR0915
if minversion("astropy", "5.1") and not minversion("asdf_transform_schemas", "0.2.2", inclusive=False):
UNSUPPORTED_MODELS.append(astropy.modeling.powerlaws.Schechter1D)

if minversion("astropy", "6.0.dev"):
UNSUPPORTED_MODELS.append(astropy.modeling.functional_models.GeneralSersic2D)


@pytest.mark.parametrize("model", create_single_models())
def test_single_model(tmp_path, model):
helpers.assert_model_roundtrip(model, tmp_path)
kwargs = {}
# GeneralSersic2D is only supported in asdf standard 1.6.0
if isinstance(model, astropy_models.GeneralSersic2D):
kwargs["version"] = "1.6.0"
helpers.assert_model_roundtrip(model, tmp_path, **kwargs)


def get_all_models():
Expand Down Expand Up @@ -529,6 +531,7 @@ def test_all_models_supported(model):

def test_legacy_const(tmp_path):
with asdf.config_context() as config:
config.remove_extension("asdf://asdf-format.org/transform/extensions/transform-1.6.0")
config.remove_extension("asdf://asdf-format.org/transform/extensions/transform-1.5.0")

model = astropy_models.Const1D(amplitude=5.0)
Expand Down
6 changes: 3 additions & 3 deletions asdf_astropy/converters/unit/tests/test_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_read_untagged_unit():
value: {value}
unit: kpc
"""
buff = helpers.yaml_to_asdf(yaml)
buff = helpers.yaml_to_asdf(yaml, version="1.5.0")
with asdf.open(buff) as af:
assert af["quantity"].value == value
assert af["quantity"].unit.is_equivalent(units.kpc)
Expand All @@ -57,7 +57,7 @@ def test_read_tagged_unit():
value: {value}
unit: !unit/unit-1.0.0 kpc
"""
buff = helpers.yaml_to_asdf(yaml)
buff = helpers.yaml_to_asdf(yaml, version="1.5.0")
with asdf.open(buff) as af:
assert af["quantity"].value == value
assert af["quantity"].unit.is_equivalent(units.kpc)
Expand All @@ -69,7 +69,7 @@ def test_read_array_value():
value: !core/ndarray-1.0.0 [1.0, 2.0, 3.0, 4.0]
unit: km
"""
buff = helpers.yaml_to_asdf(yaml)
buff = helpers.yaml_to_asdf(yaml, version="1.5.0")
with asdf.open(buff) as af:
assert_array_equal(af["quantity"].value, np.array([1.0, 2.0, 3.0, 4.0]))
assert af["quantity"].unit.is_equivalent(units.km)
Expand Down
88 changes: 63 additions & 25 deletions asdf_astropy/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
via an ``entry-point`` in the ``pyproject.toml`` file.
"""

import asdf_coordinates_schemas
import asdf_standard
import asdf_transform_schemas
from asdf.extension import ManifestExtension
from astropy.utils import minversion

Expand Down Expand Up @@ -38,11 +41,12 @@
"TRANSFORM_EXTENSIONS",
"COORDINATES_CONVERTERS",
"ASTROPY_CONVERTERS",
"COORDINATES_EXTENSION",
"COORDINATES_EXTENSIONS",
"ASTROPY_EXTENSIONS",
"CORE_CONVERTERS",
"CORE_MANIFEST_URIS",
"CORE_EXTENSIONS",
"UNIT_EXTENSIONS",
]

TRANSFORM_CONVERTERS = [
Expand Down Expand Up @@ -82,6 +86,10 @@
["tag:stsci.edu:asdf/transform/gaussian2d-*"],
"astropy.modeling.functional_models.Gaussian2D",
),
SimpleTransformConverter(
["tag:stsci.edu:asdf/transform/general_sersic2d-*"],
"astropy.modeling.functional_models.GeneralSersic2D",
),
SimpleTransformConverter(
["tag:stsci.edu:asdf/transform/king_projected_analytic1d-*"],
"astropy.modeling.functional_models.KingProjectedAnalytic1D",
Expand Down Expand Up @@ -406,6 +414,11 @@
"asdf://asdf-format.org/transform/manifests/transform-1.0.0",
]

if asdf_transform_schemas.__version__ > "0.4.0":
TRANSFORM_MANIFEST_URIS.insert(
0,
"asdf://asdf-format.org/transform/manifests/transform-1.6.0",
)

TRANSFORM_EXTENSIONS = [
ManifestExtension.from_uri(
Expand Down Expand Up @@ -483,14 +496,27 @@
NdarrayMixinConverter(),
]


COORDINATES_EXTENSION = ManifestExtension.from_uri(
_COORDINATES_MANIFEST_URIS = [
"asdf://asdf-format.org/astronomy/coordinates/manifests/coordinates-1.0.0",
converters=COORDINATES_CONVERTERS,
)
]

if asdf_coordinates_schemas.__version__ > "0.2.0":
_COORDINATES_MANIFEST_URIS.insert(
0,
"asdf://asdf-format.org/astronomy/coordinates/manifests/coordinates-1.1.0",
)

COORDINATES_EXTENSIONS = [
ManifestExtension.from_uri(
manifest_uri,
converters=COORDINATES_CONVERTERS,
)
for manifest_uri in _COORDINATES_MANIFEST_URIS
]


_ASTROPY_EXTENSION_MANIFEST_URIS = [
"asdf://astropy.org/astropy/manifests/astropy-1.2.0",
"asdf://astropy.org/astropy/manifests/astropy-1.1.0",
"asdf://astropy.org/astropy/manifests/astropy-1.0.0",
]
Expand All @@ -507,40 +533,52 @@
for manifest_uri in _ASTROPY_EXTENSION_MANIFEST_URIS
]

UNIT_CONVERTERS = [

# These tags are part of the ASDF Standard,
# but we want to override serialization here so that users can
# work with nice astropy objects for those entities.
_FITS_CONVERTERS = [
AsdfFitsConverter(),
]

_TIME_CONVERTERS = [
TimeConverter(),
]

_TABLE_CONVERTERS = [
ColumnConverter(),
AsdfTableConverter(),
]

_UNIT_CONVERTERS = [
UnitConverter(),
EquivalencyConverter(),
MagUnitConverter(),
QuantityConverter(),
]

CORE_CONVERTERS = _FITS_CONVERTERS + _TIME_CONVERTERS + _TABLE_CONVERTERS + _UNIT_CONVERTERS

UNIT_EXTENSIONS = [
ManifestExtension.from_uri(
"asdf://astropy.org/astropy/manifests/units-1.0.0",
converters=UNIT_CONVERTERS,
converters=_UNIT_CONVERTERS,
),
]

# These tags are part of the ASDF Standard,
# but we want to override serialization here so that users can
# work with nice astropy objects for those entities.

CORE_CONVERTERS = [
QuantityConverter(),
TimeConverter(),
ColumnConverter(),
AsdfTableConverter(),
AsdfFitsConverter(),
]

# up to asdf 1.5.0 many tags supported by asdf-astropy
# were defined in core manifests
CORE_MANIFEST_URIS = [
"asdf://asdf-format.org/core/manifests/core-1.0.0",
"asdf://asdf-format.org/core/manifests/core-1.1.0",
"asdf://asdf-format.org/core/manifests/core-1.2.0",
"asdf://asdf-format.org/core/manifests/core-1.3.0",
"asdf://asdf-format.org/core/manifests/core-1.4.0",
"asdf://asdf-format.org/core/manifests/core-1.5.0",
"asdf://asdf-format.org/core/manifests/core-1.6.0",
"asdf://asdf-format.org/core/manifests/core-1.4.0",
"asdf://asdf-format.org/core/manifests/core-1.3.0",
"asdf://asdf-format.org/core/manifests/core-1.2.0",
"asdf://asdf-format.org/core/manifests/core-1.1.0",
"asdf://asdf-format.org/core/manifests/core-1.0.0",
]
# TODO update this version check when the next asdf-standard release is made
if asdf_standard.__version__ > "1.0.3":
CORE_MANIFEST_URIS.insert(0, "asdf://asdf-format.org/astronomy/manifests/astronomy-1.0.0")


CORE_EXTENSIONS = [ManifestExtension.from_uri(u, converters=CORE_CONVERTERS) for u in CORE_MANIFEST_URIS]
4 changes: 2 additions & 2 deletions asdf_astropy/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def get_extensions():

return [
*extensions.ASTROPY_EXTENSIONS,
extensions.COORDINATES_EXTENSION,
*extensions.COORDINATES_EXTENSIONS,
*extensions.TRANSFORM_EXTENSIONS,
*extensions.CORE_EXTENSIONS,
*extensions.UNIT_EXTENSIONS,
*extensions.CORE_EXTENSIONS,
]
Loading
Loading