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

Network File: Fix crash when two variables are equally preferable #267

Merged
merged 1 commit into from
Dec 22, 2023
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
7 changes: 5 additions & 2 deletions orangecontrib/network/widgets/OWNxFile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from operator import itemgetter
from os import path
from itertools import product
from traceback import format_exception_only
Expand Down Expand Up @@ -243,13 +244,15 @@ def _vars_for_label(self, data: Table):
values= data.get_column(var)
values = values[values != ""]
set_values = set(values)
# values have to be unique, and have to include all labels
if len(values) != len(set_values) \
or not original_nodes <= set_values:
continue
vars_and_overs.append((len(set_values - original_nodes), var))
vars_and_overs.append((len(set_values), var))
if not vars_and_overs:
return None, []
_, best_var = min(vars_and_overs)
# Prefer variables with less extra values
_, best_var = min(vars_and_overs, key=itemgetter(0))
useful_string_vars = [var for _, var in vars_and_overs]
return best_var, useful_string_vars

Expand Down
19 changes: 19 additions & 0 deletions orangecontrib/network/widgets/tests/test_OWNxFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ def test_vars_for_label(self):
self.assertIs(best_var, domain["label"])
self.assertEqual(useful_vars, [domain["with_extras"], domain["label"]])

data = Orange.data.Table.from_list(
Orange.data.Domain(
[], None, [Orange.data.StringVariable(x) for x in "abcde"]),
[["aa", "", "cc", "aa", ""],
["bb", "bb", "cc", "bb", "aa"],
["cc", "", "aa", "cc", "bb"],
["dd", "aa", "bb", "dd", "cc"],
["ee", "cc", "dd", "ee", ""],
["ff", "ee", "ee", "ff", "dd"],
["gg", "dd", "ff", "", "ee"],
["hh", "ff", "gg", "", "ff"],
["ii", "gg", "", "", "gg"]]
)
domain = data.domain
best_var, useful_vars = self.widget._vars_for_label(data)
self.assertIs(best_var, domain["b"])
# c is not unique and d doesn't cover all values
self.assertEqual(useful_vars, [domain["a"], domain["b"], domain["e"]])

def test_label_combo_contents(self):
widget = self.widget
widget.read_auto_data = Mock()
Expand Down