Skip to content

Commit

Permalink
SimpleDomainContextHandler: match directly to the domain
Browse files Browse the repository at this point in the history
  • Loading branch information
markotoplak committed Aug 2, 2024
1 parent 5695cb3 commit 9a2b01a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
54 changes: 26 additions & 28 deletions Orange/widgets/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def open_context(self, widget, domain):
domain = domain.domain
super().open_context(widget, domain, *self.encode_domain(domain))

def filter_value(self, setting, data, domain, *args):
def _filter_value(self, setting, data, *args):
value = data.get(setting.name, None)
if isinstance(value, list):
new_value = [item for item in value
Expand All @@ -152,6 +152,9 @@ def filter_value(self, setting, data, domain, *args):
and not self._var_exists(setting, value, *args):
del data[setting.name]

def filter_value(self, setting, data, domain, *args):
self._filter_value(setting, data, *args)

@staticmethod
def encode_variable(var):
return var.name, 100 + vartype(var)
Expand Down Expand Up @@ -219,9 +222,9 @@ def _var_exists(cls, setting, value, attributes, metas):
def match(self, context, domain, attrs, metas):
if context.attributes == attrs and context.metas == metas:
return self.PERFECT_MATCH
return self._match(context, domain, attrs, metas)
return self._match(context, attrs, metas)

def _match(self, context, domain, *args):
def _match(self, context, *args):
matches = []
try:
for setting, data, _ in \
Expand Down Expand Up @@ -295,44 +298,39 @@ def is_encoded_var(value):
and value[1] >= 0


class DomainContextHandlerPosition(DomainContextHandler):

def __init__(self, *, match_values=0, first_match=True):
super().__init__(match_values=match_values, first_match=first_match)
class SimpleDomainContextHandler(DomainContextHandler):

def encode_domain(self, domain):
match = self.match_values
encode = self.encode_variables
attributes = encode(domain.attributes, match == self.MATCH_VALUES_ALL)
class_vars = encode(domain.class_vars,
match in (self.MATCH_VALUES_ALL, self.MATCH_VALUES_CLASS))
metas = encode(domain.metas, match == self.MATCH_VALUES_ALL)

return attributes, class_vars, metas
return tuple()

new_context = ContextHandler.new_context

@classmethod
def _var_exists(cls, setting, value, attributes, class_vars, metas):
def _var_exists(cls, setting, value, domain):
assert isinstance(setting, ContextSetting)

if not cls.is_encoded_var(value):
return False

Check warning on line 313 in Orange/widgets/settings.py

View check run for this annotation

Codecov / codecov/patch

Orange/widgets/settings.py#L313

Added line #L313 was not covered by tests

attr_name, attr_type = value
# attr_type used to be either 1-4 for variables stored as string
# settings, and 101-104 for variables stored as variables. The former is
# no longer supported, but we play it safe and still handle both here.
attr_type %= 100
return (not setting.exclude_attributes and
attributes.get(attr_name, -1) == attr_type or
not setting.exclude_class_vars and
class_vars.get(attr_name, -1) == attr_type or
not setting.exclude_metas and
metas.get(attr_name, -1) == attr_type)

def match(self, context, domain, attrs, class_vars, metas):
return self._match(context, domain, attrs, class_vars, metas)
if attr_name not in domain:
return False

candidate = domain[attr_name]
idx = domain.index(candidate)
if (0 <= idx < len(domain.attributes) and setting.exclude_attributes
or idx >= len(domain.attributes) and setting.exclude_class_vars
or idx < 0 and setting.exclude_metas):
return False

return cls.encode_variable(candidate)[1] == attr_type

def filter_value(self, setting, data, domain):
self._filter_value(setting, data, domain)

def match(self, context, domain):
return self._match(context, domain)


class ClassValuesContextHandler(ContextHandler):
Expand Down
2 changes: 1 addition & 1 deletion Orange/widgets/visualize/owheatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class Outputs:

settings_version = 4

settingsHandler = settings.DomainContextHandlerPosition()
settingsHandler = settings.SimpleDomainContextHandler()

# Disable clustering for inputs bigger than this
MaxClustering = 25000
Expand Down

0 comments on commit 9a2b01a

Please sign in to comment.