Skip to content

Commit

Permalink
Merge pull request #4740 from janezd/selectrows-partial-matches
Browse files Browse the repository at this point in the history
[ENH] Select Rows: Allow partial context matches
  • Loading branch information
lanzagar authored May 8, 2020
2 parents cbe1856 + f237f96 commit e9f54cc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
27 changes: 22 additions & 5 deletions Orange/widgets/data/owselectrows.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,30 @@ def match(self, context, domain, attrs, metas):
conditions = context.values["conditions"]
all_vars = attrs.copy()
all_vars.update(metas)
# Use this after 2022/2/2:
# if all(all_vars.get(name) == tpe for name, tpe, *_ in conditions):
if all(all_vars.get(name) == tpe if len(rest) == 2 else name in all_vars
for name, tpe, *rest in conditions):
return 0.5
matched = [all_vars.get(name) == tpe
# After 2022/2/2 remove this line:
if len(rest) == 2 else name in all_vars
for name, tpe, *rest in conditions]
if any(matched):
return 0.5 * sum(matched) / len(matched)
return self.NO_MATCH

def filter_value(self, setting, data, domain, attrs, metas):
if setting.name != "conditions":
super().filter_value(setting, data, domain, attrs, metas)
return

all_vars = attrs.copy()
all_vars.update(metas)
conditions = data["conditions"]
# Use this after 2022/2/2: if any(all_vars.get(name) == tpe:
# conditions[:] = [(name, tpe, *rest) for name, tpe, *rest in conditions
# if all_vars.get(name) == tpe]
conditions[:] = [
(name, tpe, *rest) for name, tpe, *rest in conditions
if (all_vars.get(name) == tpe if len(rest) == 2
else name in all_vars)]


class FilterDiscreteType(enum.Enum):
Equal = "Equal"
Expand Down
14 changes: 14 additions & 0 deletions Orange/widgets/data/tests/test_owselectrows.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,20 @@ def test_partial_matches(self):
self.assertEqual(condition[1], 2)
self.assertTrue(condition[2][0].startswith("5.2"))

@override_locale(QLocale.C)
def test_partial_matches_with_missing_vars(self):
iris = Table("iris")
domain = iris.domain
self.widget = self.widget_with_context(
domain, [[domain[0].name, 2, ("5.2",)],
[domain[2].name, 2, ("4.2",)]])
iris2 = iris.transform(Domain(domain.attributes[2:], None))
self.send_signal(self.widget.Inputs.data, iris2)
condition = self.widget.conditions[0]
self.assertEqual(condition[0], domain[2].name)
self.assertEqual(condition[1], 2)
self.assertTrue(condition[2][0].startswith("4.2"))

def test_load_settings(self):
iris = Table("iris")[:5]
self.send_signal(self.widget.Inputs.data, iris)
Expand Down

0 comments on commit e9f54cc

Please sign in to comment.