Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Removed header types and flags from .csv and .tab #3427

Merged
merged 8 commits into from
Jan 18, 2019
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
22 changes: 14 additions & 8 deletions Orange/data/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ def write_file(cls, filename, data):
# Priority when multiple formats support the same extension. Also
# the sort order in file open/save combo boxes. Lower is better.
PRIORITY = 10000
OPTIONAL_TYPE_ANNOTATIONS = False

def __init__(self, filename):
"""
Expand Down Expand Up @@ -431,8 +432,11 @@ def get_reader(cls, filename):
raise IOError('No readers for file "{}"'.format(filename))

@classmethod
def write(cls, filename, data):
return cls.write_file(filename, data)
def write(cls, filename, data, with_annotations=True):
if cls.OPTIONAL_TYPE_ANNOTATIONS:
return cls.write_file(filename, data, with_annotations)
else:
return cls.write_file(filename, data)

@classmethod
def write_table_metadata(cls, filename, data):
Expand Down Expand Up @@ -798,11 +802,12 @@ def header_flags(data):
zip(repeat('meta'), data.domain.metas)))))

@classmethod
def write_headers(cls, write, data):
def write_headers(cls, write, data, with_annotations=True):
"""`write` is a callback that accepts an iterable"""
write(cls.header_names(data))
write(cls.header_types(data))
write(cls.header_flags(data))
if with_annotations:
write(cls.header_types(data))
write(cls.header_flags(data))

@classmethod
def formatter(cls, var):
Expand Down Expand Up @@ -856,6 +861,7 @@ class CSVReader(FileFormat):
SUPPORT_COMPRESSED = True
SUPPORT_SPARSE_DATA = False
PRIORITY = 20
OPTIONAL_TYPE_ANNOTATIONS = True

def read(self):
for encoding in (lambda: ('us-ascii', None), # fast
Expand Down Expand Up @@ -915,12 +921,12 @@ def read(self):
raise ValueError('Cannot parse dataset {}: {}'.format(self.filename, error)) from error

@classmethod
def write_file(cls, filename, data):
def write_file(cls, filename, data, with_annotations=True):
with cls.open(filename, mode='wt', newline='', encoding='utf-8') as file:
writer = csv.writer(file, delimiter=cls.DELIMITERS[0])
cls.write_headers(writer.writerow, data)
cls.write_headers(writer.writerow, data, with_annotations)
cls.write_data(writer.writerow, data)
cls.write_table_metadata(filename, data)
cls.write_table_metadata(filename, data)


class TabReader(CSVReader):
Expand Down
37 changes: 33 additions & 4 deletions Orange/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
# pylint: disable=missing-docstring

import unittest
from unittest.mock import Mock, patch
import os
import tempfile
import shutil
import io

from Orange.data.io import FileFormat, TabReader, CSVReader, PickleReader
from Orange.data.table import get_sample_datasets_dir
from Orange.data import Table


class WildcardReader(FileFormat):
Expand Down Expand Up @@ -82,11 +84,11 @@ def test_locate_wildcard_extension(self):
fn = os.path.join(tempdir, "t.wild8")
with open(fn, "wt") as f:
f.write("\n")
l = FileFormat.locate("t.wild8", search_dirs=[tempdir])
self.assertEqual(l, fn)
location = FileFormat.locate("t.wild8", search_dirs=[tempdir])
self.assertEqual(location, fn)
# test extension adding
l = FileFormat.locate("t", search_dirs=[tempdir])
self.assertEqual(l, fn)
location = FileFormat.locate("t", search_dirs=[tempdir])
self.assertEqual(location, fn)
shutil.rmtree(tempdir)


Expand Down Expand Up @@ -115,3 +117,30 @@ def test_empty_columns(self):
self.assertEqual(len(table.domain.attributes), 2)
self.assertEqual(cm.warning.args[0],
"Columns with no headers were removed.")

def test_type_annotations(self):
class FooFormat(FileFormat):
write_file = Mock()

FooFormat.write('test_file', None)
FooFormat.write_file.assert_called_with('test_file', None)

FooFormat.OPTIONAL_TYPE_ANNOTATIONS = True
FooFormat.write('test_file', None)
FooFormat.write_file.assert_called_with('test_file', None, True)

FooFormat.write('test_file', None, False)
FooFormat.write_file.assert_called_with('test_file', None, False)

FooFormat.OPTIONAL_TYPE_ANNOTATIONS = False
FooFormat.write('test_file', None)
FooFormat.write_file.assert_called_with('test_file', None)

@patch('csv.DictWriter.writerow')
def test_header_call(self, writer):
CSVReader.write_headers(writer, Table("iris"), False)
self.assertEqual(len(writer.call_args_list), 1)

writer.reset_mock()
CSVReader.write_headers(writer, Table("iris"), True)
self.assertEqual(len(writer.call_args_list), 3)
12 changes: 11 additions & 1 deletion Orange/widgets/data/owsave.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Error(widget.OWWidget.Error):
filetype = Setting(FILE_TYPES[0][0])
compression = Setting(COMPRESSIONS[0][0])
compress = Setting(False)
add_type_annotations = Setting(True)

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -89,6 +90,11 @@ def __init__(self):

box.layout().addLayout(form)

self.annotations_cb = gui.checkBox(
None, self, "add_type_annotations", label="Add type annotations",
)
form.addRow(self.annotations_cb, None)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can fix the layout (checkbox not in the box, but outside and with a smaller indent) by changing this to:

        self.annotations_cb = gui.checkBox(
            None, self, "add_type_annotations", label="Add type annotations",
        )
        form.addRow(self.annotations_cb, None)

The layout of this widget is awful anyway, but this is another issue.

self.save = gui.auto_commit(
self.controlArea, self, "auto_save", "Save", box=False,
commit=self.save_file, callback=self.adjust_label,
Expand Down Expand Up @@ -175,14 +181,18 @@ def save_file(self):
os.path.join(
self.last_dir,
self.basename + self.type_ext + self.compress_ext),
self.data)
self.data, self.add_type_annotations)

except Exception as err_value:
self.error(str(err_value))
else:
self.error()

def update_extension(self):
self.type_ext = [ext for name, ext, _ in FILE_TYPES if name == self.filetype][0]
self.annotations_cb.setEnabled(False)
if self.get_writer_selected().OPTIONAL_TYPE_ANNOTATIONS:
self.annotations_cb.setEnabled(True)
self.compress_ext = dict(COMPRESSIONS)[self.compression] if self.compress else ''

def _update_text(self):
Expand Down
28 changes: 28 additions & 0 deletions Orange/widgets/data/tests/test_owsave.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ def choose_file(a, b, c, d, e, fn=filename, w=writer):
self.widget.save_file_as()
self.assertEqual(len(Table(filename)), 150)

@patch('Orange.data.io.FileFormat.write')
def test_annotations(self, write):
widget = self.widget

self.send_signal(widget.Inputs.data, Table("iris"))
widget.filetype = FILE_TYPES[1][0]
widget.filename = 'foo.csv'
widget.update_extension()

widget.add_type_annotations = False
widget.unconditional_save_file()
write.assert_called()
self.assertFalse(write.call_args[0][2])

widget.add_type_annotations = True
widget.unconditional_save_file()
self.assertTrue(write.call_args[0][2])

def test_disable_checkbox(self):
widget = self.widget
for type_ in FILE_TYPES:
widget.filetype = type_[0]
widget.update_extension()
if widget.get_writer_selected().OPTIONAL_TYPE_ANNOTATIONS:
self.assertTrue(widget.annotations_cb.isEnabled())
else:
self.assertFalse(widget.annotations_cb.isEnabled())

def test_compression(self):
self.send_signal(self.widget.Inputs.data, Table("iris"))

Expand Down