/
draw.py
104 lines (95 loc) · 3.81 KB
/
draw.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
# -*- coding: utf-8 -*-
'''
Created on 09.10.2012
@author: Karbovnichiy-VY
'''
import numpy
import os
import matplotlib.pyplot as plt
import matplotlib
import database
class Draw:
def __init__(self, platform, outputlevel = 1, showplot = False, replot = False, save_flag = False):
if platform == u"windows":
matplotlib.rc('font',**{'family':'arial'})
if platform == u"linux":
matplotlib.rc('font',**{'family':'Cantarell'})
matplotlib.pyplot.ticklabel_format(style='sci',scilimits=(-4,4),axis='both')
self.showplot = showplot
self.outputlevel = outputlevel
self.replot = replot
self.save_flag = save_flag
def __labelchoiser(self, data, mtype):
xdict = {u"Time" : u"Время (с)", u"Spectrogram" : u"Частота (Гц)"}
ydict = {u"Volts" : u"Напряжение (В)", u"Amps" : u"Ток (А)"}
if data.device == u"Oscilloscope":
if mtype == u"Time":
x = data.xvalue
y = data.yvalue
elif mtype == u"Spectrogram":
x = data.fftxvalue
y = data.fftyvalue
xpiclabel = xdict[mtype]
ypiclabel = ydict[data.units]
return (x, y, xpiclabel, ypiclabel)
def plot(self, data, conn):
for mtype in data.type:
path = unicode(data.path[:-4] + " " + mtype + ".png")
if os.path.exists(path) == False or self.replot == True:
(x, y, xpiclabel, ypiclabel) = self.__labelchoiser(data, mtype)
end = self.__crop(y)
# fig = plt.figure()
plt.plot(x[0:end], y[0:end], linewidth = .5)
plt.xlabel(xpiclabel)
plt.ylabel(ypiclabel)
plt.grid(True)
self.saveplot(path, conn)
if self.showplot == True:
plt.show()
plt.close("all")
if self.outputlevel >= 1:
print path
if mtype == u"Time":
data.picpath = path
if mtype == u"Spectrogram":
data.fftpicpath = path
def saveplot(self, path, conn):
if self.save_flag:
plt.savefig(path, format = "png", dpi = 200)
# g = plt.imread(path)
# database.updatetable(conn, "main", "Picture", g)
def __crop(self, y):
normy = numpy.abs(y/numpy.max(y))
normy = normy[::-1]
h = 1
for n in normy:
if n > 0.05:
end = len(y)-h
break
h += 1
return end
if __name__ == "__main__":
import sys
platformd = {"linux2" : u"linux", "win32" : u"windows"}
class DataForTest:
def __init__(self,limitvalue = 10):
self.xvalue = range(1,limitvalue,1)
self.yvalue = range(1,limitvalue,1)
self.fftxvalue = range(1,limitvalue,1)
self.fftyvalue = range(1,limitvalue,1)
self.params = {}
self.params['wiretype'] = u"multicord"
self.params['wire'] = u"154.6290.600-01"
self.params['antenna'] = u"Воздушная"
self.params['range'] = u"Центр"
self.params['distance'] = u"1см"
self.params['amplitude'] = u"20кВ"
self.params['measure'] = u"Напряжение"
self.type = ["Time", "Spectrogram"]
self.device = u"Oscilloscope"
self.units = u"Volts"
self.path = "D:\\1.csv"
data1 = DataForTest()
draw1 = Draw(platform = platformd[sys.platform], outputlevel = 2, showplot = True, save_flag = True, replot = True)
conn = database.opendb()
draw1.plot(data1, conn)