daf.typing.matrices

The types here describe 2D data without names (that is, not in a pandas.DataFrame), which is how 2D data is stored in daf. Currently there are only two such types:

  • Dense is a 2D numpy.ndarray matrix.

  • Sparse is a compressed sparse matrix (either scipy.sparse.csr_matrix or scipy.sparse.csc_matrix).

The Matrix type annotations is simply their union, that is, allows for “any” 2D data without names. While this isn’t very useful to directly perform operation on, it is very useful as the return type of fetching 2D data stored in daf, as the caller has no control over whether the data was stored as sparse, and forcing it to be dense would not be practical for large data sets.

Note

The Matrix type should be only be directly used in computations with great care, as some operations are subtly different for numpy 2D arrays and scipy.sparse compressed matrices. It is typically better to use one of the concrete types instead.

Data:

Matrix

Any 2D data, in either ROW_MAJOR or COLUMN_MAJOR layout, without names.

MatrixInRows

Any 2D data in ROW_MAJOR layout, without names.

MatrixInColumns

Any 2D data in COLUMN_MAJOR layout, without names.

Functions:

is_matrix(data, *[, dtype, shape, layout])

Assert that some data is a Matrix, optionally only of some dtype, optionally only of some shape, optionally only of some layout, and return it as such for mypy.

be_matrix(data, *[, dtype, shape, layout])

Assert that some data is a Matrix, optionally only of some dtype, optionally only of some shape, optionally only of some layout, and return it as such for mypy.

as_matrix()

Access the internal 2D matrix, if possible; otherwise, or if force_copy, return a copy of the 2D data as a numpy array.

is_matrix_in_rows(data, *[, dtype, shape])

Assert that some data is a MatrixInRows, optionally only of some dtype, optionally only of some shape, and return it as such for mypy.

be_matrix_in_rows(data, *[, dtype, shape])

Assert that some data is a MatrixInRows, optionally only of some dtype, optionally only of some shape, and return it as such for mypy.

is_matrix_in_columns(data, *[, dtype, shape])

Assert that some data is a MatrixInColumns, optionally only of some dtype, optionally only of some shape, and return it as such for mypy.

be_matrix_in_columns(data, *[, dtype, shape])

Assert that some data is a MatrixInColumns, optionally only of some dtype, optionally only of some shape, and return it as such for mypy.

daf.typing.matrices.Matrix(*args, **kwargs)

Any 2D data, in either ROW_MAJOR or COLUMN_MAJOR layout, without names.

Note

This is not to be confused with the deprecated numpy.matrix type which must never be used.

alias of Union[DenseInRows, DenseInColumns, SparseInRows, SparseInColumns]

daf.typing.matrices.is_matrix(data: Any, *, dtype: Optional[_dtypes.DTypes] = None, shape: Optional[Tuple[int, int]] = None, layout: Optional[_layouts.AnyMajor] = None) TypeGuard[Matrix][source]

Assert that some data is a Matrix, optionally only of some dtype, optionally only of some shape, optionally only of some layout, and return it as such for mypy.

By default, checks that the data type is one of ALL_DTYPES.

daf.typing.matrices.be_matrix(data: Any, *, dtype: Optional[Union[str, dtype, Collection[str], Collection[dtype], Collection[Union[str, dtype]]]] = None, shape: Optional[Tuple[int, int]] = None, layout: Optional[AnyMajor] = None) Union[DenseInRows, DenseInColumns, SparseInRows, SparseInColumns][source]

Assert that some data is a Matrix, optionally only of some dtype, optionally only of some shape, optionally only of some layout, and return it as such for mypy.

By default, checks that the data type is one of ALL_DTYPES.

daf.typing.matrices.as_matrix(data: DenseInRows, *, force_copy: bool = False) DenseInRows[source]
daf.typing.matrices.as_matrix(data: DenseInColumns, *, force_copy: bool = False) DenseInColumns
daf.typing.matrices.as_matrix(data: SparseInRows, *, force_copy: bool = False) SparseInRows
daf.typing.matrices.as_matrix(data: SparseInColumns, *, force_copy: bool = False) SparseInColumns
daf.typing.matrices.as_matrix(data: spmatrix, *, force_copy: bool = False) Union[SparseInRows, SparseInColumns]
daf.typing.matrices.as_matrix(data: FrameInRows, *, force_copy: bool = False) DenseInRows
daf.typing.matrices.as_matrix(data: FrameInColumns, *, force_copy: bool = False) DenseInColumns
daf.typing.matrices.as_matrix(data: DataFrame, *, force_copy: bool = False) Union[DenseInRows, DenseInColumns]
daf.typing.matrices.as_matrix(data: Union[Sequence[Any], ndarray, _fake_sparse.spmatrix, Series, DataFrame], *, force_copy: bool = False) Union[DenseInRows, DenseInColumns, SparseInRows, SparseInColumns]

Access the internal 2D matrix, if possible; otherwise, or if force_copy, return a copy of the 2D data as a numpy array.

If the input is a pandas.DataFrame, this will only work if all the data in the frame has the same type.

daf.typing.matrices.MatrixInRows(*args, **kwargs)

Any 2D data in ROW_MAJOR layout, without names.

alias of Union[DenseInRows, SparseInRows]

daf.typing.matrices.is_matrix_in_rows(data: Any, *, dtype: Optional[_dtypes.DTypes] = None, shape: Optional[Tuple[int, int]] = None) TypeGuard[MatrixInRows][source]

Assert that some data is a MatrixInRows, optionally only of some dtype, optionally only of some shape, and return it as such for mypy.

By default, checks that the data type is one of ALL_DTYPES.

daf.typing.matrices.be_matrix_in_rows(data: Any, *, dtype: Optional[Union[str, dtype, Collection[str], Collection[dtype], Collection[Union[str, dtype]]]] = None, shape: Optional[Tuple[int, int]] = None) Union[DenseInRows, SparseInRows][source]

Assert that some data is a MatrixInRows, optionally only of some dtype, optionally only of some shape, and return it as such for mypy.

By default, checks that the data type is one of ALL_DTYPES.

daf.typing.matrices.MatrixInColumns(*args, **kwargs)

Any 2D data in COLUMN_MAJOR layout, without names.

alias of Union[DenseInColumns, SparseInColumns]

daf.typing.matrices.is_matrix_in_columns(data: Any, *, dtype: Optional[_dtypes.DTypes] = None, shape: Optional[Tuple[int, int]] = None) TypeGuard[MatrixInColumns][source]

Assert that some data is a MatrixInColumns, optionally only of some dtype, optionally only of some shape, and return it as such for mypy.

By default, checks that the data type is one of ALL_DTYPES.

daf.typing.matrices.be_matrix_in_columns(data: Any, *, dtype: Optional[Union[str, dtype, Collection[str], Collection[dtype], Collection[Union[str, dtype]]]] = None, shape: Optional[Tuple[int, int]] = None) Union[DenseInColumns, SparseInColumns][source]

Assert that some data is a MatrixInColumns, optionally only of some dtype, optionally only of some shape, and return it as such for mypy.

By default, checks that the data type is one of ALL_DTYPES.