Skip to content

Commit

Permalink
Add visual tests for flipping WCS
Browse files Browse the repository at this point in the history
  • Loading branch information
jfoster17 committed Mar 20, 2024
1 parent 4bc9f0a commit 4475ffb
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 11 deletions.
4 changes: 3 additions & 1 deletion glue/tests/visual/py311-test-visual.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
"glue.viewers.image.tests.test_viewer.test_region_layer_flip": "a142142f34961aba7e98188ad43abafe0e6e5b82e13e8cdab5131d297ed5832c",
"glue.viewers.profile.tests.test_viewer.test_simple_viewer": "f68a21be5080fec513388b2d2b220512e7b0df5498e2489da54e58708de435b3",
"glue.viewers.scatter.tests.test_viewer.test_simple_viewer": "1020a7bd3abe40510b9e03047c3b423b75c3c64ac18e6dcd6257173cec1ed53f",
"glue.viewers.scatter.tests.test_viewer.test_scatter_density_map": "3379d655262769a6ccbdbaf1970bffa9237adbec23a93d3ab75da51b9a3e7f8b"
"glue.viewers.scatter.tests.test_viewer.test_scatter_density_map": "3379d655262769a6ccbdbaf1970bffa9237adbec23a93d3ab75da51b9a3e7f8b",
"glue.viewers.image.tests.test_viewer.TestWCSRegionDisplay.test_wcs_viewer": "651e0d954c6b77becb7064de24f5101b9f2882adabc1d5aedbc183b9762c59b1",
"glue.viewers.image.tests.test_viewer.TestWCSRegionDisplay.test_flipped_wcs_viewer": "fcc18a1398d1f3f61b6989a722402cde9365a34483eedf8eac222647e20eb8ab"
}
101 changes: 91 additions & 10 deletions glue/viewers/image/tests/test_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,82 @@ def test_region_layer():
polygons = MultiPolygon([poly_3, poly_4])

geoms = np.array([poly_1, poly_2, polygons])
values = np.array([1, 2, 3])
region_data = RegionData(regions=geoms, values=values)
a_values = np.array([1, 2, 3])
b_values = np.array([1, 2, 3])

region_data = RegionData(regions=geoms, a=a_values, b=b_values)

image_data = Data(x=np.arange(10000).reshape((100, 100)), label='data1')
app = Application()
app.data_collection.append(image_data)
app.data_collection.append(region_data)

viewer = app.new_data_viewer(SimpleImageViewer)
viewer.add_data(image_data)
viewer.add_data(region_data)

# Link to region data components that are the not the x,y coordinates
link1 = LinkSame(region_data.id['a'], image_data.pixel_component_ids[0])
link2 = LinkSame(region_data.id['b'], image_data.pixel_component_ids[1])
app.data_collection.add_link(link1)
app.data_collection.add_link(link2)

app.data_collection.remove_link(link1)
app.data_collection.remove_link(link2)

link1 = LinkSame(region_data.center_x_id, image_data.pixel_component_ids[0])
link2 = LinkSame(region_data.center_y_id, image_data.pixel_component_ids[1])
app.data_collection.add_link(link1)
app.data_collection.add_link(link2)

return viewer.figure


def test_region_layer_logic():
poly_1 = Polygon([(20, 20), (60, 20), (60, 40), (20, 40)])
poly_2 = Polygon([(60, 50), (60, 70), (80, 70), (80, 50)])
poly_3 = Polygon([(10, 10), (15, 10), (15, 15), (10, 15)])
poly_4 = Polygon([(10, 20), (15, 20), (15, 30), (10, 30), (12, 25)])

polygons = MultiPolygon([poly_3, poly_4])

geoms = np.array([poly_1, poly_2, polygons])
a_values = np.array([1, 2, 3])
b_values = np.array([1, 2, 3])

region_data = RegionData(regions=geoms, a=a_values, b=b_values)

