Skip to content

Commit c5b86dc

Browse files
authored
Merge pull request #79 from mwcraig/minor-api-cleanup
Minor api cleanup
2 parents ab66f01 + 711b664 commit c5b86dc

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

src/astro_image_display_api/api_test.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ def _assert_empty_catalog_table(self, table):
7979
assert len(table) == 0
8080
assert sorted(table.colnames) == sorted(["x", "y", "coord"])
8181

82-
def _get_catalog_names_as_set(self):
83-
marks = self.image.catalog_names
82+
def _get_catalog_labels_as_set(self):
83+
marks = self.image.catalog_labels
8484
return set(marks)
8585

8686
@pytest.mark.parametrize("load_type", ["fits", "nddata", "array"])
@@ -362,7 +362,7 @@ def test_set_catalog_style_before_catalog_data_raises_error(self):
362362
with pytest.raises(
363363
ValueError, match="Must load a catalog before setting a catalog style"
364364
):
365-
self.image.set_catalog_style(color="red", shape="x", size=10)
365+
self.image.set_catalog_style(color="red", shape="circle", size=10)
366366

367367
def test_set_get_catalog_style_no_labels(self, catalog):
368368
# Check that getting without setting returns a dict that contains
@@ -376,7 +376,7 @@ def test_set_get_catalog_style_no_labels(self, catalog):
376376
# Add some data before setting a style
377377
self.image.load_catalog(catalog)
378378
# Check that setting a marker style works
379-
marker_settings = dict(color="red", shape="x", size=10)
379+
marker_settings = dict(color="red", shape="crosshair", size=10)
380380
self.image.set_catalog_style(**marker_settings.copy())
381381

382382
retrieved_style = self.image.get_catalog_style()
@@ -421,7 +421,7 @@ def test_set_get_catalog_style_preserves_extra_keywords(self, catalog):
421421
# The only required keywords are color, shape, and size.
422422
# Add some extra keyword to the style.
423423
style = dict(
424-
color="blue", shape="x", size=10, extra_kw="extra_value", alpha=0.5
424+
color="blue", shape="circle", size=10, extra_kw="extra_value", alpha=0.5
425425
)
426426
self.image.set_catalog_style(**style.copy())
427427

@@ -489,7 +489,7 @@ def test_load_multiple_catalogs(self, catalog):
489489
catalog_label="test2",
490490
)
491491

492-
assert sorted(self.image.catalog_names) == ["test1", "test2"]
492+
assert sorted(self.image.catalog_labels) == ["test1", "test2"]
493493

494494
# No guarantee markers will come back in the same order, so sort them.
495495
t1 = self.image.get_catalog(catalog_label="test1")
@@ -513,7 +513,7 @@ def test_load_multiple_catalogs(self, catalog):
513513
# other one without a label.
514514
self.image.remove_catalog(catalog_label="test1")
515515
# Make sure test1 is really gone.
516-
assert self.image.catalog_names == ("test2",)
516+
assert self.image.catalog_labels == ("test2",)
517517

518518
# Get without a catalog
519519
t2 = self.image.get_catalog()
@@ -534,7 +534,7 @@ def test_load_catalog_multiple_same_label(self, catalog):
534534
self.image.load_catalog(catalog, catalog_label="test1")
535535

536536
retrieved_catalog = self.image.get_catalog(catalog_label="test1")
537-
assert len(retrieved_catalog) == 2 * len(catalog)
537+
assert len(retrieved_catalog) == len(catalog)
538538

539539
def test_load_catalog_with_skycoord_no_wcs(self, catalog, data):
540540
# Check that loading a catalog with skycoord but no x/y and
@@ -543,13 +543,10 @@ def test_load_catalog_with_skycoord_no_wcs(self, catalog, data):
543543

544544
# Remove x/y columns from the catalog
545545
del catalog["x", "y"]
546-
self.image.load_catalog(catalog)
547-
# Retrieve the catalog and check that the x and y columns are None
548-
retrieved_catalog = self.image.get_catalog()
549-
assert "x" in retrieved_catalog.colnames
550-
assert "y" in retrieved_catalog.colnames
551-
assert all(rc is None for rc in retrieved_catalog["x"])
552-
assert all(rc is None for rc in retrieved_catalog["y"])
546+
with pytest.raises(
547+
ValueError, match="Cannot use pixel coordinates without pixel columns"
548+
):
549+
self.image.load_catalog(catalog)
553550

