-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
108ba0e
commit 5934a91
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import numpy as np | ||
import pytest | ||
import sparse | ||
from xarray import DataArray | ||
|
||
from depiction.image.xarray_helper import XarrayHelper | ||
|
||
|
||
@pytest.fixture | ||
def dense_dataarray() -> DataArray: | ||
data = np.array([[1, 2, 3], [4, 5, 6]]) | ||
return DataArray(data, dims=["x", "y"]) | ||
|
||
|
||
@pytest.fixture | ||
def sparse_dataarray() -> DataArray: | ||
data = sparse.COO.from_numpy(np.array([[1, 0, 0], [0, 0, 6]])) | ||
return DataArray(data, dims=["x", "y"]) | ||
|
||
|
||
def test_is_sparse_dense(dense_dataarray) -> None: | ||
assert not XarrayHelper.is_sparse(dense_dataarray) | ||
|
||
|
||
def test_is_sparse_sparse(sparse_dataarray) -> None: | ||
assert XarrayHelper.is_sparse(sparse_dataarray) | ||
|
||
|
||
def test_ensure_dense_dense(dense_dataarray) -> None: | ||
result = XarrayHelper.ensure_dense(dense_dataarray) | ||
assert isinstance(result.data, np.ndarray) | ||
assert np.array_equal(result.data, dense_dataarray.data) | ||
|
||
|
||
def test_ensure_dense_sparse(sparse_dataarray) -> None: | ||
result = XarrayHelper.ensure_dense(sparse_dataarray) | ||
assert isinstance(result.data, np.ndarray) | ||
assert np.array_equal(result.data, sparse_dataarray.data.todense()) | ||
|
||
|
||
def test_ensure_dense_copy(dense_dataarray) -> None: | ||
result = XarrayHelper.ensure_dense(dense_dataarray, copy=True) | ||
assert isinstance(result.data, np.ndarray) | ||
assert np.array_equal(result.data, dense_dataarray.data) | ||
assert result is not dense_dataarray | ||
assert np.shares_memory(result.data, dense_dataarray.data) == False | ||
|
||
|
||
def test_ensure_dense_no_copy(dense_dataarray) -> None: | ||
result = XarrayHelper.ensure_dense(dense_dataarray, copy=False) | ||
assert result is dense_dataarray | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main() |