Skip to content

Commit

Permalink
Merge pull request #6252 from markotoplak/instance-str
Browse files Browse the repository at this point in the history
RowInstance: use existing _x, _y, _metas for printout
  • Loading branch information
lanzagar authored Dec 15, 2022
2 parents 3835959 + 19e4124 commit e75f21c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
25 changes: 11 additions & 14 deletions Orange/data/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,20 @@ def __setitem__(self, key, value):
self._metas[-1 - key] = value

def _str(self, limit):
def sp_values(matrix, variables):
if not sp.issparse(matrix):
if matrix.ndim == 1:
matrix = matrix[:, np.newaxis]
return Instance.str_values(matrix[row], variables, limit)
def sp_values(row, variables, sparsity=None):
if sparsity is None:
return Instance.str_values(row, variables, limit)

# row is sparse
row_entries, idx = [], 0
while idx < len(variables):
# Make sure to stop printing variables if we limit the output
if limit and len(row_entries) >= 5:
break

var = variables[idx]
if var.is_discrete or matrix[row, idx]:
row_entries.append("%s=%s" % (var.name, var.str_val(matrix[row, idx])))
if var.is_discrete or row[idx]:
row_entries.append("%s=%s" % (var.name, var.str_val(row[idx])))

idx += 1

Expand All @@ -185,15 +184,13 @@ def sp_values(matrix, variables):

return s

table = self.table
domain = table.domain
row = self.row_index
s = "[" + sp_values(table.X, domain.attributes)
domain = self._domain
s = "[" + sp_values(self._x, domain.attributes, self.sparse_x)
if domain.class_vars:
s += " | " + sp_values(table.Y, domain.class_vars)
s += " | " + sp_values(self._y, domain.class_vars, self.sparse_y)
s += "]"
if self._domain.metas:
s += " {" + sp_values(table.metas, domain.metas) + "}"
if domain.metas:
s += " {" + sp_values(self._metas, domain.metas, self.sparse_metas) + "}"
return s

def __str__(self):
Expand Down
33 changes: 32 additions & 1 deletion Orange/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,10 @@ def test_repr_sparse_with_one_row(self):
table = data.Table("iris")[:1]
with table.unlocked_reference():
table.X = sp.csr_matrix(table.X)
repr(table) # make sure repr does not crash
r = repr(table) # make sure repr does not crash
self.assertEqual(r.replace("\n", ""),
"[[sepal length=5.1, sepal width=3.5, "
"petal length=1.4, petal width=0.2 | Iris-setosa]]")

def test_inf(self):
a = np.array([[2, 0, 0, 0],
Expand All @@ -1192,6 +1195,34 @@ def test_inf(self):
tab = data.Table.from_numpy(None, a)
self.assertEqual(tab.get_nan_frequency_attribute(), 3/12)

def test_str(self):
iris = Table("iris")
# instance
self.assertEqual("[5.1, 3.5, 1.4, 0.2 | Iris-setosa]", str(iris[0]))
# table
table_str = str(iris)
lines = table_str.split('\n')
self.assertEqual(150, len(lines))
self.assertEqual("[[5.1, 3.5, 1.4, 0.2 | Iris-setosa],", lines[0])
self.assertEqual(" [5.9, 3.0, 5.1, 1.8 | Iris-virginica]]", lines[-1])

def test_str_sparse(self):
iris = Table("iris")
with iris.unlocked_reference():
iris.X = sp.csr_matrix(iris.X)
# instance
s0 = "[sepal length=5.1, sepal width=3.5, " \
"petal length=1.4, petal width=0.2 | Iris-setosa]"
self.assertEqual(s0, str(iris[0]))
# table
table_str = str(iris)
lines = table_str.split('\n')
self.assertEqual(150, len(lines))
self.assertEqual("[" + s0 + ",", lines[0])
slast = "[sepal length=5.9, sepal width=3.0, " \
"petal length=5.1, petal width=1.8 | Iris-virginica]"
self.assertEqual(" " + slast + "]", lines[-1])


def column_sizes(table):
return (len(table.domain.attributes),
Expand Down

0 comments on commit e75f21c

Please sign in to comment.