-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
251 lines (208 loc) · 8.87 KB
/
common.py
File metadata and controls
251 lines (208 loc) · 8.87 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import matplotlib.pyplot as plt
import pandas as pd
from IPython.core.display import display, HTML
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtCore import Qt
from scipy.io import loadmat
def superscript(n):
return "".join(["⁰¹²³⁴⁵⁶⁷⁸⁹"[ord(c) - ord('0')] for c in str(n)])
def subscript(n):
return "".join(["₀₁₂₃₄₅₆₇₈₉"[ord(c) - ord('0')] for c in str(n)])
def cuboid_data(center, size):
"""
Reference: https://stackoverflow.com/questions/30715083/python-plotting-a-wireframe-3d-cuboid
Create a data array for cuboid plotting.
============= ================================================
Argument Description
============= ================================================
center center of the cuboid, triple
size size of the cuboid, triple, (x_length,y_width,z_height)
:type size: tuple, numpy.array, list
:param size: size of the cuboid, triple, (x_length,y_width,z_height)
:type center: tuple, numpy.array, list
:param center: center of the cuboid, triple, (x,y,z)
"""
# suppose axis direction: x: to left; y: to inside; z: to upper
# get the (left, outside, bottom) point
o = [a - b / 2 for a, b in zip(center, size)]
# get the length, width, and height
l, w, h = size
x = [[o[0], o[0] + l, o[0] + l, o[0], o[0]], # x coordinate of points in bottom surface
[o[0], o[0] + l, o[0] + l, o[0], o[0]], # x coordinate of points in upper surface
[o[0], o[0] + l, o[0] + l, o[0], o[0]], # x coordinate of points in outside surface
[o[0], o[0] + l, o[0] + l, o[0], o[0]]] # x coordinate of points in inside surface
y = [[o[1], o[1], o[1] + w, o[1] + w, o[1]], # y coordinate of points in bottom surface
[o[1], o[1], o[1] + w, o[1] + w, o[1]], # y coordinate of points in upper surface
[o[1], o[1], o[1], o[1], o[1]], # y coordinate of points in outside surface
[o[1] + w, o[1] + w, o[1] + w, o[1] + w, o[1] + w]] # y coordinate of points in inside surface
z = [[o[2], o[2], o[2], o[2], o[2]], # z coordinate of points in bottom surface
[o[2] + h, o[2] + h, o[2] + h, o[2] + h, o[2] + h], # z coordinate of points in upper surface
[o[2], o[2], o[2] + h, o[2] + h, o[2]], # z coordinate of points in outside surface
[o[2], o[2], o[2] + h, o[2] + h, o[2]]] # z coordinate of points in inside surface
return x, y, z
def read_data(data_matfile):
"""
Reads the MATLAB data containing 4 fields viz., X, Y, Z and MN_Ratio
:param data_matfile: path to a .mat file
:return: pandas.DataFrame
"""
tb = loadmat(data_matfile)
pos = tb['pos']
df_apt = pd.DataFrame(list(zip(pos[:, 0], pos[:, 1], pos[:, 2], pos[:, 3])),
columns=['X', 'Y', 'Z', 'MN_Ratio'])
return df_apt
def view_image(image):
"""
Visualize the image such as png graphs
:param image: The image read in almost any format
:return: None
"""
fig = plt.figure(figsize=(18, 16), dpi=80, facecolor='w', edgecolor='k')
ax = fig.add_subplot(1, 2, 1)
plt.imshow(image, cmap=plt.cm.gray)
plt.axis("off")
plt.show()
def bring_df_to_positive_coord(df):
"""
To make all the coordinates ('X', 'Y', 'Z') in the dataset offset by the minimum value and into positive side
:param df: pandas.DataFrame
:return: pandas.DataFrame
"""
if (df["X"] < 0).any().any():
x_offset = abs(df['X'].min())
df['X'] = df['X'] + x_offset
if (df["Y"] < 0).any().any():
y_offset = abs(df['Y'].min())
df['Y'] = df['Y'] + y_offset
if (df["Z"] < 0).any().any():
z_offset = abs(df['Z'].min())
df['Z'] = df['Z'] + z_offset
return df
def print_dataframe_full(df):
"""
Displays the whole dataframe regardless of the size
:param df: pandas.DataFrame
:return: None
"""
with pd.option_context('display.max_rows', None, 'display.max_columns', None): # more options can be specified also
display(df)
def display_side_by_side(dfs: list, captions: list):
"""
Display tables side by side to save vertical space
Input:
dfs: list of pandas.DataFrame
captions: list of table captions
Note:
Do not use the complete dataframe in the following function, use head or tail for the display
"""
output = ""
combined = dict(zip(captions, dfs))
for caption, df in combined.items():
output += df.style.set_table_attributes("style='display:inline'").set_caption(caption)._repr_html_()
output += "\xa0\xa0\xa0"
display(HTML(output))
def show_message(message, btn1=False, btn1_name="Button1", btn1_fun=lambda: None,
btn2=False, btn2_name="Button2", btn2_fun=lambda: None,
btn3=False, btn3_name="Button3", btn3_fun=lambda: None): # Default layout has an OK Button
global show_message_btn1
global show_message_btn2
global show_message_btn3
show_message_btn1 = False
show_message_btn2 = False
show_message_btn3 = False
msg_box = QMessageBox()
msg_box.raise_()
msg_box.setWindowFlags(Qt.WindowStaysOnTopHint)
msg_box.setText(message)
if btn1:
btn1 = msg_box.addButton(btn1_name, QMessageBox.YesRole)
btn1.clicked.connect(btn1_fun)
if btn2:
btn2 = msg_box.addButton(btn2_name, QMessageBox.NoRole)
btn2.clicked.connect(btn2_fun)
if btn3:
btn3 = msg_box.addButton(btn3_name, QMessageBox.RejectRole)
btn3.clicked.connect(btn3_fun)
msg_box.exec_()
# The following codes are not required yet, can be added in future if found to be needed
# The class that supports 3D plot by adding zoom function with mouse scroll.
# reference: https://stackoverflow.com/questions/11551049/matplotlib-plot-zooming-with-scroll-wheel
# Does not inherit any UI files
# class Zoom:
# def __init__(self):
# self.cur_xlim = None
# self.cur_ylim = None
#
# def zoom_factory(self, ax, base_scale=2.):
# def zoom(event):
# cur_xlim = ax.get_xlim()
# cur_ylim = ax.get_ylim()
#
# xdata = event.xdata # get event x location
# ydata = event.ydata # get event y location
#
# if event.button == 'up':
# # deal with zoom in
# scale_factor = 1 / base_scale
# elif event.button == 'down':
# # deal with zoom out
# scale_factor = base_scale
# else:
# # deal with something that should never happen
# scale_factor = 1
# print(event.button)
#
# new_width = (cur_xlim[1] - cur_xlim[0]) * scale_factor
# new_height = (cur_ylim[1] - cur_ylim[0]) * scale_factor
#
# relx = (cur_xlim[1] - xdata) / (cur_xlim[1] - cur_xlim[0])
# rely = (cur_ylim[1] - ydata) / (cur_ylim[1] - cur_ylim[0])
#
# ax.set_xlim([xdata - new_width * (1 - relx), xdata + new_width * (relx)])
# ax.set_ylim([ydata - new_height * (1 - rely), ydata + new_height * (rely)])
# ax.figure.canvas.draw()
#
# fig = ax.get_figure() # get the figure of interest
# fig.canvas.mpl_connect('scroll_event', zoom)
#
# return zoom
# 3D Plot Window for APT dataset, opens a new Window with pan and zoom
# Does not inherit any UI files
# class Plot3D(QWidget):
# def __init__(self, xs, ys, zs, ION, parent=None):
# super(Plot3D, self).__init__(parent)
# self.fig = Figure()
# self.canvas = FigureCanvas(self.fig)
# self.axes = self.fig.add_subplot(111, projection='3d')
# self.toolbar = NavigationToolbar(self.canvas, self)
#
# self.setLayout(QVBoxLayout())
# self.layout().addWidget(self.toolbar)
# self.layout().addWidget(self.canvas)
#
# self.axes.scatter(xs, ys, zs, c='blue', s=30, label=ION, marker='.')
# self.axes.legend()
#
# self.axes.set_xlabel('X Axis')
# self.axes.set_ylabel('Y Axis')
# self.axes.set_zlabel('Z Axis')
#
# zp = Zoom()
# scale = 1.1
# zp.zoom_factory(self.axes, base_scale=scale)
# self.fig.tight_layout()
# For confirmation on closing the window
# def closeEvent(self, event):
# reply = QMessageBox.question(self, 'Window Close', 'Are you sure you want to close the window?',
# QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
#
# if reply == QMessageBox.Yes:
# event.accept()
# print('Window closed')
# else:
# event.ignore()
# For converting into JSON and back
# df_apt_non_layer.to_json(
# r'C:\Users\arjun\Downloads\APT_Code\APT_Project\df_apt_non_layer.json', orient='split')
# df_apt_non_layer = pd.read_json(r'C:\Users\arjun\Downloads\APT_Code\APT_Project\df_apt_non_layer.json',
# orient='split')