From 385274f08b0c4b37d4d8e793b1dd320435ff7e23 Mon Sep 17 00:00:00 2001 From: Xen0Xys Date: Wed, 7 Aug 2024 14:53:22 +0200 Subject: [PATCH] :sparkles: Implement radial error and ellipse enclosed probability value --- js/models/message_handler.js | 2 ++ src/ipyaladin/widget.py | 62 ++++++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/js/models/message_handler.js b/js/models/message_handler.js index 8a6afa5..df6279b 100644 --- a/js/models/message_handler.js +++ b/js/models/message_handler.js @@ -152,6 +152,8 @@ export default class MessageHandler { s.data[errorDict["smin"]["col"]], s.data[errorDict["pa"]["col"]], ); + if (errorDict["r"] && s.data[errorDict["r"]["col"]]) + return A.circle(s.ra, s.dec, s.data[errorDict["r"]["col"]]); }; A.catalogFromURL( url, diff --git a/src/ipyaladin/widget.py b/src/ipyaladin/widget.py index 89ad615..d6533c2 100644 --- a/src/ipyaladin/widget.py +++ b/src/ipyaladin/widget.py @@ -5,6 +5,7 @@ It allows to display astronomical images and catalogs in an interactive way. """ +import math from collections.abc import Callable import io import pathlib @@ -20,7 +21,6 @@ from astropy.io import fits as astropy_fits from astropy.io.fits import HDUList import astropy.units as u -from astropy.units import Unit from astropy.wcs import WCS import numpy as np import traitlets @@ -636,13 +636,33 @@ def add_moc_from_dict( self.add_moc(moc_dict, **moc_options) def _convert_table_units( - self, table: Union[QTable, Table], error_dict: Dict, unit_to: Unit = u.deg + self, table: Union[QTable, Table], error_dict: Dict, unit_to: u.Unit = u.deg ) -> Union[QTable, Table]: + """Convert the units of a table according to the error_dict. + + Parameters + ---------- + table : astropy.table.table.QTable or astropy.table.table.Table + The table to convert. + error_dict : dict + The dictionary containing the error specifications. + unit_to : astropy.units.Unit + The unit to convert to. Default is degrees. + + Returns + ------- + astropy.table.table.QTable or astropy.table.table.Table + The table with the units converted. + + """ table = table.copy() for _, error_spec in error_dict.values(): + if not isinstance(error_spec, dict): + continue col_name = error_spec["col"] unit_from = error_spec["unit"] table[col_name].unit = unit_from + table[col_name] = table[col_name].astype(float) for row in table: if row[col_name] != "--" and not np.isnan(row[col_name]): row[col_name] = ( @@ -652,6 +672,35 @@ def _convert_table_units( return table + def _update_ellipse_enclosed_probability( + self, table: Union[QTable, Table], error_dict: Dict + ) -> Union[QTable, Table]: + """Update the table according to the ellipse_enclosed_probability. + + Parameters + ---------- + table : astropy.table.table.QTable or astropy.table.table.Table + The table to update. + error_dict : dict + The dictionary containing the error specifications. + + Returns + ------- + astropy.table.table.QTable or astropy.table.table.Table + The updated table. + + """ + table = table.copy() + r = math.sqrt(-2 * math.log(1 - error_dict["ellipse_enclosed_probability"])) + impacted_keys = {"smin", "smaj", "r"} + for key in impacted_keys: + if not error_dict.get(key): + continue + # Multiply table column by r + table[error_dict[key]["col"]] = table[error_dict[key]["col"]] * r + + return table + def add_table(self, table: Union[QTable, Table], **table_options: any) -> None: """Load a table into the widget. @@ -667,8 +716,15 @@ def add_table(self, table: Union[QTable, Table], **table_options: any) -> None: """ if table_options.get("error_dict"): table = self._convert_table_units(table, table_options["error_dict"]) - # remove unit sub-key for all the keys + # if dict contains ellipse_enclosed_probability, update the table + if table_options["error_dict"].get("ellipse_enclosed_probability"): + table = self._update_ellipse_enclosed_probability( + table, table_options["error_dict"] + ) + # Remove unit sub-key for all the keys for key in table_options["error_dict"]: + if key == "ellipse_enclosed_probability": + continue table_options["error_dict"][key].pop("unit") table_bytes = io.BytesIO() table.write(table_bytes, format="votable")