Skip to content

Reference

A sparse boolean array, i.e., set of indices.

Provides a memory efficient set interface, with optimized conversion between numpy arrays.

Parameters:

Name Type Description Default
keys Iterable

optional iterable of keys

required

add(key) method descriptor

Add an index key.

clear() method descriptor

Remove all indices.

difference(*others) method descriptor

Return the difference of sets as a new set.

discard(key) method descriptor

Remove an index key, if present.

dot(*others) method descriptor

Return the intersection count of sets.

fromdense(values) classmethod

Return indices from a dense array representation.

intersection(*others) method descriptor

Return the intersection of sets as a new set.

isdisjoint(other) method descriptor

Return whether two indices have a null intersection.

todense(minlength=0, dtype=bool) method descriptor

Return a dense array representation of indices.

union(*others) method descriptor

Return the union of sets as a new set.

update(*others) method descriptor

Update from indices, arrays, or iterables.

A sparse array of index keys mapped to numeric values.

Provides a memory efficient Counter interface, with optimized conversion between numpy arrays.

Parameters:

Name Type Description Default
keys Iterable[int]

optional iterable of keys

required
values

optional scalar or iterable of values

required

argmax(**kwargs) method descriptor

Return key with maximum value.

argmin(**kwargs) method descriptor

Return key with minimum value.

argpartition(kth, **kwargs) method descriptor

Return keys partitioned by values.

argsort(**kwargs) method descriptor

Return keys sorted by values.

clear() method descriptor

Remove all items.

difference(*others) method descriptor

Provisional set difference; return vector without keys.

equal(other) method descriptor

Return whether vectors are equal as scalar bool; == is element-wise.

filter(ufunc, *args, **kwargs) method descriptor

Return element-wise array of keys from applying predicate across vectors.

fromdense(values) classmethod

Return vector from a dense array representation.

items() method descriptor

Return zipped keys and values.

keys() method descriptor

Return keys as numpy array.

map(ufunc, *args, **kwargs) method descriptor

Return element-wise array of values from applying function across vectors.

max(initial=None, **kwargs) method descriptor

Return maximum value.

maximum(value) method descriptor

Return element-wise maximum vector.

min(initial=None, **kwargs) method descriptor

Return minimum value.

minimum(value) method descriptor

Return element-wise minimum vector.

sum(initial=0.0, dtype=float, **kwargs) method descriptor

Return sum of values.

todense(minlength=0, dtype=float) method descriptor

Return a dense array representation of vector.

update(keys, values=1.0) method descriptor

Update from vector, arrays, mapping, or keys with scalar.

values(dtype=float) method descriptor

Return values as numpy array.

Bases: defaultdict

A sparse vector of vectors.

Parameters:

Name Type Description Default
data Iterable
()
Source code in spector/matrix.py
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class matrix(collections.defaultdict):
    """A sparse vector of vectors.

    Args:
        data (Iterable):
    """

    def __init__(self, data=(), copy=True):
        super().__init__(vector)
        (self if copy else super()).update(data)

    @classmethod
    def cast(cls, data) -> 'matrix':
        return cls(data, copy=False)

    @property
    def row(self) -> np.ndarray:
        """COO format row index array of the matrix"""
        return np.concatenate([np.full(len(self[key]), key) for key in self])

    @property
    def col(self) -> np.ndarray:
        """COO format column index array of the matrix"""
        return np.concatenate([vec.keys() for vec in self.values()])

    @property
    def data(self) -> np.ndarray:
        """COO format data array of the matrix"""
        return np.concatenate([vec.values() for vec in self.values()])

    def update(self, data):
        """Update from mapping or iterable."""
        if isinstance(data, Mapping):
            for key in data:
                self[key].update(data[key])
        else:
            for key, value in data:
                self[key].update(value)

    def __iadd__(self, other):
        if isinstance(other, Mapping):
            for key in other:
                self[key] += other[key]
        else:
            self.map(vector.__iadd__, other)
        return self

    def __add__(self, other) -> 'matrix':
        return type(self)(self).__iadd__(other)

    def __imul__(self, other):
        if isinstance(other, Mapping):
            for key in set(self).difference(other):
                del self[key]
            for key in self:
                self[key] *= other[key]
        else:
            self.map(vector.__imul__, other)
        return self

    def __mul__(self, other) -> dict:
        if isinstance(other, Mapping):
            return self.cast((key, self[key] * other[key]) for key in set(self).intersection(other))
        return self.map(vector.__mul__, other)

    def sum(self, axis: Optional[int] = None):
        """Return sum of matrix elements across axis, by default both."""
        if axis in (0, -2):
            return functools.reduce(vector.__iadd__, self.values(), vector())
        if axis in (1, -1):
            return self.map(np.sum)
        if axis is None:
            return sum(map(np.sum, self.values()))
        raise np.exceptions.AxisError(axis, ndim=2)

    def map(self, func: Callable, *args, **kwargs) -> dict:
        """Return matrix with function applies across vectors."""
        result = {key: func(self[key], *args, **kwargs) for key in self}
        if all(isinstance(value, vector) for value in result.values()):
            return self.cast(result)
        return result

    def filter(self, func: Callable, *args, **kwargs) -> 'matrix':
        """Return matrix with function applies across vectors."""
        return self.cast((key, vec) for key, vec in self.items() if func(vec, *args, **kwargs))

    @classmethod
    def fromcoo(cls, row: Iterable, col: Iterable[int], data: Iterable[float]) -> 'matrix':
        """Return matrix from COOrdinate format arrays."""
        return cls.cast((key, vector(col, data)) for key, col, data in groupby(row, col, data))

    def transpose(self) -> 'matrix':
        """Return matrix with reversed dimensions."""
        return self.fromcoo(self.col, self.row, self.data)

    T = property(transpose)

    def __matmul__(self, other: 'matrix') -> 'matrix':
        other = other.transpose()
        return self.cast((key, vector(other.map(self[key].__matmul__))) for key in self)

