Skip to content

Commit

Permalink
Get unique names when renaming vars
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrejaKovacic committed Feb 18, 2020
1 parent b5be01d commit 35231b7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
13 changes: 9 additions & 4 deletions Orange/widgets/data/owfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from Orange.widgets.utils.filedialogs import RecentPathsWComboMixin, \
open_filename_dialog
from Orange.widgets.utils.widgetpreview import WidgetPreview
from Orange.widgets.widget import Output
from Orange.widgets.widget import Output, Msg

# Backward compatibility: class RecentPath used to be defined in this module,
# and it is used in saved (pickled) settings. It must be imported into the
Expand Down Expand Up @@ -121,11 +121,13 @@ class Outputs:
domain_editor = SettingProvider(DomainEditor)

class Warning(widget.OWWidget.Warning):
file_too_big = widget.Msg("The file is too large to load automatically."
" Press Reload to load.")
load_warning = widget.Msg("Read warning:\n{}")
file_too_big = Msg("The file is too large to load automatically."
" Press Reload to load.")
load_warning = Msg("Read warning:\n{}")
performance_warning = widget.Msg(
"Categorical variables with >100 values may decrease performance.")
renamed_vars = Msg("Some variables have been renamed "
"to avoid duplicates.\n{}")

class Error(widget.OWWidget.Error):
file_not_found = widget.Msg("File not found.")
Expand Down Expand Up @@ -478,6 +480,7 @@ def _inspect_discrete_variables(self, domain):

def apply_domain_edit(self):
self.Warning.performance_warning.clear()
self.Warning.renamed_vars.clear()
if self.data is None:
table = None
else:
Expand All @@ -493,6 +496,8 @@ def apply_domain_edit(self):
table.ids = np.array(self.data.ids)
table.attributes = getattr(self.data, 'attributes', {})
self._inspect_discrete_variables(domain)
if self.domain_editor.renamed_variables:
self.Warning.renamed_vars(', '.join(self.domain_editor.renamed_variables))

self.Outputs.data.send(table)
self.apply_button.setEnabled(False)
Expand Down
16 changes: 16 additions & 0 deletions Orange/widgets/data/tests/test_owfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ def test_domain_changes_are_stored(self):
data = self.get_output(self.widget.Outputs.data)
self.assertIsInstance(data.domain["iris"], StringVariable)

def test_rename_duplicates(self):
self.open_dataset("iris")

idx = self.widget.domain_editor.model().createIndex(3, 0)
self.assertFalse(self.widget.Warning.renamed_vars.is_shown())
self.widget.domain_editor.model().setData(idx, "iris", Qt.EditRole)
self.widget.apply_button.click()
data = self.get_output(self.widget.Outputs.data)
self.assertIn("iris (1)", data.domain)
self.assertIn("iris (2)", data.domain)
self.assertTrue(self.widget.Warning.renamed_vars.is_shown())

self.widget.domain_editor.model().setData(idx, "different iris", Qt.EditRole)
self.widget.apply_button.click()
self.assertFalse(self.widget.Warning.renamed_vars.is_shown())

def test_variable_name_change(self):
"""
Test whether the name of the variable is changed correctly by
Expand Down
12 changes: 11 additions & 1 deletion Orange/widgets/utils/domaineditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from Orange.data import DiscreteVariable, ContinuousVariable, StringVariable, \
TimeVariable, Domain
from Orange.data.util import get_unique_names_duplicates
from Orange.statistics.util import unique
from Orange.widgets import gui
from Orange.widgets.gui import HorizontalGridDelegate
Expand Down Expand Up @@ -219,6 +220,8 @@ def __init__(self, widget):
self.place_delegate = PlaceDelegate(self, VarTableModel.places)
self.setItemDelegateForColumn(Column.place, self.place_delegate)

self.renamed_variables = []

@staticmethod
def _is_missing(x):
return str(x) in ("nan", "")
Expand Down Expand Up @@ -264,7 +267,7 @@ def get_domain(self, domain, data):
"""
# Allow type-checking with type() instead of isinstance() for exact comparison
# pylint: disable=unidiomatic-typecheck

self.renamed_variables = []
variables = self.model().variables
places = [[], [], []] # attributes, class_vars, metas
cols = [[], [], []] # Xcols, Ycols, Mcols
Expand All @@ -285,6 +288,13 @@ def numbers_are_round(var, col_data):
((mt, Place.meta) for mt in domain.metas)))):
return domain, [data.X, data.Y, data.metas]

unique_names = get_unique_names_duplicates([var[0] for var in variables])
for var, u in zip(variables, unique_names):
if var[0] != u:
self.renamed_variables.append(var[0])
var[0] = u
self.model().set_variables(variables)

for (name, tpe, place, _, may_be_numeric), (orig_var, orig_plc) in \
zip(variables,
chain([(at, Place.feature) for at in domain.attributes],
Expand Down

0 comments on commit 35231b7

Please sign in to comment.