Skip to content

matrix

matrix

Program input/output utilities.

MatrixFormat

Bases: Enum

Enumeration of possible supported matrix formats.

Source code in cssfinder/io/matrix.py
class MatrixFormat(Enum):
    """Enumeration of possible supported matrix formats."""

    MATRIX_MARKET = ".mtx"

MatrixIO

Bases: ABC

Abstract base class declaring interface of matrix input-output manager.

Source code in cssfinder/io/matrix.py
class MatrixIO(ABC):
    """Abstract base class declaring interface of matrix input-output manager."""

    matrix_format: ClassVar[MatrixFormat]

    @classmethod
    def new(cls, matrix_format: MatrixFormat) -> MatrixIO:
        """Create new instance of matrix loader for file format. When format is None,
        file format is deduced from file extension.

        Parameters
        ----------
        matrix_format : MatrixFormat
            Path to file containing matrix data.

        Returns
        -------
        MatrixIO
            Matrix loader.

        """
        io = FORMAT_TO_LOADER[matrix_format]()
        logging.debug("Selected matrix IO %r for format %r", io, matrix_format.name)
        return io

    def load(
        self,
        src: str | Path | IO[bytes],
    ) -> npt.NDArray[np.int64 | np.float64 | np.complex128]:
        """Load matrix from file as numpy array."""
        if isinstance(src, (str, Path)):
            with Path(src).open("rb") as file:
                return self._load(file)

        return self._load(file)

    @abstractmethod
    def _load(
        self,
        src: IO[bytes],
    ) -> npt.NDArray[np.int64 | np.float64 | np.complex128]:
        ...

    def dump(
        self,
        data: npt.NDArray[np.int64 | np.float64 | np.complex128],
        dest: str | Path | IO[bytes],
    ) -> None:
        """Dump matrix to file from numpy array."""
        if isinstance(dest, (str, Path)):
            file_path = Path(dest).with_suffix(self.matrix_format.value)
            file_path.touch(0o777, exist_ok=True)
            with file_path.open("wb") as file:
                return self._dump(data, file)

        return self._dump(data, file)

    @abstractmethod
    def _dump(
        self,
        data: npt.NDArray[np.int64 | np.float64 | np.complex128],
        dest: IO[bytes],
    ) -> None:
        ...

new classmethod

new(matrix_format: MatrixFormat) -> MatrixIO

Create new instance of matrix loader for file format. When format is None, file format is deduced from file extension.

Parameters:

Name Type Description Default
matrix_format MatrixFormat

Path to file containing matrix data.

required

Returns:

Type Description
MatrixIO

Matrix loader.

Source code in cssfinder/io/matrix.py
@classmethod
def new(cls, matrix_format: MatrixFormat) -> MatrixIO:
    """Create new instance of matrix loader for file format. When format is None,
    file format is deduced from file extension.

    Parameters
    ----------
    matrix_format : MatrixFormat
        Path to file containing matrix data.

    Returns
    -------
    MatrixIO
        Matrix loader.

    """
    io = FORMAT_TO_LOADER[matrix_format]()
    logging.debug("Selected matrix IO %r for format %r", io, matrix_format.name)
    return io

load

load(
    src: str | Path | IO[bytes],
) -> npt.NDArray[np.int64 | np.float64 | np.complex128]

Load matrix from file as numpy array.

Source code in cssfinder/io/matrix.py
def load(
    self,
    src: str | Path | IO[bytes],
) -> npt.NDArray[np.int64 | np.float64 | np.complex128]:
    """Load matrix from file as numpy array."""
    if isinstance(src, (str, Path)):
        with Path(src).open("rb") as file:
            return self._load(file)

    return self._load(file)

dump

dump(
    data: npt.NDArray[
        np.int64 | np.float64 | np.complex128
    ],
    dest: str | Path | IO[bytes],
) -> None

Dump matrix to file from numpy array.

Source code in cssfinder/io/matrix.py
def dump(
    self,
    data: npt.NDArray[np.int64 | np.float64 | np.complex128],
    dest: str | Path | IO[bytes],
) -> None:
    """Dump matrix to file from numpy array."""
    if isinstance(dest, (str, Path)):
        file_path = Path(dest).with_suffix(self.matrix_format.value)
        file_path.touch(0o777, exist_ok=True)
        with file_path.open("wb") as file:
            return self._dump(data, file)

    return self._dump(data, file)

MatrixMarketIO

Bases: MatrixIO

MatrixIO implementation for loading MatrixMarket exchange format files.

Source code in cssfinder/io/matrix.py
class MatrixMarketIO(MatrixIO):
    """MatrixIO implementation for loading MatrixMarket exchange format files."""

    matrix_format: ClassVar[MatrixFormat] = MatrixFormat.MATRIX_MARKET

    def _load(
        self,
        dest: IO[bytes],
    ) -> npt.NDArray[np.int64 | np.float64 | np.complex128]:
        mtx = scipy.io.mmread(dest)
        if mtx is None:
            raise TypeError
        return np.array(mtx)

    def _dump(
        self,
        data: npt.NDArray[np.int64 | np.float64 | np.complex128],
        dest: IO[bytes],
    ) -> None:
        scipy.io.mmwrite(
            dest,
            data,
            comment=f"Created with CSSFinder {cssfinder.__version__}.",
        )