Skip to content

Commit

Permalink
Domain: throw exception if all var names are not unique
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrejaKovacic committed May 16, 2020
1 parent b0bfdaf commit 2c60ff3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
6 changes: 6 additions & 0 deletions Orange/data/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ def __init__(self, attributes, class_vars=None, metas=None, source=None):
raise TypeError(
"descriptors must be instances of Variable, "
"not '%s'" % type(var).__name__)

names = [var.name for var in chain(attributes, class_vars, metas)]
if len(names) != len(set(names)):
print(names)
raise Exception('All the variables in the domain should have'
' unique names.')

# Store everything
self.attributes = tuple(attributes)
Expand Down
24 changes: 12 additions & 12 deletions Orange/tests/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ def test_init_source_class(self):
def test_init_metas(self):
attributes = (age, gender, income)
metas = (ssn, race)
d = Domain(attributes, race, metas=metas)
self.assertEqual(d.variables, attributes + (race, ))
d = Domain(attributes, education, metas=metas)
self.assertEqual(d.variables, attributes + (education, ))
self.assertEqual(d.attributes, attributes)
self.assertEqual(d.class_var, race)
self.assertEqual(d.class_vars, (race, ))
self.assertEqual(d.class_var, education)
self.assertEqual(d.class_vars, (education, ))
self.assertEqual(d.metas, metas)

def test_from_numpy_names(self):
Expand Down Expand Up @@ -230,7 +230,7 @@ def test_index(self):
self.assertEqual(d.index(idx), var)

def test_get_item_slices(self):
d = Domain((age, gender, income, race), metas=(ssn, race))
d = Domain((age, gender, income, race), metas=(ssn, education))
self.assertEqual(d[:2], (age, gender))
self.assertEqual(d[1:3], (gender, income))
self.assertEqual(d[2:], (income, race))
Expand Down Expand Up @@ -377,19 +377,19 @@ def test_has_time(self):

def test_get_conversion(self):
compute_value = lambda: 42
new_income = income.copy(compute_value=compute_value)
new_income = income.copy(compute_value=compute_value, name='new_income')

d = Domain((age, gender, income), metas=(ssn, race))
e = Domain((gender, race), None, metas=(age, gender, ssn))
f = Domain((gender,), (race, income), metas=(age, income, ssn))
e = Domain((gender, race), None, metas=(age, income, ssn))
f = Domain((gender,), (race, income), metas=(age, incomeA, ssn))
g = Domain((), metas=(age, gender, ssn))
h = Domain((gender,), (race, new_income), metas=(age, new_income, ssn))
h = Domain((gender,), (race, income), metas=(age, new_income, ssn))

for conver, domain, attr, class_vars, metas in (
(d, e, [1, -2], [], [0, 1, -1]),
(d, f, [1], [-2, 2], [0, 2, -1]),
(d, e, [1, -2], [], [0, 2, -1]),
(d, f, [1], [-2, 2], [0, None, -1]),
(f, g, [], [], [-1, 0, -3]),
(g, h, [-2], [None, compute_value], [-1, compute_value, -3])):
(g, h, [-2], [None, None], [-1, compute_value, -3])):
to_domain = DomainConversion(conver, domain)
self.assertIs(to_domain.source, conver)
self.assertEqual(to_domain.attributes, attr)
Expand Down

0 comments on commit 2c60ff3

Please sign in to comment.