Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deepcopy of Enumerate transform #290

Merged
merged 1 commit into from
Oct 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/orion/core/worker/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ def __init__(self, categories):
self._map = numpy.vectorize(lambda x: map_dict[x], otypes='i')
self._imap = numpy.vectorize(lambda x: categories[x], otypes=[numpy.object])

def __deepcopy__(self, memo):
"""Make a deepcopy"""
return type(self)(self.categories)

def transform(self, point):
"""Return integers corresponding uniquely to the categories in `point`.

Expand Down
37 changes: 37 additions & 0 deletions tests/unittests/core/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
class TestIdentity(object):
"""Test subclasses of `Identity` transformation."""

def test_deepcopy(self):
"""Verify that the transformation object can be copied"""
t = Identity()
t.transform([2])
copy.deepcopy(t)

def test_domain_and_target_type(self):
"""Check if attribute-like `domain_type` and `target_type` do
what's expected.
Expand Down Expand Up @@ -53,6 +59,12 @@ def test_repr_format(self):
class TestReverse(object):
"""Test subclasses of `Reverse` transformation."""

def test_deepcopy(self):
"""Verify that the transformation object can be copied"""
t = Reverse(Quantize())
t.transform([2])
copy.deepcopy(t)

def test_domain_and_target_type(self):
"""Check if attribute-like `domain_type` and `target_type` do
what's expected.
Expand Down Expand Up @@ -94,6 +106,12 @@ def test_repr_format(self):
class TestCompose(object):
"""Test subclasses of `Compose` transformation."""

def test_deepcopy(self):
"""Verify that the transformation object can be copied"""
t = Compose([Enumerate([2, 'asfa', 'ipsi']), OneHotEncode(3)], 'categorical')
t.transform([2])
copy.deepcopy(t)

def test_domain_and_target_type(self):
"""Check if attribute-like `domain_type` and `target_type` do
what's expected.
Expand Down Expand Up @@ -189,6 +207,12 @@ def test_repr_format(self):
class TestQuantize(object):
"""Test subclasses of `Quantize` transformation."""

def test_deepcopy(self):
"""Verify that the transformation object can be copied"""
t = Quantize()
t.transform([2])
copy.deepcopy(t)

def test_domain_and_target_type(self):
"""Check if attribute-like `domain_type` and `target_type` do
what's expected.
Expand Down Expand Up @@ -225,6 +249,13 @@ def test_repr_format(self):
class TestEnumerate(object):
"""Test subclasses of `Enumerate` transformation."""

def test_deepcopy(self):
"""Verify that the transformation object can be copied"""
t = Enumerate([2, 'asfa', 'ipsi'])
# Copy won't fail if vectorized function is not called at least once.
t.transform([2])
copy.deepcopy(t)

def test_domain_and_target_type(self):
"""Check if attribute-like `domain_type` and `target_type` do
what's expected.
Expand Down Expand Up @@ -283,6 +314,12 @@ def test_repr_format(self):
class TestOneHotEncode(object):
"""Test subclasses of `OneHotEncode` transformation."""

def test_deepcopy(self):
"""Verify that the transformation object can be copied"""
t = OneHotEncode(3)
t.transform([2])
copy.deepcopy(t)

def test_domain_and_target_type(self):
"""Check if attribute-like `domain_type` and `target_type` do
what's expected.
Expand Down