11import mock
22import json
3- from . import fixtures as fx
3+ from mip_helper import testing as t
4+
45from statistics import intermediate_stats , aggregate_stats
56
67
78@mock .patch ('statistics.io_helper.fetch_data' )
89@mock .patch ('statistics.io_helper.save_results' )
910def test_intermediate_stats_real (mock_save_results , mock_fetch_data ):
1011 # input data with some null values
11- data = fx .inputs_regression (include_categorical = True , add_null = True )
12- data ['data' ]['dependent' ][0 ]['series' ][0 ] = None
13- data ['data' ]['independent' ][1 ]['series' ][0 ] = None
12+ data = t .inputs_regression (include_nominal = True , add_null = True )
1413
1514 mock_fetch_data .return_value = data
1615
1716 intermediate_stats ()
1817 results = json .loads (mock_save_results .call_args [0 ][0 ])
19- assert len (results ['data' ]) == 16
20- assert results ['data' ][:2 ] == [
18+ assert len (results ['data' ]) == 12
19+ res = sorted (results ['data' ], key = lambda d : (d ['index' ], tuple (d ['group' ])))
20+ assert t .round_dict (res [:2 ]) == [
2121 {
2222 'index' : 'agegroup' ,
23- 'label' : 'Age group ' ,
23+ 'label' : 'Age Group ' ,
2424 'type' : 'polynominal' ,
2525 'group' : ['-50y' ],
26- 'group_variables' : ['Age group ' ],
26+ 'group_variables' : ['Age Group ' ],
2727 'count' : 3 ,
2828 'unique' : 1 ,
2929 'top' : '-50y' ,
3030 'frequency' : {
3131 '-50y' : 3 ,
32- '59y-' : 0 ,
3332 '50-59y' : 0
3433 },
3534 'null_count' : 0
3635 }, {
37- 'index' : 'iq' ,
38- 'label' : 'IQ' ,
39- 'type' : 'real' ,
40- 'group' : ['-50y' ],
41- 'group_variables' : ['Age group' ],
42- 'count' : 3 ,
43- 'mean' : 73.8895774452 ,
44- 'std' : 0.1412026822 ,
45- 'min' : 73.7897320711 ,
46- '25%' : 73.8396547582 ,
47- '50%' : 73.8895774452 ,
48- '75%' : 73.9395001323 ,
49- 'max' : 73.9894228193 ,
50- 'EX^2' : 5459.6796241289 ,
51- 'null_count' : 1
36+ 'index' : 'agegroup' ,
37+ 'label' : 'Age Group' ,
38+ 'type' : 'polynominal' ,
39+ 'group' : ['50-59y' ],
40+ 'group_variables' : ['Age Group' ],
41+ 'count' : 2 ,
42+ 'unique' : 1 ,
43+ 'top' : '50-59y' ,
44+ 'frequency' : {
45+ '50-59y' : 2 ,
46+ '-50y' : 0
47+ },
48+ 'null_count' : 0
5249 }
5350 ]
5451
5552
5653@mock .patch ('statistics.io_helper.fetch_data' )
5754@mock .patch ('statistics.io_helper.save_results' )
5855def test_intermediate_stats_nominal (mock_save_results , mock_fetch_data ):
59- # input data with some null values
60- data = fx .inputs_classification (include_categorical = True )
61- # data['data']['dependent'][0]['series'][0] = None
62- # data['data']['independent'][1]['series'][0] = None
56+ data = t .inputs_classification (include_nominal = True )
6357
6458 mock_fetch_data .return_value = data
6559
6660 intermediate_stats ()
6761 results = json .loads (mock_save_results .call_args [0 ][0 ])
68- assert len (results ['data' ]) == 16
69- assert results ['data' ][:2 ] == [
62+ assert len (results ['data' ]) == 12
63+ res = sorted (results ['data' ], key = lambda d : (d ['index' ], tuple (d ['group' ])))
64+ assert t .round_dict (res [:2 ]) == [
7065 {
7166 'index' : 'adnicategory' ,
7267 'label' : 'ADNI category' ,
7368 'type' : 'polynominal' ,
7469 'group' : ['-50y' ],
75- 'group_variables' : ['Age group ' ],
70+ 'group_variables' : ['Age Group ' ],
7671 'count' : 3 ,
7772 'unique' : 3 ,
7873 'top' : 'Other' ,
@@ -83,18 +78,18 @@ def test_intermediate_stats_nominal(mock_save_results, mock_fetch_data):
8378 },
8479 'null_count' : 0
8580 }, {
86- 'index' : 'agegroup ' ,
87- 'label' : 'Age group ' ,
81+ 'index' : 'adnicategory ' ,
82+ 'label' : 'ADNI category ' ,
8883 'type' : 'polynominal' ,
89- 'group' : ['-50y ' ],
90- 'group_variables' : ['Age group ' ],
91- 'count' : 3 ,
84+ 'group' : ['50-59y ' ],
85+ 'group_variables' : ['Age Group ' ],
86+ 'count' : 2 ,
9287 'unique' : 1 ,
93- 'top' : '-50y ' ,
88+ 'top' : 'Other ' ,
9489 'frequency' : {
95- '-50y ' : 3 ,
96- '59y- ' : 0 ,
97- '50-59y ' : 0
90+ 'Other ' : 2 ,
91+ 'CN ' : 0 ,
92+ 'AD ' : 0
9893 },
9994 'null_count' : 0
10095 }
@@ -103,17 +98,44 @@ def test_intermediate_stats_nominal(mock_save_results, mock_fetch_data):
10398
10499@mock .patch ('statistics.io_helper.fetch_data' )
105100@mock .patch ('statistics.io_helper.save_results' )
106- @mock .patch ('statistics.io_helper.save_error' )
107- def test_intermediate_stats_empty (mock_save_error , mock_save_results , mock_fetch_data ):
108- data = fx .inputs_regression (include_categorical = True , add_null = True )
109- data ['data' ]['dependent' ][0 ]['series' ] = []
101+ def test_intermediate_stats_empty (mock_save_results , mock_fetch_data ):
102+ data = t .inputs_regression (include_nominal = True , limit_to = 0 )
110103 mock_fetch_data .return_value = data
111104
112- with mock .patch ('sys.exit' ):
113- intermediate_stats ()
105+ intermediate_stats ()
114106
115- error = mock_save_error .call_args [0 ]
116- assert error == ('Dependent variable has no values, check your SQL query.' ,)
107+ results = json .loads (mock_save_results .call_args [0 ][0 ])
108+ assert len (results ['data' ]) == 12
109+ res = sorted (results ['data' ], key = lambda d : (d ['index' ], tuple (d ['group' ])))
110+ assert t .round_dict (res [:2 ]) == [
111+ {
112+ 'index' : 'agegroup' ,
113+ 'label' : 'Age Group' ,
114+ 'type' : 'polynominal' ,
115+ 'group' : ['-50y' ],
116+ 'group_variables' : ['Age Group' ],
117+ 'count' : 0 ,
118+ 'unique' : 0 ,
119+ 'frequency' : {
120+ '50-59y' : 0 ,
121+ '-50y' : 0
122+ },
123+ 'null_count' : 0
124+ }, {
125+ 'index' : 'agegroup' ,
126+ 'label' : 'Age Group' ,
127+ 'type' : 'polynominal' ,
128+ 'group' : ['50-59y' ],
129+ 'group_variables' : ['Age Group' ],
130+ 'count' : 0 ,
131+ 'unique' : 0 ,
132+ 'frequency' : {
133+ '50-59y' : 0 ,
134+ '-50y' : 0
135+ },
136+ 'null_count' : 0
137+ }
138+ ]
117139
118140
119141def intermediate_data_1 ():
0 commit comments