Skip to content

Commit 623f359

Browse files
committed
Combine load_ methods into single load method
1 parent b9891ac commit 623f359

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

115115
# Methods for loading data
116-
def load_fits(self, file: str | os.PathLike) -> None:
116+
def load(self, file: str | os.PathLike | ArrayLike | NDData) -> None:
117117
"""
118118
Load a FITS file into the viewer.
119119
@@ -123,6 +123,15 @@ def load_fits(self, file: str | os.PathLike) -> None:
123123
The FITS file to load. If a string, it can be a URL or a
124124
file path.
125125
"""
126+
if isinstance(file, (str, os.PathLike)):
127+
self._load_fits(file)
128+
elif isinstance(file, NDData):
129+
self._load_nddata(file)
130+
else:
131+
# Assume it is a 2D array
132+
self._load_array(file)
133+
134+
def _load_fits(self, file: str | os.PathLike) -> None:
126135
ccd = CCDData.read(file)
127136
self._wcs = ccd.wcs
128137
self.image_height, self.image_width = ccd.shape
@@ -131,7 +140,7 @@ def load_fits(self, file: str | os.PathLike) -> None:
131140
self.zoom_level = 1.0
132141
self.center_on((self.image_width / 2, self.image_height / 2))
133142

134-
def load_array(self, array: ArrayLike) -> None:
143+
def _load_array(self, array: ArrayLike) -> None:
135144
"""
136145
Load a 2D array into the viewer.
137146
@@ -147,7 +156,7 @@ def load_array(self, array: ArrayLike) -> None:
147156
self.center_on((self.image_width / 2, self.image_height / 2))
148157

149158

150-
def load_nddata(self, data: NDData) -> None:
159+
def _load_nddata(self, data: NDData) -> None:
151160
"""
152161
Load an `astropy.nddata.NDData` object into the viewer.
153162

src/astro_image_display_api/interface_definition.py

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

5353
# The methods, grouped loosely by purpose
5454

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

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)