Exemple #1
0
def test_ISFC():
    curr_dir = os.path.dirname(__file__)

    mask_fname = os.path.join(curr_dir, 'mask.nii.gz')
    mask = io.load_boolean_mask(mask_fname)
    fnames = [os.path.join(curr_dir, 'subj1.nii.gz'),
              os.path.join(curr_dir, 'subj2.nii.gz')]
    masked_images = image.mask_images(io.load_images(fnames), mask)

    D = image.MaskedMultiSubjectData.from_masked_images(masked_images,
                                                        len(fnames))

    assert D.shape == (4, 5, 2), "Loaded data has incorrect shape"

    (ISFC, p) = brainiak.isfc.isfc(D, return_p=True, num_perm=100,
                                   two_sided=True, random_state=0)

    ground_truth = \
        [[1, 1, 0, -1],
         [1, 1, 0, -1],
         [0, 0, 1,  0],
         [-1, -1, 0, 1]]

    ground_truth_p = 1 - np.abs(ground_truth)

    assert np.isclose(ISFC, ground_truth).all(), \
        "Calculated ISFC does not match ground truth"

    assert np.isclose(p, ground_truth_p).all(), \
        "Calculated p values do not match ground truth"
Exemple #2
0
def test_prepare_fcma_data():
    images = io.load_images_from_dir(data_dir, suffix=suffix)
    mask = io.load_boolean_mask(mask_file)
    conditions = io.load_labels(epoch_file)
    raw_data, _, labels = prepare_fcma_data(images, conditions, mask)
    expected_raw_data = np.load(expected_dir / 'expected_raw_data.npy')
    assert len(raw_data) == len(expected_raw_data), \
        'numbers of epochs do not match in test_prepare_fcma_data'
    for idx in range(len(raw_data)):
        assert np.allclose(raw_data[idx], expected_raw_data[idx]), \
            'raw data do not match in test_prepare_fcma_data'
    assert np.array_equal(labels, expected_labels), \
        'the labels do not match in test_prepare_fcma_data'
    from brainiak.fcma.preprocessing import RandomType
    images = io.load_images_from_dir(data_dir, suffix=suffix)
    random_raw_data, _, _ = prepare_fcma_data(images,
                                              conditions,
                                              mask,
                                              random=RandomType.REPRODUCIBLE)
    assert len(random_raw_data) == len(expected_raw_data), \
        'numbers of epochs do not match in test_prepare_fcma_data'
    images = io.load_images_from_dir(data_dir, suffix=suffix)
    random_raw_data, _, _ = prepare_fcma_data(images,
                                              conditions,
                                              mask,
                                              random=RandomType.UNREPRODUCIBLE)
    assert len(random_raw_data) == len(expected_raw_data), \
        'numbers of epochs do not match in test_prepare_fcma_data'
Exemple #3
0
def test_ISFC():
    curr_dir = os.path.dirname(__file__)

    mask_fname = os.path.join(curr_dir, 'mask.nii.gz')
    mask = io.load_boolean_mask(mask_fname)
    fnames = [
        os.path.join(curr_dir, 'subj1.nii.gz'),
        os.path.join(curr_dir, 'subj2.nii.gz')
    ]
    masked_images = image.mask_images(io.load_images(fnames), mask)

    D = image.MaskedMultiSubjectData.from_masked_images(
        masked_images, len(fnames))

    assert D.shape == (4, 5, 2), "Loaded data has incorrect shape"

    ISFC = brainiak.isfc.isfc(D)

    ground_truth = \
        [[1, 1, 0, -1],
         [1, 1, 0, -1],
         [0, 0, 1,  0],
         [-1, -1, 0, 1]]

    assert np.isclose(ISFC, ground_truth).all(), \
        "Calculated ISFC does not match ground truth"
