Skip to content

Commit

Permalink
Added a components argument to named_arrays.plt.plot() (#19)
Browse files Browse the repository at this point in the history
Added a `components` argument to `named_arrays.plt.plot()` and `named_arrays.plt.fill()`.
  • Loading branch information
byrdie authored Sep 14, 2023
1 parent 3e523e3 commit ec254f4
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 96 deletions.
10 changes: 10 additions & 0 deletions named_arrays/_matrices/tests/test_matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ def test_matmul(
array @ array_2
return

class TestNamedArrayFunctions(
named_arrays._vectors.tests.test_vectors.AbstractTestAbstractVectorArray.TestNamedArrayFunctions
):

@pytest.mark.skip
class TestPltPlotLikeFunctions(
named_arrays._vectors.tests.test_vectors.AbstractTestAbstractVectorArray.TestNamedArrayFunctions.TestPltPlotLikeFunctions
):
pass


class AbstractTestAbstractExplicitMatrixArray(
AbstractTestAbstractMatrixArray,
Expand Down
4 changes: 4 additions & 0 deletions named_arrays/_scalars/scalar_named_array_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,13 @@ def plt_plot_like(
ax: None | matplotlib.axes.Axes | na.ScalarArray[npt.NDArray[matplotlib.axes.Axes]] = None,
axis: None | str = None,
where: bool | na.AbstractScalarArray = True,
components: None | tuple[str, ...] = None,
**kwargs,
) -> na.ScalarArray[npt.NDArray[None | matplotlib.artist.Artist]]:

if components is not None:
raise ValueError(f"`components` should be `None` for scalars, got {components}")

try:
args = tuple(scalars._normalize(arg) for arg in args)
where = scalars._normalize(where)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,16 @@ def plt_plot_like(
ax: None | matplotlib.axes.Axes = None,
axis: None | str = None,
where: bool | na.AbstractScalarArray = True,
components: None | tuple[str, ...] = None,
**kwargs,
) -> na.UncertainScalarArray[
npt.NDArray[matplotlib.artist.Artist],
npt.NDArray[matplotlib.artist.Artist]
]:

if components is not None:
raise ValueError(f"`components` should be `None` for scalars, got {components}")

try:
args = tuple(uncertainties._normalize(arg) for arg in args)
where = uncertainties._normalize(where)
Expand Down

This file was deleted.

12 changes: 0 additions & 12 deletions named_arrays/_vectors/cartesian/vectors_cartesian_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,6 @@ def type_explicit(self: Self) -> Type[Cartesian2dVectorArray]:
def type_matrix(self) -> Type[na.Cartesian2dMatrixArray]:
return na.Cartesian2dMatrixArray

def __named_array_function__(self, func, *args, **kwargs):
result = super().__named_array_function__(func, *args, **kwargs)
if result is not NotImplemented:
return result

from . import vector_cartesian_2d_named_array_functions

if func in vector_cartesian_2d_named_array_functions.PLT_PLOT_LIKE_FUNCTIONS:
return vector_cartesian_2d_named_array_functions.plt_plot_like(func, *args, **kwargs)

return NotImplemented


@dataclasses.dataclass(eq=False, repr=False)
class Cartesian2dVectorArray(
Expand Down
46 changes: 1 addition & 45 deletions named_arrays/_vectors/tests/test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,51 +513,7 @@ class TestNamedArrayFunctions(
class TestPltPlotLikeFunctions(
named_arrays.tests.test_core.AbstractTestAbstractArray.TestNamedArrayFunctions.TestPltPlotLikeFunctions
):
def test_plt_plot_like(
self,
func: Callable,
array: na.AbstractVectorArray,
array_2: None,
ax: None | matplotlib.axes.Axes,
axis: None | str,
where: bool | na.AbstractScalar,
transformation: None | na.transformations.AbstractTransformation,
alpha: None | str | na.AbstractScalar,
):

args = array,

kwargs = dict()
if ax is not np._NoValue:
kwargs["ax"] = ax
if axis is not np._NoValue:
kwargs["axis"] = axis
if where is not np._NoValue:
kwargs["where"] = where
if transformation is not np._NoValue:
kwargs["transformation"] = transformation
if alpha is not np._NoValue:
kwargs["alpha"] = alpha

for c in array.components:
if not isinstance(na.as_named_array(array.components[c]), na.AbstractScalar):
with pytest.raises(
expected_exception=TypeError,
match="all types, .*, returned .* for function .*"
):
func(*args, **kwargs)
return

return super().test_plt_plot_like(
func=func,
array=array,
array_2=array_2,
ax=ax,
axis=axis,
where=where,
transformation=transformation,
alpha=alpha,
)
pass

@pytest.mark.xfail
class TestOptimizeRootSecant(
Expand Down
43 changes: 42 additions & 1 deletion named_arrays/_vectors/vector_named_array_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Callable
import numpy as np
import numpy.typing as npt
import matplotlib.axes
import astropy.units as u
import named_arrays as na
import named_arrays._scalars.scalar_named_array_functions
Expand All @@ -8,12 +10,15 @@
__all__ = [
"ASARRAY_LIKE_FUNCTIONS",
"RANDOM_FUNCTIONS",
"PLT_PLOT_LIKE_FUNCTIONS",
"HANDLED_FUNCTIONS",
"random"
"random",
"plt_plot_like",
]

ASARRAY_LIKE_FUNCTIONS = named_arrays._scalars.scalar_named_array_functions.ASARRAY_LIKE_FUNCTIONS
RANDOM_FUNCTIONS = named_arrays._scalars.scalar_named_array_functions.RANDOM_FUNCTIONS
PLT_PLOT_LIKE_FUNCTIONS = named_arrays._scalars.scalar_named_array_functions.PLT_PLOT_LIKE_FUNCTIONS
HANDLED_FUNCTIONS = dict()


Expand Down Expand Up @@ -168,3 +173,39 @@ def random(
}

return prototype.type_explicit.from_components(components)


def plt_plot_like(
func: Callable,
*args: na.AbstractCartesian2dVectorArray,
ax: None | matplotlib.axes.Axes | na.ScalarArray[npt.NDArray[matplotlib.axes.Axes]] = None,
axis: None | str = None,
where: bool | na.AbstractScalarArray = True,
components: None | tuple[str, ...] = None,
**kwargs,
) -> na.ScalarArray[npt.NDArray[None | matplotlib.artist.Artist]]:

if len(args) != 1:
return NotImplemented

a, = args

if not isinstance(a, na.AbstractVectorArray):
return NotImplemented

a = a.cartesian_nd

components_a = a.components

if components is None:
components = components_a

args = tuple(na.as_named_array(components_a[c]) for c in components)

return func(
*args,
ax=ax,
axis=axis,
where=where,
**kwargs,
)
3 changes: 3 additions & 0 deletions named_arrays/_vectors/vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,9 @@ def __named_array_function__(self, func, *args, **kwargs):
if func in vector_named_array_functions.RANDOM_FUNCTIONS:
return vector_named_array_functions.random(func=func, *args, **kwargs)

if func in vector_named_array_functions.PLT_PLOT_LIKE_FUNCTIONS:
return vector_named_array_functions.plt_plot_like(func, *args, **kwargs)

if func in vector_named_array_functions.HANDLED_FUNCTIONS:
return vector_named_array_functions.HANDLED_FUNCTIONS[func](*args, **kwargs)

Expand Down
10 changes: 10 additions & 0 deletions named_arrays/plt.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def plot(
axis: None | str = None,
where: bool | na.AbstractScalar = True,
transformation: None | na.transformations.AbstractTransformation = None,
components: None | tuple[str, ...] = None,
**kwargs,
) -> na.ScalarArray[npt.NDArray[None | matplotlib.artist.Artist]]:
"""
Expand Down Expand Up @@ -108,6 +109,9 @@ def plot(
A boolean array that selects which elements to plot
transformation
A callable that is applied to args before plotting
components
The component names of ``*args`` to plot, helpful if ``*args`` are an instance of
:class:`named_arrays.AbstractVectorArray`.
kwargs
Additional keyword arguments passed to :meth:`matplotlib.axes.Axes.plot`.
These can be instances of :class:`named_arrays.AbstractArray`.
Expand Down Expand Up @@ -180,6 +184,7 @@ def plot(
ax=ax,
axis=axis,
where=where,
components=components,
**kwargs,
)

Expand All @@ -190,6 +195,7 @@ def fill(
axis: None | str = None,
where: bool | na.AbstractScalar = True,
transformation: None | na.transformations.AbstractTransformation = None,
components: None | tuple[str, ...] = None,
**kwargs,
) -> na.ScalarArray[npt.NDArray]:
"""
Expand Down Expand Up @@ -219,6 +225,9 @@ def fill(
A boolean array that selects which elements to plot
transformation
A callable that is applied to args before plotting
components
The component names of ``*args`` to plot, helpful if ``*args`` are an instance of
:class:`named_arrays.AbstractVectorArray`.
kwargs
Additional keyword arguments passed to :meth:`matplotlib.axes.Axes.fill`.
These can be instances of :class:`named_arrays.AbstractArray`.
Expand All @@ -231,5 +240,6 @@ def fill(
ax=ax,
axis=axis,
where=where,
components=components,
**kwargs,
)

0 comments on commit ec254f4

Please sign in to comment.