1
1
'''
2
2
-----------------------------------------------------------------------------
3
- LICENSE
3
+ LICENSE
4
4
5
5
Copyright 2020 Mario Senden
6
6
18
18
along with this program. If not, see <http://www.gnu.org/licenses/>.
19
19
20
20
-----------------------------------------------------------------------------
21
- DESCRPTION
21
+ DESCRPTION
22
22
23
- This module contains functions either to be used in conjunction with
24
- the core tools of the CNI toolbox; or to be used by the tools but
23
+ This module contains functions either to be used in conjunction with
24
+ the core tools of the CNI toolbox; or to be used by the tools but
25
25
without being clearly assignable to one specific tool.
26
26
27
27
'''
28
28
29
- import numpy as np
29
+ import numpy as np
30
30
import scipy as sc
31
31
from scipy .special import gamma
32
32
@@ -51,40 +51,39 @@ def two_gamma(timepoints):
51
51
52
52
53
53
def regress (Y , X , l = 0. ):
54
-
55
- '''
54
+ '''
56
55
Parameters
57
56
----------
58
- Y : floating point array (observations-by-outcomes)
57
+ Y: floating point array (observations-by-outcomes)
59
58
outcome variables
60
- X : floating pint array (observation-by-predictors)
59
+ X: floating pint array (observation-by-predictors)
61
60
predictors
62
- l : float
61
+ l: float
63
62
(optional) ridge penalty parameter
64
63
65
64
Returns
66
65
-------
67
- beta : floating point array (predictors-by-outcomes)
66
+ beta: floating point array (predictors-by-outcomes)
68
67
beta coefficients
69
68
'''
70
69
71
70
if X .ndim > 1 :
72
71
n_observations , n_predictors = X .shape
73
-
72
+
74
73
else :
75
74
n_observations = X .size
76
75
n_predictors = 1
77
-
76
+
78
77
79
78
if n_observations < n_predictors :
80
79
U , D , V = np .linalg .svd (X , full_matrices = False )
81
-
80
+
82
81
D = np .diag (D )
83
82
beta = np .matmul (
84
83
np .matmul (
85
84
np .matmul (
86
85
np .matmul (
87
- V .transpose (),
86
+ V .transpose (),
88
87
sc .linalg .inv (
89
88
D ** 2 +
90
89
l * np .eye (n_observations ))),
@@ -95,26 +94,26 @@ def regress(Y, X, l = 0.):
95
94
np .matmul (
96
95
sc .linalg .inv (
97
96
np .matmul (X .transpose (), X ) +
98
- l * np .eye (n_predictors )),
97
+ l * np .eye (n_predictors )),
99
98
X .transpose ()), Y )
100
-
101
- return beta
99
+
100
+ return beta
102
101
103
102
def correct_autocorr (X , W ):
104
103
'''
105
104
Parameters
106
105
----------
107
- X : floating point array (2D)
106
+ X: floating point array (2D)
108
107
timecourses
109
- W : floating point array (1D)
108
+ W: floating point array (1D)
110
109
AR(2) model weights
111
110
112
111
Returns
113
112
-------
114
- X_corrected : floating point array (2D)
113
+ X_corrected: floating point array (2D)
115
114
timecourses corrected for autocorrelation
116
115
'''
117
-
116
+
118
117
rows , cols = size (X , 2 )
119
118
X = np .reshape (X , (rows , cols ))
120
119
X_corrected = np .zeros ((rows , cols ))
@@ -124,27 +123,5 @@ def correct_autocorr(X, W):
124
123
x_shift_2 = np .append (np .zeros (2 ), X [0 :- 2 , j ]).reshape (rows , - 1 )
125
124
x_sliced = np .hstack ([x_shift_0 , x_shift_1 , x_shift_2 ])
126
125
X_corrected [:, j ] = np .matmul (x_sliced , W )
127
-
128
- return X_corrected
129
-
130
- def size (X , num_desired ):
131
- '''
132
- Parameters
133
- ----------
134
- X : floating point array (of unknown dimension)
135
- num_desired : integer
136
- the number of dimensions for which once would like
137
- to query the size
138
126
139
- Returns
140
- -------
141
- output : integer array
142
- size for each of num_desired dimensions
143
-
144
- '''
145
- num_existing = X .ndim
146
- output = np .ones (num_desired ).astype (int )
147
- output [0 :num_existing ] = np .shape (X )
148
- return output
149
-
150
-
127
+ return X_corrected
0 commit comments