Skip to content

Commit

Permalink
Merge pull request #2409 from janezd/enum-nested-reduce
Browse files Browse the repository at this point in the history
[FIX] Some preprocessors couldn't be pickled
  • Loading branch information
markotoplak committed Jun 16, 2017
2 parents 8d882bf + f3abc8e commit 1a965b1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
30 changes: 19 additions & 11 deletions Orange/preprocess/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ def __call__(self, data):


class Continuize(Preprocess):
MultinomialTreatment = Enum(
"Continuize",
("Indicators", "FirstAsBase", "FrequentAsBase", "Remove",
"RemoveMultinomial", "ReportError", "AsOrdinal", "AsNormalizedOrdinal",
"Leave"),
qualname="Continuize.MultinomialTreatment")
(Indicators, FirstAsBase, FrequentAsBase, Remove, RemoveMultinomial,
ReportError, AsOrdinal, AsNormalizedOrdinal, Leave) = Enum(
"Continuize",
"Indicators, FirstAsBase, FrequentAsBase,"
"Remove, RemoveMultinomial, ReportError, AsOrdinal,"
"AsNormalizedOrdinal, Leave")
ReportError, AsOrdinal, AsNormalizedOrdinal, Leave) = MultinomialTreatment

def __init__(self, zero_based=True,
multinomial_treatment=Indicators):
Expand Down Expand Up @@ -255,8 +257,8 @@ class Normalize(Preprocess):
>>> normalizer = Normalize(norm_type=Normalize.NormalizeBySpan)
>>> normalized_data = normalizer(data)
"""
Type = Enum("Normalize",
"NormalizeBySpan, NormalizeBySD")
Type = Enum("Normalize", ("NormalizeBySpan", "NormalizeBySD"),
qualname="Normalize.Type")
NormalizeBySpan, NormalizeBySD = Type

def __init__(self,
Expand Down Expand Up @@ -326,8 +328,12 @@ class Randomize(Preprocess):
>>> randomizer = Randomize(Randomize.RandomizeClasses)
>>> randomized_data = randomizer(data)
"""
Type = Enum("Randomize", dict(RandomizeClasses=1, RandomizeAttributes=2,
RandomizeMetas=4), type=int)
Type = Enum("Randomize",
dict(RandomizeClasses=1,
RandomizeAttributes=2,
RandomizeMetas=4),
type=int,
qualname="Randomize.Type")
RandomizeClasses, RandomizeAttributes, RandomizeMetas = Type

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

CenteringType = _MethodEnum('Scale', 'NoCentering, Mean, Median')
ScalingType = _MethodEnum('Scale', 'NoScaling, Std, Span')
CenteringType = _MethodEnum("Scale", ("NoCentering", "Mean", "Median"),
qualname="Scale.CenteringType")
ScalingType = _MethodEnum("Scale", ("NoScaling", "Std", "Span"),
qualname="Scale.ScalingType")
NoCentering, Mean, Median = CenteringType
NoScaling, Std, Span = ScalingType

Expand Down
21 changes: 21 additions & 0 deletions Orange/tests/test_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# pylint: disable=missing-docstring

import os
import pickle
import unittest
from unittest.mock import Mock, MagicMock, patch
import numpy as np
Expand Down Expand Up @@ -130,3 +131,23 @@ def test_reprs(self):
repr_str = repr(preproc())
new_preproc = eval(repr_str)
self.assertEqual(repr(new_preproc), repr_str)

class TestEnumPickling(unittest.TestCase):
def test_continuize_pickling(self):
c = Continuize(multinomial_treatment=Continuize.FirstAsBase)
s = pickle.dumps(c, -1)
c1 = pickle.loads(s)
self.assertIs(c1.multinomial_treatment, c.multinomial_treatment)

def test_randomize_pickling(self):
c = Randomize(rand_type=Randomize.RandomizeMetas)
s = pickle.dumps(c, -1)
c1 = pickle.loads(s)
self.assertIs(c1.rand_type, c.rand_type)

def test_scaling_pickling(self):
c = Scale(center=Scale.Median, scale=Scale.Span)
s = pickle.dumps(c, -1)
c1 = pickle.loads(s)
self.assertIs(c1.center, c.center)
self.assertIs(c1.scale, c.scale)

0 comments on commit 1a965b1

Please sign in to comment.