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

Improve Colorbar for supporting numpy dtypes #1782

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions geemap/map_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from . import common

from traceback import format_tb


def _set_css_in_cell_output():
display(
Expand Down Expand Up @@ -109,9 +111,9 @@ def __init__(
Raises:
TypeError: If the vis_params is not a dictionary.
ValueError: If the orientation is not either horizontal or vertical.
ValueError: If the provided min value is not scalar type.
ValueError: If the provided max value is not scalar type.
ValueError: If the provided opacity value is not scalar type.
ValueError: If the provided min value is not convertible to float.
ValueError: If the provided max value is not convertible to float.
ValueError: If the provided opacity value is not convertible to float.
ValueError: If cmap or palette is not provided.
"""

Expand All @@ -138,16 +140,22 @@ def __init__(
width, height = self._get_dimensions(orientation, kwargs)

vmin = vis_params.get("min", kwargs.pop("vmin", 0))
if type(vmin) not in (int, float):
raise TypeError("The provided min value must be scalar type.")
try:
vmin = float(vmin)
except ValueError as err:
raise ValueError("The provided min value must be scalar type.")

vmax = vis_params.get("max", kwargs.pop("mvax", 1))
if type(vmax) not in (int, float):
raise TypeError("The provided max value must be scalar type.")
try:
vmax = float(vmax)
except ValueError as err:
raise ValueError("The provided max value must be scalar type.")

alpha = vis_params.get("opacity", kwargs.pop("alpha", 1))
if type(alpha) not in (int, float):
raise TypeError("The provided opacity or alpha value must be type scalar.")
try:
alpha = float(alpha)
except ValueError as err:
raise ValueError("opacity or alpha value must be scalar type.")

if "palette" in vis_params.keys():
hexcodes = common.to_hex_colors(common.check_cmap(vis_params["palette"]))
Expand Down
6 changes: 3 additions & 3 deletions tests/test_map_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ def test_colorbar_min_max(self):
self.normalize_class_mock.assert_called_with(vmin=-1.5, vmax=1)

def test_colorbar_invalid_min(self):
with self.assertRaisesRegex(TypeError, "min value must be scalar type"):
with self.assertRaisesRegex(ValueError, "min value must be scalar type"):
map_widgets.Colorbar(vis_params={"min": "invalid_min"})

def test_colorbar_invalid_max(self):
with self.assertRaisesRegex(TypeError, "max value must be scalar type"):
with self.assertRaisesRegex(ValueError, "max value must be scalar type"):
map_widgets.Colorbar(vis_params={"max": "invalid_max"})

def test_colorbar_opacity(self):
Expand All @@ -218,7 +218,7 @@ def test_colorbar_alpha(self):

def test_colorbar_invalid_alpha(self):
with self.assertRaisesRegex(
TypeError, "opacity or alpha value must be type scalar"
ValueError, "opacity or alpha value must be scalar type"
):
map_widgets.Colorbar(alpha="invalid_alpha", colors=self.TEST_COLORS)

Expand Down