Example #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 = 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"
Example #2
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"
Example #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, 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"
Example #4
0
def test_load_images_data_shape(
        image_paths: Iterable[Path],
        expected_image_data_shape: Sequence[int],
        expected_n_subjects: int
        ) -> None:
    for i, image in enumerate(io.load_images(image_paths)):
        assert image.get_data().shape == (64, 64, 26, 10)
    assert i + 1 == expected_n_subjects
Example #5
0
def boldfiles_to_arrays(boldfiles, z_score=True):
    """
    Load bold data into list of arrays (bc that's what brainiak wants)
    """
    # create generator that returns nibabel nifti instances
    nibs_gen = load_images(boldfiles)
    # get numpy array data from each those instances (might take a while)
    bold_arrays_list = [
        np.reshape(nib_instance.get_fdata(),
                   (1327104, 256))  # TODO: don't hard code this
        for nib_instance in nibs_gen
    ]
    if z_score:
        zscored = []
        for bold_array in bold_arrays_list:
            zs = stats.zscore(bold_array, axis=1, ddof=1)
            zs = np.nan_to_num(zs)
            zscored.append(zs)
            # TODO: put z-scoring in preprocessing pipeline later
        bold_arrays_list = zscored

    return bold_arrays_list
Example #6
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"
Example #7
0
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)
Example #8
0
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')
Example #9
0
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')

ISC_mask = ISC > 0.2
Example #10
0
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 = {}
masked_images = {}
bold_base = {}
n_subjs_base = {}

fnames = get_file_names(data_dir_, which_base.lower())

images = io.load_images(fnames)
masked_images = image.mask_images(images, brain_mask)

# Concatenate all of the masked images across participants
bold_base = image.MaskedMultiSubjectData.from_masked_images(
    masked_images, len(fnames))

# Convert nans into zeros
bold_base[np.isnan(bold_base)] = 0

# reshape the data
bold_base = np.transpose(bold_base, [1, 0, 2])

n_subjs_base = np.shape(bold_base)[-1]
print("")
print(f'Base data loaded: {which_base}\t shape: {np.shape(bold_base)}')
Example #11
0
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')

ISC_mask = ISC > 0.2