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

HeatMap doesn't use weights #1271

Closed
ibayer opened this issue Mar 12, 2020 · 23 comments · Fixed by #1282
Closed

HeatMap doesn't use weights #1271

ibayer opened this issue Mar 12, 2020 · 23 comments · Fixed by #1282
Labels
bug An issue describing unexpected or malicious behaviour

Comments

@ibayer
Copy link

ibayer commented Mar 12, 2020

I spend quite some time tracking down this bug. I think it should be fixed or the limitation should be clearly stated in the docs. This is a major time waster and makes a bad impression on this otherwise great package.

This is a major bug coming for the upstream Leaflet.heat project.

Problem description

The plugins.HeatMap states that the points used to create the heat map can
be weighted.

data (list of points of the form [lat, lng] or [lat, lng, weight])

However weights are simply ignored. This makes it useless for heatmaps that are based on a few points with different weights.

Expected Output

Weight should have impact. Or remove this option from the docs.

@Conengmo Conengmo added the bug An issue describing unexpected or malicious behaviour label Apr 7, 2020
@Conengmo Conengmo mentioned this issue Apr 7, 2020
2 tasks
@Conengmo
Copy link
Member

Conengmo commented Apr 7, 2020

Hi @ibayer , sorry to hear this bug caused you some issues. I opened a PR which applies a patch from the leaflet.heat repo. Can you perhaps help by testing the PR, see if it solves the issue for you?

Alternatively, if we don't get a fix working, we should indeed update the documentation.

@Conengmo Conengmo added the work in progress Work is in progress on a PR, check the PR to see its status label Apr 7, 2020
@ibayer
Copy link
Author

ibayer commented Apr 9, 2020

@ibayer

Thanks for looking into this! Sorry, project has moved on and I don't have any code handy to
reproduce / test your fix. When I get the opportunity I'll for sure give folium another shot in the future but that might take a while.

@Conengmo
Copy link
Member

Conengmo commented Apr 9, 2020

No problem, then thanks for the bug report :)

@MiguelRodriguezAnticona

Hi i Have the same problem so far, what is the solution?

@Conengmo
Copy link
Member

We have a fix pending, I reckon it will be in the next release. In the meantime you could test it out by manually applying it: https://github.com/python-visualization/folium/pull/1282/files

@MiguelRodriguezAnticona

Conengmo this work in R?

@Conengmo
Copy link
Member

No this is a Python library

@fcs30
Copy link

fcs30 commented Jun 4, 2020

Hello! I have the same problem - is the best solution still manually applying it? When is the next released planned for?

@Conengmo
Copy link
Member

Conengmo commented Jun 5, 2020

We have a fix pending that I have to finish up. When that's merged you can install folium from the master branch. A new release (v0.12.0) is not yet scheduled, but will follow reasonably soon.

You could help out by testing the fix and reporting if it worked for you: #1282

@rbiele02
Copy link

rbiele02 commented Aug 5, 2020

Still not working correctly. I would expect another output.
Please do try the following code to see the problem:

m = folium.Map((0.5, 0.5), zoom_start=8, tiles=None)
HeatMap(
# make five dots with different weights: 1, 2, 3, 4 and 10
data=[(0, 0, 1),
(0.5, 0, 2),
(1.0, 0, 3),
(1.5, 0, 4),
(2.0, 0, 10)] , radius=15).add_to(m)
m

Just on sphere is bigger, there is no continuous gradient between 1 and 10.

@Conengmo
Copy link
Member

@rbiele02 which folium version are you using?

@rbiele02
Copy link

Thanks @Conengmo for looking into this!
I am using Version: 0.11.0+19.g46f820e.
Somehow it uses weights, but not correctly. In the attached figure you can see that 4 circles are equally weighted and one has higher intensity. However I would expect something else when choosing the weights 1,2,3,4 and 10.
A similar behaviour can be found for example for weights 0.1, 0.2, 0.3, 0.4, 0.8. The first 4 are equally and only the last has a higher intensity.

Screenshot 2020-08-12 at 14 02 15

@Conengmo
Copy link
Member

