Skip to content

Commit

Permalink
move to mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
cshanahan1 committed Jan 8, 2025
1 parent 61c553b commit 4392488
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
3 changes: 2 additions & 1 deletion jdaviz/configs/imviz/plugins/catalogs/catalogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class Catalogs(PluginTemplateMixin, ViewerSelectMixin, HasFileImportSelect, Tabl
@property
def user_api(self):
return PluginUserApi(self, expose=('clear_table', 'export_table',
'zoom_to_selected'))
'zoom_to_selected', 'select_rows',
'select_all'))

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
14 changes: 7 additions & 7 deletions jdaviz/configs/imviz/tests/test_catalogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_plugin_image_with_result(self, imviz_helper, tmp_path):
assert catalogs_plugin.results_available
assert catalogs_plugin.number_of_results == prev_results

catalogs_plugin._obj.table.select_rows(slice(0, 2))
catalogs_plugin.select_rows(slice(0, 2))
assert len(catalogs_plugin.table.selected_rows) == 2

# test Gaia catalog
Expand Down Expand Up @@ -308,7 +308,7 @@ def test_zoom_to_selected(imviz_helper, image_2d_wcs, tmp_path):
catalogs_plugin._obj.search()

# select both sources
catalogs_plugin._obj.table.select_all()
catalogs_plugin.select_all()

# check viewer limits before zoom
xmin, xmax, ymin, ymax = imviz_helper.app._jdaviz_helper._default_viewer.get_limits()
Expand Down Expand Up @@ -422,31 +422,31 @@ def test_select_catalog_table_rows(imviz_helper, image_2d_wcs, tmp_path):
catalogs_plugin._obj.search()

# select a single row:
plugin_table.select_rows(3)
catalogs_plugin.select_rows(3)
assert len(plugin_table.selected_rows) == 1
assert plugin_table.selected_rows[0]['Right Ascension (degrees)'] == '337.48000'

# select multiple rows by indices
plugin_table.select_rows([3, 2, 4])
catalogs_plugin.select_rows([3, 2, 4])
assert len(plugin_table.selected_rows) == 3
assert plugin_table.selected_rows[0]['Right Ascension (degrees)'] == '337.48000'
assert plugin_table.selected_rows[1]['Right Ascension (degrees)'] == '337.47000'
assert plugin_table.selected_rows[2]['Right Ascension (degrees)'] == '337.49000'

# select a range of rows with a slice
plugin_table.select_rows(slice(0, 2))
catalogs_plugin.select_rows(slice(0, 2))
assert len(plugin_table.selected_rows) == 2
assert plugin_table.selected_rows[0]['Right Ascension (degrees)'] == '337.49000'
assert plugin_table.selected_rows[1]['Right Ascension (degrees)'] == '337.46000'

# select rows with multi dim. numpy slice
plugin_table.select_rows(np.s_[0:2, 3:5])
catalogs_plugin.select_rows(np.s_[0:2, 3:5])
assert len(plugin_table.selected_rows) == 4
assert plugin_table.selected_rows[0]['Right Ascension (degrees)'] == '337.49000'
assert plugin_table.selected_rows[1]['Right Ascension (degrees)'] == '337.46000'
assert plugin_table.selected_rows[2]['Right Ascension (degrees)'] == '337.48000'
assert plugin_table.selected_rows[3]['Right Ascension (degrees)'] == '337.49000'

# test select_all
plugin_table.select_all()
catalogs_plugin.select_all()
assert len(plugin_table.selected_rows) == 6
26 changes: 25 additions & 1 deletion jdaviz/core/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4713,7 +4713,8 @@ def __init__(self, plugin, name='table', selected_rows_changed_callback=None,

@property
def user_api(self):
return UserApiWrapper(self, ('clear_table', 'export_table'))
return UserApiWrapper(self, ('clear_table', 'export_table',
'select_rows', 'select_all'))

def default_value_for_column(self, colname=None, value=None):
if colname in self._default_values_by_colname:
Expand Down Expand Up @@ -4895,6 +4896,8 @@ class TableMixin(VuetifyTemplate, HubListener):
* :meth:`clear_table`
* :meth:`export_table`
* :meth:`select_rows`
* :meth:`select_all`
To render in the plugin's vue file::
Expand Down Expand Up @@ -4933,6 +4936,27 @@ def export_table(self, filename=None, overwrite=False):
"""
return self.table.export_table(filename=filename, overwrite=overwrite)

def select_rows(self, rows):
"""
Select rows from the current table by index, indices, or slice.
Parameters
----------
rows : int, list of int, slice, or tuple of slice
The rows to select. This can be:
- An integer specifying a single row index.
- A list of integers specifying multiple row indices.
- A slice object specifying a range of rows.
- A tuple of slices (e.g using numpy slice)
"""

self.table.select_rows(rows)

def select_all(self):
""" Select all rows in table."""
self.table.select_all()


class Plot(PluginSubcomponent):
"""
Expand Down

0 comments on commit 4392488

Please sign in to comment.