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 error message when validating vis_params #1771

Closed
rodrigo-j-goncalves opened this issue Oct 12, 2023 · 2 comments
Closed

improve error message when validating vis_params #1771

rodrigo-j-goncalves opened this issue Oct 12, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@rodrigo-j-goncalves
Copy link
Contributor

Environment Information


          Date : Thu Oct 12 17:37:37 2023 CEST
            OS : Linux
        CPU(s) : 24
       Machine : x86_64
  Architecture : 64bit
           RAM : 62.5 GiB
   Environment : Python
   File system : ext4

Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]

        geemap : 0.27.1
            ee : 0.1.370
    ipyleaflet : 0.17.4
        folium : 0.14.0
    jupyterlab : 4.0.7
      notebook : Module not found
     ipyevents : 2.0.1
     geopandas : 0.14.0
localtileserver : 0.7.1

Description

I'm adding a layer of a single-band raster file.
This includes the following code:


1. Map = geemap.Map()
2. Map.add_raster(filename, cmap='terrain', layer_name="DEM")
3. vis_params = {'min': 0, 'max': 4000, 'palette': 'terrain'}
4. Map.add_colorbar(vis_params, label='Elevation (m)')
5. Map

However, since I want to automate this task, I want to use the raster max and min values instead of coding the numbers by hand. Thus, I used variables rastermin and rastermax I got from the raster statistics:

3. vis_params = {'min': rastermin, 'max': rastermax, 'palette': 'terrain'}

But I get an error: TypeError: The provided min value must be scalar type.
However, the variables are in fact scalar. When inspecting the code a little deeper I note that map_widgets.py only asks if the max and min variables are either int or float:

83   vmin = vis_params.get("min", kwargs.pop("vmin", 0))
84   if type(vmin) not in (int, float):
85        raise TypeError("The provided min value must be scalar type.")

My min and max variables are scalar, just a different type of float:

type(rastermin)
numpy.float32

And to confirm that this is the problem, this works fine:
vis_params = {'min': float(rastermin), 'max': float(rastermax), 'palette': 'terrain'}

Conclusion

I see two alternatives

  1. Change the error message:
    To match the behavior of the code and change the error message to
    TypeError: The provided min/max values can only be 'int' or 'float'.

  2. Allow other types:
    Allow float32 (and possible other numpy types?). Maybe this could be done by forcing the conversion to int or float as I did above.



Acknowledgement

Thanks a lot for this great library!

@rodrigo-j-goncalves rodrigo-j-goncalves added the bug Something isn't working label Oct 12, 2023
@giswqs
Copy link
Member

giswqs commented Oct 12, 2023

Good suggestion! I would encourage you to submit a pull request so that you can become a contributor to the project.

@giswqs
Copy link
Member

giswqs commented Oct 19, 2023

Fix in #1782

@giswqs giswqs closed this as completed Oct 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants