Skip to content

Commit

Permalink
Fix matplotlib 3.9.0 has no attribute 'get_cmap` (Project-MONAI#7780)
Browse files Browse the repository at this point in the history
Fixes Project-MONAI#7776

A few sentences describing the changes proposed in this pull request.

<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
(cherry picked from commit 5e9ac1b)
  • Loading branch information
KumoLiu authored and marksgraham committed May 22, 2024
1 parent 15e739b commit 3e23785
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
48 changes: 47 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import os
import subprocess
import sys
import importlib
import inspect

sys.path.insert(0, os.path.abspath(".."))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
Expand Down Expand Up @@ -137,7 +139,7 @@ def generate_apidocs(*args):
"github_user": "Project-MONAI",
"github_repo": "MONAI",
"github_version": "dev",
"doc_path": "docs/",
"doc_path": "docs/source",
"conf_py_path": "/docs/",
"VERSION": version,
}
Expand All @@ -162,3 +164,47 @@ def setup(app):
# Hook to allow for automatic generation of API docs
# before doc deployment begins.
app.connect("builder-inited", generate_apidocs)


# -- Linkcode configuration --------------------------------------------------
DEFAULT_REPOSITORY = "Project-MONAI/MONAI"
repository = os.environ.get("GITHUB_REPOSITORY", DEFAULT_REPOSITORY)

base_code_url = f"https://github.com/{repository}/blob/{version}"
MODULE_ROOT_FOLDER = "monai"


# Adjusted from https://github.com/python-websockets/websockets/blob/main/docs/conf.py
def linkcode_resolve(domain, info):
if domain != "py":
raise ValueError(
f"expected domain to be 'py', got {domain}."
"Please adjust linkcode_resolve to either handle this domain or ignore it."
)

mod = importlib.import_module(info["module"])
if "." in info["fullname"]:
objname, attrname = info["fullname"].split(".")
obj = getattr(mod, objname)
try:
# object is a method of a class
obj = getattr(obj, attrname)
except AttributeError:
# object is an attribute of a class
return None
else:
obj = getattr(mod, info["fullname"])

try:
file = inspect.getsourcefile(obj)
source, lineno = inspect.getsourcelines(obj)
except TypeError:
# e.g. object is a typing.Union
return None
file = os.path.relpath(file, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
if not file.startswith(MODULE_ROOT_FOLDER):
# e.g. object is a typing.NewType
return None
start, end = lineno, lineno + len(source) - 1
url = f"{base_code_url}/{file}#L{start}-L{end}"
return url
4 changes: 1 addition & 3 deletions monai/visualize/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
from monai.utils.type_conversion import convert_data_type, convert_to_dst_type

if TYPE_CHECKING:
from matplotlib import cm
from matplotlib import pyplot as plt
else:
plt, _ = optional_import("matplotlib", name="pyplot")
cm, _ = optional_import("matplotlib", name="cm")

__all__ = ["matshow3d", "blend_images"]

Expand Down Expand Up @@ -210,7 +208,7 @@ def blend_images(
image = repeat(image, 3, axis=0)

def get_label_rgb(cmap: str, label: NdarrayOrTensor) -> NdarrayOrTensor:
_cmap = cm.get_cmap(cmap)
_cmap = plt.colormaps.get_cmap(cmap)
label_np, *_ = convert_data_type(label, np.ndarray)
label_rgb_np = _cmap(label_np[0])
label_rgb_np = np.moveaxis(label_rgb_np, -1, 0)[:3]
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ einops
transformers>=4.36.0
mlflow>=1.28.0, <=2.11.3
clearml>=1.10.0rc0
matplotlib!=3.5.0
matplotlib>=3.6.3
tensorboardX
types-PyYAML
pyyaml
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ all =
transformers<4.22; python_version <= '3.10'
mlflow>=1.28.0, <=2.11.3
clearml>=1.10.0rc0
matplotlib
matplotlib>=3.6.3
tensorboardX
pyyaml
fire
Expand Down Expand Up @@ -127,7 +127,7 @@ transformers =
mlflow =
mlflow>=1.28.0, <=2.11.3
matplotlib =
matplotlib
matplotlib>=3.6.3
clearml =
clearml
tensorboardX =
Expand Down
1 change: 1 addition & 0 deletions tests/test_matshow3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def test_3d_rgb(self):
every_n=2,
frame_dim=-1,
channel_dim=0,
fill_value=0,
show=False,
)

Expand Down

0 comments on commit 3e23785

Please sign in to comment.