Skip to content

Commit

Permalink
Merge 36a7338 into 0444a66
Browse files Browse the repository at this point in the history
  • Loading branch information
JIE LI committed Nov 20, 2015
2 parents 0444a66 + 36a7338 commit af1e1b6
Show file tree
Hide file tree
Showing 5 changed files with 295,475 additions and 294,912 deletions.
51 changes: 51 additions & 0 deletions code/utils/kernal_smoothing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import numpy as np
import nibabel as nib
import matplotlib.pyplot as plt
from scipy.stats import norm
from sklearn.neighbors import KernelDensity
from sklearn.grid_search import GridSearchCV

def load_data(f1):
"""
Return the image data as an array and the convolved time course
Parameters
----------
f1,f2 : string
The name of data to process
Returns
-------
tuple:
Contains the image data as an array and the convolved time course
"""
# Load the image as an image object
img = nib.load('../../data/sub001/BOLD/' + f1 + '.nii.gz')
# Load the image data as an array
# Drop the first 4 3D volumes from the array
data = img.get_data()[..., 4:]

return data

def smoothing(data, width):
kde = KernelDensity(kernel='gaussian', bandwidth=width).fit(data)

return kde.score_samples(data)

def best_bandwidth(data):
grid = GridSearchCV(KernelDensity(), {'bandwidth': np.linspace(0.1, 1.0, 30)}, cv = 10)
grid.fit(data[:, None])

return grid.best_params_['bandwidth']

if __name__ == '__main__':
from sys import argv

filename = argv[1]
data = load_data('../../data/sub001/BOLD/' + filename)
data_2d = data.reshape(-1, data.shape[-1])

simulation = np.ones(data_2d.shape)
for i in range(data_2d.shape[1]):
best_width = best_bandwidth(data_2d[:,i])
simulation[:,i] = smoothing(data_2d[:,i], best_width)
np.savetxt("simulated_data.txt", simulation, , newline='\r\n')
Binary file added data/beta/.RData
Binary file not shown.
Loading

0 comments on commit af1e1b6

Please sign in to comment.