Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 171 additions & 0 deletions examples/13_Programatic_Region_Selection.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "fb39c230",
"metadata": {},
"source": [
"# Programatic Region Selection\n",
"This notebook shows how you can select all of the sources within an astropy region as a selection, export the selection region, and draw a graphic overlay of the exported region."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72180ae5-f9ec-4355-9497-200439da187b",
"metadata": {},
"outputs": [],
"source": [
"from ipyaladin import Aladin\n",
"\n",
"from astropy.coordinates import SkyCoord\n",
"from astroquery.mast import Catalogs\n",
"\n",
"from regions import CircleSkyRegion\n",
"from astropy import units as u"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "43c0c170",
"metadata": {},
"outputs": [],
"source": [
"aladin = Aladin(\n",
" show_coo_grid=True,\n",
" target=\"TRAPPIST-1\",\n",
" coo_frame=\"icrs\",\n",
" fov=0.05,\n",
" height=400,\n",
" samp=True,\n",
")\n",
"aladin"
]
},
{
"cell_type": "markdown",
"id": "30b5ab5a",
"metadata": {},
"source": [
"### Extract region selections\n",
"First, load a custom catalog that will provide the sources to select from"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1dc08de",
"metadata": {},
"outputs": [],
"source": [
"target_name = \"TRAPPIST-1\"\n",
"catalog_data = Catalogs.query_region(\n",
" coordinates=SkyCoord.from_name(target_name),\n",
" radius=0.01, # [deg]\n",
" catalog=\"Panstarrs\",\n",
")\n",
"catalog_data.rename_columns([\"raMean\", \"decMean\"], [\"ra\", \"dec\"])\n",
"\n",
"aladin.add_table(\n",
" catalog_data, name=\"test-table\", color=\"lime\", shape=\"circle\", source_size=10\n",
")"
]
},
{
"cell_type": "markdown",
"id": "018d9426",
"metadata": {},
"source": [
"## Import a selection region\n",
"We then define a region to select within the aladin viewer"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f290b822",
"metadata": {},
"outputs": [],
"source": [
"selection_region = CircleSkyRegion(SkyCoord.from_name(target_name), radius=0.01 * u.deg)\n",
"aladin.select_region(selection_region)"
]
},
{
"cell_type": "markdown",
"id": "5e1450cb-4d6e-440f-b72c-97d8a20127a6",
"metadata": {},
"source": [
"Note: Aladin also supports selections from `RectangleSkyRegions` and `PolygonSkyRegions`"
]
},
{
"cell_type": "markdown",
"id": "bc529c0a",
"metadata": {},
"source": [
"## Export a selection region\n",
"The list of selection made in the aladin viewer is made available through a property on the aladin instance"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "89624109",
"metadata": {},
"outputs": [],
"source": [
"aladin.selected_regions"
]
},
{
"cell_type": "markdown",
"id": "4c5f9ed8",
"metadata": {},
"source": [
"### Display the selected regions as graphic overlays \n",
"You can then display those selected regions as graphic overlays"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a95e51c7",
"metadata": {},
"outputs": [],
"source": [
"aladin.add_graphic_overlay_from_region(aladin.selected_regions)"
]
},
{
"cell_type": "markdown",
"id": "267eec63-a782-4ecd-b944-5be61e484cb1",
"metadata": {},
"source": [
"The list of graphic overlays drawn is available through a property on the aladin instance"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "43b6c363-7d6d-49ac-99b9-86b5270ea41a",
"metadata": {},
"outputs": [],
"source": [
"aladin.graphic_overlays"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ab82cfb5-cc72-402a-b7e0-5844d49b82a4",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
8 changes: 8 additions & 0 deletions js/models/event_handler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import MessageHandler from "./message_handler";
import { divNumber, setDivNumber, Lock, setDivHeight } from "../utils";
import { SelectorToJson } from "./selection_handler";

export default class EventHandler {
/**
Expand Down Expand Up @@ -244,6 +245,12 @@ export default class EventHandler {
event_type: "select",
content: objectsData,
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revisit with further discussion

this.model.set("_selected_regions", [
...(this.model.get("_selected_regions") ?? []),
SelectorToJson(this.aladin),
]);
this.model.save_changes();
});

/* Aladin functionalities */
Expand Down Expand Up @@ -279,6 +286,7 @@ export default class EventHandler {
change_colormap: this.messageHandler.handleChangeColormap,
get_JPG_thumbnail: this.messageHandler.handleGetJPGThumbnail,
trigger_selection: this.messageHandler.handleTriggerSelection,
trigger_select_region: this.messageHandler.handleTriggerSelectRegion,
add_table: this.messageHandler.handleAddTable,
};

Expand Down
5 changes: 5 additions & 0 deletions js/models/message_handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { convertOptionNamesToCamelCase } from "../utils";
import { SelectRegion } from "./selection_handler";
import A from "../aladin_lite";

let imageCount = 0;
Expand Down Expand Up @@ -155,6 +156,10 @@ export default class MessageHandler {
this.aladin.select(selectionType);
}

handleTriggerSelectRegion(msg) {
SelectRegion(msg, this.aladin);
}

handleAddTable(msg, buffers) {
const options = convertOptionNamesToCamelCase(msg["options"] || {});
const buffer = buffers[0].buffer;
Expand Down
Loading