Commit 1529f450 authored by Claes Sjofors's avatar Claes Sjofors

Sev application for multivariate analysis added

parent 5c6ce7cf
include $(pwre_dir_symbols)
ifndef variables_mk
include $(pwre_kroot)/tools/bld/src/variables.mk
endif
ifndef rules_mk
include $(pwre_kroot)/tools/bld/src/rules.mk
endif
vpath %.py $(co_source)
source_dirs := $(co_source)
py_sources := $(sort \
$(foreach file, \
$(foreach dir, \
$(source_dirs), \
$(wildcard $(dir)/*.py) \
), $(notdir $(file)) \
) \
)
export_py := $(patsubst %.py, $(exe_dir)/%.py, $(py_sources))
clean_py := $(patsubst %.py, clean_%.py, $(py_sources))
.PHONY : all init copy lib exe clean realclean\
$(clean_py)
all : init copy | silent
init : silent
copy : $(export_py) | silent
lib : silent
exe : silent
clean :
realclean : clean $(clean_py)
silent :
@ :
$(export_py) : $(exe_dir)/%.py : %.py
@ echo "Exporting $< ..."
@ $(cp) $(cpflags) $(source) $(target)
$(clean_py) : clean_%.py : %.py
@ rm $(exe_dir)/$*.py
#!/usr/bin/python
#
# ProviewR Open Source Process Control.
# Copyright (C) 2005-2019 SSAB EMEA AB.
#
# This file is part of ProviewR.
#
# 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 2 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 ProviewR. If not, see <http://www.gnu.org/licenses/>
#
# Linking ProviewR statically or dynamically with other modules is
# making a combined work based on ProviewR. Thus, the terms and
# conditions of the GNU General Public License cover the whole
# combination.
#
# In addition, as a special exception, the copyright holders of
# ProviewR give you permission to, from the build function in the
# ProviewR Configurator, combine ProviewR with modules generated by the
# ProviewR PLC Editor to a PLC program, regardless of the license
# terms of these modules. You may copy and distribute the resulting
# combined work under the terms of your choice, provided that every
# copy of the combined work is accompanied by a complete copy of
# the source code of ProviewR (the version used to produce the
# combined work), being distributed under the terms of the GNU
# General Public License plus this exception.
#
# Sev data analysis application
from Tkinter import *
import tkFileDialog
import tkMessageBox
import sys
import time
import pwrrt
from datetime import datetime
import statsmodels.api as am
from statsmodels.sandbox.regression.predstd import wls_prediction_std
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
# adapted from http://matplotlib.org/examples/specialty_plots/hinton_demo.html
def hinton(matrix, max_weight=None, ax=None):
"""Draw Hinton diagram for visualizing a weight matrix."""
ax = ax if ax is not None else plt.gca()
if not max_weight:
max_weight = 2**np.ceil(np.log(np.abs(matrix).max())/np.log(2))
ax.patch.set_facecolor('lightgray')
ax.set_aspect('equal', 'box')
ax.xaxis.set_major_locator(plt.NullLocator())
ax.yaxis.set_major_locator(plt.NullLocator())
for (x, y), w in np.ndenumerate(matrix):
color = 'red' if w > 0 else 'blue'
size = np.sqrt(np.abs(w))
if size > 0.98: size = 0.98
rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
facecolor=color, edgecolor=color)
ax.add_patch(rect)
nticks = matrix.shape[0]
ax.xaxis.tick_top()
ax.set_xticks(range(nticks))
ax.set_xticklabels(list(matrix.columns), rotation=90)
ax.set_yticks(range(nticks))
ax.set_yticklabels(matrix.columns)
ax.grid(False)
ax.autoscale_view()
ax.invert_yaxis()
# Plot callback
def plot_action_cb():
cols = []
i = 0;
for name in wdname:
if datasel[i].get():
cols.append(wdcol[i])
i += 1
if len(cols) != 0:
plotdata = wd[cols]
else:
plotdata = wd
ax = plotdata.plot()
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5));
plt.show()
# Individual plot callback
def indplot_action_cb():
cols = []
i = 0;
ccnt = 0
for name in wdname:
if datasel[i].get():
cols.append(wdcol[i])
ccnt += 1
i += 1
if len(cols) != 0:
plotdata = wd[cols]
else:
plotdata = wd
ccnt = wd.shape[1]
cols = wdcol
layo = ccnt * 100 + 10
j = 0
plt.figure()
while j < ccnt:
plt.subplot(layo+j+1)
plt.plot(wdtime, plotdata[cols[j]])
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5));
j = j + 1
plt.show()
# Scatterplot callback
def scatter_action_cb():
cols = []
i = 0;
for name in wdname:
if datasel[i].get():
cols.append(wdcol[i])
i += 1
if len(cols) != 0:
plotdata = wd[cols]
else:
plotdata = wd
pd.tools.plotting.scatter_matrix(plotdata, diagonal="kde")
plt.tight_layout()
plt.show()
# Correlationplot callback
def corr_action_cb():
cols = []
i = 0;
for name in wdname:
if datasel[i].get():
cols.append(wdcol[i])
i += 1
if len(cols) != 0:
plotdata = wd[cols]
else:
plotdata = wd
corrmat = plotdata.corr()
#print corrmat
plt.figure()
hinton(corrmat)
plt.show()
def corr2_action_cb():
cols = []
i = 0;
for name in wdname:
if datasel[i].get():
cols.append(wdcol[i])
i += 1
if len(cols) != 0:
plotdata = wd[cols]
else:
plotdata = wd
corrmat = plotdata.corr()
plt.figure()
sns.heatmap(corrmat, vmin=-1., vmax=1., square=False, cmap='RdBu_r').xaxis.tick_top()
plt.show()
# Regressionplot callback
def regr_action_cb():
cols = []
i = 0;
for name in wdname:
if datasel[i].get():
cols.append(wdcol[i])
i += 1
if len(cols) != 2:
tkMessageBox.showerror("Error", "Select two attributes")
return
plotdata = wd[cols]
plt.figure()
sns.regplot(x=cols[0],y=cols[1],data=plotdata)
plt.show()
# Clip reset callback
def clipreset_action_cb():
global wd
global wdtime
global wdcol
global wdname
wd = origdata.loc[:,origcol[1]:]
wdtime = origdata.loc[:,origcol[0]]
wdcol = []
wdname = []
i = 0
while i < wd.shape[1]:
colname = "A" + str(i+1)
wdcol.append(colname)
wdname.append(origcol[i+1])
i += 1
wd.columns = wdcol
dataframe.pack_forget()
dataframe.destroy()
actionframe.pack_forget()
actionframe.destroy()
create_dataframe()
create_actionframe()
# Clip callback
def clip_action_cb():
global minentry
global maxentry
global clipdia
clipdia = Toplevel(window, bg=bgcolor)
clipdia.title('Clip')
defaultmin = StringVar()
minlabel = Label(clipdia, text='Min', bg=bgcolor)
minlabel.grid(column=0, row=0, padx=20, pady=5, sticky=W)
minentry = Entry(clipdia, textvariable=defaultmin, bg=bgcolor)
defaultmin.set(str(0))
minentry.grid(column=1, row=0, padx=20, pady=5, sticky=W)
# Max entry
defaultmax = StringVar()
maxlabel = Label(clipdia, text='Max', bg=bgcolor)
maxlabel.grid(column=0, row=1, padx=20, pady=5, sticky=W)
maxentry = Entry(clipdia, textvariable=defaultmax, bg=bgcolor)
defaultmax.set(str(wd.shape[0]))
maxentry.grid(column=1, row=1, padx=20, pady=5, sticky=W)
readdatabutton = Button(clipdia, text="Apply", command=clip_action_ok_cb, bg=buttoncolor);
readdatabutton.grid(column=0, row=2, padx=60, pady=20, sticky=W)
readdatabutton = Button(clipdia, text="Cancel", command=clip_action_cancel_cb, bg=buttoncolor);
readdatabutton.grid(column=1, row=2, padx=60, pady=20, sticky=W)
def clip_action_cancel_cb():
clipdia.destroy()
def clip_action_ok_cb():
global wd
global wdtime
global wdname
global wdcol
minvalue = int(minentry.get())
maxvalue = int(maxentry.get())
clipdia.destroy()
i = 0
new_wdcol = []
new_wdname = []
cols = []
for name in wdname:
if datasel[i].get():
new_wdcol.append(wdcol[i])
new_wdname.append(wdname[i])
i += 1
if len(new_wdcol) != 0:
wd = wd[new_wdcol]
wdcol = new_wdcol
wdname = new_wdname
wd = wd[minvalue:maxvalue]
wdtime = wdtime[minvalue:maxvalue]
dataframe.pack_forget()
dataframe.destroy()
actionframe.pack_forget()
actionframe.destroy()
create_dataframe()
create_actionframe()
def close_action_cb(arg=0):
print 'Close action'
window.quit()
# Save original dataframe
def save_action_cb():
file = tkFileDialog.asksaveasfilename(initialdir='./', title='Select file',
filetypes=[('dat files','*.dat'),('all files','*.*')])
if file != '':
origdata.to_csv(file, index=False)
# Save work dataframe
def savewd_action_cb():
file = tkFileDialog.asksaveasfilename(initialdir='./', title='Select file',
filetypes=[('dat files','*.dat'),('all files','*.*')])
if file != '':
save = wd
save.columns = wdname
save.insert(0, 'Time', wdtime)
save.to_csv(file, index=False)
# Open from file
def open_action_cb(arg=0):
global origdata
global wd
global wdtime
global wdname
global origcol
global wdcol
file = tkFileDialog.askopenfilename(initialdir='./', title='Open file',
filetypes=[('dat files','*.dat'),('all files','*.*')])
if file != '':
origdata = pd.read_csv(file, parse_dates=[0])
origcol = origdata.columns
wd = origdata.loc[:,origcol[1]:]
wdtime = origdata.loc[:,origcol[0]]
wdcol = []
wdname = []
i = 0
while i < wd.shape[1]:
colname = "A" + str(i+1)
wdcol.append(colname)
wdname.append(origcol[i+1])
i += 1
wd.columns = wdcol
if 'dataframe' in globals():
dataframe.pack_forget()
dataframe.destroy()
actionframe.pack_forget()
actionframe.destroy()
create_dataframe()
create_actionframe()
def on_configure(event):
size = iteminnerframe.winfo_reqwidth(), iteminnerframe.winfo_reqheight()
canvas.config(scrollregion="0 0 %s %s" % size)
# Create frame to show and select sev items
def fetchitems_cb():
global itemframe
global sel
global items
global canvas
global iteminnerframe
global server
global fromentry
global toentry
global intervalentry
global maxentry
filtervalue = filterentry.get()
server = serverentry.get()
items = pwrrt.getSevItemList(server, filtervalue)
itemframe = Frame(window, bg=bgcolor)
# Scrollbars
scrollbar = Scrollbar(itemframe, orient=VERTICAL)
canvas = Canvas(itemframe, bd= 0, bg=bgcolor, yscrollcommand=scrollbar.set)
scrollbar.config(command=canvas.yview)
scrollbar.pack(side=RIGHT, fill=Y)
canvas.pack(expand=1, side=LEFT, fill=BOTH)
canvas.xview("moveto", 0)
canvas.yview("moveto", 0)
iteminnerframe = Frame(canvas, bg=bgcolor)
iteminnerframe.bind('<Configure>', on_configure)
canvas.create_window(0, 0, window=iteminnerframe, anchor=NW)
row = 0
valframe = Frame(iteminnerframe, bg=bgcolor)
# From entry
defaultfrom = StringVar()
fromlabel = Label(valframe, text='From')
fromlabel.grid(column=0, row=row, padx=0, pady=5, sticky=W)
fromlabel.config(bg=bgcolor)
fromentry = Entry(valframe, textvariable=defaultfrom)
defaultfrom.set('00:05:00')
fromentry.grid(column=1, row=row, padx=20, pady=5, sticky=W)
fromentry.config(bg=bgcolor)
row += 1
# From entry
defaultto = StringVar()
tolabel = Label(valframe, text='To')
tolabel.grid(column=0, row=row, padx=0, pady=5, sticky=W)
tolabel.config(bg=bgcolor)
toentry = Entry(valframe, textvariable=defaultto)
defaultto.set('now')
toentry.grid(column=1, row=row, padx=20, pady=5, sticky=W)
toentry.config(bg=bgcolor)
row += 1
# Interval entry
defaultinterval = StringVar()
intervallabel = Label(valframe, text='Interval')
intervallabel.grid(column=0, row=row, padx=0, pady=5, sticky=W)
intervallabel.config(bg=bgcolor)
intervalentry = Entry(valframe, textvariable=defaultinterval)
defaultinterval.set('1.0')
intervalentry.grid(column=1, row=row, padx=20, pady=5, sticky=W)
intervalentry.config(bg=bgcolor)
row += 1
# Max rows
defaultmax = StringVar()
maxlabel = Label(valframe, text='Max')
maxlabel.grid(column=0, row=row, padx=0, pady=5, sticky=W)
maxlabel.config(bg=bgcolor)
maxentry = Entry(valframe, textvariable=defaultmax)
defaultmax.set('500')
maxentry.grid(column=1, row=row, padx=20, pady=5, sticky=W)
maxentry.config(bg=bgcolor)
row += 1
readdatabutton = Button(valframe, text="Read DataSet", command=readdata_cb, bg=buttoncolor);
readdatabutton.grid(column=0, row=row, padx=0, pady=20, sticky=W)
row += 1
valframe.grid(column=0, row=0, padx=20, pady=5, sticky=W)
i = 0
row = 1
sel = [None] * len(items)
for item in items:
sel[i] = IntVar()
text = item[0] + "." + item[6]
item_checkbox = Checkbutton(iteminnerframe, text=text, variable=sel[i], highlightthickness=0,
bg=bgcolor)
item_checkbox.grid(column=0, row=i+row, padx=20, pady=5, sticky=W)
i = i + 1
filterframe.pack_forget()
filterframe.destroy()
itemframe.pack(expand=1, fill=BOTH)
# Create frame with action buttons
def create_actionframe():
global actionframe
actionframe = Frame(window, bg=bgcolor)
# Create plot button
plot_button = Button(actionframe, text="Plot", command=plot_action_cb, bg=buttoncolor)
plot_button.grid(column=0, row=1, padx=10, pady=10, sticky='ew')
# Create plot button
plots_button = Button(actionframe, text="Plot2", command=indplot_action_cb, bg=buttoncolor)
plots_button.grid(column=1, row=1, padx=0, pady=10, sticky='ew')
# Create scatterplot button
scatter_button = Button(actionframe, text="ScatterPlot", command=scatter_action_cb, bg=buttoncolor)
scatter_button.grid(column=0, row=2, padx=10, pady=10, sticky='ew')
# Create correlation plot button
corr_button = Button(actionframe, text="CorrelationPlot", command=corr_action_cb, bg=buttoncolor)
corr_button.grid(column=0, row=3, padx=10, pady=10, sticky='ew')
# Create correlation plot button
corr2_button = Button(actionframe, text="CorrelationPlot2", command=corr2_action_cb, bg=buttoncolor)
corr2_button.grid(column=1, row=3, padx=10, pady=10, sticky='ew')
# Create linear regression plot button
regr_button = Button(actionframe, text="LinearRegressionPlot", command=regr_action_cb, bg=buttoncolor)
regr_button.grid(column=0, row=4, padx=10, pady=10, sticky='ew')
# Create menu
menubar = Menu(window, bg=buttoncolor)
filemenu = Menu(menubar, bg=buttoncolor)
filemenu.add_command(label='Open', command=open_action_cb, accelerator='Ctrl+O')
filemenu.add_command(label='Import from server', command=create_filterframe)
filemenu.add_command(label='Save', command=save_action_cb)
filemenu.add_command(label='Save Work Data', command=savewd_action_cb)
filemenu.add_command(label='Clip', command=clip_action_cb)
filemenu.add_command(label='Clip Reset', command=clipreset_action_cb)
filemenu.add_command(label='Close', command=close_action_cb, accelerator='Ctrl+W')
menubar.add_cascade(label='File', menu=filemenu)
window.config(menu=menubar)
actionframe.pack(side=LEFT)
# Read dataframe from sev database
def readdata_cb():
# Get selected items
global origcol
global origdata
global wd
global wdname
global wdcol
global wdtime
global sel
wdname = []
dataattr = []
dataoid = []
origcol = ['Time']
wdcol = []
i = 0
j = 0
for selected in sel:
if selected.get():
colname = items[i][0] + '.' + items[i][6]
wdname.append(colname)
dataattr.append(items[i][6])
dataoid.append(items[i][1])
origcol.append(colname)
colname = "A" + str(j+1)
wdcol.append(colname)
j += 1
i += 1
if j == 0:
tkMessageBox.showerror("Error", "No item is selected")
return
fromvalue = fromentry.get()
tovalue = toentry.get()
intervalvalue = float(intervalentry.get())
maxvalue = int(maxentry.get())
result = pwrrt.getSevItemsDataFrame( server, dataoid, dataattr,
fromvalue, tovalue, intervalvalue, maxvalue)
origdata = pd.DataFrame(data=result)
origdata.columns = origcol
wd = origdata.loc[:,origcol[1]:]
wd.columns = wdcol
wdtime = origdata.loc[:,origcol[0]]
itemframe.pack_forget()
itemframe.destroy()
create_dataframe()
create_actionframe()
def create_dataframe():
global datasel
global dataframe
global startframe
if 'startframe' in globals():
startframe.pack_forget()
startframe.destroy()
dataframe = Frame(window, bg=bgcolor)
# Create dataset label
dataset_label = Label(dataframe, text="Dataset " + str(wd.shape[1]) + " X " +
str(wd.shape[0]));
dataset_label.grid(column=0, row=0, padx=50, pady=10, sticky='ew')
dataset_label.config(bg=bgcolor)
i = 0
datasel = [None] * len(wdcol)
for name in wdcol:
datasel[i] = IntVar()
text = wdcol[i] + ' ' + wdname[i]
item_checkbox = Checkbutton(dataframe, text=text, variable=datasel[i], highlightthickness=0,
bg=bgcolor)
item_checkbox.grid(column=0, row=i+1, padx=20, pady=5, sticky=W)
i = i + 1
dataframe.pack(side=LEFT, fill=X)
# Create filter frame
def create_filterframe():
global filterframe
global filterentry
global serverentry
global startframe
if 'itemframe' in globals():
itemframe.pack_forget()
itemframe.destroy()
if 'dataframe' in globals():
dataframe.pack_forget()
dataframe.destroy()
actionframe.pack_forget()
actionframe.destroy()
if 'startframe' in globals():
startframe.pack_forget()
startframe.destroy()
filterframe = Frame(window, bg=bgcolor)
defaultserver = StringVar()
# Server entry
serverlabel = Label(filterframe, text='Sev server')
serverlabel.grid(column=0, row=0, padx=20, pady=5, sticky=W)
serverlabel.config(bg=bgcolor)
serverentry = Entry(filterframe, textvariable=defaultserver)
defaultserver.set('pwr56-build')
serverentry.grid(column=1, row=0, padx=20, pady=5, sticky=W)
serverentry.config(bg=bgcolor)
# Filter entry
filterlabel = Label(filterframe, text='Item Filter')
filterlabel.grid(column=0, row=1, padx=20, pady=5, sticky=W)
filterlabel.config(bg=bgcolor)
filterentry = Entry(filterframe)
filterentry.grid(column=1, row=1, padx=20, pady=5, sticky=W)
filterentry.config(bg=bgcolor)
# Fetch items button
filterbutton = Button(filterframe, text='Fetch Items', command=fetchitems_cb, bg=buttoncolor)
filterbutton.grid(column=2, row=1, padx=20, pady=5, sticky=W)
filterframe.pack(side=LEFT, fill=X)
# Create window
bgcolor = 'white'
buttoncolor = '#F0F0F0'
window = Tk()
window.title("ProviewR Multivariate Analysis")
window.geometry('800x500')
# Create menu
menubar = Menu(window, bg=buttoncolor)
filemenu = Menu(menubar, bg=buttoncolor)
filemenu.add_command(label='Open', command=open_action_cb, accelerator='Ctrl+O')
filemenu.add_command(label='Import from server', command=create_filterframe)
filemenu.add_command(label='Close', command=close_action_cb, accelerator='Ctrl+W')
menubar.add_cascade(label='File', menu=filemenu)
window.config(menu=menubar,bg=bgcolor)
window.bind_all("<Control-w>", close_action_cb)
window.bind_all("<Control-o>", open_action_cb)
startframe = Frame(window, bg=bgcolor)
canvas = Canvas(startframe, width=800, height=200, bg=bgcolor)
canvas.pack()
img = PhotoImage(file="/data0/x5-6-1/rls/os_linux/hw_x86_64/exp/exe/pwr_logga.png")
canvas.create_image(400, 100, image=img)
startframe.pack(side=LEFT, fill=X)
window.mainloop()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment