Skip to content

Commit 425ae4c

Browse files
authored
Merge pull request astropy#42 from mwcraig/unload-some-cruft
Combine load_ methods into single load method
2 parents 93b4e0e + 7f4b3f4 commit 425ae4c

File tree

3 files changed

+52
-53
lines changed

3 files changed

+52
-53
lines changed

src/astro_image_display_api/dummy_viewer.py

Lines changed: 25 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_image(self, file: str | os.PathLike | ArrayLike | NDData) -> None:
9393
"""
9494
Load a FITS file into the viewer.
9595
@@ -99,6 +99,22 @@ 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+
if isinstance(file, str):
104+
is_adsf = file.endswith(".asdf")
105+
else:
106+
is_asdf = file.suffix == ".asdf"
107+
if is_asdf:
108+
self._load_asdf(file)
109+
else:
110+
self._load_fits(file)
111+
elif isinstance(file, NDData):
112+
self._load_nddata(file)
113+
else:
114+
# Assume it is a 2D array
115+
self._load_array(file)
116+
117+
def _load_fits(self, file: str | os.PathLike) -> None:
102118
ccd = CCDData.read(file)
103119
self._wcs = ccd.wcs
104120
self.image_height, self.image_width = ccd.shape
@@ -107,7 +123,7 @@ def load_fits(self, file: str | os.PathLike) -> None:
107123
self.zoom_level = 1.0
108124
self.center_on((self.image_width / 2, self.image_height / 2))
109125

110-
def load_array(self, array: ArrayLike) -> None:
126+
def _load_array(self, array: ArrayLike) -> None:
111127
"""
112128
Load a 2D array into the viewer.
113129
@@ -123,7 +139,7 @@ def load_array(self, array: ArrayLike) -> None:
123139
self.center_on((self.image_width / 2, self.image_height / 2))
124140

125141

126-
def load_nddata(self, data: NDData) -> None:
142+
def _load_nddata(self, data: NDData) -> None:
127143
"""
128144
Load an `astropy.nddata.NDData` object into the viewer.
129145
@@ -140,6 +156,12 @@ def load_nddata(self, data: NDData) -> None:
140156
self.zoom_level = 1.0
141157
self.center_on((self.image_width / 2, self.image_height / 2))
142158

159+
def _load_asdf(self, asdf_file: str | os.PathLike) -> None:
160+
"""
161+
Not implementing some load types is fine.
162+
"""
163+
raise NotImplementedError("ASDF loading is not implemented in this dummy viewer.")
164+
143165
# Saving contents of the view and accessing the view
144166
def save(self, filename: str | os.PathLike, overwrite: bool = False) -> None:
145167
"""

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 image data
5353
@abstractmethod
54-
def load_fits(self, file: str | os.PathLike) -> None:
54+
def load_image(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: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# TODO: How to enable switching out backend and still run the same tests?
21

32
import pytest
43

@@ -76,19 +75,21 @@ def test_width_height(self):
7675
assert self.image.image_width == width
7776
assert self.image.image_height == height
7877

79-
def test_load_fits(self, data, tmp_path):
80-
hdu = fits.PrimaryHDU(data=data)
81-
image_path = tmp_path / 'test.fits'
82-
hdu.header["BUNIT"] = "adu"
83-
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)
78+
@pytest.mark.parametrize("load_type", ["fits", "nddata", "array"])
79+
def test_load(self, data, tmp_path, load_type):
80+
match load_type:
81+
case "fits":
82+
hdu = fits.PrimaryHDU(data=data)
83+
image_path = tmp_path / 'test.fits'
84+
hdu.header["BUNIT"] = "adu"
85+
hdu.writeto(image_path)
86+
load_arg = image_path
87+
case "nddata":
88+
load_arg = NDData(data=data)
89+
case "array":
90+
load_arg = data
91+
92+
self.image.load_image(load_arg)
9293

9394
def test_center_on(self):
9495
self.image.center_on((10, 10)) # X, Y
@@ -100,7 +101,7 @@ def test_offset_by(self, data, wcs):
100101
# have) taken care of setting up the WCS internally if initialized with
101102
# an NDData that has a WCS.
102103
ndd = NDData(data=data, wcs=wcs)
103-
self.image.load_nddata(ndd)
104+
self.image.load_image(ndd)
104105

105106
self.image.offset_by(10 * u.arcmin, 10 * u.arcmin)
106107

@@ -114,7 +115,7 @@ def test_offset_by(self, data, wcs):
114115

115116
def test_zoom_level(self, data):
116117
# Set data first, since that is needed to determine zoom level
117-
self.image.load_array(data)
118+
self.image.load_image(data)
118119
self.image.zoom_level = 5
119120
assert self.image.zoom_level == 5
120121

@@ -131,8 +132,6 @@ def test_marker_properties(self):
131132
for key in marker_style.keys():
132133
assert key in m_str
133134

134-
# TODO: add test that checks that retrieving markers with an unknown name issues no error
135-
136135
def test_add_markers(self):
137136
original_marker_name = self.image.DEFAULT_MARKER_NAME
138137
data = np.arange(10).reshape(5, 2)
@@ -248,7 +247,7 @@ def test_remove_marker_accepts_list(self):
248247

249248
def test_adding_markers_as_world(self, data, wcs):
250249
ndd = NDData(data=data, wcs=wcs)
251-
self.image.load_nddata(ndd)
250+
self.image.load_image(ndd)
252251

253252
# Add markers using world coordinates
254253
pixels = np.linspace(0, 100, num=10).reshape(5, 2)
@@ -294,7 +293,7 @@ def test_cuts(self, data):
294293
assert 'histogram' in self.image.autocut_options
295294

296295
# Setting using histogram requires data
297-
self.image.load_array(data)
296+
self.image.load_image(data)
298297
self.image.cuts = 'histogram'
299298
assert len(self.image.cuts) == 2
300299

0 commit comments

Comments
 (0)