Skip to content

Commit

Permalink
Merge pull request #126 from lucywowen/master
Browse files Browse the repository at this point in the history
code cleanup
  • Loading branch information
jeremymanning authored Sep 9, 2019
2 parents b4b06c7 + 56be783 commit df98b94
Show file tree
Hide file tree
Showing 7 changed files with 1,018 additions and 42 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ ENV/
# Rope project settings
.ropeproject

# pytest
.pytest_cache

# idea files
.idea/**/

*Untitled*
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

932 changes: 932 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ If you are reporting a bug, please do your best to include the following:

<h3>Contributing code</h3>

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

+ 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.

Expand Down
1 change: 0 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
import pytest

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

Expand Down
64 changes: 64 additions & 0 deletions tests/test_timecorr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import pandas as pd
import hypertools as hyp

import timecorr as tc
from timecorr.timecorr import timecorr
from timecorr.simulate import simulate_data
from timecorr.helpers import isfc, gaussian_weights, gaussian_params

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

sim_1 = simulate_data(S=1, T=30, K=50, set_random_seed=100)
sim_3 = simulate_data(S=3, T=30, K=50, set_random_seed=100)

width = 10
laplace = {'name': 'Laplace', 'weights': tc.laplace_weights, 'params': {'scale': width}}


def test_reduce_shape():
dyna_corrs_reduced_1 = timecorr(sim_1, rfun='PCA',
weights_function=laplace['weights'], weights_params=laplace['params'])

dyna_corrs_reduced_3 = timecorr(sim_3, rfun='PCA',
weights_function=laplace['weights'], weights_params=laplace['params'])
assert np.shape(dyna_corrs_reduced_1) == np.shape(sim_1)
assert np.shape(dyna_corrs_reduced_3) == np.shape(sim_3)

def test_nans():
sim_3[0][0] = np.nan
dyna_corrs_reduced_3 = timecorr(sim_3, rfun='PCA',
weights_function=laplace['weights'], weights_params=laplace['params'])

assert np.shape(dyna_corrs_reduced_3) == np.shape(sim_3)


def test_include_timepoints_all():
dyna_corrs_reduced_3 = timecorr(sim_3, rfun='PCA',
weights_function=laplace['weights'], weights_params=laplace['params'],
include_timepoints='all')

assert np.shape(dyna_corrs_reduced_3) == np.shape(sim_3)


def test_include_timepoints_pre():
dyna_corrs_reduced_3 = timecorr(sim_3, rfun='PCA',
weights_function=laplace['weights'], weights_params=laplace['params'],
include_timepoints='pre')

assert np.shape(dyna_corrs_reduced_3) == np.shape(sim_3)


def test_include_timepoints_post():
dyna_corrs_reduced_3 = timecorr(sim_3, rfun='PCA',
weights_function=laplace['weights'], weights_params=laplace['params'],
include_timepoints='post')

assert np.shape(dyna_corrs_reduced_3) == np.shape(sim_3)

def test_exclude_timepoints_pos():
dyna_corrs_reduced_3 = timecorr(sim_3, rfun='PCA',
weights_function=laplace['weights'], weights_params=laplace['params'],
exclude_timepoints=3)

assert np.shape(dyna_corrs_reduced_3) == np.shape(sim_3)


def test_exclude_timepoints_neg():
dyna_corrs_reduced_3 = timecorr(sim_3, rfun='PCA',
weights_function=laplace['weights'], weights_params=laplace['params'],
exclude_timepoints=-3)

assert np.shape(dyna_corrs_reduced_3) == np.shape(sim_3)


def test_timecorr():

Expand Down
51 changes: 11 additions & 40 deletions timecorr/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ def laplace_weights(T, params=laplace_params):


def eye_weights(T, params=eye_params):
#if params is None:
# params = eye_params

return np.eye(T)

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

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

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

def corrmean_combine(corrs):
Expand Down Expand Up @@ -313,8 +311,15 @@ def reduce(corrs, rfun=None):

if rfun in graph_measures.keys():
return apply_by_row(corrs, graph_measures[rfun])
else: # use hypertools
return hyp.reduce(corrs, reduce=rfun, ndims=V)
else:
red_corrs = hyp.reduce(corrs, reduce=rfun, ndims=V)

D = np.shape(red_corrs)[-1]

if D < V :
red_corrs = np.hstack((red_corrs, np.zeros((D, V - D))))

return red_corrs


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

corrs = []
for i in range(0, nfolds):
for d in range(1, dims + 1):

Expand Down Expand Up @@ -799,37 +803,6 @@ def decoder(corrs):
return next_results_pd


# def predict(x, n=1):
# '''
# Use a Kalman filter (with automatically inferred parameters) to estimate
# future states of a signal, n timepoints into the future.
#
# x: timepoints by features signal (numpy array)
# n: number of timepoints into the future (must be an integer)
#
# Returns a new numpy array with x.shape[0] + n rows and x.shape[1] columns,
# where the last n rows contain the predicted future states. The other
# entries contain "smoothed" estimates of the observed signals.
# '''
#
# if n == 0:
# return kf.em(x).smooth(x)[0]
#
# x_masked = np.ma.MaskedArray(np.vstack((x, np.tile(np.nan, (1, x.shape[1])))))
# x_masked[-1, :] = np.ma.masked
#
# kf = pykalman.KalmanFilter(initial_state_mean=np.mean(x, axis=0), n_dim_obs=x.shape[1], n_dim_state=x.shape[1])
# x_predicted = kf.em(x_masked, em_vars='all').smooth(x_masked)
#
# if n == 1:
# return x_predicted[0] #x_predicted[1] contains timepoint-by-timepoint covariance estimates
# elif n > 1:
# next_x_predicted = predict(x_predicted[0], n-1)
# diff = next_x_predicted.shape[0] - x_predicted[0].shape[0]
# next_x_predicted[:-diff, :] = x_predicted[0]
# return next_x_predicted


def weighted_mean(x, axis=None, weights=None, tol=1e-5):
if axis is None:
axis = len(x.shape) - 1
Expand Down Expand Up @@ -975,14 +948,12 @@ def get_xval_assignments(ndata, nfolds):
group_assignments = np.zeros(ndata)
groupsize = int(np.ceil(ndata / nfolds))

# group assignments
for i in range(1, nfolds):
inds = np.arange(i * groupsize, np.min([(i + 1) * groupsize, ndata]))
group_assignments[inds] = i
np.random.shuffle(group_assignments)
return group_assignments

# set some defaults for plots
SMALL_SIZE = 18
MEDIUM_SIZE = 21
BIGGER_SIZE = 24
Expand Down

0 comments on commit df98b94

Please sign in to comment.