Skip to content

Commit 63f4b63

Browse files
committed
Continuize: Tets and fixes; migration
1 parent d01b96e commit 63f4b63

File tree

2 files changed

+361
-196
lines changed

2 files changed

+361
-196
lines changed

Orange/widgets/data/owcontinuize.py

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from functools import partial, reduce
2-
from itertools import chain
3-
from operator import add
1+
from functools import partial
42
from types import SimpleNamespace
53
from typing import NamedTuple, Dict, List
64

@@ -418,22 +416,26 @@ def setData(self, data):
418416
domain is None or domain.has_continuous_attributes(include_metas=True))
419417
self.class_box.setVisible(
420418
domain is None or domain.has_discrete_class)
421-
if data is not None:
422-
# Clean up hints, but only when receiving new data, not on
423-
# disconnection
424-
for hints, type_ in ((self.cont_var_hints, ContinuousVariable),
425-
(self.disc_var_hints, DiscreteVariable)):
426-
# time is not continuous, pylint: disable=unidiomatic-typecheck
427-
filtered = {
428-
var.name: hints[var]
429-
for var in chain(domain.attributes, domain.metas)
430-
if type(var) is type_ and var.name in hints
431-
}
432-
filtered[DefaultKey] = hints[DefaultKey]
433-
hints.clear()
434-
hints.update(filtered)
419+
if data:
420+
# Clean up hints only when receiving new data, not on disconnection
421+
self._set_hints()
435422
self.commit.now()
436423

424+
def _set_hints(self):
425+
assert self.data
426+
for hints, model, options in (
427+
(self.cont_var_hints, self.cont_view.model(), ContinuousOptions),
428+
(self.disc_var_hints, self.disc_view.model(), DiscreteOptions)):
429+
filtered = {DefaultKey: hints[DefaultKey]}
430+
for i, var in enumerate(model):
431+
if var is model.Separator or var.name not in hints:
432+
continue
433+
filtered[var.name] = hints[var.name]
434+
model.setData(model.index(i, 0),
435+
options[hints[var.name]].short_desc, Qt.UserRole)
436+
hints.clear()
437+
hints.update(filtered)
438+
437439
@gui.deferred
438440
def commit(self):
439441
self.Outputs.data.send(self._prepare_output())
@@ -494,13 +496,12 @@ def _unsupported_sparse(self):
494496

495497
def _create_vars(self, part):
496498
# time is not continuous, pylint: disable=unidiomatic-typecheck
497-
return reduce(
498-
add,
499+
return sum(
499500
(self._continuized_vars(var) if var.is_discrete
500501
else self._scaled_vars(var) if type(var) is ContinuousVariable
501502
else [var]
502503
for var in part),
503-
[])
504+
start=[])
504505

505506
def _get(self, var, stat):
506507
def most_frequent(col):
@@ -528,12 +529,13 @@ def _scaled_vars(self, var):
528529

529530
get = partial(self._get, var)
530531
if hint == Normalize.Standardize:
531-
off, scale = get("mean"), get("std")
532+
off, scale = get("mean"), 1 / (get("std") or 1)
532533
elif hint == Normalize.Center:
533534
off, scale = get("mean"), 1
534535
elif hint == Normalize.Scale:
535-
off, scale = 0, get("std")
536+
off, scale = 0, 1 / (get("std") or 1)
536537
else:
538+
assert hint in (Normalize.Normalize11, Normalize.Normalize01), f"hint={hint}?!"
537539
min_, max_ = get("min"), get("max")
538540
span = (max_ - min_) or 1
539541
if hint == Normalize.Normalize11:
@@ -577,7 +579,7 @@ def _continuized_vars(self, var, hint=None):
577579
elif hint == Continuize.Indicators:
578580
base = None
579581
else:
580-
assert False
582+
assert False, f"hint={hint}?!"
581583
return [
582584
ContinuousVariable(f"{var.name}={value}",
583585
compute_value=Indicator(var, value=i))
@@ -601,7 +603,17 @@ def migrate_settings(cls, settings, version):
601603
settings["continuous_treatment"] = Normalize.Normalize11
602604
elif cont_treat == 2:
603605
settings["continuous_treatment"] = Normalize.Standardize
604-
# TODO: Migrate to hints
606+
if version < 3:
607+
settings["cont_var_hints"] = \
608+
{DefaultKey:
609+
settings.pop("continuous_treatment", Normalize.Leave)}
610+
settings["disc_var_hints"] = \
611+
{DefaultKey:
612+
settings.pop("multinomial_treatment", Continuize.FirstAsBase)}
613+
614+
615+
# Backward compatibility for unpickling settings
616+
OWContinuize.Normalize = Normalize
605617

606618

607619
if __name__ == "__main__": # pragma: no cover

0 commit comments

Comments
 (0)