Skip to content

Commit

Permalink
Merge from_sparse
Browse files Browse the repository at this point in the history
  • Loading branch information
dlzou committed Sep 17, 2022
1 parent 865e16d commit 27b29b8
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
6 changes: 5 additions & 1 deletion nums/core/array/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@ def loadtxt(
def scalar(self, value):
return BlockArray.from_scalar(value, self.km)

def array(self, array: Union[np.ndarray, sparse.COO, List[float]], block_shape: tuple = None):
def array(
self,
array: Union[np.ndarray, sparse.COO, List[float]],
block_shape: tuple = None,
):
if not isinstance(array, (np.ndarray, sparse.COO)):
if array_utils.is_array_like(array):
array = np.array(array)
Expand Down
4 changes: 2 additions & 2 deletions nums/core/array/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,10 @@ def from_np(cls, arr, block_shape, copy, km):
return rarr

@classmethod
def from_sparse(cls, arr, block_shape, copy, km, fill_value=0):
def from_sparse(cls, arr, block_shape, copy, km):
dtype_str = str(arr.dtype)
grid = ArrayGrid(arr.shape, block_shape, dtype_str)
rarr = SparseBlockArray(grid, km, fill_value)
rarr = SparseBlockArray(grid, km)
grid_entry_iterator = grid.get_entry_iterator()
for grid_entry in grid_entry_iterator:
grid_slice = grid.get_slice(grid_entry)
Expand Down
2 changes: 1 addition & 1 deletion nums/numpy/api/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def array(object, dtype=None, copy=True, order="K", ndmin=0, subok=False) -> Blo


def from_coo(a: sparse.COO):
assert(isinstance(a, sparse.COO))
assert isinstance(a, sparse.COO)
dtype = np.__getattribute__(str(a.dtype))
shape = a.shape
app = _instance()
Expand Down
4 changes: 1 addition & 3 deletions tests/core/array/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@ def test_sparse_init(app_inst: ArrayApplication):
x_ba = app_inst.array(x1, block_shape=(2, 2))
x_sba = SparseBlockArray.from_ba(x_ba)
assert x_sba.nnz == 8
assert x_sba.nbytes == 8 * 4 + 2 * 8 * 8
y_ba = x_sba.to_ba()
assert np.array_equal(x1, y_ba.get())


def test_from_coo(app_inst: ArrayApplication):
row_coords = [0, 1, 0, 1, 2, 2, 3, 3]
col_coords = [3, 2, 2, 3, 0, 1, 0, 1]
values = [1, 1, 1, 1, 2, 2, 2, 2]
values = [1, 1, 1, 1, 2, 2, 2, 2]
x_sp = sparse.COO([row_coords, col_coords], values)
x_de = x_sp.todense()
x_sba = app_inst.array(x_sp, block_shape=(2, 2))
assert x_sba.nnz == 8
assert x_sba.nbytes == 8 * 4 + 2 * 8 * 8
y_ba = x_sba.to_ba()
assert np.array_equal(x_de, y_ba.get())

Expand Down

0 comments on commit 27b29b8

Please sign in to comment.