1
|
|
|
from __future__ import annotations |
2
|
|
|
|
3
|
|
|
import unittest |
4
|
|
|
|
5
|
|
|
import numpy as np |
6
|
|
|
import pandas as pd |
7
|
|
|
|
8
|
|
|
from klib.describe import corr_mat |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class Test_corr_mat(unittest.TestCase): |
12
|
|
|
@classmethod |
13
|
|
|
def setUpClass(cls): |
14
|
|
|
cls.data_corr_df = pd.DataFrame( |
15
|
|
|
[[1, 0, 3, 4, "a"], [3, 4, 5, 6, "b"], [5, 4, 2, 1, "c"]], |
16
|
|
|
columns=["Col1", "Col2", "Col3", "Col4", "Col5"], |
17
|
|
|
) |
18
|
|
|
|
19
|
|
|
cls.data_corr_list = [1, 2, -3] |
20
|
|
|
cls.data_corr_target_series = pd.Series([1, 2, -3], name="Target Series") |
21
|
|
|
cls.data_corr_target_array = np.array([1, 2, -3]) |
22
|
|
|
cls.data_corr_target_list = [1, 2, -3] |
23
|
|
|
|
24
|
|
|
def test_output_type(self): |
25
|
|
|
# Test conversion from pd.io.formats.style.Styler to pd.core.frame.DataFrame |
26
|
|
|
assert isinstance( |
27
|
|
|
type(corr_mat(self.data_corr_df)), |
28
|
|
|
type(pd.io.formats.style.Styler), |
29
|
|
|
) |
30
|
|
|
assert isinstance( |
31
|
|
|
type(corr_mat(self.data_corr_list)), |
32
|
|
|
type(pd.io.formats.style.Styler), |
33
|
|
|
) |
34
|
|
|
assert isinstance( |
35
|
|
|
type(corr_mat(self.data_corr_df, target="Col1")), |
36
|
|
|
type(pd.io.formats.style.Styler), |
37
|
|
|
) |
38
|
|
|
assert isinstance( |
39
|
|
|
type(corr_mat(self.data_corr_df, target=self.data_corr_target_series)), |
40
|
|
|
type(pd.io.formats.style.Styler), |
41
|
|
|
) |
42
|
|
|
assert isinstance( |
43
|
|
|
type(corr_mat(self.data_corr_df, target=self.data_corr_target_array)), |
44
|
|
|
type(pd.io.formats.style.Styler), |
45
|
|
|
) |
46
|
|
|
assert isinstance( |
47
|
|
|
type(corr_mat(self.data_corr_df, target=self.data_corr_target_list)), |
48
|
|
|
type(pd.io.formats.style.Styler), |
49
|
|
|
) |
50
|
|
|
|
51
|
|
|
assert isinstance( |
52
|
|
|
type(corr_mat(self.data_corr_df, colored=False)), |
53
|
|
|
type(pd.DataFrame), |
54
|
|
|
) |
55
|
|
|
assert isinstance( |
56
|
|
|
type(corr_mat(self.data_corr_list, colored=False)), |
57
|
|
|
type(pd.DataFrame), |
58
|
|
|
) |
59
|
|
|
assert isinstance( |
60
|
|
|
type(corr_mat(self.data_corr_df, target="Col1", colored=False)), |
61
|
|
|
type(pd.DataFrame), |
62
|
|
|
) |
63
|
|
|
assert isinstance( |
64
|
|
|
type( |
65
|
|
|
corr_mat( |
66
|
|
|
self.data_corr_df, |
67
|
|
|
target=self.data_corr_target_series, |
68
|
|
|
colored=False, |
69
|
|
|
), |
70
|
|
|
), |
71
|
|
|
type(pd.DataFrame), |
72
|
|
|
) |
73
|
|
|
assert isinstance( |
74
|
|
|
type( |
75
|
|
|
corr_mat( |
76
|
|
|
self.data_corr_df, |
77
|
|
|
target=self.data_corr_target_array, |
78
|
|
|
colored=False, |
79
|
|
|
), |
80
|
|
|
), |
81
|
|
|
type(pd.DataFrame), |
82
|
|
|
) |
83
|
|
|
assert isinstance( |
84
|
|
|
type( |
85
|
|
|
corr_mat( |
86
|
|
|
self.data_corr_df, |
87
|
|
|
target=self.data_corr_target_list, |
88
|
|
|
colored=False, |
89
|
|
|
), |
90
|
|
|
), |
91
|
|
|
type(pd.DataFrame), |
92
|
|
|
) |
93
|
|
|
|
94
|
|
|
def test_output_shape(self): |
95
|
|
|
# Test for output dimensions |
96
|
|
|
assert ( |
97
|
|
|
corr_mat(self.data_corr_df).data.shape[0] == corr_mat(self.data_corr_df).data.shape[1] |
98
|
|
|
) |
99
|
|
|
assert ( |
100
|
|
|
corr_mat(self.data_corr_list).data.shape[0] |
101
|
|
|
== corr_mat(self.data_corr_list).data.shape[1] |
102
|
|
|
) |
103
|
|
|
assert corr_mat(self.data_corr_df, target="Col1", colored=False).shape == (3, 1) |
104
|
|
|
assert corr_mat( |
105
|
|
|
self.data_corr_df, |
106
|
|
|
target=self.data_corr_target_series, |
107
|
|
|
colored=False, |
108
|
|
|
).shape == (4, 1) |
109
|
|
|
assert corr_mat( |
110
|
|
|
self.data_corr_df, |
111
|
|
|
target=self.data_corr_target_array, |
112
|
|
|
colored=False, |
113
|
|
|
).shape == (4, 1) |
114
|
|
|
assert corr_mat( |
115
|
|
|
self.data_corr_df, |
116
|
|
|
target=self.data_corr_target_list, |
117
|
|
|
colored=False, |
118
|
|
|
).shape == (4, 1) |
119
|
|
|
|