Module bioiain.utilities.maths_old
Functions
def M(axis, theta)-
Expand source code
def M(axis, theta): return expm(cross(eye(3), axis / norm(axis) * theta)) def add(v, w)-
Expand source code
def add(v, w): #TODO: make this take any dimensions x, y, z = v X, Y, Z = w return (x + X, y + Y, z + Z) def add_multiple(vectors)-
Expand source code
def add_multiple(vectors): total = [0] * len(vectors[0]) for v in vectors: total = add(total, v) return total def angle_dif_3d(angles1, angles2)-
Expand source code
def angle_dif_3d(angles1, angles2): if len(angles1) != len(angles2): print("input must be same length") quit() diffs = [] input_len = len(angles1) for i in range(input_len): pos1 = abs(angles1[i] - angles2[i]) if are_different_sign(angles1[i], angles2[i]): pos2 = 180 - abs(angles1[i]) + 180 - abs(angles2[i]) if pos1 > pos2: diffs.append(pos2) else: diffs.append(pos1) else: diffs.append(pos1) n_diffs = [] for n in diffs: # n_diffs.append(abs((n / 180) - 0.5) * 2) n_diffs.append(abs(n / 180)) # n_diffs.append(n / 180) print(angles1, "\n", angles2) print(diffs) print(1 - (sum(n_diffs) / input_len)) return 1 - (sum(n_diffs) / input_len) def angle_modulus(angle)-
Expand source code
def angle_modulus(angle): return (angle + 360) % 360 def are_different_sign(angle1, angle2)-
Expand source code
def are_different_sign(angle1, angle2): if angle1 < 0 and angle2 >= 0: return True elif angle1 >= 0 and angle2 < 0: return True else: return False def difference_between_boolean_pairs(A1, A2, B1, B2)-
Expand source code
def difference_between_boolean_pairs(A1, A2, B1, B2): diffX = [0,0] # Matches / Total diffx = [0,0] if A1 or B1: diffX[1] += 0.5 if A1 == B1: diffX[0] += 0.5 if A2 or B2: diffX[1] += 0.5 if A2 == B2: diffX[0] += 0.5 if A1 or B2: diffx[1] += 0.5 if A1 == B2: diffx[0] += 0.5 if A2 or B1: diffx[1] += 0.5 if A2 == B1: diffx[0] += 0.5 return diffX, diffx def dihedral_angle(p0, p1, p2, p3)-
Expand source code
def dihedral_angle(p0, p1, p2, p3): """Praxeolitic formula 1 sqrt, 1 cross product""" #p0 = p[0] #p1 = p[1] #p2 = p[2] #p3 = p[3] b0 = -1.0*(p1 - p0) b1 = p2 - p1 b2 = p3 - p2 # normalize b1 so that it does not influence magnitude of vector # rejections that come next b1 /= np.linalg.norm(b1) # vector rejections # v = projection of b0 onto plane perpendicular to b1 # = b0 minus component that aligns with b1 # w = projection of b2 onto plane perpendicular to b1 # = b2 minus component that aligns with b1 v = b0 - np.dot(b0, b1)*b1 w = b2 - np.dot(b2, b1)*b1 # angle between v and w in a plane is the torsion angle # v and w may not be normalized but that's fine since tan is y/x x = np.dot(v, w) y = np.dot(np.cross(b1, v), w) return np.degrees(np.arctan2(y, x))Praxeolitic formula 1 sqrt, 1 cross product
def get_closest(start, end, point)-
Expand source code
def get_closest(start, end, point): start_end = end - start start_point = point - start return pnt2line(point, start, end) def get_closest_point(point, points)-
Expand source code
def get_closest_point(point, points): shortest_dist = 999 shortest_coords = None for p in points: #print("shorter?",point,p) dist = distance(p, point) #print(dist, shortest_dist) if dist < shortest_dist: #print("shorter:" ,dist, shortest_dist) shortest_dist = dist shortest_coords = p #print("shortest:", shortest_coords) return shortest_coords def get_line(start, vector)-
Expand source code
def get_line(start, vector): end = start + vector return [start[0], end[0]], [start[1], end[1]], [start[2], end[2]] def get_middlepoint(v1, v2)-
Expand source code
def get_middlepoint(v1, v2): return (v1 + v2) / 2.0 def get_vector(start, end)-
Expand source code
def get_vector(start, end): return end - start def normalize1D(values, add_to=None)-
Expand source code
def normalize1D(values, add_to=None): maximum = max(values) minimum = min(values) r = abs(maximum-minimum) new_values = [] for v in values: if r == 0: new_values.append(v) continue new_values.append((v-minimum)/r) if add_to is not None: total = sum([v for v in new_values]) ratio = total/add_to new_values = [v/ratio for v in new_values] return new_values def pnt2line(pnt, start, end)-
Expand source code
def pnt2line(pnt, start, end): line_vec = vector(start, end) pnt_vec = vector(start, pnt) line_len = length(line_vec) line_unitvec = unit(line_vec) pnt_vec_scaled = scale(pnt_vec, 1.0 / line_len) t = dot(line_unitvec, pnt_vec_scaled) '''if t < 0.0: t = 0.0 elif t > 1.0: t = 1.0''' nearest = scale(line_vec, t) dist = distance(nearest, pnt_vec) nearest = add(nearest, start) return np.array([nearest[0], nearest[1], nearest[2]]) def points_to_line(start, end)-
Expand source code
def points_to_line(start,end): return list(zip(start,end)) def rotate_around_axis(axis, theta, point)-
Expand source code
def rotate_around_axis(axis, theta, point): m = M(axis, theta) return dot(m, point) def string_numbers()-
Expand source code
def string_numbers(): return ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] def sub_vectors(start, end)-
Expand source code
def sub_vectors(start,end): new_vector = [] for dim in range(len(start)): new_vector.append(start[dim] - end[dim]) return tuple(new_vector) def test()-
Expand source code
def test(): start = np.array([0, 0, 0]) end = np.array([1, 1, 1]) point = np.array([rnd.random(), rnd.random(), rnd.random()]) get_closest(start, end, point)