From c9eff43b09c4f6d0280da8522a668dbe353ba1fb Mon Sep 17 00:00:00 2001 From: Xen0Xys Date: Mon, 13 May 2024 15:56:12 +0200 Subject: [PATCH] :sparkles: Mark add_overlay_from_stcs as deprecated Fix typing and optional regions dependency --- js/models/event_handler.js | 1 - js/models/message_handler.js | 17 ++++------------- pyproject.toml | 2 +- src/ipyaladin/aladin.py | 33 +++++++++++++++++++++++---------- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/js/models/event_handler.js b/js/models/event_handler.js index aa275ab2..590da955 100644 --- a/js/models/event_handler.js +++ b/js/models/event_handler.js @@ -176,7 +176,6 @@ export default class EventHandler { add_MOC_from_URL: this.messageHandler.handleAddMOCFromURL, add_MOC_from_dict: this.messageHandler.handleAddMOCFromDict, add_overlay: this.messageHandler.handleAddOverlay, - add_overlay_from_stcs: this.messageHandler.handleAddOverlayFromSTCS, change_colormap: this.messageHandler.handleChangeColormap, get_JPG_thumbnail: this.messageHandler.handleGetJPGThumbnail, trigger_rectangular_selection: diff --git a/js/models/message_handler.js b/js/models/message_handler.js index e3f37df4..205fc06a 100644 --- a/js/models/message_handler.js +++ b/js/models/message_handler.js @@ -33,34 +33,25 @@ export default class MessageHandler { const infos = msg["infos"]; const options = convertOptionNamesToCamelCase(msg["options"] || {}); const overlay = A.graphicOverlay(options); + this.aladin.addOverlay(overlay); switch (msg["region_type"]) { + case "stcs": + overlay.addFootprints(A.footprintsFromSTCS(infos.stcs)); + break; case "circle": - this.aladin.addOverlay(overlay); overlay.add(A.circle(infos.ra, infos.dec, infos.radius)); break; case "ellipse": - this.aladin.addOverlay(overlay); overlay.add( A.ellipse(infos.ra, infos.dec, infos.a, infos.b, infos.theta), ); break; case "line": - this.aladin.addOverlay(overlay); overlay.add(A.line(infos.ra1, infos.dec1, infos.ra2, infos.dec2)); break; } } - handleAddOverlayFromSTCS(msg) { - const overlayOptions = convertOptionNamesToCamelCase( - msg["overlay_options"] || {}, - ); - const stcString = msg["stc_string"]; - const overlay = A.graphicOverlay(overlayOptions); - this.aladin.addOverlay(overlay); - overlay.addFootprints(A.footprintsFromSTCS(stcString)); - } - handleChangeColormap(msg) { this.aladin.getBaseImageLayer().setColormap(msg["colormap"]); } diff --git a/pyproject.toml b/pyproject.toml index 0c301e02..97031efd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ requires-python = ">=3.8" [project.optional-dependencies] dev = ["pytest", "watchfiles", "jupyterlab", "ruff"] -recommended = ["mocpy"] +recommended = ["mocpy", "regions"] # automatically add the dev feature to the default env (e.g., hatch shell) [tool.hatch.envs.default] diff --git a/src/ipyaladin/aladin.py b/src/ipyaladin/aladin.py index f940eca8..c870e37c 100644 --- a/src/ipyaladin/aladin.py +++ b/src/ipyaladin/aladin.py @@ -15,7 +15,13 @@ from astropy.table import Table from astropy.coordinates import SkyCoord, Angle import traitlets -from regions import CircleSkyRegion, EllipseSkyRegion, LineSkyRegion + +try: + from regions import CircleSkyRegion, EllipseSkyRegion, LineSkyRegion +except ImportError: + CircleSkyRegion = None + EllipseSkyRegion = None + LineSkyRegion = None from traitlets import ( Float, Int, @@ -352,13 +358,22 @@ def add_overlay( overlay_options: keyword arguments """ + if not isinstance(region, str) and ( + CircleSkyRegion is None or EllipseSkyRegion is None or LineSkyRegion is None + ): + raise ValueError( + "A region can be given as an STC-S string or a regions " + "object. To read regions objects, you need to install the regions " + "library with 'pip install regions'." + ) + region_type = "" infos = {} if isinstance(region, str): - self.add_overlay_from_stcs(region, **overlay_options) - return - if isinstance(region, CircleSkyRegion): + region_type = "stcs" + infos = {"stcs": region} + elif isinstance(region, CircleSkyRegion): region_type = "circle" infos = { "ra": region.center.ra.deg, @@ -402,13 +417,11 @@ def add_overlay_from_stcs(self, stc_string: str, **overlay_options: any) -> None overlay_options: keyword arguments """ - self.send( - { - "event_name": "add_overlay_from_stcs", - "stc_string": stc_string, - "overlay_options": overlay_options, - } + # Add deprecation warning + warnings.warn( + "add_overlay_from_stcs is deprecated, use add_overlay instead", stacklevel=2 ) + self.add_overlay(stc_string, **overlay_options) def get_JPEG_thumbnail(self) -> None: """Create a popup window with the current Aladin view."""