From 80475e79d60d43054e40f4cdce04187798a21630 Mon Sep 17 00:00:00 2001 From: nikicc Date: Thu, 25 May 2017 12:44:47 +0200 Subject: [PATCH] Table.from_table: Obey is_sparse when returning subarrays When we return subarryas, the flag `is_sparse` wasn't considered, but we simpy returned the subarray in it's original format. --- Orange/data/table.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/Orange/data/table.py b/Orange/data/table.py index d204961e605..e695160554c 100644 --- a/Orange/data/table.py +++ b/Orange/data/table.py @@ -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: @@ -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: