Skip to content

Commit

Permalink
Improve Colorbar for supporting numpy dtypes (#1782)
Browse files Browse the repository at this point in the history
* Update map_widgets.py

Worked with vis_params 'min' and 'max':
- Accept types other than (int, float), for example numpy.float32. In deed, anything that can be converted to float is accepted.
- Improved error messages in case 'min', 'max' can not be converted to float.

* Change ValueError to TypeError

* Fix unit test errors

---------

Co-authored-by: Qiusheng Wu <giswqs@gmail.com>
  • Loading branch information
rodrigo-j-goncalves and giswqs authored Oct 18, 2023
1 parent d2024d1 commit 9782032
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
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

0 comments on commit 9782032

Please sign in to comment.