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

[ENH] Domain: Add copy method #2734

Merged
merged 1 commit into from
Nov 3, 2017
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
15 changes: 15 additions & 0 deletions Orange/data/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,21 @@ def _compute_col_indices(self, col_idx):
def checksum(self):
return hash(self)

def copy(self):
"""
Make a copy of the domain. New features are proxies of the old ones,
hence the new domain can be used anywhere the old domain was used.

Returns:
Domain: a copy of the domain.
"""
return Domain(
attributes=[a.make_proxy() for a in self.attributes],
class_vars=[a.make_proxy() for a in self.class_vars],
metas=[a.make_proxy() for a in self.metas],
source=self,
)

def __eq__(self, other):
if not isinstance(other, Domain):
return False
Expand Down
12 changes: 12 additions & 0 deletions Orange/tests/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,18 @@ def test_domain_conversion_is_fast_enough(self):

self.assertLessEqual(time() - start, 1)

def test_copy(self):
age.number_of_decimals = 5
attributes = (age, gender, income)

domain = Domain(attributes, [race], [ssn])

new_domain = domain.copy()
new_domain[age].number_of_decimals = 10

self.assertEqual(domain[age].number_of_decimals, 5)
self.assertEqual(new_domain[age].number_of_decimals, 10)


class TestDomainFilter(unittest.TestCase):
def setUp(self):
Expand Down