diff --git a/Orange/statistics/contingency.py b/Orange/statistics/contingency.py index 76135576ac4..da9a1c81983 100644 --- a/Orange/statistics/contingency.py +++ b/Orange/statistics/contingency.py @@ -179,9 +179,20 @@ def __reduce__(self): return ( _create_discrete, (Discrete, np.copy(self), self.col_variable, self.row_variable, - self.col_unknowns, self.row_unknowns) + self.col_unknowns, self.row_unknowns, self.unknowns) ) + def __array_finalize__(self, obj): + # defined in __new__, pylint: disable=attribute-defined-outside-init + """See http://docs.scipy.org/doc/numpy/user/basics.subclassing.html""" + if obj is None: + return + self.col_variable = getattr(obj, 'col_variable', None) + self.row_variable = getattr(obj, 'row_variable', None) + self.col_unknowns = getattr(obj, 'col_unknowns', None) + self.row_unknowns = getattr(obj, 'row_unknowns', None) + self.unknowns = getattr(obj, 'unknowns', None) + class Continuous: def __init__(self, dat, col_variable=None, row_variable=None, diff --git a/Orange/tests/test_contingency.py b/Orange/tests/test_contingency.py index f866de16899..9b3bf2a36fe 100644 --- a/Orange/tests/test_contingency.py +++ b/Orange/tests/test_contingency.py @@ -1,6 +1,6 @@ # Test methods with long descriptive names can omit docstrings # pylint: disable=missing-docstring - +import copy import unittest from unittest.mock import Mock @@ -71,6 +71,13 @@ def test_discrete_missing(self): np.testing.assert_almost_equal(cont.row_unknowns, [0, 0]) self.assertEqual(1, cont.unknowns) + def test_deepcopy(self): + cont = contingency.Discrete(self.zoo, 0) + dc = copy.deepcopy(cont) + self.assertEqual(dc, cont) + self.assertEqual(dc.col_variable, cont.col_variable) + self.assertEqual(dc.row_variable, cont.row_variable) + def test_array_with_unknowns(self): d = data.Table("zoo") d.Y[2] = float("nan")