diff --git a/glue/core/units.py b/glue/core/units.py index 576772f6e..8090a4888 100644 --- a/glue/core/units.py +++ b/glue/core/units.py @@ -27,19 +27,19 @@ def to_unit(self, data, cid, values, target_units): if target_units is None: return values original_units = self._get_units(data, cid) - if original_units: - return self.converter_helper.to_unit(data, cid, values, original_units, target_units) - else: + if original_units == target_units or not original_units: return values + else: + return self.converter_helper.to_unit(data, cid, values, original_units, target_units) def to_native(self, data, cid, values, original_units): if original_units is None: return values target_units = self._get_units(data, cid) - if target_units: - return self.converter_helper.to_unit(data, cid, values, original_units, target_units) - else: + if original_units == target_units or not target_units: return values + else: + return self.converter_helper.to_unit(data, cid, values, original_units, target_units) def _get_units(self, data, cid): data = data.data if isinstance(data, Subset) else data diff --git a/glue/main.py b/glue/main.py index 0b228b1ce..d32aca234 100644 --- a/glue/main.py +++ b/glue/main.py @@ -59,7 +59,7 @@ def load_plugins(splash=None, require_qt_plugins=False, plugins_to_load=None): n_plugins = len(plugins_to_require) for i_plugin, item in enumerate(list(iter_plugin_entry_points())): - if item.module in plugins_to_require: + if item.module in plugins_to_load: if item.module not in _installed_plugins: _installed_plugins.add(item.name) @@ -115,8 +115,9 @@ def load_plugins(splash=None, require_qt_plugins=False, plugins_to_load=None): from glue._settings_helpers import load_settings load_settings() + def list_plugins(): """ Function to list all plugins that are currently loaded """ - return sorted(_loaded_plugins) \ No newline at end of file + return sorted(_loaded_plugins) diff --git a/glue/plugins/wcs_autolinking/tests/test_wcs_autolinking.py b/glue/plugins/wcs_autolinking/tests/test_wcs_autolinking.py index c3f02ba25..37f2445ee 100644 --- a/glue/plugins/wcs_autolinking/tests/test_wcs_autolinking.py +++ b/glue/plugins/wcs_autolinking/tests/test_wcs_autolinking.py @@ -591,3 +591,29 @@ def test_wcs_no_approximation(): with pytest.raises(NoAffineApproximation): link.as_affine_link(tolerance=0.1) + + +def test_no_wcs_overlap(): + + wcs1 = WCS(naxis=2) + wcs1.wcs.ctype = 'RA---TAN', 'DEC--TAN' + wcs1.wcs.crval = 10, 20 + wcs1.wcs.set() + + data1 = Data(label='Data 1') + data1.coords = wcs1 + data1['x'] = np.ones((2, 3)) + + wcs2 = WCS(naxis=2) + wcs2.wcs.ctype = 'RA---TAN', 'DEC--TAN' + wcs2.wcs.crval = 190, -20 + wcs2.wcs.set() + + data2 = Data(label='Data 2') + data2.coords = wcs2 + data2['x'] = np.ones((2, 3)) + + link = WCSLink(data1, data2) + + with pytest.raises(NoAffineApproximation, match='no overlap'): + link.as_affine_link() diff --git a/glue/plugins/wcs_autolinking/wcs_autolinking.py b/glue/plugins/wcs_autolinking/wcs_autolinking.py index 854abf706..44abe524d 100644 --- a/glue/plugins/wcs_autolinking/wcs_autolinking.py +++ b/glue/plugins/wcs_autolinking/wcs_autolinking.py @@ -246,6 +246,9 @@ def as_affine_link(self, n_samples=1000, tolerance=1): For now this will only work for datasets in which two pixel coordinates are linked. + + The deviation to be compared to the tolerance is measured in the frame + of reference of the second dataset. """ if len(self.cids1) != 2 or len(self.cids2) != 2: @@ -260,6 +263,17 @@ def as_affine_link(self, n_samples=1000, tolerance=1): # Convert to pixel positions in data2 pixel2 = self.forwards(*pixel1) + keep = np.ones(n_samples, dtype=bool) + for p in pixel1 + pixel2: + keep[np.isnan(p)] = False + + if not np.any(keep): + raise NoAffineApproximation(f'Could not find a good affine approximation to ' + f'WCSLink with tolerance={tolerance}, as no overlap') + + pixel1 = [p[keep] for p in pixel1] + pixel2 = [p[keep] for p in pixel2] + # First try simple offset def transform_offset(offsets): diff --git a/glue/tests/test_main.py b/glue/tests/test_main.py index 961416a8d..b5fcff609 100644 --- a/glue/tests/test_main.py +++ b/glue/tests/test_main.py @@ -13,9 +13,8 @@ def test_load_plugins(capsys): with patch.object(logger, 'info') as info: load_plugins() - plugin = [call[0][0] for call in info.call_args_list] - assert False - + plugin = [call[0][0] for call in info.call_args_list if 'loaded' in call[0][0]] + assert len(plugin) == 5 def test_no_duplicate_loading(capsys): @@ -42,4 +41,3 @@ def test_list_plugins(): load_plugins(require_qt_plugins=False) plugins = list_plugins() assert isinstance(plugins, list) - assert len(plugins) == 14 diff --git a/glue/viewers/image/tests/test_state.py b/glue/viewers/image/tests/test_state.py index ce88efb0c..5ae4be992 100644 --- a/glue/viewers/image/tests/test_state.py +++ b/glue/viewers/image/tests/test_state.py @@ -458,3 +458,16 @@ def test_stretch_global(): assert layer_state.v_min == 49.95 assert layer_state.v_max == 949.05 + + +def test_attribute_units_invalid(): + + # Regression test for a bug that caused a crash if a dataset had an + # unrecognized unit + + viewer_state = ImageViewerState() + + data = Data(x=np.arange(100).reshape((10, 10))) + data.get_component('x').units = 'banana' + + ImageLayerState(layer=data, viewer_state=viewer_state)