Skip to content

Commit 4c69c4e

Browse files
mariomario
authored andcommitted
removed size function
1 parent 0d20d5b commit 4c69c4e

File tree

1 file changed

+22
-45
lines changed

1 file changed

+22
-45
lines changed

code/python/cni_toolbox/gadgets.py

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'''
22
-----------------------------------------------------------------------------
3-
LICENSE
3+
LICENSE
44
55
Copyright 2020 Mario Senden
66
@@ -18,15 +18,15 @@
1818
along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
2020
-----------------------------------------------------------------------------
21-
DESCRPTION
21+
DESCRPTION
2222
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
2525
without being clearly assignable to one specific tool.
2626
2727
'''
2828

29-
import numpy as np
29+
import numpy as np
3030
import scipy as sc
3131
from scipy.special import gamma
3232

@@ -51,40 +51,39 @@ def two_gamma(timepoints):
5151

5252

5353
def regress(Y, X, l = 0.):
54-
55-
'''
54+
'''
5655
Parameters
5756
----------
58-
Y : floating point array (observations-by-outcomes)
57+
Y: floating point array (observations-by-outcomes)
5958
outcome variables
60-
X : floating pint array (observation-by-predictors)
59+
X: floating pint array (observation-by-predictors)
6160
predictors
62-
l : float
61+
l: float
6362
(optional) ridge penalty parameter
6463
6564
Returns
6665
-------
67-
beta : floating point array (predictors-by-outcomes)
66+
beta: floating point array (predictors-by-outcomes)
6867
beta coefficients
6968
'''
7069

7170
if X.ndim>1:
7271
n_observations, n_predictors = X.shape
73-
72+
7473
else:
7574
n_observations = X.size
7675
n_predictors = 1
77-
76+
7877

7978
if n_observations < n_predictors:
8079
U, D, V = np.linalg.svd(X, full_matrices = False)
81-
80+
8281
D = np.diag(D)
8382
beta = np.matmul(
8483
np.matmul(
8584
np.matmul(
8685
np.matmul(
87-
V.transpose(),
86+
V.transpose(),
8887
sc.linalg.inv(
8988
D**2 +
9089
l * np.eye(n_observations))),
@@ -95,26 +94,26 @@ def regress(Y, X, l = 0.):
9594
np.matmul(
9695
sc.linalg.inv(
9796
np.matmul(X.transpose(), X) +
98-
l * np.eye(n_predictors)),
97+
l * np.eye(n_predictors)),
9998
X.transpose()), Y)
100-
101-
return beta
99+
100+
return beta
102101

103102
def correct_autocorr(X, W):
104103
'''
105104
Parameters
106105
----------
107-
X : floating point array (2D)
106+
X: floating point array (2D)
108107
timecourses
109-
W : floating point array (1D)
108+
W: floating point array (1D)
110109
AR(2) model weights
111110
112111
Returns
113112
-------
114-
X_corrected : floating point array (2D)
113+
X_corrected: floating point array (2D)
115114
timecourses corrected for autocorrelation
116115
'''
117-
116+
118117
rows, cols = size(X, 2)
119118
X = np.reshape(X, (rows, cols))
120119
X_corrected = np.zeros((rows, cols))
@@ -124,27 +123,5 @@ def correct_autocorr(X, W):
124123
x_shift_2 = np.append(np.zeros(2), X[0:-2, j]).reshape(rows, -1)
125124
x_sliced = np.hstack([x_shift_0, x_shift_1, x_shift_2])
126125
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
138126

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

Comments
 (0)