Skip to content

HeatMap doesn't use weights #1271

Closed

Description

@ibayer

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.

Activity

  1. added
    bug
    on Apr 7, 2020
  2. Conengmo commented on Apr 7, 2020

    @Conengmo
    Member

    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.

  3. added
    work in progress
    on Apr 7, 2020
  4. ibayer commented on Apr 9, 2020

    @ibayer
    Author

    @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.

  5. Conengmo commented on Apr 9, 2020

    @Conengmo
    Member

    No problem, then thanks for the bug report :)

  6. MiguelRodriguezAnticona commented on May 14, 2020

    @MiguelRodriguezAnticona

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

  7. Conengmo commented on May 14, 2020

    @Conengmo
    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

  8. MiguelRodriguezAnticona commented on May 14, 2020

    @MiguelRodriguezAnticona

    Conengmo this work in R?

  9. Conengmo commented on May 14, 2020

    @Conengmo
    Member

    No this is a Python library

  10. fcs30 commented on Jun 4, 2020

    @fcs30

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

  11. Conengmo commented on Jun 5, 2020

    @Conengmo
    Member

    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

  12. rbiele02 commented on Aug 5, 2020

    @rbiele02

    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.

  13. Conengmo commented on Aug 12, 2020

    @Conengmo
    Member

    @rbiele02 which folium version are you using?

  14. rbiele02 commented on Aug 12, 2020

    @rbiele02

    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

  15. Conengmo commented on Aug 12, 2020

    @Conengmo
    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

  16. rbiele02 commented on Aug 12, 2020

    @rbiele02

    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.

  17. Conengmo commented on Aug 12, 2020

    @Conengmo
    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.

  18. rbiele02 commented on Aug 12, 2020

    @rbiele02

    {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!

  19. Conengmo commented on Aug 12, 2020

    @Conengmo
    Member

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

  20. Clemmarchal commented on Aug 13, 2020

    @Clemmarchal

    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

  21. Conengmo commented on Aug 13, 2020

    @Conengmo
    Member

    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.

  22. Clemmarchal commented on Aug 13, 2020

    @Clemmarchal

    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 ?

  23. Conengmo commented on Aug 13, 2020

    @Conengmo
    Member

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

  24. BriceChivu commented on Aug 19, 2021

    @BriceChivu

    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.

  25. BriceChivu commented on Aug 19, 2021

    @BriceChivu

    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

  26. removed
    work in progress
    on Nov 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions