Skip to content

Commit e9f54cc

Browse files
authored
Merge pull request #4740 from janezd/selectrows-partial-matches
[ENH] Select Rows: Allow partial context matches
2 parents cbe1856 + f237f96 commit e9f54cc

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

Orange/widgets/data/owselectrows.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,30 @@ def match(self, context, domain, attrs, metas):
8080
conditions = context.values["conditions"]
8181
all_vars = attrs.copy()
8282
all_vars.update(metas)
83-
# Use this after 2022/2/2:
84-
# if all(all_vars.get(name) == tpe for name, tpe, *_ in conditions):
85-
if all(all_vars.get(name) == tpe if len(rest) == 2 else name in all_vars
86-
for name, tpe, *rest in conditions):
87-
return 0.5
83+
matched = [all_vars.get(name) == tpe
84+
# After 2022/2/2 remove this line:
85+
if len(rest) == 2 else name in all_vars
86+
for name, tpe, *rest in conditions]
87+
if any(matched):
88+
return 0.5 * sum(matched) / len(matched)
8889
return self.NO_MATCH
8990

91+
def filter_value(self, setting, data, domain, attrs, metas):
92+
if setting.name != "conditions":
93+
super().filter_value(setting, data, domain, attrs, metas)
94+
return
95+
96+
all_vars = attrs.copy()
97+
all_vars.update(metas)
98+
conditions = data["conditions"]
99+
# Use this after 2022/2/2: if any(all_vars.get(name) == tpe:
100+
# conditions[:] = [(name, tpe, *rest) for name, tpe, *rest in conditions
101+
# if all_vars.get(name) == tpe]
102+
conditions[:] = [
103+
(name, tpe, *rest) for name, tpe, *rest in conditions
104+
if (all_vars.get(name) == tpe if len(rest) == 2
105+
else name in all_vars)]
106+
90107

91108
class FilterDiscreteType(enum.Enum):
92109
Equal = "Equal"

Orange/widgets/data/tests/test_owselectrows.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,20 @@ def test_partial_matches(self):
191191
self.assertEqual(condition[1], 2)
192192
self.assertTrue(condition[2][0].startswith("5.2"))
193193

194+
@override_locale(QLocale.C)
195+
def test_partial_matches_with_missing_vars(self):
196+
iris = Table("iris")
197+
domain = iris.domain
198+
self.widget = self.widget_with_context(
199+
domain, [[domain[0].name, 2, ("5.2",)],
200+
[domain[2].name, 2, ("4.2",)]])
201+
iris2 = iris.transform(Domain(domain.attributes[2:], None))
202+
self.send_signal(self.widget.Inputs.data, iris2)
203+
condition = self.widget.conditions[0]
204+
self.assertEqual(condition[0], domain[2].name)
205+
self.assertEqual(condition[1], 2)
206+
self.assertTrue(condition[2][0].startswith("4.2"))
207+
194208
def test_load_settings(self):
195209
iris = Table("iris")[:5]
196210
self.send_signal(self.widget.Inputs.data, iris)

0 commit comments

Comments
 (0)