Skip to content

Commit

Permalink
adding coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bmorris3 committed Jul 22, 2024
1 parent 7fd2f51 commit 12f8c56
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 25 deletions.
1 change: 1 addition & 0 deletions lcviz/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def light_curve_like_kepler_quarter(seed=42):
lc['flux_alt_err'] = flux_err
lc.meta['MISSION'] = 'KEPLER'
lc.meta['QUARTER'] = 10
lc.meta['OBJECT'] = 'HAT-P-11'

return lc

Expand Down
63 changes: 44 additions & 19 deletions lcviz/plugins/ephemeris/ephemeris.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
SelectPluginComponent, EditableSelectPluginComponent,
with_spinner)
from jdaviz.core.user_api import PluginUserApi
from jdaviz.core.events import SnackbarMessage

from lightkurve import periodogram, FoldedLightCurve

Expand Down Expand Up @@ -137,6 +138,10 @@ def __init__(self, *args, **kwargs):
selected='method_selected',
manual_options=['Lomb-Scargle', 'Box Least Squares'])

self.query_result = SelectPluginComponent(self,
items='query_result_items',
selected='query_result_selected')

# TODO: could optimize by only updating for the new data entry only
# (would require some refactoring and probably wouldn't have significant gains)
self.hub.subscribe(self, DataCollectionAddMessage, handler=self._update_all_phase_arrays)
Expand All @@ -153,7 +158,7 @@ def user_api(self):
'times_to_phases', 'phases_to_times', 'get_data',
'dataset', 'method', 'period_at_max_power',
'adopt_period_at_max_power', 'query_for_ephemeris',
'query_result'
'query_result', 'adopt_from_catalog', 'adopt_from_catalog_in_new_viewer'
]
return PluginUserApi(self, expose=expose)

Expand Down Expand Up @@ -641,24 +646,25 @@ def query_for_ephemeris(self):
return None

Check warning on line 646 in lcviz/plugins/ephemeris/ephemeris.py

View check run for this annotation

Codecov / codecov/patch

lcviz/plugins/ephemeris/ephemeris.py#L646

Added line #L646 was not covered by tests

else:
self.query_result = query_result
self.query_result.add_index('pl_name')
query_result.sort('pl_name')
self.astroquery_result = query_result
self.astroquery_result.add_index('pl_name')
self.query_result_items = [
{
'name': name,
'label': name, # required key for SelectPluginComponent
'period': period,
'epoch': epoch if not np.isnan(epoch) else 0
}
for name, period, epoch in zip(
sorted(list(self.query_result['pl_name'])),
np.array(self.query_result['pl_orbper'].to_value(u.day)),
np.array(self.query_result['pl_tranmid'].to_value(u.day))
list(self.astroquery_result['pl_name']),
np.array(self.astroquery_result['pl_orbper'].to_value(u.day)),
np.array(self.astroquery_result['pl_tranmid'].to_value(u.day))
)
]

@observe('query_result_selected')
def _select_query_result(self, *args):
selected_query_result = self.query_result.loc[self.query_result_selected]
selected_query_result = self.astroquery_result.loc[self.query_result_selected]
self.period_from_catalog = selected_query_result['pl_orbper'].base.to_value(u.day)
ref_time = self.app.data_collection[0].coords.reference_time.jd
if np.isnan(selected_query_result['pl_tranmid'].base.to_value(u.day)):
Expand All @@ -673,19 +679,38 @@ def vue_query_for_ephemeris(self, *args):
self.query_for_ephemeris()

Check warning on line 679 in lcviz/plugins/ephemeris/ephemeris.py

View check run for this annotation

Codecov / codecov/patch

lcviz/plugins/ephemeris/ephemeris.py#L679

Added line #L679 was not covered by tests

def adopt_from_catalog(self, *args):
if not np.any(np.isnan([self.period_from_catalog, self.t0_from_catalog])):
self.period = self.period_from_catalog
self.t0 = self.t0_from_catalog

# reset the phase axis wrap to feature the primary transit:
self.wrap_at = 0.5
viewer = self._get_phase_viewers()[0]
viewer.reset_limits()
if len(self._get_phase_viewers()):
# if a phase viewer is available, adopt the ephemeris in the phase viewer:
if not np.any(np.isnan([self.period_from_catalog, self.t0_from_catalog])):
self.period = self.period_from_catalog
self.t0 = self.t0_from_catalog

# reset the phase axis wrap to feature the primary transit:
self.wrap_at = 0.5
viewer = self._get_phase_viewers()[0]
viewer.reset_limits()
else:
# otherwise, adopt the ephemeris in a new phase viewer:
self.adopt_from_catalog_in_new_viewer()

def vue_adopt_from_catalog(self, *args):
self.adopt_from_catalog()

Check warning on line 697 in lcviz/plugins/ephemeris/ephemeris.py

View check run for this annotation

Codecov / codecov/patch

lcviz/plugins/ephemeris/ephemeris.py#L697

Added line #L697 was not covered by tests

