Skip to content

Commit e75f21c

Browse files
authored
Merge pull request #6252 from markotoplak/instance-str
RowInstance: use existing _x, _y, _metas for printout
2 parents 3835959 + 19e4124 commit e75f21c

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

Orange/data/table.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,20 @@ def __setitem__(self, key, value):
160160
self._metas[-1 - key] = value
161161

162162
def _str(self, limit):
163-
def sp_values(matrix, variables):
164-
if not sp.issparse(matrix):
165-
if matrix.ndim == 1:
166-
matrix = matrix[:, np.newaxis]
167-
return Instance.str_values(matrix[row], variables, limit)
163+
def sp_values(row, variables, sparsity=None):
164+
if sparsity is None:
165+
return Instance.str_values(row, variables, limit)
168166

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

175174
var = variables[idx]
176-
if var.is_discrete or matrix[row, idx]:
177-
row_entries.append("%s=%s" % (var.name, var.str_val(matrix[row, idx])))
175+
if var.is_discrete or row[idx]:
176+
row_entries.append("%s=%s" % (var.name, var.str_val(row[idx])))
178177

179178
idx += 1
180179

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

186185
return s
187186

188-
table = self.table
189-
domain = table.domain
190-
row = self.row_index
191-
s = "[" + sp_values(table.X, domain.attributes)
187+
domain = self._domain
188+
s = "[" + sp_values(self._x, domain.attributes, self.sparse_x)
192189
if domain.class_vars:
193-
s += " | " + sp_values(table.Y, domain.class_vars)
190+
s += " | " + sp_values(self._y, domain.class_vars, self.sparse_y)
194191
s += "]"
195-
if self._domain.metas:
196-
s += " {" + sp_values(table.metas, domain.metas) + "}"
192+
if domain.metas:
193+
s += " {" + sp_values(self._metas, domain.metas, self.sparse_metas) + "}"
197194
return s
198195

199196
def __str__(self):

Orange/tests/test_table.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,10 @@ def test_repr_sparse_with_one_row(self):
11821182
table = data.Table("iris")[:1]
11831183
with table.unlocked_reference():
11841184
table.X = sp.csr_matrix(table.X)
1185-
repr(table) # make sure repr does not crash
1185+
r = repr(table) # make sure repr does not crash
1186+
self.assertEqual(r.replace("\n", ""),
1187+
"[[sepal length=5.1, sepal width=3.5, "
1188+
"petal length=1.4, petal width=0.2 | Iris-setosa]]")
11861189

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

1198+
def test_str(self):
1199+
iris = Table("iris")
1200+
# instance
1201+
self.assertEqual("[5.1, 3.5, 1.4, 0.2 | Iris-setosa]", str(iris[0]))
1202+
# table
1203+
table_str = str(iris)
1204+
lines = table_str.split('\n')
1205+
self.assertEqual(150, len(lines))
1206+
self.assertEqual("[[5.1, 3.5, 1.4, 0.2 | Iris-setosa],", lines[0])
1207+
self.assertEqual(" [5.9, 3.0, 5.1, 1.8 | Iris-virginica]]", lines[-1])
1208+
1209+
def test_str_sparse(self):
1210+
iris = Table("iris")
1211+
with iris.unlocked_reference():
1212+
iris.X = sp.csr_matrix(iris.X)
1213+
# instance
1214+
s0 = "[sepal length=5.1, sepal width=3.5, " \
1215+
"petal length=1.4, petal width=0.2 | Iris-setosa]"
1216+
self.assertEqual(s0, str(iris[0]))
1217+
# table
1218+
table_str = str(iris)
1219+
lines = table_str.split('\n')
1220+
self.assertEqual(150, len(lines))
1221+
self.assertEqual("[" + s0 + ",", lines[0])
1222+
slast = "[sepal length=5.9, sepal width=3.0, " \
1223+
"petal length=5.1, petal width=1.8 | Iris-virginica]"
1224+
self.assertEqual(" " + slast + "]", lines[-1])
1225+
11951226

11961227
def column_sizes(table):
11971228
return (len(table.domain.attributes),

0 commit comments

Comments
 (0)