col: np.ndarray property

COO format column index array of the matrix

data: np.ndarray property

COO format data array of the matrix

row: np.ndarray property

COO format row index array of the matrix

filter(func, *args, **kwargs)

Return matrix with function applies across vectors.

Source code in spector/matrix.py
114
115
116
def filter(self, func: Callable, *args, **kwargs) -> 'matrix':
    """Return matrix with function applies across vectors."""
    return self.cast((key, vec) for key, vec in self.items() if func(vec, *args, **kwargs))

fromcoo(row, col, data) classmethod

Return matrix from COOrdinate format arrays.

Source code in spector/matrix.py
118
119
120
121
@classmethod
def fromcoo(cls, row: Iterable, col: Iterable[int], data: Iterable[float]) -> 'matrix':
    """Return matrix from COOrdinate format arrays."""
    return cls.cast((key, vector(col, data)) for key, col, data in groupby(row, col, data))

map(func, *args, **kwargs)

Return matrix with function applies across vectors.

Source code in spector/matrix.py
107
108
109
110
111
112
def map(self, func: Callable, *args, **kwargs) -> dict:
    """Return matrix with function applies across vectors."""
    result = {key: func(self[key], *args, **kwargs) for key in self}
    if all(isinstance(value, vector) for value in result.values()):
        return self.cast(result)
    return result

sum(axis=None)

Return sum of matrix elements across axis, by default both.

Source code in spector/matrix.py
 97
 98
 99
100
101
102
103
104
105
def sum(self, axis: Optional[int] = None):
    """Return sum of matrix elements across axis, by default both."""
    if axis in (0, -2):
        return functools.reduce(vector.__iadd__, self.values(), vector())
    if axis in (1, -1):
        return self.map(np.sum)
    if axis is None:
        return sum(map(np.sum, self.values()))
    raise np.exceptions.AxisError(axis, ndim=2)

transpose()

Return matrix with reversed dimensions.

Source code in spector/matrix.py
123
124
125
def transpose(self) -> 'matrix':
    """Return matrix with reversed dimensions."""
    return self.fromcoo(self.col, self.row, self.data)

update(data)

Update from mapping or iterable.

Source code in spector/matrix.py
62
63
64
65
66
67
68
69
def update(self, data):
    """Update from mapping or iterable."""
    if isinstance(data, Mapping):
        for key in data:
            self[key].update(data[key])
    else:
        for key, value in data:
            self[key].update(value)

Generate unique keys with associated groups.

Parameters:

Name Type Description Default
keys Iterable
required
*arrays Iterable
()
Source code in spector/matrix.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def groupby(keys: Iterable, *arrays) -> Iterator[tuple]:
    """Generate unique keys with associated groups.

    Args:
        keys:
        *arrays (Iterable):
    """
    arrays = tuple(map(np.asarray, arrays))
    try:
        items = _arggroupby(asiarray(keys))
    except TypeError:  # fallback to sorting
        items = arggroupby(keys)
    for key, values in items:
        yield key, *(arr[values] for arr in arrays)