def cluster(self): if self.hasNext: old_clusters = self.clusters[:] # calculate distance matrix dist = zeros((self.k, self.k)) for i in range(self.k - 1): c1 = self.clusters[i] compare_to = [] for j in range(i + 1, self.k): c2 = self.clusters[j] dist[i][j] = dist[j][i] = distance(c1["centroid"], c2["centroid"]) print dist for i, cluster in enumerate(self.clusters): compare_to = [] for j, c2 in enumerate(self.clusters): if (tuple(cluster["centroid"]) != tuple(c2["centroid"])) and ( cluster["radius"] + c2["radius"] ) > dist[i][j]: compare_to.append(j) print compare_to print "clustering agents in %d: %d" % (i, len(old_clusters[i]["agents"])) for agent in old_clusters[i]["agents"]: d1 = distance(agent.ngenome, cluster["centroid"]) distances = [distance(agent.ngenome, self.clusters[j]["centroid"]) for j in compare_to] if min(distances) < d1: closest_cluster = compare_to[Cluster.index_min(distances)] cluster["agents"].remove(agent) self.clusters[closest_cluster]["agents"].append(agent) print "completed clustering, recalculating centroids" to_remove = [] for i, cluster in enumerate(self.clusters): print "old", cluster["centroid"], cluster["radius"], len(cluster["agents"]) # centroid calculation using numpy for optimizaiton if len(cluster["agents"]) > 0: genomes = array([agent.ngenome for agent in cluster["agents"]]) cluster["centroid"] = average(genomes, axis=0) cluster["radius"] = max([distance(g, cluster["centroid"]) for g in genomes]) print "new", cluster["centroid"], cluster["radius"], len(cluster["agents"]) else: to_remove.append(i) to_remove.reverse() for cluster in to_remove: del self.clusters[cluster] while len(self.clusters) < self.k: self._random_cluster() self.iterations += 1
def __init__(self, p, k, genes=range(12)): self.p = p[:] self.k = k self.n = len(p) self.d = len(genes) self.genes = genes #self.gs = zs([agent.genome.select(genes) for agent in self.p]) self.gs = [agent.genome.select(genes) for agent in self.p] for i, agent in enumerate(self.p): agent.ngenome = self.gs[i] self.clusters = [] while len(self.clusters) < self.k: self._random_cluster() for agent in self.p: distances = [ distance(agent.ngenome, self.clusters[j]['centroid']) for j in range(len(self.clusters)) ] cluster = Cluster.index_min(distances) print agent.id, cluster self.clusters[cluster]['agents'].append(agent) for cluster in self.clusters: if len(cluster['agents']) > 0: genomes = array([agent.ngenome for agent in cluster['agents']]) cluster['centroid'] = average(genomes, axis=0) cluster['radius'] = max( [distance(g, cluster['centroid']) for g in genomes]) print cluster['centroid'], cluster['radius'] self.hasNext = True self.iterations = 0
def build_bfactor(self, ligands, protein, protein_pdb): """Builds b-factor descriptors for series of ligands. Parameters ---------- ligands: iterable of oddt.toolkit.Molecules or oddt.toolkit.Molecule A list or iterable of ligands to build the descriptor or a single molecule. protein: oddt.toolkit.Molecule or None (default=None) Default protein to use as reference protein_pdb: the pdb id of the protein. """ if protein: self.protein = protein if is_molecule(ligands): ligands = [ligands] out = [] for mol in ligands: mol_dict = atoms_by_type(mol.atom_dict, self.ligand_types, self.mode) if self.aligned_pairs: pairs = zip(self.ligand_types, self.protein_types) else: pairs = [(mol_type, prot_type) for mol_type in self.ligand_types for prot_type in self.protein_types] dist = distance(self.protein.atom_dict['coords'], mol.atom_dict['coords']) within_cutoff = (dist <= self.cutoff.max()).any(axis=1) local_protein_dict = self.protein.atom_dict[within_cutoff] prot_dict = atoms_by_type(local_protein_dict, self.protein_types, self.mode) desc = [] for mol_type, prot_type in pairs: d = distance(prot_dict[prot_type]['coords'], mol_dict[mol_type]['coords'])[..., np.newaxis] if len(self.cutoff) > 1: count = ((d > self.cutoff[..., 0]) & (d <= self.cutoff[..., 1])).sum(axis=(0, 1)) else: count = (d <= self.cutoff).sum() desc.append(count) desc = np.array(desc, dtype=int).flatten() out.append(desc) parser = PDBParser() structure = parser.get_structure('pdb', protein_pdb) atoms = structure.get_atoms() # Get the b_factors for each atom the structure for a in atoms: out = [np.append(out[0], np.array(a.get_bfactor()))] return np.vstack(out)
def __init__(self, p, k, genes=range(12)): self.p = p[:] self.k = k self.n = len(p) self.d = len(genes) self.genes = genes # self.gs = zs([agent.genome.select(genes) for agent in self.p]) self.gs = [agent.genome.select(genes) for agent in self.p] for i, agent in enumerate(self.p): agent.ngenome = self.gs[i] self.clusters = [] while len(self.clusters) < self.k: self._random_cluster() for agent in self.p: distances = [distance(agent.ngenome, self.clusters[j]["centroid"]) for j in range(len(self.clusters))] cluster = Cluster.index_min(distances) print agent.id, cluster self.clusters[cluster]["agents"].append(agent) for cluster in self.clusters: if len(cluster["agents"]) > 0: genomes = array([agent.ngenome for agent in cluster["agents"]]) cluster["centroid"] = average(genomes, axis=0) cluster["radius"] = max([distance(g, cluster["centroid"]) for g in genomes]) print cluster["centroid"], cluster["radius"] self.hasNext = True self.iterations = 0
def build_num_aromat_rings(self, ligands, protein, ligand_sdf): """Builds number of aromatic rings descriptors for series of ligands. Parameters ---------- ligands: iterable of oddt.toolkit.Molecules or oddt.toolkit.Molecule A list or iterable of ligands to build the descriptor or a single molecule. protein: oddt.toolkit.Molecule or None (default=None) Default protein to use as reference ligand_sdf: the path to the sdf-file of the ligand. """ if protein: self.protein = protein if is_molecule(ligands): ligands = [ligands] out = [] for mol in ligands: mol_dict = atoms_by_type(mol.atom_dict, self.ligand_types, self.mode) if self.aligned_pairs: pairs = zip(self.ligand_types, self.protein_types) else: pairs = [(mol_type, prot_type) for mol_type in self.ligand_types for prot_type in self.protein_types] dist = distance(self.protein.atom_dict['coords'], mol.atom_dict['coords']) within_cutoff = (dist <= self.cutoff.max()).any(axis=1) local_protein_dict = self.protein.atom_dict[within_cutoff] prot_dict = atoms_by_type(local_protein_dict, self.protein_types, self.mode) desc = [] for mol_type, prot_type in pairs: d = distance(prot_dict[prot_type]['coords'], mol_dict[mol_type]['coords'])[..., np.newaxis] if len(self.cutoff) > 1: count = ((d > self.cutoff[..., 0]) & (d <= self.cutoff[..., 1])).sum(axis=(0, 1)) else: count = (d <= self.cutoff).sum() desc.append(count) desc = np.array(desc, dtype=int).flatten() out.append(desc) for mol in pybel.readfile("sdf", ligand_sdf): # could be sdf or mol2 result = [ "Aromatic" for r in mol.OBMol.GetSSSR() if r.IsAromatic() ] out = [np.append(out[0], np.array(len(result)))] output = np.vstack(out) return output
def get_nearest_n(self, point): nearest = distance(point, self.neurons[0]) nearestn = 0 for n in range(self.number_of_neurons): dist = distance(point, self.neurons[n]) if dist < nearest: nearest = dist nearestn = n return nearestn
def build(self, ligands, protein=None, single=False): """Builds descriptors for series of ligands Parameters ---------- ligands: iterable of oddt.toolkit.Molecules or oddt.toolkit.Molecule A list or iterable of ligands to build the descriptor or a single molecule. protein: oddt.toolkit.Molecule or None (default=None) Default protein to use as reference single: bool (default=False) Flag indicating if the ligand is single. """ if protein: self.protein = protein if single and type(ligands) is not list: ligands = [ligands] desc_size = len( self.ligand_types ) * self.cutoff.shape[0] if self.aligned_pairs else len( self.ligand_types) * len(self.protein_types) * self.cutoff.shape[0] out = np.zeros(desc_size, dtype=int) for mol in ligands: mol_dict = atoms_by_type(mol.atom_dict, self.ligand_types, self.mode) if self.aligned_pairs: pairs = zip(self.ligand_types, self.protein_types) else: pairs = [(mol_type, prot_type) for mol_type in self.ligand_types for prot_type in self.protein_types] #desc = np.array([(distance(atoms_by_type(protein.atom_dict, [prot_type], self.mode)[prot_type]['coords'], atoms_by_type(mol.atom_dict, [mol_type], self.mode)[mol_type]['coords'])[..., np.newaxis] <= self.cutoff).sum(axis=(0,1)) for mol_type, prot_type in pairs], dtype=int).flatten() local_protein_dict = self.protein.atom_dict[(distance( self.protein.atom_dict['coords'], mol.atom_dict['coords']) <= self.cutoff.max()).any(axis=1)] prot_dict = atoms_by_type(local_protein_dict, self.protein_types, self.mode) desc = [] for mol_type, prot_type in pairs: d = distance(prot_dict[prot_type]['coords'], mol_dict[mol_type]['coords'])[..., np.newaxis] if len(self.cutoff) > 1: count = ((d > self.cutoff[..., 0]) & (d <= self.cutoff[..., 1])).sum(axis=(0, 1)) #count = ne.evaluate('(d > c0) & (d <= c1)', {'d': d, 'c0': cutoff[...,0], 'c1': self.cutoff[...,1]}).sum(axis=(0,1)) else: count = (d <= self.cutoff).sum() desc.append(count) desc = np.array(desc, dtype=int).flatten() out = np.vstack((out, desc)) return out[1:]
def count_groups(self): counter = np.zeros(self.neurons.shape[0]) for row in self.data: minn = 0 min = distance(row, self.neurons[0]) for n in range(1, self.number_of_neurons): dist = distance(row, self.neurons[n]) if dist < min: min = dist minn = n counter[minn] += 1 print(counter)
def build(self, ligands, protein=None, single=False): """Builds descriptors for series of ligands Parameters ---------- ligands: iterable of oddt.toolkit.Molecules or oddt.toolkit.Molecule A list or iterable of ligands to build the descriptor or a single molecule. protein: oddt.toolkit.Molecule or None (default=None) Default protein to use as reference single: bool (default=False) Flag indicating if the ligand is single. """ if protein is None: protein = self.protein if single and type(ligands) is not list: ligands = [ligands] desc_size = ( len(self.ligand_types) * self.cutoff.shape[0] if self.aligned_pairs else len(self.ligand_types) * len(self.protein_types) * self.cutoff.shape[0] ) out = np.zeros(desc_size, dtype=int) for mol in ligands: mol_dict = atoms_by_type(mol.atom_dict, self.ligand_types, self.mode) if self.aligned_pairs: pairs = zip(self.ligand_types, self.protein_types) else: pairs = [(mol_type, prot_type) for mol_type in self.ligand_types for prot_type in self.protein_types] # desc = np.array([(distance(atoms_by_type(protein.atom_dict, [prot_type], self.mode)[prot_type]['coords'], atoms_by_type(mol.atom_dict, [mol_type], self.mode)[mol_type]['coords'])[..., np.newaxis] <= self.cutoff).sum(axis=(0,1)) for mol_type, prot_type in pairs], dtype=int).flatten() local_protein_dict = protein.atom_dict[ (distance(protein.atom_dict["coords"], mol.atom_dict["coords"]) <= self.cutoff.max()).any(axis=1) ] prot_dict = atoms_by_type(local_protein_dict, self.protein_types, self.mode) desc = [] for mol_type, prot_type in pairs: d = distance(prot_dict[prot_type]["coords"], mol_dict[mol_type]["coords"])[..., np.newaxis] if len(self.cutoff) > 1: count = ((d > self.cutoff[..., 0]) & (d <= self.cutoff[..., 1])).sum(axis=(0, 1)) # count = ne.evaluate('(d > c0) & (d <= c1)', {'d': d, 'c0': cutoff[...,0], 'c1': self.cutoff[...,1]}).sum(axis=(0,1)) else: count = (d <= self.cutoff).sum() desc.append(count) desc = np.array(desc, dtype=int).flatten() out = np.vstack((out, desc)) return out[1:]
def build(self, ligands, protein=None): """Builds descriptors for series of ligands Parameters ---------- ligands: iterable of oddt.toolkit.Molecules or oddt.toolkit.Molecule A list or iterable of ligands to build the descriptor or a single molecule. protein: oddt.toolkit.Molecule or None (default=None) Default protein to use as reference """ if protein: self.protein = protein if is_molecule(ligands): ligands = [ligands] out = [] for mol in ligands: mol_dict = atoms_by_type(mol.atom_dict, self.ligand_types, self.mode) if self.aligned_pairs: pairs = zip(self.ligand_types, self.protein_types) else: pairs = [(mol_type, prot_type) for mol_type in self.ligand_types for prot_type in self.protein_types] dist = distance(self.protein.atom_dict['coords'], mol.atom_dict['coords']) within_cutoff = (dist <= self.cutoff.max()).any(axis=1) local_protein_dict = self.protein.atom_dict[within_cutoff] prot_dict = atoms_by_type(local_protein_dict, self.protein_types, self.mode) desc = [] for mol_type, prot_type in pairs: d = distance(prot_dict[prot_type]['coords'], mol_dict[mol_type]['coords'])[..., np.newaxis] if len(self.cutoff) > 1: count = ((d > self.cutoff[..., 0]) & (d <= self.cutoff[..., 1])).sum(axis=(0, 1)) else: count = (d <= self.cutoff).sum() desc.append(count) desc = np.array(desc, dtype=int).flatten() out.append(desc) return np.vstack(out)
def latlongout(): start_t = time.time() lat = request.form['lat'] lon = request.form['lon'] dist = float(request.form['dist']) query = "select * from Earthquake " cache_name = 'result'+str(lat)+str(lon)+str(dist) if r.get(cache_name): results = cPickle.loads(r.get(cache_name)) t = 'with cache' else : con = sql.connect("database.db") cur = con.cursor() cur.execute(query) rows = cur.fetchall() t = 'without cache' i = 0 results = [] while(i < len(rows)): dest_lat = rows[i][2] dest_lon = rows[i][3] distan = distance.distance((lat,lon), (dest_lat,dest_lon)).km if(distan<dist): results.append(rows[i]) i = i+1 r.set(cache_name,cPickle.dumps(results)) end_t = time.time() - start_t print(end_t) return render_template("latlong.html",rows=results, time = end_t, cache=t)
def _validate_point(self, circle_radius, points, new_point): is_point_intersect = False for point in points: is_point_intersect = distance(point, new_point) < 2 * circle_radius if is_point_intersect: break return not is_point_intersect
def informed_t_pile_audit(contest): piles =[[] for n in range(NPILES)] start_idx = 0 last_idx = SAMPLE_0 shuffle(contest.ballots) # Initial Random Ballots for i in xrange(start_idx, last_idx): piles[i % NPILES].append(contest.ballots[i]) start_idx = last_idx last_idx = roundup(last_idx * GROWTH_RATE, SAMPLE_DIVISOR) while last_idx <= len(contest.ballots): combined_piles = list(chain(*piles)) total_score = contest.aggregator(contest.candidates, combined_piles)[1] remaining_piles = set(range(NPILES)) round_pile_size = last_idx / SAMPLE_DIVISOR for i in xrange(start_idx, last_idx): ballot = contest.ballots[i] max_index, max_value = None , None random_remaining_piles = list(remaining_piles) shuffle(random_remaining_piles) for pile_index in random_remaining_piles: current_score = contest.aggregator(contest.candidates, piles[pile_index])[1] score_with_new_ballot = contest.aggregator(contest.candidates, piles[pile_index] + [ballot])[1] improvement = distance(current_score,total_score) - distance(score_with_new_ballot,total_score) if max_value == None or improvement > max_value: max_value, max_index = improvement, pile_index if max_index == None: raise Exception("ERROR: Max index is None") piles[max_index].append(ballot) if len(piles[max_index]) == round_pile_size: remaining_piles.remove(max_index) length = [len(pile) for pile in piles] if length.count(length[0]) != len(length): raise Exception("Piles non uniform size") outcome = agreement(contest.candidates, piles, contest.aggregator) if outcome: return (outcome, last_idx) start_idx = last_idx last_idx = roundup(last_idx * GROWTH_RATE, SAMPLE_DIVISOR) return (contest.outcome[0], last_idx)
def makepath(A,B): dist=distance(A,B) total=int(dist/0.1) x=np.linspace(A[0],B[0],total) y=np.linspace(A[1],B[1],total) xy=np.concatenate([[x],[y]]).T xy=xy.tolist() return xy
def is_stars_intersect(_star1, _star2): center_dist = distance(get("x", "y")(_star1), get("x", "y")(_star2)) if (center_dist < _star1["r_inner"] + _star2["r_outer"]): return True elif (center_dist < _star1["r_outer"] * 2): return hard_check(_star1, _star2) else: return False
def build(self, ligands, protein = None, single = False): if protein is None: protein = self.protein if single: ligands = [ligands] # prot_dict = atoms_by_type(protein.atom_dict, self.protein_types, self.mode) desc_size = len(self.ligand_types) if self.aligned_pairs else len(self.ligand_types)*len(self.protein_types) out = np.zeros(desc_size, dtype=int) for mol in ligands: # mol_dict = atoms_by_type(mol.atom_dict, self.ligand_types, self.mode) if self.aligned_pairs: #desc = np.array([(distance(prot_dict[str(prot_type)]['coords'], mol_dict[str(mol_type)]['coords']) <= self.cutoff).sum() for mol_type, prot_type in zip(self.ligand_types, self.protein_types)], dtype=int) # this must be LAZY! desc = np.array([(distance(atoms_by_type(protein.atom_dict, [prot_type], self.mode)[prot_type]['coords'], atoms_by_type(mol.atom_dict, [mol_type], self.mode)[mol_type]['coords']) <= self.cutoff).sum() for mol_type, prot_type in zip(self.ligand_types, self.protein_types)], dtype=int) else: desc = np.array([(distance(atoms_by_type(protein.atom_dict, [prot_type], self.mode)[prot_type]['coords'], atoms_by_type(mol.atom_dict, [mol_type], self.mode)[mol_type]['coords']) <= self.cutoff).sum() for mol_type in self.ligand_types for prot_type in self.protein_types], dtype=int) out = np.vstack((out, desc)) return out[1:]
def sorted_neurons_by_distance(self, point): distances = [] for i in range(self.number_of_neurons): distances.append(distance(self.neurons[i], point)) distances = np.reshape(distances, (self.number_of_neurons, 1)) self.neurons = np.hstack((self.neurons, distances)) self.neurons = self.neurons[np.argsort(self.neurons[:, -1])] self.neurons = np.delete(self.neurons, -1, 1) return self.neurons
def calculate_distance(self, x): min_d = float('inf') closest = 0 for i, x2 in enumerate(self.observations): d = distance(x, x2) if d<min_d: # Update min_d min_d = d closest = i self.idx = closest return min_d, closest
def power_method(M_final, tolerance = .001, init = None): size = M_final.shape[0] init = init or (1 / size) * np.ones(size) dist_old = init dist_new = M_final.dot(dist_old) while distance(dist_old, dist_new) > tolerance: dist_old = dist_new dist_new = M_final.dot(dist_old) scaled_result = dist_new / dist_new.sum() return scaled_result
def hard_check(_star1, _star2): p1arr = generate_accurate_star_points(_star1) p2arr = generate_accurate_star_points(_star2) dr = ((p1arr[0][1] - p1arr[0][0])**2 + (p2arr[1][1] - p2arr[1][0])**2)**0.5 for p1 in zip(p1arr[0], p1arr[1]): for p2 in zip(p2arr[0], p2arr[1]): if distance(p1, p2) < dr: return True return False
def dunnIndex(labels, datapoints, clusters=None): points = datapoints.shape[0] if clusters is None: clusters = np.max(labels) + 1 centroids = np.zeros(shape=(clusters, datapoints.shape[1])) for i in range(clusters): if datapoints[labels == i, :].shape[0] != 0: centroids[i, :] = np.mean(datapoints[labels == i, :], axis=0) #distances = np.zeros(shape = (clusters, clusters)) distancesC = distance(centroids, centroids, metric='euclidean') for i in range(clusters): distancesC[i][i] = 1000000000 maxIntraCluster = np.zeros(clusters) for i in range(clusters): datapointsHere = datapoints[labels == i] pointsHere = datapointsHere.shape[0] if pointsHere != 0: distances = distance(datapointsHere, datapointsHere, metric='euclidean') maxIntraCluster[i] = np.max(distances) return np.min(distancesC) / np.max(maxIntraCluster)
def is_stars_intersect(_star1, _star2): r_dist = _star1["r_inner"] + _star2["r_outer"] center_dist = distance(get("x", "y")(_star1), get("x", "y")(_star2)) print("R_DIST: ") print(r_dist) print("CENTER_DIST: ") print(center_dist) if (center_dist < r_dist): return True elif (center_dist < _star1["r_outer"] * 2): print("Stars are too close for additional check") return hard_check(_star1, _star2) else: return False
def __init__(self, dataset, lamb, epochs, lrate, generate_plot=False, col1=None, col2=None): lrate_i = 0.002 for e in range(epochs): if generate_plot: dataset.generate_plot(col1, col2, e) dataset.shuffle() for row in dataset.data: for n in range(dataset.number_of_neurons): nearest = dataset.get_nearest_neuron(row) dist = distance(dataset.neurons[n], nearest) dataset.neurons[n] += lrate * self.gaussian_neighborhood(dist, lamb) * (row - dataset.neurons[n]) if lrate - lrate_i > 0: lrate -= lrate_i dataset.create_animation('kohonen.gif') dataset.count_groups() dataset.generate_neurons()
def DTW(series1, series2, warp=1): series1, series2 = series1.reshape(-1, 1), series2.reshape(-1, 1) M = len(series1)+1 N = len(series2)+1 D0 = np.zeros([M, N]) D0[0, 1:] = np.inf D0[1:, 0] = np.inf D1 = D0[1:, 1:] D0[1:, 1:] = distance(series1, series2, measure) for i in range(M-1): for j in range(N-1): mins = [D0[i, j]] for k in range(1, warp + 1): mins += [D0[min(i + k, M-1), j], D0[i, min(j + k, N-1)]] D1[i, j] += min(mins) return D1[-1, -1] / sum(D1.shape)
def get_3D_projection_from_corrs(big_corr_list): distance_c = np.zeros((big_corr_list.shape[0], big_corr_list.shape[0])) for ind, (c1, c2) in enumerate( itertools.combinations(range(big_corr_list.shape[0]), 2)): c1t = np.triu(big_corr_list[c1]).flatten() c2t = np.triu(big_corr_list[c2]).flatten() if c1 > c2: c1, c2 = c2, c1 distance_c[c1, c2] = distance(c1t, c2t) distance_c[c2, c1] = distance_c[c1, c2] Y_original, evals = cmdscale(distance_c) Y = Y_original[:, :3] print('Y shape: ', Y.shape[0]) return Y
def saveCalibrationImage(i, imgset, dims): """ Save our image in the calibration set """ if (len(imgset)): lastcb = imgset[-1].findChessboard(dims, subpixel = False) thiscb = i.findChessboard(dims, subpixel = False) cbmid = len(lastcb.coordinates()) / 2 if distance(lastcb.coordinates()[cbmid], thiscb.coordinates()[cbmid]) < 30: showText(i, "Move the chessboard around inside the green rectangle") return imgset.append(i.copy()) global save_location if not save_location: return filename = save_location + "_image" + str(len(imgset)) + ".png" imgset[-1].save(filename)
def kmeans(self, dataset, generate_plot=False, col1=None, col2=None): start_neurons = dataset.neurons while True: previous_neurons = dataset.neurons.copy() if generate_plot: dataset.generate_plot(col1, col2, self.i) self.i += 1 for g in range(dataset.number_of_neurons): group = np.empty(shape=(0, len(dataset.data[0]))) for r in range(dataset.length): distances = np.zeros(shape=dataset.number_of_neurons) for n in range(dataset.number_of_neurons): distances[n] = distance(dataset.neurons[n], dataset.data[r]) index = np.where(distances == min(distances)) index = int(index[0][0]) if g == index: group = np.vstack((group, dataset.data[r])) dataset.neurons[g] = np.mean(group, axis=0) if np.array_equal(dataset.neurons, previous_neurons): break error = self.quantization_error(dataset) if error < self.q_error: self.q_error = error self.neurons = start_neurons
def _distance(args): """ wrapper function for Pool.map support """ return distance(*args)
from SimpleSeer.models import * import numpy as np from scipy.spatial.distance import euclidean as distance meas = Measurement.objects( method = "closestcolor" )[0] pallette = meas.parameters['pallette'] colormatches = {} for f in Frame.objects: clr = f.features[0].meancolor detected = f.results[0].string if not detected in colormatches: colormatches[detected] = [] colormatches[detected].append(clr) means = {} for match in colormatches.keys(): count = len(colormatches[match]) mean = np.mean(colormatches[match], 0) means[match] = list(mean) stdev = np.std(colormatches[match], 0) dist = distance(pallette[match], mean) print "%d %s matches meancolor distance %f std %s " % (count, match, dist, stdev) print "new pallette" print str(means)
ligand_acceptors_nbr.append(nbr) if atom.type in ['O3-', '02-' 'O-']: # types from mol2 and Chimera, ref: http://www.cgl.ucsf.edu/chimera/docs/UsersGuide/idatm.html ligand_salt_minus.append(atom.coords) if atom.OBAtom.IsMetal(): ligand_metal.append(atom.coords) ligand_donors = np.array(ligand_donors) ligand_acceptors = np.array(ligand_acceptors) ligand_salt_plus = np.array(ligand_salt_plus) ligand_salt_minus = np.array(ligand_salt_minus) ligand_hydrophobe = np.array(ligand_hydrophobe) ligand_metal = np.array(ligand_metal) # detect hbonds if len(ligand_donors) > 0: for res, lig in np.argwhere(distance(protein_acceptors, ligand_donors) < hbond_cutoff): hbond = True # assume that ther is hbond, than check angles for dn in protein_acceptors_nbr[res]: for an in ligand_donors_nbr[lig]: v_da = ligand_donors[lig] - protein_acceptors[res] v_ad = protein_acceptors[res] - ligand_donors[lig] v_dn = dn - protein_acceptors[res] v_an = an - ligand_donors[lig] # check if hbond should be discarded if angle(v_an, v_ad) < 120/len(protein_acceptors_nbr[res]) - hbond_tolerance or angle(v_da, v_dn) < 120/len(ligand_donors_nbr[lig]) - hbond_tolerance: hbond = False break if not hbond: break if hbond: hbond_acceptor.append(protein_acceptors_res[res])
def main(): global dv, size_field, max_velocity, max_acceleration, t # change this later data=globalset() n = data[0] size_field = data[1] max_velocity = data[2]##0.5 these works for smaller radiuses, also produces the dancing thingy mentioned in the paper max_acceleration = data[3]##0.5 dv = data[4]#0.1 # Step size when looking for new velocities t = data[5] # timestep I guess simulation_time = data[6] radius = data[7] pos,goal=init_pos_goal(n,size_field) #goals = pos[::-1] agents = create_agents(n, radius, pos, goal) #agentA = Agents([1,1], [0.5, 0.5], [8,8], radius,'r') # position, velocity, goal, radius, color #agentB = Agents([8,8], [1, -0.5], [1,1], radius, 'b') #agentC = Agents([1,8], [1, 2], [8,1], radius, 'y') #agentD = Agents([8,1], [-1, 2], [1,8], radius, 'g') #agentF = Agents([1, 5], [1, 2], [8, 5], radius, 'b') #agentG = Agents([8, 5], [-1, 2], [1, 5], radius, 'b') #agents = [agentA, agentB]#, agentC, agentD, agentF, agentG] fig, ax = plt.subplots() # Fig ska man aldrig bry sig om om man inte vill ändra själva plotrutan boundary_polygons = init_map_old(size_field, ax, radius) #guid=Guid(size_field,boundary_polygons) # guid.update(agents,radius*2) # guid.astar(agents[0]) # plt.show() # Consider the obstacles as agents with zero velocity! Don't consider these when updating velocities etc obstacle_agents = create_fake_agents(len(boundary_polygons), radius, boundary_polygons, 1) time = 0 avoid = [agents+obstacle_agents] # want to send in both agents and obstacles when creating VOs counting=np.zeros(n) while time < simulation_time: if(sum(counting)==n): print("time is ",time) break #guid.update(agents,radius*2) for agent in agents: if distance(agent.pos,agent.final)<0.1: index=agents.index(agent) counting[index]=1 #print("agent ",index," get!") continue # if (time>simulation_time/4*3): # agent.goal=agent.final # else: # next=guid.astar(agent) # agent.goal=next VOs = agent.calculate_velocity_obstacles(avoid[0], boundary_polygons) #if retreat: # print("Fly din dåre!") # print(agent.vel) # preffered_vel = [agent.vel[0]*(-1), agent.vel[1]*(-1)] # Helt om! # print(preffered_vel) #else: preffered_vel = agent.find_preffered_velocity() #print("preffered velocity", preffered_vel) new_velocity = agent.avoidance_strategy(preffered_vel) agent.pos = [agent.pos[0]+new_velocity[0]*t, agent.pos[1]+new_velocity[1]*t] agent.vel = new_velocity if time==0: anims = [] #goals= [] patches = [] fs = [] for agent in agents: dd = ax.plot(agent.pos[0],agent.pos[1],'go') anims.append(dd) #pp = ax.plot(agent.goal[0],agent.goal[1],'ro') #goals.append(pp) #ww = PolygonPatch(agent.velocity_objects[0], facecolor='#ff3333', edgecolor='#6699cc', alpha=0.5, zorder=2) #patches.append(ww) oo = ax.add_artist(agent.shape) fs.append(oo) #for patch in patches: # ax.add_patch(patch) #anim1, anim2, anim3, anim4, anim5, anim6 = ax.plot(agentA.pos[0],agentA.pos[1],'go',agentB.pos[0],agentB.pos[1],'bo', agentC.pos[0],agentC.pos[1],'ro', agentD.pos[0],agentD.pos[1],'yo', agentF.pos[0],agentF.pos[1],'yo', agentG.pos[0],agentG.pos[1],'yo') #patch = PolygonPatch(agentA.velocity_objects[0], facecolor='#ff3333', edgecolor='#6699cc', alpha=0.5, zorder=2) #ax.add_patch(patch) #patch1a = PolygonPatch(agentA.velocity_objects[1], facecolor='#ff3333', edgecolor='#6699cc', alpha=0.5, zorder=2) #ax.add_patch(patch1a) #patch2 = PolygonPatch(agentA.velocity_objects[2], facecolor='#ff3333', edgecolor='#6699cc', alpha=0.5, zorder=2) #ax.add_patch(patch2) #patch3a = PolygonPatch(agentA.velocity_objects[3], facecolor='#ff3333', edgecolor='#6699cc', alpha=0.5, zorder=2) #ax.add_patch(patch3a) #patch4 = PolygonPatch(agentA.velocity_objects[4], facecolor='#ff3333', edgecolor='#6699cc', alpha=0.5, zorder=2) #ax.add_patch(patch4) ax.axis([-0, size_field, -0, size_field], 'equal') #f = ax.add_artist(agentA.shape) #f2 = ax.add_artist(agentB.shape) #f3 = ax.add_artist(agentC.shape) #f4 = ax.add_artist(agentD.shape) #f5 = ax.add_artist(agentF.shape) #f6 = ax.add_artist(agentG.shape) #vel1 = ax.quiver(agentB.pos[0], agentB.pos[1], agentB.vel[0], agentB.vel[1], scale=6, scale_units ='width') ##in case we want velocity vector #vel2 = ax.quiver(agentA.pos[0], agentA.pos[1], agentA.vel[0], agentA.vel[1], scale=6, scale_units ='width') ##in case we want velocity vector #vel3 = ax.quiver(agentC.pos[0], agentC.pos[1], agentC.vel[0], agentC.vel[1], scale=6, scale_units ='width') ##in case we want velocity vector #vel4 = ax.quiver(agentD.pos[0], agentD.pos[1], agentD.vel[0], agentD.vel[1], scale=6, scale_units ='width') ##in case we want velocity vector else: for (agent,anim, f) in zip(agents,anims,fs): #patch.remove() #patch = PolygonPatch(agent.velocity_objects[0], facecolor='#ff3333', edgecolor='#6699cc', alpha=0.5, zorder=2) #ax.add_patch(patch) anim[0].set_data(agent.pos[0],agent.pos[1]) #goal[0].set_data(agent.goal[0],agent.goal[1]) f.center= agent.pos[0],agent.pos[1] ax.plot(agent.final[0], agent.final[1],'r*') #anim2.set_data(agentB.pos[0], agentB.pos[1]) #anim3.set_data(agentC.pos[0], agentC.pos[1]) #anim4.set_data(agentD.pos[0], agentD.pos[1]) #anim5.set_data(agentF.pos[0], agentF.pos[1]) #anim6.set_data(agentG.pos[0], agentG.pos[1]) #velocity_object = Polygon([pos11, tp22, tp11]) #s11 = patch.get_path() #print(s11) #patch.bounds= (ps11, tp22, tp11)# = PolygonPatch(agentA.velocity_objects[0], facecolor='#ff3333', edgecolor='#6699cc', alpha=0.5, zorder=2) #print(type(patch)) #patch.remove() #patch = PolygonPatch(agentA.velocity_objects[0], facecolor='#ff3333', edgecolor='#6699cc', alpha=0.5, zorder=2) #ax.add_patch(patch) #patch1a.remove() #patch1a = PolygonPatch(agentA.velocity_objects[1], facecolor='#ff3333', edgecolor='#6699cc', alpha=0.5, zorder=2) #ax.add_patch(patch1a) #patch2.remove() #patch2 = PolygonPatch(agentA.velocity_objects[2], facecolor='#ff3333', edgecolor='#6699cc', alpha=0.5, zorder=2) #ax.add_patch(patch2) #patch3a.remove() #patch3a = PolygonPatch(agentA.velocity_objects[3], facecolor='#ff3333', edgecolor='#6699cc', alpha=0.5, zorder=2) #ax.add_patch(patch3a) #patch4.remove() #patch4 = PolygonPatch(agentA.velocity_objects[4], facecolor='#ff3333', edgecolor='#6699cc', alpha=0.5, zorder=2) #ax.add_patch(patch4) # patch2.remove() # patch2 = PolygonPatch(agentB.velocity_objects[0], facecolor='#FFFF00', edgecolor='#FFFF00', alpha=0.5, zorder=2) # ax.add_patch(patch2) # patch3.remove() # patch3 = PolygonPatch(agentC.velocity_objects[0], facecolor='#FFFF00', edgecolor='#FFFF00', alpha=0.5, zorder=2) # ax.add_patch(patch3) # patch4.remove() # patch4 = PolygonPatch(agentD.velocity_objects[0], facecolor='#FFFF00', edgecolor='#FFFF00', alpha=0.5, zorder=2) # ax.add_patch(patch4) #f.center= agentA.pos[0],agentA.pos[1] #f2.center = agentB.pos[0], agentB.pos[1] #f3.center = agentC.pos[0], agentC.pos[1] #f4.center = agentD.pos[0], agentD.pos[1] #f5.center = agentF.pos[0], agentF.pos[1] #f6.center = agentG.pos[0], agentG.pos[1] #vel1.remove() #vel2.remove() #vel3.remove() #vel4.remove() #vel1 = ax.quiver(agentB.pos[0], agentB.pos[1], agentB.vel[0], agentB.vel[1], scale=6, scale_units ='width') ##in case we want velocity vector #vel2 = ax.quiver(agentA.pos[0], agentA.pos[1], agentA.vel[0], agentA.vel[1], scale=6, scale_units ='width') ##in case we want velocity vector #vel3 = ax.quiver(agentC.pos[0], agentC.pos[1], agentC.vel[0], agentC.vel[1], scale=6, scale_units ='width') ##in case we want velocity vector #vel4 = ax.quiver(agentD.pos[0], agentD.pos[1], agentD.vel[0], agentD.vel[1], scale=6, scale_units ='width') ##in case we want velocity vector #f = ax.add_artist(agentA.shape) #f2 = ax.add_artist(agentB.shape) #ax.quiver(agentB.pos[0], agentB.pos[1], agentB.vel[0], agentB.vel[1], scale=6, scale_units ='width') ##in case we want velocity vector #ax.quiver(agentA.pos[0], agentA.pos[1], agentA.vel[0], agentA.vel[1], scale=6, scale_units ='width') ##in case we want velocity vector #ax.plot(agentA.goal[0], agentA.goal[1],'r*') #ax.plot(agentB.goal[0], agentB.goal[1],'g*') #ax.plot(agentC.goal[0], agentC.goal[1],'b*') #ax.plot(agentD.goal[0], agentD.goal[1],'y*') #ax.plot(agentF.goal[0], agentF.goal[1],'b*') #ax.plot(agentG.goal[0], agentG.goal[1],'b*') time += 1 print("time:",time,"sum is ",sum(counting)) plt.pause(0.01)
def astar(self,agent): node_star=deepcopy(self.nodes) pos=agent.pos mindis=np.Inf st=0 ed=0 mined=np.Inf ed_pos=agent.final # print(agent.pos) # print(agent.final) for nd in node_star: #print(nd.pos) dis=distance(nd.pos,pos) if dis<mindis: st=nd mindis=dis eddis=distance(nd.pos,ed_pos) if(eddis<mined): ed=nd mined=eddis # print(ed.pos) # print(st.pos) # plt.plot(ed.pos[0],ed.pos[1],'b+') # plt.plot(st.pos[0],st.pos[1],'r+') # print(node_star.index(st)) # print(node_star.index(ed)) if(mindis>distance(agent.pos,agent.final)): return agent.final st.G=0 thisnode=st openset=[] closeset=[] #print("astar!!") while(1): closeset.append(thisnode) if((ed in openset) or (ed in closeset)): break n_id=node_star.index(thisnode) #print(n_id) for nebid in thisnode.neb: nb=node_star[nebid] if (nb in closeset): #print("1") continue elif (nb in openset): #print("2") newG,newscore=self.score(thisnode,nb,ed.pos) if(newscore<nb.F): nb.F=newscore nb.G=newG nb.father=n_id else: #print("3") newG,newscore=self.score(thisnode,nb,ed.pos) nb.F=newscore nb.G=newG nb.father=n_id #print("open add!!", nb.pos,"F=:",nb.F) openset.append(nb) minF=np.Inf; for nd in openset: if (nd.F<=minF): thisnode=nd minF=nd.F openset.remove(thisnode) #print("pickone!", thisnode.pos,"F=:",thisnode.F) if(len(openset)==0): break #print("traceback") ##traceback tc=[] while (node_star[ed.father]!=st ): if ed.father==-1: return agent.final #print(ed.father) #plt.plot(ed.pos[0],node_star.index(ed.father),'g+') #showedage(ed,node_star[ed.father],'r-') tc.append(ed) ed=node_star[ed.father] ltc=len(tc) if ltc<=4: target=ed else: target=tc[math.floor(ltc/4*3)] #target=ed return target.pos
def score(self,n1,n2,final): G=n1.G+distance(n1.pos,n2.pos)*(n2.weight+1) H=distance(n2.pos,final)*1.2 return G,G+H
def ellipse_dist(self, _ellipse1, _ellipse2): return distance(get("x", "y")(_ellipse1), get("x", "y")(_ellipse2))
def compare(stat_vector): """Compare character usage vectors using cosine distance.""" return 1 - distance(freq_list, stat_vector)
def d(self, s, a, ns, na): # Create one big s,a array sa = np.append(s, a) nsa = np.append(ns, na) # Use scipy to compute the chebyshev distance => Max norm return distance(sa, nsa)
except ValueError, e: print '[ERROR] found here' raise AnnoyingError('F**k this') H1, _, _ = np.histogram2d(*D1.T, bins=(bx, by)) H2, _, _ = np.histogram2d(*D2.T, bins=(bx, by)) H1 /= H1.sum() H2 /= H2.sum() _x, _y = np.indices(H1.shape) coords = np.array(zip(_x.ravel(), _y.ravel())) D = distance(coords, coords) return emd(H1.ravel(), H2.ravel(), D) def _calculate_emd_1D(D1, D2, bins=40): """ Args: ----- D1, D2: two np arrays with potentially differing numbers of rows, but two columns. The empirical distributions you want a similarity over bins: number of bins in each dim """ D1 = D1[np.isnan(D1).sum(axis=-1) < 1]
def _distance(args): ''' wrapper function for Pool.map support ''' return distance(*args)
def verticalTilt(cb): #radio between the 0, 1 and 2,3 point pairs distance_ratio = distance(cb.points[0], cb.points[1]) / distance(cb.points[2], cb.points[3]) if distance_ratio > 1: return 1.0 / distance_ratio return distance_ratio
def horizontalTilt(cb): #ratio between the 0,3 and 1,2 point pairs distance_ratio = distance(cb.points[0], cb.points[3]) / distance(cb.points[1], cb.points[2]) if distance_ratio > 1: return 1.0 / distance_ratio return distance_ratio
def cluster(self): if self.hasNext: old_clusters = self.clusters[:] # calculate distance matrix dist = zeros((self.k, self.k)) for i in range(self.k - 1): c1 = self.clusters[i] compare_to = [] for j in range(i + 1, self.k): c2 = self.clusters[j] dist[i][j] = dist[j][i] = distance(c1['centroid'], c2['centroid']) print dist for i, cluster in enumerate(self.clusters): compare_to = [] for j, c2 in enumerate(self.clusters): if (tuple(cluster['centroid']) != tuple(c2['centroid'])) and\ (cluster['radius'] + c2['radius']) > dist[i][j]: compare_to.append(j) print compare_to print "clustering agents in %d: %d"\ % (i,len(old_clusters[i]['agents'])) for agent in old_clusters[i]['agents']: d1 = distance(agent.ngenome, cluster['centroid']) distances = [ distance(agent.ngenome, self.clusters[j]['centroid']) for j in compare_to ] if min(distances) < d1: closest_cluster = compare_to[Cluster.index_min( distances)] cluster['agents'].remove(agent) self.clusters[closest_cluster]['agents'].append(agent) print "completed clustering, recalculating centroids" to_remove = [] for i, cluster in enumerate(self.clusters): print "old", cluster['centroid'], cluster['radius'], len( cluster['agents']) # centroid calculation using numpy for optimizaiton if len(cluster['agents']) > 0: genomes = array( [agent.ngenome for agent in cluster['agents']]) cluster['centroid'] = average(genomes, axis=0) cluster['radius'] = max( [distance(g, cluster['centroid']) for g in genomes]) print "new", cluster['centroid'], cluster['radius'], len( cluster['agents']) else: to_remove.append(i) to_remove.reverse() for cluster in to_remove: del self.clusters[cluster] while len(self.clusters) < self.k: self._random_cluster() self.iterations += 1
def improvement(ballots, new_ballot, target_mean, comparison__method=mean_votes): current_mean = mean_votes(ballots) initial_distance = distance(current_mean,target_mean) new_mean = mean_votes(vstack((ballots,new_ballot))) new_distance = distance(new_mean,target_mean) return initial_distance - new_distance