-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2453 from astrofrog/custom-stretches-instances
Make stretches be customizable on a layer by layer basis
- Loading branch information
Showing
11 changed files
with
154 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from glue.config import stretches | ||
from glue.viewers.matplotlib.state import ( | ||
DeferredDrawDictCallbackProperty as DDDCProperty, | ||
DeferredDrawSelectionCallbackProperty as DDSCProperty, | ||
) | ||
|
||
__all__ = ["StretchStateMixin"] | ||
|
||
|
||
class StretchStateMixin: | ||
stretch = DDSCProperty( | ||
docstring="The stretch used to render the layer, " | ||
"which should be one of ``linear``, " | ||
"``sqrt``, ``log``, or ``arcsinh``" | ||
) | ||
stretch_parameters = DDDCProperty( | ||
docstring="Keyword arguments to pass to the stretch" | ||
) | ||
|
||
_stretch_set_up = False | ||
|
||
def setup_stretch_callback(self): | ||
type(self).stretch.set_choices(self, list(stretches.members)) | ||
type(self).stretch.set_display_func(self, stretches.display_func) | ||
self._reset_stretch() | ||
self.add_callback("stretch", self._reset_stretch) | ||
self.add_callback("stretch_parameters", self._sync_stretch_parameters) | ||
self._stretch_set_up = True | ||
|
||
@property | ||
def stretch_object(self): | ||
if not self._stretch_set_up: | ||
raise Exception("setup_stretch_callback has not been called") | ||
return self._stretch_object | ||
|
||
def _sync_stretch_parameters(self, *args): | ||
for key, value in self.stretch_parameters.items(): | ||
if hasattr(self._stretch_object, key): | ||
setattr(self._stretch_object, key, value) | ||
else: | ||
raise ValueError( | ||
f"Stretch object {self._stretch_object.__class__.__name__} has no attribute {key}" | ||
) | ||
|
||
def _reset_stretch(self, *args): | ||
self._stretch_object = stretches.members[self.stretch]() | ||
self.stretch_parameters.clear() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import pytest | ||
|
||
from astropy.visualization import LinearStretch, LogStretch | ||
|
||
from glue.core.state_objects import State | ||
from glue.viewers.common.stretch_state_mixin import StretchStateMixin | ||
|
||
|
||
class ExampleStateWithStretch(State, StretchStateMixin): | ||
pass | ||
|
||
|
||
def test_not_set_up(): | ||
state = ExampleStateWithStretch() | ||
with pytest.raises(Exception, match="setup_stretch_callback has not been called"): | ||
state.stretch_object | ||
|
||
|
||
class TestStretchStateMixin: | ||
def setup_method(self, method): | ||
self.state = ExampleStateWithStretch() | ||
self.state.setup_stretch_callback() | ||
|
||
def test_defaults(self): | ||
assert self.state.stretch == "linear" | ||
assert len(self.state.stretch_parameters) == 0 | ||
assert isinstance(self.state.stretch_object, LinearStretch) | ||
|
||
def test_change_stretch(self): | ||
self.state.stretch = "log" | ||
assert self.state.stretch == "log" | ||
assert len(self.state.stretch_parameters) == 0 | ||
assert isinstance(self.state.stretch_object, LogStretch) | ||
|
||
def test_invalid_parameter(self): | ||
with pytest.raises( | ||
ValueError, match="Stretch object LinearStretch has no attribute foo" | ||
): | ||
self.state.stretch_parameters["foo"] = 1 | ||
|
||
def test_set_parameter(self): | ||
self.state.stretch = "log" | ||
|
||
assert self.state.stretch_object.exp == 1000 | ||
|
||
# Setting the stretch parameter 'exp' is synced with the stretch object attribute | ||
self.state.stretch_parameters["exp"] = 200 | ||
assert self.state.stretch_object.exp == 200 | ||
|
||
# Changing stretch resets the stretch parameter dictionary | ||
self.state.stretch = "linear" | ||
assert len(self.state.stretch_parameters) == 0 | ||
|
||
# And there is no memory of previous parameters | ||
self.state.stretch = "log" | ||
assert self.state.stretch_object.exp == 1000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters