def _run_interface(self, runtime): fname = self.inputs.volume #load data, read lines 8~penultimate datafile = open(fname, 'rb') data = [i.strip().split() for i in datafile.readlines()] stringmatrix = data[8:-1] datafile.close() if self.inputs.hemi == 'lh': chosenvertices = lhvertices if self.inputs.hemi == 'rh': chosenvertices = rhvertices corrmatrix = np.zeros((len(chosenvertices),len(chosenvertices))) for x, vertex in enumerate(chosenvertices): for i in xrange(len(chosenvertices)): corrmatrix[x][i] = abs(float(stringmatrix[vertex][i])) if self.inputs.cluster_type == 'spectral': labels = spectral(corrmatrix, n_clusters=self.inputs.n_clusters, mode='arpack') if self.inputs.cluster_type == 'hiercluster': labels = Ward(n_clusters=self.inputs.n_clusters).fit_predict(corrmatrix) if self.inputs.cluster_type == 'kmeans': labels = km(n_clusters=self.inputs.n_clusters).fit_predict(corrmatrix) if self.inputs.cluster_type == 'dbscan': labels = DBSCAN(eps=np.average(corrmatrix)+np.std(corrmatrix)).fit_predict(corrmatrix) sxfmout = self.inputs.sxfmout img = nb.load(sxfmout) outarray = -np.ones(shape=img.shape[0]) for j, cluster in enumerate(labels): outarray[chosenvertices[j]] = cluster+1 new_img = nb.Nifti1Image(outarray, img.get_affine(), img.get_header()) _, base, _ = split_filename(fname) nb.save(new_img, os.path.abspath(base + '_clustered.nii')) return runtime
def _run_interface(self, runtime): #load data data = nb.load(self.inputs.in_File).get_data() corrmatrix = np.squeeze(data) if self.inputs.cluster_type == 'spectral': positivecorrs = np.where( corrmatrix > 0, corrmatrix, 0) #threshold at 0 (spectral uses non-negative values) newmatrix = np.asarray( positivecorrs, dtype=np.double) #spectral expects dtype=double values labels = spectral(newmatrix, n_clusters=self.inputs.n_clusters, eigen_solver='arpack', assign_labels='discretize') if self.inputs.cluster_type == 'hiercluster': labels = Ward( n_clusters=self.inputs.n_clusters).fit_predict(corrmatrix) if self.inputs.cluster_type == 'kmeans': labels = km( n_clusters=self.inputs.n_clusters).fit_predict(corrmatrix) if self.inputs.cluster_type == 'dbscan': labels = DBSCAN(eps=self.inputs.epsilon).fit_predict(corrmatrix) new_img = nb.Nifti1Image(labels + 1, None) #+1 because cluster labels start at 0 _, base, _ = split_filename(self.inputs.in_File) nb.save( new_img, os.path.abspath(base + '_' + str(self.inputs.n_clusters) + '_' + self.inputs.cluster_type + '_' + self.inputs.hemi + '.nii')) return runtime
def _run_interface(self, runtime): #load data data = nb.load(self.inputs.in_File).get_data() corrmatrix = np.squeeze(data) if self.inputs.cluster_type == 'spectral': positivecorrs = np.where(corrmatrix>0,corrmatrix,0) #threshold at 0 (spectral uses non-negative values) newmatrix = np.asarray(positivecorrs,dtype=np.double) #spectral expects dtype=double values labels = spectral(newmatrix, n_clusters=self.inputs.n_clusters, eigen_solver='arpack', assign_labels='discretize') if self.inputs.cluster_type == 'hiercluster': labels = Ward(n_clusters=self.inputs.n_clusters).fit_predict(corrmatrix) if self.inputs.cluster_type == 'kmeans': labels = km(n_clusters=self.inputs.n_clusters).fit_predict(corrmatrix) if self.inputs.cluster_type == 'dbscan': labels = DBSCAN(eps=self.inputs.epsilon).fit_predict(corrmatrix) new_img = nb.Nifti1Image(labels+1, None) #+1 because cluster labels start at 0 _, base, _ = split_filename(self.inputs.in_File) nb.save(new_img, os.path.abspath(base+'_'+str(self.inputs.n_clusters)+'_'+self.inputs.cluster_type+'_'+self.inputs.hemi+'.nii')) return runtime