image_data = Data(x=np.arange(10000).reshape((100, 100)), label='data1')
app = Application()
app.data_collection.append(image_data)
app.data_collection.append(region_data)

viewer = app.new_data_viewer(SimpleImageViewer)
viewer.add_data(image_data)
viewer.add_data(region_data)

return viewer.figure
assert viewer.layers[0].enabled # image
assert not viewer.layers[1].enabled # regions

# Link to region data components that are the not the x,y coordinates
link1 = LinkSame(region_data.id['a'], image_data.pixel_component_ids[0])
link2 = LinkSame(region_data.id['b'], image_data.pixel_component_ids[1])
app.data_collection.add_link(link1)
app.data_collection.add_link(link2)

assert viewer.layers[0].enabled # image
assert not viewer.layers[1].enabled # regions

app.data_collection.remove_link(link1)
app.data_collection.remove_link(link2)

link1 = LinkSame(region_data.center_x_id, image_data.pixel_component_ids[0])
link2 = LinkSame(region_data.center_y_id, image_data.pixel_component_ids[1])
app.data_collection.add_link(link1)
app.data_collection.add_link(link2)

assert viewer.layers[0].enabled # image
assert viewer.layers[1].enabled # regions


@visual_test
Expand Down Expand Up @@ -106,26 +164,47 @@ def setup_method(self, method):

wcs1 = WCS(naxis=2)
wcs1.wcs.ctype = 'RA---TAN', 'DEC--TAN'
wcs1.wcs.crpix = -3, 5
wcs1.wcs.crpix = 15, 15
wcs1.wcs.cd = [[2, -1], [1, 2]]

wcs1.wcs.set()

self.image1 = Data(label='image1', a=[[3, 3], [2, 2]], b=[[4, 4], [3, 2]],
coords=wcs1)
SHAPELY_CIRCLE_ARRAY = np.array([Point(1.5, 2.5).buffer(1), Polygon([(1, 1), (2, 2), (2, 3), (1, 3)])])
np.random.seed(2)
self.image1 = Data(label='image1', a=np.random.rand(30, 30), coords=wcs1)
SHAPELY_ARRAY = np.array([Point(1.5, 2.5).buffer(4),
Polygon([(10, 10), (10, 15), (20, 15), (20, 10)])])
self.region_data = RegionData(label='My Regions',
color=np.array(['red', 'blue']),
area=shapely.area(SHAPELY_CIRCLE_ARRAY),
boundary=SHAPELY_CIRCLE_ARRAY)
area=shapely.area(SHAPELY_ARRAY),
boundary=SHAPELY_ARRAY)
self.application = Application()

self.application.data_collection.append(self.image1)
self.application.data_collection.append(self.region_data)

self.viewer = self.application.new_data_viewer(SimpleImageViewer)

def test_flipped_viewer(self):
@visual_test
def test_wcs_viewer(self):
self.viewer.add_data(self.image1)

link1 = LinkSame(self.region_data.center_x_id, self.image1.world_component_ids[1])
link2 = LinkSame(self.region_data.center_y_id, self.image1.world_component_ids[0])

self.application.data_collection.add_link(link1)
self.application.data_collection.add_link(link2)

self.viewer.add_data(self.region_data)

assert self.viewer.state._display_world is True
assert len(self.viewer.state.layers) == 2
assert self.viewer.layers[0].enabled
assert self.viewer.layers[1].enabled

return self.viewer.figure

@visual_test
def test_flipped_wcs_viewer(self):
self.viewer.add_data(self.image1)

link1 = LinkSame(self.region_data.center_x_id, self.image1.world_component_ids[1])
Expand All @@ -152,3 +231,5 @@ def test_flipped_viewer(self):

# Because we have flipped the viewer, the patches should have changed
assert np.array_equal(original_path_patch, np.flip(new_path_patch, axis=1))

return self.viewer.figure

0 comments on commit 4475ffb

Please sign in to comment.