-
Notifications
You must be signed in to change notification settings - Fork 33
/
stats_utils.py
98 lines (68 loc) · 2.04 KB
/
stats_utils.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
Some statistics utils.
"""
import numpy as np
from os.path import join
def expectation_2d(pdf):
"""
Computes the statistical expectation of a pdf defined
over two discrete random variables.
Parameters
----------
pdf: ndarray
a numpy 2-dimensional array with probability for each (x, y).
Returns
-------
ndarray
the expectation for the x and y random variables.
"""
h, w = pdf.shape
pdf = np.float32(pdf)
pdf /= np.sum(pdf)
x_range = range(0, w)
y_range = range(0, h)
cols, rows = np.meshgrid(x_range, y_range)
grid = np.stack((rows, cols), axis=-1)
weighted_grid = pdf[..., None] * grid # broadcasting
E = np.apply_over_axes(np.sum, weighted_grid, axes=[0, 1])
E = np.squeeze(E)
return E
def covariance_matrix_2d(pdf):
"""
Computes the covariance matrix of a 2-dimensional gaussian
fitted over a joint pdf of two discrete random variables.
Parameters
----------
pdf: ndarray
a numpy 2-dimensional array with probability for each (x, y).
Returns
-------
ndarray
the covariance matrix.
"""
h, w = pdf.shape
pdf = np.float32(pdf)
pdf /= np.sum(pdf)
x_range = range(0, w)
y_range = range(0, h)
cols, rows = np.meshgrid(x_range, y_range)
grid = np.stack((rows, cols), axis=-1)
mu = expectation_2d(pdf)
grid = np.float32(grid)
# remove mean
grid -= mu[None, None, :]
grid_flat = np.reshape(grid, newshape=(-1, 2))
# in computing the dot product, pdf has to be counted one (outside the square!)
cov = np.dot(grid_flat.T, grid_flat * np.reshape(pdf, -1)[..., None])
return cov
def read_dreyeve_design(dreyeve_root):
"""
Reads the whole dr(eye)ve design.
Returns
-------
ndarray
the dr(eye)ve design in the form (sequences, params).
"""
with open(join(dreyeve_root, 'dr(eye)ve_design.txt')) as f:
dreyeve_design = np.array([l.rstrip().split('\t') for l in f.readlines()])
return dreyeve_design