forked from google/telluride_decoding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.py
61 lines (48 loc) · 2.04 KB
/
utils_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Tests for utils."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from telluride_decoding import utils
import numpy as np
import tensorflow as tf
class UtilsTest(tf.test.TestCase):
def test_pearson(self):
"""Test the Pearson correlation graph.
Use simulated data and make sure we get the result we expect.
"""
# Example data from: http://www.statisticshowto.com/
# . probability-and-statistics/correlation-coefficient-formula/
correlation_data = np.array([
[1, 43, 99],
[2, 21, 65],
[3, 25, 79],
[4, 42, 75],
[5, 57, 87],
[6, 59, 81]])
# Test the correlation graph using the sample data above
test_x = tf.constant(correlation_data[:, 1].T, dtype=tf.float32)
test_y = tf.constant(correlation_data[:, 2].T, dtype=tf.float32)
correlation_tensor = utils.pearson_correlation_graph(test_x, test_y)
with tf.compat.v1.Session() as _:
correlation_r = correlation_tensor.eval()
print('Test correlation produced:', correlation_r)
self.assertAlmostEqual(correlation_r[0, 0], 1.0, delta=0.0001)
self.assertAlmostEqual(correlation_r[0, 1], 0.5298, delta=0.0001)
self.assertAlmostEqual(correlation_r[1, 0], 0.5298, delta=0.0001)
self.assertAlmostEqual(correlation_r[1, 1], 1.0, delta=0.0001)
def test_pearson2(self):
"""Test to make sure TF and np.corrcoef give same answers with random data.
This test makes sure it works with multi-dimensional features.
"""
fsize = 2 # feature size
dsize = 6 # sample count
x = np.random.random((fsize, dsize))
y = np.random.random((fsize, dsize))
tf.InteractiveSession()
cor = utils.pearson_correlation_graph(x, y)
print('test_pearson2: np.corrcoef:', np.corrcoef(x, y))
print('test_pearson2: cor.eval():', cor.eval())
print('test_pearson2: Difference:', np.corrcoef(x, y) - cor.eval())
self.assertTrue(np.allclose(np.corrcoef(x, y), cor.eval(), atol=2e-7))
if __name__ == '__main__':
tf.test.main()