-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_net.py
More file actions
186 lines (153 loc) · 6.37 KB
/
python_net.py
File metadata and controls
186 lines (153 loc) · 6.37 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
import base64
import io
from os import path
import clr
import matplotlib
matplotlib.use('Agg') # Needed to stop QT backend errors
import numpy as np
from lxml import etree
from matplotlib.figure import Figure
from matplotlib.ticker import FormatStrFormatter
from functions import (generate_xml, parse_file, parse_results, round_down, round_up)
clr.AddReference("wpf\PresentationFramework")
from Microsoft.Win32 import OpenFileDialog, SaveFileDialog
from System import Convert, Uri, UriKind
from System.IO import MemoryStream, Path, StreamReader
from System.Threading import ApartmentState, Thread, ThreadStart
from System.Windows import Application, LogicalTreeHelper, Window
from System.Windows.Markup import XamlReader
from System.Windows.Media import Imaging
dir_path = path.dirname(__file__)
xaml_path = 'MainWindow.xaml'
class MyWindow(Window):
def __init__(self):
try:
stream = StreamReader(xaml_path)
self.window = XamlReader.Load(stream.BaseStream)
self.initialise_icon()
self.initialise_elements()
self.initialise_visibility()
Application().Run(self.window)
except Exception as ex:
print(ex)
def initialise_elements(self):
"""
Formerly used:
self.FileComboBox = LogicalTreeHelper.FindLogicalNode(self.window, "FileComboBox")
self.FileComboBox.IsEnabled = False
self.FileComboBox.SelectionChanged += self.FileComboBox_SelectionChanged
"""
root = etree.parse('MainWindow.xaml').getroot()
ns = root.nsmap
data = [(etree.QName(i).localname, i.attrib['Name']) for i in root.findall(".//*[@Name]", ns)]
for tag, attr in data:
self.__setattr__(attr, LogicalTreeHelper.FindLogicalNode(self.window, f"{attr}"))
if tag == 'Button':
self.__getattribute__(attr).Click += self.__getattribute__(f"{attr}_Click")
if tag == 'ComboBox':
self.__getattribute__(attr).SelectionChanged += self.__getattribute__(f"{attr}_SelectionChanged")
def initialise_icon(self):
icon_uri = Uri(dir_path + "/" + "program_icon.png", UriKind.Absolute)
self.window.Icon = Imaging.BitmapImage(icon_uri)
def initialise_visibility(self):
self.OpenFileButton.IsEnabled = True
self.SaveXMLButton.IsEnabled = False
self.FileComboBox.IsEnabled = False
def combo_clear(self):
self.FileComboBox.Items.Clear()
def log_append(self, msg):
self.LogTextBox.AppendText(msg + '\n')
def log_clear(self):
self.LogTextBox.Clear()
def clear_plot(self):
self.PlotImage.Source = None
def generate_plot(self, x, y, title):
fig = Figure()
axes = fig.add_subplot(111)
new_y = [value*0.001 for value in y]
displacement = round(max(new_y) - min(new_y), 3)
axes.plot(x, new_y, marker='.')
axes.set_title(title)
axes.set_xlabel("Time (mins)")
axes.set_ylabel("Gauge Reading (mm)")
axes.set_xlim(min(x), round_up(max(x), decimals=-2))
axes.set_ylim(round_down(min(new_y), decimals=1), max(new_y))
axes.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
self.log_clear()
self.log_append(f" Displacement = {displacement}mm")
self.log_append(f" Max Gauge Reading = {max(y)}")
self.log_append(f" Min Gauge Reading = {min(y)}")
self.log_append(f" Gauge Reading Diff = {max(y)-min(y)}")
self.log_append(f" Time Taken = {int(max(x))} min")
return fig
def set_plot(self, fig):
base64string = base64_fig(fig)
imagebytes = Convert.FromBase64String(base64string)
image = stream_bitmap(imagebytes)
self.PlotImage.Source = image
def OpenFileButton_Click(self, sender, e):
dlg = OpenFileDialog()
dlg.DefaultExt = ".txt"
dlg.Filter = "Text Files (*.txt)|*.txt"
dlg.Multiselect = True
if dlg.ShowDialog() == True:
self.file_names = dlg.FileNames
self.combo_clear()
self.log_clear()
self.log_append(f'Successfully loaded {len(self.file_names)} stages:')
for file_name in self.file_names:
stem = Path.GetFileNameWithoutExtension(file_name)
self.log_append(stem)
self.FileComboBox.Items.Add(stem)
self.SaveXMLButton.IsEnabled = True
self.FileComboBox.IsEnabled = True
def FileComboBox_SelectionChanged(self, sender, e):
self.clear_plot()
if self.FileComboBox.SelectedIndex != -1:
comboIndex = self.FileComboBox.SelectedIndex
file_name = self.file_names[comboIndex]
data = parse_file(file_name)
stem = Path.GetFileNameWithoutExtension(file_name)
fig = self.generate_plot(
x = data['Stage_StageReadings_StagePasteMins1'],
y = data['Stage_StageReadings_StagePasteDive1'],
title = stem,
)
self.set_plot(fig)
def SaveXMLButton_Click(self, sender, e):
dlg = SaveFileDialog()
dlg.Filter = "XML file (*.xml)|*.xml"
dlg.FilterIndex = 2
dlg.RestoreDirectory = True
if dlg.ShowDialog() == True:
file_name = dlg.FileName
try:
with open(file_name, 'wb') as file:
results = parse_results(self.file_names)
xml = generate_xml(results)
file.write(xml)
self.SaveXMLButton.IsEnabled = False
self.log_clear()
self.log_append("XML successfully created.")
except IOError:
print("Cannot save current data to file.")
def base64_fig(plt):
buf = io.BytesIO()
plt.savefig(buf, format='png', dpi=200)
buf.seek(0)
py_base64string = str(base64.b64encode(buf.read()))
return py_base64string[2:-1]
def stream_bitmap(imagebytes):
memoryStream = MemoryStream(imagebytes)
memoryStream.Position = 0
image = Imaging.BitmapImage()
image.BeginInit()
image.StreamSource = memoryStream
image.CacheOption = Imaging.BitmapCacheOption.OnLoad
image.EndInit()
return image
if __name__ == '__main__':
thread = Thread(ThreadStart(MyWindow))
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()