Skip to content

Commit

Permalink
basic test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kecnry committed May 26, 2023
1 parent fae2bac commit 0799c17
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lcviz/plugins/coords_info/coords_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def update_display(self, viewer, x, y):

is_phase = isinstance(viewer, PhaseScatterView)
# TODO: update with display_unit when supported in lcviz
x_unit = '' if is_phase else viewer.time_unit
x_unit = '' if is_phase else str(viewer.time_unit)
y_unit = str(viewer.data()[0].flux.unit)

def _cursor_fallback():
Expand All @@ -37,6 +37,11 @@ def _cursor_fallback():
self._dict['phase'] = x if is_phase else np.nan
self._dict['flux'] = y
self._dict['ephemeris'] = ''

self.row2_title = ''
self.row2_text = ''
self.row3_title = ''
self.row3_text = ''
self.icon = 'mdi-cursor-default'
self.marks[viewer._reference_id].visible = False

Expand Down Expand Up @@ -72,7 +77,7 @@ def _cursor_fallback():
# (making it easier to get the snapping point into shallow eclipses, etc)
distsqs = ((lyr_x - x)/xrange)**2 + ((lyr_y - y)/yrange)**2
cur_i = np.nanargmin(distsqs)
cur_x, cur_y = lyr_x[cur_i], lyr_y[cur_i]
cur_x, cur_y = float(lyr_x[cur_i]), float(lyr_y[cur_i])
cur_distsq = distsqs[cur_i]

if (closest_distsq is None) or (cur_distsq < closest_distsq):
Expand All @@ -94,7 +99,7 @@ def _cursor_fallback():
component_labels = [comp.label for comp in closest_lyr.layer.components]
time_comp = closest_lyr.layer.components[component_labels.index('World 0')]
times = closest_lyr.layer.get_data(time_comp)
self._dict['time'] = times[closest_i]
self._dict['time'] = float(times[closest_i])
self._dict['phase'] = closest_x
self._dict['ephemeris'] = viewer.reference.split(':')[1]
else:
Expand Down
103 changes: 103 additions & 0 deletions lcviz/tests/test_plugin_markers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import numpy as np
from numpy.testing import assert_allclose

from jdaviz.core.marks import MarkersMark


def _get_markers_from_viewer(viewer):
return [m for m in viewer.figure.marks if isinstance(m, MarkersMark)][0]


def _assert_dict_allclose(dict1, dict2):
assert dict1.keys() == dict2.keys()
for k, v in dict1.items():
if isinstance(v, float):
assert_allclose(v, dict2.get(k))
elif isinstance(v, (tuple, list)):
assert_allclose(np.asarray(v), np.asarray(dict2.get(k)))
else:
assert v == dict2.get(k)


def test_plugin_markers(helper, light_curve_like_kepler_quarter):
helper.load_data(light_curve_like_kepler_quarter)
tv = helper.app.get_viewer(helper._default_time_viewer_reference_name)

mp = helper.plugins['Markers']
label_mouseover = mp._obj.coords_info
mp.open_in_tray()

# test event in flux-vs-time viewer
label_mouseover._viewer_mouse_event(tv,
{'event': 'mousemove',
'domain': {'x': 0, 'y': 0}})

assert label_mouseover.as_text() == ('Cursor 0.00000e+00, 0.00000e+00',
'Time 5.45833e+00 d',
'Flux 9.67587e-01')

_assert_dict_allclose(label_mouseover.as_dict(), {'data_label': 'Light curve',
'time': 5.4583335,
'phase': np.nan,
'ephemeris': '',
'axes_x': 5.4583335,
'axes_x:unit': 'd',
'index': 262.0,
'axes_y': 0.96758735,
'axes_y:unit': '',
'flux': 0.96758735})

mp._obj._on_viewer_key_event(tv, {'event': 'keydown',
'key': 'm'})
assert len(mp.export_table()) == 1
assert len(_get_markers_from_viewer(tv).x) == 1

ephem = helper.plugins['Ephemeris']
pv = ephem.create_phase_viewer()

# test event in flux-vs-phase viewer
label_mouseover._viewer_mouse_event(pv,
{'event': 'mousemove',
'domain': {'x': 0.5, 'y': 0}})

assert label_mouseover.as_text() == ('Cursor 5.00000e-01, 0.00000e+00',
'Phase 0.45833',
'Flux 9.67587e-01')

_assert_dict_allclose(label_mouseover.as_dict(), {'data_label': 'Light curve',
'time': 5.458333374001086,
'phase': 0.4583333730697632,
'ephemeris': 'default',
'axes_x': 0.4583333730697632,
'axes_x:unit': '',
'index': 262.0,
'axes_y': 0.9675873517990112,
'axes_y:unit': '',
'flux': 0.9675873517990112})

mp._obj._on_viewer_key_event(pv, {'event': 'keydown',
'key': 'm'})
assert len(mp.export_table()) == 2
assert len(_get_markers_from_viewer(tv).x) == 1
assert len(_get_markers_from_viewer(pv).x) == 1

# test event in flux-vs-phase viewer (with cursor only)
label_mouseover.dataset.selected = 'none'
label_mouseover._viewer_mouse_event(pv,
{'event': 'mousemove',
'domain': {'x': 0.6, 'y': 0}})

print(label_mouseover.as_text())
assert label_mouseover.as_text() == ('Cursor 6.00000e-01, 0.00000e+00',
'',
'')

_assert_dict_allclose(label_mouseover.as_dict(), {'axes_x': 0.6,
'axes_x:unit': '',
'axes_y': 0,
'axes_y:unit': '',
'data_label': '',
'time': np.nan,
'phase': 0.6,
'flux': 0,
'ephemeris': ''})

0 comments on commit 0799c17

Please sign in to comment.