1
1
# TODO: How to enable switching out backend and still run the same tests?
2
+ import warnings
2
3
3
4
import pytest
4
5
@@ -46,6 +47,11 @@ class variable does the trick.
46
47
"""
47
48
self .image = self .image_widget_class (image_width = 250 , image_height = 100 )
48
49
50
+ def _check_marker_table_return_properties (self , table ):
51
+ assert isinstance (table , Table )
52
+ assert len (table ) == 0
53
+ assert sorted (table .colnames ) == sorted (['x' , 'y' , 'coord' , 'marker name' ])
54
+
49
55
def test_width_height (self ):
50
56
assert self .image .image_width == 250
51
57
assert self .image .image_height == 100
@@ -103,7 +109,7 @@ def test_zoom(self):
103
109
104
110
def test_marking_operations (self ):
105
111
marks = self .image .get_markers (marker_name = "all" )
106
- assert marks is None
112
+ self . _check_marker_table_return_properties ( marks )
107
113
assert not self .image .is_marking
108
114
109
115
# Ensure you cannot set it like this.
@@ -138,12 +144,13 @@ def test_marking_operations(self):
138
144
assert not self .image .click_drag
139
145
assert not self .image .scroll_pan
140
146
141
- # Regression test for GitHub Issue 97:
142
- # Marker name with no markers should give warning.
143
- with pytest .warns (UserWarning , match = 'is empty' ) as warning_lines :
147
+ # Make sure no warning is issued when trying to retrieve markers
148
+ # with a name that does not exist.
149
+ with warnings .catch_warnings ():
150
+ warnings .simplefilter ("error" )
144
151
t = self .image .get_markers (marker_name = 'markymark' )
145
- assert t is None
146
- assert len ( warning_lines ) == 1
152
+
153
+ self . _check_marker_table_return_properties ( t )
147
154
148
155
self .image .click_drag = True
149
156
self .image .start_marking ()
@@ -160,7 +167,7 @@ def test_marking_operations(self):
160
167
self .image .stop_marking (clear_markers = True )
161
168
162
169
assert self .image .is_marking is False
163
- assert self .image .get_markers (marker_name = "all" ) is None
170
+ self ._check_marker_table_return_properties ( self . image .get_markers (marker_name = "all" ))
164
171
165
172
# Hate this, should add to public API
166
173
marknames = self .image ._marktags
@@ -170,8 +177,7 @@ def test_marking_operations(self):
170
177
assert self .image .click_drag
171
178
172
179
def test_add_markers (self ):
173
- rng = np .random .default_rng (1234 )
174
- data = rng .integers (0 , 100 , (5 , 2 ))
180
+ data = np .arange (10 ).reshape (5 , 2 )
175
181
orig_tab = Table (data = data , names = ['x' , 'y' ], dtype = ('float' , 'float' ))
176
182
tab = Table (data = data , names = ['x' , 'y' ], dtype = ('float' , 'float' ))
177
183
self .image .add_markers (tab , x_colname = 'x' , y_colname = 'y' ,
@@ -234,35 +240,73 @@ def test_add_markers(self):
234
240
self .image .reset_markers ()
235
241
marknames = self .image ._marktags
236
242
assert len (marknames ) == 0
237
- assert self .image .get_markers (marker_name = "all" ) is None
238
- with pytest .warns (UserWarning , match = 'is empty' ):
239
- assert self .image .get_markers (marker_name = self .image ._default_mark_tag_name ) is None
243
+ self ._check_marker_table_return_properties (self .image .get_markers (marker_name = "all" ))
244
+ # Check that no markers remain after clearing
245
+ tab = self .image .get_markers (marker_name = self .image ._default_mark_tag_name )
246
+ self ._check_marker_table_return_properties (tab )
247
+
248
+ # Check that retrieving a marker set that doesn't exist returns
249
+ # an empty table with the right columns
250
+ tab = self .image .get_markers (marker_name = 'test1' )
251
+ self ._check_marker_table_return_properties (tab )
252
+
253
+ def test_get_markers_accepts_list_of_names (self ):
254
+ # Check that the get_markers method accepts a list of marker names
255
+ # and returns a table with all the markers from all the named sets.
256
+ data = np .arange (10 ).reshape ((5 , 2 ))
257
+ tab = Table (data = data , names = ['x' , 'y' ])
258
+ self .image .add_markers (tab , marker_name = 'test1' )
259
+ self .image .add_markers (tab , marker_name = 'test2' )
240
260
241
- with pytest .raises (ValueError , match = "No markers named 'test1'" ):
242
- self .image .get_markers (marker_name = 'test1' )
243
- with pytest .raises (ValueError , match = "No markers named 'test2'" ):
244
- self .image .get_markers (marker_name = 'test2' )
261
+ # No guarantee markers will come back in the same order, so sort them.
262
+ t1 = self .image .get_markers (marker_name = ['test1' , 'test2' ])
263
+ # Sort before comparing
264
+ t1 .sort ('x' )
265
+ expected = vstack ([tab , tab ], join_type = 'exact' )
266
+ expected .sort ('x' )
267
+ np .testing .assert_array_equal (t1 ['x' ], expected ['x' ])
268
+ np .testing .assert_array_equal (t1 ['y' ], expected ['y' ])
245
269
246
270
def test_remove_markers (self ):
247
271
with pytest .raises (ValueError , match = 'arf' ):
248
272
self .image .remove_markers (marker_name = 'arf' )
249
273
274
+ def test_remove_markers_name_all (self ):
275
+ data = np .arange (10 ).reshape (5 , 2 )
276
+ tab = Table (data = data , names = ['x' , 'y' ])
277
+ self .image .add_markers (tab , marker_name = 'test1' )
278
+ self .image .add_markers (tab , marker_name = 'test2' )
279
+
280
+ self .image .remove_markers (marker_name = 'all' )
281
+ self ._check_marker_table_return_properties (self .image .get_markers (marker_name = 'all' ))
282
+
283
+ def test_remove_marker_accepts_list (self ):
284
+ data = np .arange (10 ).reshape (5 , 2 )
285
+ tab = Table (data = data , names = ['x' , 'y' ])
286
+ self .image .add_markers (tab , marker_name = 'test1' )
287
+ self .image .add_markers (tab , marker_name = 'test2' )
288
+
289
+ self .image .remove_markers (marker_name = ['test1' , 'test2' ])
290
+ marks = self .image .get_markers (marker_name = 'all' )
291
+ self ._check_marker_table_return_properties (marks )
292
+
250
293
def test_adding_markers_as_world (self , data , wcs ):
251
294
ndd = NDData (data = data , wcs = wcs )
252
295
self .image .load_nddata (ndd )
253
296
254
297
# Add markers using world coordinates
255
- rng = np .random .default_rng (9435 )
256
-
257
- pixels = rng .integers (0 , 100 , (5 , 2 ))
298
+ pixels = np .linspace (0 , 100 , num = 10 ).reshape (5 , 2 )
258
299
marks_pix = Table (data = pixels , names = ['x' , 'y' ], dtype = ('float' , 'float' ))
259
300
marks_world = wcs .pixel_to_world (marks_pix ['x' ], marks_pix ['y' ])
260
301
marks_coords = SkyCoord (marks_world , unit = 'degree' )
261
302
mark_coord_table = Table (data = [marks_coords ], names = ['coord' ])
262
303
self .image .add_markers (mark_coord_table , use_skycoord = True )
263
304
result = self .image .get_markers ()
264
305
# Check the x, y positions as long as we are testing things...
265
- np .testing .assert_allclose (result ['x' ], marks_pix ['x' ])
306
+ # The first test had one entry that was zero, so any check
307
+ # based on rtol will will. Added a small atol to make sure
308
+ # the test passes.
309
+ np .testing .assert_allclose (result ['x' ], marks_pix ['x' ], atol = 1e-9 )
266
310
np .testing .assert_allclose (result ['y' ], marks_pix ['y' ])
267
311
np .testing .assert_allclose (result ['coord' ].ra .deg ,
268
312
mark_coord_table ['coord' ].ra .deg )
0 commit comments