Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Minor code cleanup #1041

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions uxarray/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,22 +7,20 @@
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")
except Exception:
# 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."""
Expand All @@ -47,10 +42,7 @@ def disable_fma():
"INT_DTYPE",
"INT_FILL_VALUE",
"Grid",
"sequential",
"diverging",
"sequential_blue",
"sequential_green",
"enable_fma",
"disable_fma",
"colormaps",
)
105 changes: 75 additions & 30 deletions uxarray/cmaps.py
Original file line number Diff line number Diff line change
@@ -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()
18 changes: 8 additions & 10 deletions uxarray/grid/grid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"""uxarray.core.grid module."""

import xarray as xr
import numpy as np

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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.

Expand Down
1 change: 0 additions & 1 deletion uxarray/plot/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading