Export to GitHub

django-tagging - issue #91

utils.calculate_cloud can leave font_size unset for most frequent tag


Posted on Jan 30, 2008 by Swift Bear

With a logarithmic distribution, _calculate_tag_weight could return a weight greater than the maximum threshold, and the most frequent tag could end up without a font_size attribute.

This patch changes _calculate_tag_weight to never return a weight greater than max_weight.

Attachments

Comment #1

Posted on Feb 24, 2008 by Happy Rabbit

I just encountered this myself. There is a subtle rounding error that leads to the condition as described by recalcitrare. A very cursory analysis shows that this happens around 10% of the time (at least up through tag counts of up to 1000).

Comment #2

Posted on Mar 10, 2008 by Quick Hippo

I experienced this myself. Say that in utils._calculate_tag_weight(weight, max_weight, distribution) you have weight = 6.0, max_weight = 6.0 then the expression math.log(weight) * max_weight / math.log(max_weight) would return 6.000000000000009 which is greater than 6.0 and it would fail to compare True in calculate_cloud.

Comment #3

Posted on May 6, 2008 by Quick Bird

Just ran into this issue. To clarify for others in case they see it too: this originated as an AttributeError exception in one of my views.

AttributeError: 'Tag' object has no attribute 'font_size'

This occurred after doing Tag.objects.cloud_for_model(Model, min_count=2) and then trying to sort the results by 'font_size' attribute.

The patch does the trick. Thanks!

Comment #4

Posted on Jun 25, 2008 by Grumpy Rhino

I've hit this too. I had come up with this patch to fix it though I think I like recalcitrare's patch better.

{{{ --- utils.py (revision 132) +++ utils.py (working copy) @@ -252,10 +252,19 @@ max_weight = float(max(counts)) thresholds = _calculate_thresholds(min_weight, max_weight, steps) for tag in tags: - font_set = False tag_weight = _calculate_tag_weight(tag.count, max_weight, distribution) for i in range(steps): - if not font_set and tag_weight <= thresholds[i]: + if tag_weight <= thresholds[i]: tag.font_size = i + 1 - font_set = True + break + else: + # With a logarithmic distribution, it is possible with + # to get a tag_weight that is slight greater-than + # thresholds[-1], e.g.: + # counts = [1, 2, 6, 1] + # thresholds = [2.25, 3.5, 4.75, 6.0] + # tag_weight for count 6 = 6.0000000000000009 + # The result is an unexpected tag.font_size = None. + # Guard against that. + tag.font_size = steps return tags }}}

Jonathan or Jacob, would it be possible to get recalcitrare's patch commited?

Comment #5

Posted on Jun 27, 2008 by Massive Rabbit

i ran into this problem to, after spending hours trying to figure out what i am doing wrong i realized to problem must be inside django-tagging. i attached an extremly simple patch ;-)

Attachments

Comment #6

Posted on Sep 11, 2008 by Quick Giraffe

I ran into this problem too, thought it was in my own code initially but using werkzeug traced it down to the _calculate_tag_weight just as recalcitrare found.

There's a couple of patches here, but I think I like recalcitrare's one as well. Any chance to get this committed? Thanks.

Comment #7

Posted on Oct 23, 2008 by Swift Panda

Just hit this one myself. +1 to a patch being committed.

Comment #8

Posted on Oct 31, 2008 by Quick Rhino

Just hit this one myself

Comment #9

Posted on Nov 17, 2008 by Quick Kangaroo

I've also encoutered this issue.

Both "dtcw.patch" and "missing_size_fix.patch" are good solutions, although "dtcw.patch" has the least lines of code, so +1 to "dtcw.patch".

Comment #10

Posted on Feb 27, 2009 by Quick Giraffe

My apologies for posting several times on this topic, but I believe something needs to be done about this bug. This bug was first reported in Jan 2008, which makes it over a year old. There is a patch "dtcw.patch", which several people, including myself have tested without any problems. The bug is classed as medium priority, however I get this bug so often I believe it should really be classed as a high priority bug. At the moment for me, django-tagging is not usable in any of my apps unless this patch is manually applied.

Please anyone, apply this patch and close this bug if possible.

Comment #11

Posted on Apr 17, 2009 by Happy Panda

Yes, please apply this patch already :)

Comment #12

Posted on Aug 10, 2009 by Happy Cat

I also encoutered this issue too, and spend a while until I realised it was a bug on django-tagging. "dtcw.patch" is a nice fix, please go ahead and commit it. Thanks.

Comment #13

Posted on Nov 18, 2009 by Helpful Cat

I've encoutered this issue too. I open a new issue (sorry for that) and added a new patch on issue #223.

Comment #14

Posted on Dec 23, 2009 by Grumpy Camel

Just ran into this issue.

For me, a repeatable test condition is:

import tagging.utils as utils utils._calculate_tag_weight(weight=26, max_weight=26, distribution=tagging.utils.LOGARITHMIC) 26.000000000000004

And since that tag's weight is never <= the last threshold (if the endpoint threshold is at 26), the font_size never gets set and the tag cloud blows up the page/application. This bug is particularly insidious in that all my test cases, even ones testing tags, succeeded just fine because the bug only shows up with how various numbers handle conversion into floating point numbers.

Comment #15

Posted on Jan 14, 2010 by Happy Bear

I just ran into this. dtcw.patch fixed it for me.

Comment #16

Posted on Feb 17, 2010 by Swift Rabbit

Faced to the same issue. dtcw.patch solved the problem.

Comment #17

Posted on Aug 30, 2010 by Quick Giraffe

I'm a bit disappointed how long it has taken for this patch to be approved, a fix was released 2 years that works perfectly for so many people, yet it hasn't been committed. please commit dtcw.patch

Comment #18

Posted on Mar 3, 2012 by Quick Giraffe

This issue is fixed in the Ubuntu repository (Oneiric at least) version of python-django-tagging, but not here on Google Code, I was really hoping this could be patched here too.

Status: New

Labels:
Type-Defect Priority-Medium