-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* stitch plugin: basic implementation * only shown in plugin tray if at least two light curves are loaded into a viewer * optionally replace input datasets when stitching
- Loading branch information
Showing
8 changed files
with
227 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .stitch import * # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
from traitlets import Bool, Unicode, observe | ||
from lightkurve import LightCurve, LightCurveCollection | ||
|
||
from jdaviz.core.registries import tray_registry | ||
from jdaviz.core.template_mixin import (PluginTemplateMixin, | ||
DatasetMultiSelectMixin, | ||
AddResultsMixin, | ||
with_spinner) | ||
from jdaviz.core.user_api import PluginUserApi | ||
|
||
from lcviz.utils import data_not_folded | ||
|
||
__all__ = ['Stitch'] | ||
|
||
|
||
@tray_registry('stitch', label="Stitch") | ||
class Stitch(PluginTemplateMixin, DatasetMultiSelectMixin, AddResultsMixin): | ||
""" | ||
See the :ref:`Stitch Plugin Documentation <stitch>` for more details. | ||
Only the following attributes and methods are available through the | ||
:ref:`public plugin API <plugin-apis>`: | ||
* :meth:`~jdaviz.core.template_mixin.PluginTemplateMixin.show` | ||
* :meth:`~jdaviz.core.template_mixin.PluginTemplateMixin.open_in_tray` | ||
* :meth:`~jdaviz.core.template_mixin.PluginTemplateMixin.close_in_tray` | ||
* ``dataset`` (:class:`~jdaviz.core.template_mixin.DatasetSelect`): | ||
Datasets to stitch. | ||
* ``remove_input_datasets`` | ||
* ``add_results`` (:class:`~jdaviz.core.template_mixin.AddResults`) | ||
* :meth:`stitch` | ||
""" | ||
template_file = __file__, "stitch.vue" | ||
uses_active_status = Bool(False).tag(sync=False) | ||
|
||
remove_input_datasets = Bool(False).tag(sync=True) | ||
stitch_err = Unicode().tag(sync=True) | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
self.dataset.multiselect = True | ||
# do not support stitching data in phase-space | ||
self.dataset.add_filter(data_not_folded) | ||
|
||
self.results_label_default = 'stitched' | ||
|
||
@property | ||
def user_api(self): | ||
expose = ['dataset', 'stitch', 'remove_input_datasets', 'add_results'] | ||
return PluginUserApi(self, expose=expose) | ||
|
||
@observe('dataset_items') | ||
def _set_relevent(self, *args): | ||
if len(self.dataset_items) < 2: | ||
self.irrelevant_msg = 'Requires at least two datasets loaded into viewers' | ||
else: | ||
self.irrelevant_msg = '' | ||
|
||
@with_spinner() | ||
def stitch(self, add_data=True): | ||
""" | ||
Stitch multiple light curves (``dataset``) together using lightkurve.stitch. | ||
Parameters | ||
---------- | ||
add_data : bool | ||
Whether to add the resulting light curve to the app. | ||
Returns | ||
------- | ||
output_lc : `~lightkurve.LightCurve` | ||
The flattened light curve. | ||
""" | ||
if not self.dataset.multiselect: | ||
raise ValueError("dataset must be in multiselect mode") | ||
if len(self.dataset.selected) < 2: | ||
raise ValueError("multiple datasets must be selected") | ||
lcc = LightCurveCollection([dci.get_object(LightCurve) | ||
for dci in self.dataset.selected_dc_item]) | ||
stitched_lc = lcc.stitch(corrector_func=lambda x: x) | ||
|
||
if add_data: | ||
self.add_results.add_results_from_plugin(stitched_lc) | ||
if self.remove_input_datasets: | ||
for dataset in self.dataset.selected: | ||
self.app.vue_data_item_remove({'item_name': dataset}) | ||
return stitched_lc | ||
|
||
def vue_apply(self, *args, **kwargs): | ||
try: | ||
self.stitch(add_data=True) | ||
except Exception as e: | ||
self.stitch_err = str(e) | ||
else: | ||
self.stitch_err = '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<template> | ||
<j-tray-plugin | ||
description='Stitch light curves together.' | ||
:link="'https://lcviz.readthedocs.io/en/'+vdocs+'/plugins.html#stitch'" | ||
:popout_button="popout_button"> | ||
|
||
<plugin-dataset-select | ||
:items="dataset_items" | ||
:selected.sync="dataset_selected" | ||
:show_if_single_entry="true" | ||
:multiselect="true" | ||
label="Data" | ||
hint="Select the light curves as input." | ||
/> | ||
|
||
<v-row v-if="dataset_selected.length < 2"> | ||
<span class="v-messages v-messages__message text--secondary"> | ||
<b style="color: red !important">Must select at least two input light curves to stitch.</b> | ||
</span> | ||
</v-row> | ||
|
||
<plugin-add-results v-else | ||
:label.sync="results_label" | ||
:label_default="results_label_default" | ||
:label_auto.sync="results_label_auto" | ||
:label_invalid_msg="results_label_invalid_msg" | ||
:label_overwrite="results_label_overwrite" | ||
label_hint="Label for the binned data." | ||
:add_to_viewer_items="add_to_viewer_items" | ||
:add_to_viewer_selected.sync="add_to_viewer_selected" | ||
action_label="Stitch" | ||
action_tooltip="Stitch data" | ||
:action_disabled="dataset_selected.length < 2" | ||
:action_spinner="spinner" | ||
@click:action="apply" | ||
> | ||
<v-row> | ||
<v-switch | ||
v-model="remove_input_datasets" | ||
label="Remove input datasets" | ||
hint='Delete input datasets from the app' | ||
persistent-hint | ||
> | ||
</v-switch> | ||
</v-row> | ||
</plugin-add-results> | ||
|
||
<v-row v-if="stitch_err"> | ||
<span class="v-messages v-messages__message text--secondary"> | ||
<b style="color: red !important">ERROR:</b> {{stitch_err}} | ||
</span> | ||
</v-row> | ||
|
||
</j-tray-plugin> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
def test_docs_snippets(helper, light_curve_like_kepler_quarter): | ||
lcviz, lc = helper, light_curve_like_kepler_quarter | ||
lc1 = lc | ||
lc2 = lc.copy() | ||
|
||
lcviz.load_data(lc1, 'lc1') | ||
lcviz.load_data(lc2, 'lc2') | ||
lcviz.app.add_data_to_viewer('flux-vs-time', 'lc2') | ||
# lcviz.show() | ||
|
||
stitch = lcviz.plugins['Stitch'] | ||
stitch.open_in_tray() | ||
stitch.dataset.select_all() | ||
stitched_lc = stitch.stitch() | ||
print(stitched_lc) | ||
|
||
|
||
def test_plugin_stitch(helper, light_curve_like_kepler_quarter): | ||
helper.load_data(light_curve_like_kepler_quarter) | ||
|
||
assert "Stitch" not in helper.plugins.keys() | ||
|
||
helper.load_data(light_curve_like_kepler_quarter.copy()) | ||
assert "Stitch" in helper.plugins.keys() | ||
|
||
stitch = helper.plugins['Stitch'] | ||
stitch.dataset.select_all() | ||
stitched_lc = stitch.stitch() | ||
|
||
assert len(stitched_lc) == 2 * len(light_curve_like_kepler_quarter) |