def adopt_from_catalog_in_new_viewer(self, *args):
new_component_label = self.query_result_selected.replace(' ', '')
if len(self._get_phase_viewers(new_component_label)):
# warn the user that an ephemeris component already exists with this label,
# a second won't be added:
self.hub.broadcast(

Check warning on line 704 in lcviz/plugins/ephemeris/ephemeris.py

View check run for this annotation

Codecov / codecov/patch

lcviz/plugins/ephemeris/ephemeris.py#L704

Added line #L704 was not covered by tests
SnackbarMessage(
f"Ephemeris component {new_component_label} already exists, skipping",
sender=self, color="warning"
)
)
else:
self.add_component(new_component_label)
self.create_phase_viewer()
self.adopt_from_catalog()

def vue_adopt_from_catalog_in_new_viewer(self, *args):
self.add_component(self.query_result_selected.replace(' ', ''))
self.create_phase_viewer()
self.adopt_from_catalog()
self.adopt_from_catalog_in_new_viewer()

Check warning on line 716 in lcviz/plugins/ephemeris/ephemeris.py

View check run for this annotation

Codecov / codecov/patch

lcviz/plugins/ephemeris/ephemeris.py#L716

Added line #L716 was not covered by tests
6 changes: 3 additions & 3 deletions lcviz/plugins/ephemeris/ephemeris.vue
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
:menu-props="{ left: true }"
attach
:items="query_result_items"
:item-value="item => item.name"
:item-value="item => item.label"
v-model="query_result_selected"
label="Ephemerides available"
:hint="'Ephemeris parameters from ' + query_result_items.length + ' available query result(s)'"
Expand All @@ -222,12 +222,12 @@

<template v-slot:selection="{ item }">
<span>
{{ item.name }}
{{ item.label }}
</span>
</template>
<template v-slot:item="{ item }">
<span style="margin-top: 8px; margin-bottom: 0px">
{{ item.name }}
{{ item.label }}
<v-row style="line-height: 1.0; margin: 0px; opacity: 0.85; font-size: 8pt">
Period: {{ item.period }} d, Epoch: {{ item.epoch }} d
</v-row>
Expand Down
3 changes: 2 additions & 1 deletion lcviz/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def test_apply_yrangerois(helper, light_curve_like_kepler_quarter):
def test_data_label(helper, light_curve_like_kepler_quarter):
# add data without specifying data label:
helper.load_data(light_curve_like_kepler_quarter)
assert helper.app.data_collection[-1].label == 'Light curve [Q10]'
object_name = helper.app.data_collection[-1].meta['OBJECT']
assert helper.app.data_collection[-1].label == f'{object_name} [Q10]'

# specify label, check that quarter isn't appended:
data_label = 'Cool target'
Expand Down
22 changes: 22 additions & 0 deletions lcviz/tests/test_plugin_ephemeris.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,25 @@ def test_create_phase_viewer(helper, light_curve_like_kepler_quarter):

ephem.add_component('new')
assert len(vc.viewer_types) == 3


def test_ephemeris_queries(helper, light_curve_like_kepler_quarter):
helper.load_data(light_curve_like_kepler_quarter)
ephem = helper.plugins['Ephemeris']

ephem.query_for_ephemeris()
# this should be HAT-P-11 b:
planet = ephem.query_result.choices[0]
assert planet == 'HAT-P-11 b'

ephem.query_result = planet
ephem.adopt_from_catalog()

# compare against best/recent parameters:
period_yee_2018 = 4.88780244
assert abs(1 - period_yee_2018 / ephem.period) < 1e-3

epoch_kokori_2022 = 2455109.335119
ref_time = helper.app.data_collection[0].coords.reference_time.jd
expected_t0 = (epoch_kokori_2022 - ref_time) % period_yee_2018
assert abs(1 - expected_t0 / ephem.t0) < 1e-3
5 changes: 3 additions & 2 deletions lcviz/tests/test_plugin_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def test_plugin_markers(helper, light_curve_like_kepler_quarter):
'Time 5.45833e+00 d',
'Flux 9.67587e-01')

_assert_dict_allclose(label_mouseover.as_dict(), {'data_label': 'Light curve [Q10]',
object_name = helper.app.data_collection[-1].meta['OBJECT']
_assert_dict_allclose(label_mouseover.as_dict(), {'data_label': f'{object_name} [Q10]',
'time': 5.4583335,
'time:unit': 'd',
'phase': np.nan,
Expand Down Expand Up @@ -81,7 +82,7 @@ def test_plugin_markers(helper, light_curve_like_kepler_quarter):
'Phase 0.45833',
'Flux 9.67587e-01')

_assert_dict_allclose(label_mouseover.as_dict(), {'data_label': 'Light curve [Q10]',
_assert_dict_allclose(label_mouseover.as_dict(), {'data_label': f'{object_name} [Q10]',
'time': 5.458333374001086,
'time:unit': 'd',
'phase': 0.4583333730697632,
Expand Down

0 comments on commit 12f8c56

Please sign in to comment.