6
6
:organization: Educational Testing Service
7
7
:date: 2022-09-05
8
8
"""
9
+ import unittest
10
+
9
11
import numpy as np
10
12
import pandas as pd
11
- from nose .tools import eq_ , raises
12
13
from numpy .testing import assert_array_equal
13
14
14
15
from factor_analyzer .confirmatory_factor_analyzer import (
21
22
THRESHOLD = 1.0
22
23
23
24
24
- def test_11_cfa (): # noqa: D103
25
-
26
- json_name_input = "test11"
27
- data_name_input = "test11"
28
-
29
- for check in check_cfa (json_name_input , data_name_input , rel_tol = 0.1 ):
30
- assert check >= THRESHOLD
31
-
32
-
33
- def test_12_cfa (): # noqa: D103
34
-
35
- json_name_input = "test12"
36
- data_name_input = "test12"
37
-
38
- for check in check_cfa (json_name_input , data_name_input , abs_tol = 0.05 ):
39
- assert check >= THRESHOLD
40
-
25
+ class TestConfirmatoryFactorAnalysis (unittest .TestCase ):
26
+ def test_11_cfa (self ): # noqa: D103
27
+ json_name_input = "test11"
28
+ data_name_input = "test11"
41
29
42
- def test_13_cfa (): # noqa: D103
30
+ for check in check_cfa (json_name_input , data_name_input , rel_tol = 0.1 ):
31
+ self .assertGreaterEqual (check , THRESHOLD )
43
32
44
- json_name_input = "test13"
45
- data_name_input = "test13"
33
+ def test_12_cfa (self ): # noqa: D103
34
+ json_name_input = "test12"
35
+ data_name_input = "test12"
46
36
47
- for check in check_cfa (
48
- json_name_input ,
49
- data_name_input ,
50
- index_col = 0 ,
51
- is_cov = True ,
52
- n_obs = 64 ,
53
- rel_tol = 0.1 ,
54
- ):
55
- assert check >= THRESHOLD
37
+ for check in check_cfa (json_name_input , data_name_input , abs_tol = 0.05 ):
38
+ self .assertGreaterEqual (check , THRESHOLD )
56
39
40
+ def test_13_cfa (self ): # noqa: D103
41
+ json_name_input = "test13"
42
+ data_name_input = "test13"
57
43
58
- def test_14_cfa (): # noqa: D103
44
+ for check in check_cfa (
45
+ json_name_input ,
46
+ data_name_input ,
47
+ index_col = 0 ,
48
+ is_cov = True ,
49
+ n_obs = 64 ,
50
+ rel_tol = 0.1 ,
51
+ ):
52
+ self .assertGreaterEqual (check , THRESHOLD )
59
53
60
- json_name_input = "test14"
61
- data_name_input = "test14"
54
+ def test_14_cfa (self ): # noqa: D103
55
+ json_name_input = "test14"
56
+ data_name_input = "test14"
62
57
63
- for check in check_cfa (json_name_input , data_name_input , index_col = 0 , rel_tol = 0.1 ):
64
- assert check >= THRESHOLD
58
+ for check in check_cfa (
59
+ json_name_input , data_name_input , index_col = 0 , rel_tol = 0.1
60
+ ):
61
+ self .assertGreaterEqual (check , THRESHOLD )
65
62
63
+ def test_14_cfa_no_model (self ): # noqa: D103
64
+ X = np .array ([[0 , 0 , 0 , 0 ], [0 , 0 , 0 , 0 ]])
66
65
67
- @raises (ValueError )
68
- def test_14_cfa_no_model (): # noqa: D103
66
+ with self .assertRaises (ValueError ):
67
+ cfa = ConfirmatoryFactorAnalyzer ("string_not_model" )
68
+ cfa .fit (X )
69
69
70
- X = np .array ([[0 , 0 , 0 , 0 ], [0 , 0 , 0 , 0 ]])
70
+ def test_14_cfa_bad_bounds (self ): # noqa: D103
71
+ X = np .array ([[0 , 0 , 0 , 0 ], [0 , 0 , 0 , 0 ]])
71
72
72
- cfa = ConfirmatoryFactorAnalyzer ("string_not_model" )
73
- cfa .fit (X )
73
+ with self .assertRaises (AssertionError ):
74
+ cfa = ConfirmatoryFactorAnalyzer (bounds = [(0 , 1 )])
75
+ cfa .fit (X )
74
76
77
+ def test_14_cfa_cov_with_no_obs (self ): # noqa: D103
78
+ with self .assertRaises (ValueError ):
79
+ ConfirmatoryFactorAnalyzer (is_cov_matrix = True )
75
80
76
- @raises (AssertionError )
77
- def test_14_cfa_bad_bounds (): # noqa: D103
78
81
79
- X = np .array ([[0 , 0 , 0 , 0 ], [0 , 0 , 0 , 0 ]])
80
-
81
- cfa = ConfirmatoryFactorAnalyzer (bounds = [(0 , 1 )])
82
- cfa .fit (X )
83
-
84
-
85
- @raises (ValueError )
86
- def test_14_cfa_cov_with_no_obs (): # noqa: D103
87
-
88
- ConfirmatoryFactorAnalyzer (is_cov_matrix = True )
89
-
90
-
91
- class TestModelSpecificationParser : # noqa: D101
82
+ class TestModelSpecificationParser (unittest .TestCase ):
92
83
def test_model_spec_str (self ): # noqa: D102
93
-
94
84
ms = ModelSpecification (np .array ([[0 , 0 , 0 ]]), 3 , 1 )
95
- assert str (ms ).startswith ("<ModelSpecification object at " )
85
+ self . assertTrue ( str (ms ).startswith ("<ModelSpecification object at " ) )
96
86
97
87
def test_model_spec_as_dict (self ): # noqa: D102
98
-
99
88
loadings = np .array ([[0 , 0 , 0 ]])
100
89
n_factors = 3
101
90
n_variables = 1
@@ -112,48 +101,52 @@ def test_model_spec_as_dict(self): # noqa: D102
112
101
if isinstance (value , np .ndarray ):
113
102
assert_array_equal (new_dict [key ], value )
114
103
else :
115
- eq_ (new_dict [key ], value )
104
+ self . assertEqual (new_dict [key ], value )
116
105
117
106
def test_model_spec_parser_from_dict_none (self ): # noqa: D102
118
107
X = np .array ([[0 , 0 , 0 ]])
119
108
ms = ModelSpecificationParser .parse_model_specification_from_dict (X , None )
120
- assert isinstance (ms , ModelSpecification )
121
- eq_ (ms .n_factors , 3 )
122
- eq_ (ms .n_variables , 3 )
109
+ self . assertTrue ( isinstance (ms , ModelSpecification ) )
110
+ self . assertEqual (ms .n_factors , 3 )
111
+ self . assertEqual (ms .n_variables , 3 )
123
112
assert_array_equal (ms .loadings , np .ones ((3 , 3 ), dtype = int ))
124
113
125
- @raises (ValueError )
126
114
def test_model_spec_parser_from_dict_error (self ): # noqa: D102
127
115
X = np .array ([[0 , 0 , 0 ]])
128
- ModelSpecificationParser .parse_model_specification_from_dict (X , "not_a_model" )
116
+ with self .assertRaises (ValueError ):
117
+ ModelSpecificationParser .parse_model_specification_from_dict (
118
+ X , "not_a_model"
119
+ )
129
120
130
121
def test_model_spec_parser_from_array_none (self ): # noqa: D102
131
122
X = np .array ([[0 , 0 , 0 ]])
132
123
ms = ModelSpecificationParser .parse_model_specification_from_array (X , None )
133
- assert isinstance (ms , ModelSpecification )
134
- eq_ (ms .n_factors , 3 )
135
- eq_ (ms .n_variables , 3 )
124
+ self . assertTrue ( isinstance (ms , ModelSpecification ) )
125
+ self . assertEqual (ms .n_factors , 3 )
126
+ self . assertEqual (ms .n_variables , 3 )
136
127
assert_array_equal (ms .loadings , np .ones ((3 , 3 ), dtype = int ))
137
128
138
129
def test_model_spec_parser_from_array (self ): # noqa: D102
139
130
X = np .array ([[0 , 0 , 0 ]])
140
131
spec = np .ones ((3 , 3 ), dtype = int )
141
132
ms = ModelSpecificationParser .parse_model_specification_from_array (X , spec )
142
- assert isinstance (ms , ModelSpecification )
143
- eq_ (ms .n_factors , 3 )
144
- eq_ (ms .n_variables , 3 )
133
+ self . assertTrue ( isinstance (ms , ModelSpecification ) )
134
+ self . assertEqual (ms .n_factors , 3 )
135
+ self . assertEqual (ms .n_variables , 3 )
145
136
assert_array_equal (ms .loadings , np .ones ((3 , 3 ), dtype = int ))
146
137
147
138
def test_model_spec_parser_from_frame (self ): # noqa: D102
148
139
X = np .array ([[0 , 0 , 0 ]])
149
140
spec = pd .DataFrame (np .ones ((3 , 3 ), dtype = int ))
150
141
ms = ModelSpecificationParser .parse_model_specification_from_array (X , spec )
151
- assert isinstance (ms , ModelSpecification )
152
- eq_ (ms .n_factors , 3 )
153
- eq_ (ms .n_variables , 3 )
142
+ self . assertTrue ( isinstance (ms , ModelSpecification ) )
143
+ self . assertEqual (ms .n_factors , 3 )
144
+ self . assertEqual (ms .n_variables , 3 )
154
145
assert_array_equal (ms .loadings , np .ones ((3 , 3 ), dtype = int ))
155
146
156
- @raises (ValueError )
157
147
def test_model_spec_parser_from_array_error (self ): # noqa: D102
158
148
X = np .array ([[0 , 0 , 0 ]])
159
- ModelSpecificationParser .parse_model_specification_from_array (X , "not_a_model" )
149
+ with self .assertRaises (ValueError ):
150
+ ModelSpecificationParser .parse_model_specification_from_array (
151
+ X , "not_a_model"
152
+ )
0 commit comments