Skip to content

Commit f3abc8e

Browse files
janezdmarkotoplak
authored andcommitted
Enum: Fix reduce of nested enums
1 parent a1bf961 commit f3abc8e

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

Orange/preprocess/preprocess.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ def __call__(self, data):
3333

3434

3535
class Continuize(Preprocess):
36+
MultinomialTreatment = Enum(
37+
"Continuize",
38+
("Indicators", "FirstAsBase", "FrequentAsBase", "Remove",
39+
"RemoveMultinomial", "ReportError", "AsOrdinal", "AsNormalizedOrdinal",
40+
"Leave"),
41+
qualname="Continuize.MultinomialTreatment")
3642
(Indicators, FirstAsBase, FrequentAsBase, Remove, RemoveMultinomial,
37-
ReportError, AsOrdinal, AsNormalizedOrdinal, Leave) = Enum(
38-
"Continuize",
39-
"Indicators, FirstAsBase, FrequentAsBase,"
40-
"Remove, RemoveMultinomial, ReportError, AsOrdinal,"
41-
"AsNormalizedOrdinal, Leave")
43+
ReportError, AsOrdinal, AsNormalizedOrdinal, Leave) = MultinomialTreatment
4244

4345
def __init__(self, zero_based=True,
4446
multinomial_treatment=Indicators):
@@ -255,8 +257,8 @@ class Normalize(Preprocess):
255257
>>> normalizer = Normalize(norm_type=Normalize.NormalizeBySpan)
256258
>>> normalized_data = normalizer(data)
257259
"""
258-
Type = Enum("Normalize",
259-
"NormalizeBySpan, NormalizeBySD")
260+
Type = Enum("Normalize", ("NormalizeBySpan", "NormalizeBySD"),
261+
qualname="Normalize.Type")
260262
NormalizeBySpan, NormalizeBySD = Type
261263

262264
def __init__(self,
@@ -326,8 +328,12 @@ class Randomize(Preprocess):
326328
>>> randomizer = Randomize(Randomize.RandomizeClasses)
327329
>>> randomized_data = randomizer(data)
328330
"""
329-
Type = Enum("Randomize", dict(RandomizeClasses=1, RandomizeAttributes=2,
330-
RandomizeMetas=4), type=int)
331+
Type = Enum("Randomize",
332+
dict(RandomizeClasses=1,
333+
RandomizeAttributes=2,
334+
RandomizeMetas=4),
335+
type=int,
336+
qualname="Randomize.Type")
331337
RandomizeClasses, RandomizeAttributes, RandomizeMetas = Type
332338

333339
def __init__(self, rand_type=RandomizeClasses, rand_seed=None):
@@ -396,8 +402,10 @@ class _MethodEnum(Enum):
396402
def __call__(self, *args, **kwargs):
397403
return getattr(Scale, '_' + self.name)(*args, **kwargs)
398404

399-
CenteringType = _MethodEnum('Scale', 'NoCentering, Mean, Median')
400-
ScalingType = _MethodEnum('Scale', 'NoScaling, Std, Span')
405+
CenteringType = _MethodEnum("Scale", ("NoCentering", "Mean", "Median"),
406+
qualname="Scale.CenteringType")
407+
ScalingType = _MethodEnum("Scale", ("NoScaling", "Std", "Span"),
408+
qualname="Scale.ScalingType")
401409
NoCentering, Mean, Median = CenteringType
402410
NoScaling, Std, Span = ScalingType
403411

Orange/tests/test_preprocess.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# pylint: disable=missing-docstring
33

44
import os
5+
import pickle
56
import unittest
67
from unittest.mock import Mock, MagicMock, patch
78
import numpy as np
@@ -130,3 +131,23 @@ def test_reprs(self):
130131
repr_str = repr(preproc())
131132
new_preproc = eval(repr_str)
132133
self.assertEqual(repr(new_preproc), repr_str)
134+
135+
class TestEnumPickling(unittest.TestCase):
136+
def test_continuize_pickling(self):
137+
c = Continuize(multinomial_treatment=Continuize.FirstAsBase)
138+
s = pickle.dumps(c, -1)
139+
c1 = pickle.loads(s)
140+
self.assertIs(c1.multinomial_treatment, c.multinomial_treatment)
141+
142+
def test_randomize_pickling(self):
143+
c = Randomize(rand_type=Randomize.RandomizeMetas)
144+
s = pickle.dumps(c, -1)
145+
c1 = pickle.loads(s)
146+
self.assertIs(c1.rand_type, c.rand_type)
147+
148+
def test_scaling_pickling(self):
149+
c = Scale(center=Scale.Median, scale=Scale.Span)
150+
s = pickle.dumps(c, -1)
151+
c1 = pickle.loads(s)
152+
self.assertIs(c1.center, c.center)
153+
self.assertIs(c1.scale, c.scale)

0 commit comments

Comments
 (0)