OpenDrift / opendrift

Open source framework for ocean trajectory modelling
https://opendrift.github.io
GNU General Public License v2.0
245 stars 120 forks source link

import cmocean failing with latest matplotlib #1368

Open rsignell opened 1 month ago

rsignell commented 1 month ago

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 <kthyng@tamu.edu>']

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

knutfrode commented 1 month ago

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

rsignell commented 1 month ago

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...

kthyng commented 1 month ago

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

rsignell commented 1 month ago

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

ocefpaf commented 1 month ago

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?

kthyng commented 1 month ago

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

knutfrode commented 1 month ago

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

kthyng commented 1 month ago

@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.

knutfrode commented 1 month ago

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?

kthyng commented 1 month ago

Yes I think that is a good idea.

knutfrode commented 1 month ago

Ok, this is now updated.

ocefpaf commented 1 month ago

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 notcreated. The problem persists for those using conda-forge and it requires a build number bump to fix it.