Skip to content

Commit

Permalink
Contingency.Discrete: Fix deepcopy and pickle
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Jul 27, 2021
1 parent 525e599 commit edc12b3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
13 changes: 12 additions & 1 deletion Orange/statistics/contingency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion Orange/tests/test_contingency.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit edc12b3

Please sign in to comment.