554551
def test_load_catalog_with_use_skycoord_no_skycoord_no_wcs(self, catalog, data):
555552
# Check that loading a catalog with use_skycoord=True but no
@@ -605,7 +602,7 @@ def test_load_catalog_with_no_style_has_a_style(self, catalog):
605602
def test_load_catalog_with_style_sets_style(self, catalog):
606603
# Check that loading a catalog with a style sets the style
607604
# for that catalog.
608-
style = dict(color="blue", shape="x", size=10)
605+
style = dict(color="blue", shape="square", size=10)
609606
self.image.load_catalog(
610607
catalog, catalog_label="test1", catalog_style=style.copy()
611608
)

src/astro_image_display_api/image_viewer_logic.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from astropy import units as u
1010
from astropy.coordinates import SkyCoord
1111
from astropy.nddata import CCDData, NDData
12-
from astropy.table import Table, vstack
12+
from astropy.table import Table
1313
from astropy.units import Quantity
1414
from astropy.visualization import (
1515
AsymmetricPercentileInterval,
@@ -492,9 +492,16 @@ def load_catalog(
492492
x, y = self._wcs.world_to_pixel(coords)
493493
to_add[x_colname] = x
494494
to_add[y_colname] = y
495+
xy = (x, y)
495496
else:
496497
to_add[x_colname] = to_add[y_colname] = None
497498

499+
if not use_skycoord and xy is None:
500+
raise ValueError(
501+
"Cannot use pixel coordinates without pixel columns or both "
502+
"coordinates and a WCS."
503+
)
504+
498505
if coords is None:
499506
if use_skycoord and self._wcs is None:
500507
raise ValueError(
@@ -509,17 +516,8 @@ def load_catalog(
509516

510517
catalog_label = self._resolve_catalog_label(catalog_label)
511518

512-
# Either set new data or append to existing data
513-
if (
514-
catalog_label in self._catalogs
515-
and self._catalogs[catalog_label].data is not None
516-
):
517-
# If the catalog already exists, we append to it
518-
old_table = self._catalogs[catalog_label].data
519-
self._catalogs[catalog_label].data = vstack([old_table, to_add])
520-
else:
521-
# If the catalog does not exist, we create a new one
522-
self._catalogs[catalog_label].data = to_add
519+
# Set the new data
520+
self._catalogs[catalog_label].data = to_add
523521

524522
# Ensure a catalog always has a style
525523
if catalog_style is None:
@@ -590,7 +588,7 @@ def get_catalog(
590588
return result
591589

592590
@property
593-
def catalog_names(self) -> tuple[str, ...]:
591+
def catalog_labels(self) -> tuple[str, ...]:
594592
return tuple(self._user_catalog_labels())
595593

596594
# Methods that modify the view
@@ -712,13 +710,15 @@ def get_viewport(
712710
"sky coordinates."
713711
)
714712
else:
715-
center = viewport.wcs.pixel_to_world(
716-
viewport.center[0], viewport.center[1]
717-
)
718-
pixel_scale = proj_plane_pixel_scales(viewport.wcs)[
719-
viewport.largest_dimension
720-
]
721-
fov = pixel_scale * viewport.fov * u.degree
713+
if center is None:
714+
center = viewport.wcs.pixel_to_world(
715+
viewport.center[0], viewport.center[1]
716+
)
717+
if fov is None:
718+
pixel_scale = proj_plane_pixel_scales(viewport.wcs)[
719+
viewport.largest_dimension
720+
]
721+
fov = pixel_scale * viewport.fov * u.degree
722722
else:
723723
# Pixel coordinates
724724
if isinstance(viewport.center, SkyCoord):

src/astro_image_display_api/interface_definition.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ def load_catalog(
345345
"""
346346
Add catalog entries to the viewer at positions given by the catalog.
347347
348+
Loading a catalog using a ``catalog_label`` that already exists will
349+
overwrite the existing catalog with the new one.
350+
348351
Parameters
349352
----------
350353
table : `astropy.table.Table`
@@ -543,7 +546,7 @@ def get_catalog(
543546

544547
@property
545548
@abstractmethod
546-
def catalog_names(self) -> tuple[str, ...]:
549+
def catalog_labels(self) -> tuple[str, ...]:
547550
"""
548551
Names of the loaded catalogs.
549552

0 commit comments

Comments
 (0)