ecmwf / climetlab

Python package for easy access to weather and climate data
Apache License 2.0
371 stars 58 forks source link

TypeError while saving a plot to SVG #30

Closed emadehsan closed 2 years ago

emadehsan commented 2 years ago

While using cml.plot_map with a path argument to save the plot as SVG, the following error appears, even though the plot is successfully saved to a .svg file. This error does not appear when saving the file to pdf or png.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-35-aab10829c1c0> in <module>()
      1 # cml.plot_map(data, title=True)
----> 2 cml.plot_map(data, title=True, path="test-grib-plot.svg")

/usr/local/lib/python3.7/dist-packages/climetlab/plotting/__init__.py in plot_map(data, **kwargs)
    176     p = new_plot(**kwargs)
    177     p.plot_map(data)
--> 178     p.show()
    179 
    180 

/usr/local/lib/python3.7/dist-packages/climetlab/plotting/__init__.py in show(self)
    120 
    121     def show(self):
--> 122         self.backend.show(display=display)
    123 
    124     def macro(self) -> list:

/usr/local/lib/python3.7/dist-packages/climetlab/plotting/backends/magics/backend.py in show(self, display)
    327             Display = Image  # noqa: N806
    328 
--> 329         return display(Display(path, metadata=dict(width=width)))
    330 
    331     def save(self, path):

TypeError: __init__() got an unexpected keyword argument 'metadata'

Steps to reproduce

# download the grib file that comes with ClieMetLab
!wget https://raw.githubusercontent.com/ecmwf/climetlab/develop/docs/examples/test.grib

Load the file and try to plot it with while saving also providing path for plot be saved in svg

import climetlab as cml

data = cml.load_source("file", "test.grib")

cml.plot_map(data, title=True, path="test-grib-plot.svg")
floriankrb commented 2 years ago

Thanks for the report, I cannot reproduce it on my environment.

Are you using the latest release 0.9.4? (from yesterday!) Can you run "climetlab versions" on a shell?

emadehsan commented 2 years ago

I'm using Google Colab notebook.

!climetlab versions

gives:

IPython            5.5.0
Magics             1.5.6
argparse           1.1
atexit             builtin
branca             0.3.1
bz2                python3.7
cdsapi             0.5.1
cgi                2.6
climetlab          0.9.4
cmd                python3.7
codc               [Errno 2] No such file or directory: '/usr/local/lib/python3.7/dist-packages/codc/processed_odc.h'
collections        python3.7
contextlib         python3.7
copy               python3.7
csv                1.0
ctypes             1.1.0
datetime           python3.7
dateutil           2.8.2
docutils           0.18
eccodes            1.3.3
ecmwfapi           1.6.1
ecmwflibs          0.3.14
email              python3.7
entrypoints        0.3
filelock           3.3.2
findlibs           0.0.2
fnmatch            python3.7
folium             0.8.3
ftplib             python3.7
functools          python3.7
getpass            python3.7
glob               python3.7
gribapi            2.24.0
gzip               python3.7
hashlib            python3.7
importlib          python3.7
importlib_metadata 4.8.1
inspect            python3.7
io                 python3.7
ipywidgets         7.6.5
itertools          builtin
jellyfish          0.8.9
jinja2             2.11.3
json               2.0.9
logging            0.5.1.2
lzma               python3.7
markdown           3.3.4
matplotlib         3.2.2
metview            missing
mimetypes          python3.7
numpy              1.19.5
oauthlib           3.1.1
os                 python3.7
pandas             1.1.5
pathlib            1.0.1
pdbufr             0.9.0
pkgutil            python3.7
platform           1.0.8
pyodc              1.1.1
pytest             3.6.4
pytz               2018.9
random             python3.7
re                 2.2.1
readline           python3.7
requests           2.23.0
requests_oauthlib  1.3.0
s3fs               missing
seaborn            0.11.2
shlex              python3.7
shutil             python3.7
sqlite3            2.6.0
stat               python3.7
subprocess         python3.7
sys                builtin
tarfile            0.9.0
tempfile           python3.7
tensorflow         2.7.0
termcolor          1.1.0
textwrap           python3.7
threading          python3.7
time               builtin
tqdm               4.62.3
traceback          python3.7
typing             python3.7
unittest           python3.7
urllib             python3.7
warnings           python3.7
weakref            python3.7
xarray             0.20.1
xml                python3.7
yaml               3.13
zarr               missing
zipfile            python3.7

Python 3.7.12 and CliMetLab 0.9.4

emadehsan commented 2 years ago

You can have a look at this Colab Notebook to see this error

floriankrb commented 2 years ago

Ok, this is because colab is using an old version of ipython (5.5). And we are testing only with the most recent versions of the packages (on python 3.7 to 3.10). I am not sure how much we want to support old version, but an error on collab is indeed annoying.

I see that the signature of the function "IPython/core/display.py:display()" changed.

We have in climetlab: from IPython.display import HTML, SVG, Image, Markdown, display

This may be the right place to put specific code to support older ipython versions. Do you want to address this with a PR?

emadehsan commented 2 years ago

Sure. Let me look into this

floriankrb commented 2 years ago

Please have a look at the latest commit on develop branch (https://github.com/ecmwf/climetlab/commit/d9736991a8adac36418238930ce7af577f922b89), we made a change there to fix the issue. The other functions (HTML, Image, Markdown, display) may need similar tweaks

emadehsan commented 2 years ago

The commit you referred to has fixed this problem for SVG. How can replicate this problem for other functions (HTML, Image, Markdown, display)?

floriankrb commented 2 years ago

The reason why I pointed to them is that if we want to ensure compatibility with the collab environment, we may need similar tweaks. I do not know if there is any issue with these other functions. "If it is not broken, don't fix it", I think we can close the current issue (adding more tests would be good otherwise but this is another point)

emadehsan commented 2 years ago

Got it. Thank you for time and guidance.