def similarity(self, image, method='correlation'): """ Calculate similarity of Brain_Data() instance with single Brain_Data or Nibabel image Args: image: Brain_Data or Nibabel instance of weight map Returns: pexp: Outputs a vector of pattern expression values """ if not isinstance(image, Brain_Data): if isinstance(image, nib.Nifti1Image): image = Brain_Data(image) else: raise ValueError( "Image is not a Brain_Data or nibabel instance") dim = image.shape() # Check to make sure masks are the same for each dataset and if not create a union mask # This might be handy code for a new Brain_Data method if np.sum(self.nifti_masker.mask_img.get_data() == 1) != np.sum( image.nifti_masker.mask_img.get_data() == 1): new_mask = intersect_masks( [self.nifti_masker.mask_img, image.nifti_masker.mask_img], threshold=1, connected=False) new_nifti_masker = NiftiMasker(mask_img=new_mask) data2 = new_nifti_masker.fit_transform(self.to_nifti()) image2 = new_nifti_masker.fit_transform(image.to_nifti()) else: data2 = self.data image2 = image.data # Calculate pattern expression if method is 'dot_product': if len(image2.shape) > 1: if image2.shape[0] > 1: pexp = [] for i in range(image2.shape[0]): pexp.append(np.dot(data2, image2[i, :])) pexp = np.array(pexp) else: pexp = np.dot(data2, image2) else: pexp = np.dot(data2, image2) elif method is 'correlation': if len(image2.shape) > 1: if image2.shape[0] > 1: pexp = [] for i in range(image2.shape[0]): pexp.append(pearson(image2[i, :], data2)) pexp = np.array(pexp) else: pexp = pearson(image2, data2) else: pexp = pearson(image2, data2) return pexp
def similarity(self, image, method='correlation'): """ Calculate similarity of Brain_Data() instance with single Brain_Data or Nibabel image Args: self: Brain_Data instance of data to be applied image: Brain_Data or Nibabel instance of weight map Returns: pexp: Outputs a vector of pattern expression values """ if not isinstance(image, Brain_Data): if isinstance(image, nib.Nifti1Image): image = Brain_Data(image) else: raise ValueError("Image is not a Brain_Data or nibabel instance") dim = image.shape() # Check to make sure masks are the same for each dataset and if not create a union mask # This might be handy code for a new Brain_Data method if np.sum(self.nifti_masker.mask_img.get_data()==1)!=np.sum(image.nifti_masker.mask_img.get_data()==1): new_mask = intersect_masks([self.nifti_masker.mask_img, image.nifti_masker.mask_img], threshold=1, connected=False) new_nifti_masker = NiftiMasker(mask_img=new_mask) data2 = new_nifti_masker.fit_transform(self.to_nifti()) image2 = new_nifti_masker.fit_transform(image.to_nifti()) else: data2 = self.data image2 = image.data # Calculate pattern expression if method is 'dot_product': if len(image2.shape) > 1: if image2.shape[0]>1: pexp = [] for i in range(image2.shape[0]): pexp.append(np.dot(data2, image2[i,:])) pexp = np.array(pexp) else: pexp = np.dot(data2, image2) else: pexp = np.dot(data2, image2) elif method is 'correlation': if len(image2.shape) > 1: if image2.shape[0]>1: pexp = [] for i in range(image2.shape[0]): pexp.append(pearson(image2[i,:], data2)) pexp = np.array(pexp) else: pexp = pearson(image2, data2) else: pexp = pearson(image2, data2) return pexp
def apply_mask(data=None, weight_map=None, mask=None, method='dot_product', save_output=False, output_dir='.'): """ Apply Nifti weight map to Nifti Images. Args: data: nibabel instance of data to be applied weight_map: nibabel instance of weight map mask: binary nibabel mask method: type of pattern expression (e.g,. 'dot_product','correlation') save_output: Boolean indicating whether or not to save output to csv file. output_dir: Directory to use for writing all outputs **kwargs: Additional parameters to pass Returns: pexp: Outputs a vector of pattern expression values """ if mask is not None: if type(mask) is not nib.nifti1.Nifti1Image: raise ValueError("Mask is not a nibabel instance") else: mask = nib.load( os.path.join(resource_dir, 'MNI152_T1_2mm_brain_mask_dil.nii.gz')) if type(data) is not nib.nifti1.Nifti1Image: raise ValueError("Data is not a nibabel instance") nifti_masker = NiftiMasker(mask_img=mask) data_masked = nifti_masker.fit_transform(data) if type(weight_map) is not nib.nifti1.Nifti1Image: raise ValueError("Weight_map is not a nibabel instance") weight_map_masked = nifti_masker.fit_transform(weight_map) # Calculate pattern expression if method is 'dot_product': pexp = np.dot(data_masked, np.transpose(weight_map_masked)).squeeze() elif method is 'correlation': pexp = pearson(data_masked, weight_map_masked) if save_output: np.savetxt(os.path.join(output_dir, "Pattern_Expression_" + method + ".csv"), pexp, delimiter=",") return pexp
def apply_mask(data=None, weight_map=None, mask=None, method='dot_product', save_output=False, output_dir='.'): """ Apply Nifti weight map to Nifti Images. Args: data: nibabel instance of data to be applied weight_map: nibabel instance of weight map mask: binary nibabel mask method: type of pattern expression (e.g,. 'dot_product','correlation') save_output: Boolean indicating whether or not to save output to csv file. output_dir: Directory to use for writing all outputs **kwargs: Additional parameters to pass Returns: pexp: Outputs a vector of pattern expression values """ if mask is not None: if type(mask) is not nib.nifti1.Nifti1Image: raise ValueError("Mask is not a nibabel instance") else: mask = nib.load(os.path.join(resource_dir,'MNI152_T1_2mm_brain_mask_dil.nii.gz')) if type(data) is not nib.nifti1.Nifti1Image: raise ValueError("Data is not a nibabel instance") nifti_masker = NiftiMasker(mask_img=mask) data_masked = nifti_masker.fit_transform(data) if type(weight_map) is not nib.nifti1.Nifti1Image: raise ValueError("Weight_map is not a nibabel instance") weight_map_masked = nifti_masker.fit_transform(weight_map) # Calculate pattern expression if method is 'dot_product': pexp = np.dot(data_masked,np.transpose(weight_map_masked)).squeeze() elif method is 'correlation': pexp = pearson(data_masked,weight_map_masked) if save_output: np.savetxt(os.path.join(output_dir,"Pattern_Expression_" + method + ".csv"), pexp, delimiter=",") return pexp
def apply_mask(data=None, weight_map=None, mask=None, method='dot_product', save_output=False, output_dir='.'): """ Apply Nifti weight map to Nifti Images. Args: data: nibabel instance of data to be applied weight_map: nibabel instance of weight map mask: binary nibabel mask method: type of pattern expression (e.g,. 'dot_product','correlation') save_output: Boolean indicating whether or not to save output to csv file. output_dir: Directory to use for writing all outputs **kwargs: Additional parameters to pass Returns: pexp: Outputs a vector of pattern expression values """ if mask is not None: if type(mask) is not nib.nifti1.Nifti1Image: raise ValueError("Mask is not a nibabel instance") else: mask = nib.load( os.path.join(get_resource_path(), 'MNI152_T1_2mm_brain_mask.nii.gz')) if type(data) is not nib.nifti1.Nifti1Image: if type(data) is str: if os.path.isfile(data): data = nib.load(data) elif type(data) is list: data = nib.funcs.concat_images(data) else: raise ValueError( "Data is not a nibabel instance, list of files, or a valid file name." ) nifti_masker = NiftiMasker(mask_img=mask) data_masked = nifti_masker.fit_transform(data) if len(data_masked.shape) > 2: data_masked = data_masked.squeeze() if type(weight_map) is not nib.nifti1.Nifti1Image: if type(weight_map) is str: if os.path.isfile(weight_map): data = nib.load(weight_map) elif type(weight_map) is list: weight_map = nib.funcs.concat_images(weight_map) else: raise ValueError( "Weight_map is not a nibabel instance, list of files, or a valid file name." ) weight_map_masked = nifti_masker.fit_transform(weight_map) if len(weight_map_masked.shape) > 2: weight_map_masked = weight_map_masked.squeeze() # Calculate pattern expression pexp = pd.DataFrame() for w in range(0, weight_map_masked.shape[0]): if method == 'dot_product': pexp = pexp.append(pd.Series( np.dot(data_masked, np.transpose(weight_map_masked[w, :]))), ignore_index=True) elif method == 'correlation': pexp = pexp.append(pd.Series( pearson(data_masked, weight_map_masked[w, :])), ignore_index=True) pexp = pexp.T if save_output: pexp.to_csv( os.path.join(output_dir, "Pattern_Expression_" + method + ".csv")) # np.savetxt(os.path.join(output_dir,"Pattern_Expression_" + method + ".csv"), pexp, delimiter=",") return pexp
def apply_mask(data=None, weight_map=None, mask=None, method='dot_product', save_output=False, output_dir='.'): """ Apply Nifti weight map to Nifti Images. Args: data: nibabel instance of data to be applied weight_map: nibabel instance of weight map mask: binary nibabel mask method: type of pattern expression (e.g,. 'dot_product','correlation') save_output: Boolean indicating whether or not to save output to csv file. output_dir: Directory to use for writing all outputs **kwargs: Additional parameters to pass Returns: pexp: Outputs a vector of pattern expression values """ if mask is not None: if type(mask) is not nib.nifti1.Nifti1Image: raise ValueError("Mask is not a nibabel instance") else: mask = nib.load(os.path.join(get_resource_path(),'MNI152_T1_2mm_brain_mask.nii.gz')) if type(data) is not nib.nifti1.Nifti1Image: if type(data) is str: if os.path.isfile(data): data = nib.load(data) elif type(data) is list: data = nib.funcs.concat_images(data) else: raise ValueError("Data is not a nibabel instance, list of files, or a valid file name.") nifti_masker = NiftiMasker(mask_img=mask) data_masked = nifti_masker.fit_transform(data) if len(data_masked.shape) > 2: data_masked = data_masked.squeeze() if type(weight_map) is not nib.nifti1.Nifti1Image: if type(weight_map) is str: if os.path.isfile(weight_map): data = nib.load(weight_map) elif type(weight_map) is list: weight_map = nib.funcs.concat_images(weight_map) else: raise ValueError("Weight_map is not a nibabel instance, list of files, or a valid file name.") weight_map_masked = nifti_masker.fit_transform(weight_map) if len(weight_map_masked.shape) > 2: weight_map_masked = weight_map_masked.squeeze() # Calculate pattern expression pexp = pd.DataFrame() for w in range(0, weight_map_masked.shape[0]): if method == 'dot_product': pexp = pexp.append(pd.Series(np.dot(data_masked,np.transpose(weight_map_masked[w,:]))), ignore_index=True) elif method == 'correlation': pexp = pexp.append(pd.Series(pearson(data_masked,weight_map_masked[w,:])), ignore_index=True) pexp = pexp.T if save_output: pexp.to_csv(os.path.join(output_dir,"Pattern_Expression_" + method + ".csv")) # np.savetxt(os.path.join(output_dir,"Pattern_Expression_" + method + ".csv"), pexp, delimiter=",") return pexp