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

Math error in BinaryPower example in User Guide #608

Closed
tzelleke opened this issue Feb 22, 2022 · 2 comments
Closed

Math error in BinaryPower example in User Guide #608

tzelleke opened this issue Feb 22, 2022 · 2 comments
Milestone

Comments

@tzelleke
Copy link

The BinaryPower class in the Specialized Parameter types subsection of the user guide does not validate binary powers but merely divisibility by 2.

https://param.holoviz.org/user_guide/Parameters.html#specialized-parameter-types

Another issue is that this Parameter class does not respect None values.

If you want to keep this example based on param.Parameter you might update it to:

    def _validate_value(self, val, allow_None):
        if allow_None and val is None:
            return
        
        super(BinaryPower, self)._validate_value(val, allow_None)
        if not isinstance(val, numbers.Number):
            raise ValueError("BinaryPower parameter %r must be a number, "
                             "not %r." % (self.name, val))
        
        if (not float(val).is_integer()) or (val < 2) or (bin(int(val)).count("1") is not 1):
            # in Python >= 3.10 use `int(val).bit_count() is not 1`
            raise ValueError("BinaryPower parameter %r must be a power of 2, "
                             "not %r." % (self.name, val))

I would rather suggest to have the example based on param.Integer with default=2... but that does not read so well with the way this section is structured currently ?

@jlstevens
Copy link
Contributor

jlstevens commented May 30, 2022

Thanks for reporting this!

In #634 I address this by keeping the current code and changing the example to one that only allows even integers. The purpose of the docs here is to show that parameters can be defined easily and not to have people think too much about the precise validation logic itself.

@jlstevens
Copy link
Contributor

Addressed in #634 which is now merged.

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

No branches or pull requests

3 participants