|
| 1 | +"""Optional matplotlib plotting API for NNS, faithful to R NNS ``plot = TRUE``. |
| 2 | +
|
| 3 | +This subpackage is an **optional extra**. The NNS core stays NumPy/SciPy-only; |
| 4 | +matplotlib is required only here and is imported lazily inside each plot |
| 5 | +function (``pip install ovvo-nns[plot]``). |
| 6 | +
|
| 7 | +Design contract for every ``plot_*`` function: |
| 8 | +
|
| 9 | +* Accept an already-computed NNS result (or the same raw inputs) plus a keyword |
| 10 | + ``ax=None`` and return the matplotlib ``Axes`` (or ``Figure`` for 3-D). |
| 11 | +* Never call ``plt.show()`` -- the caller controls display and saving. |
| 12 | +* Be **color/element-faithful** to R (see :mod:`nns.plotting.palette`), not |
| 13 | + pixel-diffed. |
| 14 | +
|
| 15 | +Colors are exposed via :mod:`nns.plotting.palette`; the only same-named colors |
| 16 | +that must *not* be trusted from matplotlib are ``green`` (-> ``#00FF00``) and |
| 17 | +``grey`` (-> ``#BEBEBE``). |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +from typing import TYPE_CHECKING, Any |
| 23 | + |
| 24 | +from nns.plotting import palette as palette |
| 25 | + |
| 26 | +if TYPE_CHECKING: # pragma: no cover - typing only |
| 27 | + from nns.plotting.anova import plot_nns_anova as plot_nns_anova |
| 28 | + from nns.plotting.arma import plot_nns_arma as plot_nns_arma |
| 29 | + from nns.plotting.arma import plot_nns_arma_optim as plot_nns_arma_optim |
| 30 | + from nns.plotting.causation import plot_nns_causation as plot_nns_causation |
| 31 | + from nns.plotting.copula import plot_nns_copula as plot_nns_copula |
| 32 | + from nns.plotting.differentiation import plot_nns_diff as plot_nns_diff |
| 33 | + from nns.plotting.dominance import plot_fsd as plot_fsd |
| 34 | + from nns.plotting.dominance import plot_ssd as plot_ssd |
| 35 | + from nns.plotting.dominance import plot_tsd as plot_tsd |
| 36 | + from nns.plotting.normalization import plot_nns_norm as plot_nns_norm |
| 37 | + from nns.plotting.partial_moments import plot_nns_cdf as plot_nns_cdf |
| 38 | + from nns.plotting.regression import plot_nns_part as plot_nns_part |
| 39 | + from nns.plotting.regression import plot_nns_reg as plot_nns_reg |
| 40 | + from nns.plotting.seasonality import plot_nns_seas as plot_nns_seas |
| 41 | + |
| 42 | +_EXPORTS = { |
| 43 | + "plot_nns_anova": ("nns.plotting.anova", "plot_nns_anova"), |
| 44 | + "plot_nns_arma": ("nns.plotting.arma", "plot_nns_arma"), |
| 45 | + "plot_nns_arma_optim": ("nns.plotting.arma", "plot_nns_arma_optim"), |
| 46 | + "plot_nns_causation": ("nns.plotting.causation", "plot_nns_causation"), |
| 47 | + "plot_nns_copula": ("nns.plotting.copula", "plot_nns_copula"), |
| 48 | + "plot_nns_diff": ("nns.plotting.differentiation", "plot_nns_diff"), |
| 49 | + "plot_fsd": ("nns.plotting.dominance", "plot_fsd"), |
| 50 | + "plot_ssd": ("nns.plotting.dominance", "plot_ssd"), |
| 51 | + "plot_tsd": ("nns.plotting.dominance", "plot_tsd"), |
| 52 | + "plot_nns_norm": ("nns.plotting.normalization", "plot_nns_norm"), |
| 53 | + "plot_nns_cdf": ("nns.plotting.partial_moments", "plot_nns_cdf"), |
| 54 | + "plot_nns_part": ("nns.plotting.regression", "plot_nns_part"), |
| 55 | + "plot_nns_reg": ("nns.plotting.regression", "plot_nns_reg"), |
| 56 | + "plot_nns_seas": ("nns.plotting.seasonality", "plot_nns_seas"), |
| 57 | +} |
| 58 | + |
| 59 | +__all__ = sorted((*_EXPORTS, "palette")) |
| 60 | + |
| 61 | + |
| 62 | +def __getattr__(name: str) -> Any: |
| 63 | + if name not in _EXPORTS: |
| 64 | + raise AttributeError(f"module 'nns.plotting' has no attribute {name!r}") |
| 65 | + from importlib import import_module |
| 66 | + |
| 67 | + module_name, attr_name = _EXPORTS[name] |
| 68 | + value = getattr(import_module(module_name), attr_name) |
| 69 | + globals()[name] = value |
| 70 | + return value |
0 commit comments