Repository navigation
Fix heatmap intensity #1282
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
Merged
Conengmo
merged 13 commits into
python-visualization:master
from
Conengmo:fix-heatmap-intensity
Jun 14, 2020
Merged
Fix heatmap intensity #1282
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9985dfd
Update link
Conengmo 97cdd15
update link, discard max_val
Conengmo f135e79
Update test_heat_map.py
Conengmo b406fd6
Update url after rebase
Conengmo 55d917a
Rename temp html file utility function
Conengmo b7ad417
temp_html_filepath accept str and bytes
Conengmo 7a06357
Refactor selenium tests
Conengmo 0428ca5
Small additional refactor of selenium fixture
Conengmo e794209
Add HeatMap with weights selenium test
Conengmo e99e7bc
Set window size at driver load
Conengmo cd2d227
Print screenshot if test fails
Conengmo a7fc4e6
export canvas, not full screenshot
Conengmo cce48dc
Use JS resource from folium master
Conengmo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
|
|
||
| import pytest | ||
| from selenium.webdriver import Chrome, ChromeOptions | ||
| from selenium.webdriver.common.by import By | ||
| from selenium.webdriver.support.ui import WebDriverWait | ||
| from selenium.webdriver.support.expected_conditions import visibility_of_element_located | ||
|
|
||
|
|
||
| @pytest.fixture(scope='session') | ||
| def driver(): | ||
| """Pytest fixture that yields a Selenium WebDriver instance""" | ||
| driver = DriverFolium() | ||
| try: | ||
| yield driver | ||
| finally: | ||
| driver.quit() | ||
|
|
||
|
|
||
| class DriverFolium(Chrome): | ||
| """Selenium WebDriver wrapper that adds folium test specific features.""" | ||
|
|
||
| def __init__(self): | ||
| options = ChromeOptions() | ||
| options.add_argument('--no-sandbox') | ||
| options.add_argument('--disable-dev-shm-usage') | ||
| options.add_argument('--disable-gpu') | ||
| options.add_argument('--headless') | ||
| options.add_argument("--window-size=1024,768") | ||
| super().__init__(options=options) | ||
|
|
||
| def get_file(self, filepath): | ||
| self.clean_window() | ||
| super().get('file://' + filepath) | ||
|
|
||
| def clean_window(self): | ||
| """Make sure we have a fresh window (without restarting the browser).""" | ||
| # open new tab | ||
| self.execute_script('window.open();') | ||
| # close old tab | ||
| self.close() | ||
| # switch to new tab | ||
| self.switch_to.window(self.window_handles[0]) | ||
|
|
||
| def verify_js_logs(self): | ||
| """Raise an error if there are errors in the browser JS console.""" | ||
| logs = self.get_log('browser') | ||
| for log in logs: | ||
| if log['level'] == 'SEVERE': | ||
| msg = ' '.join(log['message'].split()[2:]) | ||
| raise RuntimeError('Javascript error: "{}".'.format(msg)) | ||
|
|
||
| def wait_until(self, css_selector, timeout=10): | ||
| """Wait for and return the element(s) selected by css_selector.""" | ||
| wait = WebDriverWait(self, timeout=timeout) | ||
| is_visible = visibility_of_element_located((By.CSS_SELECTOR, css_selector)) | ||
| return wait.until(is_visible) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import base64 | ||
| import os | ||
|
|
||
| import folium | ||
| from folium.plugins.heat_map import HeatMap | ||
| from folium.utilities import temp_html_filepath | ||
|
Conengmo marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
| def test_heat_map_with_weights(driver): | ||
| """Verify that HeatMap uses weights in data correctly. | ||
|
|
||
| This test will fail in non-headless mode because window size will be different. | ||
|
|
||
| """ | ||
| m = folium.Map((0.5, 0.5), zoom_start=8, tiles=None) | ||
| HeatMap( | ||
| # make four dots with different weights: 1, 1, 1.5 and 2. | ||
| data=[ | ||
| (0, 0, 1.5), | ||
| (0, 1, 1), | ||
| (1, 0, 1), | ||
| (1, 1, 2), | ||
| ], | ||
| radius=70, | ||
| blur=50, | ||
| ).add_to(m) | ||
| html = m.get_root().render() | ||
| with temp_html_filepath(html) as filepath: | ||
| driver.get_file(filepath) | ||
| assert driver.wait_until('.folium-map') | ||
| driver.verify_js_logs() | ||
| canvas = driver.wait_until('canvas.leaflet-heatmap-layer') | ||
| assert canvas | ||
| # get the canvas as a PNG base64 string | ||
| canvas_base64 = driver.execute_script( | ||
| "return arguments[0].toDataURL('image/png').substring(21);", canvas) | ||
| screenshot = base64.b64decode(canvas_base64) | ||
| path = os.path.dirname(__file__) | ||
| with open(os.path.join(path, 'test_heat_map_selenium_screenshot.png'), 'rb') as f: | ||
| screenshot_expected = f.read() | ||
| if hash(screenshot) != hash(screenshot_expected): | ||
| print(screenshot) | ||
| assert False, 'screenshot is not as expected' | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.