Esempio n. 1
0
def apply_repro_analysis(dataset, thresholds=[3.0], method='crfx'):
    """
    perform the reproducibility  analysis according to the 
    """
    from nipy.labs.spatial_models.discrete_domain import \
        grid_domain_from_binary_array

    n_subj, dimx, dimy = dataset.shape

    func = np.reshape(dataset, (n_subj, dimx * dimy)).T
    var = np.ones((dimx * dimy, n_subj))
    domain = grid_domain_from_binary_array(np.ones((dimx, dimy, 1)))

    ngroups = 5
    sigma = 2.0
    csize = 10
    niter = 5
    verbose = 0
    swap = False

    kap, clt, pkd = [], [], []
    for threshold in thresholds:
        kappa, cls, pks = [], [], []
        kwargs = {'threshold': threshold, 'csize': csize}
        for i in range(niter):
            k = voxel_reproducibility(func, var, domain, ngroups, method, swap,
                                      verbose, **kwargs)
            kappa.append(k)
            cld = cluster_reproducibility(func, var, domain, ngroups, sigma,
                                          method, swap, verbose, **kwargs)
            cls.append(cld)
            pk = peak_reproducibility(func, var, domain, ngroups, sigma,
                                      method, swap, verbose, **kwargs)
            pks.append(pk)

        kap.append(np.array(kappa))
        clt.append(np.array(cls))
        pkd.append(np.array(pks))
    kap = np.array(kap)
    clt = np.array(clt)
    pkd = np.array(pkd)
    return kap, clt, pkd
Esempio n. 2
0
def apply_repro_analysis(dataset, thresholds=[3.0], method = 'crfx'):
    """
    perform the reproducibility  analysis according to the 
    """
    from nipy.labs.spatial_models.discrete_domain import \
        grid_domain_from_binary_array

    n_subj, dimx, dimy = dataset.shape
    
    func = np.reshape(dataset,(n_subj, dimx * dimy)).T
    var = np.ones((dimx * dimy, n_subj))
    domain = grid_domain_from_binary_array(np.ones((dimx, dimy, 1)))

    ngroups = 5
    sigma = 2.0
    csize = 10
    niter = 5
    verbose = 0
    swap = False

    kap, clt, pkd = [], [], []
    for threshold in thresholds:
        kappa, cls, pks = [], [], []
        kwargs = {'threshold':threshold, 'csize':csize}        
        for i in range(niter):
            k = voxel_reproducibility(func, var, domain, ngroups,
                                  method, swap, verbose, **kwargs)
            kappa.append(k)
            cld = cluster_reproducibility(func, var, domain, ngroups, sigma,
                                      method, swap, verbose, **kwargs)
            cls.append(cld)
            pk = peak_reproducibility(func, var, domain, ngroups, sigma,
                                      method, swap, verbose, **kwargs)
            pks.append(pk)
        
        kap.append(np.array(kappa))
        clt.append(np.array(cls))
        pkd.append(np.array(pks))
    kap = np.array(kap)
    clt = np.array(clt)
    pkd = np.array(pkd)
    return kap, clt, pkd
)
from nipy.labs.spatial_models.discrete_domain import grid_domain_from_binary_array

###############################################################################
# Generate the data
n_subj = 105
shape = (60, 60)
pos = np.array([[12, 14], [20, 20], [30, 20]])
ampli = np.array([2.5, 3.5, 3])
betas = simul.surrogate_2d_dataset(n_subj=n_subj, shape=shape, pos=pos, ampli=ampli, width=5.0)

n_vox = np.prod(shape)
# set the variance at 1 everywhere
func = np.reshape(betas, (n_subj, n_vox)).T
var = np.ones((n_vox, n_subj))
domain = grid_domain_from_binary_array(np.ones((shape[0], shape[1], 1)))

###############################################################################
# Run reproducibility analysis

ngroups = 10
thresholds = np.arange(0.5, 6.0, 0.5)
sigma = 2.0
csize = 10
niter = 10
method = "crfx"
verbose = 0

# do not use permutations
swap = False
def cc_array_mask(mask, size_threshold):
    """Filter the mask to keep only connected components above a given size"""
    graph = wgraph_from_coo_matrix(grid_domain_from_binary_array(mask).topology)
    mask[mask] = cc_mask(graph, mask[mask], size_threshold)
    return mask
