Example #1
0
def contrast(universe, studies, outroot, **kwargs):
	
	# Split universe into two sets
	print "Splitting the universe in two..."
	inc_studies = list(set(universe.studies) & set(studies))
	inc_idx = np.in1d(universe.studies, inc_studies)
	# Note: sparse matrices don't support proper logical indexing,
	# so get indexes with np.where.
	inc_imgs = universe.data[:,np.where(inc_idx)[0]]
	no_inc_imgs = universe.data[:,np.where(inc_idx==0)[0]]
	
	n_studies = len(universe.studies)

	# Calculate different count variables
	print "Calculating count variables..."
	n_inc = inc_imgs.shape[1]
	print "\t# of studies with inc: %d" % n_inc
	n_no_inc = no_inc_imgs.shape[1]
	print "\t# of studies without inc: %d" % n_no_inc
	n_inc_act = np.asarray(spmatrix.sum(inc_imgs, 1))
	n_no_inc_act = np.asarray(spmatrix.sum(no_inc_imgs, 1))
	p_inc = (n_inc+1.0)/(n_studies+2)

	# P(A|T), P(A|~T), and P(T|A), with Laplace smoothing
	print "Calculating conditional probabilities..."
	p_act = np.asarray(spmatrix.mean(universe.data, 1)) + 1.0/n_studies
	p_act_inc = (n_inc_act+1.0)/(n_inc+2)
	p_act_no_inc = (n_no_inc_act+1.0)/(n_no_inc+2)
	pp = p_act_inc * p_inc / p_act
	
	# Recompute P(A) and P(T|A) with uniform priors
	print "Recomputing with uniform priors..."
	pt_unif = kwargs.get('prior', 0.5)
	pa_unif = pt_unif * p_act_inc + 0.5 * p_act_no_inc
	pp_unif = p_act_inc * pt_unif / pa_unif

	# Get p and z values
	print "Computing and saving statistical maps..."

	# Posterior prob. w/ and w/o uniform prior
	imageutils.save_img(pp, outroot + '_pp.nii.gz', mask_inds=universe.vox_kept)  # Ugly; need to make masking object
	imageutils.save_img(pp_unif, outroot + '_pp_unif.nii.gz', mask_inds=universe.vox_kept)
	
	fdr_q = kwargs.get('fdr_q', 0.05)

	# One-way chi-square test for consistency of activation
	p_vals = stats.one_way(np.squeeze(n_inc_act), n_inc)
	p_vals[p_vals < 1e-240] = 1e-240  # prevents overflow
	z_sign = np.sign(n_inc_act - np.mean(n_inc_act)).ravel()
	z_vals = np.abs(norm.ppf(p_vals/2)) * z_sign
	imageutils.save_img(z_vals, outroot + '_fi_z.nii.gz', mask_inds=universe.vox_kept)
	below = np.where(stats.fdr(p_vals, fdr_q)==False)
	z_vals[below] = 0
	imageutils.save_img(z_vals, outroot + '_fi_z_fdr.nii.gz', mask_inds=universe.vox_kept)

	# Two-way chi-square for specificity of activation
	cells = np.squeeze(np.array([[n_inc_act, n_no_inc_act], [n_inc-n_inc_act, n_no_inc-n_no_inc_act]]).T)
	p_vals = stats.two_way(cells)
	p_vals[p_vals < 1e-240] = 1e-240  # prevents overflow
	z_sign = np.sign(p_act_inc - p_act_no_inc).ravel()
	z_vals = np.abs(norm.ppf(p_vals/2)) * z_sign
	imageutils.save_img(z_vals, outroot + '_ri_z.nii.gz', mask_inds=universe.vox_kept)
	below = np.where(stats.fdr(p_vals, fdr_q)==False)
	z_vals[below] = 0
	imageutils.save_img(z_vals, outroot + '_ri_z_fdr.nii.gz', mask_inds=universe.vox_kept)
	pp_unif[below] = 0
	imageutils.save_img(pp_unif, outroot + '_pp_unif_fdr.nii.gz', mask_inds=universe.vox_kept)
	
Example #2
0
 def trim_voxels(self, min_studies=0.03):
     means = np.asarray(spmatrix.mean(self.data, 1))
     self.vox_kept = np.where(means >= min_studies)[0]
     self.data = self.data[self.vox_kept, :]
