<s> from copy import deepcopy <EOL> import numpy as np <EOL> from six import string_types <EOL> from sklearn import decomposition as sk_decomp <EOL> from sklearn import cluster as sk_cluster <EOL> from sklearn . metrics import pairwise_distances <EOL> from os . path import exists , join <EOL> from os import makedirs <EOL> from nibabel import nifti1 <EOL> from neurosynth . analysis import meta <EOL> class Clusterable ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , dataset , mask = None , features = None , <EOL> feature_threshold = None , min_voxels = None , min_studies = None ) : <EOL> self . dataset = dataset <EOL> self . masker = deepcopy ( dataset . masker ) <EOL> if features is not None : <EOL> ids = dataset . get_studies ( features = features , <EOL> frequency_threshold = feature_threshold ) <EOL> data = dataset . get_image_data ( ids , dense = False ) <EOL> else : <EOL> data = dataset . image_table . data <EOL> if min_studies is not None : <EOL> av = self . masker . unmask ( <EOL> data . sum ( <NUM_LIT:1> ) >= min_studies , output = '<STR_LIT>' ) <EOL> self . masker . add ( { '<STR_LIT>' : av } ) <EOL> if min_voxels is not None : <EOL> data = data [ : , np . array ( data . sum ( <NUM_LIT:0> ) >= min_voxels ) . squeeze ( ) ] <EOL> if mask is not None : <EOL> self . masker . add ( { '<STR_LIT>' : mask } ) <EOL> self . data = data [ self . masker . get_mask ( [ '<STR_LIT>' , '<STR_LIT>' ] ) , : ] . toarray ( ) <EOL> def transform ( self , transformer , transpose = False ) : <EOL> '''<STR_LIT>''' <EOL> data = self . data . T if transpose else self . data <EOL> data = transformer . fit_transform ( data ) <EOL> self . data = data . T if transpose else data <EOL> return self <EOL> def magic ( dataset , method = '<STR_LIT>' , roi_mask = None , <EOL> coactivation_mask = None , features = None , feature_threshold = <NUM_LIT> , <EOL> min_voxels_per_study = None , min_studies_per_voxel = None , <EOL> reduce_reference = '<STR_LIT>' , n_components = <NUM_LIT:100> , <EOL> distance_metric = '<STR_LIT>' , clustering_algorithm = '<STR_LIT>' , <EOL> n_clusters = <NUM_LIT:5> , clustering_kwargs = { } , output_dir = None , filename = None , <EOL> coactivation_images = False , coactivation_threshold = <NUM_LIT:0.1> ) : <EOL> '''<STR_LIT>''' <EOL> roi = Clusterable ( dataset , roi_mask , min_voxels = min_voxels_per_study , <EOL> min_studies = min_studies_per_voxel , features = features , <EOL> feature_threshold = feature_threshold ) <EOL> if method == '<STR_LIT>' : <EOL> reference = Clusterable ( dataset , coactivation_mask , <EOL> min_voxels = min_voxels_per_study , <EOL> min_studies = min_studies_per_voxel , <EOL> features = features , <EOL> feature_threshold = feature_threshold ) <EOL> elif method == '<STR_LIT>' : <EOL> reference = deepcopy ( roi ) <EOL> feature_data = dataset . feature_table . data <EOL> n_studies = len ( feature_data ) <EOL> reference . data = reference . data . dot ( feature_data . values ) / n_studies <EOL> elif method == '<STR_LIT>' : <EOL> reference = roi <EOL> if reduce_reference is not None : <EOL> if isinstance ( reduce_reference , string_types ) : <EOL> reduce_reference = { <EOL> '<STR_LIT>' : sk_decomp . RandomizedPCA , <EOL> '<STR_LIT>' : sk_decomp . FastICA <EOL> } [ reduce_reference ] ( n_components ) <EOL> transpose = ( method == '<STR_LIT>' ) <EOL> reference = reference . transform ( reduce_reference , transpose = transpose ) <EOL> if method == '<STR_LIT>' : <EOL> distances = pairwise_distances ( roi . data , reference . data , <EOL> metric = distance_metric ) <EOL> else : <EOL> distances = reference . data <EOL> if isinstance ( clustering_algorithm , string_types ) : <EOL> clustering_algorithm = { <EOL> '<STR_LIT>' : sk_cluster . KMeans , <EOL> '<STR_LIT>' : sk_cluster . MiniBatchKMeans <EOL> } [ clustering_algorithm ] ( n_clusters , ** clustering_kwargs ) <EOL> labels = clustering_algorithm . fit_predict ( distances ) + <NUM_LIT:1.> <EOL> header = roi . masker . get_header ( ) <EOL> header [ '<STR_LIT>' ] = labels . max ( ) <EOL> header [ '<STR_LIT>' ] = labels . min ( ) <EOL> voxel_labels = roi . masker . unmask ( labels ) <EOL> img = nifti1 . Nifti1Image ( voxel_labels , None , header ) <EOL> if output_dir is not None : <EOL> if not exists ( output_dir ) : <EOL> makedirs ( output_dir ) <EOL> if filename is None : <EOL> filename = '<STR_LIT>' % n_clusters <EOL> outfile = join ( output_dir , filename ) <EOL> img . to_filename ( outfile ) <EOL> if coactivation_images : <EOL> for l in np . unique ( voxel_labels ) : <EOL> roi_mask = np . copy ( voxel_labels ) <EOL> roi_mask [ roi_mask != l ] = <NUM_LIT:0> <EOL> ids = dataset . get_studies ( <EOL> mask = roi_mask , activation_threshold = coactivation_threshold ) <EOL> ma = meta . MetaAnalysis ( dataset , ids ) <EOL> ma . save_results ( output_dir = join ( output_dir , '<STR_LIT>' ) , <EOL> prefix = '<STR_LIT>' % l ) <EOL> else : <EOL> return img </s>
