33import cmapPy .pandasGEXpress .setup_GCToo_logger as setup_logger
44import cmapPy .math .fast_corr as fast_corr
55import numpy
6+ import pandas
67
78
89logger = logging .getLogger (setup_logger .LOGGER_NAME )
@@ -29,77 +30,25 @@ def test_fast_corr_just_x(self):
2930 logger .debug ("*************happy path just x" )
3031 x , _ = TestFastCorr .build_standard_x_y ()
3132
32- ex = numpy .corrcoef (x )
33+ ex = numpy .corrcoef (x , rowvar = False )
3334 logger .debug ("expected ex: {}" .format (ex ))
3435
35- r = fast_corr .fast_corr (x , do_print_trace = True )
36+ r = fast_corr .fast_corr (x )
3637 logger .debug ("r: {}" .format (r ))
3738
3839 self .assertTrue (numpy .allclose (ex , r ))
3940
40- #happy path just x, transposed
41- ex = numpy .corrcoef (x .T )
42- logger .debug ("happy path just x, transposed, expected ex: {}" .format (ex ))
43- r = fast_corr .fast_corr (x .T , do_print_trace = True )
44- logger .debug ("r: {}" .format (r ))
45- self .assertTrue (numpy .allclose (ex , r ))
46-
47- def test_fast_corr_just_x_different_axis (self ):
48- logger .debug ("*************happy path just x, use different axis" )
49- x , _ = TestFastCorr .build_standard_x_y ()
50-
51- ex = numpy .corrcoef (x , rowvar = False )
52- logger .debug ("happy path just x, use different axis, expected ex: {}" .format (ex ))
53- r = fast_corr .fast_corr (x , axis = 1 , do_print_trace = True )
54- logger .debug ("r: {}" .format (r ))
55- self .assertTrue (numpy .allclose (ex , r ))
56-
57- #happy path just x, use different axis, transpose
58- ex = numpy .corrcoef (x )
59- logger .debug ("happy path just x, use different axis, transpose, expected ex: {}" .format (ex ))
60- r = fast_corr .fast_corr (x .T , axis = 1 , do_print_trace = True )
41+ #happy path just x, other direction
42+ ex = numpy .corrcoef (x , rowvar = True )
43+ logger .debug ("happy path just x, other direction, expected ex: {}" .format (ex ))
44+ r = fast_corr .fast_corr (x .T )
6145 logger .debug ("r: {}" .format (r ))
6246 self .assertTrue (numpy .allclose (ex , r ))
6347
6448 def test_fast_corr_x_and_y (self ):
6549 logger .debug ("*************happy path x and y" )
6650 x , y = TestFastCorr .build_standard_x_y ()
6751
68- combined = numpy .vstack ([x , y ])
69- logger .debug ("combined: {}" .format (combined ))
70- logger .debug ("combined.shape: {}" .format (combined .shape ))
71-
72- off_diag_ind = combined .shape [0 ] / 2
73-
74- raw_ex = numpy .corrcoef (combined )
75- logger .debug ("raw expected produced from numpy.cov on full combined - raw_ex: {}" .format (raw_ex ))
76- ex = raw_ex [:off_diag_ind , off_diag_ind :]
77- logger .debug ("expected ex: {}" .format (ex ))
78-
79- r = fast_corr .fast_corr (x , y , do_print_trace = True )
80- logger .debug ("r: {}" .format (r ))
81- self .assertTrue (numpy .allclose (ex , r ))
82-
83- #happy path x and y, transposed
84- combined = numpy .vstack ([x .T , y .T ])
85- logger .debug ("*************happy path x and y, transposed - combined: {}" .format (combined ))
86- logger .debug ("combined.shape: {}" .format (combined .shape ))
87-
88- off_diag_ind = combined .shape [0 ] / 2
89-
90- raw_ex = numpy .corrcoef (combined )
91- logger .debug ("raw expected produced from numpy.cov on full combined - raw_ex: {}" .format (raw_ex ))
92- ex = raw_ex [:off_diag_ind , off_diag_ind :]
93- logger .debug ("expected ex: {}" .format (ex ))
94-
95- r = fast_corr .fast_corr (x .T , y .T , do_print_trace = True )
96- logger .debug ("r: {}" .format (r ))
97- self .assertTrue (numpy .allclose (ex , r ))
98-
99- def test_fast_corr_x_and_y_different_axis (self ):
100- logger .debug ("*************happy path, x and y using different axis" )
101- x , y = TestFastCorr .build_standard_x_y ()
102-
10352 combined = numpy .hstack ([x , y ])
10453 logger .debug ("combined: {}" .format (combined ))
10554 logger .debug ("combined.shape: {}" .format (combined .shape ))
@@ -111,16 +60,13 @@ def test_fast_corr_x_and_y_different_axis(self):
11160 ex = raw_ex [:off_diag_ind , off_diag_ind :]
11261 logger .debug ("expected ex: {}" .format (ex ))
11362
114- r = fast_corr .fast_corr (x , y , axis = 1 , do_print_trace = True )
63+ r = fast_corr .fast_corr (x , y )
11564 logger .debug ("r: {}" .format (r ))
11665 self .assertTrue (numpy .allclose (ex , r ))
11766
118- def test_fast_corr_x_and_y_different_axis_transpose (self ):
119- logger .debug ("*************happy path x and y different axis, transposed" )
120- x , y = TestFastCorr .build_standard_x_y ()
121-
67+ #happy path x and y, other direction
12268 combined = numpy .hstack ([x .T , y .T ])
123- logger .debug ("combined: {}" .format (combined ))
69+ logger .debug ("*************happy path x and y, other direction - combined: {}" .format (combined ))
12470 logger .debug ("combined.shape: {}" .format (combined .shape ))
12571
12672 off_diag_ind = combined .shape [1 ] / 2
@@ -130,7 +76,7 @@ def test_fast_corr_x_and_y_different_axis_transpose(self):
13076 ex = raw_ex [:off_diag_ind , off_diag_ind :]
13177 logger .debug ("expected ex: {}" .format (ex ))
13278
133- r = fast_corr .fast_corr (x .T , y .T , axis = 1 , do_print_trace = True )
79+ r = fast_corr .fast_corr (x .T , y .T )
13480 logger .debug ("r: {}" .format (r ))
13581 self .assertTrue (numpy .allclose (ex , r ))
13682
@@ -153,56 +99,53 @@ def test_fast_corr_x_and_y_different_shapes(self):
15399 logger .debug ("expected ex: {}" .format (ex ))
154100 logger .debug ("ex.shape: {}" .format (ex .shape ))
155101
156- r = fast_corr .fast_corr (x , y , axis = 1 , do_print_trace = True )
102+ r = fast_corr .fast_corr (x , y )
157103 logger .debug ("r: {}" .format (r ))
158104 self .assertTrue (numpy .allclose (ex , r ))
159105
160106 def test_fast_corr_functional (self ):
161107 logger .debug ("*************happy path functional test using randomly generated matrices" )
162108
163- axes = numpy .random .choice ([0 , 1 ], size = num_iterations_functional_tests )
164-
165- for cur_axis in axes :
166- logger .debug ("*******cur_axis: {}" .format (cur_axis ))
167-
109+ for i in xrange (num_iterations_functional_tests ):
168110 #the dimension containing the observations must have at least size 2
169- x_shape = [numpy .random .randint (1 , max_dimension_functional_tests ),
170- numpy .random .randint (2 , max_dimension_functional_tests )]
171- if cur_axis == 1 :
172- x_shape .reverse ()
111+ x_shape = [numpy .random .randint (2 , max_dimension_functional_tests ),
112+ numpy .random .randint (1 , max_dimension_functional_tests )]
173113 logger .debug ("x_shape: {}" .format (x_shape ))
174114
175115 x = numpy .random .rand (x_shape [0 ], x_shape [1 ]) * numpy .random .randint (1 , multiplier_max_functional_tests , size = 1 )
176116 logger .debug ("x:\n {}" .format (x ))
177117
178118 y_other_shape = numpy .random .randint (1 , max_dimension_functional_tests , size = 1 )[0 ]
179- y_shape = (y_other_shape , x_shape [ 1 ]) if cur_axis == 0 else ( x_shape [0 ], y_other_shape )
119+ y_shape = (x_shape [0 ], y_other_shape )
180120 logger .debug ("y_shape: {}" .format (y_shape ))
181121 y = numpy .random .rand (y_shape [0 ], y_shape [1 ]) * numpy .random .randint (1 , multiplier_max_functional_tests , size = 1 )
182122 logger .debug ("y:\n {}" .format (y ))
183123
184- if cur_axis == 0 :
185- row_var = True
186- combined = numpy .vstack ([x , y ])
187- off_diag_ind = (x .shape [0 ], - y .shape [0 ])
188- else :
189- row_var = False
190- combined = numpy .hstack ([x , y ])
191- off_diag_ind = (x .shape [1 ], - y .shape [1 ])
124+ combined = numpy .hstack ([x , y ])
192125
193- raw_ex = numpy .corrcoef (combined , rowvar = row_var )
126+ raw_ex = numpy .corrcoef (combined , rowvar = False )
194127 logger .debug ("raw_ex.shape: {}" .format (raw_ex .shape ))
195128
196- ex = raw_ex [:off_diag_ind [ 0 ], off_diag_ind [1 ]:]
129+ ex = raw_ex [:x . shape [ 1 ], - y . shape [1 ]:]
197130 logger .debug ("ex:\n {}" .format (ex ))
198131 logger .debug ("ex.shape: {}" .format (ex .shape ))
199132
200- r = fast_corr .fast_corr (x , y , axis = cur_axis )
133+ r = fast_corr .fast_corr (x , y )
201134 logger .debug ("r:\n {}" .format (r ))
202135 logger .debug ("r.shape: {}" .format (r .shape ))
203136
204137 self .assertTrue (numpy .allclose (ex , r ))
205138
139+ def test_fast_spearman (self ):
140+ x , y = TestFastCorr .build_standard_x_y ()
141+
142+ ex = numpy .array ([[1.0 , 1.0 , 1.0 ], [- 1.0 , - 1.0 , - 1.0 ], [1.0 , 1.0 , 1.0 ]])
143+
144+ r = fast_corr .fast_spearman (x , y )
145+ logger .debug ("r: {}" .format (r ))
146+
147+ self .assertTrue (numpy .allclose (ex , r ))
148+
206149
207150if __name__ == "__main__" :
208151 setup_logger .setup (verbose = True )
0 commit comments