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 3 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
24 changes: 13 additions & 11 deletions Orange/data/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,8 @@ 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):
return cls.write_file(filename, data, with_annotations)

@classmethod
def write_table_metadata(cls, filename, data):
Expand Down Expand Up @@ -798,11 +798,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):
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe 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 @@ -915,12 +916,13 @@ 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)
if with_annotations:
Copy link
Contributor

Choose a reason for hiding this comment

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

I would remove this if - saving the additional metadata file is not related to type annotations for variables.

cls.write_table_metadata(filename, data)


class TabReader(CSVReader):
Expand All @@ -947,7 +949,7 @@ def read(self):
return table

@classmethod
def write_file(cls, filename, data):
def write_file(cls, filename, data, with_annotations=True):
with cls.open(filename, 'wb') as f:
pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)

Expand Down Expand Up @@ -1024,7 +1026,7 @@ def read(self):
return table

@classmethod
def write_file(cls, filename, data):
def write_file(cls, filename, data, with_annotations=True):
vars = list(chain((ContinuousVariable('_w'),) if data.has_weights() else (),
data.domain.attributes,
data.domain.class_vars,
Expand Down Expand Up @@ -1058,7 +1060,7 @@ def write_graph(cls, filename, graph):
tree.export_graphviz(graph, out_file=cls.open(filename, 'wt'))

@classmethod
def write(cls, filename, tree):
def write(cls, filename, tree, with_annotations):
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe with_annotations=True.

if type(tree) == dict:
tree = tree['tree']
cls.write_graph(filename, tree)
Expand Down
11 changes: 10 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)
with_annotations = Setting(True)

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

box.layout().addLayout(form)

self.annotations_cb = gui.checkBox(
self.controlArea, self, "with_annotations", label="Save with Orange annotations",
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe rename the setting to Add type annotations.

)

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 +180,18 @@ def save_file(self):
os.path.join(
self.last_dir,
self.basename + self.type_ext + self.compress_ext),
self.data)
self.data, self.with_annotations,
Copy link
Contributor

Choose a reason for hiding this comment

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

This breaks compatibility with existing additional writers (if there are any!) that don't accept this argument. Writers that do not support it, should be called without it (see the general comment).

)
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(True)
if self.type_ext in ['.pkl', '.xlsx']:
Copy link
Contributor

Choose a reason for hiding this comment

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

Whether this checkbox is shown should be decided by the file writer and not hard coded in the widget. See the general comment.

self.annotations_cb.setEnabled(False)
self.compress_ext = dict(COMPRESSIONS)[self.compression] if self.compress else ''

def _update_text(self):
Expand Down