Example #1
0
def twosample_test(data_images,
                   vardata_images,
                   mask_images,
                   labels,
                   stat_id,
                   permutations=0,
                   cluster_forming_th=0.01):
    """
    Helper function for permutation-based mass univariate twosample group
    analysis. Labels is a binary vector (1-2). Regions more active for group
    1 than group 2 are inferred.
    """
    # Prepare arrays
    data, vardata, xyz, mask = prepare_arrays(data_images, vardata_images,
                                              mask_images)

    # Create two-sample permutation test instance
    if vardata_images is None:
        ptest = permutation_test_twosample(data[labels == 1],
                                           data[labels == 2],
                                           xyz,
                                           stat_id=stat_id)
    else:
        ptest = permutation_test_twosample(data[labels == 1],
                                           data[labels == 2],
                                           xyz,
                                           vardata1=vardata[labels == 1],
                                           vardata2=vardata[labels == 2],
                                           stat_id=stat_id)

    # Compute z-map image
    zmap = np.zeros(data_images[0].shape).squeeze()
    zmap[list(xyz)] = ptest.zscore()
    zimg = Image(zmap, get_affine(data_images[0]))

    # Compute mask image
    maskimg = Image(mask, get_affine(data_images[0]))

    # Multiple comparisons
    if permutations <= 0:
        return zimg, maskimg
    else:
        # Cluster definition: (threshold, diameter)
        cluster_def = (ptest.height_threshold(cluster_forming_th), None)

        # Calibration
        voxel_res, cluster_res, region_res = \
            ptest.calibrate(nperms=permutations, clusters=[cluster_def])
        nulls = {}
        nulls['zmax'] = ptest.zscore(voxel_res['perm_maxT_values'])
        nulls['s'] = cluster_res[0]['perm_size_values']
        nulls['smax'] = cluster_res[0]['perm_maxsize_values']

        # Return z-map image, mask image and dictionary of null
        # distribution for cluster sizes (s), max cluster size (smax)
        # and max z-score (zmax)
        return zimg, maskimg, nulls
Example #2
0
def onesample_test(data_images,
                   vardata_images,
                   mask_images,
                   stat_id,
                   permutations=0,
                   cluster_forming_th=0.01):
    """
    Helper function for permutation-based mass univariate onesample
    group analysis.
    """
    # Prepare arrays
    data, vardata, xyz, mask = prepare_arrays(data_images, vardata_images,
                                              mask_images)

    # Create one-sample permutation test instance
    ptest = permutation_test_onesample(data,
                                       xyz,
                                       vardata=vardata,
                                       stat_id=stat_id)

    # Compute z-map image
    zmap = np.zeros(data_images[0].shape).squeeze()
    zmap[list(xyz)] = ptest.zscore()
    zimg = Image(zmap, get_affine(data_images[0]))

    # Compute mask image
    maskimg = Image(mask.astype(np.int8), get_affine(data_images[0]))

    # Multiple comparisons
    if permutations <= 0:
        return zimg, maskimg
    else:
        # Cluster definition: (threshold, diameter)
        cluster_def = (ptest.height_threshold(cluster_forming_th), None)

        # Calibration
        voxel_res, cluster_res, region_res = \
            ptest.calibrate(nperms=permutations, clusters=[cluster_def])
        nulls = {}
        nulls['zmax'] = ptest.zscore(voxel_res['perm_maxT_values'])
        nulls['s'] = cluster_res[0]['perm_size_values']
        nulls['smax'] = cluster_res[0]['perm_maxsize_values']

        # Return z-map image, mask image and dictionary of null distribution
        # for cluster sizes (s), max cluster size (smax) and max z-score (zmax)
        return zimg, maskimg, nulls
Example #3
0
def remake_images():
    # Get group data
    group_data = example_data.get_filename(
        'neurospin', 'language_babies', 'offset_002.npz')
    f = np.load(group_data)
    data, vardata, xyz = f['mat'], f['var'], f['xyz']
    dX = xyz[0].max() + 1
    dY = xyz[1].max() + 1
    dZ = xyz[2].max() + 1
    aux = np.zeros([dX, dY, dZ])
    data_images = []
    vardata_images = []
    mask_images = []
    for i in range(data.shape[0]):
        aux[list(xyz)] = data[i]
        data_images.append(Image(aux.copy(), np.eye(4)))
        aux[list(xyz)] = vardata[i]
        vardata_images.append(Image(aux.copy(), np.eye(4)))
        aux[list(xyz)] = 1
        mask_images.append(aux)

    return data_images, vardata_images, mask_images
Example #4
0
    def contrast(self, vector):
        """Compute images of contrast and contrast variance.
        """
        # Compute the overall contrast across models
        c = self.glm[0].contrast(vector)
        for g in self.glm[1:]:
            c += g.contrast(vector)

        def affect_inmask(dest, src, xyz):
            if xyz is None:
                dest = src
            else:
                dest[xyz] = src
            return dest

        con = np.zeros(self.spatial_shape)
        con_img = Image(affect_inmask(con, c.effect, self.xyz), self.affine)
        vcon = np.zeros(self.spatial_shape)
        vcon_img = Image(affect_inmask(vcon, c.variance, self.xyz),
                         self.affine)
        z = np.zeros(self.spatial_shape)
        z_img = Image(affect_inmask(z, c.zscore(), self.xyz), self.affine)
        dof = c.dof
        return con_img, vcon_img, z_img, dof
Example #5
0
def linear_model_fit(data_images, mask_images, design_matrix, vector):
    """
    Helper function for group data analysis using arbitrary design matrix
    """

    # Prepare arrays
    data, vardata, xyz, mask = prepare_arrays(data_images, None, mask_images)

    # Create glm instance
    G = glm(data, design_matrix)

    # Compute requested contrast
    c = G.contrast(vector)

    # Compute z-map image
    zmap = np.zeros(data_images[0].shape).squeeze()
    zmap[list(xyz)] = c.zscore()
    zimg = Image(zmap, get_affine(data_images[0]))

    return zimg