opendrift icon indicating copy to clipboard operation
opendrift copied to clipboard

import cmocean failing with latest matplotlib

Open rsignell opened this issue 1 year ago • 12 comments

Using the latest packages from conda-forge,

o = OceanDrift()

fails with

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[1], line 2
      1 from datetime import datetime, timedelta
----> 2 from opendrift.models.oceandrift import OceanDrift
      4 # Basic ocean drift module: current + 2% of wind
      5 o = OceanDrift(loglevel=50)

File /home/conda/global/f2dda3e0-1721739333-153-opendrift/lib/python3.11/site-packages/opendrift/models/oceandrift.py:21
     19 from scipy.interpolate import interp1d
     20 import logging; logger = logging.getLogger(__name__)
---> 21 from opendrift.models.basemodel import OpenDriftSimulation
     22 from opendrift.elements import LagrangianArray
     23 from opendrift.models.physics_methods import verticaldiffusivity_Large1994, verticaldiffusivity_Sundby1983, gls_tke, skillscore_liu_weissberg

File /home/conda/global/f2dda3e0-1721739333-153-opendrift/lib/python3.11/site-packages/opendrift/models/basemodel/__init__.py:27
     24 import logging
     25 import psutil
---> 27 from opendrift.models.basemodel.environment import Environment
     28 from opendrift.readers import reader_global_landmask
     30 logging.captureWarnings(True)

File /home/conda/global/f2dda3e0-1721739333-153-opendrift/lib/python3.11/site-packages/opendrift/models/basemodel/environment.py:13
     11 from opendrift.readers import reader_from_url, reader_global_landmask
     12 from opendrift.errors import NotCoveredError
---> 13 from opendrift.models import physics_methods as pm
     15 from opendrift.config import Configurable
     17 logger = logging.getLogger(__name__)

File /home/conda/global/f2dda3e0-1721739333-153-opendrift/lib/python3.11/site-packages/opendrift/models/physics_methods.py:24
     22 import matplotlib.pyplot as plt
     23 import pyproj
---> 24 import cmocean
     27 def wind_drift_factor_from_trajectory(trajectory_dict, min_period=None):
     28     '''Estimate wind_drift_fator based on wind and current along given trajectory
     29 
     30     trajectory_dict: dictionary with arrays of same length of the following variables:
   (...)
     33     Returns array of same length minus one of the fitted wind_drift_factor
     34     '''

File /home/conda/global/f2dda3e0-1721739333-153-opendrift/lib/python3.11/site-packages/cmocean/__init__.py:11
      8 from __future__ import absolute_import
     10 # from cmocean import *
---> 11 from . import cm, tools, data
     13 __all__ = ['cm',
     14            'tools',
     15            'plots',
     16            'data']
     18 __authors__ = ['Kristen Thyng <[email protected]>']

File /home/conda/global/f2dda3e0-1721739333-153-opendrift/lib/python3.11/site-packages/cmocean/cm.py:43
     41 rgb_with_alpha[:,3]  = 1.  #set alpha channel to 1
     42 reg_map = colors.ListedColormap(rgb_with_alpha, 'cmo.' + cmapname, rgb.shape[0])
---> 43 cm.register_cmap(cmap = reg_map)
     45 # Register the reversed map
     46 reg_map_r = colors.ListedColormap(rgb_with_alpha[::-1,:], 'cmo.' + cmapname + '_r', rgb.shape[0])

AttributeError: module 'matplotlib.cm' has no attribute 'register_cmap'

Looks like the problem is here: https://github.com/matplotlib/matplotlib/issues/28349

I took the "easy route" of pinning matplotlib to 3.7.3

rsignell avatar Jul 23 '24 14:07 rsignell

@kthyng I guess an update is then needed in cmocean?

knutfrode avatar Jul 23 '24 14:07 knutfrode

Oof, turns out cmocean fixed this problem a year ago, but for some reason I didn't get that version -- I have cmocean=2.0 in the environment I just built. I need to figure out which package in my environment is pinning that...

rsignell avatar Jul 23 '24 15:07 rsignell

I think that same realization process happened to me in my opendrift environment! I ending up deciding it might be OpenDrift's requirements, although I have read about poetry's notation and I don't think it should be that based on what it said. From my recollection though, in my env OpenDrift was the only package pinning cmocean at all at 2.0. I ended up updating the package locally in my env and moving on without saying anything since I wasn't sure that was the problem

kthyng avatar Jul 23 '24 16:07 kthyng

Aha! https://github.com/conda-forge/opendrift-feedstock/blob/main/recipe/meta.yaml#L32 (@ocefpaf found this)

rsignell avatar Jul 23 '24 16:07 rsignell

BTW, inspecting opendrift's dependencies, many of them looks like optional deps. Maybe those should be listed as such to make it easier people to install it?

ocefpaf avatar Jul 23 '24 16:07 ocefpaf

Of course @ocefpaf figures it out! Good thinking to look there.

kthyng avatar Jul 23 '24 16:07 kthyng

I updated to cmocean>=3.0, which is hopefully ok? https://github.com/conda-forge/opendrift-feedstock/blob/main/recipe/meta.yaml#L32

knutfrode avatar Jul 26 '24 13:07 knutfrode

@knutfrode If you are using numpy 2.0 you'll probably need v.3.1.3 to get the release correct because it always takes me a few releases to get everything right... https://github.com/matplotlib/cmocean/releases. I have cmocean set up differently than my other packages and always mess it up.

kthyng avatar Jul 26 '24 14:07 kthyng

OpenDrift presently pins numpy<2.0 since adios_db (oil library) is not yet adapted to numpy 2. This might be updated within a couple of months. Should I anyway change to cmocean>=3.1.3 right away?

knutfrode avatar Jul 26 '24 14:07 knutfrode

Yes I think that is a good idea.

kthyng avatar Jul 26 '24 15:07 kthyng

Ok, this is now updated.

knutfrode avatar Jul 26 '24 15:07 knutfrode

I updated to cmocean>=3.0, which is hopefully ok? https://github.com/conda-forge/opendrift-feedstock/blob/main/recipe/meta.yaml#L32

Please see my comment in https://github.com/conda-forge/opendrift-feedstock/commit/6fa5d0b6f10161d3e076b60bc5133013350f725e#r144694960

and https://conda-forge.org/docs/maintainer/updating_pkgs/#forking-and-pull-requests for more info.

Note that a new package was not created. The problem persists for those using conda-forge and it requires a build number bump to fix it.

ocefpaf avatar Jul 26 '24 15:07 ocefpaf