def group_reproducibility_metrics(mask_images,
                                  contrast_images,
                                  variance_images,
                                  thresholds,
                                  ngroups,
                                  method,
                                  cluster_threshold=10,
                                  number_of_samples=10,
                                  sigma=6.,
                                  do_clusters=True,
                                  do_voxels=True,
                                  do_peaks=True,
                                  swap=False):
    """
    Main function to perform reproducibility analysis, including nifti1 io

    Parameters
    ----------
    threshold: list or 1-d array,
               the thresholds to be tested

    Returns
    -------
    cluster_rep_results: dictionary,
                         results of cluster-level reproducibility analysis
    voxel_rep_results: dictionary,
                       results of voxel-level reproducibility analysis
    peak_rep_results: dictionary,
                      results of peak-level reproducibility analysis
    """
    from nibabel import load
    from ..mask import intersect_masks

    if ((len(variance_images) == 0) & (method is not 'crfx')):
        raise ValueError('Variance images are necessary')

    nsubj = len(contrast_images)

    # compute the group mask
    affine = get_affine(load(mask_images[0]))
    mask = intersect_masks(mask_images, threshold=0) > 0
    domain = grid_domain_from_binary_array(mask, affine)

    # read the data
    group_con = []
    group_var = []
    for s in range(nsubj):
        group_con.append(load(contrast_images[s]).get_data()[mask])
        if len(variance_images) > 0:
            group_var.append(load(variance_images[s]).get_data()[mask])

    group_con = np.squeeze(np.array(group_con)).T
    group_con[np.isnan(group_con)] = 0
    if len(variance_images) > 0:
        group_var = np.squeeze(np.array(group_var)).T
        group_var[np.isnan(group_var)] = 0
        group_var = np.maximum(group_var, 1.e-15)

    # perform the analysis
    voxel_rep_results = {}
    cluster_rep_results = {}
    peak_rep_results = {}

    for ng in ngroups:
        if do_voxels:
            voxel_rep_results.update({ng: {}})
        if do_clusters:
            cluster_rep_results.update({ng: {}})
        if do_peaks:
            peak_rep_results.update({ng: {}})
        for th in thresholds:
            kappa = []
            cls = []
            pk = []
            kwargs = {'threshold': th, 'csize': cluster_threshold}

            for i in range(number_of_samples):
                if do_voxels:
                    kappa.append(
                        voxel_reproducibility(group_con, group_var, domain, ng,
                                              method, swap, **kwargs))
                if do_clusters:
                    cls.append(
                        cluster_reproducibility(group_con, group_var, domain,
                                                ng, sigma, method, swap,
                                                **kwargs))
                if do_peaks:
                    pk.append(
                        peak_reproducibility(group_con, group_var, domain, ng,
                                             sigma, method, swap, **kwargs))

            if do_voxels:
                voxel_rep_results[ng].update({th: np.array(kappa)})
            if do_clusters:
                cluster_rep_results[ng].update({th: np.array(cls)})
            if do_peaks:
                peak_rep_results[ng].update({th: np.array(cls)})

    return voxel_rep_results, cluster_rep_results, peak_rep_results
Esempio n. 6
0
# step 1:  generate some synthetic data
n_subj = 10
shape = (60, 60)
pos = 3 * np.array([[6, 7],
                  [10, 10],
                  [15, 10]])
ampli = np.array([5, 7, 6])
sjitter = 6.0
dataset = simul.surrogate_2d_dataset(n_subj=n_subj, shape=shape, pos=pos, 
                                     ampli=ampli, width=10.0)
# dataset represents 2D activation images from n_subj subjects,

# step 2 : prepare all the information for the parcellation
nbparcel = 10
ldata = np.reshape(dataset, (n_subj, np.prod(shape), 1))
domain = dom.grid_domain_from_binary_array(np.ones(shape))

# step 3 : run the algorithm
Pa = hp.hparcel(domain, ldata, nbparcel, mu=3.0)
# note: play with mu to change the 'stiffness of the parcellation'

