Skip to content

Commit

Permalink
Merge pull request #548 from larrybradley/fix-text-serialization
Browse files Browse the repository at this point in the history
Fix DS9 IO of Text regions
  • Loading branch information
larrybradley authored Apr 9, 2024
2 parents bc9a0e6 + 60d1594 commit 44d81b4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ New Features
Bug Fixes
---------

- Fixed a bug where the text string of ``TextPixelRegion`` and
``TextSkyRegion`` was dropped when serializing or writing
to a DS9 file. [#548]

API Changes
-----------

Expand Down
3 changes: 3 additions & 0 deletions regions/io/ds9/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ def _translate_metadata_to_ds9(region, shape):
Translate region metadata to valid ds9 meta keys.
"""
meta = {**region.meta, **region.visual}
# special case for Text regions
if 'text' in region._params:
meta = {'text': region.text, **meta}

if 'annulus' in shape:
# ds9 does not allow fill for annulus regions
Expand Down
6 changes: 4 additions & 2 deletions regions/io/ds9/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,10 +657,12 @@ def _make_region(region_data):

regions = []
for shape_params in region_params:
# for Text region, we need to add meta['text'] to params;
# set to '' if the text meta value was not specified
# for Text region, we need to add meta['text'] to params and
# remove it from meta; set to '' if the text meta value was not
# specified
if shape == 'text':
shape_params.append(region_data.raw_meta.get('text', ''))
meta.pop('text', None)

region = ds9_shape_to_region[region_type][shape](*shape_params)

Expand Down
19 changes: 18 additions & 1 deletion regions/io/ds9/tests/test_ds9.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from regions._utils.optional_deps import HAS_MATPLOTLIB
from regions.core import PixCoord, Regions, RegionVisual
from regions.shapes import (CirclePixelRegion, CircleSkyRegion,
PointPixelRegion, RegularPolygonPixelRegion)
PointPixelRegion, RegularPolygonPixelRegion,
TextPixelRegion)
from regions.tests.helpers import assert_region_allclose


Expand Down Expand Up @@ -53,6 +54,22 @@ def test_serialize():
assert actual == expected


def test_serialize_parse_text():
"""
Test serialization of Text region.
"""
text = 'Example Text'
region = TextPixelRegion(PixCoord(42, 43), text=text)
expected = ('# Region file format: DS9 astropy/regions\nglobal '
f'text={{{text}}}\nimage\ntext(43.0000,44.0000)\n')
actual = region.serialize(format='ds9', precision=4)
assert actual == expected

reg = Regions.parse(actual, format='ds9')[0]
assert reg.text == text
assert 'text' not in reg.meta


def test_parse():
"""
Simple test for parse.
Expand Down
2 changes: 1 addition & 1 deletion regions/io/ds9/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def _get_region_params(region, shape_template, precision=8):

param = {}
for param_name in region._params:
if param_name in ('text'):
if param_name in ('text',):
continue

value = getattr(region, param_name)
Expand Down

0 comments on commit 44d81b4

Please sign in to comment.