Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion Orange/data/io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import csv
import locale
import os
import pickle
import re
import subprocess
Expand Down Expand Up @@ -668,6 +669,14 @@ def read(self):
try:
reader = csv.reader(file, dialect=dialect)
data = self.data_table(reader)

# TODO: Name can be set unconditionally when/if
# self.filename will always be a string with the file name.
# Currently, some tests pass StringIO instead of
# the file name to a reader.
if isinstance(self.filename, str):
data.name = os.path.splitext(
os.path.split(self.filename)[-1])[0]
if error and isinstance(error, UnicodeDecodeError):
pos, endpos = error.args[2], error.args[3]
warning = ('Skipped invalid byte(s) in position '
Expand Down Expand Up @@ -731,8 +740,10 @@ def constr_vars(inds):
classes = constr_vars(class_indices)
meta_attrs = constr_vars(meta_indices)
domain = Domain(attrs, classes, meta_attrs)
return Table.from_numpy(
table = Table.from_numpy(
domain, attrs and X, classes and Y, metas and meta_attrs)
table.name = os.path.splitext(os.path.split(self.filename)[-1])[0]
return table


class ExcelReader(FileFormat):
Expand Down Expand Up @@ -767,6 +778,9 @@ def read(self):
for col in range(first_col, row_len)]
for row in range(first_row, ss.nrows)])
table = self.data_table(cells)
table.name = os.path.splitext(os.path.split(self.filename)[-1])[0]
if self.sheet:
table.name = '-'.join((table.name, self.sheet))
except Exception:
raise IOError("Couldn't load spreadsheet from " + self.filename)
return table
Expand Down
1 change: 0 additions & 1 deletion Orange/data/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,6 @@ def from_file(cls, filename):
if cls != data.__class__:
data = cls(data)

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

Expand Down
4 changes: 4 additions & 0 deletions Orange/tests/test_basket_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ def test_handles_quote(self, fname):
table = read_basket(fname)
self.assertEqual(len(table.domain.variables), 4)

def test_data_name(self):
filename = os.path.join(os.path.dirname(__file__), 'iris_basket.basket')
self.assertEqual(read_basket(filename).name, 'iris_basket')


if __name__ == "__main__":
unittest.main()
9 changes: 8 additions & 1 deletion Orange/tests/test_tab_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

import numpy as np

from Orange.data import Table, ContinuousVariable, DiscreteVariable
from Orange.data import Table, DiscreteVariable
from Orange.data.io import TabReader


def read_tab_file(filename):
return TabReader(filename).read()

Expand Down Expand Up @@ -152,3 +153,9 @@ def test_attributes_saving(self):
table = Table(path.join(tempdir, "out.tab"))
self.assertEqual(table.attributes[1], "test")
shutil.rmtree(tempdir)

def test_data_name(self):
table1 = Table('iris')
table2 = TabReader(table1.__file__).read()
self.assertEqual(table1.name, 'iris')
self.assertEqual(table2.name, 'iris')
6 changes: 5 additions & 1 deletion Orange/tests/test_xlsx_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import numpy as np

from Orange.data import io, ContinuousVariable, DiscreteVariable, StringVariable
from Orange.data import io, ContinuousVariable, DiscreteVariable


def get_dataset(name):
Expand All @@ -31,6 +31,7 @@ def test_read(self):
np.array([[0.1, 0.5, 0.1, 21],
[0.2, 0.1, 2.5, 123],
[0, 0, 0, 0]]))
self.assertEqual(table.name, 'header_0')


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


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


class TestExcelHeader3(unittest.TestCase):
def test_read(self):
table = read_file("header_3.xlsx")
Expand Down Expand Up @@ -130,5 +133,6 @@ def test_read(self):
np.testing.assert_equal(
table.metas[:, 2], np.array(list("abcdefghijklmnopqrstuvw")))


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion Orange/widgets/data/owfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def load_data(self):
try:
data = self.reader.read()
except Exception as ex:
errors.append("An error occured:")
errors.append("An error occurred:")
errors.append(str(ex))
data = None
self.editor_model.reset()
Expand Down