Skip to content

Commit df98b94

Browse files
Merge pull request #126 from lucywowen/master
code cleanup
2 parents b4b06c7 + 56be783 commit df98b94

File tree

7 files changed

+1018
-42
lines changed

7 files changed

+1018
-42
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ ENV/
101101
# Rope project settings
102102
.ropeproject
103103

104+
# pytest
105+
.pytest_cache
106+
107+
# idea files
104108
.idea/**/
105109

106110
*Untitled*

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 932 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ If you are reporting a bug, please do your best to include the following:
7777

7878
<h3>Contributing code</h3>
7979

80-
The preferred way to contribute to supereeg is to fork the main repository on GitHub, then submit a pull request.
80+
The preferred way to contribute to timecorr is to fork the main repository on GitHub, then submit a pull request.
8181

8282
+ If your pull request addresses an issue, please use the title to describe the issue and mention the issue number in the pull request description to ensure a link is created to the original issue.
8383

tests/test_helpers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import os
55
import pytest
66

7-
#using method from supereeg
87
from timecorr.helpers import gaussian_weights, gaussian_params, wcorr, wisfc, mat2vec, vec2mat, isfc, mean_combine, \
98
corrmean_combine, timepoint_decoder, laplace_params, laplace_weights
109

tests/test_timecorr.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import pandas as pd
44
import hypertools as hyp
55

6+
import timecorr as tc
67
from timecorr.timecorr import timecorr
8+
from timecorr.simulate import simulate_data
79
from timecorr.helpers import isfc, gaussian_weights, gaussian_params
810

911
#TODO: need *real* tests-- e.g. generate a small dataset and verify that we actually get the correct answers
@@ -16,6 +18,68 @@
1618
# if above is how to make a numpy list than TC isn't capible np.lists currently
1719
random_numbers= (2 ,3 ,5, 10, 12, 4, 6)
1820

21+
sim_1 = simulate_data(S=1, T=30, K=50, set_random_seed=100)
22+
sim_3 = simulate_data(S=3, T=30, K=50, set_random_seed=100)
23+
24+
width = 10
25+
laplace = {'name': 'Laplace', 'weights': tc.laplace_weights, 'params': {'scale': width}}
26+
27+
28+
def test_reduce_shape():
29+
dyna_corrs_reduced_1 = timecorr(sim_1, rfun='PCA',
30+
weights_function=laplace['weights'], weights_params=laplace['params'])
31+
32+
dyna_corrs_reduced_3 = timecorr(sim_3, rfun='PCA',
33+
weights_function=laplace['weights'], weights_params=laplace['params'])
34+
assert np.shape(dyna_corrs_reduced_1) == np.shape(sim_1)
35+
assert np.shape(dyna_corrs_reduced_3) == np.shape(sim_3)
36+
37+
def test_nans():
38+
sim_3[0][0] = np.nan
39+
dyna_corrs_reduced_3 = timecorr(sim_3, rfun='PCA',
40+
weights_function=laplace['weights'], weights_params=laplace['params'])
41+
42+
assert np.shape(dyna_corrs_reduced_3) == np.shape(sim_3)
43+
44+
45+
def test_include_timepoints_all():
46+
dyna_corrs_reduced_3 = timecorr(sim_3, rfun='PCA',
47+
weights_function=laplace['weights'], weights_params=laplace['params'],
48+
include_timepoints='all')
49+
50+
assert np.shape(dyna_corrs_reduced_3) == np.shape(sim_3)
51+
52+
53+
def test_include_timepoints_pre():
54+
dyna_corrs_reduced_3 = timecorr(sim_3, rfun='PCA',
55+
weights_function=laplace['weights'], weights_params=laplace['params'],
56+
include_timepoints='pre')
57+
58+
assert np.shape(dyna_corrs_reduced_3) == np.shape(sim_3)
59+
60+
61+
def test_include_timepoints_post():
62+
dyna_corrs_reduced_3 = timecorr(sim_3, rfun='PCA',
63+
weights_function=laplace['weights'], weights_params=laplace['params'],
64+
include_timepoints='post')
65+
66+
assert np.shape(dyna_corrs_reduced_3) == np.shape(sim_3)
67+
68+
def test_exclude_timepoints_pos():
69+
dyna_corrs_reduced_3 = timecorr(sim_3, rfun='PCA',
70+
weights_function=laplace['weights'], weights_params=laplace['params'],
71+
exclude_timepoints=3)
72+
73+
assert np.shape(dyna_corrs_reduced_3) == np.shape(sim_3)
74+
75+
76+
def test_exclude_timepoints_neg():
77+
dyna_corrs_reduced_3 = timecorr(sim_3, rfun='PCA',
78+
weights_function=laplace['weights'], weights_params=laplace['params'],
79+
exclude_timepoints=-3)
80+
81+
assert np.shape(dyna_corrs_reduced_3) == np.shape(sim_3)
82+
1983

