glue-viz / glue

Linked Data Visualizations Across Multiple Files
http://glueviz.org
Other
742 stars 153 forks source link

New CircularAnnulusROI class to represent circular annulus ROI #2403

Closed pllim closed 1 year ago

pllim commented 1 year ago

This PR adds a new class to represent circular annulus as ROI.

Motivation: Even though we can use annulus now after https://github.com/glue-viz/glue-astronomy/pull/90 , it is not very user-friendly, as we have to expose parameters from the outer and inner circles separately, which would cause user to easily make the subset state no longer a circular annulus (see https://github.com/spacetelescope/jdaviz/pull/2182#issuecomment-1542882590 for exciting screenshots). Therefore, there is a need to represent this shape as ROI so that we only expose the relevant parameters (one center only, inner radius, outer radius) without a lot of hacky workaround downstream.

With this patch, I could do the following in Jdaviz after unpinning maxversion of glue-core (xref https://github.com/spacetelescope/jdaviz/issues/2183).

--- a/pyproject.toml
+++ b/pyproject.toml
@@ -12,7 +12,7 @@ dependencies = [
     "traitlets>=5.0.5",
     "bqplot>=0.12.37",
     "bqplot-image-gl>=1.4.11",
-    "glue-core>=1.6.0,!=1.9.0,<1.10",
+    "glue-core>=1.6.0,!=1.9.0",
     "glue-jupyter>=0.15.0",
     "echo>=0.5.0",
     "ipykernel>=6.19.4",
import numpy as np
from glue.core.roi import CircularAnnulusROI
from glue.core.subset import RoiSubsetState

from jdaviz import Imviz

a = np.arange(100).reshape((10, 10))

imviz = Imviz()
imviz.load_data(a, data_label='a')
imviz.show()

data = imviz.app.data_collection[0]
ann = CircularAnnulusROI(xc=4.5, yc=4.5, inner_radius=2, outer_radius=4)
sbst = RoiSubsetState(xatt=data.pixel_component_ids[1], yatt=data.pixel_component_ids[0], roi=ann)
imviz.app.data_collection.new_subset_group(subset_state=sbst)

Screenshot 2023-05-11 162530

The next step following this PR is to support this new ROI both in glue-astronomy (https://github.com/glue-viz/glue-astronomy/pull/92) and Jdaviz.

🐱

pllim commented 1 year ago

Using the example posted above, this is what the polygon looks like:

from matplotlib import pyplot as plt

x, y = ann.to_polygon()

plt.gca().set_aspect('equal')
plt.plot(x, y)

annulus_poly

astrofrog commented 1 year ago

Great! Maybe we should have 100 points instead of 20?

pllim commented 1 year ago

Done!

ann_poly_100

pllim commented 1 year ago

Not sure why CI didn't kick off.