Exemple #4
0
def test_ISFC():
    curr_dir = os.path.dirname(__file__)

    mask_fname = os.path.join(curr_dir, 'mask.nii.gz')
    mask = io.load_boolean_mask(mask_fname)
    fnames = [
        os.path.join(curr_dir, 'subj1.nii.gz'),
        os.path.join(curr_dir, 'subj2.nii.gz')
    ]
    masked_images = image.mask_images(io.load_images(fnames), mask)

    D = image.MaskedMultiSubjectData.from_masked_images(
        masked_images, len(fnames))

    assert D.shape == (4, 5, 2), "Loaded data has incorrect shape"

    (ISFC, p) = brainiak.isfc.isfc(D,
                                   return_p=True,
                                   num_perm=100,
                                   two_sided=True,
                                   random_state=0)

    ground_truth = \
        [[1, 1, 0, -1],
         [1, 1, 0, -1],
         [0, 0, 1,  0],
         [-1, -1, 0, 1]]

    ground_truth_p = 1 - np.abs(ground_truth)

    assert np.isclose(ISFC, ground_truth).all(), \
        "Calculated ISFC does not match ground truth"

    assert np.isclose(p, ground_truth_p).all(), \
        "Calculated p values do not match ground truth"
def test_prepare_fcma_data():
    images = io.load_images_from_dir(data_dir, suffix)
    mask = io.load_boolean_mask(mask_file)
    conditions = io.load_labels(epoch_file)
    raw_data, _, labels = prepare_fcma_data(images, conditions, mask)
    expected_raw_data = np.load(expected_dir / 'expected_raw_data.npy')
    assert len(raw_data) == len(expected_raw_data), \
        'numbers of epochs do not match in test_prepare_fcma_data'
    for idx in range(len(raw_data)):
        assert np.allclose(raw_data[idx], expected_raw_data[idx]), \
            'raw data do not match in test_prepare_fcma_data'
    assert np.array_equal(labels, expected_labels), \
        'the labels do not match in test_prepare_fcma_data'
def test_prepare_mvpa_data():
    images = io.load_images_from_dir(data_dir, suffix=suffix)
    mask = io.load_boolean_mask(mask_file)
    conditions = io.load_labels(epoch_file)
    processed_data, labels = prepare_mvpa_data(images, conditions, mask)
    expected_processed_data = np.load(expected_dir
                                      / 'expected_processed_data.npy')
    assert len(processed_data) == len(expected_processed_data), \
        'numbers of epochs do not match in test_prepare_mvpa_data'
    for idx in range(len(processed_data)):
        assert np.allclose(processed_data[idx],
                           expected_processed_data[idx]), (
            'raw data do not match in test_prepare_mvpa_data')
    assert np.array_equal(labels, expected_labels), \
        'the labels do not match in test_prepare_mvpa_data'
def test_prepare_fcma_data():
    images = io.load_images_from_dir(data_dir, suffix=suffix)
    mask = io.load_boolean_mask(mask_file)
    conditions = io.load_labels(epoch_file)
    raw_data, _, labels = prepare_fcma_data(images, conditions, mask)
    expected_raw_data = np.load(expected_dir / 'expected_raw_data.npy')
    assert len(raw_data) == len(expected_raw_data), \
        'numbers of epochs do not match in test_prepare_fcma_data'
    for idx in range(len(raw_data)):
        assert np.allclose(raw_data[idx], expected_raw_data[idx]), \
            'raw data do not match in test_prepare_fcma_data'
    assert np.array_equal(labels, expected_labels), \
        'the labels do not match in test_prepare_fcma_data'
    from brainiak.fcma.preprocessing import RandomType
    images = io.load_images_from_dir(data_dir, suffix=suffix)
    random_raw_data, _, _ = prepare_fcma_data(images, conditions, mask,
                                              random=RandomType.REPRODUCIBLE)
    assert len(random_raw_data) == len(expected_raw_data), \
        'numbers of epochs do not match in test_prepare_fcma_data'
    images = io.load_images_from_dir(data_dir, suffix=suffix)
    random_raw_data, _, _ = prepare_fcma_data(images, conditions, mask,
                                              random=RandomType.UNREPRODUCIBLE)
    assert len(random_raw_data) == len(expected_raw_data), \
        'numbers of epochs do not match in test_prepare_fcma_data'
