Skip to content

Commit 40ccac7

Browse files
authored
Merge pull request #1481 from lanzagar/filename
[FIX] Table names set by readers
2 parents df1c477 + 39e597a commit 40ccac7

File tree

6 files changed

+33
-5
lines changed

6 files changed

+33
-5
lines changed

Orange/data/io.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import contextlib
22
import csv
33
import locale
4+
import os
45
import pickle
56
import re
67
import subprocess
@@ -668,6 +669,14 @@ def read(self):
668669
try:
669670
reader = csv.reader(file, dialect=dialect)
670671
data = self.data_table(reader)
672+
673+
# TODO: Name can be set unconditionally when/if
674+
# self.filename will always be a string with the file name.
675+
# Currently, some tests pass StringIO instead of
676+
# the file name to a reader.
677+
if isinstance(self.filename, str):
678+
data.name = os.path.splitext(
679+
os.path.split(self.filename)[-1])[0]
671680
if error and isinstance(error, UnicodeDecodeError):
672681
pos, endpos = error.args[2], error.args[3]
673682
warning = ('Skipped invalid byte(s) in position '
@@ -731,8 +740,10 @@ def constr_vars(inds):
731740
classes = constr_vars(class_indices)
732741
meta_attrs = constr_vars(meta_indices)
733742
domain = Domain(attrs, classes, meta_attrs)
734-
return Table.from_numpy(
743+
table = Table.from_numpy(
735744
domain, attrs and X, classes and Y, metas and meta_attrs)
745+
table.name = os.path.splitext(os.path.split(self.filename)[-1])[0]
746+
return table
736747

737748

738749
class ExcelReader(FileFormat):
@@ -767,6 +778,9 @@ def read(self):
767778
for col in range(first_col, row_len)]
768779
for row in range(first_row, ss.nrows)])
769780
table = self.data_table(cells)
781+
table.name = os.path.splitext(os.path.split(self.filename)[-1])[0]
782+
if self.sheet:
783+
table.name = '-'.join((table.name, self.sheet))
770784
except Exception:
771785
raise IOError("Couldn't load spreadsheet from " + self.filename)
772786
return table

Orange/data/table.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,6 @@ def from_file(cls, filename):
525525
if cls != data.__class__:
526526
data = cls(data)
527527

528-
data.name = os.path.splitext(os.path.split(filename)[-1])[0]
529528
# no need to call _init_ids as fuctions from .io already
530529
# construct a table with .ids
531530

Orange/tests/test_basket_reader.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ def test_handles_quote(self, fname):
8989
table = read_basket(fname)
9090
self.assertEqual(len(table.domain.variables), 4)
9191

92+
def test_data_name(self):
93+
filename = os.path.join(os.path.dirname(__file__), 'iris_basket.basket')
94+
self.assertEqual(read_basket(filename).name, 'iris_basket')
95+
9296

9397
if __name__ == "__main__":
9498
unittest.main()

Orange/tests/test_tab_reader.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99

1010
import numpy as np
1111

12-
from Orange.data import Table, ContinuousVariable, DiscreteVariable
12+
from Orange.data import Table, DiscreteVariable
1313
from Orange.data.io import TabReader
1414

15+
1516
def read_tab_file(filename):
1617
return TabReader(filename).read()
1718

@@ -152,3 +153,9 @@ def test_attributes_saving(self):
152153
table = Table(path.join(tempdir, "out.tab"))
153154
self.assertEqual(table.attributes[1], "test")
154155
shutil.rmtree(tempdir)
156+
157+
def test_data_name(self):
158+
table1 = Table('iris')
159+
table2 = TabReader(table1.__file__).read()
160+
self.assertEqual(table1.name, 'iris')
161+
self.assertEqual(table2.name, 'iris')

Orange/tests/test_xlsx_reader.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import numpy as np
88

9-
from Orange.data import io, ContinuousVariable, DiscreteVariable, StringVariable
9+
from Orange.data import io, ContinuousVariable, DiscreteVariable
1010

1111

1212
def get_dataset(name):
@@ -31,6 +31,7 @@ def test_read(self):
3131
np.array([[0.1, 0.5, 0.1, 21],
3232
[0.2, 0.1, 2.5, 123],
3333
[0, 0, 0, 0]]))
34+
self.assertEqual(table.name, 'header_0')
3435

3536

3637
class TextExcelSheets(unittest.TestCase):
@@ -45,6 +46,7 @@ def test_named_sheet(self):
4546
self.reader.select_sheet("my_sheet")
4647
table = self.reader.read()
4748
self.assertEqual(len(table.domain.attributes), 4)
49+
self.assertEqual(table.name, 'header_0_sheet-my_sheet')
4850

4951

5052
class TestExcelHeader1(unittest.TestCase):
@@ -94,6 +96,7 @@ def test_flags(self):
9496
np.testing.assert_almost_equal(
9597
table.metas[:, 1], np.array([0, 1, 2, 3] * 5 + [0, 1, 2]))
9698

99+
97100
class TestExcelHeader3(unittest.TestCase):
98101
def test_read(self):
99102
table = read_file("header_3.xlsx")
@@ -130,5 +133,6 @@ def test_read(self):
130133
np.testing.assert_equal(
131134
table.metas[:, 2], np.array(list("abcdefghijklmnopqrstuvw")))
132135

136+
133137
if __name__ == "__main__":
134138
unittest.main()

Orange/widgets/data/owfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def load_data(self):
256256
try:
257257
data = self.reader.read()
258258
except Exception as ex:
259-
errors.append("An error occured:")
259+
errors.append("An error occurred:")
260260
errors.append(str(ex))
261261
data = None
262262
self.editor_model.reset()

0 commit comments

Comments
 (0)