def __init__(self, targetMDG, Num_Cluster=-1, WCAresult=None): self.graph = targetMDG self.result = [] if WCAresult is None: # initialize clusters by random if Num_Cluster == -1 or Num_Cluster > len(targetMDG.nodes): for i in range(len(targetMDG.nodes)): self.result.append(Cluster.Cluster()) for i in range(len(targetMDG.nodes)): idx = random.randrange(0, len(self.result)) self.result[idx].add_node(targetMDG.nodes[i]) for cluster in self.result[:]: if len(cluster.get_nodes()) == 0: self.result.remove(cluster) else: for i in range(Num_Cluster): self.result.append(Cluster.Cluster()) suffled_node = targetMDG.nodes[:] random.shuffle(suffled_node) list = range(0, len(targetMDG.nodes)) out = random.sample(list, Num_Cluster - 1) out.sort() cut = 0 for i in range(len(suffled_node)): if cut < Num_Cluster - 1 and i >= out[cut]: cut += 1 self.result[cut].add_node(suffled_node[i]) else: # based on WCA self.result = WCAresult self.score = TurboMQ.calculate_fitness(self.result, self.graph) self.position = self.cluster_to_position_matrix() self.velocity = [[0] * len(self.result)] * len(self.position) self.lbest = self.result[:] self.gbest = self.result[:]
def Execute(self): bundleFile = self.GetInputStageValue(0, "bundleFile") images = self.GetInputStageValue(0, "images") pmvsPath = self._properties["Target Path"] self.StartProcess() paths = self.Bundle2PMVS(images.GetImageListPath(), bundleFile.GetBundleFilePath(), pmvsPath) visFile = self.Bundle2Vis(pmvsPath, bundleFile.GetBundleFilePath()) images = self.CopyImagesToVisualizeDirectory(images, paths[1]) bundleFileDest = self.CopyBundleFile(bundleFile.GetBundleFilePath(), pmvsPath) bundleFile = BundleAdjustment.BundleFile(bundleFileDest, images.GetImages()) visFile = Cluster.VisFile(visFile) # TODO: linux hack if (not Common.Utility.IsWindows() or Common.Utility.PlatformName=="Windows64bit"): Common.Utility.CopyFiles(pmvsPath, paths[0], "txt") os.remove(os.path.join(paths[0], "pmvs_options.txt")) cameraMatrices = Cluster.CameraMatrices(paths[0]) self.SetOutputValue("bundleFile", bundleFile) self.SetOutputValue("images", images) self.SetOutputValue("visFile", visFile) self.SetOutputValue("cameraMatrices", cameraMatrices)
def Execute(self): bundleFile = self.GetInputStageValue(0, "bundleFile") images = self.GetInputStageValue(0, "images") self.StartProcess() numImages = len(images.GetImages()) pmvsPath = os.path.split(bundleFile.GetBundleFilePath())[0] optionsFileName = "pmvs_options.txt" optionsFilePath = os.path.join(pmvsPath, optionsFileName) modelFile = os.path.join(pmvsPath, "models", optionsFileName + ".ply") patchFile = os.path.join(pmvsPath, "models", optionsFileName + ".patch") psetFile = os.path.join(pmvsPath, "models", optionsFileName + ".pset") if (Common.Utility.ShouldRun(self._properties["Force Run"], optionsFilePath, modelFile, patchFile, psetFile)): self.WriteOptionsFile(optionsFilePath, numImages) self.RunCommand( "pmvs2", Common.Utility.CommandArgs("\"%s/\"" % pmvsPath, optionsFileName)) self.SetOutputValue("model", Cluster.PlyFile(modelFile)) self.SetOutputValue("patch", Cluster.PatchFile(patchFile)) self.SetOutputValue("pset", Cluster.PsetFile(psetFile))
def pipeline(path, start, end): moni = Monitor.Monitor(path) moni.creat_json_file() anchor = start while anchor < end: # print(anchor, end) if anchor == 0: GeoOPt.geo_opt(path, moni) elif anchor == 1: if start == 1: HF1.hf1_start(path, moni) else: HF1.hf1(path, moni) elif anchor == 2: Localization.localization(path, moni) elif anchor == 3: HF2.hf2(path, moni) elif anchor == 4: LMP2.lmp2(path, moni) if if_skip_rpa() == 1: anchor += 1 elif anchor == 5: RPA.rpa(path, moni) elif anchor == 6: Cluster.cluster(path) elif anchor == 7: Correction.correction(path, moni) elif anchor == 8: Results.results(path) anchor += 1 end_programm(path)
def divide_clusters(field, cluster): plus = Cluster.Cluster() minus = Cluster.Cluster() plus.set_center(Point.Point()) minus.set_center(Point.Point()) gamma = 0.5 if cluster.get_sigma_max_component() == "X": plus.get_center().set_x(cluster.get_center().get_x() + cluster.get_sigma_max() * gamma) minus.get_center().set_x(cluster.get_center().get_x() - cluster.get_sigma_max() * gamma) plus.get_center().set_y(cluster.get_center().get_y()) minus.get_center().set_y(cluster.get_center().get_y()) else: plus.get_center().set_y(cluster.get_center().get_y() + cluster.get_sigma_max() * gamma) minus.get_center().set_y(cluster.get_center().get_y() - cluster.get_sigma_max() * gamma) plus.get_center().set_x(cluster.get_center().get_x()) minus.get_center().set_x(cluster.get_center().get_x()) field.get_clusters().remove(cluster) field.get_clusters().append(plus) field.get_clusters().append(minus)
def read_geometry_info(self): """ read dimensionality, lattice vector and geometry infomation from different input :return: dimensionality, lattice vector and geometry """ try: # try to find infos in CRYSTAL geometry optimization OUTPUT file. geo_opt_file = self.cluster_path.replace('cluster', 'geo_opt') geo_opt_file = os.path.join(geo_opt_file, 'geo_opt.out') dimensionality, lattice_vector, geometry = Cluster.read_info( geo_opt_file) except Exception as e: print(e) try: # try to find infos in .ini file IniReader = Cluster.GeoIniReader(self.geometry_file) dimensionality, lattice_vector, geometry = IniReader.get_infos( ) except Exception as e: print(e) try: # try to find infos in CRYSTAL INPUT file dimensionality, lattice_vector, geometry = Cluster.read_CrystalInput( self.geometry_file) except Exception as e: print(e) print('Please use correct form of geometry input.') print('Programm exits...') sys.exit() self.record_data('dimensionality', dimensionality) self.record_data('lattice vector', lattice_vector) self.record_geometry(geometry) return dimensionality, lattice_vector, geometry
def cluster(path): rec = 'Cluster Cutting begins.\n' rec += '---' * 25 print(rec) record(path, rec) # read parameters from ini file Ini = ReadIni() name, slab_or_molecule, group, lattice_parameter, number_of_atoms, geometry, fixed_atoms = Ini.get_basic_info( ) center_atoms, factors, deleted_atoms, coord, add_h, out_layer_number = Ini.get_cluster( ) cutting_setting = [coord, add_h] record_data_json(path, 'central atoms', center_atoms, section='cluster') record_data_json(path, 'cutting factors', factors, section='cluster') record_data_json(path, 'deleted atoms', deleted_atoms, section='cluster') cutting_setting_dict = { 'coord': coord, 'add_h': add_h, 'out_layer_number': out_layer_number } record_data_json(path, 'cutting setting', cutting_setting_dict, section='cluster') # get bilayer jobs rpa_jobs = get_jobs(path) cluster_jobs = [job for job in rpa_jobs if job.layertype == 'bilayer'] for job in cluster_jobs: if 'rpa' in job.path: job.path = job.path.replace('rpa', 'cluster') elif 'geo_opt' in job.path: job.path = job.path.replace('geo_opt', 'cluster') job.method = 'cluster' # generate clusters cluster_path = os.path.join(path, 'cluster') mkdir(cluster_path) Cluster.creat_json_file(cluster_path) for job in cluster_jobs: Clu = Cluster.ClusterCutter(job, center=center_atoms, name=name, fixed_atoms=fixed_atoms, factors=factors, cutting_setting=cutting_setting, deleted_atoms=deleted_atoms) if not Cluster.if_cluster_already_generated(job): Clu.get_cluster() if out_layer_number is True: Clu.write_xyz_with_layernumber() else: Clu.write_xyz() rec = 'Cluster Cutting finished!\n' rec += '***' * 25 print(rec) record(path, rec)
def splitCluster(self, cluster_key): if not cluster_key in self.__cluster_dic: raise Exception("ClusterManager splitCluster:cluster不存在") cluster = self.__cluster_dic[cluster_key] grids = cluster.getAllGrids() keys = [] for k in grids: g = grids[k] keys.append(g.key()) #!!!这里的处理流程和isSingle重复了,但是为了保证函数的不变,只能复制一遍 stop = 0 # 初始化flag_dic flag_dic = {} #keys = self.__grid_dic.keys() for k in keys: flag_dic[k] = 0 # 将第一个key对应的flag设置为1 flag_dic[keys[0]] = 1 while not stop: # 1、删除标记为2的key keys_todelete = [] for k in flag_dic: if 2 == flag_dic[k]: keys_todelete.append(k) for k in keys_todelete: flag_dic.pop(k) # 2、将标记为1的key的neighbor标记为1,然后把自己标记为2 for k in flag_dic: item = flag_dic[k] if 1 == item: neighbor_keys = Helper.getNeighborKeys(k) for neighbor_key in neighbor_keys: if neighbor_key in flag_dic: flag_dic[neighbor_key] = 1 flag_dic[k] = 2 # 3、若dic为空或全部标记都是0,循环结束 stop = 1 for k in flag_dic: item = flag_dic[k] if 0 != item: stop = 0 # 循环结束后,判断flag_dic的长度,若为0,则是single if 0 != len(flag_dic): #这部分就是被分割的部分 #将这些grid移动到新的cluster中 self.__cluster_key_index += 1 new_cluster = Cluster(self.__cluster_key_index) self.__cluster_dic[self.__cluster_key_index] = new_cluster for k in flag_dic: moving_grid = cluster.getGrid(k) cluster.delGrid(moving_grid) new_cluster.addGrid(moving_grid)
def edit_article(id): data = request.get_json() title = data['title'] body = data['body'] author = user_exists(request.authorization['username']) cluster = Cluster(['172.17.0.2']) session = cluster.connect()
def clustering(self): X = np.array([np.array(self.feature_table[i]) for i in self.cluster_factors]) for i in range(len(X)): X[i] = (X[i] - X[i].mean()) / X[i].std() X = X.T Cluster.KMeans(X).test() self.y_km = cluster(Cluster.KMeans(X), self.feature_table[self.cluster_factors], self.CLUSTER) fig_km = self.plot(self.y_km)
def addNewCluster(self, grid_object): if (not grid_object.isNotClustered()): raise Exception("this grid is being clustering") self.__cluster_key_index += 1 cluster = Cluster(self.__cluster_key_index) logging.debug("created new Cluster " + str(self.__cluster_key_index)) cluster.addGrid(grid_object) self.__cluster_dic[self.__cluster_key_index] = cluster
def cluster_read_pairs_by_chr(read_pair_list): """this generates a list of maximal clusters, ie sets of overlapping read pairs. note: these clusters can be themselves overlapping. returns a disctionary of lists of Cluster objects, one entry per chromosome""" #sort according to end position then chromosome. sort is stable so the second sort will not unsort the positions read_pair_list.sort(key=lambda read_pair: read_pair.interval_end) read_pair_list.sort(key=lambda read_pair: read_pair.interval_chr) #store each a list of current Cluster objects, which contains a list of AlignedReadPair objects cluster_list = [] #store a seperate cluster_list for each chromosome chr_cluster_lists = {} #read_pair_Q stores a list of currently overlapping read pair intervals read_pair_Q = deque([read_pair_list[0]]) for read_pair in read_pair_list: #print read_pair_Q #if you can add the next interval to the current list of overlapping intervals, do so if read_pair.interval_chr == read_pair_Q[ 0].interval_chr and read_pair.interval_start <= read_pair_Q[ 0].interval_end: read_pair_Q.append(read_pair) #if the current read is from another chromosome, save the list of currently overlapping intervals as a cluster and empty it elif read_pair.interval_chr != read_pair_Q[0].interval_chr: new_cluster = Cluster(list(read_pair_Q)) cluster_list.append(new_cluster) chr_cluster_lists[read_pair_Q[0].interval_chr] = cluster_list #empty queue and current cluster list since we are starting with a new chromosome cluster_list = [] read_pair_Q.clear() read_pair_Q.append(read_pair) #otherwise, save the list of currently overlapping intervals as a cluster else: new_cluster = Cluster(list(read_pair_Q)) cluster_list.append(new_cluster) # and pop off intervals in the Q as long as they do not overlap with your current interval -- these cannot constitute another maximal cluster while len( read_pair_Q ) != 0 and read_pair.interval_start > read_pair_Q[0].interval_end: read_pair_Q.popleft() #then add your current read to the Q read_pair_Q.append(read_pair) #for cluster in cluster_list: # print " ".join(read.str_int() for read in cluster) #print " ".join(read.str_TE_annot_list() for read in cluster) last_cluster = Cluster(list(read_pair_Q)) cluster_list.append(last_cluster) chr_cluster_lists[read_pair_Q[0].interval_chr] = cluster_list return chr_cluster_lists
def kmeans_process(alarmLog, testDt_List): seg_Dt_List = Cluster.JieBaSplit( testDt_List) #调用Cluster文件中的JieBaSplit()函数,将文本进行分词 test_arr = Cluster.toTfidfVec( seg_Dt_List) #调用toTfidfVec()函数,将分完词的文本转换成特征向量 #dimension=66 #test_arr_lowD=Cluster.DimenReduPCA(test_arr,dimension) k = 10 #设置聚类簇数 label = Cluster.kmeans(test_arr, k, alarmLog) #调用kmeans()函数进行聚类 label.sort(key=lambda x: x[35]) #将聚类结果安装分类标签排序 return label
def readData(Path): testDt_List = [] with open(Path, 'r', encoding='utf-8') as file: next(file) for line in file: testDt = line.split(',') testDt_List.append(testDt[12]) dimension = 66 tfidf_vec = Cluster.toTfidfVec(testDt_List) # print(len(test_arr[0])) tfidf_vec_lowD = Cluster.DimenReduPCA(tfidf_vec, dimension) return tfidf_vec_lowD, testDt_List
def pipeline(): Ini = IniReader() path = Ini.project_path start = Ini.start end = Ini.end now = datetime.now() now = now.strftime("%b %d %Y %H:%M:%S") rec = 'Project begins.' rec += '\n' + '***' * 25 rename_file(path, 'record') record(path, rec, init=True) print('***' * 25) print(now) print(rec) mkdir(path) try: shutil.copy(Ini.ini_path, path + '/input.ini') except Exception as e: print(e) anchor = start while anchor < end: # print(anchor, end) if anchor == 0: GeoOpt.geo_opt(path) elif anchor == 1: HF1.hf1(path) elif anchor == 2: Loc.localization(path) elif anchor == 3: HF2.hf2(path) elif anchor == 4: LMP2.lmp2(path) elif anchor == 5: RPA.rpa(path) elif anchor == 6: Cluster.cluster(path) elif anchor == 7: Correction.correction(path) elif anchor == 8: Results.results(path) anchor += 1 now = datetime.now() now = now.strftime("%b %d %Y %H:%M:%S") rec = 'Project End.\n' rec += '***' * 25 rename_file(path, 'record') record(path, rec, init=True) print(now) print(rec)
def SciPyClustering(self,col,row,tot,energyGC,energyPbPC): pixels = [[col[i],row[i]] for i,x in enumerate(col)] if(len(pixels)>1): result=fclusterdata(pixels,sqrt(2.),criterion="distance") clusters=[Cluster() for i in range(max(result))] [clusters[x-1].addPixel(col[j],row[j],tot[j],energyGC[j],energyPbPC[j]) for j,x in enumerate(result)] else: if(len(pixels)==1): c=Cluster() c.addPixel(col[0],row[0],tot[0],energyGC[0],energyPbPC[0]) clusters=[c] return clusters
def RecursiveClustering(self,row,col,tot) : clusters = [] while(len(row)!=0) : cluster = Cluster() cluster.addPixel(col[0], row[0], tot[0]) #print "[DEBUG] adding pixel col=%d row=%d as seed"%(col_tmp[0],row_tmp[0]) row.pop(0) col.pop(0) tot.pop(0) while(self.addNeighbor(cluster, col,row, tot)>0): pass clusters.append(cluster) return clusters
def RecursiveClustering(self, row, col, tot): clusters = [] while (len(row) != 0): cluster = Cluster() cluster.addPixel(col[0], row[0], tot[0]) #print "[DEBUG] adding pixel col=%d row=%d as seed"%(col_tmp[0],row_tmp[0]) row.pop(0) col.pop(0) tot.pop(0) while (self.addNeighbor(cluster, col, row, tot) > 0): pass clusters.append(cluster) return clusters
def cluster(self, headers = None, clusters = 10, name = None, normalize = True): #check initial values if (headers == None): headers = self.header_num() if( name == None): name = str(len(self.clusterList)) print "name",name #get variable data that contains the normalized data data = self.select(headers) #check to see if we are normalizing the data if( normalize == True): colNum = 0 normalizedData = () for header in headers: #gets the column of data dataCol = data[:,colNum] #gets the index the header is index = self.getIndex(header) #gets the range rangeValue = self.range_num(index) #get the normalized column normalizedcol = (dataCol - rangeValue[1])/(rangeValue[0] - rangeValue[1]) #add it to the now normalized data set normalizedData += (normalizedcol, ) colNum += 1 #set the normalized data to be a matrix norm = np.hstack((normalizedData)) print "norm", norm #if there is no normalization, the data is just set to be the norm data (for simplicity later) else: norm = data #create a new cluster object for kmeans clusterInfo = Cluster() #add it to the list of clusters self.clusterList.append(clusterInfo) #generate the cluster data clusterInfo.kmeans(name = name, headers = headers, k = clusters, data = norm) #get the id of each point in the data ids = clusterInfo.classify(data = norm) # add the column to the data as an enum type and with a header name = to name self.addColunn(header = name, typeString = "enum", columnData = ids) return
def sfmChainBuild(chainFilePath, imagePath): # build chain imageSource = Sources.ImageSource(imagePath, "jpg") sift = FeatureExtraction.Sift(imageSource, False, "SiftHess") keyMatch = FeatureMatch.KeyMatch(sift, False, "KeyMatchFull") bundler = BundleAdjustment.Bundler([keyMatch, imageSource]) radialUndistort = Cluster.RadialUndistort([bundler, imageSource]) prepCmvsPmvs = Cluster.PrepCmvsPmvs(radialUndistort, os.path.join(imagePath, "pmvs")) cmvs = Cluster.CMVS(prepCmvsPmvs) pmvs = Cluster.PMVS(cmvs) # persist chain Chain.StageRegistry.Save(chainFilePath)
def compare_similarity(clusters, nodes): """ Compare two clusters in clusters list, and return two most similar clusters among all clusters. :param clusters: A list of clusters :param nodes: A list of nodes :return: Two most similar clusters """ max_UENM = -1 max_c1 = Cluster.Cluster() max_c2 = Cluster.Cluster() for i in range(len(clusters)): # for all two-cluster combinations for j in range(len(clusters)): if i >= j: continue cluster1 = copy(clusters[i]) cluster2 = copy(clusters[j]) feature1 = cluster1.get_feature_vector() feature2 = cluster2.get_feature_vector() # use WCA_UENM value a = b = c = d = n = Ma = 0 # get a,b,c,d,n,Ma for k in range(len(nodes)): if feature1[k] > 0 and feature2[k] > 0: a += 1 Ma += feature1[k] + feature2[k] if feature1[k] > 0 and feature2[k] == 0: b += 1 if feature1[k] == 0 and feature2[k] > 0: c += 1 if feature1[k] == 0 and feature2[k] == 0: d += 1 # calculate UENM n = a + b + c + d UENM = (0.5 * Ma) / ((0.5 * Ma) + b + c + n) # if new UENM is higher than origianal max_UENM, then update it if UENM > max_UENM: max_UENM = UENM max_c1 = clusters[i] max_c2 = clusters[j] # print("UENM= ",max_UENM) return max_c1, max_c2
def _formCluster(index, data_set): data = data_set[index] x = data.get_x() y = data.get_y() z = data.get_z() cluster = Cluster.ClusterCenter(x, y, z, x, y, z, data.get_class()) return cluster
def run_clustering_city(filepath, filename, k, eps, latitude, longitude): """ The function clusters data for a given city and draws the result obtained on the map. :param filepath: path of file .csv :param filename: name of file .csv :param k: the value of k :param eps: the value of eps :param latitude: latitude of city :param longitude: longitude of city :return: None """ d = Cluster.ClusterGreatCircles(filepath, filename) for k in [7]: for eps in [50]: c = Clustering.K_MXTGreatCircle(eps, k, d) c() m = Metrics.Modularity(c) print(f'k-MXT k={k} eps={eps} Modularity={m()}') c.cluster.view_at_map(latitude=latitude, longitude=longitude, filename_of_map=f'{k}-MXT-eps{eps}') c = Clustering.K_MXTGaussGreatCircle(eps, k, d) c() c.cluster.view_at_map(latitude=latitude, longitude=longitude, filename_of_map=f'{k}-MXTGauss-eps{eps}') m = Metrics.Modularity(c) print(f'k-MXT-Gauss k = {k} eps = {eps} Modularity = {m()}')
def train(histogram): data, centroidList, distance = Cluster.kMeans(histogram, main.CLUSTERS) data = Utils.prepareData(histogram) labels = Utils.closestCentroidList(data, centroidList) clf = svm.SVC(kernel='rbf') clf.fit(data, labels) return data, clf
def try_replace_node(self, node, from_idx, to_idx): """ Try to replace node between two clusters and give fitness score of result :param node: Given node that wanted to remove to given cluster. :param from_idx: An index of cluster based on self.result :param to_idx: An index of cluster based on self.result :return: Fitness score after replace node """ result = self.result[:] target_cluster = result[from_idx] target_cluster.remove_node(node) if to_idx == len(result): target_cluster = Cluster.Cluster() result.append(target_cluster) else: target_cluster = result[to_idx] target_cluster.add_node(node) tried_score = TurboMQ.calculate_fitness(result, self.graph) target_cluster = result[to_idx] target_cluster.remove_node(node) target_cluster = result[from_idx] target_cluster.add_node(node) return tried_score
def add_new_cluster(self): """ Add new cluster with 0 nodes to result :return: None """ self.result.append(Cluster.Cluster()) return len(self.result) - 1
def find_clusters(header, pixels, threshold): """ Find all the clusters in a FITS image, given a threshold :param header: header of the FITS image :param pixels: original image matrix :param threshold: threshold to be applied :return: list of clusters in the image """ # We define an array of pixels visited: 1 if visited, 0 elsewise pixels_visited = np.zeros_like(pixels) cluster_list = [] # WARNING : pixels[row, col]: row corresponds to y and col to x for row in range(len(pixels)): for col in range(len(pixels[0])): if pixels_visited[row, col]: continue # If pixel visited, go to next pixel (next step of the loop) if pixels[row, col] < threshold: pixels_visited[row, col] = 1 # visited else: # add the new cluster to the list try: cluster = Cluster.Cluster(explore_cluster( pixels_visited, pixels, row, col, threshold), pixels, header) cluster_list.append(cluster) pixels_visited[row, col] = 1 # visited except RuntimeError: # If max recursion depth is reached, return an empty list print "Max recursion depth reached" return [] # at this point, the cluster list is build return cluster_list
def read_bam_file(file_name, tag, quiet, debug=False): verbosity('Reading and storing alignment information ' + file_name, quiet) file = pysam.AlignmentFile(file_name, 'rb') clusters = {} aln_count = 0 for aln in file.fetch(until_eof=True): aln_count += 1 try: cluster_id = aln.get_tag(tag=tag) except KeyError: verbosity('Tag ' + tag + 'not present in bam record', True) verbosity('Exiting program', True) sys.exit() if cluster_id not in clusters: clusters[cluster_id] = Cluster(cluster_id) clusters[cluster_id].add_pysam_alignment(aln) if aln_count % 1e6 == 0: verbosity('#' + str(aln_count) + ' alignments analyced', quiet) sys.stdout.flush() if aln_count % 1e5 == 0 and debug: break file.close() verbosity('Complete', quiet) return clusters
def convert_DF_to_Cluster(data): clusters = [] for row in data: cluster = Cluster_Class.Cluster(row[0], row[1], row[2]) clusters.append(cluster) return clusters
def test_MgO(): path = r'C:\Users\ccccc\Documents\Theoritische Chemie\Masterarbeit\test\cluster' Cluster.creat_json_file(path) job = r'C:\Users\ccccc\Documents\Theoritische Chemie\Masterarbeit\test\geo_opt\x_1\z_2' dimen, lattice, geo = read_infomation(job) job = Job(job) center_upp = [25, 25] center_und = [9, 9] center = [center_upp, center_und] fixed_atoms = [13, 24] fac_upp = [0.3, 0.5, 0.5] fac_und = [0.4, 0.6, 0.6] factors = [fac_upp, fac_und] Clu = Cluster.ClusterCutter(job, name='MgO', centre=center, basic_infos=[dimen, lattice, geo], factors=factors, fixed_atoms=fixed_atoms) Clu.final_cluster = Clu.choose_atoms_from_distance() Clu.write_xyz(Clu.choosed_atoms)
def Build_From_System(self,Old): #From another System : we copy everything self.Expansion = Old.Expansion self.ParticleType = Old.ParticleType self.TopologieUp = Old.TopologieUp self.TopologieDown = Old.TopologieDown self.Lx,self.Ly=Old.Lx,Old.Ly self.State=copy.copy(Old.State) self.Kmain,self.Kvol,self.Eps,self.Kcoupling=Old.Kmain,Old.Kvol,Old.Eps,Old.Kcoupling self.J=Old.J self.Np=Old.Np self.ElasticEnergy=Old.ElasticEnergy self.SurfaceEnergy=Old.SurfaceEnergy self.OccupiedSite=copy.copy(Old.OccupiedSite) self.FreeSite=copy.copy(Old.FreeSite) # need to deep copy all the objects self.BinaryClusters=deepcopy(Old.BinaryClusters) # need to deep copy all the object in the list self.ObjectClusters=list() # create a new list for Clust in Old.ObjectClusters: # take every cluster in the old system # Cluster has a built-in copy constructor if the old_cluster # argument is given self.ObjectClusters.append(Cluster(old_cluster=Clust)) if self.ObjectClusters.__len__()!=Old.ObjectClusters.__len__(): print('fail') self.PlotPerSite() Old.PlotPerSite()
def DOCK(self): if(self.library): subprocess.call([Paths.DOCKBASE + "docking/setup/setup_db2.csh", self.compound]) clu = Cluster.Cluster() clu.runJobs("dirlist", self.dock_command) else: subprocess.call([self.dock_command])
def sub_clustering(self, identity, per_lead, target_leader_size, hi_pct, height, l): tmp_leads = [] tmp_clusters = [] if l > 1: # set max capacity to target_size + the added extra 'Hi' percent self.max_capacity = math.ceil( (target_leader_size / 100) * (100 + hi_pct)) # set size to target self.size = per_lead for i in range(per_lead): leader = Leader(identity, height, leader=self) identity += 1 self.cluster.append(leader) tmp_leads.append(leader) leads, clusters, c_id = leader.sub_clustering( identity, per_lead, target_leader_size, hi_pct, height - 1, (l - 1)) identity = c_id tmp_leads.extend(leads) tmp_clusters.extend(clusters) return tmp_leads, tmp_clusters, identity else: self.represents_cluster = True cluster = Cluster.Cluster(leader=self) self.cluster.append(cluster) return tmp_leads, self.cluster, identity
def test_delGrid(self): #case1:key不存在,抛出异常 cluster=Cluster(1) with self.assertRaises(KeyError): grid=Grid() grid._Grid__key=23 cluster.delGrid(grid) #case2:key存在,正确删除 grid=Grid() grid._Grid__key=343 cluster.addGrid(grid) cluster.delGrid(grid) self.assertEqual(0,cluster.size())
def kMeans(): histogram = DataParse.loadStack() data, centroidList, distance = Cluster.kMeans(histogram, main.CLUSTERS) Draw.clusterDraw(data, centroidList, histogram)
def ClusterEvent(self,i,method="QWeighted",sigma=0.003, scaler=1): self.getEvent(i) row_tmp = [] col_tmp = [] tot_tmp = [] clusterid=0 row_tmp = [s for s in self.p_row] col_tmp = [s for s in self.p_col] tot_tmp = [s for s in self.p_tot] # for index in self.p_row: # row_tmp.append(index) # for index in self.p_col: # col_tmp.append(index) # for index in self.p_tot: # tot_tmp.append(index) hpindex=0 if len(self.hotpixels)>0: while(hpindex<len(row_tmp)) : if([col_tmp[hpindex],row_tmp[hpindex]] in self.hotpixels): #print "Removing hot pixel x: %i y:%i"%(col_tmp[hpindex],row_tmp[hpindex]) col_tmp.pop(hpindex) row_tmp.pop(hpindex) tot_tmp.pop(hpindex) else : hpindex+=1 clusters = [] while(len(row_tmp)!=0) : cluster = Cluster() cluster.addPixel(col_tmp[0], row_tmp[0], tot_tmp[0]) #print "[DEBUG] adding pixel col=%d row=%d as seed"%(col_tmp[0],row_tmp[0]) row_tmp.pop(0) col_tmp.pop(0) tot_tmp.pop(0) while(self.addNeighbor(cluster, col_tmp, row_tmp, tot_tmp)>0): pass cluster.Statistics() if (method=="QWeighted"): cluster.GetQWeightedCentroid() elif (method=="DigitalCentroid"): cluster.GetDigitalCentroid() elif (method=="maxTOT"): cluster.GetMaxTOTCentroid() elif (method=="EtaCorrection"): # cluster.GetEtaCorrectedQWeightedCentroid() cluster.GetEtaCorrectedQWeightedCentroid(sigma) # to be implemented in the future: # digital, maxTOT/maxQ, eta corrected cluster.id=clusterid clusters.append(cluster) clusterid+=1 cluster=0 for ind in range(i,i+scaler): self.AllClusters.append(clusters) del clusters
import Image import Cluster im = Image.open("original_images/Stand1.png") im2 = Cluster.clusterImage(im, 6) im2.show()
def EM(): histogram = DataParse.loadStack() MG, obs = Cluster.expectationMaximization(histogram, main.CLUSTERS) Draw.EMDraw(obs, MG, histogram)
def dirichelet(): histogram = DataParse.loadStack() MG, obs = Cluster.expectationMaximization(histogram, main.CLUSTERS, mode="Dirichelet") Draw.EMDraw(obs, MG, histogram)
def sliceDraw(): histogram = DataParse.loadStack() data, centroidList, distance = Cluster.kMeans(histogram, main.CLUSTERS) Draw.drawSlice(data, centroidList)
def howManyClusters(): histogram = DataParse.loadStack() Cluster.howManyClusters(histogram, 2, 45, 5)
def ReadClusterTree(self): event = 0 events = [] nentries = self.pixelTree.GetEntriesFast() for i in xrange(nentries) : self.entry = self.pixelTree.GetEntry(i) events.append(self.pixelTree.event) for i in range(max(events)+1): self.AllClusters.append([]) print "%i events in cluster file"%max(events) for i in xrange(nentries) : self.entry = self.pixelTree.GetEntry(i) cluster = Cluster() for i in range(self.pixelTree.size) : cluster.col.append(self.pixelTree.col[i]) cluster.row.append(self.pixelTree.row[i]) cluster.tot.append(self.pixelTree.tot[i]) cluster.energyGC.append(self.pixelTree.energyGC[i]) cluster.energyPbPC.append(self.pixelTree.energyPbPC[i]) event = self.pixelTree.event cluster.sizeX = self.pixelTree.sizeX cluster.sizeY =self.pixelTree.sizeY cluster.size = self.pixelTree.size cluster.totalTOT =self.pixelTree.totalTOT cluster.totalEnergyGC =self.pixelTree.totalEnergyGC cluster.totalEnergyPbPC =self.pixelTree.totalEnergyPbPC cluster.aspectRatio = self.pixelTree.aspectRatio cluster.relX = self.pixelTree.relX cluster.relY = self.pixelTree.relY cluster.absX = self.pixelTree.absX cluster.absY = self.pixelTree.absY cluster.resX = self.pixelTree.resX cluster.resY = self.pixelTree.resY cluster.relX_energyGC = self.pixelTree.relX_energyGC cluster.relY_energyGC = self.pixelTree.relY_energyGC cluster.absX_energyGC = self.pixelTree.absX_energyGC cluster.absY_energyGC = self.pixelTree.absY_energyGC cluster.resX_energyGC = self.pixelTree.resX_energyGC cluster.resY_energyGC = self.pixelTree.resY_energyGC cluster.relX_energyPbPC = self.pixelTree.relX_energyPbPC cluster.relY_energyPbPC = self.pixelTree.relY_energyPbPC cluster.absX_energyPbPC = self.pixelTree.absX_energyPbPC cluster.absY_energyPbPC = self.pixelTree.absY_energyPbPC cluster.resX_energyPbPC = self.pixelTree.resX_energyPbPC cluster.resY_energyPbPC = self.pixelTree.resY_energyPbPC cluster.id = self.pixelTree.id cluster.trackNum = self.pixelTree.trackNum self.AllClusters[event].append(cluster)
# print tags # print cluster_radiuses # print distance_to_matrix # print characteristic errors.append(np.min(characteristic[np.where(characteristic > PREDICTION_COEFFICIENT)]) * 100) times.append(time.time() - start_time) index_matched = np.where(characteristic <= PREDICTION_COEFFICIENT) return zip(tags[index_matched], characteristic[index_matched]) if __name__ == "__main__": np.set_printoptions(precision=4, threshold='nan', linewidth='nan') data_matrix = get_data_from_json("data/10_5_S.json") Cluster.clusterize(data_matrix) with open('clusters.pkl') as f: clusters = pickle.load(f) predicted = [] for tag, matrix in data_matrix: pred = predict(clusters, matrix) if len(pred) == 1: predicted.append(pred[0][0]) else: sys.stderr.write("Prediction error: actual pattern: '{0}', predicted: '{1}'\n".format(tag, pred)) predicted.append('--') act = np.array(map(lambda (x, _): x, data_matrix)) predicted = np.array(predicted)