Skip to content

sgnts.transforms.matmul

Matmul dataclass

Bases: TSTransform

Performs matrix multiplication with provided matrix.

Parameters:

Name Type Description Default
matrix Optional[Array]

Optional[Array], the matrix to multiply the data with, out = matrix x data

None
backend type[ArrayBackend]

type[ArrayBackend], the array backend for array operations

NumpyBackend
Source code in sgnts/transforms/matmul.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
@dataclass
class Matmul(TSTransform):
    """Performs matrix multiplication with provided matrix.

    Args:
        matrix:
            Optional[Array], the matrix to multiply the data with, out = matrix x data
        backend:
            type[ArrayBackend], the array backend for array operations
    """

    matrix: Optional[Array] = None
    backend: type[ArrayBackend] = NumpyBackend

    def __post_init__(self):
        super().__post_init__()
        assert len(self.sink_pads) == 1 and len(self.source_pads) == 1, (
            f"MatMul requires exactly one sink pad and one source pad, "
            f"got {len(self.sink_pads)} sink pads and "
            f"{len(self.source_pads)} source pads"
        )
        assert self.matrix is not None, "Matrix must be provided for MatMul operation"
        self.shape = self.matrix.shape

    def new(self, pad: SourcePad) -> TSFrame:
        outbufs = []
        # loop over the input data, only perform matmul on non-gaps
        frame = self.preparedframes[self.sink_pads[0]]
        for inbuf in frame:
            is_gap = inbuf.is_gap

            if is_gap:
                data = None
            else:
                data = self.backend.matmul(self.matrix, inbuf.data)

            outbuf = SeriesBuffer(
                offset=inbuf.offset,
                sample_rate=inbuf.sample_rate,
                data=data,
                shape=self.shape[:-1] + (inbuf.samples,),
            )
            outbufs.append(outbuf)

        return TSFrame(buffers=outbufs, EOS=frame.EOS, metadata=frame.metadata)