-
Notifications
You must be signed in to change notification settings - Fork 0
/
measurement.py
142 lines (105 loc) · 3.99 KB
/
measurement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from __future__ import print_function
import os
import argparse
import numpy as np
import scipy.spatial
import matplotlib.pyplot as plt
import matplotlib
from numpy.core.umath_tests import inner1d
import quat_utils
import sph_voronoi
import simplex_bound
import generators
label_size = 15
matplotlib.rcParams['xtick.labelsize'] = label_size
def normalized(a, axis=-1, order=2):
l2 = np.atleast_1d(np.linalg.norm(a, order, axis))
l2[l2 == 0] = 1
return a / np.expand_dims(l2, axis)
def misorientation(a, b):
dots = inner1d(a, b)
dots = np.minimum(1, np.maximum(-1, dots))
return 2 * np.arccos(np.abs(dots))
def read_file(input_file):
lines = open(input_file).read().split('\n')
if len(lines) == 0:
raise Exception("input file is empty")
lines = [e for e in lines if len(e) > 1]
input_format = lines[0]
if input_format not in ['euler', 'quaternion', 'matrix']:
raise Exception("invalid input format")
num = int(lines[1])
if num <= 0:
raise Exception("input file contains no orientations")
data = []
for line in lines[2:]:
data += [[float(e) for e in line.split()]]
data = np.array(data)
if input_format == 'quaternion':
assert data.shape[1] >= 4
data = data[:,:4]
elif input_format == 'euler':
assert data.shape[1] >= 3
data = np.deg2rad(data)
data = np.array([quat_utils.euler_to_quaternion(e[:3]) for e in data])
elif input_format == 'matrix':
assert data.shape[1] >= 9
data = np.array([quat_utils.rotation_matrix_to_quaternion(e[:9].reshape((3,3))) for e in data])
return data
def go(input_file, target_group, k, gdict):
basis_points = read_file(input_file)
gs, _, _ = gdict[target_group]
mapped_points = []
for g in gs:
for b in basis_points:
r = quat_utils.multiply(b, g)
mapped_points += [r]
mapped_points = np.array(mapped_points)
qs = mapped_points
n = len(qs)
qs = np.concatenate((-qs, qs))
#calculate a lower bound on the maximum error
r = simplex_bound.find_r(2 * n)
lower_bound = np.rad2deg(2 * r)
#sample the error distribution
tree = scipy.spatial.cKDTree(qs)
ps = quat_utils.random_quaternions(k)
_, indices = tree.query(ps)
ms = misorientation(ps, qs[indices])
#calculate the maximum error exactly (covering radius)
simplices = scipy.spatial.ConvexHull(qs).simplices
vs = sph_voronoi.calculate_voronoi_vertices(qs[simplices])
rs = misorientation(vs, qs[simplices[:,0]])
mmax = np.rad2deg(np.max(rs))
print("maximum misorientation: %.2f degrees" % mmax)
print(" simplex bound: %.2f degrees" % lower_bound)
print(" optimality gap: %.2f%%" % (100 * (mmax / lower_bound - 1)))
#plot the results
counts, _, _, = plt.hist(np.rad2deg(ms), bins=200)
plt.plot([lower_bound, lower_bound], [0, max(counts)], ls=':')
plt.plot([mmax, mmax], [0, max(counts)], ls=':')
plt.xlabel('Misorientation (degrees)', fontsize=label_size)
plt.ylabel('Frequency', fontsize=15)
plt.yticks([])
plt.show()
def run():
parser = argparse.ArgumentParser(description='Plot the error statistics of a set of orientations')
parser.add_argument("inputfile", help="file containing orientations")
parser.add_argument("lauegroup", help="Laue group of orientation set")
parser.add_argument("k", help="number of error distribution samples")
args = parser.parse_args()
gdict = generators.generator_dict
target_group = args.inputfile
target_group = args.lauegroup.upper()
if target_group not in gdict.keys():
ordered = sorted(gdict.keys())
ordered[-1], ordered[-2] = ordered[-2], ordered[-1]
eprint("Input error:")
eprint("Laue group must be one of [%s]" % (", ".join(["'%s'" % e for e in ordered])))
return None
input_file = args.inputfile
k = int(args.k)
assert k > 0
go(input_file, target_group, k, gdict)
if __name__ == "__main__":
run()