Skip to content

Commit d3c6fe7

Browse files
committed
Combine load_ methods into single load method
1 parent 93b4e0e commit d3c6fe7

File tree

3 files changed

+30
-46
lines changed

3 files changed

+30
-46
lines changed

src/astro_image_display_api/dummy_viewer.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def cursor(self, value: str) -> None:
8989
# The methods, grouped loosely by purpose
9090

9191
# Methods for loading data
92-
def load_fits(self, file: str | os.PathLike) -> None:
92+
def load(self, file: str | os.PathLike | ArrayLike | NDData) -> None:
9393
"""
9494
Load a FITS file into the viewer.
9595
@@ -99,6 +99,15 @@ def load_fits(self, file: str | os.PathLike) -> None:
9999
The FITS file to load. If a string, it can be a URL or a
100100
file path.
101101
"""
102+
if isinstance(file, (str, os.PathLike)):
103+
self._load_fits(file)
104+
elif isinstance(file, NDData):
105+
self._load_nddata(file)
106+
else:
107+
# Assume it is a 2D array
108+
self._load_array(file)
109+
110+
def _load_fits(self, file: str | os.PathLike) -> None:
102111
ccd = CCDData.read(file)
103112
self._wcs = ccd.wcs
104113
self.image_height, self.image_width = ccd.shape
@@ -107,7 +116,7 @@ def load_fits(self, file: str | os.PathLike) -> None:
107116
self.zoom_level = 1.0
108117
self.center_on((self.image_width / 2, self.image_height / 2))
109118

110-
def load_array(self, array: ArrayLike) -> None:
119+
def _load_array(self, array: ArrayLike) -> None:
111120
"""
112121
Load a 2D array into the viewer.
113122
@@ -123,7 +132,7 @@ def load_array(self, array: ArrayLike) -> None:
123132
self.center_on((self.image_width / 2, self.image_height / 2))
124133

125134

126-
def load_nddata(self, data: NDData) -> None:
135+
def _load_nddata(self, data: NDData) -> None:
127136
"""
128137
Load an `astropy.nddata.NDData` object into the viewer.
129138

src/astro_image_display_api/interface_definition.py

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,41 +49,19 @@ class ImageViewerInterface(Protocol):
4949

5050
# The methods, grouped loosely by purpose
5151

52-
# Methods for loading data
52+
# Method for loading data
5353
@abstractmethod
54-
def load_fits(self, file: str | os.PathLike) -> None:
54+
def load(self, data: Any) -> None:
5555
"""
56-
Load a FITS file into the viewer.
56+
Load data into the viewer. At a minimum, this should allow a FITS file
57+
to be loaded. Viewers may allow additional data types to be loaded, such as
58+
2D arrays or `astropy.nddata.NDData` objects.
5759
5860
Parameters
5961
----------
60-
file : str or `astropy.io.fits.HDU`
61-
The FITS file to load. If a string, it can be a URL or a
62-
file path.
63-
"""
64-
raise NotImplementedError
65-
66-
@abstractmethod
67-
def load_array(self, array: ArrayLike) -> None:
68-
"""
69-
Load a 2D array into the viewer.
70-
71-
Parameters
72-
----------
73-
array : array-like
74-
The array to load.
75-
"""
76-
raise NotImplementedError
77-
78-
@abstractmethod
79-
def load_nddata(self, data: NDData) -> None:
80-
"""
81-
Load an `astropy.nddata.NDData` object into the viewer.
82-
83-
Parameters
84-
----------
85-
data : `astropy.nddata.NDData`
86-
The NDData object to load.
62+
data : Any
63+
The data to load. This can be a FITS file, a 2D array,
64+
or an `astropy.nddata.NDData` object.
8765
"""
8866
raise NotImplementedError
8967

src/astro_image_display_api/widget_api_test.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,12 @@ def test_width_height(self):
7676
assert self.image.image_width == width
7777
assert self.image.image_height == height
7878

79-
def test_load_fits(self, data, tmp_path):
79+
def test_load(self, data, tmp_path):
8080
hdu = fits.PrimaryHDU(data=data)
8181
image_path = tmp_path / 'test.fits'
8282
hdu.header["BUNIT"] = "adu"
8383
hdu.writeto(image_path)
84-
self.image.load_fits(image_path)
85-
86-
def test_load_nddata(self, data):
87-
nddata = NDData(data)
88-
self.image.load_nddata(nddata)
89-
90-
def test_load_array(self, data):
91-
self.image.load_array(data)
84+
self.image.load(image_path)
9285

9386
def test_center_on(self):
9487
self.image.center_on((10, 10)) # X, Y
@@ -100,7 +93,8 @@ def test_offset_by(self, data, wcs):
10093
# have) taken care of setting up the WCS internally if initialized with
10194
# an NDData that has a WCS.
10295
ndd = NDData(data=data, wcs=wcs)
103-
self.image.load_nddata(ndd)
96+
# TODO: THIS WOULD FAIL ON A VIEWER THAT DOES NOT ALLOW NDDATA OR ARRAY LOADING
97+
self.image.load(ndd)
10498

10599
self.image.offset_by(10 * u.arcmin, 10 * u.arcmin)
106100

@@ -114,7 +108,8 @@ def test_offset_by(self, data, wcs):
114108

115109
def test_zoom_level(self, data):
116110
# Set data first, since that is needed to determine zoom level
117-
self.image.load_array(data)
111+
# TODO: THIS WOULD FAIL ON A VIEWER THAT DOES NOT ALLOW NDDATA OR ARRAY LOADING
112+
self.image.load(data)
118113
self.image.zoom_level = 5
119114
assert self.image.zoom_level == 5
120115

@@ -248,7 +243,8 @@ def test_remove_marker_accepts_list(self):
248243

249244
def test_adding_markers_as_world(self, data, wcs):
250245
ndd = NDData(data=data, wcs=wcs)
251-
self.image.load_nddata(ndd)
246+
# TODO: THIS WOULD FAIL ON A VIEWER THAT DOES NOT ALLOW NDDATA OR ARRAY LOADING
247+
self.image.load(ndd)
252248

253249
# Add markers using world coordinates
254250
pixels = np.linspace(0, 100, num=10).reshape(5, 2)
@@ -294,7 +290,8 @@ def test_cuts(self, data):
294290
assert 'histogram' in self.image.autocut_options
295291

296292
# Setting using histogram requires data
297-
self.image.load_array(data)
293+
# TODO: THIS WOULD FAIL ON A VIEWER THAT DOES NOT ALLOW NDDATA OR ARRAY LOADING
294+
self.image.load(data)
298295
self.image.cuts = 'histogram'
299296
assert len(self.image.cuts) == 2
300297

0 commit comments

Comments
 (0)