Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Creation of the Norm Protocol #30149

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

Closed
wants to merge 2 commits into from
Closed

Conversation

trygvrad
Copy link
Contributor

@trygvrad trygvrad commented Jun 6, 2025

This PR is a response to a discussion in the past weeks weekly developer meeting regarding the creation of a Norm protocol before the introduction of MultiNorm #29876 (comment)

Prior to this PR there are no Protocols in matplotlib.

This implementation uses @runtime_checkable so that Colorizer.set_norm() can check _api.check_isinstance((colors.Norm, str, None), norm=norm)

Note that the error message if the class one attempts to use is missing a member, is just the standard wrong-type message, and does not tell you what member of the protocol is missing.

@timhoffm @tacaswell @ksunden @story645

The implementation looks like this:


@runtime_checkable
class Norm(Protocol):

    @property
    def vmin(self):
        """Lower limit of the input data interval; maps to 0."""
        ...


    @property
    def vmax(self):
        """Upper limit of the input data interval; maps to 1."""
        ...

    @property
    def clip(self):
        """
        Determines the behavior for mapping values outside the range ``[vmin, vmax]``.

        See the *clip* parameter in `.Normalize`.
        """
        ...

    def _changed(self):
        """
        Call this whenever the norm is changed to notify all the
        callback listeners to the 'changed' signal.
        """
        ...


    def __call__(self, value, clip=None):
        """
        Normalize the data and return the normalized data.

        Parameters
        ----------
        value
            Data to normalize.
        clip : bool, optional
            See the description of the parameter *clip* in `.Normalize`.

            If ``None``, defaults to ``self.clip`` (which defaults to
            ``False``).

        Notes
        -----
        If not already initialized, ``self.vmin`` and ``self.vmax`` are
        initialized using ``self.autoscale_None(value)``.
        """
        ...

    def inverse(self, value):
        """
        Maps the normalized value (i.e., index in the colormap) back to image
        data value.

        Parameters
        ----------
        value
            Normalized value.
        """
        ...


    def autoscale(self, A):
        """Set *vmin*, *vmax* to min, max of *A*."""
        ...

    def autoscale_None(self, A):
        """If *vmin* or *vmax* are not set, use the min/max of *A* to set them."""
        ...

    def scaled(self):
        """Return whether *vmin* and *vmax* are both set."""
        ...

and some of the docstrings will need to be updated to also allow for MultiNorm, but this can happen in the next PR.

This requires a lot more than the bare minimum. I tested, and I can do a plt.imshow() with just __call__ and autoscale_None, but I get the feeling that it is desirable to lock it down more.

class Norm(Protocol):
callbacks: cbook.CallbackRegistry
@property
def vmin(self) -> float | None: ...
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't we need something like this to support MultiNorm?

Suggested change
def vmin(self) -> float | None: ...
def vmin(self) -> float | ArrayLike | None: ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes,

My logic was to get this into main first, and then change this with the MultiNorm PR, but I guess it is easier if I do it here. I will try to find the time early next week.

Is this PR otherwise as you would expect?

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 should be updated now, I set it to:

    @property
    def vmin(self) -> float | tuple[float] | None: ...
    @property
    def vmax(self) -> float | tuple[float] | None: ...
    @property
    def clip(self) -> bool | tuple[bool]: ...

@timhoffm
Copy link
Member

timhoffm commented Jun 9, 2025

Considering this again, I'm leaning slightly towards an abstract base class.

To be clear, both variants are acceptable solutions for the use case and would do the job. The relevant arguments for me are:

  • inheritance is a slightly stronger in communicating the interface: (i) it's made explicit on the class through inheritance - this is by design implicit on protocols, where the implementing class does not state its protocol conformance. (ii) conformance is checked at load time, whereas protocols are only statically type checked and optionally in runtime code.
  • Protocols shine when you want to maintain looser coupling through duck-typing. But we don't need that: For everything internal, we can afford the stronger coupling. Also third parties implementing norms can do that: They have until now for Normalize and somebody who provides a norm can at least afford to conditionally import matplotlib.
  • the usage of runtime_checkable and the test here indicates to me that we are actually more on the side of nominal subtyping, I.e. inheritance.

Since I missed the discussion: What were the reasons to suggest unsung Protocols?

@ksunden
Copy link
Member

ksunden commented Jun 12, 2025

The main reasoning to avoid ABCs was to avoid having to deal with metaclasses, since that is often more painful than helpful. Protocols get us to essentially the same place without having to worry about that aspect.

@trygvrad
Copy link
Contributor Author

@timhoffm Could you take a look at the alternative PR #30178, and see if you still prefer an ABC after considering the implementation details?

I think with the right argument we can get everyone on the same page :)

@timhoffm
Copy link
Member

@trygvrad Thanks for taking the effort to write out both alternatives! I still like the ABC approach a bit more.

Could you please add Norm to the docs here to see how that renders.

@ksunden I think ABC is much simpler than a generic metaclass. The only complication I see is that one would have to watch out in case of multiple inheritance - But I don't see a case where norms would need multiple inheritance. They are quite straight forward.

@trygvrad
Copy link
Contributor Author

@timhoffm I'm not able to work on this now, but I will try to get to it at the end of the week.

@tacaswell If i recall you had a preference for a Protocol at the weekly meeting. What is your opinion now, in light of @timhoffm arguments here #30149 (comment) ?

@tacaswell tacaswell modified the milestone: v3.11.0 Jun 19, 2025
@tacaswell
Copy link
Member

Closing in favor of #30178 (see #30178 (comment))

Thank you @trygvrad for working up both versions!

@tacaswell tacaswell closed this Jun 19, 2025
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.

5 participants