Skip to content

gilbert_io

gilbert_io

Module contains implementation of asset loader class.

GilbertIO

Loader of Gilbert algorithm assets.

Source code in cssfinder/io/gilbert_io.py
class GilbertIO:
    """Loader of Gilbert algorithm assets."""

    def __init__(
        self,
        matrix_format: MatrixFormat = MatrixFormat.MATRIX_MARKET,
    ) -> None:
        self.loader = MatrixIO.new(matrix_format)

    def load_state(self, source: str | Path) -> npt.NDArray[np.complex128]:
        """Load state matrix from file.

        Parameters
        ----------
        source : str | Path
            Path to matrix file.

        Returns
        -------
        npt.NDArray[np.complex128]
            Loaded matrix. Always returns np.complex128.

        """
        mtx = self.loader.load(source).astype(np.complex128)
        logging.info(
            "Loaded matrix from %r of shape %r",
            Path(source).as_posix(),
            mtx.shape,
        )

        # We are expecting loaded ndarray to be a square matrix, all other numbers of
        # dimensions cause crash.
        self._check_matrix_shape(mtx)

        return mtx

    def _check_matrix_shape(self, mtx: npt.NDArray[np.complex128]) -> None:
        """Check if ndarray conforms shape rules."""
        if len(mtx.shape) == NUMBER_OF_DIMENSIONS_IN_MATRIX:
            pass

        elif len(mtx.shape) > NUMBER_OF_DIMENSIONS_IN_MATRIX:
            logging.critical(
                "Expected square matrix but got tensor with shape %r",
                mtx.shape,
            )
            raise NotExpectedTensorError(mtx)

        elif len(mtx.shape) == 1:
            logging.critical(
                "Expected square matrix but got vector with shape %r",
                mtx.shape,
            )
            raise NotExpectedVectorError(mtx)

        elif len(mtx.shape) == 0:
            logging.critical("Expected square matrix but got scalar (%r)", mtx)
            raise NotExpectedScalarError(mtx)

        else:
            raise AssertionError(mtx.shape)

        # Only square matrices are accepted.
        x_size, y_size = mtx.shape
        if x_size != y_size:
            logging.critical("Expected square matrix, but received shape %r", mtx.shape)
            raise IncorrectMatrixShapeError(mtx)

    def load_symmetries(
        self,
        symmetries: Optional[list[list[str]]],
    ) -> list[list[npt.NDArray[np.complex128]]]:
        """Load matrices describing symmetries of system state."""
        if symmetries is None:
            return []
        return [
            [self.loader.load(sym).astype(np.complex128) for sym in row]
            for row in symmetries
        ]

    def load_projection(
        self,
        projection: Optional[str],
    ) -> npt.NDArray[np.complex128] | None:
        """Load matrix describing projection of system state."""
        if projection is None:
            return None

        return self.loader.load(projection).astype(np.complex128)

    def dump_state(self, state: npt.NDArray[np.complex128], dest: str | Path) -> None:
        """Save state to file."""
        return self.loader.dump(state, dest)

    def dump_corrections(self, corrections: Any, dest: str | Path) -> None:
        """Save state to file."""
        file_path = Path(dest).with_suffix(".json")
        with file_path.open("w", encoding="utf-8") as file:
            return json.dump(corrections, file)

    def load_corrections(self, source: Path) -> pd.DataFrame:
        """Load corrections from a JSON file and return them as a pandas DataFrame.

        Parameters
        ----------
        source : Path
            Path to the JSON file containing corrections data.

        Returns
        -------
        pd.DataFrame
            A DataFrame containing the corrections data, with columns renamed to
            "iteration", "index", and "value".

        """
        data_frame: pd.DataFrame = cast(pd.DataFrame, pd.read_json(source))

        return data_frame.rename(
            columns={0: "iteration", 1: "index", 2: "value"},
        )

load_state

load_state(
    source: str | Path,
) -> npt.NDArray[np.complex128]

Load state matrix from file.

Parameters:

Name Type Description Default
source str | Path

Path to matrix file.

required

Returns:

Type Description
NDArray[complex128]

Loaded matrix. Always returns np.complex128.

Source code in cssfinder/io/gilbert_io.py
def load_state(self, source: str | Path) -> npt.NDArray[np.complex128]:
    """Load state matrix from file.

    Parameters
    ----------
    source : str | Path
        Path to matrix file.

    Returns
    -------
    npt.NDArray[np.complex128]
        Loaded matrix. Always returns np.complex128.

    """
    mtx = self.loader.load(source).astype(np.complex128)
    logging.info(
        "Loaded matrix from %r of shape %r",
        Path(source).as_posix(),
        mtx.shape,
    )

    # We are expecting loaded ndarray to be a square matrix, all other numbers of
    # dimensions cause crash.
    self._check_matrix_shape(mtx)

    return mtx