Weights in heatmaps can work a bit non-intuitive. I suspect that one value of 10 is so high that the other values are all small relatively. Please try a range where the values are closer together and see how that looks. For example some values between 1 and 2.

We have a test that verifies the behavior when using weights, so I'm fairly sure it's not broken. In it we create four points with weights 1, 1, 1.5 and 2: https://github.com/python-visualization/folium/pull/1282/files#diff-b6e9c94435e2e8e2a966d436a06a52b6R16 The result looks like this: https://github.com/Conengmo/folium/blob/cce48dc1fd46e23fefd84d6011c6a132f08ad006/tests/selenium/test_heat_map_selenium_screenshot.png

@rbiele02
Copy link

Thanks for the fast response.
When I use weights of {1, 2, 3, 4, 5} it seems to work.
1_2_3_4_5

However, when I use {0.1, 0.2, 0.3, 0.4, 0.5} I dont see any effect at all.
0 1_0 2_0 3_0 4_0 5

Would be nice to know in which range the weights needs to be to see weighting having effect. It might be a good idea to normalise the given weights to the range where it works.

@Conengmo
Copy link
Member

Can you try {0.5, 1, 1.5} ? Maybe it assumes a minimum weight of 1...

Looking at the plugin documenation it seems to suggest values below 1 are valid: https://github.com/Leaflet/Leaflet.heat

I'm hesitant to do normalization, because I don't think we can determine an optimal range automatically. That depends not only on the weights, but also on the locations of the points, the zoom level and the radius settings.

@rbiele02
Copy link

{0.5, 1, 1.5} works very well!
{0, 0.5, 1.0} not really. Maybe it needs to be centred around 1? Also {0.8,1.0,1.2} looks reasonable.

I can shift my distribution to have a mean of 1. This might work.
Thanks a lot!

@Conengmo
Copy link
Member

I think it's safe to say it assumes a default weight of 1 then.

@Clemmarchal
Copy link

Clemmarchal commented Aug 13, 2020

Hi, I am new to this subject but I've tried playing with the min_opacity and the max_value and this works fine :

m = folium.Map((0.5, 0.5), zoom_start=8, tiles=None)
HeatMap(
# make five dots with different weights: 1, 2, 3, 4 and 5
data=[(0  , 0, 0.1),
      (0.5, 0, 0.2),
      (1.0, 0, 0.3),
      (1.5, 0, 0.4),
      (2.0, 0, 0.5)] , radius=25, blur = 10, min_opacity = 0, max_val = 0.0005).add_to(m)
m

It seems that max_value works like a normalisation. So if you want to display value between 1 and 5, you have to put the max_value at 0.005 and so on...

I'm using follium 0.11.0

@Conengmo
Copy link
Member

Conengmo commented Aug 13, 2020

Note that you're using v0.11.0 and we were talking about the master branch, which contains a fix for heatmaps and changes its behavior slightly. With the fix, max_val is deprecated.

@Clemmarchal
Copy link

Clemmarchal commented Aug 13, 2020

Thanks @Conengmo I didn't get it. Your patch automatically set the max_val depending on the intensity of the data ?
How can I use it ?

@Conengmo
Copy link
Member

You can install folium with pip from the master branch like this: pip install git+https://github.com/python-visualization/folium.git

@BriceChivu
Copy link

BriceChivu commented Aug 19, 2021

Hello,
As of today (folium 0.12.1), the heatmap is still showing some unexpected behaviors. Below is an example.
image

  1. Barcelona should be shown "hotter" than Augsburg but they appear to be of the same "heat".
  2. NL looks "hotter" than Augsburg and Barcelona even though its weight is only 5 (2+3). My guess is that the number of locations is much more important than their weight.
    image

Edit for point 1): I believe the disparity between the different volumes is too high. Taking the log2 of the column "Volume" seems to give more expected results. Point 2) is still relevant.

@BriceChivu
Copy link

BriceChivu commented Aug 19, 2021

Following my last comment, it seems that there are also different behaviors depending on the zoom of the map (see screenshot below - look at Polska and Katowice).
image
image
image
image

@Conengmo Conengmo removed the work in progress Work is in progress on a PR, check the PR to see its status label Nov 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An issue describing unexpected or malicious behaviour
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants