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

feat: Adding addition of 0 #370

Merged
merged 1 commit into from
Jul 10, 2020
Merged

feat: Adding addition of 0 #370

merged 1 commit into from
Jul 10, 2020

Conversation

henryiii
Copy link
Member

@henryiii henryiii commented Jun 3, 2020

Adding with a false-like, such as 0 or None, is now supported. This allows syntax like sum(histogram_list) without answering the hard question of what it means to add a constant to a histogram with flow bins and unevenly sized bins. That can be done much more clearly using the view or [...] = syntax.

Closes #261.

@henryiii henryiii changed the title Adding addition of 0 feat: Adding addition of 0 Jun 4, 2020
@henryiii henryiii requested a review from HDembinski June 4, 2020 01:19
@henryiii
Copy link
Member Author

henryiii commented Jun 5, 2020

@HDembinski, thoughts? Is this fine?

@HDembinski
Copy link
Member

It is weird that adding None and False also works. Adding 0 makes sense, but why allow the others?

@henryiii
Copy link
Member Author

henryiii commented Jun 5, 2020

Partially because it is natural in construction (you can just check truthiness, and don't have to require a specific type check; any false-like does not affect the histogram when added - you can have WeightedSum(0,0) be false-like an then it would be immediately included). We could do type checks though if it's better to restrict things like None + hist == hist. Let me know if you want it restricted, and I'll do it.

False will always work, as it's a subclass of integer with value 0 (It was just an integer before Python 2.2 or 2.3, IIRC). 1 + False is valid Python syntax. As is 1 + True == 2. isinstance(False, int) is True.

@HDembinski
Copy link
Member

Ok, I understand better now. If the argument to __radd__ is not another histogram, I would again push the responsibility to what should happen to the numpy view of the internal storage. This way, we get all the numpy broadcasting rules automatically consistent, including broadcasting of scalars.

The question then again is what should happen with the flow bins. The consistent thing is to include them, since the other operators also work on the flow bins.

@henryiii henryiii marked this pull request as draft June 7, 2020 02:01
@henryiii
Copy link
Member Author

henryiii commented Jun 7, 2020

Okay, will place on hold until we have more support for operators on views. We need at least addition then for all views. Is it legal to add a double/int to each of the 3 accumulators?

@HDembinski
Copy link
Member

HDembinski commented Jul 1, 2020

Is it legal to add a double/int to each of the 3 accumulators?

That should work for WeightedSum out of the box, but not for Mean or WeightedMean.

@henryiii
Copy link
Member Author

henryiii commented Jul 2, 2020

So Mean and WeightedMean should not support sum(histogram_list)?

@HDembinski
Copy link
Member

They should, but they cannot support addition of constants in general, unless we find a reasonable interpretation of adding a number to a mean. So far, we should only allow the addition of 0, which we define to do nothing.

@HDembinski
Copy link
Member

You probably already know this:

import numpy as np
a = np.zeros(3)
a + 0 # ok
a + False # ok, False interpreted as zero
a + None # Error: unsupported operand type(s) for +: 'float' and 'NoneType'

We should also raise an error in case someone adds None.

@henryiii
Copy link
Member Author

henryiii commented Jul 6, 2020

Okay, we should just check for int/float 0 and only allow that, then.

@henryiii henryiii marked this pull request as ready for review July 10, 2020 23:03
@henryiii henryiii merged commit 8901037 into develop Jul 10, 2020
@henryiii henryiii deleted the henryiii/addition branch July 10, 2020 23:55
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

Successfully merging this pull request may close these issues.

Allow adding constants
2 participants