Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Markers: Save to CSV #3089

Merged
merged 6 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ Specviz2d
Bug Fixes
---------

- Markers table can now export to CSV but its columns had to be changed to accomodate this fix. [#3089]
pllim marked this conversation as resolved.
Show resolved Hide resolved

Cubeviz
^^^^^^^

Expand Down
32 changes: 17 additions & 15 deletions jdaviz/configs/default/plugins/markers/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
_default_table_values = {'spectral_axis': np.nan,
'spectral_axis:unit': '',
'slice': np.nan,
'pixel': (np.nan, np.nan),
'pixel_x': np.nan,
'pixel_y': np.nan,
'pixel:unreliable': None,
'world': (np.nan, np.nan),
'world_ra': np.nan,
'world_dec': np.nan,
'world:unreliable': None,
'value': np.nan,
'value:unit': '',
Expand All @@ -50,12 +52,12 @@
super().__init__(*args, **kwargs)
if self.config == 'cubeviz':
headers = ['spectral_axis', 'spectral_axis:unit',
'slice', 'pixel',
'world', 'value', 'value:unit', 'viewer']
'slice', 'pixel_x', 'pixel_y',
'world_ra', 'world_dec', 'value', 'value:unit', 'viewer']

elif self.config == 'imviz':
headers = ['pixel', 'pixel:unreliable',
'world', 'world:unreliable',
headers = ['pixel_x', 'pixel_y', 'pixel:unreliable',
'world_ra', 'world_dec', 'world:unreliable',
'value', 'value:unit', 'value:unreliable',
'viewer']

Expand All @@ -65,11 +67,11 @@
elif self.config == 'specviz2d':
# TODO: add "index" if/when specviz2d supports plotting spectral_axis
headers = ['spectral_axis', 'spectral_axis:unit',
'pixel', 'value', 'value:unit', 'viewer']
'pixel_x', 'pixel_y', 'value', 'value:unit', 'viewer']
elif self.config == 'mosviz':
headers = ['spectral_axis', 'spectral_axis:unit',
'pixel', 'world', 'index', 'value', 'value:unit',
'viewer']
'pixel_x', 'pixel_y', 'world_ra', 'world_dec', 'index',
'value', 'value:unit', 'viewer']
else:
# allow downstream configs to override headers
headers = kwargs.get('headers', [])
Expand Down Expand Up @@ -107,7 +109,7 @@
def _recompute_mark_positions(self, viewer):
if self.table is None or self.table._qtable is None:
return
if 'world' not in self.table.headers_avail:
if 'world_ra' not in self.table.headers_avail:
return

viewer_id = viewer.reference if viewer.reference is not None else viewer.reference_id
Expand All @@ -124,8 +126,8 @@
viewer_mark.x, viewer_mark.y = [], []
return

orig_world_x = np.asarray(self.table._qtable['world'][:, 0][in_viewer])
orig_world_y = np.asarray(self.table._qtable['world'][:, 1][in_viewer])
orig_world_x = np.asarray(self.table._qtable['world_ra'][in_viewer])
orig_world_y = np.asarray(self.table._qtable['world_dec'][in_viewer])

if self.app._link_type.lower() == 'wcs':
# convert from the sky coordinates in the table to pixels via the WCS of the current
Expand Down Expand Up @@ -162,8 +164,8 @@
# TODO: should we rescale these since pixel coordinates when linked by WCS are always
# on the range 0-1 because of the orientation layer? Or hide the pixel option in the
# cycler when WCS-linked?
pixel_x = np.asarray(self.table._qtable['pixel'][:, 0])
pixel_y = np.asarray(self.table._qtable['pixel'][:, 1])
pixel_x = np.asarray(self.table._qtable['pixel_x'])
pixel_y = np.asarray(self.table._qtable['pixel_y'])

Check warning on line 168 in jdaviz/configs/default/plugins/markers/markers.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/configs/default/plugins/markers/markers.py#L167-L168

Added lines #L167 - L168 were not covered by tests
new_x = np.append(new_x, pixel_x[pixel_only_inds])
new_y = np.append(new_y, pixel_y[pixel_only_inds])

Expand Down Expand Up @@ -221,7 +223,7 @@
try:
self.table.add_item({k: v for k, v in row_info.items()
if k in self.table.headers_avail})
except ValueError as err:
except ValueError as err: # pragma: no cover
raise ValueError(f'failed to add {row_info} to table: {repr(err)}')

