diff --git a/ultraplot/__init__.py b/ultraplot/__init__.py index 1becececd..1bbf7b018 100644 --- a/ultraplot/__init__.py +++ b/ultraplot/__init__.py @@ -39,16 +39,16 @@ from .colors import PerceptualColormap as PerceptualColormap from .colors import SegmentedNorm as SegmentedNorm from .colors import _cmap_database as colormaps - from .config import config_inline_backend as config_inline_backend - from .config import Configurator as Configurator - from .config import rc as rc - from .config import rc_matplotlib as rc_matplotlib - from .config import rc_ultraplot as rc_ultraplot - from .config import register_cmaps as register_cmaps - from .config import register_colors as register_colors - from .config import register_cycles as register_cycles - from .config import register_fonts as register_fonts - from .config import use_style as use_style + from ._config import config_inline_backend as config_inline_backend + from ._config import Configurator as Configurator + from ._config import rc as rc + from ._config import rc_matplotlib as rc_matplotlib + from ._config import rc_ultraplot as rc_ultraplot + from ._config import register_cmaps as register_cmaps + from ._config import register_colors as register_colors + from ._config import register_cycles as register_cycles + from ._config import register_fonts as register_fonts + from ._config import use_style as use_style from .constructor import Colormap as Colormap from .constructor import Cycle as Cycle from .constructor import Formatter as Formatter @@ -200,30 +200,9 @@ def _setup(): _SETUP_RUNNING = True success = False try: - from .config import ( - rc, - register_cmaps, - register_colors, - register_cycles, - register_fonts, - ) - from .internals import ( - fonts as _fonts, # noqa: F401 - ensure mathtext override is active - ) - from .internals import rcsetup, warnings - from .internals.benchmarks import _benchmark - - with _benchmark("cmaps"): - register_cmaps(default=True) - with _benchmark("cycles"): - register_cycles(default=True) - with _benchmark("colors"): - register_colors(default=True) - with _benchmark("fonts"): - register_fonts(default=True) - - rcsetup.VALIDATE_REGISTERED_CMAPS = True - rcsetup.VALIDATE_REGISTERED_COLORS = True + from ._config import _initialize, rc + + _initialize() if rc["ultraplot.check_for_latest_version"]: from .utils import check_for_update @@ -242,7 +221,7 @@ def setup(eager: Optional[bool] = None) -> None: """ _setup() if eager is None: - from .config import rc + from ._config import rc eager = bool(rc["ultraplot.eager_import"]) if eager: diff --git a/ultraplot/_config.py b/ultraplot/_config.py new file mode 100644 index 000000000..a4bd60b6f --- /dev/null +++ b/ultraplot/_config.py @@ -0,0 +1,1940 @@ +#!/usr/bin/env python3 +""" +Tools for setting up ultraplot and configuring global settings. +See the :ref:`configuration guide ` for details. +""" + +# NOTE: The matplotlib analogue to this file is actually __init__.py +# but it makes more sense to have all the setup actions in a separate file +# so the namespace of the top-level module is unpolluted. +# NOTE: Why also load colormaps and cycles in this file and not colors.py? +# Because I think it makes sense to have all the code that "runs" (i.e. not +# just definitions) in the same place, and I was having issues with circular +# dependencies and where import order of __init__.py was affecting behavior. +import logging +import os +import re +import sys +from collections import namedtuple +from collections.abc import MutableMapping +from numbers import Real +from typing import Any, Callable, Dict + +import cycler +import matplotlib as mpl +import matplotlib.colors as mcolors +import matplotlib.font_manager as mfonts +import matplotlib.mathtext # noqa: F401 +import matplotlib.style as mstyle +import numpy as np +from matplotlib import RcParams + +from .internals import ( + _not_none, + _pop_kwargs, + _pop_props, + _translate_grid, + _version_mpl, + docstring, + ic, # noqa: F401 + rcsetup, + warnings, +) + +# Suppress warnings emitted by mathtext.py (_mathtext.py in recent versions) +# when when substituting dummy unavailable glyph due to fallback disabled. +logging.getLogger("matplotlib.mathtext").setLevel(logging.ERROR) + +__all__ = [ + "Configurator", + "rc", + "rc_ultraplot", + "rc_matplotlib", + "use_style", + "config_inline_backend", + "register_cmaps", + "register_cycles", + "register_colors", + "register_fonts", +] + +# Constants +if hasattr(mstyle, "_STYLE_BLACKLIST"): # Matplotlib >= 3.11 + _STYLE_BLACKLIST = mstyle._STYLE_BLACKLIST +else: + from matplotlib.style.core import STYLE_BLACKLIST as _STYLE_BLACKLIST + +COLORS_KEEP = ("red", "green", "blue", "cyan", "yellow", "magenta", "white", "black") + +_ULTRAPLOT_STYLES = { + "poster": { + "font.size": 14, + "axes.titlesize": 18, + "axes.labelsize": 16, + "xtick.labelsize": 13, + "ytick.labelsize": 13, + "legend.fontsize": 13, + "figure.titlesize": 20, + "lines.linewidth": 2.0, + "lines.markersize": 6, + "figure.facecolor": "none", + "savefig.facecolor": "none", + "savefig.edgecolor": "none", + }, + "dark_background": { + "figure.facecolor": "#000000", + "figure.edgecolor": "#000000", + "axes.facecolor": "#000000", + "axes.edgecolor": "#cbd5e1", + "axes.labelcolor": "#f8fafc", + "text.color": "#f8fafc", + "xtick.color": "#cbd5e1", + "ytick.color": "#cbd5e1", + "grid.color": "#475569", + "grid.alpha": 0.35, + "legend.facecolor": "#000000", + "legend.edgecolor": "#475569", + "savefig.facecolor": "#000000", + "savefig.edgecolor": "#000000", + "axes.prop_cycle": cycler.cycler( + color=( + "#60a5fa", + "#f59e0b", + "#34d399", + "#f472b6", + "#a78bfa", + "#f87171", + ) + ), + }, +} +_ULTRAPLOT_STYLES["dark"] = _ULTRAPLOT_STYLES["dark_background"] + +# Configurator docstrings +_rc_docstring = """ +local : bool, default: True + Whether to load settings from the `~Configurator.local_files` file. +user : bool, default: True + Whether to load settings from the `~Configurator.user_file` file. +default : bool, default: True + Whether to reload built-in default ultraplot settings. +""" +docstring._snippet_manager["rc.params"] = _rc_docstring + +# Registration docstrings +_shared_docstring = """ +*args : path-spec or `~ultraplot.colors.{type}Colormap`, optional + The {objects} to register. These can be file paths containing + RGB data or `~ultraplot.colors.{type}Colormap` instances. By default, + if positional arguments are passed, then `user` is set to ``False``. + + Valid file extensions are listed in the below table. Note that {objects} + are registered according to their filenames -- for example, ``name.xyz`` + will be registered as ``'name'``. +""" # noqa: E501 +_cmap_exts_docstring = """ + =================== ========================================== + Extension Description + =================== ========================================== + ``.json`` JSON database of the channel segment data. + ``.hex`` Comma-delimited list of HEX strings. + ``.rgb``, ``.txt`` 3-4 column table of channel values. + =================== ========================================== +""" +_cycle_exts_docstring = """ + ================== ========================================== + Extension Description + ================== ========================================== + ``.hex`` Comma-delimited list of HEX strings. + ``.rgb``, ``.txt`` 3-4 column table of channel values. + ================== ========================================== +""" +_color_docstring = """ +*args : path-like or dict, optional + The colors to register. These can be file paths containing RGB data or + dictionary mappings of names to RGB values. By default, if positional + arguments are passed, then `user` is set to ``False``. Files must have + the extension ``.txt`` and should contain one line per color in the + format ``name : hex``. Whitespace is ignored. +""" +_font_docstring = """ +*args : path-like, optional + The font files to add. By default, if positional arguments are passed, then + `user` is set to ``False``. Files must have the extensions ``.ttf`` or ``.otf``. + See `this link \ +`__ + for a guide on converting other font files to ``.ttf`` and ``.otf``. +""" +_register_docstring = """ +user : bool, optional + Whether to reload {objects} from `~Configurator.user_folder`. Default is + ``False`` if positional arguments were passed and ``True`` otherwise. +local : bool, optional + Whether to reload {objects} from `~Configurator.local_folders`. Default is + ``False`` if positional arguments were passed and ``True`` otherwise. +default : bool, default: False + Whether to reload the default {objects} packaged with ultraplot. + Default is always ``False``. +""" +docstring._snippet_manager["rc.cmap_params"] = _register_docstring.format( + objects="colormaps" +) # noqa: E501 +docstring._snippet_manager["rc.cycle_params"] = _register_docstring.format( + objects="color cycles" +) # noqa: E501 +docstring._snippet_manager["rc.color_params"] = _register_docstring.format( + objects="colors" +) # noqa: E501 +docstring._snippet_manager["rc.font_params"] = _register_docstring.format( + objects="fonts" +) # noqa: E501 +docstring._snippet_manager["rc.cmap_args"] = _shared_docstring.format( + objects="colormaps", type="Continuous" +) # noqa: E501 +docstring._snippet_manager["rc.cycle_args"] = _shared_docstring.format( + objects="color cycles", type="Discrete" +) # noqa: E501 +docstring._snippet_manager["rc.color_args"] = _color_docstring +docstring._snippet_manager["rc.font_args"] = _font_docstring +docstring._snippet_manager["rc.cmap_exts"] = _cmap_exts_docstring +docstring._snippet_manager["rc.cycle_exts"] = _cycle_exts_docstring + +_rc_register_handler_docstring = """ + Register a callback function to be executed when a setting is modified. + + This is an extension point for "special" settings that require complex + logic or have side-effects, such as updating other matplotlib settings. + It is used internally to decouple the configuration system from other + subsystems and avoid circular imports. + + Parameters + ---------- + name : str + The name of the setting (e.g., ``'cycle'``). + func : callable + The handler function to be executed. The function must accept a + single positional argument, which is the new `value` of the + setting, and must return a dictionary. The keys of the dictionary + should be valid ``matplotlib`` rc setting names, and the values + will be applied to the ``rc_matplotlib`` object. + + Example + ------- + >>> def _cycle_handler(value): + ... # ... logic to create a cycler object from the value ... + ... return {'axes.prop_cycle': new_cycler} + >>> rc.register_handler('cycle', _cycle_handler) + """ +docstring._snippet_manager["rc.register_handler"] = _rc_register_handler_docstring + + +def _init_user_file(): + """ + Initialize .ultraplotrc file. + """ + file = Configurator.user_file() + if not os.path.exists(file): + Configurator._save_yaml(file, comment=True) + + +def _init_user_folders(): + """ + Initialize .ultraplot folder. + """ + for subfolder in ("", "cmaps", "cycles", "colors", "fonts"): + folder = Configurator.user_folder(subfolder) + if not os.path.isdir(folder): + os.mkdir(folder) + + +def _get_data_folders(folder, user=True, local=True, default=True, reverse=False): + """ + Return data folder paths in reverse order of precedence. + """ + # When loading colormaps, cycles, and colors, files in the latter + # directories overwrite files in the former directories. When loading + # fonts, the resulting paths need to be *reversed*. + paths = [] + if default: + paths.append(os.path.join(os.path.dirname(__file__), folder)) + if user: + paths.append(Configurator.user_folder(folder)) + if local: + paths.extend(Configurator.local_folders(folder)) + if reverse: + paths = paths[::-1] + return paths + + +def _iter_data_objects(folder, *args, **kwargs): + """ + Iterate over input objects and files in the data folders that should be + registered. Also yield an index indicating whether these are user files. + """ + i = 0 # default files + for i, path in enumerate(_get_data_folders(folder, **kwargs)): + for dirname, dirnames, filenames in os.walk(path): + for filename in filenames: + if filename[0] == ".": # UNIX-style hidden files + continue + path = os.path.join(dirname, filename) + yield i, path + i += 1 # user files + for path in args: + path = os.path.expanduser(path) + if os.path.isfile(path): + yield i, path + else: + raise FileNotFoundError(f"Invalid file path {path!r}.") + + +def _filter_style_dict(rcdict, warn=True): + """ + Filter out blacklisted style parameters. + """ + # NOTE: This implements bugfix: https://github.com/matplotlib/matplotlib/pull/17252 + # This fix is *critical* for ultraplot because we always run style.use() + # when the configurator is made. Without fix backend is reset every time + # you import ultraplot in jupyter notebooks. So apply retroactively. + rcdict_filtered = {} + for key in rcdict: + if key in _STYLE_BLACKLIST: + if warn: + warnings._warn_ultraplot( + f"Dictionary includes a parameter, {key!r}, that is not related " + "to style. Ignoring." + ) + else: + rcdict_filtered[key] = rcdict[key] + return rcdict_filtered + + +def _get_default_style_dict(): + """ + Get the default rc parameters dictionary with deprecated parameters filtered. + """ + # NOTE: Use RcParams update to filter and translate deprecated settings + # before actually applying them to rcParams down pipeline. This way we can + # suppress warnings for deprecated default params but still issue warnings + # when user-supplied stylesheets have deprecated params. + # WARNING: Some deprecated rc params remain in dictionary as None so we + # filter them out. Beware if hidden attribute changes. + # WARNING: The examples.directory deprecation was handled specially inside + # RcParams in early versions. Manually pop it out here. + rcdict = _filter_style_dict(mpl.rcParamsDefault, warn=False) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", mpl.MatplotlibDeprecationWarning) + rcdict = dict(RcParams(rcdict)) + for attr in ("_deprecated_set", "_deprecated_remain_as_none"): + deprecated = getattr(mpl, attr, ()) + for key in deprecated: # _deprecated_set is in matplotlib < 3.4 + rcdict.pop(key, None) + rcdict.pop("examples.directory", None) # special case for matplotlib < 3.2 + return rcdict + + +def _get_style_dict(style, filter=True): + """ + Return a dictionary of settings belonging to the requested style(s). If `filter` + is ``True``, invalid style parameters like `backend` are filtered out. + """ + # NOTE: This is adapted from matplotlib source for the following changes: + # 1. Add an 'original' pseudo style. Like rcParamsOrig except we also reload + # from the user matplotlibrc file. + # 2. When the style is changed we reset to the default state ignoring matplotlibrc. + # By contrast matplotlib applies styles on top of current state (including + # matplotlibrc changes and runtime rcParams changes) but the word 'style' + # implies a rigid static format. This makes more sense. + # 3. Add a separate function that returns lists of style dictionaries so that + # we can modify the active style in a context block. ultraplot context is more + # conservative than matplotlib's rc_context because it gets called a lot + # (e.g. every time you make an axes and every format() call). Instead of + # copying the entire rcParams dict we just track the keys that were changed. + style_aliases = { + "538": "fivethirtyeight", + "dark": "dark_background", + "mpl20": "default", + "mpl15": "classic", + "original": mpl.matplotlib_fname(), + "seaborn": "seaborn-v0_8", + } + + # Always apply the default style *first* so styles are rigid + kw_matplotlib = _get_default_style_dict() + if style == "default" or style is mpl.rcParamsDefault: + return kw_matplotlib + + # Apply limited deviations from the matplotlib style that we want to propagate to + # other styles. Want users selecting stylesheets to have few surprises, so + # currently just enforce the new aesthetically pleasing fonts. + kw_matplotlib["font.family"] = "sans-serif" + for fmly in ("serif", "sans-serif", "monospace", "cursive", "fantasy"): + kw_matplotlib["font." + fmly] = rcsetup._rc_matplotlib_default["font." + fmly] + + # Apply user input style(s) one by one + if isinstance(style, str) or isinstance(style, dict): + styles = [style] + else: + styles = style + for style in styles: + if isinstance(style, dict): + kw = style + elif isinstance(style, str): + style = style_aliases.get(style, style) + if style in _ULTRAPLOT_STYLES: + kw = _ULTRAPLOT_STYLES[style] + elif style in mstyle.library: + kw = mstyle.library[style] + else: + try: + kw = mpl.rc_params_from_file(style, use_default_template=False) + except IOError: + raise IOError( + f"Style {style!r} not found in the style library and input " + "is not a valid URL or file path. Available styles are: " + + ", ".join(map(repr, mstyle.available)) + + "." + ) + else: + raise ValueError(f"Invalid style {style!r}. Must be string or dictionary.") + if filter: + kw = _filter_style_dict(kw, warn=True) + kw_matplotlib.update(kw) + + return kw_matplotlib + + +def _infer_ultraplot_dict(kw_params): + """ + Infer values for ultraplot's "added" parameters from stylesheet parameters. + """ + kw_ultraplot = {} + mpl_to_ultraplot = { + "xtick.labelsize": ( + "tick.labelsize", + "grid.labelsize", + ), + "ytick.labelsize": ( + "tick.labelsize", + "grid.labelsize", + ), + "axes.titlesize": ( + "abc.size", + "suptitle.size", + "title.size", + "leftlabel.size", + "rightlabel.size", + "toplabel.size", + "bottomlabel.size", + ), + "text.color": ( + "abc.color", + "suptitle.color", + "title.color", + "tick.labelcolor", + "grid.labelcolor", + "leftlabel.color", + "rightlabel.color", + "toplabel.color", + "bottomlabel.color", + ), + } + for key, params in mpl_to_ultraplot.items(): + if key in kw_params: + value = kw_params[key] + for param in params: + kw_ultraplot[param] = value + return kw_ultraplot + + +def config_inline_backend(fmt=None): + """ + Set up the ipython `inline backend display format \ +`__ + and ensure that inline figures always look the same as saved figures. + This runs the following ipython magic commands: + + .. code-block:: ipython + + %%config InlineBackend.figure_formats = rc['inlineformat'] + %%config InlineBackend.rc = {} # never override rc settings + %%config InlineBackend.close_figures = True # cells start with no active figures + %%config InlineBackend.print_figure_kwargs = {'bbox_inches': None} + + When the inline backend is inactive or unavailable, this has no effect. + This function is called when you modify the :rcraw:`inlineformat` property. + + Parameters + ---------- + fmt : str or sequence, default: :rc:`inlineformat` + The inline backend file format or a list thereof. Valid formats + include ``'jpg'``, ``'png'``, ``'svg'``, ``'pdf'``, and ``'retina'``. + + See also + -------- + Configurator + """ + # Note if inline backend is unavailable this will fail silently + try: + from IPython import get_ipython + except ImportError: + return + ipython = get_ipython() + if ipython is None: + return + fmt = _not_none(fmt, rc_ultraplot["inlineformat"]) + if isinstance(fmt, str): + fmt = [fmt] + elif np.iterable(fmt): + fmt = list(fmt) + else: + raise ValueError(f"Invalid inline backend format {fmt!r}. Must be string.") + + # IPython < 9.0.0 + if hasattr(ipython, "magic"): + ipython.magic("config InlineBackend.figure_formats = " + repr(fmt)) + ipython.magic("config InlineBackend.rc = {}") + ipython.magic("config InlineBackend.close_figures = True") + ipython.magic( + "config InlineBackend.print_figure_kwargs = {'bbox_inches': None}" + ) + + # IPython >= 9.0.0 + else: + ipython.run_line_magic("config", "InlineBackend.figure_formats = " + repr(fmt)) + ipython.run_line_magic("config", "InlineBackend.rc = {}") + ipython.run_line_magic("config", "InlineBackend.close_figures = True") + ipython.run_line_magic( + "config", "InlineBackend.print_figure_kwargs = {'bbox_inches': None}" + ) + + +def use_style(style): + """ + Apply the `matplotlib style(s) \ +`__ + with `matplotlib.style.use`. This function is + called when you modify the :rcraw:`style` property. + + Parameters + ---------- + style : str or sequence or dict-like + The matplotlib style name(s) or stylesheet filename(s), or dictionary(s) + of settings. Use ``'default'`` to apply matplotlib default settings and + ``'original'`` to include settings from your user ``matplotlibrc`` file. + + See also + -------- + Configurator + matplotlib.style.use + """ + # NOTE: This function is not really necessary but makes ultraplot's + # stylesheet-supporting features obvious. Plus changing the style does + # so much *more* than changing rc params or quick settings, so it is + # nice to have dedicated function instead of just another rc_param name. + kw_matplotlib = _get_style_dict(style) + rc_matplotlib.update(kw_matplotlib) + rc_ultraplot.update(_infer_ultraplot_dict(kw_matplotlib)) + + +@docstring._snippet_manager +def register_cmaps(*args, user=None, local=None, default=False): + """ + Register named colormaps. This is called on import. + + Parameters + ---------- + %(rc.cmap_args)s + + %(rc.cmap_exts)s + + %(rc.cmap_params)s + + See also + -------- + register_cycles + register_colors + register_fonts + ultraplot.demos.show_cmaps + """ + # Register input colormaps + from . import colors as pcolors + + user = _not_none(user, not bool(args)) # skip user folder if input args passed + local = _not_none(local, not bool(args)) + paths = [] + for arg in args: + if isinstance(arg, mcolors.Colormap): + pcolors._cmap_database.register(arg, name=arg.name) + else: + paths.append(arg) + + # Register data files + for i, path in _iter_data_objects( + "cmaps", *paths, user=user, local=local, default=default + ): + name, ext = os.path.splitext(os.path.basename(path)) + if ext and ext[1:] in ("json", "txt", "rgb", "xml", "hex"): + pcolors._cmap_database.register_lazy( + name, path, "continuous", is_default=(i == 0) + ) + + +@docstring._snippet_manager +def register_cycles(*args, user=None, local=None, default=False): + """ + Register named color cycles. This is called on import. + + Parameters + ---------- + %(rc.cycle_args)s + + %(rc.cycle_exts)s + + %(rc.cycle_params)s + + See also + -------- + register_cmaps + register_colors + register_fonts + ultraplot.demos.show_cycles + """ + # Register input color cycles + from . import colors as pcolors + + user = _not_none(user, not bool(args)) # skip user folder if input args passed + local = _not_none(local, not bool(args)) + paths = [] + for arg in args: + if isinstance(arg, mcolors.Colormap): + pcolors._cmap_database.register(arg, name=arg.name) + else: + paths.append(arg) + + # Register data files + for _, path in _iter_data_objects( + "cycles", *paths, user=user, local=local, default=default + ): + name, ext = os.path.splitext(os.path.basename(path)) + if ext and ext[1:] in ("hex", "rgb", "txt"): + pcolors._cmap_database.register_lazy(name, path, "discrete") + + +@docstring._snippet_manager +def register_colors( + *args, user=None, local=None, default=False, space=None, margin=None, **kwargs +): + """ + Register named colors. This is called on import. + + Parameters + ---------- + %(rc.color_args)s + %(rc.color_params)s + space : {'hcl', 'hsl', 'hpl'}, optional + The colorspace used to pick "perceptually distinct" colors from + the `XKCD color survey `__. + If passed then `default` is set to ``True``. + margin : float, default: 0.1 + The margin used to pick "perceptually distinct" colors from the + `XKCD color survey `__. The normalized hue, + saturation, and luminance of each color must differ from the channel + values of the prededing colors by `margin` in order to be registered. + Must fall between ``0`` and ``1`` (``0`` will register all colors). + If passed then `default` is set to ``True``. + **kwargs + Additional color name specifications passed as keyword arguments rather + than positional argument dictionaries. + + See also + -------- + register_cmaps + register_cycles + register_fonts + ultraplot.demos.show_colors + """ + from . import colors as pcolors + + default = default or space is not None or margin is not None + margin = _not_none(margin, 0.1) + space = _not_none(space, "hcl") + + # Remove previously registered colors + # NOTE: Try not to touch matplotlib colors for compatibility + srcs = {"xkcd": pcolors.COLORS_XKCD, "opencolor": pcolors.COLORS_OPEN} + if default: # possibly slow but not these dicts are empty on startup + for src in srcs.values(): + for key in src: + if key not in COLORS_KEEP: + pcolors._color_database.pop(key, None) # this also clears cache + src.clear() + + # Register input colors + user = _not_none(user, not bool(args) and not bool(kwargs)) # skip if args passed + local = _not_none(local, not bool(args) and not bool(kwargs)) + paths = [] + for arg in args: + if isinstance(arg, dict): + kwargs.update(arg) + else: + paths.append(arg) + for key, color in kwargs.items(): + if mcolors.is_color_like(color): + pcolors._color_database[key] = mcolors.to_rgba(color) + else: + raise ValueError(f"Invalid color {key}={color!r}.") + + # Load colors from file and get their HCL values + # NOTE: Colors that come *later* overwrite colors that come earlier. + for i, path in _iter_data_objects( + "colors", *paths, user=user, local=local, default=default + ): + loaded = pcolors._load_colors(path, warn_on_failure=True) + if i == 0: + cat, _ = os.path.splitext(os.path.basename(path)) + if cat not in srcs: + raise RuntimeError(f"Unknown ultraplot color database {path!r}.") + src = srcs[cat] + if cat == "xkcd": + for key in COLORS_KEEP: + loaded[key] = pcolors._color_database[key] # keep the same + loaded = pcolors._standardize_colors(loaded, space, margin) + src.clear() + src.update(loaded) # needed for demos.show_colors() + pcolors._color_database.update(loaded) + + +@docstring._snippet_manager +def register_fonts(*args, user=True, local=True, default=False): + """ + Register font families. This is called on import. + + Parameters + ---------- + %(rc.font_args)s + %(rc.font_params)s + + See also + -------- + register_cmaps + register_cycles + register_colors + ultraplot.demos.show_fonts + """ + # Find ultraplot fonts + # WARNING: Must search data files in reverse because font manager will + # not overwrite existing fonts with user-input fonts. + # WARNING: If you include a font file with an unrecognized style, + # matplotlib may use that font instead of the 'normal' one! Valid styles: + # 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', + # 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black' + # https://matplotlib.org/api/font_manager_api.html + # For macOS the only fonts with 'Thin' in one of the .ttf file names + # are Helvetica Neue and .SF NS Display Condensed. Never try to use these! + paths_ultraplot = _get_data_folders( + "fonts", user=user, local=local, default=default, reverse=True + ) + fnames_ultraplot = set(mfonts.findSystemFonts(paths_ultraplot)) + for path in args: + path = os.path.expanduser(path) + if os.path.isfile(path): + fnames_ultraplot.add(path) + else: + raise FileNotFoundError(f"Invalid font file path {path!r}.") + + # Detect user-input ttc fonts and issue warning + fnames_ultraplot_ttc = { + file for file in fnames_ultraplot if os.path.splitext(file)[1] == ".ttc" + } + if fnames_ultraplot_ttc: + warnings._warn_ultraplot( + "Ignoring the following .ttc fonts because they cannot be " + "saved into PDF or EPS files (see matplotlib issue #3135): " + + ", ".join(map(repr, sorted(fnames_ultraplot_ttc))) + + ". Please consider expanding them into separate .ttf files." + ) + + # Rebuild font cache only if necessary! Can be >50% of total import time! + fnames_all = {font.fname for font in mfonts.fontManager.ttflist} + fnames_ultraplot -= fnames_ultraplot_ttc + if not fnames_all >= fnames_ultraplot: + warnings._warn_ultraplot( + "Rebuilding font cache. This usually happens " + "after installing or updating ultraplot." + ) + if hasattr(mfonts.fontManager, "addfont"): + # Newer API lets us add font files manually and deprecates TTFPATH. However + # to cache fonts added this way, we must call json_dump explicitly. + # NOTE: Previously, cache filename was specified as _fmcache variable, but + # recently became inaccessible. Must reproduce mpl code instead. + # NOTE: Older mpl versions used fontList.json as the cache, but these + # versions also did not have 'addfont', so makes no difference. + for fname in fnames_ultraplot: + mfonts.fontManager.addfont(fname) + cache = os.path.join( + mpl.get_cachedir(), f"fontlist-v{mfonts.FontManager.__version__}.json" + ) + mfonts.json_dump(mfonts.fontManager, cache) + else: + # Older API requires us to modify TTFPATH + # NOTE: Previously we tried to modify TTFPATH before importing + # font manager with hope that it would load ultraplot fonts on + # initialization. But 99% of the time font manager just imports + # the FontManager from cache, so we would have to rebuild anyway. + paths = ":".join(paths_ultraplot) + if "TTFPATH" not in os.environ: + os.environ["TTFPATH"] = paths + elif paths not in os.environ["TTFPATH"]: + os.environ["TTFPATH"] += ":" + paths + mfonts._rebuild() + + # Remove ttc files and 'Thin' fonts *after* rebuild + # NOTE: 'Thin' filter is ugly kludge but without this matplotlib picks up on + # Roboto thin ttf files installed on the RTD server when compiling docs. + mfonts.fontManager.ttflist = [ + font + for font in mfonts.fontManager.ttflist + if os.path.splitext(font.fname)[1] != ".ttc" + and (_version_mpl >= "3.3" or "Thin" not in os.path.basename(font.fname)) + ] + + +class Configurator(MutableMapping, dict): + """ + A dictionary-like class for managing `matplotlib settings + `__ + stored in `rc_matplotlib` and :ref:`ultraplot settings ` + stored in `rc_ultraplot`. This class is instantiated as the `rc` object + on import. See the :ref:`user guide ` for details. + """ + + def __repr__(self): + cls = type("rc", (dict,), {}) # temporary class with short name + src = cls({key: val for key, val in rc_ultraplot.items() if "." not in key}) + return type(rc_matplotlib).__repr__(src).strip()[:-1] + ",\n ...\n })" + + def __str__(self): + cls = type("rc", (dict,), {}) # temporary class with short name + src = cls({key: val for key, val in rc_ultraplot.items() if "." not in key}) + return type(rc_matplotlib).__str__(src) + "\n..." + + def __iter__(self): + yield from rc_ultraplot # sorted ultraplot settings, ignoring deprecations + yield from rc_matplotlib # sorted matplotlib settings, ignoring deprecations + + def __len__(self): + return len(tuple(iter(self))) + + def __delitem__(self, key): # noqa: U100 + raise RuntimeError("rc settings cannot be deleted.") + + def __delattr__(self, attr): # noqa: U100 + raise RuntimeError("rc settings cannot be deleted.") + + @docstring._snippet_manager + def __init__(self, local=True, user=True, default=True, **kwargs): + """ + Parameters + ---------- + %(rc.params)s + """ + self._context = [] + self._setting_handlers = {} + self._init(local=local, user=user, default=default, **kwargs) + + @docstring._snippet_manager + def register_handler( + self, name: str, func: Callable[[Any], Dict[str, Any]] + ) -> None: + """ + %(rc.register_handler)s + """ + self._setting_handlers[name] = func + + def __getitem__(self, key): + """ + Return an `rc_matplotlib` or `rc_ultraplot` setting using dictionary notation + (e.g., ``value = uplt.rc[name]``). + """ + key, _ = self._validate_key(key) # might issue ultraplot removed/renamed error + try: + return rc_ultraplot[key] + except KeyError: + pass + return rc_matplotlib[key] # might issue matplotlib removed/renamed error + + def __setitem__(self, key, value): + """ + Modify an `rc_matplotlib` or `rc_ultraplot` setting using dictionary notation + (e.g., ``uplt.rc[name] = value``). + """ + kw_ultraplot, kw_matplotlib = self._get_item_dicts(key, value) + rc_ultraplot.update(kw_ultraplot) + rc_matplotlib.update(kw_matplotlib) + + def __getattr__(self, attr): + """ + Return an `rc_matplotlib` or `rc_ultraplot` setting using "dot" notation + (e.g., ``value = uplt.rc.name``). + """ + if attr[:1] == "_": + return super().__getattribute__(attr) # raise built-in error + else: + return self.__getitem__(attr) + + def __setattr__(self, attr, value): + """ + Modify an `rc_matplotlib` or `rc_ultraplot` setting using "dot" notation + (e.g., ``uplt.rc.name = value``). + """ + if attr[:1] == "_": + super().__setattr__(attr, value) + else: + self.__setitem__(attr, value) + + def __enter__(self): + """ + Apply settings from the most recent context block. + """ + if not self._context: + raise RuntimeError( + "rc object must be initialized for context block using rc.context()." + ) + context = self._context[-1] + kwargs = context.kwargs + rc_new = context.rc_new # used for context-based _get_item_context + rc_old = context.rc_old # used to re-apply settings without copying whole dict + for key, value in kwargs.items(): + try: + kw_ultraplot, kw_matplotlib = self._get_item_dicts(key, value) + except Exception as e: + self.__exit__() + raise e + + for rc_dict, kw_new in zip( + (rc_ultraplot, rc_matplotlib), + (kw_ultraplot, kw_matplotlib), + ): + for key, value in kw_new.items(): + rc_old[key] = rc_dict[key] + rc_new[key] = rc_dict[key] = value + + def __exit__(self, *args): # noqa: U100 + """ + Restore settings from the most recent context block. + """ + if not self._context: + raise RuntimeError( + "rc object must be initialized for context block using rc.context()." + ) + context = self._context[-1] + for key, value in context.rc_old.items(): + kw_ultraplot, kw_matplotlib = self._get_item_dicts(key, value) + rc_ultraplot.update(kw_ultraplot) + rc_matplotlib.update(kw_matplotlib) + del self._context[-1] + + def _init(self, *, local, user, default): + """ + Initialize the configurator. + """ + # Always remove context objects + self._context.clear() + + # Update from default settings + # NOTE: see _remove_blacklisted_style_params bugfix + if default: + rc_matplotlib.update(_get_style_dict("original", filter=False)) + rc_matplotlib.update(rcsetup._rc_matplotlib_default) + rc_ultraplot.update(rcsetup._rc_ultraplot_default) + for key, value in rc_ultraplot.items(): + kw_ultraplot, kw_matplotlib = self._get_item_dicts(key, value) + rc_matplotlib.update(kw_matplotlib) + rc_ultraplot.update(kw_ultraplot) + + # Update from user home + user_path = None + if user: + user_path = self.user_file() + if os.path.isfile(user_path): + self.load(user_path) + + # Update from local paths + if local: + local_paths = self.local_files() + for path in local_paths: + if path == user_path: # local files always have precedence + continue + self.load(path) + + @staticmethod + def _validate_key(key, value=None): + """ + Validate setting names and handle `rc_ultraplot` deprecations. + """ + # NOTE: Not necessary to check matplotlib key here because... not sure why. + # Think deprecated matplotlib keys are not involved in any synced settings. + # Also note _check_key includes special handling for some renamed keys. + if not isinstance(key, str): + raise KeyError(f"Invalid key {key!r}. Must be string.") + key = key.lower() + if "." not in key: + key = rcsetup._rc_nodots.get(key, key) + key, value = rc_ultraplot._check_key( + key, value + ) # may issue deprecation warning + return key, value + + @staticmethod + def _validate_value(key, value): + """ + Validate setting values and convert numpy ndarray to list if possible. + """ + # NOTE: Ideally would implicitly validate on subsequent assignment to rc + # dictionaries, but must explicitly do it here, so _get_item_dicts can + # work with e.g. 'tick.lenratio', so _get_item_dicts does not have to include + # deprecated name handling in its if statements, and so _load_file can + # catch errors and emit warnings with line number indications as files + # are being read rather than after the end of the file reading. + if isinstance(value, np.ndarray): + value = value.item() if value.size == 1 else value.tolist() + validate_matplotlib = getattr(rc_matplotlib, "validate", None) + validate_ultraplot = rc_ultraplot._validate + if validate_matplotlib is not None and key in validate_matplotlib: + value = validate_matplotlib[key](value) + elif key in validate_ultraplot: + value = validate_ultraplot[key](value) + return value + + def _get_item_context(self, key, mode=None): + """ + As with `~Configurator.__getitem__` but the search is limited based + on the context mode and ``None`` is returned if the key is not found. + """ + key, _ = self._validate_key(key) + if mode is None: + mode = self._context_mode + cache = tuple(context.rc_new for context in self._context) + if mode == 0: + rcdicts = (*cache, rc_ultraplot, rc_matplotlib) + elif mode == 1: + rcdicts = (*cache, rc_ultraplot) # added settings only! + elif mode == 2: + rcdicts = (*cache,) + else: + raise ValueError(f"Invalid caching mode {mode!r}.") + for rcdict in rcdicts: + if not rcdict: + continue + try: + return rcdict[key] + except KeyError: + continue + if mode == 0: # otherwise return None + raise KeyError(f"Invalid rc setting {key!r}.") + + def _get_item_dicts(self, key, value): + """ + Return dictionaries for updating the `rc_ultraplot` and `rc_matplotlib` + properties associated with this key. Used when setting items, entering + context blocks, or loading files. + """ + # Get validated key, value, and child keys + key, value = self._validate_key(key, value) + value = self._validate_value(key, value) + keys = (key,) + rcsetup._rc_children.get(key, ()) # settings to change + contains = lambda *args: any(arg in keys for arg in args) # noqa: E731 + + # Fill dictionaries of matplotlib and ultraplot settings + # NOTE: Raise key error right away so it can be caught by _load_file(). + # Also ignore deprecation warnings so we only get them *once* on assignment + kw_ultraplot = {} # custom properties + kw_matplotlib = {} # builtin properties + with warnings.catch_warnings(): + warnings.simplefilter("ignore", mpl.MatplotlibDeprecationWarning) + warnings.simplefilter("ignore", warnings.UltraPlotWarning) + for key_i in keys: + if key_i in rc_matplotlib: + kw_matplotlib[key_i] = value + elif key_i in rc_ultraplot: + kw_ultraplot[key_i] = value + else: + raise KeyError(f"Invalid rc setting {key_i!r}.") + + # Special key: configure inline backend + if contains("inlineformat"): + config_inline_backend(value) + + # Special key: apply stylesheet + elif contains("style"): + if value is not None: + ikw_matplotlib = _get_style_dict(value) + kw_matplotlib.update(ikw_matplotlib) + kw_ultraplot.update(_infer_ultraplot_dict(ikw_matplotlib)) + + # Generic handler for special properties + if key in self._setting_handlers: + kw_matplotlib.update(self._setting_handlers[key](value)) + + # Turning bounding box on should turn border off and vice versa + elif contains("abc.bbox", "title.bbox", "abc.border", "title.border"): + if value: + name, this = key.split(".") + other = "border" if this == "bbox" else "bbox" + kw_ultraplot[name + "." + other] = False + + # Fontsize + # NOTE: Re-application of e.g. size='small' uses the updated 'font.size' + elif contains("font.size"): + kw_ultraplot.update( + { + key: value + for key, value in rc_ultraplot.items() + if key in rcsetup.FONT_KEYS and value in mfonts.font_scalings + } + ) + kw_matplotlib.update( + { + key: value + for key, value in rc_matplotlib.items() + if key in rcsetup.FONT_KEYS and value in mfonts.font_scalings + } + ) + + # Tick length/major-minor tick length ratio + elif contains("tick.len", "tick.lenratio"): + if contains("tick.len"): + ticklen = value + ratio = rc_ultraplot["tick.lenratio"] + else: + ticklen = rc_ultraplot["tick.len"] + ratio = value + kw_matplotlib["xtick.minor.size"] = ticklen * ratio + kw_matplotlib["ytick.minor.size"] = ticklen * ratio + + # Spine width/major-minor tick width ratio + elif contains("tick.width", "tick.widthratio"): + if contains("tick.width"): + tickwidth = value + ratio = rc_ultraplot["tick.widthratio"] + else: + tickwidth = rc_ultraplot["tick.width"] + ratio = value + kw_matplotlib["xtick.minor.width"] = tickwidth * ratio + kw_matplotlib["ytick.minor.width"] = tickwidth * ratio + + # Gridline width + elif contains("grid.width", "grid.widthratio"): + if contains("grid.width"): + gridwidth = value + ratio = rc_ultraplot["grid.widthratio"] + else: + gridwidth = rc_ultraplot["grid.width"] + ratio = value + kw_ultraplot["gridminor.linewidth"] = gridwidth * ratio + kw_ultraplot["gridminor.width"] = gridwidth * ratio + + # Gridline toggling + elif contains("grid", "gridminor"): + b, which = _translate_grid( + value, "gridminor" if contains("gridminor") else "grid" + ) + kw_matplotlib["axes.grid"] = b + kw_matplotlib["axes.grid.which"] = which + + return kw_ultraplot, kw_matplotlib + + @staticmethod + def _get_axisbelow_zorder(axisbelow): + """ + Convert the `axisbelow` string to its corresponding `zorder`. + """ + if axisbelow is True: + zorder = 0.5 + elif axisbelow is False: + zorder = 2.5 + elif axisbelow in ("line", "lines"): + zorder = 1.5 + else: + raise ValueError(f"Unexpected axisbelow value {axisbelow!r}.") + return zorder + + def _get_background_props(self, patch_kw=None, native=True, **kwargs): + """ + Return background properties, optionally filtering the output dictionary + based on the context. + """ + # Deprecated behavior + context = native or self._context_mode == 2 + if patch_kw: + warnings._warn_ultraplot( + "'patch_kw' is no longer necessary as of ultraplot v0.8. " + "Pass the parameters as keyword arguments instead." + ) + kwargs.update(patch_kw) + + # Get user-input properties and changed rc settings + # NOTE: Here we use 'color' as an alias for just 'edgecolor' rather than + # both 'edgecolor' and 'facecolor' to match 'xcolor' and 'ycolor' arguments. + props = _pop_props(kwargs, "patch") + if "color" in props: + props.setdefault("edgecolor", props.pop("color")) + for key in ("alpha", "facecolor", "linewidth", "edgecolor"): + value = self.find("axes." + key, context=context) + if value is not None: + props.setdefault(key, value) + + # Partition properties into face and edge + kw_face = _pop_kwargs(props, "alpha", "facecolor") + kw_edge = _pop_kwargs(props, "edgecolor", "linewidth", "linestyle") + kw_edge["capstyle"] = "projecting" # NOTE: needed to fix cartopy bounds + if "color" in props: + kw_edge.setdefault("edgecolor", props.pop("color")) + if kwargs: + raise TypeError(f"Unexpected keyword argument(s): {kwargs!r}") + + return kw_face, kw_edge + + def _get_gridline_bool(self, grid=None, axis=None, which="major", native=True): + """ + Return major and minor gridline toggles from ``axes.grid``, ``axes.grid.which``, + and ``axes.grid.axis``, optionally returning `None` based on the context. + """ + # NOTE: If you pass 'grid' or 'gridminor' the native args are updated + # NOTE: Very careful to return not None only if setting was changed. + # Avoid unnecessarily triggering grid redraws (esp. bad for geo.py) + context = native or self._context_mode == 2 + grid_on = self.find("axes.grid", context=context) + which_on = self.find("axes.grid.which", context=context) + if grid_on is not None or which_on is not None: # if *one* was changed + axis_on = self["axes.grid.axis"] # always need this property + grid_on = _not_none(grid_on, self["axes.grid"]) + which_on = _not_none(which_on, self["axes.grid.which"]) + axis = _not_none(axis, "x") + axis_on = axis is None or axis_on in (axis, "both") + which_on = which_on in (which, "both") + grid = _not_none(grid, grid_on and axis_on and which_on) + return grid + + def _get_gridline_props(self, which="major", native=True, rebuild=False): + """ + Return gridline properties, optionally filtering the output dictionary + based on the context. + """ + # Line properties + # NOTE: Gridline zorder is controlled automatically by matplotlib but + # must be controlled manually for geographic projections + key = "grid" if which == "major" else "gridminor" + prefix = "grid_" if native else "" # for native gridlines use this prefix + context = not rebuild and (native or self._context_mode == 2) + kwlines = self.fill( + { + f"{prefix}alpha": f"{key}.alpha", + f"{prefix}color": f"{key}.color", + f"{prefix}linewidth": f"{key}.linewidth", + f"{prefix}linestyle": f"{key}.linestyle", + }, + context=context, + ) + axisbelow = self.find("axes.axisbelow", context=context) + if axisbelow is not None: + if native: # this is a native plot so use set_axisbelow() down the line + kwlines["axisbelow"] = axisbelow + else: # this is a geographic plot so apply with zorder + kwlines["zorder"] = self._get_axisbelow_zorder(axisbelow) + return kwlines + + def _get_label_props(self, native=True, **kwargs): + """ + Return the axis label properties, optionally filtering the output dictionary + based on the context. + """ + # Get the label settings + # NOTE: This permits passing arbitrary additional args to set_[xy]label() + context = native or self._context_mode == 2 + kw = self.fill( + { + "color": "axes.labelcolor", + "weight": "axes.labelweight", + "size": "axes.labelsize", + "family": "font.family", + "labelpad": "axes.labelpad", # read by set_xlabel/set_ylabel + }, + context=context, + ) + for key, value in kwargs.items(): + if value is not None: # allow e.g. color=None + kw[key] = value + return kw + + def _get_loc_string(self, string, axis=None, native=True): + """ + Return `tickloc` and `spineloc` location strings from the `rc` boolean toggles, + optionally returning `None` based on the context. + """ + context = native or self._context_mode == 2 + axis = _not_none(axis, "x") + opt1, opt2 = ("top", "bottom") if axis == "x" else ("left", "right") + b1 = self.find(f"{string}.{opt1}", context=context) + b2 = self.find(f"{string}.{opt2}", context=context) + if b1 is None and b2 is None: + return None + elif b1 and b2: + return "both" + elif b1: + return opt1 + elif b2: + return opt2 + else: + return "neither" + + def _get_tickline_props(self, axis=None, which="major", native=True, rebuild=False): + """ + Return the tick line properties, optionally filtering the output dictionary + based on the context. + """ + # Tick properties obtained with rc.category + # NOTE: This loads 'size', 'width', 'pad', 'bottom', and 'top' + axis = _not_none(axis, "x") + context = not rebuild and (native or self._context_mode == 2) + kwticks = self.category(f"{axis}tick.{which}", context=context) + kwticks.pop("visible", None) + + # NOTE: We pop visibility properties from the styling dictionary so that + # stylistic updates (like applying a dark_background theme) do not override + # the tick visibility logic strictly managed by ax._update_locs() and alternate axes. + for key in ("bottom", "top", "left", "right"): + kwticks.pop(key, None) + + for key in ("color", "direction"): + value = self.find(f"{axis}tick.{key}", context=context) + if value is not None: + kwticks[key] = value + return kwticks + + def _get_ticklabel_props(self, axis=None, native=True, rebuild=False): + """ + Return the tick label properties, optionally filtering the output dictionary + based on the context. + """ + # Geographic gridline labels use the ultraplot-only grid.label* settings, + # while native matplotlib tick labels use x/y tick rcParams. + sprefix = axis or "" + cprefix = sprefix if _version_mpl >= "3.4" else "" # new settings + context = not rebuild and (native or self._context_mode == 2) + color_key = f"{cprefix}tick.labelcolor" if native else "grid.labelcolor" + size_key = f"{sprefix}tick.labelsize" if native else "grid.labelsize" + weight_key = "tick.labelweight" if native else "grid.labelweight" + kwtext = self.fill( + { + "color": color_key, # native setting sometimes avail + "size": size_key, + "weight": weight_key, # native setting never avail + "family": "font.family", # apply manually + }, + context=context, + ) + if kwtext.get("color", None) == "inherit": + # Inheritence is not automatic for geographic + # gridline labels so we apply inheritence here. + kwtext["color"] = self[f"{sprefix}tick.color"] + return kwtext + + @staticmethod + def local_files(): + """ + Return locations of files named ``ultraplotrc`` in this directory and in parent + directories. "Hidden" files with a leading dot are also recognized. These are + automatically loaded when ultraplot is imported. + + See also + -------- + Configurator.user_file + Configurator.local_folders + """ + cdir = os.getcwd() + paths = [] + while cdir: # i.e. not root + for name in ("ultraplotrc", ".ultraplotrc"): + path = os.path.join(cdir, name) + if os.path.isfile(path): + paths.append(path) + ndir = os.path.dirname(cdir) + if ndir == cdir: # root + break + cdir = ndir + return paths[::-1] # sort from decreasing to increasing importantce + + @staticmethod + def local_folders(subfolder=None): + """ + Return locations of folders named ``ultraplot_cmaps``, ``ultraplot_cycles``, + ``ultraplot_colors``, and ``ultraplot_fonts`` in this directory and in parent + directories. "Hidden" folders with a leading dot are also recognized. Files + in these directories are automatically loaded when ultraplot is imported. + + See also + -------- + Configurator.user_folder + Configurator.local_files + """ + cdir = os.getcwd() + paths = [] + if subfolder is None: + subfolder = ("cmaps", "cycles", "colors", "fonts") + if isinstance(subfolder, str): + subfolder = (subfolder,) + while cdir: # i.e. not root + for prefix in ("ultraplot", ".ultraplot"): + for suffix in subfolder: + path = os.path.join(cdir, "_".join((prefix, suffix))) + if os.path.isdir(path): + paths.append(path) + ndir = os.path.dirname(cdir) + if ndir == cdir: # root + break + cdir = ndir + return paths[::-1] + + @staticmethod + def _config_folder(): + """ + Get the XDG ultraplot folder. + """ + home = os.path.expanduser("~") + base = os.environ.get("XDG_CONFIG_HOME") + if not base: + base = os.path.join(home, ".config") + if sys.platform.startswith(("linux", "freebsd")) and os.path.isdir(base): + configdir = os.path.join(base, "ultraplot") + else: + configdir = os.path.join(home, ".ultraplot") + return configdir + + @staticmethod + def user_file(): + """ + Return location of the default ultraplotrc file. On Linux, this is either + ``$XDG_CONFIG_HOME/ultraplot/ultraplotrc`` or ``~/.config/ultraplot/ultraplotrc`` + if the `XDG directory `__ + is unset. On other operating systems, this is ``~/.ultraplot/ultraplotrc``. The + location ``~/.ultraplotrc`` or ``~/.ultraplot/ultraplotrc`` is always returned if the + file exists, regardless of the operating system. If multiple valid locations + are found, a warning is raised. + + See also + -------- + Configurator.user_folder + Configurator.local_files + """ + # Support both loose files and files inside .ultraplot + file = os.path.join(Configurator.user_folder(), "ultraplotrc") + universal = os.path.join(os.path.expanduser("~"), ".ultraplotrc") + if os.path.isfile(universal): + if file != universal and os.path.isfile(file): + warnings._warn_ultraplot( + "Found conflicting default user ultraplotrc files at " + f"{universal!r} and {file!r}. Ignoring the second one." + ) + file = universal + return file + + @staticmethod + def user_folder(subfolder=None): + """ + Return location of the default ultraplot folder. On Linux, this + is either ``$XDG_CONFIG_HOME/ultraplot`` or ``~/.config/ultraplot`` + if the `XDG directory `__ + is unset. On other operating systems, this is ``~/.ultraplot``. The location + ``~/.ultraplot`` is always returned if the folder exists, regardless of the + operating system. If multiple valid locations are found, a warning is raised. + + See also + -------- + Configurator.user_file + Configurator.local_folders + """ + # Try the XDG standard location + # NOTE: This is borrowed from matplotlib.get_configdir + home = os.path.expanduser("~") + universal = folder = os.path.join(home, ".ultraplot") + if sys.platform.startswith(("linux", "freebsd")): + xdg = os.environ.get("XDG_CONFIG_HOME") + xdg = xdg or os.path.join(home, ".config") + folder = os.path.join(xdg, "ultraplot") + # Fallback to the loose ~/.ultraplot if it is present + # NOTE: This is critical or we might ignore previously stored settings! + if os.path.isdir(universal): + if folder != universal and os.path.isdir(folder): + warnings._warn_ultraplot( + "Found conflicting default user ultraplot folders at " + f"{universal!r} and {folder!r}. Ignoring the second one." + ) + folder = universal + # Return the folder + if subfolder: + folder = os.path.join(folder, subfolder) + return folder + + def context(self, *args, mode=0, file=None, **kwargs): + """ + Temporarily modify the rc settings in a "with as" block. + + Parameters + ---------- + *args + Dictionaries of `rc` keys and values. + file : path-like, optional + Filename from which settings should be loaded. + **kwargs + `rc` names and values passed as keyword arguments. + If the name has dots, simply omit them. + + Other parameters + ---------------- + mode : {0, 1, 2}, optional + The context mode. Dictates the behavior of `~Configurator.find`, + `~Configurator.fill`, and `~Configurator.category` within a + "with as" block when called with ``context=True``. + + The options are as follows: + + * ``mode=0``: Matplotlib's `rc_matplotlib` settings + and ultraplot's `rc_ultraplot` settings are all returned, + whether or not they are local to the "with as" block. + * ``mode=1``: Matplotlib's `rc_matplotlib` settings are only + returned if they are local to the "with as" block. For example, + if :rcraw:`axes.titlesize` was passed to `~Configurator.context`, + then ``uplt.rc.find('axes.titlesize', context=True)`` will return + this value, but ``uplt.rc.find('axes.titleweight', context=True)`` will + return ``None``. This is used internally when instantiating axes. + * ``mode=2``: Matplotlib's `rc_matplotlib` settings and ultraplot's + `rc_ultraplot` settings are only returned if they are local to the + "with as" block. This is used internally when formatting axes. + + Note + ---- + Context "modes" are primarily used internally but may also be useful for power + users. Mode ``1`` is used when `~ultraplot.axes.Axes.format` is called during + axes instantiation, and mode ``2`` is used when `~ultraplot.axes.Axes.format` + is manually called by users. The latter prevents successive calls to + `~ultraplot.axes.Axes.format` from constantly looking up and re-applying + unchanged settings and significantly increasing the runtime. + + Example + ------- + The below applies settings to axes in a specific figure using + `~Configurator.context`. + + >>> import ultraplot as uplt + >>> with uplt.rc.context(ticklen=5, metalinewidth=2): + >>> fig, ax = uplt.subplots() + >>> ax.plot(data) + + The below applies settings to a specific axes using + `~ultraplot.axes.Axes.format`, which uses `~Configurator.context` + internally. + + >>> import ultraplot as uplt + >>> fig, ax = uplt.subplots() + >>> ax.format(ticklen=5, metalinewidth=2) + """ + # Add input dictionaries + for arg in args: + if not isinstance(arg, dict): + raise ValueError(f"Non-dictionary argument {arg!r}.") + kwargs.update(arg) + + # Add settings from file + if file is not None: + kw = self._load_file(file) + kw = {key: value for key, value in kw.items() if key not in kwargs} + kwargs.update(kw) + + # Activate context object + if mode not in range(3): + raise ValueError(f"Invalid mode {mode!r}.") + cls = namedtuple("RcContext", ("mode", "kwargs", "rc_new", "rc_old")) + context = cls(mode=mode, kwargs=kwargs, rc_new={}, rc_old={}) + self._context.append(context) + return self + + def category(self, cat, *, trimcat=True, context=False): + """ + Return a dictionary of settings beginning with the substring ``cat + '.'``. + Optionally limit the search to the context level. + + Parameters + ---------- + cat : str, optional + The `rc` setting category. + trimcat : bool, default: True + Whether to trim ``cat`` from the key names in the output dictionary. + context : bool, default: False + If ``True``, then settings not found in the context dictionaries + are omitted from the output dictionary. See `~Configurator.context`. + + See also + -------- + Configurator.find + Configurator.fill + """ + kw = {} + if cat not in rcsetup._rc_categories: + raise ValueError( + f"Invalid rc category {cat!r}. Valid categories are: " + + ", ".join(map(repr, rcsetup._rc_categories)) + + "." + ) + for key in self: + if not re.match(rf"\A{cat}\.[^.]+\Z", key): + continue + value = self._get_item_context(key, None if context else 0) + if value is None: + continue + if trimcat: + key = re.sub(rf"\A{cat}\.", "", key) + kw[key] = value + return kw + + def fill(self, props, *, context=False): + """ + Return a dictionary filled with settings whose names match the string values + in the input dictionary. Optionally limit the search to the context level. + + Parameters + ---------- + props : dict-like + Dictionary whose values are setting names -- for example + ``rc.fill({'edgecolor': 'axes.edgecolor', 'facecolor': 'axes.facecolor'})``. + context : bool, default: False + If ``True``, then settings not found in the context dictionaries + are omitted from the output dictionary. See `~Configurator.context`. + + See also + -------- + Configurator.category + Configurator.find + """ + kw = {} + for key, value in props.items(): + item = self._get_item_context(value, None if context else 0) + if item is not None: + kw[key] = item + return kw + + def find(self, key, *, context=False): + """ + Return a single setting. Optionally limit the search to the context level. + + Parameters + ---------- + key : str + The single setting name. + context : bool, default: False + If ``True``, then ``None`` is returned if the setting is not found + in the context dictionaries. See `~Configurator.context`. + + See also + -------- + Configurator.category + Configurator.fill + """ + return self._get_item_context(key, None if context else 0) + + def update(self, *args, **kwargs): + """ + Update several settings at once. + + Parameters + ---------- + *args : str or dict-like, optional + A dictionary containing `rc` keys and values. You can also pass + a "category" name as the first argument, in which case all + settings are prepended with ``'category.'``. For example, + ``rc.update('axes', labelsize=20, titlesize=20)`` changes the + :rcraw:`axes.labelsize` and :rcraw:`axes.titlesize` settings. + **kwargs + `rc` keys and values passed as keyword arguments. + If the name has dots, simply omit them. + + See also + -------- + Configurator.category + Configurator.fill + """ + prefix, kw = "", {} + if not args: + pass + elif len(args) == 1 and isinstance(args[0], str): + prefix = args[0] + elif len(args) == 1 and isinstance(args[0], dict): + kw = args[0] + elif len(args) == 2 and isinstance(args[0], str) and isinstance(args[1], dict): + prefix, kw = args + else: + raise ValueError( + f"Invalid arguments {args!r}. Usage is either " + "rc.update(dict), rc.update(kwy=value, ...), " + "rc.update(category, dict), or rc.update(category, key=value, ...)." + ) + prefix = prefix and prefix + "." + kw.update(kwargs) + for key, value in kw.items(): + self.__setitem__(prefix + key, value) + + @docstring._snippet_manager + def reset(self, local=True, user=True, default=True, **kwargs): + """ + Reset the configurator to its initial state. + + Parameters + ---------- + %(rc.params)s + """ + self._init(local=local, user=user, default=default, **kwargs) + + def _load_file(self, path): + """ + Return dictionaries of ultraplot and matplotlib settings loaded from the file. + """ + # WARNING: Critical to not yet apply _get_item_dicts() syncing or else we + # can overwrite input settings (e.g. label.size followed by font.size). + path = os.path.expanduser(path) + added = set() + rcdict = {} + with open(path, "r") as fh: + for idx, line in enumerate(fh): + # Strip comments + message = f"line #{idx + 1} in file {path!r}" + stripped = line.split("#", 1)[0].strip() + if not stripped: + pass # no warning + continue + # Parse the pair + pair = stripped.split(":", 1) + if len(pair) != 2: + warnings._warn_ultraplot(f'Illegal {message}:\n{line}"') + continue + # Detect duplicates + key, value = map(str.strip, pair) + if key in added: + warnings._warn_ultraplot(f"Duplicate rc key {key!r} on {message}.") + added.add(key) + # Get child dictionaries. Careful to have informative messages + with warnings.catch_warnings(): + warnings.simplefilter("error", warnings.UltraPlotWarning) + try: + key, value = self._validate_key(key, value) + value = self._validate_value(key, value) + except KeyError: + warnings.simplefilter("default", warnings.UltraPlotWarning) + warnings._warn_ultraplot( + f"Invalid rc key {key!r} on {message}." + ) + continue + except ValueError as err: + warnings.simplefilter("default", warnings.UltraPlotWarning) + warnings._warn_ultraplot( + f"Invalid rc value {value!r} for key {key!r} on {message}: {err}" + ) # noqa: E501 + continue + except warnings.UltraPlotWarning as err: + warnings.simplefilter("default", warnings.UltraPlotWarning) + warnings._warn_ultraplot( + f"Outdated rc key {key!r} on {message}: {err}" + ) # noqa: E501 + warnings.simplefilter("ignore", warnings.UltraPlotWarning) + key, value = self._validate_key(key, value) + value = self._validate_value(key, value) + # Update the settings + rcdict[key] = value + + return rcdict + + def load(self, path): + """ + Load settings from the specified file. + + Parameters + ---------- + path : path-like + The file path. + + See also + -------- + Configurator.save + """ + rcdict = self._load_file(path) + for key, value in rcdict.items(): + self.__setitem__(key, value) + + @staticmethod + def _save_rst(path): + """ + Create an RST table file. Used for online docs. + """ + string = rcsetup._rst_table() + with open(path, "w") as fh: + fh.write(string) + + @staticmethod + def _save_yaml(path, user_dict=None, *, comment=False, description=False): + """ + Create a YAML file. Used for online docs and default and user-generated + ultraplotrc files. Extra settings can be passed with the input dictionary. + """ + user_table = () + if user_dict: # add always-uncommented user settings + user_table = rcsetup._yaml_table(user_dict, comment=False) + user_table = ("# Changed settings", user_table, "") + ultraplot_dict = ( + rcsetup._rc_ultraplot_table + if description + else rcsetup._rc_ultraplot_default + ) # noqa: E501 + ultraplot_table = rcsetup._yaml_table( + ultraplot_dict, comment=comment, description=description + ) # noqa: E501 + ultraplot_table = ("# ultraplot settings", ultraplot_table, "") + matplotlib_dict = rcsetup._rc_matplotlib_default + matplotlib_table = rcsetup._yaml_table(matplotlib_dict, comment=comment) + matplotlib_table = ("# Matplotlib settings", matplotlib_table) + parts = ( + "#--------------------------------------------------------------------", + "# Use this file to change the default ultraplot and matplotlib settings.", + "# The syntax is identical to matplotlibrc syntax. For details see:", + "# https://ultraplot.readthedocs.io/en/latest/configuration.html", + "# https://matplotlib.org/stable/tutorials/introductory/customizing.html", + "#--------------------------------------------------------------------", + *user_table, # empty if nothing passed + *ultraplot_table, + *matplotlib_table, + ) + with open(path, "w") as fh: + fh.write("\n".join(parts)) + + def save(self, path=None, user=True, comment=None, backup=True, description=False): + """ + Save the current settings to a ``ultraplotrc`` file. This writes + the default values commented out plus the values that *differ* + from the defaults at the top of the file. + + Parameters + ---------- + path : path-like, default: 'ultraplotrc' + The file name and/or directory. The default file name is ``ultraplotrc`` + and the default directory is the current directory. + user : bool, default: True + If ``True`` then settings that have been `~Configurator.changed` from + the ultraplot defaults are shown uncommented at the top of the file. + backup : bool, default: True + Whether to "backup" an existing file by renaming with the suffix ``.bak`` + or overwrite an existing file. + comment : bool, optional + Whether to comment out the default settings. If not passed + this takes the same value as `user`. + description : bool, default: False + Whether to include descriptions of each setting (as seen in the + :ref:`user guide table `) as comments. + + See also + -------- + Configurator.load + Configurator.changed + """ + path = os.path.expanduser(path or ".") + if os.path.isdir(path): # includes '' + path = os.path.join(path, "ultraplotrc") + if os.path.isfile(path) and backup: + backup = path + ".bak" + os.rename(path, backup) + warnings._warn_ultraplot(f"Existing file {path!r} was moved to {backup!r}.") + comment = _not_none(comment, user) + user_dict = self.changed if user else None + self._save_yaml(path, user_dict, comment=comment, description=description) + + @property + def _context_mode(self): + """ + Return the highest (least permissive) context mode. + """ + return max((context.mode for context in self._context), default=0) + + @property + def changed(self): + """ + A dictionary of settings that have changed from the ultraplot defaults. + + See also + -------- + Configurator.save + """ + # Carefully detect changed settings + rcdict = {} + for key, value in self.items(): + default = rcsetup._get_default_param(key) + if ( + isinstance(value, Real) + and isinstance(default, Real) + and np.isclose(value, default) + ): # noqa: E501 + pass + elif value == default: + pass + else: + rcdict[key] = value + # Ignore non-style-related settings. See mstyle.STYLE_BLACKLIST + # TODO: For now not sure how to detect if prop cycle changed since + # we cannot load it from _cmap_database in rcsetup. + rcdict.pop("interactive", None) # changed by backend + rcdict.pop("axes.prop_cycle", None) + return _filter_style_dict(rcdict, warn=False) + + +# Initialize locations +_init_user_folders() +_init_user_file() + +#: A dictionary-like container of matplotlib settings. Assignments are +#: validated and restricted to recognized setting names. +rc_matplotlib = mpl.rcParams # PEP8 4 lyfe + +#: A dictionary-like container of ultraplot settings. Assignments are +#: validated and restricted to recognized setting names. +rc_ultraplot = rcsetup._rc_ultraplot_default.copy() # a validated rcParams-style dict + +#: Instance of `Configurator`. This controls both `rc_matplotlib` and `rc_ultraplot` +#: settings. See the :ref:`configuration guide ` for details. +# Private bootstrap state; public config.py initializes it before exporting it. +rc = Configurator(local=False, user=False, default=False) +_INITIALIZED = False +_INITIALIZING = False + + +def _initialize(): + """Register configuration dependencies and load settings exactly once.""" + global _INITIALIZED, _INITIALIZING + if _INITIALIZED or _INITIALIZING: + return + _INITIALIZING = True + try: + from .internals import fonts as _fonts # noqa: F401 + from .internals.benchmarks import _benchmark + + with _benchmark("cmaps"): + register_cmaps(default=True) + with _benchmark("cycles"): + register_cycles(default=True) + with _benchmark("colors"): + register_colors(default=True) + with _benchmark("fonts"): + register_fonts(default=True) + + rcsetup.VALIDATE_REGISTERED_CMAPS = True + rcsetup.VALIDATE_REGISTERED_COLORS = True + rc._init(local=True, user=True, default=True) + _INITIALIZED = True + finally: + _INITIALIZING = False diff --git a/ultraplot/_interaction.py b/ultraplot/_interaction.py index 0f7a67ce3..51159f177 100644 --- a/ultraplot/_interaction.py +++ b/ultraplot/_interaction.py @@ -35,7 +35,7 @@ def _preview_enabled(): """Return the current runtime setting for approximate navigation frames.""" # Keep this local to avoid config -> figure -> animation import cycles. - from .config import rc_ultraplot + from ._config import rc_ultraplot return rc_ultraplot["navigation.preview"] diff --git a/ultraplot/axes/base.py b/ultraplot/axes/base.py index 5aea24ca1..6cb5e9e4f 100644 --- a/ultraplot/axes/base.py +++ b/ultraplot/axes/base.py @@ -53,7 +53,7 @@ _register_inset_colorbar_reflow, _solve_inset_colorbar_bounds, ) -from ..config import rc +from .._config import rc from ..internals import ( _alias_kwargs, _canonicalize_kwargs, diff --git a/ultraplot/axes/cartesian.py b/ultraplot/axes/cartesian.py index 3e12ae1e0..ca65776f8 100644 --- a/ultraplot/axes/cartesian.py +++ b/ultraplot/axes/cartesian.py @@ -18,7 +18,7 @@ from .. import constructor from .. import scale as pscale from .. import ticker as pticker -from ..config import rc +from .._config import rc from ..internals import ( _alias_kwargs, _canonicalize_kwargs, diff --git a/ultraplot/axes/container.py b/ultraplot/axes/container.py index 22cd87496..f0e0eeb9c 100644 --- a/ultraplot/axes/container.py +++ b/ultraplot/axes/container.py @@ -14,7 +14,7 @@ import numpy as np from matplotlib import cbook, container -from ..config import rc +from .._config import rc from ..internals import _pop_rc, _version_mpl, warnings from . import shared from .cartesian import CartesianAxes diff --git a/ultraplot/axes/geo.py b/ultraplot/axes/geo.py index 7deeff7be..daf72269d 100644 --- a/ultraplot/axes/geo.py +++ b/ultraplot/axes/geo.py @@ -34,7 +34,7 @@ from .. import constructor from .. import proj as pproj from .. import ticker as pticker -from ..config import rc +from .._config import rc from ..internals import ( _alias_kwargs, _not_none, diff --git a/ultraplot/axes/plot.py b/ultraplot/axes/plot.py index 405d027e0..268177adf 100644 --- a/ultraplot/axes/plot.py +++ b/ultraplot/axes/plot.py @@ -35,7 +35,7 @@ from .. import _sharing as psharing from .. import colors as pcolors from .. import constructor, utils -from ..config import rc +from .._config import rc from ..internals import ( _alias_kwargs, _canonicalize_kwargs, diff --git a/ultraplot/axes/plot_types/circlize.py b/ultraplot/axes/plot_types/circlize.py index e322564bc..56577ada2 100644 --- a/ultraplot/axes/plot_types/circlize.py +++ b/ultraplot/axes/plot_types/circlize.py @@ -13,7 +13,7 @@ from matplotlib.projections.polar import PolarAxes as MplPolarAxes from ... import constructor -from ...config import rc +from ..._config import rc from ...internals import warnings #: Settings ``pycirclize.config`` writes into the global rcParams the moment it diff --git a/ultraplot/axes/plot_types/raincloud.py b/ultraplot/axes/plot_types/raincloud.py index 3de04e9d3..fa55ace06 100644 --- a/ultraplot/axes/plot_types/raincloud.py +++ b/ultraplot/axes/plot_types/raincloud.py @@ -15,7 +15,7 @@ import matplotlib.colors as mcolors from ... import _sharing as psharing, colors as pcolors -from ...config import rc +from ..._config import rc from ...internals import _canonicalize_kwargs, _not_none, _pop_props, inputs, warnings if TYPE_CHECKING: diff --git a/ultraplot/axes/plot_types/sankey.py b/ultraplot/axes/plot_types/sankey.py index fb3b6180a..677541e1d 100644 --- a/ultraplot/axes/plot_types/sankey.py +++ b/ultraplot/axes/plot_types/sankey.py @@ -8,7 +8,7 @@ from matplotlib import patches as mpatches from matplotlib import path as mpath -from ...config import rc +from ..._config import rc from ...internals import _not_none diff --git a/ultraplot/axes/polar.py b/ultraplot/axes/polar.py index 456460dc6..152896dd6 100644 --- a/ultraplot/axes/polar.py +++ b/ultraplot/axes/polar.py @@ -17,7 +17,7 @@ from .. import constructor from .. import ticker as pticker -from ..config import rc +from .._config import rc from ..internals import ( _alias_kwargs, _not_none, diff --git a/ultraplot/axes/shared.py b/ultraplot/axes/shared.py index f7805d548..6bae94399 100644 --- a/ultraplot/axes/shared.py +++ b/ultraplot/axes/shared.py @@ -10,7 +10,7 @@ import numpy as np -from ..config import rc +from .._config import rc from .._sharing import ( AXIS_LABEL_FORMAT_KEYS, axis_supports_format_key, diff --git a/ultraplot/axes/taylor.py b/ultraplot/axes/taylor.py index 1816247e9..5b00f064f 100644 --- a/ultraplot/axes/taylor.py +++ b/ultraplot/axes/taylor.py @@ -10,7 +10,7 @@ import matplotlib.transforms as mtransforms import numpy as np -from ..config import rc +from .._config import rc from ..internals import _not_none, _pop_rc, docstring, _alias_kwargs from . import shared from .polar import PolarAxes diff --git a/ultraplot/colorbar.py b/ultraplot/colorbar.py index bb1f3e8d2..e2a194402 100644 --- a/ultraplot/colorbar.py +++ b/ultraplot/colorbar.py @@ -18,7 +18,7 @@ from . import constructor, colors as pcolors from .internals import _alias_kwargs, _not_none, _pop_params, guides, warnings -from .config import rc, _version_mpl +from ._config import rc, _version_mpl from .ultralayout import KIWI_AVAILABLE, ColorbarLayoutSolver from . import ticker as pticker from .utils import units diff --git a/ultraplot/colors.py b/ultraplot/colors.py index 2be4b6b65..9626aae04 100644 --- a/ultraplot/colors.py +++ b/ultraplot/colors.py @@ -31,7 +31,7 @@ import numpy as np import numpy.ma as ma -from .config import rc +from ._config import rc def _cycle_handler(value): diff --git a/ultraplot/config.py b/ultraplot/config.py index 600f002ee..d972196f7 100644 --- a/ultraplot/config.py +++ b/ultraplot/config.py @@ -1,50 +1,22 @@ -#!/usr/bin/env python3 -""" -Tools for setting up ultraplot and configuring global settings. -See the :ref:`configuration guide ` for details. -""" - -# NOTE: The matplotlib analogue to this file is actually __init__.py -# but it makes more sense to have all the setup actions in a separate file -# so the namespace of the top-level module is unpolluted. -# NOTE: Why also load colormaps and cycles in this file and not colors.py? -# Because I think it makes sense to have all the code that "runs" (i.e. not -# just definitions) in the same place, and I was having issues with circular -# dependencies and where import order of __init__.py was affecting behavior. -import logging -import os -import re -import sys -from collections import namedtuple -from collections.abc import MutableMapping -from numbers import Real -from typing import Any, Callable, Dict - -import cycler -import matplotlib as mpl -import matplotlib.colors as mcolors -import matplotlib.font_manager as mfonts -import matplotlib.mathtext # noqa: F401 -import matplotlib.style as mstyle -import numpy as np -from matplotlib import RcParams - -from .internals import ( - _not_none, - _pop_kwargs, - _pop_props, - _translate_grid, - _version_mpl, - docstring, - ic, # noqa: F401 - rcsetup, - warnings, +"""Public configuration API, initialized before its settings are exposed.""" + +from . import _config + +_config._initialize() + +from ._config import ( + Configurator, + rc, + rc_ultraplot, + rc_matplotlib, + use_style, + config_inline_backend, + register_cmaps, + register_cycles, + register_colors, + register_fonts, ) -# Suppress warnings emitted by mathtext.py (_mathtext.py in recent versions) -# when when substituting dummy unavailable glyph due to fallback disabled. -logging.getLogger("matplotlib.mathtext").setLevel(logging.ERROR) - __all__ = [ "Configurator", "rc", @@ -58,1853 +30,7 @@ "register_fonts", ] -# Constants -if hasattr(mstyle, "_STYLE_BLACKLIST"): # Matplotlib >= 3.11 - _STYLE_BLACKLIST = mstyle._STYLE_BLACKLIST -else: - from matplotlib.style.core import STYLE_BLACKLIST as _STYLE_BLACKLIST - -COLORS_KEEP = ("red", "green", "blue", "cyan", "yellow", "magenta", "white", "black") - -_ULTRAPLOT_STYLES = { - "poster": { - "font.size": 14, - "axes.titlesize": 18, - "axes.labelsize": 16, - "xtick.labelsize": 13, - "ytick.labelsize": 13, - "legend.fontsize": 13, - "figure.titlesize": 20, - "lines.linewidth": 2.0, - "lines.markersize": 6, - "figure.facecolor": "none", - "savefig.facecolor": "none", - "savefig.edgecolor": "none", - }, - "dark_background": { - "figure.facecolor": "#000000", - "figure.edgecolor": "#000000", - "axes.facecolor": "#000000", - "axes.edgecolor": "#cbd5e1", - "axes.labelcolor": "#f8fafc", - "text.color": "#f8fafc", - "xtick.color": "#cbd5e1", - "ytick.color": "#cbd5e1", - "grid.color": "#475569", - "grid.alpha": 0.35, - "legend.facecolor": "#000000", - "legend.edgecolor": "#475569", - "savefig.facecolor": "#000000", - "savefig.edgecolor": "#000000", - "axes.prop_cycle": cycler.cycler( - color=( - "#60a5fa", - "#f59e0b", - "#34d399", - "#f472b6", - "#a78bfa", - "#f87171", - ) - ), - }, -} -_ULTRAPLOT_STYLES["dark"] = _ULTRAPLOT_STYLES["dark_background"] - -# Configurator docstrings -_rc_docstring = """ -local : bool, default: True - Whether to load settings from the `~Configurator.local_files` file. -user : bool, default: True - Whether to load settings from the `~Configurator.user_file` file. -default : bool, default: True - Whether to reload built-in default ultraplot settings. -""" -docstring._snippet_manager["rc.params"] = _rc_docstring - -# Registration docstrings -_shared_docstring = """ -*args : path-spec or `~ultraplot.colors.{type}Colormap`, optional - The {objects} to register. These can be file paths containing - RGB data or `~ultraplot.colors.{type}Colormap` instances. By default, - if positional arguments are passed, then `user` is set to ``False``. - - Valid file extensions are listed in the below table. Note that {objects} - are registered according to their filenames -- for example, ``name.xyz`` - will be registered as ``'name'``. -""" # noqa: E501 -_cmap_exts_docstring = """ - =================== ========================================== - Extension Description - =================== ========================================== - ``.json`` JSON database of the channel segment data. - ``.hex`` Comma-delimited list of HEX strings. - ``.rgb``, ``.txt`` 3-4 column table of channel values. - =================== ========================================== -""" -_cycle_exts_docstring = """ - ================== ========================================== - Extension Description - ================== ========================================== - ``.hex`` Comma-delimited list of HEX strings. - ``.rgb``, ``.txt`` 3-4 column table of channel values. - ================== ========================================== -""" -_color_docstring = """ -*args : path-like or dict, optional - The colors to register. These can be file paths containing RGB data or - dictionary mappings of names to RGB values. By default, if positional - arguments are passed, then `user` is set to ``False``. Files must have - the extension ``.txt`` and should contain one line per color in the - format ``name : hex``. Whitespace is ignored. -""" -_font_docstring = """ -*args : path-like, optional - The font files to add. By default, if positional arguments are passed, then - `user` is set to ``False``. Files must have the extensions ``.ttf`` or ``.otf``. - See `this link \ -`__ - for a guide on converting other font files to ``.ttf`` and ``.otf``. -""" -_register_docstring = """ -user : bool, optional - Whether to reload {objects} from `~Configurator.user_folder`. Default is - ``False`` if positional arguments were passed and ``True`` otherwise. -local : bool, optional - Whether to reload {objects} from `~Configurator.local_folders`. Default is - ``False`` if positional arguments were passed and ``True`` otherwise. -default : bool, default: False - Whether to reload the default {objects} packaged with ultraplot. - Default is always ``False``. -""" -docstring._snippet_manager["rc.cmap_params"] = _register_docstring.format( - objects="colormaps" -) # noqa: E501 -docstring._snippet_manager["rc.cycle_params"] = _register_docstring.format( - objects="color cycles" -) # noqa: E501 -docstring._snippet_manager["rc.color_params"] = _register_docstring.format( - objects="colors" -) # noqa: E501 -docstring._snippet_manager["rc.font_params"] = _register_docstring.format( - objects="fonts" -) # noqa: E501 -docstring._snippet_manager["rc.cmap_args"] = _shared_docstring.format( - objects="colormaps", type="Continuous" -) # noqa: E501 -docstring._snippet_manager["rc.cycle_args"] = _shared_docstring.format( - objects="color cycles", type="Discrete" -) # noqa: E501 -docstring._snippet_manager["rc.color_args"] = _color_docstring -docstring._snippet_manager["rc.font_args"] = _font_docstring -docstring._snippet_manager["rc.cmap_exts"] = _cmap_exts_docstring -docstring._snippet_manager["rc.cycle_exts"] = _cycle_exts_docstring - -_rc_register_handler_docstring = """ - Register a callback function to be executed when a setting is modified. - - This is an extension point for "special" settings that require complex - logic or have side-effects, such as updating other matplotlib settings. - It is used internally to decouple the configuration system from other - subsystems and avoid circular imports. - - Parameters - ---------- - name : str - The name of the setting (e.g., ``'cycle'``). - func : callable - The handler function to be executed. The function must accept a - single positional argument, which is the new `value` of the - setting, and must return a dictionary. The keys of the dictionary - should be valid ``matplotlib`` rc setting names, and the values - will be applied to the ``rc_matplotlib`` object. - - Example - ------- - >>> def _cycle_handler(value): - ... # ... logic to create a cycler object from the value ... - ... return {'axes.prop_cycle': new_cycler} - >>> rc.register_handler('cycle', _cycle_handler) - """ -docstring._snippet_manager["rc.register_handler"] = _rc_register_handler_docstring - - -def _init_user_file(): - """ - Initialize .ultraplotrc file. - """ - file = Configurator.user_file() - if not os.path.exists(file): - Configurator._save_yaml(file, comment=True) - - -def _init_user_folders(): - """ - Initialize .ultraplot folder. - """ - for subfolder in ("", "cmaps", "cycles", "colors", "fonts"): - folder = Configurator.user_folder(subfolder) - if not os.path.isdir(folder): - os.mkdir(folder) - - -def _get_data_folders(folder, user=True, local=True, default=True, reverse=False): - """ - Return data folder paths in reverse order of precedence. - """ - # When loading colormaps, cycles, and colors, files in the latter - # directories overwrite files in the former directories. When loading - # fonts, the resulting paths need to be *reversed*. - paths = [] - if default: - paths.append(os.path.join(os.path.dirname(__file__), folder)) - if user: - paths.append(Configurator.user_folder(folder)) - if local: - paths.extend(Configurator.local_folders(folder)) - if reverse: - paths = paths[::-1] - return paths - - -def _iter_data_objects(folder, *args, **kwargs): - """ - Iterate over input objects and files in the data folders that should be - registered. Also yield an index indicating whether these are user files. - """ - i = 0 # default files - for i, path in enumerate(_get_data_folders(folder, **kwargs)): - for dirname, dirnames, filenames in os.walk(path): - for filename in filenames: - if filename[0] == ".": # UNIX-style hidden files - continue - path = os.path.join(dirname, filename) - yield i, path - i += 1 # user files - for path in args: - path = os.path.expanduser(path) - if os.path.isfile(path): - yield i, path - else: - raise FileNotFoundError(f"Invalid file path {path!r}.") - - -def _filter_style_dict(rcdict, warn=True): - """ - Filter out blacklisted style parameters. - """ - # NOTE: This implements bugfix: https://github.com/matplotlib/matplotlib/pull/17252 - # This fix is *critical* for ultraplot because we always run style.use() - # when the configurator is made. Without fix backend is reset every time - # you import ultraplot in jupyter notebooks. So apply retroactively. - rcdict_filtered = {} - for key in rcdict: - if key in _STYLE_BLACKLIST: - if warn: - warnings._warn_ultraplot( - f"Dictionary includes a parameter, {key!r}, that is not related " - "to style. Ignoring." - ) - else: - rcdict_filtered[key] = rcdict[key] - return rcdict_filtered - - -def _get_default_style_dict(): - """ - Get the default rc parameters dictionary with deprecated parameters filtered. - """ - # NOTE: Use RcParams update to filter and translate deprecated settings - # before actually applying them to rcParams down pipeline. This way we can - # suppress warnings for deprecated default params but still issue warnings - # when user-supplied stylesheets have deprecated params. - # WARNING: Some deprecated rc params remain in dictionary as None so we - # filter them out. Beware if hidden attribute changes. - # WARNING: The examples.directory deprecation was handled specially inside - # RcParams in early versions. Manually pop it out here. - rcdict = _filter_style_dict(mpl.rcParamsDefault, warn=False) - with warnings.catch_warnings(): - warnings.simplefilter("ignore", mpl.MatplotlibDeprecationWarning) - rcdict = dict(RcParams(rcdict)) - for attr in ("_deprecated_set", "_deprecated_remain_as_none"): - deprecated = getattr(mpl, attr, ()) - for key in deprecated: # _deprecated_set is in matplotlib < 3.4 - rcdict.pop(key, None) - rcdict.pop("examples.directory", None) # special case for matplotlib < 3.2 - return rcdict - - -def _get_style_dict(style, filter=True): - """ - Return a dictionary of settings belonging to the requested style(s). If `filter` - is ``True``, invalid style parameters like `backend` are filtered out. - """ - # NOTE: This is adapted from matplotlib source for the following changes: - # 1. Add an 'original' pseudo style. Like rcParamsOrig except we also reload - # from the user matplotlibrc file. - # 2. When the style is changed we reset to the default state ignoring matplotlibrc. - # By contrast matplotlib applies styles on top of current state (including - # matplotlibrc changes and runtime rcParams changes) but the word 'style' - # implies a rigid static format. This makes more sense. - # 3. Add a separate function that returns lists of style dictionaries so that - # we can modify the active style in a context block. ultraplot context is more - # conservative than matplotlib's rc_context because it gets called a lot - # (e.g. every time you make an axes and every format() call). Instead of - # copying the entire rcParams dict we just track the keys that were changed. - style_aliases = { - "538": "fivethirtyeight", - "dark": "dark_background", - "mpl20": "default", - "mpl15": "classic", - "original": mpl.matplotlib_fname(), - "seaborn": "seaborn-v0_8", - } - - # Always apply the default style *first* so styles are rigid - kw_matplotlib = _get_default_style_dict() - if style == "default" or style is mpl.rcParamsDefault: - return kw_matplotlib - - # Apply limited deviations from the matplotlib style that we want to propagate to - # other styles. Want users selecting stylesheets to have few surprises, so - # currently just enforce the new aesthetically pleasing fonts. - kw_matplotlib["font.family"] = "sans-serif" - for fmly in ("serif", "sans-serif", "monospace", "cursive", "fantasy"): - kw_matplotlib["font." + fmly] = rcsetup._rc_matplotlib_default["font." + fmly] - - # Apply user input style(s) one by one - if isinstance(style, str) or isinstance(style, dict): - styles = [style] - else: - styles = style - for style in styles: - if isinstance(style, dict): - kw = style - elif isinstance(style, str): - style = style_aliases.get(style, style) - if style in _ULTRAPLOT_STYLES: - kw = _ULTRAPLOT_STYLES[style] - elif style in mstyle.library: - kw = mstyle.library[style] - else: - try: - kw = mpl.rc_params_from_file(style, use_default_template=False) - except IOError: - raise IOError( - f"Style {style!r} not found in the style library and input " - "is not a valid URL or file path. Available styles are: " - + ", ".join(map(repr, mstyle.available)) - + "." - ) - else: - raise ValueError(f"Invalid style {style!r}. Must be string or dictionary.") - if filter: - kw = _filter_style_dict(kw, warn=True) - kw_matplotlib.update(kw) - - return kw_matplotlib - - -def _infer_ultraplot_dict(kw_params): - """ - Infer values for ultraplot's "added" parameters from stylesheet parameters. - """ - kw_ultraplot = {} - mpl_to_ultraplot = { - "xtick.labelsize": ( - "tick.labelsize", - "grid.labelsize", - ), - "ytick.labelsize": ( - "tick.labelsize", - "grid.labelsize", - ), - "axes.titlesize": ( - "abc.size", - "suptitle.size", - "title.size", - "leftlabel.size", - "rightlabel.size", - "toplabel.size", - "bottomlabel.size", - ), - "text.color": ( - "abc.color", - "suptitle.color", - "title.color", - "tick.labelcolor", - "grid.labelcolor", - "leftlabel.color", - "rightlabel.color", - "toplabel.color", - "bottomlabel.color", - ), - } - for key, params in mpl_to_ultraplot.items(): - if key in kw_params: - value = kw_params[key] - for param in params: - kw_ultraplot[param] = value - return kw_ultraplot - - -def config_inline_backend(fmt=None): - """ - Set up the ipython `inline backend display format \ -`__ - and ensure that inline figures always look the same as saved figures. - This runs the following ipython magic commands: - - .. code-block:: ipython - - %%config InlineBackend.figure_formats = rc['inlineformat'] - %%config InlineBackend.rc = {} # never override rc settings - %%config InlineBackend.close_figures = True # cells start with no active figures - %%config InlineBackend.print_figure_kwargs = {'bbox_inches': None} - - When the inline backend is inactive or unavailable, this has no effect. - This function is called when you modify the :rcraw:`inlineformat` property. - - Parameters - ---------- - fmt : str or sequence, default: :rc:`inlineformat` - The inline backend file format or a list thereof. Valid formats - include ``'jpg'``, ``'png'``, ``'svg'``, ``'pdf'``, and ``'retina'``. - - See also - -------- - Configurator - """ - # Note if inline backend is unavailable this will fail silently - try: - from IPython import get_ipython - except ImportError: - return - ipython = get_ipython() - if ipython is None: - return - fmt = _not_none(fmt, rc_ultraplot["inlineformat"]) - if isinstance(fmt, str): - fmt = [fmt] - elif np.iterable(fmt): - fmt = list(fmt) - else: - raise ValueError(f"Invalid inline backend format {fmt!r}. Must be string.") - - # IPython < 9.0.0 - if hasattr(ipython, "magic"): - ipython.magic("config InlineBackend.figure_formats = " + repr(fmt)) - ipython.magic("config InlineBackend.rc = {}") - ipython.magic("config InlineBackend.close_figures = True") - ipython.magic( - "config InlineBackend.print_figure_kwargs = {'bbox_inches': None}" - ) - - # IPython >= 9.0.0 - else: - ipython.run_line_magic("config", "InlineBackend.figure_formats = " + repr(fmt)) - ipython.run_line_magic("config", "InlineBackend.rc = {}") - ipython.run_line_magic("config", "InlineBackend.close_figures = True") - ipython.run_line_magic( - "config", "InlineBackend.print_figure_kwargs = {'bbox_inches': None}" - ) - - -def use_style(style): - """ - Apply the `matplotlib style(s) \ -`__ - with `matplotlib.style.use`. This function is - called when you modify the :rcraw:`style` property. - - Parameters - ---------- - style : str or sequence or dict-like - The matplotlib style name(s) or stylesheet filename(s), or dictionary(s) - of settings. Use ``'default'`` to apply matplotlib default settings and - ``'original'`` to include settings from your user ``matplotlibrc`` file. - - See also - -------- - Configurator - matplotlib.style.use - """ - # NOTE: This function is not really necessary but makes ultraplot's - # stylesheet-supporting features obvious. Plus changing the style does - # so much *more* than changing rc params or quick settings, so it is - # nice to have dedicated function instead of just another rc_param name. - kw_matplotlib = _get_style_dict(style) - rc_matplotlib.update(kw_matplotlib) - rc_ultraplot.update(_infer_ultraplot_dict(kw_matplotlib)) - - -@docstring._snippet_manager -def register_cmaps(*args, user=None, local=None, default=False): - """ - Register named colormaps. This is called on import. - - Parameters - ---------- - %(rc.cmap_args)s - - %(rc.cmap_exts)s - - %(rc.cmap_params)s - - See also - -------- - register_cycles - register_colors - register_fonts - ultraplot.demos.show_cmaps - """ - # Register input colormaps - from . import colors as pcolors - - user = _not_none(user, not bool(args)) # skip user folder if input args passed - local = _not_none(local, not bool(args)) - paths = [] - for arg in args: - if isinstance(arg, mcolors.Colormap): - pcolors._cmap_database.register(arg, name=arg.name) - else: - paths.append(arg) - - # Register data files - for i, path in _iter_data_objects( - "cmaps", *paths, user=user, local=local, default=default - ): - name, ext = os.path.splitext(os.path.basename(path)) - if ext and ext[1:] in ("json", "txt", "rgb", "xml", "hex"): - pcolors._cmap_database.register_lazy( - name, path, "continuous", is_default=(i == 0) - ) - - -@docstring._snippet_manager -def register_cycles(*args, user=None, local=None, default=False): - """ - Register named color cycles. This is called on import. - - Parameters - ---------- - %(rc.cycle_args)s - - %(rc.cycle_exts)s - - %(rc.cycle_params)s - - See also - -------- - register_cmaps - register_colors - register_fonts - ultraplot.demos.show_cycles - """ - # Register input color cycles - from . import colors as pcolors - - user = _not_none(user, not bool(args)) # skip user folder if input args passed - local = _not_none(local, not bool(args)) - paths = [] - for arg in args: - if isinstance(arg, mcolors.Colormap): - pcolors._cmap_database.register(arg, name=arg.name) - else: - paths.append(arg) - - # Register data files - for _, path in _iter_data_objects( - "cycles", *paths, user=user, local=local, default=default - ): - name, ext = os.path.splitext(os.path.basename(path)) - if ext and ext[1:] in ("hex", "rgb", "txt"): - pcolors._cmap_database.register_lazy(name, path, "discrete") - - -@docstring._snippet_manager -def register_colors( - *args, user=None, local=None, default=False, space=None, margin=None, **kwargs -): - """ - Register named colors. This is called on import. - - Parameters - ---------- - %(rc.color_args)s - %(rc.color_params)s - space : {'hcl', 'hsl', 'hpl'}, optional - The colorspace used to pick "perceptually distinct" colors from - the `XKCD color survey `__. - If passed then `default` is set to ``True``. - margin : float, default: 0.1 - The margin used to pick "perceptually distinct" colors from the - `XKCD color survey `__. The normalized hue, - saturation, and luminance of each color must differ from the channel - values of the prededing colors by `margin` in order to be registered. - Must fall between ``0`` and ``1`` (``0`` will register all colors). - If passed then `default` is set to ``True``. - **kwargs - Additional color name specifications passed as keyword arguments rather - than positional argument dictionaries. - - See also - -------- - register_cmaps - register_cycles - register_fonts - ultraplot.demos.show_colors - """ - from . import colors as pcolors - - default = default or space is not None or margin is not None - margin = _not_none(margin, 0.1) - space = _not_none(space, "hcl") - - # Remove previously registered colors - # NOTE: Try not to touch matplotlib colors for compatibility - srcs = {"xkcd": pcolors.COLORS_XKCD, "opencolor": pcolors.COLORS_OPEN} - if default: # possibly slow but not these dicts are empty on startup - for src in srcs.values(): - for key in src: - if key not in COLORS_KEEP: - pcolors._color_database.pop(key, None) # this also clears cache - src.clear() - - # Register input colors - user = _not_none(user, not bool(args) and not bool(kwargs)) # skip if args passed - local = _not_none(local, not bool(args) and not bool(kwargs)) - paths = [] - for arg in args: - if isinstance(arg, dict): - kwargs.update(arg) - else: - paths.append(arg) - for key, color in kwargs.items(): - if mcolors.is_color_like(color): - pcolors._color_database[key] = mcolors.to_rgba(color) - else: - raise ValueError(f"Invalid color {key}={color!r}.") - - # Load colors from file and get their HCL values - # NOTE: Colors that come *later* overwrite colors that come earlier. - for i, path in _iter_data_objects( - "colors", *paths, user=user, local=local, default=default - ): - loaded = pcolors._load_colors(path, warn_on_failure=True) - if i == 0: - cat, _ = os.path.splitext(os.path.basename(path)) - if cat not in srcs: - raise RuntimeError(f"Unknown ultraplot color database {path!r}.") - src = srcs[cat] - if cat == "xkcd": - for key in COLORS_KEEP: - loaded[key] = pcolors._color_database[key] # keep the same - loaded = pcolors._standardize_colors(loaded, space, margin) - src.clear() - src.update(loaded) # needed for demos.show_colors() - pcolors._color_database.update(loaded) - - -@docstring._snippet_manager -def register_fonts(*args, user=True, local=True, default=False): - """ - Register font families. This is called on import. - - Parameters - ---------- - %(rc.font_args)s - %(rc.font_params)s - - See also - -------- - register_cmaps - register_cycles - register_colors - ultraplot.demos.show_fonts - """ - # Find ultraplot fonts - # WARNING: Must search data files in reverse because font manager will - # not overwrite existing fonts with user-input fonts. - # WARNING: If you include a font file with an unrecognized style, - # matplotlib may use that font instead of the 'normal' one! Valid styles: - # 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', - # 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black' - # https://matplotlib.org/api/font_manager_api.html - # For macOS the only fonts with 'Thin' in one of the .ttf file names - # are Helvetica Neue and .SF NS Display Condensed. Never try to use these! - paths_ultraplot = _get_data_folders( - "fonts", user=user, local=local, default=default, reverse=True - ) - fnames_ultraplot = set(mfonts.findSystemFonts(paths_ultraplot)) - for path in args: - path = os.path.expanduser(path) - if os.path.isfile(path): - fnames_ultraplot.add(path) - else: - raise FileNotFoundError(f"Invalid font file path {path!r}.") - - # Detect user-input ttc fonts and issue warning - fnames_ultraplot_ttc = { - file for file in fnames_ultraplot if os.path.splitext(file)[1] == ".ttc" - } - if fnames_ultraplot_ttc: - warnings._warn_ultraplot( - "Ignoring the following .ttc fonts because they cannot be " - "saved into PDF or EPS files (see matplotlib issue #3135): " - + ", ".join(map(repr, sorted(fnames_ultraplot_ttc))) - + ". Please consider expanding them into separate .ttf files." - ) - - # Rebuild font cache only if necessary! Can be >50% of total import time! - fnames_all = {font.fname for font in mfonts.fontManager.ttflist} - fnames_ultraplot -= fnames_ultraplot_ttc - if not fnames_all >= fnames_ultraplot: - warnings._warn_ultraplot( - "Rebuilding font cache. This usually happens " - "after installing or updating ultraplot." - ) - if hasattr(mfonts.fontManager, "addfont"): - # Newer API lets us add font files manually and deprecates TTFPATH. However - # to cache fonts added this way, we must call json_dump explicitly. - # NOTE: Previously, cache filename was specified as _fmcache variable, but - # recently became inaccessible. Must reproduce mpl code instead. - # NOTE: Older mpl versions used fontList.json as the cache, but these - # versions also did not have 'addfont', so makes no difference. - for fname in fnames_ultraplot: - mfonts.fontManager.addfont(fname) - cache = os.path.join( - mpl.get_cachedir(), f"fontlist-v{mfonts.FontManager.__version__}.json" - ) - mfonts.json_dump(mfonts.fontManager, cache) - else: - # Older API requires us to modify TTFPATH - # NOTE: Previously we tried to modify TTFPATH before importing - # font manager with hope that it would load ultraplot fonts on - # initialization. But 99% of the time font manager just imports - # the FontManager from cache, so we would have to rebuild anyway. - paths = ":".join(paths_ultraplot) - if "TTFPATH" not in os.environ: - os.environ["TTFPATH"] = paths - elif paths not in os.environ["TTFPATH"]: - os.environ["TTFPATH"] += ":" + paths - mfonts._rebuild() - - # Remove ttc files and 'Thin' fonts *after* rebuild - # NOTE: 'Thin' filter is ugly kludge but without this matplotlib picks up on - # Roboto thin ttf files installed on the RTD server when compiling docs. - mfonts.fontManager.ttflist = [ - font - for font in mfonts.fontManager.ttflist - if os.path.splitext(font.fname)[1] != ".ttc" - and (_version_mpl >= "3.3" or "Thin" not in os.path.basename(font.fname)) - ] - - -class Configurator(MutableMapping, dict): - """ - A dictionary-like class for managing `matplotlib settings - `__ - stored in `rc_matplotlib` and :ref:`ultraplot settings ` - stored in `rc_ultraplot`. This class is instantiated as the `rc` object - on import. See the :ref:`user guide ` for details. - """ - - def __repr__(self): - cls = type("rc", (dict,), {}) # temporary class with short name - src = cls({key: val for key, val in rc_ultraplot.items() if "." not in key}) - return type(rc_matplotlib).__repr__(src).strip()[:-1] + ",\n ...\n })" - - def __str__(self): - cls = type("rc", (dict,), {}) # temporary class with short name - src = cls({key: val for key, val in rc_ultraplot.items() if "." not in key}) - return type(rc_matplotlib).__str__(src) + "\n..." - - def __iter__(self): - yield from rc_ultraplot # sorted ultraplot settings, ignoring deprecations - yield from rc_matplotlib # sorted matplotlib settings, ignoring deprecations - - def __len__(self): - return len(tuple(iter(self))) - - def __delitem__(self, key): # noqa: U100 - raise RuntimeError("rc settings cannot be deleted.") - - def __delattr__(self, attr): # noqa: U100 - raise RuntimeError("rc settings cannot be deleted.") - - @docstring._snippet_manager - def __init__(self, local=True, user=True, default=True, **kwargs): - """ - Parameters - ---------- - %(rc.params)s - """ - self._context = [] - self._setting_handlers = {} - self._init(local=local, user=user, default=default, **kwargs) - - @docstring._snippet_manager - def register_handler( - self, name: str, func: Callable[[Any], Dict[str, Any]] - ) -> None: - """ - %(rc.register_handler)s - """ - self._setting_handlers[name] = func - - def __getitem__(self, key): - """ - Return an `rc_matplotlib` or `rc_ultraplot` setting using dictionary notation - (e.g., ``value = uplt.rc[name]``). - """ - key, _ = self._validate_key(key) # might issue ultraplot removed/renamed error - try: - return rc_ultraplot[key] - except KeyError: - pass - return rc_matplotlib[key] # might issue matplotlib removed/renamed error - - def __setitem__(self, key, value): - """ - Modify an `rc_matplotlib` or `rc_ultraplot` setting using dictionary notation - (e.g., ``uplt.rc[name] = value``). - """ - kw_ultraplot, kw_matplotlib = self._get_item_dicts(key, value) - rc_ultraplot.update(kw_ultraplot) - rc_matplotlib.update(kw_matplotlib) - - def __getattr__(self, attr): - """ - Return an `rc_matplotlib` or `rc_ultraplot` setting using "dot" notation - (e.g., ``value = uplt.rc.name``). - """ - if attr[:1] == "_": - return super().__getattribute__(attr) # raise built-in error - else: - return self.__getitem__(attr) - - def __setattr__(self, attr, value): - """ - Modify an `rc_matplotlib` or `rc_ultraplot` setting using "dot" notation - (e.g., ``uplt.rc.name = value``). - """ - if attr[:1] == "_": - super().__setattr__(attr, value) - else: - self.__setitem__(attr, value) - - def __enter__(self): - """ - Apply settings from the most recent context block. - """ - if not self._context: - raise RuntimeError( - "rc object must be initialized for context block using rc.context()." - ) - context = self._context[-1] - kwargs = context.kwargs - rc_new = context.rc_new # used for context-based _get_item_context - rc_old = context.rc_old # used to re-apply settings without copying whole dict - for key, value in kwargs.items(): - try: - kw_ultraplot, kw_matplotlib = self._get_item_dicts(key, value) - except Exception as e: - self.__exit__() - raise e - - for rc_dict, kw_new in zip( - (rc_ultraplot, rc_matplotlib), - (kw_ultraplot, kw_matplotlib), - ): - for key, value in kw_new.items(): - rc_old[key] = rc_dict[key] - rc_new[key] = rc_dict[key] = value - - def __exit__(self, *args): # noqa: U100 - """ - Restore settings from the most recent context block. - """ - if not self._context: - raise RuntimeError( - "rc object must be initialized for context block using rc.context()." - ) - context = self._context[-1] - for key, value in context.rc_old.items(): - kw_ultraplot, kw_matplotlib = self._get_item_dicts(key, value) - rc_ultraplot.update(kw_ultraplot) - rc_matplotlib.update(kw_matplotlib) - del self._context[-1] - - def _init(self, *, local, user, default): - """ - Initialize the configurator. - """ - # Always remove context objects - self._context.clear() - - # Update from default settings - # NOTE: see _remove_blacklisted_style_params bugfix - if default: - rc_matplotlib.update(_get_style_dict("original", filter=False)) - rc_matplotlib.update(rcsetup._rc_matplotlib_default) - rc_ultraplot.update(rcsetup._rc_ultraplot_default) - for key, value in rc_ultraplot.items(): - kw_ultraplot, kw_matplotlib = self._get_item_dicts(key, value) - rc_matplotlib.update(kw_matplotlib) - rc_ultraplot.update(kw_ultraplot) - - # Update from user home - user_path = None - if user: - user_path = self.user_file() - if os.path.isfile(user_path): - self.load(user_path) - - # Update from local paths - if local: - local_paths = self.local_files() - for path in local_paths: - if path == user_path: # local files always have precedence - continue - self.load(path) - - @staticmethod - def _validate_key(key, value=None): - """ - Validate setting names and handle `rc_ultraplot` deprecations. - """ - # NOTE: Not necessary to check matplotlib key here because... not sure why. - # Think deprecated matplotlib keys are not involved in any synced settings. - # Also note _check_key includes special handling for some renamed keys. - if not isinstance(key, str): - raise KeyError(f"Invalid key {key!r}. Must be string.") - key = key.lower() - if "." not in key: - key = rcsetup._rc_nodots.get(key, key) - key, value = rc_ultraplot._check_key( - key, value - ) # may issue deprecation warning - return key, value - - @staticmethod - def _validate_value(key, value): - """ - Validate setting values and convert numpy ndarray to list if possible. - """ - # NOTE: Ideally would implicitly validate on subsequent assignment to rc - # dictionaries, but must explicitly do it here, so _get_item_dicts can - # work with e.g. 'tick.lenratio', so _get_item_dicts does not have to include - # deprecated name handling in its if statements, and so _load_file can - # catch errors and emit warnings with line number indications as files - # are being read rather than after the end of the file reading. - if isinstance(value, np.ndarray): - value = value.item() if value.size == 1 else value.tolist() - validate_matplotlib = getattr(rc_matplotlib, "validate", None) - validate_ultraplot = rc_ultraplot._validate - if validate_matplotlib is not None and key in validate_matplotlib: - value = validate_matplotlib[key](value) - elif key in validate_ultraplot: - value = validate_ultraplot[key](value) - return value - - def _get_item_context(self, key, mode=None): - """ - As with `~Configurator.__getitem__` but the search is limited based - on the context mode and ``None`` is returned if the key is not found. - """ - key, _ = self._validate_key(key) - if mode is None: - mode = self._context_mode - cache = tuple(context.rc_new for context in self._context) - if mode == 0: - rcdicts = (*cache, rc_ultraplot, rc_matplotlib) - elif mode == 1: - rcdicts = (*cache, rc_ultraplot) # added settings only! - elif mode == 2: - rcdicts = (*cache,) - else: - raise ValueError(f"Invalid caching mode {mode!r}.") - for rcdict in rcdicts: - if not rcdict: - continue - try: - return rcdict[key] - except KeyError: - continue - if mode == 0: # otherwise return None - raise KeyError(f"Invalid rc setting {key!r}.") - - def _get_item_dicts(self, key, value): - """ - Return dictionaries for updating the `rc_ultraplot` and `rc_matplotlib` - properties associated with this key. Used when setting items, entering - context blocks, or loading files. - """ - # Get validated key, value, and child keys - key, value = self._validate_key(key, value) - value = self._validate_value(key, value) - keys = (key,) + rcsetup._rc_children.get(key, ()) # settings to change - contains = lambda *args: any(arg in keys for arg in args) # noqa: E731 - - # Fill dictionaries of matplotlib and ultraplot settings - # NOTE: Raise key error right away so it can be caught by _load_file(). - # Also ignore deprecation warnings so we only get them *once* on assignment - kw_ultraplot = {} # custom properties - kw_matplotlib = {} # builtin properties - with warnings.catch_warnings(): - warnings.simplefilter("ignore", mpl.MatplotlibDeprecationWarning) - warnings.simplefilter("ignore", warnings.UltraPlotWarning) - for key_i in keys: - if key_i in rc_matplotlib: - kw_matplotlib[key_i] = value - elif key_i in rc_ultraplot: - kw_ultraplot[key_i] = value - else: - raise KeyError(f"Invalid rc setting {key_i!r}.") - - # Special key: configure inline backend - if contains("inlineformat"): - config_inline_backend(value) - - # Special key: apply stylesheet - elif contains("style"): - if value is not None: - ikw_matplotlib = _get_style_dict(value) - kw_matplotlib.update(ikw_matplotlib) - kw_ultraplot.update(_infer_ultraplot_dict(ikw_matplotlib)) - - # Generic handler for special properties - if key in self._setting_handlers: - kw_matplotlib.update(self._setting_handlers[key](value)) - - # Turning bounding box on should turn border off and vice versa - elif contains("abc.bbox", "title.bbox", "abc.border", "title.border"): - if value: - name, this = key.split(".") - other = "border" if this == "bbox" else "bbox" - kw_ultraplot[name + "." + other] = False - - # Fontsize - # NOTE: Re-application of e.g. size='small' uses the updated 'font.size' - elif contains("font.size"): - kw_ultraplot.update( - { - key: value - for key, value in rc_ultraplot.items() - if key in rcsetup.FONT_KEYS and value in mfonts.font_scalings - } - ) - kw_matplotlib.update( - { - key: value - for key, value in rc_matplotlib.items() - if key in rcsetup.FONT_KEYS and value in mfonts.font_scalings - } - ) - - # Tick length/major-minor tick length ratio - elif contains("tick.len", "tick.lenratio"): - if contains("tick.len"): - ticklen = value - ratio = rc_ultraplot["tick.lenratio"] - else: - ticklen = rc_ultraplot["tick.len"] - ratio = value - kw_matplotlib["xtick.minor.size"] = ticklen * ratio - kw_matplotlib["ytick.minor.size"] = ticklen * ratio - - # Spine width/major-minor tick width ratio - elif contains("tick.width", "tick.widthratio"): - if contains("tick.width"): - tickwidth = value - ratio = rc_ultraplot["tick.widthratio"] - else: - tickwidth = rc_ultraplot["tick.width"] - ratio = value - kw_matplotlib["xtick.minor.width"] = tickwidth * ratio - kw_matplotlib["ytick.minor.width"] = tickwidth * ratio - - # Gridline width - elif contains("grid.width", "grid.widthratio"): - if contains("grid.width"): - gridwidth = value - ratio = rc_ultraplot["grid.widthratio"] - else: - gridwidth = rc_ultraplot["grid.width"] - ratio = value - kw_ultraplot["gridminor.linewidth"] = gridwidth * ratio - kw_ultraplot["gridminor.width"] = gridwidth * ratio - - # Gridline toggling - elif contains("grid", "gridminor"): - b, which = _translate_grid( - value, "gridminor" if contains("gridminor") else "grid" - ) - kw_matplotlib["axes.grid"] = b - kw_matplotlib["axes.grid.which"] = which - - return kw_ultraplot, kw_matplotlib - - @staticmethod - def _get_axisbelow_zorder(axisbelow): - """ - Convert the `axisbelow` string to its corresponding `zorder`. - """ - if axisbelow is True: - zorder = 0.5 - elif axisbelow is False: - zorder = 2.5 - elif axisbelow in ("line", "lines"): - zorder = 1.5 - else: - raise ValueError(f"Unexpected axisbelow value {axisbelow!r}.") - return zorder - - def _get_background_props(self, patch_kw=None, native=True, **kwargs): - """ - Return background properties, optionally filtering the output dictionary - based on the context. - """ - # Deprecated behavior - context = native or self._context_mode == 2 - if patch_kw: - warnings._warn_ultraplot( - "'patch_kw' is no longer necessary as of ultraplot v0.8. " - "Pass the parameters as keyword arguments instead." - ) - kwargs.update(patch_kw) - - # Get user-input properties and changed rc settings - # NOTE: Here we use 'color' as an alias for just 'edgecolor' rather than - # both 'edgecolor' and 'facecolor' to match 'xcolor' and 'ycolor' arguments. - props = _pop_props(kwargs, "patch") - if "color" in props: - props.setdefault("edgecolor", props.pop("color")) - for key in ("alpha", "facecolor", "linewidth", "edgecolor"): - value = self.find("axes." + key, context=context) - if value is not None: - props.setdefault(key, value) - - # Partition properties into face and edge - kw_face = _pop_kwargs(props, "alpha", "facecolor") - kw_edge = _pop_kwargs(props, "edgecolor", "linewidth", "linestyle") - kw_edge["capstyle"] = "projecting" # NOTE: needed to fix cartopy bounds - if "color" in props: - kw_edge.setdefault("edgecolor", props.pop("color")) - if kwargs: - raise TypeError(f"Unexpected keyword argument(s): {kwargs!r}") - - return kw_face, kw_edge - - def _get_gridline_bool(self, grid=None, axis=None, which="major", native=True): - """ - Return major and minor gridline toggles from ``axes.grid``, ``axes.grid.which``, - and ``axes.grid.axis``, optionally returning `None` based on the context. - """ - # NOTE: If you pass 'grid' or 'gridminor' the native args are updated - # NOTE: Very careful to return not None only if setting was changed. - # Avoid unnecessarily triggering grid redraws (esp. bad for geo.py) - context = native or self._context_mode == 2 - grid_on = self.find("axes.grid", context=context) - which_on = self.find("axes.grid.which", context=context) - if grid_on is not None or which_on is not None: # if *one* was changed - axis_on = self["axes.grid.axis"] # always need this property - grid_on = _not_none(grid_on, self["axes.grid"]) - which_on = _not_none(which_on, self["axes.grid.which"]) - axis = _not_none(axis, "x") - axis_on = axis is None or axis_on in (axis, "both") - which_on = which_on in (which, "both") - grid = _not_none(grid, grid_on and axis_on and which_on) - return grid - - def _get_gridline_props(self, which="major", native=True, rebuild=False): - """ - Return gridline properties, optionally filtering the output dictionary - based on the context. - """ - # Line properties - # NOTE: Gridline zorder is controlled automatically by matplotlib but - # must be controlled manually for geographic projections - key = "grid" if which == "major" else "gridminor" - prefix = "grid_" if native else "" # for native gridlines use this prefix - context = not rebuild and (native or self._context_mode == 2) - kwlines = self.fill( - { - f"{prefix}alpha": f"{key}.alpha", - f"{prefix}color": f"{key}.color", - f"{prefix}linewidth": f"{key}.linewidth", - f"{prefix}linestyle": f"{key}.linestyle", - }, - context=context, - ) - axisbelow = self.find("axes.axisbelow", context=context) - if axisbelow is not None: - if native: # this is a native plot so use set_axisbelow() down the line - kwlines["axisbelow"] = axisbelow - else: # this is a geographic plot so apply with zorder - kwlines["zorder"] = self._get_axisbelow_zorder(axisbelow) - return kwlines - - def _get_label_props(self, native=True, **kwargs): - """ - Return the axis label properties, optionally filtering the output dictionary - based on the context. - """ - # Get the label settings - # NOTE: This permits passing arbitrary additional args to set_[xy]label() - context = native or self._context_mode == 2 - kw = self.fill( - { - "color": "axes.labelcolor", - "weight": "axes.labelweight", - "size": "axes.labelsize", - "family": "font.family", - "labelpad": "axes.labelpad", # read by set_xlabel/set_ylabel - }, - context=context, - ) - for key, value in kwargs.items(): - if value is not None: # allow e.g. color=None - kw[key] = value - return kw - - def _get_loc_string(self, string, axis=None, native=True): - """ - Return `tickloc` and `spineloc` location strings from the `rc` boolean toggles, - optionally returning `None` based on the context. - """ - context = native or self._context_mode == 2 - axis = _not_none(axis, "x") - opt1, opt2 = ("top", "bottom") if axis == "x" else ("left", "right") - b1 = self.find(f"{string}.{opt1}", context=context) - b2 = self.find(f"{string}.{opt2}", context=context) - if b1 is None and b2 is None: - return None - elif b1 and b2: - return "both" - elif b1: - return opt1 - elif b2: - return opt2 - else: - return "neither" - - def _get_tickline_props(self, axis=None, which="major", native=True, rebuild=False): - """ - Return the tick line properties, optionally filtering the output dictionary - based on the context. - """ - # Tick properties obtained with rc.category - # NOTE: This loads 'size', 'width', 'pad', 'bottom', and 'top' - axis = _not_none(axis, "x") - context = not rebuild and (native or self._context_mode == 2) - kwticks = self.category(f"{axis}tick.{which}", context=context) - kwticks.pop("visible", None) - - # NOTE: We pop visibility properties from the styling dictionary so that - # stylistic updates (like applying a dark_background theme) do not override - # the tick visibility logic strictly managed by ax._update_locs() and alternate axes. - for key in ("bottom", "top", "left", "right"): - kwticks.pop(key, None) - - for key in ("color", "direction"): - value = self.find(f"{axis}tick.{key}", context=context) - if value is not None: - kwticks[key] = value - return kwticks - - def _get_ticklabel_props(self, axis=None, native=True, rebuild=False): - """ - Return the tick label properties, optionally filtering the output dictionary - based on the context. - """ - # Geographic gridline labels use the ultraplot-only grid.label* settings, - # while native matplotlib tick labels use x/y tick rcParams. - sprefix = axis or "" - cprefix = sprefix if _version_mpl >= "3.4" else "" # new settings - context = not rebuild and (native or self._context_mode == 2) - color_key = f"{cprefix}tick.labelcolor" if native else "grid.labelcolor" - size_key = f"{sprefix}tick.labelsize" if native else "grid.labelsize" - weight_key = "tick.labelweight" if native else "grid.labelweight" - kwtext = self.fill( - { - "color": color_key, # native setting sometimes avail - "size": size_key, - "weight": weight_key, # native setting never avail - "family": "font.family", # apply manually - }, - context=context, - ) - if kwtext.get("color", None) == "inherit": - # Inheritence is not automatic for geographic - # gridline labels so we apply inheritence here. - kwtext["color"] = self[f"{sprefix}tick.color"] - return kwtext - - @staticmethod - def local_files(): - """ - Return locations of files named ``ultraplotrc`` in this directory and in parent - directories. "Hidden" files with a leading dot are also recognized. These are - automatically loaded when ultraplot is imported. - - See also - -------- - Configurator.user_file - Configurator.local_folders - """ - cdir = os.getcwd() - paths = [] - while cdir: # i.e. not root - for name in ("ultraplotrc", ".ultraplotrc"): - path = os.path.join(cdir, name) - if os.path.isfile(path): - paths.append(path) - ndir = os.path.dirname(cdir) - if ndir == cdir: # root - break - cdir = ndir - return paths[::-1] # sort from decreasing to increasing importantce - - @staticmethod - def local_folders(subfolder=None): - """ - Return locations of folders named ``ultraplot_cmaps``, ``ultraplot_cycles``, - ``ultraplot_colors``, and ``ultraplot_fonts`` in this directory and in parent - directories. "Hidden" folders with a leading dot are also recognized. Files - in these directories are automatically loaded when ultraplot is imported. - - See also - -------- - Configurator.user_folder - Configurator.local_files - """ - cdir = os.getcwd() - paths = [] - if subfolder is None: - subfolder = ("cmaps", "cycles", "colors", "fonts") - if isinstance(subfolder, str): - subfolder = (subfolder,) - while cdir: # i.e. not root - for prefix in ("ultraplot", ".ultraplot"): - for suffix in subfolder: - path = os.path.join(cdir, "_".join((prefix, suffix))) - if os.path.isdir(path): - paths.append(path) - ndir = os.path.dirname(cdir) - if ndir == cdir: # root - break - cdir = ndir - return paths[::-1] - - @staticmethod - def _config_folder(): - """ - Get the XDG ultraplot folder. - """ - home = os.path.expanduser("~") - base = os.environ.get("XDG_CONFIG_HOME") - if not base: - base = os.path.join(home, ".config") - if sys.platform.startswith(("linux", "freebsd")) and os.path.isdir(base): - configdir = os.path.join(base, "ultraplot") - else: - configdir = os.path.join(home, ".ultraplot") - return configdir - - @staticmethod - def user_file(): - """ - Return location of the default ultraplotrc file. On Linux, this is either - ``$XDG_CONFIG_HOME/ultraplot/ultraplotrc`` or ``~/.config/ultraplot/ultraplotrc`` - if the `XDG directory `__ - is unset. On other operating systems, this is ``~/.ultraplot/ultraplotrc``. The - location ``~/.ultraplotrc`` or ``~/.ultraplot/ultraplotrc`` is always returned if the - file exists, regardless of the operating system. If multiple valid locations - are found, a warning is raised. - - See also - -------- - Configurator.user_folder - Configurator.local_files - """ - # Support both loose files and files inside .ultraplot - file = os.path.join(Configurator.user_folder(), "ultraplotrc") - universal = os.path.join(os.path.expanduser("~"), ".ultraplotrc") - if os.path.isfile(universal): - if file != universal and os.path.isfile(file): - warnings._warn_ultraplot( - "Found conflicting default user ultraplotrc files at " - f"{universal!r} and {file!r}. Ignoring the second one." - ) - file = universal - return file - - @staticmethod - def user_folder(subfolder=None): - """ - Return location of the default ultraplot folder. On Linux, this - is either ``$XDG_CONFIG_HOME/ultraplot`` or ``~/.config/ultraplot`` - if the `XDG directory `__ - is unset. On other operating systems, this is ``~/.ultraplot``. The location - ``~/.ultraplot`` is always returned if the folder exists, regardless of the - operating system. If multiple valid locations are found, a warning is raised. - - See also - -------- - Configurator.user_file - Configurator.local_folders - """ - # Try the XDG standard location - # NOTE: This is borrowed from matplotlib.get_configdir - home = os.path.expanduser("~") - universal = folder = os.path.join(home, ".ultraplot") - if sys.platform.startswith(("linux", "freebsd")): - xdg = os.environ.get("XDG_CONFIG_HOME") - xdg = xdg or os.path.join(home, ".config") - folder = os.path.join(xdg, "ultraplot") - # Fallback to the loose ~/.ultraplot if it is present - # NOTE: This is critical or we might ignore previously stored settings! - if os.path.isdir(universal): - if folder != universal and os.path.isdir(folder): - warnings._warn_ultraplot( - "Found conflicting default user ultraplot folders at " - f"{universal!r} and {folder!r}. Ignoring the second one." - ) - folder = universal - # Return the folder - if subfolder: - folder = os.path.join(folder, subfolder) - return folder - - def context(self, *args, mode=0, file=None, **kwargs): - """ - Temporarily modify the rc settings in a "with as" block. - - Parameters - ---------- - *args - Dictionaries of `rc` keys and values. - file : path-like, optional - Filename from which settings should be loaded. - **kwargs - `rc` names and values passed as keyword arguments. - If the name has dots, simply omit them. - - Other parameters - ---------------- - mode : {0, 1, 2}, optional - The context mode. Dictates the behavior of `~Configurator.find`, - `~Configurator.fill`, and `~Configurator.category` within a - "with as" block when called with ``context=True``. - - The options are as follows: - - * ``mode=0``: Matplotlib's `rc_matplotlib` settings - and ultraplot's `rc_ultraplot` settings are all returned, - whether or not they are local to the "with as" block. - * ``mode=1``: Matplotlib's `rc_matplotlib` settings are only - returned if they are local to the "with as" block. For example, - if :rcraw:`axes.titlesize` was passed to `~Configurator.context`, - then ``uplt.rc.find('axes.titlesize', context=True)`` will return - this value, but ``uplt.rc.find('axes.titleweight', context=True)`` will - return ``None``. This is used internally when instantiating axes. - * ``mode=2``: Matplotlib's `rc_matplotlib` settings and ultraplot's - `rc_ultraplot` settings are only returned if they are local to the - "with as" block. This is used internally when formatting axes. - - Note - ---- - Context "modes" are primarily used internally but may also be useful for power - users. Mode ``1`` is used when `~ultraplot.axes.Axes.format` is called during - axes instantiation, and mode ``2`` is used when `~ultraplot.axes.Axes.format` - is manually called by users. The latter prevents successive calls to - `~ultraplot.axes.Axes.format` from constantly looking up and re-applying - unchanged settings and significantly increasing the runtime. - - Example - ------- - The below applies settings to axes in a specific figure using - `~Configurator.context`. - - >>> import ultraplot as uplt - >>> with uplt.rc.context(ticklen=5, metalinewidth=2): - >>> fig, ax = uplt.subplots() - >>> ax.plot(data) - - The below applies settings to a specific axes using - `~ultraplot.axes.Axes.format`, which uses `~Configurator.context` - internally. - - >>> import ultraplot as uplt - >>> fig, ax = uplt.subplots() - >>> ax.format(ticklen=5, metalinewidth=2) - """ - # Add input dictionaries - for arg in args: - if not isinstance(arg, dict): - raise ValueError(f"Non-dictionary argument {arg!r}.") - kwargs.update(arg) - - # Add settings from file - if file is not None: - kw = self._load_file(file) - kw = {key: value for key, value in kw.items() if key not in kwargs} - kwargs.update(kw) - - # Activate context object - if mode not in range(3): - raise ValueError(f"Invalid mode {mode!r}.") - cls = namedtuple("RcContext", ("mode", "kwargs", "rc_new", "rc_old")) - context = cls(mode=mode, kwargs=kwargs, rc_new={}, rc_old={}) - self._context.append(context) - return self - - def category(self, cat, *, trimcat=True, context=False): - """ - Return a dictionary of settings beginning with the substring ``cat + '.'``. - Optionally limit the search to the context level. - - Parameters - ---------- - cat : str, optional - The `rc` setting category. - trimcat : bool, default: True - Whether to trim ``cat`` from the key names in the output dictionary. - context : bool, default: False - If ``True``, then settings not found in the context dictionaries - are omitted from the output dictionary. See `~Configurator.context`. - - See also - -------- - Configurator.find - Configurator.fill - """ - kw = {} - if cat not in rcsetup._rc_categories: - raise ValueError( - f"Invalid rc category {cat!r}. Valid categories are: " - + ", ".join(map(repr, rcsetup._rc_categories)) - + "." - ) - for key in self: - if not re.match(rf"\A{cat}\.[^.]+\Z", key): - continue - value = self._get_item_context(key, None if context else 0) - if value is None: - continue - if trimcat: - key = re.sub(rf"\A{cat}\.", "", key) - kw[key] = value - return kw - - def fill(self, props, *, context=False): - """ - Return a dictionary filled with settings whose names match the string values - in the input dictionary. Optionally limit the search to the context level. - - Parameters - ---------- - props : dict-like - Dictionary whose values are setting names -- for example - ``rc.fill({'edgecolor': 'axes.edgecolor', 'facecolor': 'axes.facecolor'})``. - context : bool, default: False - If ``True``, then settings not found in the context dictionaries - are omitted from the output dictionary. See `~Configurator.context`. - - See also - -------- - Configurator.category - Configurator.find - """ - kw = {} - for key, value in props.items(): - item = self._get_item_context(value, None if context else 0) - if item is not None: - kw[key] = item - return kw - - def find(self, key, *, context=False): - """ - Return a single setting. Optionally limit the search to the context level. - - Parameters - ---------- - key : str - The single setting name. - context : bool, default: False - If ``True``, then ``None`` is returned if the setting is not found - in the context dictionaries. See `~Configurator.context`. - - See also - -------- - Configurator.category - Configurator.fill - """ - return self._get_item_context(key, None if context else 0) - - def update(self, *args, **kwargs): - """ - Update several settings at once. - - Parameters - ---------- - *args : str or dict-like, optional - A dictionary containing `rc` keys and values. You can also pass - a "category" name as the first argument, in which case all - settings are prepended with ``'category.'``. For example, - ``rc.update('axes', labelsize=20, titlesize=20)`` changes the - :rcraw:`axes.labelsize` and :rcraw:`axes.titlesize` settings. - **kwargs - `rc` keys and values passed as keyword arguments. - If the name has dots, simply omit them. - - See also - -------- - Configurator.category - Configurator.fill - """ - prefix, kw = "", {} - if not args: - pass - elif len(args) == 1 and isinstance(args[0], str): - prefix = args[0] - elif len(args) == 1 and isinstance(args[0], dict): - kw = args[0] - elif len(args) == 2 and isinstance(args[0], str) and isinstance(args[1], dict): - prefix, kw = args - else: - raise ValueError( - f"Invalid arguments {args!r}. Usage is either " - "rc.update(dict), rc.update(kwy=value, ...), " - "rc.update(category, dict), or rc.update(category, key=value, ...)." - ) - prefix = prefix and prefix + "." - kw.update(kwargs) - for key, value in kw.items(): - self.__setitem__(prefix + key, value) - - @docstring._snippet_manager - def reset(self, local=True, user=True, default=True, **kwargs): - """ - Reset the configurator to its initial state. - - Parameters - ---------- - %(rc.params)s - """ - self._init(local=local, user=user, default=default, **kwargs) - - def _load_file(self, path): - """ - Return dictionaries of ultraplot and matplotlib settings loaded from the file. - """ - # WARNING: Critical to not yet apply _get_item_dicts() syncing or else we - # can overwrite input settings (e.g. label.size followed by font.size). - path = os.path.expanduser(path) - added = set() - rcdict = {} - with open(path, "r") as fh: - for idx, line in enumerate(fh): - # Strip comments - message = f"line #{idx + 1} in file {path!r}" - stripped = line.split("#", 1)[0].strip() - if not stripped: - pass # no warning - continue - # Parse the pair - pair = stripped.split(":", 1) - if len(pair) != 2: - warnings._warn_ultraplot(f'Illegal {message}:\n{line}"') - continue - # Detect duplicates - key, value = map(str.strip, pair) - if key in added: - warnings._warn_ultraplot(f"Duplicate rc key {key!r} on {message}.") - added.add(key) - # Get child dictionaries. Careful to have informative messages - with warnings.catch_warnings(): - warnings.simplefilter("error", warnings.UltraPlotWarning) - try: - key, value = self._validate_key(key, value) - value = self._validate_value(key, value) - except KeyError: - warnings.simplefilter("default", warnings.UltraPlotWarning) - warnings._warn_ultraplot( - f"Invalid rc key {key!r} on {message}." - ) - continue - except ValueError as err: - warnings.simplefilter("default", warnings.UltraPlotWarning) - warnings._warn_ultraplot( - f"Invalid rc value {value!r} for key {key!r} on {message}: {err}" - ) # noqa: E501 - continue - except warnings.UltraPlotWarning as err: - warnings.simplefilter("default", warnings.UltraPlotWarning) - warnings._warn_ultraplot( - f"Outdated rc key {key!r} on {message}: {err}" - ) # noqa: E501 - warnings.simplefilter("ignore", warnings.UltraPlotWarning) - key, value = self._validate_key(key, value) - value = self._validate_value(key, value) - # Update the settings - rcdict[key] = value - - return rcdict - - def load(self, path): - """ - Load settings from the specified file. - - Parameters - ---------- - path : path-like - The file path. - - See also - -------- - Configurator.save - """ - rcdict = self._load_file(path) - for key, value in rcdict.items(): - self.__setitem__(key, value) - - @staticmethod - def _save_rst(path): - """ - Create an RST table file. Used for online docs. - """ - string = rcsetup._rst_table() - with open(path, "w") as fh: - fh.write(string) - - @staticmethod - def _save_yaml(path, user_dict=None, *, comment=False, description=False): - """ - Create a YAML file. Used for online docs and default and user-generated - ultraplotrc files. Extra settings can be passed with the input dictionary. - """ - user_table = () - if user_dict: # add always-uncommented user settings - user_table = rcsetup._yaml_table(user_dict, comment=False) - user_table = ("# Changed settings", user_table, "") - ultraplot_dict = ( - rcsetup._rc_ultraplot_table - if description - else rcsetup._rc_ultraplot_default - ) # noqa: E501 - ultraplot_table = rcsetup._yaml_table( - ultraplot_dict, comment=comment, description=description - ) # noqa: E501 - ultraplot_table = ("# ultraplot settings", ultraplot_table, "") - matplotlib_dict = rcsetup._rc_matplotlib_default - matplotlib_table = rcsetup._yaml_table(matplotlib_dict, comment=comment) - matplotlib_table = ("# Matplotlib settings", matplotlib_table) - parts = ( - "#--------------------------------------------------------------------", - "# Use this file to change the default ultraplot and matplotlib settings.", - "# The syntax is identical to matplotlibrc syntax. For details see:", - "# https://ultraplot.readthedocs.io/en/latest/configuration.html", - "# https://matplotlib.org/stable/tutorials/introductory/customizing.html", - "#--------------------------------------------------------------------", - *user_table, # empty if nothing passed - *ultraplot_table, - *matplotlib_table, - ) - with open(path, "w") as fh: - fh.write("\n".join(parts)) - - def save(self, path=None, user=True, comment=None, backup=True, description=False): - """ - Save the current settings to a ``ultraplotrc`` file. This writes - the default values commented out plus the values that *differ* - from the defaults at the top of the file. - - Parameters - ---------- - path : path-like, default: 'ultraplotrc' - The file name and/or directory. The default file name is ``ultraplotrc`` - and the default directory is the current directory. - user : bool, default: True - If ``True`` then settings that have been `~Configurator.changed` from - the ultraplot defaults are shown uncommented at the top of the file. - backup : bool, default: True - Whether to "backup" an existing file by renaming with the suffix ``.bak`` - or overwrite an existing file. - comment : bool, optional - Whether to comment out the default settings. If not passed - this takes the same value as `user`. - description : bool, default: False - Whether to include descriptions of each setting (as seen in the - :ref:`user guide table `) as comments. - - See also - -------- - Configurator.load - Configurator.changed - """ - path = os.path.expanduser(path or ".") - if os.path.isdir(path): # includes '' - path = os.path.join(path, "ultraplotrc") - if os.path.isfile(path) and backup: - backup = path + ".bak" - os.rename(path, backup) - warnings._warn_ultraplot(f"Existing file {path!r} was moved to {backup!r}.") - comment = _not_none(comment, user) - user_dict = self.changed if user else None - self._save_yaml(path, user_dict, comment=comment, description=description) - - @property - def _context_mode(self): - """ - Return the highest (least permissive) context mode. - """ - return max((context.mode for context in self._context), default=0) - - @property - def changed(self): - """ - A dictionary of settings that have changed from the ultraplot defaults. - - See also - -------- - Configurator.save - """ - # Carefully detect changed settings - rcdict = {} - for key, value in self.items(): - default = rcsetup._get_default_param(key) - if ( - isinstance(value, Real) - and isinstance(default, Real) - and np.isclose(value, default) - ): # noqa: E501 - pass - elif value == default: - pass - else: - rcdict[key] = value - # Ignore non-style-related settings. See mstyle.STYLE_BLACKLIST - # TODO: For now not sure how to detect if prop cycle changed since - # we cannot load it from _cmap_database in rcsetup. - rcdict.pop("interactive", None) # changed by backend - rcdict.pop("axes.prop_cycle", None) - return _filter_style_dict(rcdict, warn=False) - - -# Initialize locations -_init_user_folders() -_init_user_file() - -#: A dictionary-like container of matplotlib settings. Assignments are -#: validated and restricted to recognized setting names. -rc_matplotlib = mpl.rcParams # PEP8 4 lyfe - -#: A dictionary-like container of ultraplot settings. Assignments are -#: validated and restricted to recognized setting names. -rc_ultraplot = rcsetup._rc_ultraplot_default.copy() # a validated rcParams-style dict -#: Instance of `Configurator`. This controls both `rc_matplotlib` and `rc_ultraplot` -#: settings. See the :ref:`configuration guide ` for details. -rc = Configurator() +def __getattr__(name): + # Preserve access to existing configuration helpers. + return getattr(_config, name) diff --git a/ultraplot/constructor.py b/ultraplot/constructor.py index 48b651879..1a186180f 100644 --- a/ultraplot/constructor.py +++ b/ultraplot/constructor.py @@ -28,11 +28,13 @@ from matplotlib.ft2font import FT2Font import numpy as np -from . import colors as pcolors -from . import proj as pproj -from . import scale as pscale -from . import ticker as pticker -from .config import rc +# Import bootstrap dependencies directly, without invoking the public lazy API +# and starting package setup while this module is still being defined. +import ultraplot.colors as pcolors +import ultraplot.proj as pproj +import ultraplot.scale as pscale +import ultraplot.ticker as pticker +from ._config import rc from .internals import ( _alias_kwargs, _not_none, @@ -1794,3 +1796,10 @@ def Proj( proj._proj_backend = backend return proj + + +# Direct submodule imports also need the registries and settings. Wait until +# Cycle is defined, since applying rc settings may construct a cycle itself. +from ._config import _initialize + +_initialize() diff --git a/ultraplot/demos.py b/ultraplot/demos.py index e16d5378f..2d022ac7a 100644 --- a/ultraplot/demos.py +++ b/ultraplot/demos.py @@ -13,7 +13,7 @@ from . import colors as pcolors from . import constructor, ui -from .config import _get_data_folders, rc +from ._config import _get_data_folders, rc from .internals import ic # noqa: F401 from .internals import _not_none, _version_mpl, docstring, warnings from .utils import to_rgb, to_xyz diff --git a/ultraplot/figure.py b/ultraplot/figure.py index d877b72ee..2c5df672a 100644 --- a/ultraplot/figure.py +++ b/ultraplot/figure.py @@ -35,7 +35,7 @@ from . import constructor from . import gridspec as pgridspec from . import legend as plegend -from .config import rc, rc_matplotlib +from ._config import rc, rc_matplotlib from .internals import ( _alias_kwargs, _canonicalize_kwargs, diff --git a/ultraplot/gridspec.py b/ultraplot/gridspec.py index 24188cc4f..58ffc10de 100644 --- a/ultraplot/gridspec.py +++ b/ultraplot/gridspec.py @@ -18,7 +18,7 @@ from . import axes as paxes from .axes._formatting import pop_axis_format_kwargs -from .config import rc +from ._config import rc from .internals import ( _alias_kwargs, _not_none, diff --git a/ultraplot/internals/fonts.py b/ultraplot/internals/fonts.py index 505a6be6e..887f37c2e 100644 --- a/ultraplot/internals/fonts.py +++ b/ultraplot/internals/fonts.py @@ -40,7 +40,7 @@ def _is_cm_mathtext_enabled(): try: - from ..config import rc + from .._config import rc except ImportError: return False return bool(rc["mathtext.cm_symbols"]) diff --git a/ultraplot/internals/inputs.py b/ultraplot/internals/inputs.py index cd3dc0076..8fedf3ba3 100644 --- a/ultraplot/internals/inputs.py +++ b/ultraplot/internals/inputs.py @@ -492,7 +492,7 @@ def _dist_kde( f"but got {distribution.size}." ) if coords is None: - from ..config import rc # avoid a circular import at module load + from .._config import rc # avoid a circular import at module load try: points = int(_not_none(points, rc["kde.points"])) @@ -898,7 +898,7 @@ def _meta_units(data): return units # Get units from the quantity if ndarray is not Quantity and isinstance(data, Quantity): - from ..config import rc + from .._config import rc fmt = rc.unitformat try: diff --git a/ultraplot/internals/labels.py b/ultraplot/internals/labels.py index 51c0e32c5..89d1c7c9f 100644 --- a/ultraplot/internals/labels.py +++ b/ultraplot/internals/labels.py @@ -7,7 +7,7 @@ import matplotlib.text as mtext from matplotlib.font_manager import FontProperties -from ..config import rc +from .._config import rc from . import ic # noqa: F401 diff --git a/ultraplot/legend.py b/ultraplot/legend.py index b5c51dcde..bec79a797 100644 --- a/ultraplot/legend.py +++ b/ultraplot/legend.py @@ -15,7 +15,7 @@ from matplotlib import legend_handler as mhandler from matplotlib.markers import MarkerStyle -from .config import rc +from ._config import rc from .internals import ( _alias_kwargs, _not_none, diff --git a/ultraplot/scale.py b/ultraplot/scale.py index 970473f5a..f6df8c12c 100644 --- a/ultraplot/scale.py +++ b/ultraplot/scale.py @@ -11,7 +11,7 @@ import numpy as np import numpy.ma as ma -from . import ticker as pticker +import ultraplot.ticker as pticker from .internals import ( _canonicalize_kwargs, _not_none, @@ -105,7 +105,7 @@ def set_default_locators_and_formatters(self, axis, only_if_default=False): # TODO: Always use only_if_default=True? Used only for dual axes right now # NOTE: We set isDefault_minloc to True when simply toggling minor ticks # on and off with CartesianAxes format command. - from .config import rc + from ._config import rc if not only_if_default or axis.isDefault_majloc: locator = copy.copy(self._default_major_locator) diff --git a/ultraplot/tests/test_config.py b/ultraplot/tests/test_config.py index 9a53e83d8..4faadd788 100644 --- a/ultraplot/tests/test_config.py +++ b/ultraplot/tests/test_config.py @@ -234,23 +234,136 @@ def _reader(): assert all(value in allowed for value in observed) -def _run_in_subprocess(code): - code = ( - "import pathlib\n" - "import sys\n" - "sys.path.insert(0, str(pathlib.Path.cwd()))\n" + code - ) - env = os.environ.copy() +def _run_in_subprocess(code, *, cwd=None, env=None): + root = pathlib.Path(__file__).resolve().parents[2] + code = f"import sys\nsys.path.insert(0, {str(root)!r})\n" + code + env = {**os.environ, **(env or {})} env["MPLBACKEND"] = "Agg" return subprocess.run( [sys.executable, "-c", code], capture_output=True, text=True, - cwd=str(pathlib.Path(__file__).resolve().parents[2]), + cwd=str(cwd or root), env=env, ) +@pytest.mark.parametrize("location", ["user", "local"]) +@pytest.mark.parametrize("custom", [False, True]) +@pytest.mark.parametrize("overrides", [False, True]) +def test_startup_cycle_applies_to_first_plot(tmp_path, location, custom, overrides): + """Startup rc files must synchronize the cycle before the first axes exists.""" + home = tmp_path / "home" + work = tmp_path / "work" + home.mkdir() + work.mkdir() + (tmp_path / "config").mkdir() + cycle = "startup_test" if custom else "qual1" + expected = ( + ["#123456", "#abcdef"] + if custom + else ["#5bbe94", "#91be64", "#e5cf6c", "#e87b70"] + ) + folder = home if location == "user" else work + content = f"cycle: {cycle}\nultraplot.check_for_latest_version: False\n" + if overrides: + content += ( + "axes.prop_cycle: cycler('color', ['red', 'blue'])\n" + "patch.facecolor: green\n" + ) + (folder / ".ultraplotrc").write_text(content) + if custom: + cycles = ( + home / ".ultraplot" / "cycles" + if location == "user" + else work / "ultraplot_cycles" + ) + cycles.mkdir(parents=True) + (cycles / f"{cycle}.hex").write_text(", ".join(expected) + "\n") + if overrides: + expected = ["#ff0000", "#0000ff"] + result = _run_in_subprocess( + "import matplotlib as mpl\n" + "from matplotlib.colors import to_hex\n" + "import ultraplot as uplt\n" + f"assert uplt.rc.cycle == {cycle!r}\n" + f"expected = {expected!r}\n" + "actual = [to_hex(c) for c in mpl.rcParams['axes.prop_cycle'].by_key()['color']]\n" + "assert actual[:len(expected)] == expected, actual\n" + + ( + "assert to_hex(mpl.rcParams['patch.facecolor']) == '#008000'\n" + if overrides + else "" + ) + + "fig, ax = uplt.subplots()\n" + "lines = ax.plot([[i] * len(expected) for i in range(3)])\n" + "actual = [to_hex(line.get_color()) for line in lines]\n" + "assert actual == expected, actual\n" + "uplt.close(fig)\n", + cwd=work, + env={ + "HOME": str(home), + "XDG_CONFIG_HOME": str(tmp_path / "config"), + "MPLCONFIGDIR": str(tmp_path / "mpl"), + }, + ) + assert result.returncode == 0, result.stderr + + +@pytest.mark.parametrize( + "statement", + [ + "from ultraplot.config import rc", + "import ultraplot.config as config; rc = config.rc", + "import ultraplot.colors; from ultraplot.config import rc", + "import ultraplot.ticker; from ultraplot.config import rc", + "from ultraplot.constructor import Cycle; " + "assert len(Cycle('qual1')) > 0; from ultraplot.config import rc", + "import ultraplot.scale; from ultraplot.config import rc", + "from ultraplot.config import rc_matplotlib, rc_ultraplot; " + "assert rc_matplotlib['font.size'] == 23; " + "assert rc_ultraplot['cycle'] == 'qual1'; " + "rc_matplotlib['font.size'] = 31; " + "import ultraplot as uplt; uplt.setup(eager=False); " + "assert rc_matplotlib['font.size'] == 31; " + "rc_matplotlib['font.size'] = 23; from ultraplot.config import rc", + ], +) +def test_direct_config_import_initializes_once(tmp_path, statement): + """Direct imports load rc files and later setup preserves user changes.""" + home = tmp_path / "home" + home.mkdir() + (tmp_path / "config").mkdir() + (tmp_path / ".ultraplotrc").write_text( + "cycle: qual1\nfont.size: 23\nultraplot.check_for_latest_version: False\n" + ) + result = _run_in_subprocess( + statement + "\n" + "import matplotlib as mpl\n" + "from matplotlib.colors import to_hex\n" + "assert rc.cycle == 'qual1', rc.cycle\n" + "import ultraplot.config as config\n" + "assert 'rc' in dir(config)\n" + "assert rc['font.size'] == 23, rc['font.size']\n" + "assert to_hex(mpl.rcParams['axes.prop_cycle'].by_key()['color'][0]) == '#5bbe94'\n" + "rc['font.size'] = 31\n" + "rc.cycle = 'colorblind'\n" + "import ultraplot as uplt\n" + "uplt.setup(eager=False)\n" + "uplt.setup(eager=False)\n" + "assert uplt.rc is rc\n" + "assert rc['font.size'] == 31, rc['font.size']\n" + "assert rc.cycle == 'colorblind', rc.cycle\n", + cwd=tmp_path, + env={ + "HOME": str(home), + "XDG_CONFIG_HOME": str(tmp_path / "config"), + "MPLCONFIGDIR": str(tmp_path / "mpl"), + }, + ) + assert result.returncode == 0, result.stderr + + def test_matplotlib_import_before_ultraplot_allows_rc_mutation(): """ Import order regression test for issue #568. diff --git a/ultraplot/tests/test_config_helpers_extra.py b/ultraplot/tests/test_config_helpers_extra.py index 0a76eb713..8fa4c52c6 100644 --- a/ultraplot/tests/test_config_helpers_extra.py +++ b/ultraplot/tests/test_config_helpers_extra.py @@ -4,7 +4,7 @@ import numpy as np import pytest -from ultraplot import config +from ultraplot import _config as config from ultraplot.internals.warnings import UltraPlotWarning diff --git a/ultraplot/ticker.py b/ultraplot/ticker.py index a64830786..13fc9b06b 100644 --- a/ultraplot/ticker.py +++ b/ultraplot/ticker.py @@ -20,7 +20,7 @@ except ModuleNotFoundError: cftime = None -from .config import rc +from ._config import rc from .internals import ic # noqa: F401 from .internals import _not_none, context, docstring