forked from carlosqsilva/pyspc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyspc_remi.py
246 lines (193 loc) · 9.51 KB
/
pyspc_remi.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Carlos Henrique Silva <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import remi.gui as gui
import remi.server as server
from remi import App, start
from matplotlib.backends.backend_agg import FigureCanvasAgg
import os
import pandas as pd
import threading
import time
from io import BytesIO
from pyspc import *
control_charts = {"Cusum": [cusum],
"Ewma": [ewma],
"Sbar": [xbar_sbar, sbar],
"Rbar": [xbar_rbar, rbar],
"T-Square Single": [Tsquare_single],
"T-Square": [Tsquare],
"MEWMA": [mewma],
"Moving Range": [xmr, mr],
"C Chart": [c],
"NP Chart": [np],
"P Chart": [p],
"U Chart": [u],
"I-MR-R": [imrx, imrmr, imrr],
"I-MR-S": [imrx, imrmr, imrstd]}
class MatplotImage(gui.Image):
ax = None
def __init__(self, **kwargs):
super(MatplotImage, self).__init__("/%s/get_image_data?update_index=0" % id(self), **kwargs)
self._buf = None
self._buflock = threading.Lock()
self.data = None
self.layer = []
# self.redraw()
def redraw(self):
self.figure = spc(data=self.data) + rules()
for chart in self.layer:
self.figure + chart()
self.figure.make(figsize=(8, 6))
canv = FigureCanvasAgg(self.figure.fig)
buf = BytesIO()
canv.print_figure(buf, format='png')
with self._buflock:
if self._buf is not None:
self._buf.close()
self._buf = buf
i = int(time.time() * 1e6)
self.attributes['src'] = "/%s/get_image_data?update_index=%d" % (id(self), i)
super(MatplotImage, self).redraw()
def get_image_data(self, update_index):
with self._buflock:
if self._buf is None:
return None
self._buf.seek(0)
data = self._buf.read()
return [data, {'Content-type': 'image/png'}]
bgcolor = '#DCDCDC'
overflow = 'hidden'
display = 'block'
class Pyspc(App):
def __init__(self, *args):
super(Pyspc, self).__init__(*args)
def main(self):
# root widget
# vertical_container = gui.Widget(width='100%', height='100%')
# vertical_container.style['background-color'] = bgcolor
# vertical_container.style['display'] = display
# vertical_container.style['overflow'] = overflow
horizontal_container = gui.Widget(width='100%', height='100%', layout_orientation=gui.Widget.LAYOUT_HORIZONTAL)
horizontal_container.style['background-color'] = bgcolor
horizontal_container.style['display'] = display
horizontal_container.style['overflow'] = 'scroll'
horizontal_container.style['text-align'] = 'center'
sub_container_left = gui.Widget(width='50%', layout_orientation=gui.Widget.LAYOUT_VERTICAL)
sub_container_left.style['background-color'] = bgcolor
sub_container_left.style['display'] = display
sub_container_left.style['overflow'] = overflow
sub_container_left.style['text-align'] = 'center'
sub_container_right = gui.Widget(width='50%', layout_orientation=gui.Widget.LAYOUT_VERTICAL)
sub_container_right.style['background-color'] = bgcolor
sub_container_right.style['display'] = display
sub_container_right.style['overflow'] = overflow
sub_container_right.style['text-align'] = 'center'
vbox1 = gui.Widget(width='100%', layout_orientation=gui.Widget.LAYOUT_HORIZONTAL, margin='0px')
vbox1.style['background-color'] = bgcolor
open_file = gui.Button('Open File', width='auto', height=30, margin='10px')
open_file.set_on_click_listener(self, 'open_file_pressed')
open_file.attributes['title'] = 'Clik here to open a .CSV file with your data'
# open_file.style['font-size'] = 'large'
open_clipboard = gui.Button('From Clipboard', widht='auto', height=30, margin='10px')
open_clipboard.set_on_click_listener(self, 'open_clipboard_pressed')
open_clipboard.attributes['title'] = 'Clik here to Paste data from the Clipboard eg: excel, html table, etc...'
# open_clipboard.style['font-size'] = 'large'
self.data_table = gui.Table(width='100%', height='100%', margin='10px')
self.data_table.style['overflow'] = overflow
# self.data_table.style['display'] = display
vbox1.append(open_file)
vbox1.append(open_clipboard)
vbox2 = gui.Widget(width='100%', layout_orientation=gui.Widget.LAYOUT_HORIZONTAL, margin='0px')
vbox2.style['background-color'] = bgcolor
charts = ["Cusum", "Ewma", "Sbar", "Rbar", "T-Square Single", "T-Square", "MEWMA", "C Chart",
"NP Chart", "P Chart", "U Chart", "Moving Range", "I-MR-R", "I-MR-S"]
self.dropdown = gui.DropDown.new_from_list(charts, width='auto', height=30, margin='10px')
self.dropdown.attributes['title'] = 'Choose the right control for the that you have'
# self.dropdown.style['font-size'] = 'large'
plot_chart = gui.Button('Plot Chart', width='auto', height=30, margin='10px')
plot_chart.set_on_click_listener(self, 'plot_chart_pressed')
# plot_chart.style['font-size'] = 'large'
save_chart = gui.Button('Save Chart', width='auto', height=30, margin='10px')
save_chart.set_on_click_listener(self, 'save_chart_pressed')
# save_chart.style['font-size'] = 'large'
self.mpl = MatplotImage(width='100%', height='auto')
self.mpl.style['margin'] = '10px'
vbox2.append(self.dropdown)
vbox2.append(plot_chart)
vbox2.append(save_chart)
self.status_txt = gui.Label('', width='100%', height=20, margin='10px')
sub_container_left.append(vbox1)
sub_container_left.append(self.status_txt)
sub_container_left.append(self.data_table)
sub_container_right.append(vbox2)
sub_container_right.append(self.mpl)
horizontal_container.append(sub_container_left)
horizontal_container.append(sub_container_right)
# vertical_container.append(horizontal_container)
return horizontal_container
def open_file_pressed(self):
self.open_filedialog = gui.FileSelectionDialog(title='File Selection Dialog', message='Select a .CSV file',
multiple_selection=False, allow_folder_selection=False)
self.open_filedialog.set_on_confirm_value_listener(self, 'file_selection_confirm')
self.open_filedialog.show(self)
def file_selection_confirm(self, filepath):
try:
self.data = pd.read_csv(filepath[0])
self.data_table.empty()
table = [list(self.data.columns)] + [[str(x) for x in row] for row in self.data.values]
self.data_table.from_2d_matrix(table)
except Exception as e:
self.text_message(repr(e))
rows, columns = self.data.shape
filename = os.path.basename(filepath[0])
self.status_txt.set_text("File Selected: {0} | {1} Rows {2} Columns".format(filename, rows, columns))
def open_clipboard_pressed(self):
try:
self.data = pd.read_clipboard()
self.data_table.empty()
table = [list(self.data.columns)] + [[str(x) for x in row] for row in self.data.values]
self.data_table.from_2d_matrix(table)
except Exception as e:
self.text_message(repr(e))
pass
def plot_chart_pressed(self):
try:
chart = self.dropdown.get_value()
self.mpl.data = self.data
self.mpl.layer = control_charts[chart]
self.mpl.redraw()
except Exception as e:
self.text_message(repr(e))
def save_chart_pressed(self):
self.save_dialog = gui.InputDialog('Save Dialog', 'Give a name to file:', initial_value='Untitled.png',
width=500, height=160)
self.save_dialog.set_on_confirm_value_listener(self, 'save_dialog_confirm')
self.save_dialog.show(self)
def save_dialog_confirm(self, filename):
self.mpl.figure.save(filename)
_root = os.path.abspath(os.path.dirname(__file__))
self.status_txt.set_text('File Saved in: %s' % os.path.join(_root, filename))
def text_message(self, message):
self.custom_dialog = gui.GenericDialog(title='Dialog Box', width='600px')
text_input = gui.TextInput(single_line=False)
text_input.set_text(message)
self.custom_dialog.add_field_with_label('text_input_messade', 'System Message', text_input)
self.custom_dialog.show(self)
if __name__ == "__main__":
start(Pyspc, debug=True, address='127.0.0.1', port=8081)
# s = server.StandaloneServer(Pyspc, start=True, title="Pyspc: Statistical Process Control Library for Humans")