-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
318 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from __future__ import print_function | ||
from __future__ import division | ||
import json | ||
import nibabel as nib | ||
import numpy as np | ||
import sys | ||
from glob import glob | ||
from os.path import exists | ||
from multiprocessing import Pool | ||
|
||
REPO_HOME_RELATIVE_PATH = '../../' | ||
sys.path.append(REPO_HOME_RELATIVE_PATH) | ||
import config as cf | ||
|
||
DATA_PATHS = json.load(open(REPO_HOME_RELATIVE_PATH + 'data/data_path.json')) | ||
|
||
INCORRECT_NUM_ARGS_MESSAGE = 'Invalid number of arguments: specify alignment type' | ||
ILLEGAL_ARG_MESSAGE = 'preprocess.py must be provided with alignment argument' | ||
|
||
|
||
def concatenate_runs(alignment): | ||
for i, subject in enumerate(DATA_PATHS['subjects']): | ||
nii_file_name = '{0}data/processed/sub{1}_{2}'.format( | ||
REPO_HOME_RELATIVE_PATH, i + 1, alignment) | ||
if not exists(nii_file_name + '.nii') or not cf.USE_CACHED_DATA: | ||
run_data = [] | ||
for j, run in enumerate(subject['runs']): | ||
task_path = run[alignment]['path'] | ||
img = nib.load(REPO_HOME_RELATIVE_PATH + task_path) | ||
data = img.get_data() | ||
if j == 0: | ||
run_data.append(data[..., :-4]) | ||
elif j >= 1 and j <= 6: | ||
run_data.append(data[..., 4:-4]) | ||
else: | ||
run_data.append(data[..., 4:]) | ||
concatenated_run_data = np.concatenate(run_data, axis=3) | ||
concatenated_img = nib.Nifti1Image(concatenated_run_data, | ||
np.eye(4)) | ||
nib.save(concatenated_img, nii_file_name) | ||
print('Saved {0}'.format(nii_file_name + '.nii')) | ||
else: | ||
print('Using cached version of {0}'.format(nii_file_name + '.nii')) | ||
|
||
|
||
def reshape_data_to_2d(alignment): | ||
files_to_reshape = np.sort(glob('{0}data/processed/sub*_{1}.nii'.format( | ||
REPO_HOME_RELATIVE_PATH, alignment))) | ||
for f in files_to_reshape: | ||
file_name_2d = f.replace('.nii', '_2d') | ||
if not exists(file_name_2d + '.npy') or not cf.USE_CACHED_DATA: | ||
data = nib.load(f).get_data() | ||
data_chunks = partition_4d_data(data) | ||
p = Pool(cf.NUM_PROCESSES) | ||
reshaped_chunks = p.imap(reshape_4d_data, data_chunks) | ||
data_2d = merge_2d_data(reshaped_chunks) | ||
np.save(file_name_2d, data_2d) | ||
print('Saved {0}'.format(file_name_2d + '.npy')) | ||
else: | ||
print('Using cached version of {0}'.format(file_name_2d + '.npy')) | ||
|
||
|
||
def partition_4d_data(data): | ||
num_partitions = cf.NUM_PROCESSES | ||
num_volunes = data.shape[-1] | ||
partition_indices = [(num_volunes // num_partitions) * i | ||
for i in range(1, num_partitions)] | ||
return np.split(data, partition_indices, axis=3) | ||
|
||
|
||
def reshape_4d_data(data): | ||
return np.reshape(data, (-1, data.shape[-1])) | ||
|
||
|
||
def merge_2d_data(data_slices): | ||
return np.hstack(tuple(data_slices)) | ||
|
||
# def band_pass_filter(): | ||
|
||
if __name__ == '__main__': | ||
if len(sys.argv) != 2: | ||
raise ValueError(INCORRECT_NUM_ARGS_MESSAGE) | ||
alignment = sys.argv[1] | ||
if alignment not in ['linear', 'non_linear', 'rcds']: | ||
raise ValueError(ILLEGAL_ARG_MESSAGE) | ||
concatenate_runs(alignment) | ||
reshape_data_to_2d(alignment) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from __future__ import print_function | ||
from __future__ import division | ||
import nibabel as nib | ||
import numpy as np | ||
from scipy import stats | ||
from multiprocessing import Pool, Process | ||
import itertools | ||
import sys | ||
import os | ||
import sharedmem as sm | ||
import gc | ||
|
||
REPO_HOME_RELATIVE_PATH = '../../' | ||
sys.path.append(REPO_HOME_RELATIVE_PATH) | ||
import config as cf | ||
|
||
|
||
def parallelize_correlation(): | ||
chunk_size = len(shared_subject1) // cf.NUM_PROCESSES | ||
output_correlations = sm.empty(len(shared_subject1)) | ||
processes = [ | ||
Process(target=correlation, | ||
args=(shared_subject1, shared_subject2, i * chunk_size, min( | ||
(i + 1) * chunk_size, len(shared_subject1)), | ||
output_correlations)) for i in xrange(cf.NUM_PROCESSES) | ||
] | ||
for p in processes: | ||
p.start() | ||
for p in processes: | ||
p.join() | ||
return output_correlations | ||
|
||
|
||
def correlation(shared_subject1, shared_subject2, start_index, stop_index, | ||
output_correlations): | ||
for i in range(start_index, stop_index): | ||
output_correlations[i] = stats.pearsonr(shared_subject1[i, :], | ||
shared_subject2[ | ||
i, :])[0] | ||
|
||
|
||
shared_subject1 = sm.copy(np.load(REPO_HOME_RELATIVE_PATH + | ||
'data/processed/sub1_rcds_2d.npy')) | ||
shared_subject2 = sm.copy(np.load(REPO_HOME_RELATIVE_PATH + | ||
'data/processed/sub2_rcds_2d.npy')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from os import environ | ||
|
||
USE_CACHED_DATA = environ.get('STAT159_CACHED_DATA', 'True') == 'True' | ||
|
||
NUM_PROCESSES = int(environ.get('STAT159_NUM_PROCESSES', 5)) | ||
|
||
NUM_VOXELS = 1108800 | ||
|
||
NUM_TOTAL_VOLUMES = 3543 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,181 @@ | ||
{ | ||
"bold_dico_7Tad2grpbold7Tad": { | ||
"sub1" : { | ||
"runs" : [ | ||
{ | ||
"path": "data/raw/sub001/BOLD/task001_run001/bold_dico_dico7Tad2grpbold7Tad.nii", | ||
"hash": "b4350e69fdbb5c62e4c8be66e71c2d86" | ||
}, | ||
{ | ||
"path": "data/raw/sub001/BOLD/task001_run002/bold_dico_dico7Tad2grpbold7Tad.nii", | ||
"hash": "ec625fd0de6433cfecbfb542a6598683" | ||
}, | ||
{ | ||
"path": "data/raw/sub001/BOLD/task001_run003/bold_dico_dico7Tad2grpbold7Tad.nii", | ||
"hash": "9b92dc4fb03bc5080f7eae904782424a" | ||
}, | ||
{ | ||
"path": "data/raw/sub001/BOLD/task001_run004/bold_dico_dico7Tad2grpbold7Tad.nii", | ||
"hash": "658cb0bcbfe28286af1514a232d1d79d" | ||
}, | ||
{ | ||
"path": "data/raw/sub001/BOLD/task001_run005/bold_dico_dico7Tad2grpbold7Tad.nii", | ||
"hash": "3d299ebfc9aa9e4419d3f185068fd5a2" | ||
}, | ||
{ | ||
"path": "data/raw/sub001/BOLD/task001_run006/bold_dico_dico7Tad2grpbold7Tad.nii", | ||
"hash": "e381354cf8fcb8dcd50d711e0cd24d92" | ||
}, | ||
{ | ||
"path": "data/raw/sub001/BOLD/task001_run007/bold_dico_dico7Tad2grpbold7Tad.nii", | ||
"hash": "814090d79f447671ae483ec2ea9dde3c" | ||
}, | ||
{ | ||
"path": "data/raw/sub001/BOLD/task001_run008/bold_dico_dico7Tad2grpbold7Tad.nii", | ||
"hash": "4fc0745cb64f76cd9cf43d6351de5848" | ||
} | ||
] | ||
"subjects" : | ||
[ | ||
{ | ||
"runs" : [ | ||
{ | ||
"linear" : { | ||
"path": "data/raw/sub001/task001_run001/bold_dico_dico7Tad2grpbold7Tad.nii", | ||
"hash": "b4350e69fdbb5c62e4c8be66e71c2d86" | ||
}, | ||
"rcds" : { | ||
"path": "data/raw/sub001/task001_run001/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"linear" : { | ||
"path": "data/raw/sub001/task001_run002/bold_dico_dico7Tad2grpbold7Tad.nii", | ||
"hash": "ec625fd0de6433cfecbfb542a6598683" | ||
}, | ||
"rcds" : { | ||
"path": "data/raw/sub001/task001_run002/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"linear" : { | ||
"path": "data/raw/sub001/task001_run003/bold_dico_dico7Tad2grpbold7Tad.nii", | ||
"hash": "9b92dc4fb03bc5080f7eae904782424a" | ||
}, | ||
"rcds" : { | ||
"path": "data/raw/sub001/task001_run003/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"linear" : { | ||
"path": "data/raw/sub001/task001_run004/bold_dico_dico7Tad2grpbold7Tad.nii", | ||
"hash": "658cb0bcbfe28286af1514a232d1d79d" | ||
}, | ||
"rcds" : { | ||
"path": "data/raw/sub001/task001_run004/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"linear" : { | ||
"path": "data/raw/sub001/task001_run005/bold_dico_dico7Tad2grpbold7Tad.nii", | ||
"hash": "3d299ebfc9aa9e4419d3f185068fd5a2" | ||
}, | ||
"rcds" : { | ||
"path": "data/raw/sub001/task001_run005/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"linear" : { | ||
"path": "data/raw/sub001/task001_run006/bold_dico_dico7Tad2grpbold7Tad.nii", | ||
"hash": "e381354cf8fcb8dcd50d711e0cd24d92" | ||
}, | ||
"rcds" : { | ||
"path": "data/raw/sub001/task001_run006/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"linear" : { | ||
"path": "data/raw/sub001/task001_run007/bold_dico_dico7Tad2grpbold7Tad.nii", | ||
"hash": "814090d79f447671ae483ec2ea9dde3c" | ||
}, | ||
"rcds" : { | ||
"path": "data/raw/sub001/task001_run007/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"linear" : { | ||
"path": "data/raw/sub001/task001_run008/bold_dico_dico7Tad2grpbold7Tad.nii", | ||
"hash": "4fc0745cb64f76cd9cf43d6351de5848" | ||
}, | ||
"rcds" : { | ||
"path": "data/raw/sub001/task001_run008/bold_dico_dico_rcds_nl.nii" | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"runs" : [ | ||
{ | ||
"rcds" : { | ||
"path": "data/raw/sub002/task001_run001/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"rcds" : { | ||
"path": "data/raw/sub002/task001_run002/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"rcds" : { | ||
"path": "data/raw/sub002/task001_run003/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"rcds" : { | ||
"path": "data/raw/sub002/task001_run004/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"rcds" : { | ||
"path": "data/raw/sub002/task001_run005/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"rcds" : { | ||
"path": "data/raw/sub002/task001_run006/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"rcds" : { | ||
"path": "data/raw/sub002/task001_run007/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"rcds" : { | ||
"path": "data/raw/sub002/task001_run008/bold_dico_dico_rcds_nl.nii" | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"runs" : [ | ||
{ | ||
"rcds" : { | ||
"path": "data/raw/sub003/task001_run001/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"rcds" : { | ||
"path": "data/raw/sub003/task001_run002/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"rcds" : { | ||
"path": "data/raw/sub003/task001_run003/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"rcds" : { | ||
"path": "data/raw/sub003/task001_run004/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"rcds" : { | ||
"path": "data/raw/sub003/task001_run005/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"rcds" : { | ||
"path": "data/raw/sub003/task001_run006/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"rcds" : { | ||
"path": "data/raw/sub003/task001_run007/bold_dico_dico_rcds_nl.nii" | ||
} | ||
}, | ||
{ | ||
"rcds" : { | ||
"path": "data/raw/sub003/task001_run008/bold_dico_dico_rcds_nl.nii" | ||
} | ||
} | ||
] | ||
} | ||
], | ||
"templates": { | ||
"brain_mask_intersection": { | ||
"linear": { | ||
"path": "data/raw/templates/linear_brain_mask_intersection.nii", | ||
"hash": "77ba439c5681570be853be33475e6c98" | ||
}, | ||
"nonlinear": { | ||
"path": "data/raw/templates/non_linear_brain_mask_intersection.nii", | ||
"hash": "0daaed45e7f26dca2269be91afcd067e" | ||
} | ||
} | ||
} | ||
} |