Example #3
0
def contrast(universe, studies, outroot, **kwargs):

    # Split universe into two sets
    print "Splitting the universe in two..."
    inc_studies = list(set(universe.studies) & set(studies))
    inc_idx = np.in1d(universe.studies, inc_studies)
    # Note: sparse matrices don't support proper logical indexing,
    # so get indexes with np.where.
    inc_imgs = universe.data[:, np.where(inc_idx)[0]]
    no_inc_imgs = universe.data[:, np.where(inc_idx == 0)[0]]

    n_studies = len(universe.studies)

    # Calculate different count variables
    print "Calculating count variables..."
    n_inc = inc_imgs.shape[1]
    print "\t# of studies with inc: %d" % n_inc
    n_no_inc = no_inc_imgs.shape[1]
    print "\t# of studies without inc: %d" % n_no_inc
    n_inc_act = np.asarray(spmatrix.sum(inc_imgs, 1))
    n_no_inc_act = np.asarray(spmatrix.sum(no_inc_imgs, 1))
    p_inc = (n_inc + 1.0) / (n_studies + 2)

    # P(A|T), P(A|~T), and P(T|A), with Laplace smoothing
    print "Calculating conditional probabilities..."
    p_act = np.asarray(spmatrix.mean(universe.data, 1)) + 1.0 / n_studies
    p_act_inc = (n_inc_act + 1.0) / (n_inc + 2)
    p_act_no_inc = (n_no_inc_act + 1.0) / (n_no_inc + 2)
    pp = p_act_inc * p_inc / p_act

    # Recompute P(A) and P(T|A) with uniform priors
    print "Recomputing with uniform priors..."
    pt_unif = kwargs.get('prior', 0.5)
    pa_unif = pt_unif * p_act_inc + 0.5 * p_act_no_inc
    pp_unif = p_act_inc * pt_unif / pa_unif

    # Get p and z values
    print "Computing and saving statistical maps..."

    # Posterior prob. w/ and w/o uniform prior
    imageutils.save_img(
        pp, outroot + '_pp.nii.gz',
        mask_inds=universe.vox_kept)  # Ugly; need to make masking object
    imageutils.save_img(pp_unif,
                        outroot + '_pp_unif.nii.gz',
                        mask_inds=universe.vox_kept)

    fdr_q = kwargs.get('fdr_q', 0.05)

    # One-way chi-square test for consistency of activation
    p_vals = stats.one_way(np.squeeze(n_inc_act), n_inc)
    p_vals[p_vals < 1e-240] = 1e-240  # prevents overflow
    z_sign = np.sign(n_inc_act - np.mean(n_inc_act)).ravel()
    z_vals = np.abs(norm.ppf(p_vals / 2)) * z_sign
    imageutils.save_img(z_vals,
                        outroot + '_fi_z.nii.gz',
                        mask_inds=universe.vox_kept)
    below = np.where(stats.fdr(p_vals, fdr_q) == False)
    z_vals[below] = 0
    imageutils.save_img(z_vals,
                        outroot + '_fi_z_fdr.nii.gz',
                        mask_inds=universe.vox_kept)

    # Two-way chi-square for specificity of activation
    cells = np.squeeze(
        np.array([[n_inc_act, n_no_inc_act],
                  [n_inc - n_inc_act, n_no_inc - n_no_inc_act]]).T)
    p_vals = stats.two_way(cells)
    p_vals[p_vals < 1e-240] = 1e-240  # prevents overflow
    z_sign = np.sign(p_act_inc - p_act_no_inc).ravel()
    z_vals = np.abs(norm.ppf(p_vals / 2)) * z_sign
    imageutils.save_img(z_vals,
                        outroot + '_ri_z.nii.gz',
                        mask_inds=universe.vox_kept)
    below = np.where(stats.fdr(p_vals, fdr_q) == False)
    z_vals[below] = 0
    imageutils.save_img(z_vals,
                        outroot + '_ri_z_fdr.nii.gz',
                        mask_inds=universe.vox_kept)
    pp_unif[below] = 0
    imageutils.save_img(pp_unif,
                        outroot + '_pp_unif_fdr.nii.gz',
                        mask_inds=universe.vox_kept)
Example #4
0
	def trim_voxels(self, min_studies=0.03):
		means = np.asarray(spmatrix.mean(self.data, 1))
		self.vox_kept = np.where(means >= min_studies)[0]
		self.data = self.data[self.vox_kept,:]