dlamotte / django-tagging

Automatically exported from code.google.com/p/django-tagging
Other
0 stars 0 forks source link

utils.calculate_cloud can leave font_size unset for most frequent tag #91

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
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.

Original issue reported on code.google.com by recalcit...@gmail.com on 30 Jan 2008 at 5:07

Attachments:

GoogleCodeExporter commented 8 years ago
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).  

Original comment by luftyl...@gmail.com on 24 Feb 2008 at 2:38

GoogleCodeExporter commented 8 years ago
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.

Original comment by fd.calab...@gmail.com on 10 Mar 2008 at 10:48

GoogleCodeExporter commented 8 years ago
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!

Original comment by jesse.l...@gmail.com on 6 May 2008 at 7:33

GoogleCodeExporter commented 8 years ago
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?

Original comment by tre...@gmail.com on 25 Jun 2008 at 10:42

GoogleCodeExporter commented 8 years ago
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 ;-)

Original comment by initcr...@gmail.com on 27 Jun 2008 at 8:08

Attachments:

GoogleCodeExporter commented 8 years ago
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.

Original comment by rob...@gmail.com on 11 Sep 2008 at 7:01

GoogleCodeExporter commented 8 years ago
Just hit this one myself.  +1 to a patch being committed.

Original comment by giles.th...@gmail.com on 23 Oct 2008 at 2:12

GoogleCodeExporter commented 8 years ago
Just hit this one myself

Original comment by pierrer...@yahoo.fr on 31 Oct 2008 at 11:03

GoogleCodeExporter commented 8 years ago
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".

Original comment by zera...@gmail.com on 17 Nov 2008 at 2:15

GoogleCodeExporter commented 8 years ago
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.

Original comment by rob...@gmail.com on 27 Feb 2009 at 7:25

GoogleCodeExporter commented 8 years ago
Yes, please apply this patch already :)

Original comment by real.hu...@mrmachine.net on 17 Apr 2009 at 8:12

GoogleCodeExporter commented 8 years ago
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.

Original comment by mcampo85@gmail.com on 10 Aug 2009 at 3:44

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

Original comment by arthur.furlan on 18 Nov 2009 at 2:59

GoogleCodeExporter commented 8 years ago
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.

Original comment by wamc...@gmail.com on 23 Dec 2009 at 5:43

GoogleCodeExporter commented 8 years ago
I just ran into this.  dtcw.patch fixed it for me.

Original comment by audr...@gmail.com on 14 Jan 2010 at 12:09

GoogleCodeExporter commented 8 years ago
Faced to the same issue. dtcw.patch solved the problem.

Original comment by sli...@gmail.com on 17 Feb 2010 at 8:16

GoogleCodeExporter commented 8 years ago
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

Original comment by rob...@gmail.com on 30 Aug 2010 at 10:52

GoogleCodeExporter commented 8 years ago
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.

Original comment by rob...@gmail.com on 3 Mar 2012 at 4:07