Skip to content

Commit

Permalink
Add linear_combination_of_weights()
Browse files Browse the repository at this point in the history
It is a safer alternative to reduce(lambda x, y: x + y[0] * y[1] / total, parts, 0.0) expression
which was requiring a separated calculation of total, prone to errors.
  • Loading branch information
zas committed Apr 28, 2014
1 parent c850e64 commit ab9fbd7
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions picard/util/__init__.py
Expand Up @@ -325,3 +325,24 @@ def is_hidden_path(path):
"""Returns true if at least one element of the path starts with a dot"""
path = os.path.normpath(path) # we need to ignore /./ and /a/../ cases
return any(s.startswith('.') for s in path.split(os.sep))


def linear_combination_of_weights(parts):
"""Produces a probability as a linear combination of weights
Parts should be a list of tuples in the form:
[(v0, w0), (v1, w1), ..., (vn, wn)]
where vn is a value between 0.0 and 1.0
and wn corresponding weight as a positive number
"""
total = 0.0
sum_of_products = 0.0
for value, weight in parts:
if value < 0.0:
raise ValueError, "Value must be greater than or equal to 0.0"
if value > 1.0:
raise ValueError, "Value must be lesser than or equal to 1.0"
if weight < 0:
raise ValueError, "Weight must be greater than or equal to 0.0"
total += weight
sum_of_products += value * weight
return sum_of_products / total

3 comments on commit ab9fbd7

@johnlane
Copy link

Choose a reason for hiding this comment

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

I have been getting a divide by zero error when doing "Lookup" on a cluster during the phase where the found release is returned. The entry shows on the screen as "[loading Album Information]". Once this happens, I have to exit and re-start because nothing works. After re-starting, looking up the same cluster works perfectly.

I made an ad-hoc modification to check the value of total before using it in the division expression on line 347. I have no idea if this is a good thing to do but, since I did it, there have been no divide-by-zero errors.

I don't know what the correct way to raise this is.

My replacement line is

return 0 if total == 0 else sum_of_products / total

@zas
Copy link
Collaborator Author

@zas zas commented on ab9fbd7 Sep 3, 2014

Choose a reason for hiding this comment

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

@johnlane : can you provide debug output (-d option) on a bug report on http://tickets.musicbrainz.org/browse/PICARD ?
Please post the full exception message too.

@johnlane
Copy link

Choose a reason for hiding this comment

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

@zas I have opened a ticket http://tickets.musicbrainz.org/browse/PICARD-630. I will update that ticket with the details you've requested (as soon as the problem recurs, probably tomorrow now...)

Please sign in to comment.