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] Table - copy attributes when creating table from table #6189

Merged
merged 1 commit into from
Nov 4, 2022
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
7 changes: 4 additions & 3 deletions Orange/data/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import zlib
from collections.abc import Iterable, Sequence, Sized
from contextlib import contextmanager
from copy import deepcopy
from functools import reduce
from itertools import chain
from numbers import Real, Integral
Expand Down Expand Up @@ -821,7 +822,7 @@ def from_table(cls, domain, source, row_indices=...):
self.ids = source.ids[row_indices]
else:
cls._init_ids(self)
self.attributes = getattr(source, 'attributes', {})
self.attributes = deepcopy(getattr(source, 'attributes', {}))
_idcache_save(_thread_local.conversion_cache, (domain, source), self)
return self
finally:
Expand Down Expand Up @@ -879,7 +880,7 @@ def from_table_rows(cls, source, row_indices):
self.W = source.W[row_indices]
self.name = getattr(source, 'name', '')
self.ids = np.array(source.ids[row_indices])
self.attributes = getattr(source, 'attributes', {})
self.attributes = deepcopy(getattr(source, 'attributes', {}))
return self

@classmethod
Expand Down Expand Up @@ -2284,7 +2285,7 @@ def guessed_var(i, var_name):
self.domain = Domain(attributes, class_vars, metas)
progress_callback(0.9)
cls._init_ids(self)
self.attributes = table.attributes.copy()
self.attributes = deepcopy(table.attributes)
self.attributes["old_domain"] = table.domain
progress_callback(1)
return self
Expand Down
27 changes: 25 additions & 2 deletions Orange/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import scipy.sparse as sp

from Orange import data
from Orange.data import (filter, Unknown, Variable, Table, DiscreteVariable,
from Orange.data import (filter, Unknown, Table, DiscreteVariable,
ContinuousVariable, Domain, StringVariable)
from Orange.data.util import SharedComputeValue
from Orange.tests import test_dirname
Expand Down Expand Up @@ -1165,7 +1165,8 @@ def test_attributes(self):
table2 = table[:4]
self.assertEqual(table2.attributes[1], "test")
table2.attributes[1] = "modified"
self.assertEqual(table.attributes[1], "modified")
self.assertEqual(table.attributes[1], "test")
self.assertEqual(table2.attributes[1], "modified")

# TODO Test conjunctions and disjunctions of conditions

Expand Down Expand Up @@ -1893,6 +1894,28 @@ def assert_table_with_filter_matches(
np.testing.assert_almost_equal(new_table.metas, magic[rows, mcols])
np.testing.assert_almost_equal(new_table.W, old_table.W[rows])

def test_attributes_copied(self):
"""Table created from table attributes dict copied"""
self.table.attributes = {"A": "Test", "B": []}

# from_table
new_table = self.table.from_table(self.table.domain, self.table)
self.assertDictEqual(new_table.attributes, {"A": "Test", "B": []})
new_table.attributes["A"] = "Changed"
new_table.attributes["B"].append(1)
self.assertDictEqual(new_table.attributes, {"A": "Changed", "B": [1]})
# attributes dict of old table not be changed since new dist is a copy
self.assertDictEqual(self.table.attributes, {"A": "Test", "B": []})

# from_table_rows
new_table = self.table.from_table_rows(self.table, [1, 2])
self.assertDictEqual(new_table.attributes, {"A": "Test", "B": []})
new_table.attributes["A"] = "Changed"
new_table.attributes["B"].append(1)
self.assertDictEqual(new_table.attributes, {"A": "Changed", "B": [1]})
# attributes dict of old table not be changed since new dist is a copy
self.assertDictEqual(self.table.attributes, {"A": "Test", "B": []})


def isspecial(s):
return isinstance(s, slice) or s is Ellipsis
Expand Down