-
Notifications
You must be signed in to change notification settings - Fork 1
/
point-project.py
121 lines (102 loc) · 4.16 KB
/
point-project.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
import numpy as np
import matplotlib.pyplot as plt
class Plotter(object):
def __init__(self, np_arr, plane, threshold=1, visualisation=False, location=False):
self.np_arr = np_arr
self.x = np_arr[:, 0]
self.y = np_arr[:, 1]
self.z = np_arr[:, 2]
self.threshold = threshold
self.plane = plane
self.visualisation = visualisation
self.location = location
def plot(self):
xedges, yedges, zedges = self.configuration(self.x, self.y, self.z)
H, edges = np.histogramdd(self.np_arr, bins=(xedges, yedges, zedges))
if self.plane.lower().replace(' ', '') == 'xy' or self.plane.lower().replace(' ', '') == 'yx':
return self.xy(H, xedges, yedges, self.visualisation)
elif self.plane.lower().replace(' ', '') == 'zx' or self.plane.lower().replace(' ', '') == 'xz':
return self.xz(H, xedges, zedges, self.visualisation)
elif self.plane.lower().replace(' ', '') == 'yz' or self.plane.lower().replace(' ', '') == 'zy':
return self.yz(H, yedges, zedges, self.visualisation)
def configuration(self, x_vals, y_vals, z_vals):
xmax, ymax, zmax = int(x_vals.max()) + 1, int(y_vals.max()) + 1, int(z_vals.max()) + 1
xmin, ymin, zmin = int(x_vals.min()), int(y_vals.min()), int(z_vals.min())
xrange, yrange, zrange = xmax - xmin, ymax - ymin, zmax - zmin
xedges = np.linspace(xmin, xmax, (xrange + 1), dtype=int)
yedges = np.linspace(ymin, ymax, (yrange + 1), dtype=int)
zedges = np.linspace(zmin, zmax, (zrange + 1), dtype=int)
return xedges, yedges, zedges
def plotting(self, sum_of_all_z, xedges, yedges, x_vals, y_vals, labelx, labely):
plt.imshow(sum_of_all_z.transpose(), extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]],
interpolation='none',
origin='low')
plt.grid(b=True, which='both')
plt.xticks(xedges)
plt.yticks(yedges)
plt.xlabel(labelx)
plt.ylabel(labely)
plt.plot(x_vals, y_vals, 'ro')
plt.show()
def xy(self, H, xedges, yedges, visual):
"""
For histogram of points in xy plane.
"""
_sum = np.sum(H, axis=2)
loc = np.argwhere(_sum > self.threshold)
if visual:
self.plotting(_sum, xedges, yedges, self.x, self.y, 'X-AXIS', 'Y-AXIS')
arr = []
for i in loc:
arr.append(_sum[i[0], i[1]])
arr = np.vstack(arr)
arr = arr / np.linalg.norm(arr)
loc[:, 0] += int(min(self.x))
loc[:, 1] += int(min(self.y))
if self.location:
return loc
else:
return np.concatenate((loc, arr), axis=1)
def xz(self, H, xedges, zedges, visual):
"""
For histogram of points in zx plane.
"""
_sum = np.sum(H, axis=1)
loc = np.argwhere(_sum > self.threshold)
if visual:
self.plotting(_sum, xedges, zedges, self.x, self.z, 'X-AXIS', 'Z-AXIS')
arr = []
for i in loc:
arr.append(_sum[i[0], i[1]])
arr = np.vstack(arr)
arr = arr / np.linalg.norm(arr)
loc[:, 0] += int(min(self.x))
loc[:, 1] += int(min(self.z))
if self.location:
return loc
else:
return np.concatenate((loc, arr), axis=1)
def yz(self, H, yedges, zedges, visual):
"""
For histogram of points in zx plane.
"""
_sum = np.sum(H, axis=0)
loc = np.argwhere(_sum > self.threshold)
if visual:
self.plotting(_sum, yedges, zedges, self.y, self.z, 'Y-AXIS', 'Z-AXIS')
arr = []
for i in loc:
arr.append(_sum[i[0], i[1]])
arr = np.vstack(arr)
arr = arr / np.linalg.norm(arr)
loc[:, 0] += int(min(self.y))
loc[:, 1] += int(min(self.z))
if self.location:
return loc
else:
return np.concatenate((loc, arr), axis=1)
if __name__ == '__main__':
arr = np.load('sample.npy')
a = Plotter(arr, 'zx', 0, visualisation=True).plot()
b = Plotter(arr, 'zx', 0, False).plot()
c = Plotter(arr, 'yz', 0, False).plot()