# step 4:  look at the results
Label = np.array([np.reshape(Pa.individual_labels[:, s], shape)
                   for s in range(n_subj)])

import matplotlib.pylab as mp
mp.figure(figsize=(8, 4))
mp.title('Input data')
for s in range(n_subj):
    mp.subplot(2, 5, s + 1)
    mp.imshow(dataset[s], interpolation='nearest')
def group_reproducibility_metrics(
    mask_images, contrast_images, variance_images, thresholds, ngroups,
    method, cluster_threshold=10, number_of_samples=10, sigma=6.,
    do_clusters=True, do_voxels=True, do_peaks=True, swap=False):
    """
    Main function to perform reproducibility analysis, including nifti1 io

    Parameters
    ----------
    threshold: list or 1-d array,
               the thresholds to be tested

    Returns
    -------
    cluster_rep_results: dictionary,
                         results of cluster-level reproducibility analysi
    voxel_rep_results: dictionary,
                       results of voxel-level reproducibility analysis
    peak_rep_results: dictionary,
                      results of peak-level reproducibility analysis
    """
    from nibabel import load
    from ..mask import intersect_masks

    if ((len(variance_images) == 0) & (method is not 'crfx')):
        raise ValueError('Variance images are necessary')

    nsubj = len(contrast_images)

    # compute the group mask
    affine = load(mask_images[0]).get_affine()
    mask = intersect_masks(mask_images, threshold=0) > 0
    domain = grid_domain_from_binary_array(mask, affine)

    # read the data
    group_con = []
    group_var = []
    for s in range(nsubj):
        group_con.append(load(contrast_images[s]).get_data()[mask])
        if len(variance_images) > 0:
            group_var.append(load(variance_images[s]).get_data()[mask])

    group_con = np.squeeze(np.array(group_con)).T
    group_con[np.isnan(group_con)] = 0
    if len(variance_images) > 0:
        group_var = np.squeeze(np.array(group_var)).T
        group_var[np.isnan(group_var)] = 0
        group_var = np.maximum(group_var, 1.e-15)

    # perform the analysis
    voxel_rep_results = {}
    cluster_rep_results = {}
    peak_rep_results = {}

    for ng in ngroups:
        if do_voxels:
            voxel_rep_results.update({ng: {}})
        if do_clusters:
            cluster_rep_results.update({ng: {}})
        if do_peaks:
            peak_rep_results.update({ng: {}})
        for th in thresholds:
            kappa = []
            cls = []
            pk = []
            kwargs = {'threshold': th, 'csize': cluster_threshold}

            for i in range(number_of_samples):
                if do_voxels:
                    kappa.append(voxel_reproducibility(
                            group_con, group_var, domain, ng, method, swap,
                            **kwargs))
                if do_clusters:
                    cls.append(cluster_reproducibility(
                            group_con, group_var, domain, ng, sigma, method,
                            swap, **kwargs))
                if do_peaks:
                    pk.append(peak_reproducibility(
                            group_con, group_var, domain, ng, sigma, method,
                            swap, **kwargs))

            if do_voxels:
                voxel_rep_results[ng].update({th: np.array(kappa)})
            if do_clusters:
                cluster_rep_results[ng].update({th: np.array(cls)})
            if do_peaks:
                peak_rep_results[ng].update({th: np.array(cls)})

    return voxel_rep_results, cluster_rep_results, peak_rep_results
###############################################################################
# Generate the data
n_subj = 105
shape = (60, 60)
pos = np.array([[12, 14],
                [20, 20],
                [30, 20]])
ampli = np.array([2.5, 3.5, 3])
betas = simul.surrogate_2d_dataset(n_subj=n_subj, shape=shape, pos=pos,
                                     ampli=ampli, width=5.0)

n_vox = np.prod(shape)
# set the variance at 1 everywhere
func = np.reshape(betas, (n_subj, n_vox)).T
var = np.ones((n_vox, n_subj))
domain = grid_domain_from_binary_array(np.ones((shape[0], shape[1], 1)))

###############################################################################
# Run reproducibility analysis

ngroups = 10
thresholds = np.arange(.5, 6., .5)
sigma = 2.0
csize = 10
niter = 10
method = 'crfx'
verbose = 0

# do not use permutations
swap = False