Skip to content

Commit

Permalink
Table.from_table: Obey is_sparse when returning subarrays
Browse files Browse the repository at this point in the history
When we return subarryas, the flag `is_sparse` wasn't considered, but we simpy returned the subarray in it's original format.
  • Loading branch information
nikicc committed May 25, 2017
1 parent 2e601e9 commit 80475e7
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions Orange/data/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,15 @@ def from_table(cls, domain, source, row_indices=...):

def get_columns(row_indices, src_cols, n_rows, dtype=np.float64,
is_sparse=False):
def match_type(x):
""" Assure that matrix and column are both dense or sparse. """
if is_sparse == sp.issparse(x):
return x
elif is_sparse:
x = np.asarray(x)
return sp.csc_matrix(x.reshape(-1, 1).astype(np.float))
else:
return np.ravel(x.toarray())

if not len(src_cols):
if is_sparse:
Expand All @@ -278,33 +287,23 @@ def get_columns(row_indices, src_cols, n_rows, dtype=np.float64,
n_src_attrs = len(source.domain.attributes)
if all(isinstance(x, Integral) and 0 <= x < n_src_attrs
for x in src_cols):
return _subarray(source.X, row_indices, src_cols)
return match_type(_subarray(source.X, row_indices, src_cols))
if all(isinstance(x, Integral) and x < 0 for x in src_cols):
arr = _subarray(source.metas, row_indices,
[-1 - x for x in src_cols])
arr = match_type(_subarray(source.metas, row_indices,
[-1 - x for x in src_cols]))
if arr.dtype != dtype:
return arr.astype(dtype)
return arr
if all(isinstance(x, Integral) and x >= n_src_attrs
for x in src_cols):
return _subarray(source._Y, row_indices,
[x - n_src_attrs for x in src_cols])
return match_type(_subarray(source._Y, row_indices,
[x - n_src_attrs for x in src_cols]))

if is_sparse:
a = sp.dok_matrix((n_rows, len(src_cols)), dtype=dtype)
else:
a = np.empty((n_rows, len(src_cols)), dtype=dtype)

def match_type(x):
""" Assure that matrix and column are both dense or sparse. """
if is_sparse == sp.issparse(x):
return x
elif is_sparse:
x = np.asarray(x)
return sp.csc_matrix(x.reshape(-1, 1).astype(np.float))
else:
return np.ravel(x.toarray())

shared_cache = _conversion_cache
for i, col in enumerate(src_cols):
if col is None:
Expand Down

0 comments on commit 80475e7

Please sign in to comment.