Skip to content

Commit f3d50fe

Browse files
committed
Combine load_ methods into single load method
1 parent 65d5550 commit f3d50fe

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
@@ -125,7 +125,7 @@ def cursor(self, value: str) -> None:
125125
# The methods, grouped loosely by purpose
126126

127127
# Methods for loading data
128-
def load_fits(self, file: str | os.PathLike) -> None:
128+
def load(self, file: str | os.PathLike | ArrayLike | NDData) -> None:
129129
"""
130130
Load a FITS file into the viewer.
131131
@@ -135,6 +135,15 @@ def load_fits(self, file: str | os.PathLike) -> None:
135135
The FITS file to load. If a string, it can be a URL or a
136136
file path.
137137
"""
138+
if isinstance(file, (str, os.PathLike)):
139+
self._load_fits(file)
140+
elif isinstance(file, NDData):
141+
self._load_nddata(file)
142+
else:
143+
# Assume it is a 2D array
144+
self._load_array(file)
145+
146+
def _load_fits(self, file: str | os.PathLike) -> None:
138147
ccd = CCDData.read(file)
139148
self._wcs = ccd.wcs
140149
self.image_height, self.image_width = ccd.shape
@@ -143,7 +152,7 @@ def load_fits(self, file: str | os.PathLike) -> None:
143152
self.zoom_level = 1.0
144153
self.center_on((self.image_width / 2, self.image_height / 2))
145154

146-
def load_array(self, array: ArrayLike) -> None:
155+
def _load_array(self, array: ArrayLike) -> None:
147156
"""
148157
Load a 2D array into the viewer.
149158
@@ -159,7 +168,7 @@ def load_array(self, array: ArrayLike) -> None:
159168
self.center_on((self.image_width / 2, self.image_height / 2))
160169

161170

162-
def load_nddata(self, data: NDData) -> None:
171+
def _load_nddata(self, data: NDData) -> None:
163172
"""
164173
Load an `astropy.nddata.NDData` object into the viewer.
165174

src/astro_image_display_api/interface_definition.py

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

5858
# The methods, grouped loosely by purpose
5959

60-
# Methods for loading data
60+
# Method for loading data
6161
@abstractmethod
62-
def load_fits(self, file: str | os.PathLike) -> None:
62+
def load(self, data: Any) -> None:
6363
"""
64-
Load a FITS file into the viewer.
64+
Load data into the viewer. At a minimum, this should allow a FITS file
65+
to be loaded. Viewers may allow additional data types to be loaded, such as
66+
2D arrays or `astropy.nddata.NDData` objects.
6567
6668
Parameters
6769
----------
68-
file : str or `astropy.io.fits.HDU`
69-
The FITS file to load. If a string, it can be a URL or a
70-
file path.
71-
"""
72-
raise NotImplementedError
73-
74-
@abstractmethod
75-
def load_array(self, array: ArrayLike) -> None:
76-
"""
77-
Load a 2D array into the viewer.
78-
79-
Parameters
80-
----------
81-
array : array-like
82-
The array to load.
83-
"""
84-
raise NotImplementedError
85-
86-
@abstractmethod
87-
def load_nddata(self, data: NDData) -> None:
88-
"""
89-
Load an `astropy.nddata.NDData` object into the viewer.
90-
91-
Parameters
92-
----------
93-
data : `astropy.nddata.NDData`
94-
The NDData object to load.
70+
data : Any
71+
The data to load. This can be a FITS file, a 2D array,
72+
or an `astropy.nddata.NDData` object.
9573
"""
9674
raise NotImplementedError
9775

src/astro_image_display_api/widget_api_test.py

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

81-
def test_load_fits(self, data, tmp_path):
81+
def test_load(self, data, tmp_path):
8282
hdu = fits.PrimaryHDU(data=data)
8383
image_path = tmp_path / 'test.fits'
8484
hdu.header["BUNIT"] = "adu"
8585
hdu.writeto(image_path)
86-
self.image.load_fits(image_path)
87-
88-
def test_load_nddata(self, data):
89-
nddata = NDData(data)
90-
self.image.load_nddata(nddata)
91-
92-
def test_load_array(self, data):
93-
self.image.load_array(data)
86+
self.image.load(image_path)
9487

9588
def test_center_on(self):
9689
self.image.center_on((10, 10)) # X, Y
@@ -102,7 +95,8 @@ def test_offset_by(self, data, wcs):
10295
# have) taken care of setting up the WCS internally if initialized with
10396
# an NDData that has a WCS.
10497
ndd = NDData(data=data, wcs=wcs)
105-
self.image.load_nddata(ndd)
98+
# TODO: THIS WOULD FAIL ON A VIEWER THAT DOES NOT ALLOW NDDATA OR ARRAY LOADING
99+
self.image.load(ndd)
106100

107101
self.image.offset_by(10 * u.arcmin, 10 * u.arcmin)
108102

@@ -116,7 +110,8 @@ def test_offset_by(self, data, wcs):
116110

117111
def test_zoom_level(self, data):
118112
# Set data first, since that is needed to determine zoom level
119-
self.image.load_array(data)
113+
# TODO: THIS WOULD FAIL ON A VIEWER THAT DOES NOT ALLOW NDDATA OR ARRAY LOADING
114+
self.image.load(data)
120115
self.image.zoom_level = 5
121116
assert self.image.zoom_level == 5
122117

@@ -310,7 +305,8 @@ def test_remove_marker_accepts_list(self):
310305

311306
def test_adding_markers_as_world(self, data, wcs):
312307
ndd = NDData(data=data, wcs=wcs)
313-
self.image.load_nddata(ndd)
308+
# TODO: THIS WOULD FAIL ON A VIEWER THAT DOES NOT ALLOW NDDATA OR ARRAY LOADING
309+
self.image.load(ndd)
314310

315311
# Add markers using world coordinates
316312
pixels = np.linspace(0, 100, num=10).reshape(5, 2)
@@ -356,7 +352,8 @@ def test_cuts(self, data):
356352
assert 'histogram' in self.image.autocut_options
357353

358354
# Setting using histogram requires data
359-
self.image.load_array(data)
355+
# TODO: THIS WOULD FAIL ON A VIEWER THAT DOES NOT ALLOW NDDATA OR ARRAY LOADING
356+
self.image.load(data)
360357
self.image.cuts = 'histogram'
361358
assert len(self.image.cuts) == 2
362359

0 commit comments

Comments
 (0)