Skip to content

Commit

Permalink
Added a copy method for SparseNdarrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Jan 29, 2024
1 parent a0a7ccd commit a7b29d2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/delayedarray/SparseNdarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Callable, List, Optional, Sequence, Tuple, Union
from collections import namedtuple
import numpy
import copy

from ._isometric import translate_ufunc_to_op_simple, translate_ufunc_to_op_with_args, ISOMETRIC_OP_WITH_ARGS, _choose_operator, _infer_along_with_args
from ._subset import _spawn_indices, _getitem_subset_preserves_dimensions, _getitem_subset_discards_dimensions, _repr_subset
Expand Down Expand Up @@ -777,6 +778,30 @@ def T(self) -> "SparseNdarray":
return _transpose_SparseNdarray(self, axes)


# Other stuff
def __copy__(self) -> "SparseNdarray":
"""
Returns:
A deep copy of this object.
"""
return SparseNdarray(
shape=self._shape,
contents=copy.deepcopy(self._contents),
index_dtype=self._index_dtype,
dtype=self._dtype,
is_masked=self._is_masked,
check=False
)


def copy(self) -> "SparseNdarray":
"""
Returns:
A deep copy of this object.
"""
return self.__copy__()


#########################################################
#########################################################

Expand Down
4 changes: 4 additions & 0 deletions tests/test_SparseNdarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ def test_SparseNdarray_check(mask_rate):
assert delayedarray.is_masked(y) == (mask_rate > 0)
assert delayedarray.is_masked(y) == y.is_masked

clone = copy.copy(y)
clone.contents[0] = "FOOBAR"
assert y.contents[0] != "FOOBAR"

with pytest.raises(ValueError, match="match the extent"):
y = delayedarray.SparseNdarray((10, 15, 1), contents)

Expand Down

0 comments on commit a7b29d2

Please sign in to comment.