2084
def test_timecorr():
2185

timecorr/helpers.py

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ def laplace_weights(T, params=laplace_params):
4343

4444

4545
def eye_weights(T, params=eye_params):
46-
#if params is None:
47-
# params = eye_params
4846

4947
return np.eye(T)
5048

@@ -153,7 +151,7 @@ def wisfc(data, timepoint_weights, subject_weights=None):
153151
if type(data) != list:
154152
return wisfc([data], timepoint_weights, subject_weights=subject_weights)[0]
155153

156-
if subject_weights is None: #similarity-based weights
154+
if subject_weights is None:
157155
K = data[0].shape[1]
158156
connectomes = np.zeros([len(data), int((K ** 2 - K) / 2)])
159157
for s in np.arange(len(data)):
@@ -205,7 +203,7 @@ def apply_by_row(corrs, f):
205203
if type(corrs) is list:
206204
return list(map(lambda x: apply_by_row(x, f), corrs))
207205

208-
corrs = vec2mat(corrs) #V by V by T
206+
corrs = vec2mat(corrs)
209207
return np.stack(list(map(lambda x: f(np.squeeze(x)), np.split(corrs, corrs.shape[2], axis=2))), axis=0)
210208

211209
def corrmean_combine(corrs):
@@ -313,8 +311,15 @@ def reduce(corrs, rfun=None):
313311

314312
if rfun in graph_measures.keys():
315313
return apply_by_row(corrs, graph_measures[rfun])
316-
else: # use hypertools
317-
return hyp.reduce(corrs, reduce=rfun, ndims=V)
314+
else:
315+
red_corrs = hyp.reduce(corrs, reduce=rfun, ndims=V)
316+
317+
D = np.shape(red_corrs)[-1]
318+
319+
if D < V :
320+
red_corrs = np.hstack((red_corrs, np.zeros((D, V - D))))
321+
322+
return red_corrs
318323

319324

320325
def smooth(w, windowsize=10, kernel_fun=laplace_weights, kernel_params=laplace_params):
@@ -667,7 +672,6 @@ def pca_decoder(data, nfolds=2, dims=10, cfun=isfc, weights_fun=laplace_weights,
667672
group_assignments = get_xval_assignments(len(pca_data), nfolds)
668673
results_pd = pd.DataFrame()
669674

670-
corrs = []
671675
for i in range(0, nfolds):
672676
for d in range(1, dims + 1):
673677

@@ -799,37 +803,6 @@ def decoder(corrs):
799803
return next_results_pd
800804

801805

802-
# def predict(x, n=1):
803-
# '''
804-
# Use a Kalman filter (with automatically inferred parameters) to estimate
805-
# future states of a signal, n timepoints into the future.
806-
#
807-
# x: timepoints by features signal (numpy array)
808-
# n: number of timepoints into the future (must be an integer)
809-
#
810-
# Returns a new numpy array with x.shape[0] + n rows and x.shape[1] columns,
811-
# where the last n rows contain the predicted future states. The other
812-
# entries contain "smoothed" estimates of the observed signals.
813-
# '''
814-
#
815-
# if n == 0:
816-
# return kf.em(x).smooth(x)[0]
817-
#
818-
# x_masked = np.ma.MaskedArray(np.vstack((x, np.tile(np.nan, (1, x.shape[1])))))
819-
# x_masked[-1, :] = np.ma.masked
820-
#
821-
# kf = pykalman.KalmanFilter(initial_state_mean=np.mean(x, axis=0), n_dim_obs=x.shape[1], n_dim_state=x.shape[1])
822-
# x_predicted = kf.em(x_masked, em_vars='all').smooth(x_masked)
823-
#
824-
# if n == 1:
825-
# return x_predicted[0] #x_predicted[1] contains timepoint-by-timepoint covariance estimates
826-
# elif n > 1:
827-
# next_x_predicted = predict(x_predicted[0], n-1)
828-
# diff = next_x_predicted.shape[0] - x_predicted[0].shape[0]
829-
# next_x_predicted[:-diff, :] = x_predicted[0]
830-
# return next_x_predicted
831-
832-
833806
def weighted_mean(x, axis=None, weights=None, tol=1e-5):
834807
if axis is None:
835808
axis = len(x.shape) - 1
@@ -975,14 +948,12 @@ def get_xval_assignments(ndata, nfolds):
975948
group_assignments = np.zeros(ndata)
976949
groupsize = int(np.ceil(ndata / nfolds))
977950

978-
# group assignments
979951
for i in range(1, nfolds):
980952
inds = np.arange(i * groupsize, np.min([(i + 1) * groupsize, ndata]))
981953
group_assignments[inds] = i
982954
np.random.shuffle(group_assignments)
983955
return group_assignments
984956

985-
# set some defaults for plots
986957
SMALL_SIZE = 18
987958
MEDIUM_SIZE = 21
988959
BIGGER_SIZE = 24

0 commit comments

Comments
 (0)