forked from equinor/xtgeo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLN: Dynamically import matplotlib and pyplot
Normally dynamically importing modules is fairly straight forward: you can just import them in the function or method they are used in. However, by default matplotlib will look for display settings on the machine it's being run on and compute cluster nodes do not have this set. The suggested solution to this is to import matplotlib before anything else is imported, and set its backend to `use("Agg")` which doesn't look for display information. xtgeo implemented this by importing matplotlib in the root __init__, and it generally complicates the dynamic loading situation. This solution tries to ensure that using the Agg backend will still be triggered if it xtgeo believes it is in batch mode and wraps a getting around `sys.modules` after importing it. It also tries not to repeat the import logic if it is already imported.
- Loading branch information
Showing
10 changed files
with
127 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import sys | ||
from typing import Any | ||
|
||
from xtgeo.common.constants import XTG_BATCH | ||
|
||
|
||
def get_matplotlib() -> Any: | ||
if "matplotlib" not in sys.modules: | ||
import matplotlib | ||
|
||
if XTG_BATCH in os.environ: | ||
matplotlib.use("Agg") | ||
return sys.modules["matplotlib"] | ||
|
||
|
||
def get_pyplot() -> Any: | ||
if "matplotlib" not in sys.modules: | ||
# Set `use("Agg")` | ||
get_matplotlib() | ||
|
||
if "matplotlib.pyplot" not in sys.modules: | ||
# Must be imported separately | ||
import matplotlib.pyplot | ||
|
||
matplotlib.pyplot # Let flake8 ignore unused import | ||
return sys.modules["matplotlib.pyplot"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.