load_symmetries

load_symmetries(
    symmetries: Optional[list[list[str]]],
) -> list[list[npt.NDArray[np.complex128]]]

Load matrices describing symmetries of system state.

Source code in cssfinder/io/gilbert_io.py
def load_symmetries(
    self,
    symmetries: Optional[list[list[str]]],
) -> list[list[npt.NDArray[np.complex128]]]:
    """Load matrices describing symmetries of system state."""
    if symmetries is None:
        return []
    return [
        [self.loader.load(sym).astype(np.complex128) for sym in row]
        for row in symmetries
    ]

load_projection

load_projection(
    projection: Optional[str],
) -> npt.NDArray[np.complex128] | None

Load matrix describing projection of system state.

Source code in cssfinder/io/gilbert_io.py
def load_projection(
    self,
    projection: Optional[str],
) -> npt.NDArray[np.complex128] | None:
    """Load matrix describing projection of system state."""
    if projection is None:
        return None

    return self.loader.load(projection).astype(np.complex128)

dump_state

dump_state(
    state: npt.NDArray[np.complex128], dest: str | Path
) -> None

Save state to file.

Source code in cssfinder/io/gilbert_io.py
def dump_state(self, state: npt.NDArray[np.complex128], dest: str | Path) -> None:
    """Save state to file."""
    return self.loader.dump(state, dest)

dump_corrections

dump_corrections(
    corrections: Any, dest: str | Path
) -> None

Save state to file.

Source code in cssfinder/io/gilbert_io.py
def dump_corrections(self, corrections: Any, dest: str | Path) -> None:
    """Save state to file."""
    file_path = Path(dest).with_suffix(".json")
    with file_path.open("w", encoding="utf-8") as file:
        return json.dump(corrections, file)

load_corrections

load_corrections(source: Path) -> pd.DataFrame

Load corrections from a JSON file and return them as a pandas DataFrame.

Parameters:

Name Type Description Default
source Path

Path to the JSON file containing corrections data.

required

Returns:

Type Description
DataFrame

A DataFrame containing the corrections data, with columns renamed to "iteration", "index", and "value".

Source code in cssfinder/io/gilbert_io.py
def load_corrections(self, source: Path) -> pd.DataFrame:
    """Load corrections from a JSON file and return them as a pandas DataFrame.

    Parameters
    ----------
    source : Path
        Path to the JSON file containing corrections data.

    Returns
    -------
    pd.DataFrame
        A DataFrame containing the corrections data, with columns renamed to
        "iteration", "index", and "value".

    """
    data_frame: pd.DataFrame = cast(pd.DataFrame, pd.read_json(source))

    return data_frame.rename(
        columns={0: "iteration", 1: "index", 2: "value"},
    )

IncorrectMatrixShapeError

Bases: ValueError

Raised when matrix has incorrect shape.

Source code in cssfinder/io/gilbert_io.py
class IncorrectMatrixShapeError(ValueError):
    """Raised when matrix has incorrect shape."""

    def __init__(self, mtx: npt.NDArray[np.complex128]) -> None:
        """Store matrix object in `mtx` attribute."""
        super().__init__()
        self.mtx = mtx

__init__

__init__(mtx: npt.NDArray[np.complex128]) -> None

Store matrix object in mtx attribute.

Source code in cssfinder/io/gilbert_io.py
def __init__(self, mtx: npt.NDArray[np.complex128]) -> None:
    """Store matrix object in `mtx` attribute."""
    super().__init__()
    self.mtx = mtx

NotExpectedTensorError

Bases: IncorrectMatrixShapeError

Raised when got 3+ dimensional tensor instead of matrix.

Source code in cssfinder/io/gilbert_io.py
class NotExpectedTensorError(IncorrectMatrixShapeError):
    """Raised when got 3+ dimensional tensor instead of matrix."""

NotExpectedVectorError

Bases: IncorrectMatrixShapeError

Raised when got vector instead of matrix.

Source code in cssfinder/io/gilbert_io.py
class NotExpectedVectorError(IncorrectMatrixShapeError):
    """Raised when got vector instead of matrix."""

NotExpectedScalarError

Bases: IncorrectMatrixShapeError

Raised when got scalar instead of matrix.

Source code in cssfinder/io/gilbert_io.py
class NotExpectedScalarError(IncorrectMatrixShapeError):
    """Raised when got scalar instead of matrix."""