ndtools package#
- class All(initlist=None)[source]#
Bases:
UserList[Any],Combinable,EquatableImplement logical conjunction between comparables.
It should contain comparables like
All([comparable_0, comparable_1, ...]). Then the equality operation on the target array will perform like(array == comparable_0) & array == comparable_1) & ....Examples
import numpy as np from ndtools import Combinable, Equatable class Even(Combinable, Equatable): def __eq__(self, array): return array % 2 == 0 class Odd(Combinable, Equatable): def __eq__(self, array): return array % 2 == 1 Even() & Odd() # -> All([Even(), Odd()]) np.arange(3) == Even() & Odd() # -> array([False, False, False])
- class Any(initlist=None)[source]#
Bases:
UserList[Any],Combinable,EquatableImplement logical disjunction between comparables.
It should contain comparables like
Any([comparable_0, comparable_1, ...]). Then the equality operation on the target array will perform like(array == comparable_0) | array == comparable_1) & ....Examples
import numpy as np from ndtools import Combinable, Equatable class Even(Combinable, Equatable): def __eq__(self, array): return array % 2 == 0 class Odd(Combinable, Equatable): def __eq__(self, array): return array % 2 == 1 Even() | Odd() # -> Any([Even(), Odd()]) np.arange(3) == Even() | Odd() # -> array([True, True, True])
- class Combinable[source]#
Bases:
objectImplement logical operations between comparables.
Classes that inherit from this mixin class can perform logical operations between comparables. Then
comparable_0 & comparable_1 & ...will returnAll([comparable_0, comparable_1, ...])andcomparable_0 | comparable_1 | ...will returnAny([comparable_0, comparable_1, ...]). whereAllandAnyare the implementation of logical conjunction and logical disjunction, respectively.Examples
import numpy as np from ndtools import Combinable, Equatable class Even(Combinable, Equatable): def __eq__(self, array): return array % 2 == 0 class Odd(Combinable, Equatable): def __eq__(self, array): return array % 2 == 1 Even() & Odd() # -> All([Even(), Odd()]) Even() | Odd() # -> Any([Even(), Odd()]) np.arange(3) == Even() & Odd() # -> array([False, False, False]) np.arange(3) == Even() | Odd() # -> array([True, True, True])
- class Equatable[source]#
Bases:
objectImplement equality operations for multidimensional arrays.
Classes that inherit from this mixin class and implement
__eq__or__ne__special methods can perform their own equality operations on multidimensional arrays. These special methods should be implemented for the target array likedef __eq__(self, array). Then the class instance and the array can performinstance == arrayandarray == instance.Examples
import numpy as np from ndtools import Equatable class Even(Equatable): def __eq__(self, array): return array % 2 == 0 Even() == np.arange(3) # -> array([True, False, True]) np.arange(3) == Even() # -> array([True, False, True]) Even() != np.arange(3) # -> array([False, True, False]) np.arange(3) != Even() # -> array([False, True, False])
- class Match(pat: str, case: bool = True, flags: int = 0, na: Any = None)[source]#
Bases:
Combinable,EquatableComparable that matches regular expression to each array element.
It uses
pandas.Series.str.fullmatchso the same options are available.- Parameters:
pat (str) – Character sequence or regular expression.
case (bool) – If True, case sensitive matching will be performed.
flags (int) – Regular expression flags, e.g.
re.IGNORECASE.na (Any) – Fill value for missing values. The default value depends on data type of the array. For object-dtype,
numpy.nanwill be used. ForStringDtype,pandas.NAwill be used.
Examples
import numpy as np from ndtools import Match np.array(["a", "aa"]) == Match("a+") # -> array([True, True])
- pat: str#
Character sequence or regular expression.
- case: bool = True#
If True, case sensitive matching will be performed.
- flags: int = 0#
Regular expression flags, e.g.
re.IGNORECASE.
- na: Any = None#
Fill value for missing values.
- class Not(comparable: Any, /)[source]#
Bases:
Combinable,EquatableImplement logical negation for comparables.
It should wrap a comparable like
Not(comparable). Then the equality operation on the target array will perform likearray != comparable.Examples
import numpy as np from ndtools import Not np.arange(3) == Not(1) # -> array([True, False, True])
- Parameters:
comparable (Any)
- class Range(lower: Any, upper: Any, bounds: Literal['[]', '[)', '(]', '()'] = '[)')[source]#
Bases:
Combinable,OrderableComparable that implements equivalence with a certain range.
- Parameters:
lower (Any) – Lower value of the range. If
Noneis specified, then the lower value comparison will be skipped.lower – Upper value of the range. If
Noneis specified, then the upper value comparison will be skipped.bounds (Literal['[]', '[)', '(]', '()']) – Type of bounds of the range.
[]: Lower-closed and upper-closed.[): Lower-closed and upper-open (default).(]: Lower-open and upper-closed.(): Lower-open and upper-open.upper (Any)
Examples
import numpy as np from ndtools import Range np.arange(3) == Range(1, 2) # -> array([False, True, False]) np.arange(3) < Range(1, 2) # -> array([True, False, False]) np.arange(3) > Range(1, 2) # -> array([False, False, True]) np.arange(3) == Range(None, 2) # -> array([True, True, False]) np.arange(3) == Range(1, None) # -> array([False, True, True]) np.arange(3) == Range(None, None) # -> array([True, True, True])
- lower: Any#
Lower value of the range.
- upper: Any#
Upper value of the range.
- bounds: Literal['[]', '[)', '(]', '()'] = '[)'#
Type of bounds of the range.
- property is_lower_open: bool#
Check if the lower bound is open.
- property is_lower_closed: bool#
Check if the lower bound is closed.
- property is_upper_open: bool#
Check if the upper bound is open.
- property is_upper_closed: bool#
Check if the upper bound is closed.
- class Orderable[source]#
Bases:
objectImplement ordering operations for multidimensional arrays.
Classes that inherit from this mixin base class and implement both (1)
__eq__or__ne__special methods and (2)__ge__,__gt__,__le__, or__lt__special methods can perform their own ordering operations on multidimensional arrays. These special methods should be implemented for the target array likedef __ge__(self, array). Then the class instance and the array can performinstance >= arrayandarray <= instance.Examples
import numpy as np from dataclasses import dataclass from ndtools import Orderable @dataclass class Range(Orderable): lower: float upper: float def __eq__(self, array): return (array >= self.lower) & (array < self.upper) def __ge__(self, array): return array < self.upper Range(1, 2) == np.arange(3) # -> array([False, True, False]) np.arange(3) == Range(1, 2) # -> array([False, True, False]) Range(1, 2) >= np.arange(3) # -> array([True, True, False]) np.arange(3) <= Range(1, 2) # -> array([True, True, False])
- class Where(func: Callable[[...], Any], *args: Any, **kwargs: Any)[source]#
Bases:
Combinable,EquatableComparable that applies a boolean function for multidimensional arrays.
- Parameters:
func (Callable[[...], Any]) – Boolean function that takes
func(array, *args, **kwargs).*args (Any) – Positional arguments to be passed to the function.
**kwargs (Any) – Keyword arguments to be passed to the function.
Examples
import numpy as np from ndtools import Where from numpy.char import isupper np.array(["A", "b"]) == Where(isupper) # -> array([True, False])
- func: Callable[[...], Any]#
Boolean function that takes
func(array, *args, **kwargs).
- args: Any#
Positional arguments to be passed to the function.
- kwargs: Any#
Keyword arguments to be passed to the function.