Exemple #8
0
def test_ISFC():
    curr_dir = os.path.dirname(__file__)

    mask_fname = os.path.join(curr_dir, 'mask.nii.gz')
    mask = io.load_boolean_mask(mask_fname)
    fnames = [os.path.join(curr_dir, 'subj1.nii.gz'),
              os.path.join(curr_dir, 'subj2.nii.gz')]
    masked_images = image.mask_images(io.load_images(fnames), mask)

    D = image.MaskedMultiSubjectData.from_masked_images(masked_images,
                                                        len(fnames))

    assert D.shape == (4, 5, 2), "Loaded data has incorrect shape"

    ISFC = brainiak.isfc.isfc(D)

    ground_truth = \
        [[1, 1, 0, -1],
         [1, 1, 0, -1],
         [0, 0, 1,  0],
         [-1, -1, 0, 1]]

    assert np.isclose(ISFC, ground_truth).all(), \
        "Calculated ISFC does not match ground truth"
Exemple #9
0
import nibabel as nib
from brainiak import image, io
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import fcluster, linkage

curr_dir = dirname(abspath("__file__"))

mask_fn = join(curr_dir, 'avg152T1_gray_3mm.nii.gz')
func_fns = [
    join(curr_dir, 'sub-{0:03d}-task-intact1.nii.gz'.format(sub))
    for sub in np.arange(1, 6)
]

print('Loading data from {0} subjects...'.format(len(func_fns)))

mask_image = io.load_boolean_mask(mask_fn, lambda x: x > 50)
masked_images = image.mask_images(io.load_images(func_fns), mask_image)
coords = np.where(mask_image)
data = image.MaskedMultiSubjectData.from_masked_images(masked_images,
                                                       len(func_fns))

print('Calculating mean ISC on {0} voxels'.format(data.shape[1]))
iscs = isc(data, pairwise=False, summary_statistic='mean')
iscs = np.nan_to_num(iscs)

print('Writing ISC map to file...')
nii_template = nib.load(mask_fn)
isc_vol = np.zeros(nii_template.shape)
isc_vol[coords] = iscs
isc_image = nib.Nifti1Image(isc_vol, nii_template.affine, nii_template.header)
nib.save(isc_image, 'example_isc.nii.gz')
Exemple #10
0
    return masked_data_all, nacc_coords, vta_coords


# In[43]:

#load data w/ ROI masks
masked_data_all, nacc_coords, vta_coords = load_ss_masked_data(
    out_dir, sub, ROIs)

# In[44]:

#some plotting things to visualize ROIs
avg_mask_name = results_path + 'avg_brain_mask.nii.gz'
#load in mask as boolean
brain_mask = io.load_boolean_mask(avg_mask_name)  # Load the brain mask
coords = np.where(brain_mask)  # Get the list of nonzero voxel coordinates
brain_nii = nib.load(avg_mask_name)  # Load the brain nii image
print(nacc_coords.shape)
xmidpt = ((nacc_coords.shape[0] - 1) / 2) + 1
ymidpt = ((nacc_coords.shape[1] - 1) / 2) + 1
zmidpt = ((nacc_coords.shape[2] - 1) / 2) + 1

