def calcDistanceMatrixFastEuclidean(points): numPoints = len(points) distMat = sqrt( sum((repmat(points, numPoints, 1) - repeat(points, numPoints, axis=0))**2, axis=1)) return distMat.reshape((numPoints, numPoints))
def make_similarity_matrix(matrix, size=MIN_ALIGN): singles = matrix.tolist() points = [flatten(t) for t in tuples(singles, size)] numPoints = len(points) # euclidean distance distMat = np.sqrt(np.sum((repmat(points, numPoints, 1) - repeat(points, numPoints, axis=0))**2, axis=1, dtype=np.float32)) return distMat.reshape((numPoints, numPoints))
def calcDistanceMatrixFastEuclidean(points): print 'using calcDistanceMatrixFastEuclidean' #time.sleep(1) numPoints = len(points) #distMat = sqrt(sum(repmat((numPoints, numPoints, 1) - repeat(points, numPoints, axis=0))**2, axis=1)) distMat = sqrt(sum((repmat(points, numPoints, 1) - repeat(points, numPoints, axis=0))**2, axis=1)) return distMat.reshape((numPoints,numPoints))[0,1]
def get_dist_matrix(points): numPoints = len(points) distMat = numpy.sqrt( numpy.sum((matlib.repmat(points, numPoints, 1) - matlib.repeat(points, numPoints, axis=0))**2, axis=1)) return distMat.reshape((numPoints, numPoints))
def dist_matrix(pts): """Calculate the euclidean distance matrix (EDM) for a set of points. Parameters ---------- pts : np.ndarray (shape = (2,)) Returns ------- dist_mat : np.ndarray The distance matrix as 2d ndarray. Implementation Details ---------------------- Uses two auxiliary matrixes to easily calculate the distance from each point to every other point in the list using this approach: (1) aux matrixes: repmat(l, n1, n2): l is repeated n1 times, along axis 1, and n2 times along axis 2, so repmat(pts, len(pts), 1) = array( [ [1, 2], [4, 6], [1, 2], [4, 6] ] ) repeat(l, n, a): each element of l is repeated n times along axis a (w/o 'a' a plain list is generated), so repeat(pts, 2, 1) = array( [ [1, 2], [1, 2], [4, 6], [4, 6] ] ) (2) Pythagoras: Then, the element-wise difference of the generated matrixes is calculated each value is squared: array( [ [ 0, 0], [ 9, 16], [ 9, 16], [ 0, 0] ] ) These squares are then summed up (linewise) using sum(..., axis=1): array([ 0, 25, 25, 0]) Finally the square root is taken for each element: array([ 0., 5., 5., 0.]) To transform the list into a distance matrix reshape() is used. Example ------- >>> dist_matrix([ [1, 2], [4, 6] ]) array([[ 0., 5.], [ 5., 0.]]) >>> dist_matrix([ [1.8, 4.1, 4.0], [2.8, 4.7, 4.5], [5.2, 4.2, 4.7], ... [4.1, 4.5, 4.6], [5.7, 3.4, 4.5]]) array([[ 0. , 1.26885775, 3.47275107, 2.41039416, 3.99374511], [ 1.26885775, 0. , 2.45967478, 1.3190906 , 3.17804972], [ 3.47275107, 2.45967478, 0. , 1.14455231, 0.96436508], [ 2.41039416, 1.3190906 , 1.14455231, 0. , 1.94422221], [ 3.99374511, 3.17804972, 0.96436508, 1.94422221, 0. ]]) """ dist_mat = scipy.sqrt( matlib.sum( ( matlib.repmat(pts, len(pts), 1) - matlib.repeat(pts, len(pts), axis=0) ) ** 2, axis=1 ) ) return dist_mat.reshape((len(pts), len(pts)))
def make_similarity_matrix(matrix, size=MIN_ALIGN): singles = matrix.tolist() points = [flatten(t) for t in tuples(singles, size)] numPoints = len(points) distMat = sqrt( np.sum((repmat(points, numPoints, 1) - repeat(points, numPoints, axis=0))**2, axis=1, dtype=np.float32)) return distMat.reshape((numPoints, numPoints))
def calcDistanceMatrixFastEuclidean(points): """ Just a memory efficient way to calculate euclidian distances http://code.activestate.com/recipes/498246-calculate-the-distance-matrix-for-n-dimensional-po/ :param points: List of coordinates :return: Distance matrix """ numPoints = len(points) distMat = np.sqrt(np.sum((repmat(points, numPoints, 1) - repeat(points, numPoints, axis=0))**2, axis=1)) return distMat.reshape((numPoints,numPoints))
def ThinPlateSplines(e,nN,nN_linear,interpP): # Adjust linear by thin plate splines now = time.time() num_e = e.shape[0] ctrlpoints = nN[numpy.concatenate((numpy.array(list(range(num_e))),interpP),0),:] points = nN_linear[numpy.concatenate((numpy.array(list(range(num_e))),interpP),0),:] npnts = points.shape[0] distMat = numpy.sum((repmat(points, npnts, 1) - repeat(points, npnts, axis=0))**2, axis=1) k = distMat.reshape((npnts, npnts)) k[k<1e-320] = 1e-320 k = numpy.sqrt(k) # Calculate P matrix p = numpy.concatenate((numpy.ones((npnts,1)),points.copy()),1) # Calculate L matrix l = numpy.concatenate((numpy.concatenate((k,p),1),numpy.concatenate((p.T,numpy.zeros((4,4))),1)),0) param = numpy.dot( linalg.pinv(l) , numpy.concatenate((ctrlpoints,numpy.zeros((4,3))),0) ) # Calculate new coordinates (x',y',z') for each points pntsNum = nN_linear.shape[0] k = numpy.zeros((pntsNum,npnts)) gx = nN_linear[:,0] gy = nN_linear[:,1] gz = nN_linear[:,2] for nn in range(npnts): k[:,nn] = (gx - points[nn,0])**2 + (gy - points[nn,1])**2 + (gz - points[nn,2])**2 k[k<1e-320] = 1e-320 k = numpy.sqrt(k) p = numpy.concatenate((numpy.ones((pntsNum,1)),nN_linear.copy()),1) l = numpy.concatenate((k,p),1) wnN_linear = numpy.dot( l, param ) wnN_linear[numpy.concatenate((numpy.array(list(range(num_e))),interpP),0),:] = ctrlpoints print("ThinPlateSplines took: %f" % (time.time() - now)) return wnN_linear
def calcDistanceMatrix2(AB, distFunc=lambda delta: sqrt(sum(delta**2, axis=1))): assert (len(AB) in [1, 2] and type(AB) != ndarray) if len(AB) == 2: A, B = AB #if (A==B).all(): return calcDistanceMatrix2([A],distFunc) #A = array(A) #B = array(B) nA, dim = A.shape assert (B.shape[1] == dim) nB = B.shape[0] print A.shape, nB, B.shape, nA delta = repeat(A, nB, 0) - repmat(B, nA, 1) dist = distFunc(delta).reshape(nA, nB) # dist[i,j] = d(A[i],B[j]) del delta return dist else: # elif len(AB)==1: A = array(AB[0]) nA, dim = A.shape #max nA <= 800 rows = repeat(range(nA), nA) # 0,0,0,...,n-1,n-1 cols = array(range(nA) * nA) # 0,1,2 upper_ind = where(cols > rows)[0] # nA == (1+sqrt(1+8*len(upper_ind))/2 ##lower_ind = where(cols<rows)[0] delta = A[rows[upper_ind], :] - A[cols[upper_ind], :] del rows del cols # computes all possible combinations #dist = zeros(nA*nA) #partial_delta = delta[:,upper_ind] partial_dist = distFunc(delta) del delta partial_dist.setfield(upper_ind, dtype=int32) #dist[upper_ind] = partial_dist #dist = dist.reshape(nA, nA) # dist[i,j] = d(A[i],A[j]) for i<j #dist = dist + dist.T # make it symmetric return partial_dist
def calcDistanceMatrix2(AB, distFunc=lambda delta: sqrt(sum(delta**2,axis=1))): assert(len(AB) in [1,2] and type(AB)!=ndarray) if len(AB)==2: A,B = AB #if (A==B).all(): return calcDistanceMatrix2([A],distFunc) #A = array(A) #B = array(B) nA,dim = A.shape assert(B.shape[1]==dim) nB = B.shape[0] print A.shape,nB,B.shape,nA delta = repeat(A,nB,0) - repmat(B,nA,1) dist = distFunc(delta).reshape(nA,nB) # dist[i,j] = d(A[i],B[j]) del delta return dist else: # elif len(AB)==1: A = array(AB[0]) nA,dim = A.shape #max nA <= 800 rows = repeat(range(nA),nA) # 0,0,0,...,n-1,n-1 cols = array(range(nA)*nA) # 0,1,2 upper_ind = where(cols>rows)[0] # nA == (1+sqrt(1+8*len(upper_ind))/2 ##lower_ind = where(cols<rows)[0] delta = A[rows[upper_ind],:]- A[cols[upper_ind],:] del rows del cols # computes all possible combinations #dist = zeros(nA*nA) #partial_delta = delta[:,upper_ind] partial_dist = distFunc(delta) del delta partial_dist.setfield(upper_ind, dtype=int32) #dist[upper_ind] = partial_dist #dist = dist.reshape(nA, nA) # dist[i,j] = d(A[i],A[j]) for i<j #dist = dist + dist.T # make it symmetric return partial_dist
def _compliance(w, target_s, target_r, target_s_prime, source_s, source_r, source_s_prime, delta_s_prime, delta_r): n_target = target_r.shape[0] n_source = source_r.shape[0] s_prime_t = matlib.repeat(target_s_prime, n_source, axis=0).squeeze() s_prime_s = matlib.repmat(source_s_prime, n_target, 1).reshape(n_target * n_source, -1).squeeze() s_t = matlib.repeat(target_s, n_source, axis=0).squeeze() s_s = matlib.repmat(source_s, n_target, 1).reshape(n_target * n_source, -1).squeeze() phi = _phi(_distance(s_prime_t, s_t + (s_prime_s - s_s)), delta_s_prime) lambda_p = np.multiply(w, phi.reshape(n_target, -1)) r_t = matlib.repeat(target_r, n_source, axis=0).squeeze() r_s = matlib.repmat(source_r, n_target, 1).reshape(n_target * n_source, -1).squeeze() phi = _phi(_distance(r_t, r_s), delta_r) lambda_r = np.multiply(w, phi.reshape(n_target, -1)) return np.mean(lambda_p, 1) * np.mean(lambda_r, 1)
def _weights(target_sa, source_sa, delta_sa): n_target = target_sa.shape[0] n_source = source_sa.shape[0] sa_t = matlib.repeat(target_sa, n_source, axis=0) sa_s = matlib.repmat(source_sa, n_target, 1) dist = _distance(sa_t, sa_s) w = _phi(dist, delta_sa) w = w.reshape(n_target, -1) w /= np.sum(w, 1)[:, np.newaxis] w[np.isnan(w)] = 0 dist = dist.reshape(n_target, -1) return w, dist
def calc_transmat(file_list): features, labels = load_feats_labels(file_list) globalmean = np.array(map(np.mean, features)) globalcov = np.cov(features) pairs = zip(labels, np.transpose(features)) models = {'mean': np.array([]), 'sigma': np.array([])} # Create individual models for each chord states = get_labels() for i, label in enumerate(states): examples = filter(lambda (x, _): x == label, pairs) if examples: [_, feats] = zip(*examples) models['mean'] = np.append( models['mean'], np.array(map(np.mean, np.transpose(feats)))) covars = np.cov(np.transpose(feats)) if (not np.allclose(covars, covars.T) or np.any(linalg.eigvalsh(covars) <= 0)): print 'Invalid Covars, using globalcov' models['sigma'] = np.append(models['sigma'], globalcov) else: models['sigma'] = np.append(models['sigma'], np.cov(np.transpose(feats))) else: models['mean'] = np.append(models['mean'], globalmean) models['sigma'] = np.append(models['sigma'], globalcov) models['mean'] = models['mean'].reshape(12, models['mean'].size / 12) models['sigma'] = models['sigma'].reshape(models['sigma'].size / (12 * 12), 12, 12) n = len(states) transitions = np.zeros(shape=(n, n)) trans = zip(labels[0:-1], labels[1:]) for (i, ikey) in enumerate(states): for (j, jkey) in enumerate(states): # Add one so there is no zero probabilities transitions[i, j] = 0.01 + sum( [1 for (f, s) in trans if f == ikey and s == jkey]) priors = np.sum(transitions, 1) transitions = np.divide( transitions, ml.repeat(priors, transitions.shape[1]).reshape(transitions.shape)) priors = priors / np.sum(priors) return (models, transitions, priors)
def calc_transmat(file_list): features, labels = load_feats_labels(file_list) globalmean = np.array(map(np.mean, features)) globalcov = np.cov(features) pairs = zip(labels, np.transpose(features)) models = {'mean':np.array([]), 'sigma':np.array([])} # Create individual models for each chord states = get_labels() for i,label in enumerate(states): examples = filter(lambda (x,_): x == label, pairs) if examples: [_, feats] = zip(*examples) models['mean'] = np.append(models['mean'], np.array(map(np.mean, np.transpose(feats)))) covars = np.cov(np.transpose(feats)) if (not np.allclose(covars, covars.T) or np.any(linalg.eigvalsh(covars) <= 0)): print 'Invalid Covars, using globalcov' models['sigma'] = np.append(models['sigma'], globalcov) else: models['sigma'] = np.append(models['sigma'], np.cov(np.transpose(feats))) else: models['mean'] = np.append(models['mean'], globalmean) models['sigma'] = np.append(models['sigma'], globalcov) models['mean'] = models['mean'].reshape(12,models['mean'].size/12) models['sigma'] = models['sigma'].reshape(models['sigma'].size/(12*12),12,12) n = len(states) transitions = np.zeros(shape=(n,n)) trans = zip(labels[0:-1], labels[1:]) for (i, ikey) in enumerate(states): for (j, jkey) in enumerate(states): # Add one so there is no zero probabilities transitions[i,j] = 0.01 + sum([1 for (f, s) in trans if f == ikey and s == jkey]) priors = np.sum(transitions, 1) transitions = np.divide( transitions, ml.repeat(priors, transitions.shape[1]).reshape(transitions.shape) ) priors = priors/np.sum(priors) return (models, transitions, priors)
def initialize_all(numParticles, numTimeSteps, cluster, boxLength, hDiameter, aligned, field, \ fieldAmp, angFreq, timeSteps, shape, rAvg): """Initializes all particles and fields.""" #create empty matrices particleMoments, particleAxes = initialize_empty_matrices( numParticles, numTimeSteps, shape) #fill matrices, initialize particles and fields particleMoments, particleAxes, mStart, nStart, hApplied, particleCoords = initialize_particles(particleMoments, \ particleAxes, numParticles, numTimeSteps, cluster, boxLength, hDiameter, aligned, field, fieldAmp, angFreq, \ timeSteps, shape) #initialize "ghost" coordinates for periodic boundary conditions ghostCoords, masks = initialize_ghost_coords(particleCoords, rAvg, boxLength) distMatrixSq = repmat(ghostCoords, len(ghostCoords), 1) - repeat( ghostCoords, len(ghostCoords), axis=0) distMatrixSq = distMatrixSq.reshape( (len(ghostCoords), len(ghostCoords), 3)) distMatrix = np.sqrt(np.sum(distMatrixSq**2, axis=2)) return particleMoments, particleAxes, mStart, nStart, hApplied, particleCoords, ghostCoords, masks, \ distMatrixSq, distMatrix
def make_similarity_matrix_from_matrix(matrix): singles = matrix.tolist() points = [flatten(t) for t in tuples(singles, 1)] numPoints = len(points) distMat = sqrt(np.sum((repmat(points, numPoints, 1) - repeat(points, numPoints, axis=0))**2, axis=1, dtype=np.float32)) return distMat.reshape((numPoints, numPoints))
def initialize(): global M global mcGhost global M_coords global H_app_z global H_app_y global H_dip global An global nx, ny global R global RR global rBar global Start global Axes global aStart if cluster == 0: M_coords = np.random.rand( I, 3) * L #positions of particles. need to add limits if cluster == 1: M_coords = np.zeros((I, 3)) c_theta = np.random.rand(1) * np.pi / 2. c_phi = np.random.rand(1) * np.pi / 2. #fig = pl.figure() #ax = fig.add_subplot(111, projection='3d') for c in range(I): #M_coords[c,2] = c*diam1 #M_coords[c,0] = c*diam1 M_coords[c, 0] = c * diam1 * np.sin(c_theta) * np.cos(c_phi) M_coords[c, 1] = c * diam1 * np.sin(c_theta) * np.sin(c_phi) M_coords[c, 2] = c * diam1 * np.cos(c_theta) #ax.scatter(M_coords[c,0], M_coords[c,1], M_coords[c,2], c='m') #pl.show() if cluster == 2: M_coords = np.zeros((I, 3)) M_coords[0, 0] = diam1 M_coords[1, 1] = diam1 M_coords[2, 0] = diam1 M_coords[2, 1] = diam1 M_coords[3, 0] = 2 * diam1 M_coords[3, 1] = diam1 M_coords[4, 0] = diam1 M_coords[4, 1] = 2 * diam1 mcGhost = M_coords[:] M_theta = np.random.rand(I) * np.pi #theta M_phi = np.random.rand(I) * 2 * np.pi #phi if aligned == "yes": M[:, 0, 0] = 0 M[:, 1, 0] = 0 M[:, 2, 0] = 1 else: M[:, 0, 0] = np.sin(M_theta[:]) * np.cos(M_phi[:]) #random orientations M[:, 1, 0] = np.sin(M_theta[:]) * np.sin(M_phi[:]) M[:, 2, 0] = np.cos(M_theta[:]) Start = np.copy(M[:, :, 0]) #preserves initial conditions An_theta = np.random.rand(I) * np.pi #theta (n) An_phi = np.random.rand(I) * 2 * np.pi #phi (n) if aligned == "yes": An[:, 0] = 0 An[:, 1] = 0 An[:, 2] = 1 elif aligned == "y": An[:, 0] = 0 An[:, 1] = 1 An[:, 2] = 0 elif aligned == "y50": An[:I / 2., 0] = 0 An[:I / 2., 1] = 1 An[:I / 2., 2] = 0 An[I / 2.:, 0] = np.sin(An_theta[I / 2.:]) * np.cos( An_phi[I / 2.:]) #random orientations An[I / 2.:, 1] = np.sin(An_theta[I / 2.:]) * np.sin(An_phi[I / 2.:]) An[I / 2.:, 2] = np.cos(An_theta[I / 2.:]) else: An[:, 0] = np.sin(An_theta[:]) * np.cos(An_phi[:]) #random orientations An[:, 1] = np.sin(An_theta[:]) * np.sin(An_phi[:]) An[:, 2] = np.cos(An_theta[:]) if aligned == "yes": nx[:, 0] = 1 nx[:, 1] = 0 nx[:, 2] = 0 ny[:, 0] = 0 ny[:, 1] = 1 ny[:, 2] = 0 else: R_theta = np.random.rand(I) * np.pi #theta (n) R_phi = np.random.rand(I) * 2 * np.pi #phi (n) for i in range(I): R_n = np.array([ np.sin(R_theta[i]) * np.cos(R_phi[i]), np.sin(R_theta[i]) * np.sin(R_phi[i]), np.cos(R_theta[i]) ]) ny[i, :] = np.cross(An[i, :], R_n) nx[i, :] = np.cross(An[i, :], ny[i, :]) Axes[:, :, 0] = An aStart = np.copy(Axes[:, :, 0]) H_dip = np.zeros((I, 3)) H_app_z = h0 * np.cos(w * T) if twoD == "on": w2 = 1.05 * w H_app_y = h0 * np.sin(w2 * T) else: H_app_y = 0 * np.sin(w * T) #H_app_z = np.zeros(N) #H_app_z.fill(h0) #H_app[0:N/5.].fill(h0) #H_app[N/5.:N].fill(0) #---make ghost coordinate matrix def makecGhost(mcGhost): global g_mask_x1, g_mask_x2, g_mask_y1, g_mask_y2, g_mask_z1, g_mask_z2 g_mask_x1 = M_coords[:, 0] < rAvg g_mask_x2 = M_coords[:, 0] > L - rAvg j1 = mcGhost[g_mask_x1] j1[:, 0] += L j2 = mcGhost[g_mask_x2] j2[:, 0] -= L mcGhost = np.vstack((mcGhost, j1)) mcGhost = np.vstack((mcGhost, j2)) g_mask_y1 = mcGhost[:, 1] < rAvg g_mask_y2 = mcGhost[:, 1] > L - rAvg k1 = mcGhost[g_mask_y1] k1[:, 1] += L k2 = mcGhost[g_mask_y2] k2[:, 1] -= L mcGhost = np.vstack((mcGhost, k1)) mcGhost = np.vstack((mcGhost, k2)) g_mask_z1 = mcGhost[:, 2] < rAvg g_mask_z2 = mcGhost[:, 2] > L - rAvg l1 = mcGhost[g_mask_z1] l1[:, 2] += L l2 = mcGhost[g_mask_z2] l2[:, 2] -= L mcGhost = np.vstack((mcGhost, l1)) mcGhost = np.vstack((mcGhost, l2)) return mcGhost #---create distance matrix. stays fixed mcGhost = makecGhost(mcGhost) numPoints = len(mcGhost) dM = repmat(mcGhost, numPoints, 1) - repeat(mcGhost, numPoints, axis=0) RR = dM.reshape((numPoints, numPoints, 3)) R = np.sqrt(np.sum(RR**2, axis=2)) rBar = np.average(R[:, :], weights=(R[:, :] > 0))
def calcDistanceMatrixFastEuclidean(points): numPoints = len(points) distMat = sqrt(sum((repmat(points, numPoints, 1) - repeat(points, numPoints, axis=0))**2, axis=1)) return distMat.reshape((numPoints,numPoints))
def get_dist_matrix(points): numPoints = len(points) distMat = numpy.sqrt(numpy.sum((matlib.repmat(points, numPoints, 1) - matlib.repeat(points, numPoints, axis=0))**2, axis=1)) return distMat.reshape((numPoints,numPoints))