def test_is_image(): # tests for tests for image img = load_image(anatfile) yield assert_true(is_image(img)) class C(object): pass yield assert_false(is_image(C())) class C(object): def __array__(self): pass yield assert_false(is_image(C())) class C(object): coordmap = None def __array__(self): pass yield assert_true(is_image(img))
def as_image(image_input): ''' Load image from filename or pass through image instance Parameters ---------- image_input : str or Image instance image or string filename of image. If a string, load image and return. If an image, pass through without modification Returns ------- img : Image or Image-like instance Input object if `image_input` seemed to be an image, loaded Image object if `image_input` was a string. Raises ------ TypeError : if neither string nor image-like passed Examples -------- >>> from nipy.testing import anatfile >>> from nipy.io.api import load_image >>> img = as_image(anatfile) >>> img2 = as_image(img) >>> img2 is img True ''' if is_image(image_input): return image_input if isinstance(image_input, basestring): return load(image_input) raise TypeError('Expecting an image-like object or filename string')
def group_analysis_signs(design, contrast, mask, signs=None): """ Refit the EM model with a vector of signs. Used in the permutation tests. Returns the maximum of the T-statistic within mask Parameters ---------- design: one of 'block', 'event' contrast: str name of contrast to estimate mask : ``Image`` instance or array-like image containing mask, or array-like signs: ndarray, optional Defaults to np.ones. Should have shape (*,nsubj) where nsubj is the number of effects combined in the group analysis. Returns ------- minT: np.ndarray, minima of T statistic within mask, one for each vector of signs maxT: np.ndarray, maxima of T statistic within mask, one for each vector of signs """ if api.is_image(mask): maska = mask.get_data() else: maska = np.asarray(mask) maska = maska.astype(np.bool) # Which subjects have this (contrast, design) pair? subj_con_dirs = futil.subj_des_con_dirs(design, contrast) # Assemble effects and sds into 4D arrays sds = [] Ys = [] for s in subj_con_dirs: sd_img = load_image(pjoin(s, "sd.nii")) effect_img = load_image(pjoin(s, "effect.nii")) sds.append(sd_img.get_data()[maska]) Ys.append(effect_img.get_data()[maska]) sd = np.array(sds) Y = np.array(Ys) if signs is None: signs = np.ones((1, Y.shape[0])) maxT = np.empty(signs.shape[0]) minT = np.empty(signs.shape[0]) for i, sign in enumerate(signs): signY = sign[:,np.newaxis] * Y varest = onesample.estimate_varatio(signY, sd) random_var = varest['random'] adjusted_var = sd**2 + random_var adjusted_sd = np.sqrt(adjusted_var) results = onesample.estimate_mean(Y, adjusted_sd) T = results['t'] minT[i], maxT[i] = np.nanmin(T), np.nanmax(T) return minT, maxT
def group_analysis_signs(design, contrast, mask, signs=None): """ Refit the EM model with a vector of signs. Used in the permutation tests. Returns the maximum of the T-statistic within mask Parameters ---------- design: one of 'block', 'event' contrast: str name of contrast to estimate mask : ``Image`` instance or array-like image containing mask, or array-like signs: ndarray, optional Defaults to np.ones. Should have shape (*,nsubj) where nsubj is the number of effects combined in the group analysis. Returns ------- minT: np.ndarray, minima of T statistic within mask, one for each vector of signs maxT: np.ndarray, maxima of T statistic within mask, one for each vector of signs """ if api.is_image(mask): maska = mask.get_data() else: maska = np.asarray(mask) maska = maska.astype(np.bool) # Which subjects have this (contrast, design) pair? subj_con_dirs = futil.subj_des_con_dirs(design, contrast) # Assemble effects and sds into 4D arrays sds = [] Ys = [] for s in subj_con_dirs: sd_img = load_image(pjoin(s, "sd.nii")) effect_img = load_image(pjoin(s, "effect.nii")) sds.append(sd_img.get_data()[maska]) Ys.append(effect_img.get_data()[maska]) sd = np.array(sds) Y = np.array(Ys) if signs is None: signs = np.ones((1, Y.shape[0])) maxT = np.empty(signs.shape[0]) minT = np.empty(signs.shape[0]) for i, sign in enumerate(signs): signY = sign[:, np.newaxis] * Y varest = onesample.estimate_varatio(signY, sd) random_var = varest['random'] adjusted_var = sd**2 + random_var adjusted_sd = np.sqrt(adjusted_var) results = onesample.estimate_mean(Y, adjusted_sd) T = results['t'] minT[i], maxT[i] = np.nanmin(T), np.nanmax(T) return minT, maxT