Skip to content

Commit

Permalink
✨ Implement radial error and ellipse enclosed probability value
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed Aug 7, 2024
1 parent 84ff138 commit 385274f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
2 changes: 2 additions & 0 deletions js/models/message_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
62 changes: 59 additions & 3 deletions src/ipyaladin/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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] = (
Expand All @@ -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.
Expand All @@ -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")
Expand Down

0 comments on commit 385274f

Please sign in to comment.