diff --git a/uxarray/__init__.py b/uxarray/__init__.py index f00f06099..975accd8e 100644 --- a/uxarray/__init__.py +++ b/uxarray/__init__.py @@ -1,6 +1,3 @@ -# Sets the version of uxarray currently installeds -# Attempt to import the needed modules - import uxarray.constants from .core.api import open_grid, open_dataset, open_mfdataset @@ -10,12 +7,11 @@ from .grid import Grid from .constants import INT_DTYPE, INT_FILL_VALUE -from .cmaps import sequential, diverging, sequential_blue, sequential_green -try: - from importlib.metadata import version as _version -except Exception: - from importlib_metadata import version as _version +from .cmaps import colormaps + + +from importlib.metadata import version as _version try: __version__ = _version("uxarray") @@ -23,9 +19,8 @@ # Placeholder version incase an error occurs, such as the library isn't installed __version__ = "999" -# Flag for enabling FMA instructions across the package - +# Flag for enabling FMA instructions across the package def enable_fma(): """Enables Fused-Multiply-Add (FMA) instructions using the ``pyfma`` package.""" @@ -47,10 +42,7 @@ def disable_fma(): "INT_DTYPE", "INT_FILL_VALUE", "Grid", - "sequential", - "diverging", - "sequential_blue", - "sequential_green", "enable_fma", "disable_fma", + "colormaps", ) diff --git a/uxarray/cmaps.py b/uxarray/cmaps.py index 3ab9217d7..4368fc07b 100644 --- a/uxarray/cmaps.py +++ b/uxarray/cmaps.py @@ -1,33 +1,78 @@ from matplotlib.colors import LinearSegmentedColormap -diverging = LinearSegmentedColormap.from_list( - "diverging", - ( - (0.000, (0.016, 0.576, 0.565)), - (0.500, (1.000, 1.000, 1.000)), - (1.000, (0.004, 0.400, 0.569)), - ), -) - -# UXarray themed sequential color map -sequential = LinearSegmentedColormap.from_list( - "sequential", ((0.000, (0.004, 0.400, 0.569)), (1.000, (0.016, 0.576, 0.565))) -) - -sequential_blue = LinearSegmentedColormap.from_list( - "sequential_blue", ((0.000, (1.000, 1.000, 1.000)), (1.000, (0.004, 0.400, 0.569))) -) - -sequential_green = LinearSegmentedColormap.from_list( - "sequential_green", ((0.000, (1.000, 1.000, 1.000)), (1.000, (0.016, 0.576, 0.565))) -) - -sequential_green_blue = LinearSegmentedColormap.from_list( - "sequential_green_blue", - ( - (0.000, (1.000, 1.000, 1.000)), - (0.500, (0.016, 0.576, 0.565)), - (1.000, (0.004, 0.400, 0.569)), - ), -) +class _Colormaps: + """Singleton class for lazy-loaded colormaps.""" + + def __init__(self): + self._diverging = None + self._sequential = None + self._sequential_blue = None + self._sequential_green = None + self._sequential_green_blue = None + + @property + def diverging(self): + if self._diverging is None: + self._diverging = LinearSegmentedColormap.from_list( + "diverging", + [ + (0.000, (0.016, 0.576, 0.565)), + (0.500, (1.000, 1.000, 1.000)), + (1.000, (0.004, 0.400, 0.569)), + ], + ) + return self._diverging + + @property + def sequential(self): + if self._sequential is None: + self._sequential = LinearSegmentedColormap.from_list( + "sequential", + [ + (0.000, (0.004, 0.400, 0.569)), + (1.000, (0.016, 0.576, 0.565)), + ], + ) + return self._sequential + + @property + def sequential_blue(self): + if self._sequential_blue is None: + self._sequential_blue = LinearSegmentedColormap.from_list( + "sequential_blue", + [ + (0.000, (1.000, 1.000, 1.000)), + (1.000, (0.004, 0.400, 0.569)), + ], + ) + return self._sequential_blue + + @property + def sequential_green(self): + if self._sequential_green is None: + self._sequential_green = LinearSegmentedColormap.from_list( + "sequential_green", + [ + (0.000, (1.000, 1.000, 1.000)), + (1.000, (0.016, 0.576, 0.565)), + ], + ) + return self._sequential_green + + @property + def sequential_green_blue(self): + if self._sequential_green_blue is None: + self._sequential_green_blue = LinearSegmentedColormap.from_list( + "sequential_green_blue", + [ + (0.000, (1.000, 1.000, 1.000)), + (0.500, (0.016, 0.576, 0.565)), + (1.000, (0.004, 0.400, 0.569)), + ], + ) + return self._sequential_green_blue + + +# Create a singleton instance for the colormaps +colormaps = _Colormaps() diff --git a/uxarray/grid/grid.py b/uxarray/grid/grid.py index b7ea0d3db..da6f3cbd1 100644 --- a/uxarray/grid/grid.py +++ b/uxarray/grid/grid.py @@ -1,5 +1,3 @@ -"""uxarray.core.grid module.""" - import xarray as xr import numpy as np @@ -231,7 +229,7 @@ def __init__( @classmethod def from_dataset( cls, dataset: xr.Dataset, use_dual: Optional[bool] = False, **kwargs - ): + ) -> "Grid": """Constructs a ``Grid`` object from an ``xarray.Dataset``. Parameters @@ -284,7 +282,7 @@ def from_file( filename: str, backend: Optional[str] = "geopandas", **kwargs, - ): + ) -> "Grid": """Constructs a ``Grid`` object from a using the read_file method with a specified backend. @@ -335,7 +333,7 @@ def from_topology( start_index: Optional[int] = 0, dims_dict: Optional[dict] = None, **kwargs, - ): + ) -> "Grid": """Constructs a ``Grid`` object from user-defined topology variables provided in the UGRID conventions. @@ -387,7 +385,7 @@ def from_face_vertices( cls, face_vertices: Union[list, tuple, np.ndarray], latlon: Optional[bool] = True, - ): + ) -> "Grid": """Constructs a ``Grid`` object from user-defined face vertices. Parameters @@ -1485,7 +1483,7 @@ def compute_face_areas( quadrature_rule: Optional[str] = "triangular", order: Optional[int] = 4, latlon: Optional[bool] = True, - ): + ) -> None: """Face areas calculation function for grid class, calculates area of all faces in the grid. @@ -1566,7 +1564,7 @@ def compute_face_areas( return self._face_areas, self._face_jacobian - def normalize_cartesian_coordinates(self): + def normalize_cartesian_coordinates(self) -> None: """Normalizes Cartesian coordinates.""" if _check_normalization(self): @@ -1908,7 +1906,7 @@ def to_linecollection( return line_collection - def get_dual(self): + def get_dual(self) -> "Grid": """Compute the dual for a grid, which constructs a new grid centered around the nodes, where the nodes of the primal become the face centers of the dual, and the face centers of the primal become the nodes of the @@ -1933,7 +1931,7 @@ def get_dual(self): return dual - def isel(self, **dim_kwargs): + def isel(self, **dim_kwargs) -> "Grid": """Indexes an unstructured grid along a given dimension (``n_node``, ``n_edge``, or ``n_face``) and returns a new grid. diff --git a/uxarray/plot/accessor.py b/uxarray/plot/accessor.py index 8f4938f5a..fccc41ee0 100644 --- a/uxarray/plot/accessor.py +++ b/uxarray/plot/accessor.py @@ -5,7 +5,6 @@ from typing import TYPE_CHECKING, Any, Optional import cartopy.crs as ccrs -import hvplot.pandas import pandas as pd import uxarray.plot.dataarray_plot as dataarray_plot