From c4c18c1712c83d5e61dd857624cfb59a270ba25e Mon Sep 17 00:00:00 2001 From: Eli Talbert Date: Wed, 12 Jun 2024 12:43:40 -0400 Subject: [PATCH 1/2] change oloring default --- geochron/visualization/folium.py | 41 ++++++++++++++++++------------ tests/visualization/test_folium.py | 5 +--- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/geochron/visualization/folium.py b/geochron/visualization/folium.py index f5f7f18..7730623 100644 --- a/geochron/visualization/folium.py +++ b/geochron/visualization/folium.py @@ -104,26 +104,43 @@ def timehex_backgroundata(timehex: pd.DataFrame): return backgroundata -def add_hashmap_properties(original_hashmap:dict, time, cmap: Callable, opacity= float(.7)): +def add_hashmap_properties(original_hashmap:dict, time:pd.Timestamp, cmap: Callable, opacity: float): """ - Formats backgroundata polygons appropriate for a folium TimeSliderChoropleth from - a timehex dataframe + Adds properties to the hashmap necessary for display. Args: - timehex: A timehex dataframe - + original_hashmap: a hashmap + time: time of hashmap + cmap: a Branca colormap + opacity: desired opacity of shapes Returns: a GeoJson FeatureCollection """ new_dict = {} removed_empty = {key: value for key, value in original_hashmap.items() if value != 0} + + if len(removed_empty) == 0: + color_min = 0 + color_max = 0 + else: + max_key = max(removed_empty, key=removed_empty.get) + min_key = min(removed_empty, key=removed_empty.get) + color_min = removed_empty[min_key] + color_max = removed_empty[max_key] + + if cmap is None: + used_cmap = LinearColormap(colors=['blue','red'], \ + vmin= color_min, vmax= color_max) + else: + used_cmap = cmap + for key, value in removed_empty.items(): new_dict[key] = {'popup': 'weight= ' + str(value) + '
center(lat,lon)= ' + str(h3.h3_to_geo(key)), - 'time': time,'style':{'opacity': opacity, 'color': cmap(value)}} + 'time': time,'style':{'opacity': opacity, 'color': used_cmap(value)}} return new_dict -def timehex_timestampedgeojson(timehex: pd.DataFrame, cmap:Optional[Callable] = None): +def timehex_timestampedgeojson(timehex: pd.DataFrame, opacity= float(.7), cmap:Optional[Callable] = None): """ Formats a timehex into the correct data format for Folium's timestampedgeojson @@ -138,16 +155,8 @@ def timehex_timestampedgeojson(timehex: pd.DataFrame, cmap:Optional[Callable] = list_hashmaps = select_timehex.to_dict('records') polygon_list:list = [] - #color scale - max_color = select_timehex.values.max() - min_color = select_timehex.values.min() - if cmap is None: - used_cmap = LinearColormap(colors=['blue','red'], vmin=min_color, vmax=max_color) - else: - used_cmap = cmap - for hashmap, start_time in zip(list_hashmaps, start_time_list): - hashmap_properties= add_hashmap_properties(hashmap, start_time, used_cmap) + hashmap_properties= add_hashmap_properties(hashmap, start_time, cmap, opacity) polygons = {h3_to_geopolygon(k, properties= v) for k,v in hashmap_properties.items()} polygon_list.extend(polygons) diff --git a/tests/visualization/test_folium.py b/tests/visualization/test_folium.py index 6879d0f..7769675 100644 --- a/tests/visualization/test_folium.py +++ b/tests/visualization/test_folium.py @@ -131,11 +131,8 @@ def test_timehex_timestampedgeojson(): } df = pd.DataFrame(data) - # Prepare a sample colormap - cmap = LinearColormap(['blue', 'red'], vmin=1, vmax=4) - # Call the function with the sample dataframe and colormap - result = timehex_timestampedgeojson(df, cmap) + result = timehex_timestampedgeojson(df, .7) assert isinstance(result, dict) assert result['type'] == 'FeatureCollection' From 24fd74f606c5f5266f605a854d15e22572543534 Mon Sep 17 00:00:00 2001 From: Eli Talbert Date: Wed, 12 Jun 2024 13:01:56 -0400 Subject: [PATCH 2/2] deal with mypy errors --- geochron/visualization/folium.py | 11 ++++++----- tests/visualization/test_folium.py | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/geochron/visualization/folium.py b/geochron/visualization/folium.py index 7730623..51f56a4 100644 --- a/geochron/visualization/folium.py +++ b/geochron/visualization/folium.py @@ -104,7 +104,7 @@ def timehex_backgroundata(timehex: pd.DataFrame): return backgroundata -def add_hashmap_properties(original_hashmap:dict, time:pd.Timestamp, cmap: Callable, opacity: float): +def add_hashmap_properties(original_hashmap:dict, time:pd.Timestamp, opacity: float, cmap: Optional[Callable] = None): """ Adds properties to the hashmap necessary for display. @@ -123,9 +123,9 @@ def add_hashmap_properties(original_hashmap:dict, time:pd.Timestamp, cmap: Calla color_min = 0 color_max = 0 else: - max_key = max(removed_empty, key=removed_empty.get) - min_key = min(removed_empty, key=removed_empty.get) - color_min = removed_empty[min_key] + max_key = max(removed_empty, key=removed_empty.get) # type: ignore[arg-type] + min_key = min(removed_empty, key=removed_empty.get) # type: ignore[arg-type] + color_min = removed_empty[min_key] color_max = removed_empty[max_key] if cmap is None: @@ -156,7 +156,8 @@ def timehex_timestampedgeojson(timehex: pd.DataFrame, opacity= float(.7), cmap:O polygon_list:list = [] for hashmap, start_time in zip(list_hashmaps, start_time_list): - hashmap_properties= add_hashmap_properties(hashmap, start_time, cmap, opacity) + hashmap_properties= add_hashmap_properties(original_hashmap = hashmap,\ + time = start_time, opacity = opacity, cmap= cmap) polygons = {h3_to_geopolygon(k, properties= v) for k,v in hashmap_properties.items()} polygon_list.extend(polygons) diff --git a/tests/visualization/test_folium.py b/tests/visualization/test_folium.py index 7769675..cc47541 100644 --- a/tests/visualization/test_folium.py +++ b/tests/visualization/test_folium.py @@ -106,7 +106,7 @@ def cmap(value): opacity = 0.7 # Call the function with the test inputs - result = add_hashmap_properties(original_hashmap, time, cmap, opacity) + result = add_hashmap_properties(original_hashmap, time, opacity, cmap) # Define the expected result expected_result = { @@ -132,7 +132,7 @@ def test_timehex_timestampedgeojson(): df = pd.DataFrame(data) # Call the function with the sample dataframe and colormap - result = timehex_timestampedgeojson(df, .7) + result = timehex_timestampedgeojson(df) assert isinstance(result, dict) assert result['type'] == 'FeatureCollection'