Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Make stretches be customizable on a layer by layer basis #2453

Merged
merged 6 commits into from
Nov 30, 2023

Conversation

astrofrog
Copy link
Member

For the image and scatter viewers, this adds a new dictionary property stretch_parameters which can be used to set stretch parameters specific to the layer. These parameters are then set as attribute on state instances. So the requirement for new stretch classes is that parameters can be set as attributes on existing instances (e.g. LogStretch.exp). Example:

Screenshot from 2023-10-26 15-25-44

Glue deals with keeping stretch_parameters in sync with the stretch instance/object, and will deal with refreshing the plot.

Comment on lines +37 to +43
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}"
)
Copy link
Contributor

Choose a reason for hiding this comment

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

If a stretch accepts multiple attributes but needs to validate them wrt each other (for example: x/y coordinates of knot locations for a spline), what would be the best way to handle that?

As this stands, those will need to be passed as a single entry (stretch_parameters['knots'] = ([x1, x2], [y1, y2])) so that validation can occur in a single setter on the stretch object.

Alternative ideas:

  • support some sort of context manager (similar to hold_sync) that validates the attributes after setting all of them at the end of this for-loop? This of course needs to be compatible with the astropy stretch objects that are used by default as we probably can't modify those upstream just to account for this.
  • instead of looping and checking for hasattr on the individual keys, could we instead require a update_parameters method (if stretch_parameters is not empty) on the stretch object which handles this logic internally by getting the entire dictionary in one pass?

cc @haticekaratay

Copy link
Member Author

Choose a reason for hiding this comment

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

Just to put another idea out there, could you perhaps not validate when the attributes are set but when the stretch is called?

Copy link
Contributor

Choose a reason for hiding this comment

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

That's an option, but probably causes unnecessary cost when calling the stretch repeatedly. In our case, we can probably just validate externally since we manage the stretch object, but then if a user modifies the values, they will need to make sure they make sense.

@astrofrog astrofrog merged commit 127837a into glue-viz:main Nov 30, 2023
20 of 22 checks passed
@pllim
Copy link
Contributor

pllim commented Dec 5, 2023

Is this patch not backward compatible?

@astrofrog
Copy link
Member Author

I did try and make it backward compatible but may have missed something. I will investigate the jdaviz failures tomorrow.

@pllim
Copy link
Contributor

pllim commented Dec 6, 2023

I think it is ok. I ended up doing a version check and it seems to work. See spacetelescope/jdaviz#2591

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants