ndtools.comparison.comparables module#

class All(initlist=None)[source]#

Bases: UserList[Any], Combinable, Equatable

Implement 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, Equatable

Implement 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: object

Implement logical operations between comparables.

Classes that inherit from this mixin class can perform logical operations between comparables. Then comparable_0 & comparable_1 & ... will return All([comparable_0, comparable_1, ...]) and comparable_0 | comparable_1 | ... will return Any([comparable_0, comparable_1, ...]). where All and Any are 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: object

Implement 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 like def __eq__(self, array). Then the class instance and the array can perform instance == array and array == 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 Not(comparable: Any, /)[source]#

Bases: Combinable, Equatable

Implement logical negation for comparables.

It should wrap a comparable like Not(comparable). Then the equality operation on the target array will perform like array != comparable.

Examples

import numpy as np
from ndtools import Not

np.arange(3) == Not(1)  # -> array([True, False, True])
Parameters:

comparable (Any)

class Orderable[source]#

Bases: object

Implement 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 like def __ge__(self, array). Then the class instance and the array can perform instance >= array and array <= 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])