if ipynby == 1:
    f, ax = plt.subplots(1, 3, figsize=(6, 2))
    ax[0].imshow(brain_nii.dataobj[xmidpt, :, :],
                 cmap='gray',
                 origin='lower',
                 interpolation='none',
                 aspect="auto")
    ax[1].imshow(brain_nii.dataobj[:, ymidpt, :],
Exemple #11
0
import numpy as np
from matplotlib import pyplot as plt
from scipy.cluster.hierarchy import fcluster, linkage
import sys
import os

curr_dir = os.path.dirname(__file__)

brain_fname = os.path.join(curr_dir,'avg152T1_gray_3mm.nii.gz')
fnames = [os.path.join(curr_dir,
          'sub-0' + format(subj, '02') + '-task-intact1.nii.gz') for
          subj in np.arange(1, 5)]

print('Loading data from ', len(fnames), ' subjects...')

brain_mask = io.load_boolean_mask(brain_fname, lambda x: x > 50)
masked_images = image.mask_images(io.load_images(fnames), brain_mask)
coords = np.where(brain_mask)
D = image.MaskedMultiSubjectData.from_masked_images(masked_images, len(fnames))

print('Calculating ISC on ', D.shape[0], ' voxels')
ISC = brainiak.isfc.isc(D)
ISC[np.isnan(ISC)] = 0

print('Writing ISC map to file...')
brain_nii = nib.load(brain_fname)
ISC_vol = np.zeros(brain_nii.shape)
ISC_vol[coords] = ISC
ISC_nifti = nib.Nifti1Image(ISC_vol, brain_nii.affine, brain_nii.header)
nib.save(ISC_nifti, 'ISC.nii.gz')
def test_load_boolean_mask_predicate(mask_path: Path) -> None:
    mask = io.load_boolean_mask(mask_path, lambda x: np.logical_not(x))
    expected_mask = np.logical_not(io.load_boolean_mask(mask_path))
    assert np.array_equal(mask, expected_mask)
if __name__ == '__main__':
    if len(sys.argv) != 5:
        logger.error('the number of input argument is not correct')
        sys.exit(1)

    data_dir = sys.argv[1]
    extension = sys.argv[2]
    mask_file = sys.argv[3]
    epoch_file = sys.argv[4]

    epoch_list = np.load(epoch_file)
    num_subjects = len(epoch_list)
    num_epochs_per_subj = epoch_list[0].shape[1]

    images = io.load_images_from_dir(data_dir, extension)
    mask = io.load_boolean_mask(mask_file)
    conditions = io.load_labels(epoch_file)
    raw_data, _, labels = prepare_fcma_data(images, conditions, mask)

    example_of_aggregating_sim_matrix(raw_data, labels, num_subjects, num_epochs_per_subj)

    example_of_cross_validation_with_detailed_info(raw_data, labels, num_subjects, num_epochs_per_subj)

    example_of_cross_validation_using_model_selection(raw_data, labels, num_subjects, num_epochs_per_subj)

    # test of two different components for correlation computation
    # images = io.load_images_from_dir(data_dir, extension)
    # mask2 = io.load_boolean_mask('face_scene/visual_top_mask.nii.gz')
    # raw_data, raw_data2, labels = prepare_fcma_data(images, conditions, mask,
    #                                                 mask2)
    #example_of_correlating_two_components(raw_data, raw_data2, labels, num_subjects, num_epochs_per_subj)
Exemple #14
0
subj = sys.argv[2]

# 3. How many features will be learned?
num_feats = sys.argv[3]
num_feats = int(num_feats)  #turn to integer

# 4. What order do you want the training and test set? Original or swapped?
trainset = sys.argv[4]

# 5. Are you running the main analyses (collapse across all features) or want to reconstruct each feature seperatly (note, depending on number of features, this may be time-intensive)
combine = sys.argv[5]

#############################################################

# load the brain mask (whose name is specified in SR_utils.py)
brain_mask = io.load_boolean_mask(mask_name)

# load the brain nii image
brain_nii = nib.load(mask_name)

# Make a brain masker from this mask
brain_masker = NiftiMasker(mask_img=brain_nii)
test_sub = nib.load(test_sub)  # Test sub is defined in the SR_utils.py

test_fit = brain_masker.fit(test_sub)
affine_mat = test_sub.affine
dimsize = test_sub.header.get_zooms()

# load in the functional data for the comparison group (which SRM will learn features on)
fnames = {}
images = {}
Exemple #15
0
from scipy.cluster.hierarchy import fcluster, linkage
import sys
import os

curr_dir = os.path.dirname(__file__)

brain_fname = os.path.join(curr_dir, 'avg152T1_gray_3mm.nii.gz')
fnames = [
    os.path.join(curr_dir,
                 'sub-0' + format(subj, '02') + '-task-intact1.nii.gz')
    for subj in np.arange(1, 5)
]

print('Loading data from ', len(fnames), ' subjects...')

brain_mask = io.load_boolean_mask(brain_fname, lambda x: x > 50)
masked_images = image.mask_images(io.load_images(fnames), brain_mask)
coords = np.where(brain_mask)
D = image.MaskedMultiSubjectData.from_masked_images(masked_images, len(fnames))

print('Calculating ISC on ', D.shape[0], ' voxels')
ISC = brainiak.isfc.isc(D)
ISC[np.isnan(ISC)] = 0

print('Writing ISC map to file...')
brain_nii = nib.load(brain_fname)
ISC_vol = np.zeros(brain_nii.shape)
ISC_vol[coords] = ISC
ISC_nifti = nib.Nifti1Image(ISC_vol, brain_nii.affine, brain_nii.header)
nib.save(ISC_nifti, 'ISC.nii.gz')
Exemple #16
0
    is_extrinsic = 0
else:
    is_extrinsic = 1  
 
if __name__ == '__main__':
    
    # Send a message on the first node
    if MPI.COMM_WORLD.Get_rank()==0:
        logger.info(
            'Testing for participant %d.\nProgramming starts in %d process(es)' %
            (int(left_out_subj), MPI.COMM_WORLD.Get_size())
        )
    
    # Load in the volumes, mask and labels
    images = io.load_images_from_dir(data_dir, suffix=suffix)
    top_n_mask = io.load_boolean_mask(top_n_mask_file)
    epoch_list = io.load_labels(epoch_file)

    # Parse the epoch data for useful dimensions   
    epochs_per_subj = epochs_per_subj = epoch_list[0].shape[1]
    num_subjs = len(epoch_list)
    
    # Prepare the data
    int_data, _, labels = prepare_fcma_data(images, epoch_list, top_n_mask)
    
    # What indexes pick out the left out participant?
    start_idx = int(int(left_out_subj) * epochs_per_subj)
    end_idx = int(start_idx + epochs_per_subj)
    
    # Take out the idxs corresponding to all participants but this one
    training_idx = list(set(range(len(labels))) - set(range(start_idx, end_idx)))
Exemple #17
0
import nibabel as nib
from brainiak import image, io
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import fcluster, linkage


curr_dir = dirname(abspath("__file__"))

mask_fn = join(curr_dir,'avg152T1_gray_3mm.nii.gz')
func_fns = [join(curr_dir,
                 'sub-{0:03d}-task-intact1.nii.gz'.format(sub))
            for sub in np.arange(1, 6)]

print('Loading data from {0} subjects...'.format(len(func_fns)))

mask_image = io.load_boolean_mask(mask_fn, lambda x: x > 50)
masked_images = image.mask_images(io.load_images(func_fns),
                                  mask_image)
coords = np.where(mask_image)
data = image.MaskedMultiSubjectData.from_masked_images(masked_images,
                                                       len(func_fns))

print('Calculating mean ISC on {0} voxels'.format(data.shape[1]))
iscs = isc(data, pairwise=False, summary_statistic='mean')
iscs = np.nan_to_num(iscs)

print('Writing ISC map to file...')
nii_template = nib.load(mask_fn)
isc_vol = np.zeros(nii_template.shape)
isc_vol[coords] = iscs
isc_image = nib.Nifti1Image(isc_vol, nii_template.affine,
Exemple #18
0
def test_load_boolean_mask(mask_path: Path) -> None:
    mask = io.load_boolean_mask(mask_path)
    assert mask.dtype == np.bool
Exemple #19
0
# replace "stream=sys.stdout" with "filename='fcma.log'"
logging.basicConfig(level=logging.INFO, format=format, stream=sys.stdout)
logger = logging.getLogger(__name__)
"""
example running command in run_voxel_selection.sh
"""
if __name__ == '__main__':
    if MPI.COMM_WORLD.Get_rank() == 0:
        logger.info('programming starts in %d process(es)' %
                    MPI.COMM_WORLD.Get_size())
    data_dir = sys.argv[1]
    suffix = sys.argv[2]
    mask_file = sys.argv[3]
    epoch_file = sys.argv[4]
    images = io.load_images_from_dir(data_dir, suffix=suffix)
    mask = io.load_boolean_mask(mask_file)
    conditions = io.load_labels(epoch_file)
    raw_data, _, labels = prepare_fcma_data(images, conditions, mask)

    # setting the random argument produces random voxel selection results
    # for non-parametric statistical analysis.
    # There are three random options:
    # RandomType.NORANDOM is the default
    # RandomType.REPRODUCIBLE permutes the voxels in the same way every run
    # RandomType.UNREPRODUCIBLE permutes the voxels differently across runs
    # example:
    # from brainiak.fcma.preprocessing import RandomType
    # raw_data, _, labels = prepare_fcma_data(images, conditions, mask,
    #                                         random=RandomType.REPRODUCIBLE)

    # if providing two masks, just append the second mask as the last input argument
Exemple #20
0
def test_load_boolean_mask(mask_path: Path) -> None:
    mask = io.load_boolean_mask(mask_path)
    assert mask.dtype == np.bool