66
77import numpy as np
88import scipy .sparse as sp
9+ from scipy .sparse import csr_matrix , csc_matrix
910
1011from Orange .data import DiscreteVariable , Table , Domain
1112from Orange .statistics import contingency
@@ -25,6 +26,7 @@ class TestDiscrete(unittest.TestCase):
2526 @classmethod
2627 def setUpClass (cls ):
2728 cls .zoo = data .Table ("zoo" )
29+ cls .test9 = data .Table (test_filename ("datasets/test9.tab" ))
2830
2931 def test_discrete (self ):
3032 cont = contingency .Discrete (self .zoo , 0 )
@@ -153,6 +155,16 @@ def test_continuous_missing(self):
153155 0. , 0. , 0. , 0. , 0. , 0. , 0. ])
154156 self .assertEqual (cont .unknowns , 1 )
155157
158+ # this one was failing before since the issue in _contingecy.pyx
159+ d .Y [:50 ] = np .zeros (50 ) * float ("nan" )
160+ cont = contingency .Continuous (d , "sepal width" )
161+ np .testing .assert_almost_equal (cont .col_unknowns , [0 , 0 , 0 ])
162+ np .testing .assert_almost_equal (
163+ cont .row_unknowns ,
164+ [0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 1. , 5. , 5. , 5. , 2. , 9. , 6. , 2. ,
165+ 3. , 4. , 2. , 1. , 1. , 1. , 1. ])
166+ self .assertEqual (cont .unknowns , 1 )
167+
156168 def test_mixedtype_metas (self ):
157169 import Orange
158170 zoo = Orange .data .Table ("zoo" )
@@ -286,12 +298,35 @@ def test_get_contingencies(self):
286298 assert_dist_equal (cont [2 ], [1 , 0 , 0 ])
287299
288300 def test_compute_contingency_metas (self ):
289- d = data .Table (test_filename ("datasets/test9.tab" ))
290- var1 , var2 = d .domain [- 2 ], d .domain [- 4 ]
291- cont = d ._compute_contingency ([var1 ], var2 )[0 ][0 ]
301+ var1 , var2 = self .test9 .domain [- 2 ], self .test9 .domain [- 4 ]
302+ cont = contingency .Discrete (self .test9 , var1 , var2 )
292303 assert_dist_equal (cont , [[3 , 0 , 0 ], [0 , 2 , 0 ],
293304 [0 , 0 , 2 ], [0 , 1 , 0 ]])
294305
306+ def test_compute_contingency_row_attribute_sparse (self ):
307+ """
308+ Testing with sparse row variable since currently we do not test the
309+ situation when a row variable is sparse.
310+ """
311+ d = self .test9
312+ # make X sparse
313+ d .X = csr_matrix (d .X )
314+ var1 , var2 = d .domain [0 ], d .domain [1 ]
315+ cont = contingency .Discrete (d , var1 , var2 )
316+ assert_dist_equal (cont , [[1 , 0 ], [1 , 0 ], [1 , 0 ], [1 , 0 ],
317+ [0 , 1 ], [0 , 1 ], [0 , 1 ], [0 , 1 ]])
318+ cont = contingency .Discrete (d , var2 , var1 )
319+ assert_dist_equal (cont , [[1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 ],
320+ [0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 ]])
321+
322+ d .X = csc_matrix (d .X )
323+ cont = contingency .Discrete (d , var1 , var2 )
324+ assert_dist_equal (cont , [[1 , 0 ], [1 , 0 ], [1 , 0 ], [1 , 0 ],
325+ [0 , 1 ], [0 , 1 ], [0 , 1 ], [0 , 1 ]])
326+ cont = contingency .Discrete (d , var2 , var1 )
327+ assert_dist_equal (cont , [[1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 ],
328+ [0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 ]])
329+
295330 def test_compute_contingency_invalid (self ):
296331 rstate = np .random .RandomState (0xFFFF )
297332 X = data .ContinuousVariable ("X" )
0 commit comments