x, y = row_info['axes_x'], row_info['axes_y']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ def test_markers_cubeviz(tmp_path, cubeviz_helper, spectrum1d_cube):
'slice': 1.0,
'spectral_axis': 4.62360027696835e-07,
'spectral_axis:unit': 'm',
'world': (204.99988776727642,
27.000099999955538),
'world_ra': 204.99988776727642,
'world_dec': 27.000099999955538,
'world:unreliable': False,
'pixel': (0, 0),
'pixel_x': 0,
'pixel_y': 0,
'pixel:unreliable': False,
'value': 8.0,
'value:unit': 'Jy',
Expand Down Expand Up @@ -139,6 +140,13 @@ def test_markers_cubeviz(tmp_path, cubeviz_helper, spectrum1d_cube):
exp.export()
assert os.path.isfile(filename)

# Also exports to CSV
filename_2 = str(tmp_path / "cubeviz_export.csv")
exp.plugin_table_format.selected = 'csv'
exp.filename.value = filename_2
exp.export()
assert os.path.isfile(filename_2)

# clearing table clears markers
mp.clear_table()
assert mp.export_table() is None
Expand Down Expand Up @@ -170,7 +178,8 @@ def test_markers_layer_cycle(self):
'axes_y': 5,
'axes_y:unit': 'pix',
'data_label': 'no_wcs[SCI,1]',
'pixel': (5.0, 5.0),
'pixel_x': 5.0,
'pixel_y': 5.0,
'pixel:unreliable': False,
'value': 55.0,
'value:unit': '',
Expand All @@ -194,7 +203,8 @@ def test_markers_layer_cycle(self):
'axes_y': 5,
'axes_y:unit': 'pix',
'data_label': '',
'pixel': (5.0, 5.0),
'pixel_x': 5.0,
'pixel_y': 5.0,
'pixel:unreliable': False}

mp._obj._on_viewer_key_event(self.viewer, {'event': 'keydown',
Expand All @@ -215,10 +225,11 @@ def test_markers_layer_cycle(self):
'axes_y': 5,
'axes_y:unit': 'pix',
'data_label': 'has_wcs[SCI,1]',
'world': (337.5187947653852,
-20.831944164705973),
'world_ra': 337.5187947653852,
'world_dec': -20.831944164705973,
'world:unreliable': False,
'pixel': (5.0, 5.0),
'pixel_x': 5.0,
'pixel_y': 5.0,
'pixel:unreliable': False,
'value': 55.0,
'value:unit': '',
Expand Down
7 changes: 4 additions & 3 deletions jdaviz/configs/imviz/plugins/coords_info/coords_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,8 @@ def _image_viewer_update(self, viewer, x, y):
self.row3_title = ''
self.row3_text = f'{world_ra_deg} {world_dec_deg} (deg)'
self.row3_unreliable = unreliable_world
# TODO: use sky directly, but need to figure out how to have a compatible "blank" entry
self._dict['world'] = (sky.ra.value, sky.dec.value)
self._dict['world_ra'] = sky.ra.value
self._dict['world_dec'] = sky.dec.value
self._dict['world:unreliable'] = unreliable_world
elif isinstance(viewer, MosvizProfile2DView) and hasattr(getattr(image, 'coords', None),
'pixel_to_world'):
Expand Down Expand Up @@ -423,7 +423,8 @@ def _image_viewer_update(self, viewer, x, y):
self.row1a_title = 'Pixel'
self.row1a_text = (fmt.format(x, y))
self.row1_unreliable = unreliable_pixel
self._dict['pixel'] = (float(x), float(y))
self._dict['pixel_x'] = float(x)
self._dict['pixel_y'] = float(y)
self._dict['pixel:unreliable'] = unreliable_pixel

# Extract data values at this position.
Expand Down
2 changes: 1 addition & 1 deletion jdaviz/core/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4524,7 +4524,7 @@ def float_precision(column, item):
# stored in astropy table as a float so we can also store nans,
# but should display in the UI without any decimals
return f"{item:.0f}"
elif column in ('pixel', ):
elif column in ('pixel', 'pixel_x', 'pixel_y'):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This mixin is quite generic so I left 'pixel' in, just in case.

return f"{item:0.3f}"
elif column in ('xcenter', 'ycenter'):
return f"{item:0.1f}"
Expand Down