-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObj-Multiple-curves.py
147 lines (116 loc) · 4.16 KB
/
Obj-Multiple-curves.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
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import os
import fnmatch
from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog
class ProcessData:
def __init__(self, splitter):
self.splitter = splitter
self.pathsToFiles = []
def parseData(self, dataStart=int(), labelsRow=int(), path=str()):
self.xArray = []
self.yArray = []
self.labelsArray = []
self.pathsToFiles.append(path)
dataFile = open(path, "r")
lineNumber = int(0)
for eachLine in dataFile:
lineNumber += 1
lineArray = eachLine.split(self.splitter)
if (lineNumber == labelsRow):
self.labelsArray.append(lineArray[0])
self.labelsArray.append(lineArray[1])
elif (lineNumber >= dataStart):
self.xArray.append(float(lineArray[0]))
self.yArray.append(float(lineArray[1]))
dataFile.close()
def labels(self, xLabelText=str(), yLabelText=str()):
XLabel = str(xLabelText + " [" + self.labelsArray[0] + "]")
YLabel = str(yLabelText + " [" + self.labelsArray[1] + "]")
labelArray = [XLabel, YLabel]
return (labelArray)
def returnData(self):
dataArray = [self.xArray, self.yArray]
return (dataArray)
def returnPathsToFiles(self):
return (self.pathsToFiles)
class graphComposer:
def __init__(self):
self.generalArray = []
def addCurve(self, dataArray):
self.generalArray.append(dataArray)
def addText(self, title, labelsArray):
self.labelsArray = labelsArray
self.title = title
def composeGraph(self, pathsArray):
self.pathsArray = pathsArray
plt.xlabel(self.labelsArray[0])
plt.ylabel(self.labelsArray[1])
plt.title(self.title)
i = int(0)
j = int(0)
while (i < len(self.generalArray)):
dataArray = self.generalArray[i]
xArray = dataArray[j]
yArray = dataArray[j + 1]
nameArray = pathsArray[i].split("/")
name = nameArray[len(nameArray) - 1]
plt.plot(xArray, yArray, label=("Source: " + name))
i += 1
def showGraph(self):
plt.legend()
plt.show()
def window():
root = Tk()
inputLabel = Label(root, text="Path to input directory", height=4)
inputPath = Entry(root)
titleLabel = Label(root, text="Title for graph", height=4)
graphTitle = Entry(root)
XLabelLabel = Label(root, text="X axis label", height=4)
XLabel = Entry(root)
YLabelLabel = Label(root, text="Y axis label", height=4)
YLabel = Entry(root)
inputLabel.grid(row=0, column=0)
titleLabel.grid(row=1, column=0)
XLabelLabel.grid(row=2, column=0)
YLabelLabel.grid(row=3, column=0)
inputPath.grid(row=0, column=1)
graphTitle.grid(row=1, column=1)
XLabel.grid(row=2, column=1)
YLabel.grid(row=3, column=1)
def filePick():
root.dirPath = tkFileDialog.askdirectory(initialdir="/", title="Select directory")
inputPath.insert(10, root.dirPath)
entryButton = Button(root, text="...", command=filePick)
entryButton.grid(row=0, column=2)
def getValues():
inpPath = inputPath.get()
title = graphTitle.get()
XLbl = XLabel.get()
YLbl = YLabel.get()
root.destroy()
main(inpPath, title, XLbl, YLbl)
OKButton = Button(root, text="Generate graph", command=getValues)
OKButton.grid(row=4, column=1)
root.mainloop()
def main(inputPath, Title, XLabel, YLabel):
paths = []
for file in os.listdir(inputPath):
if fnmatch.fnmatch(file, '*.txt'):
paths.append(inputPath + "/" + file)
data = ProcessData("\t")
g = graphComposer()
firstOpened = bool(True)
for eachElement in paths:
data.parseData(8, 6, eachElement)
dataArray = data.returnData()
g.addCurve(dataArray)
if (firstOpened == True):
labelsArray = data.labels(XLabel, YLabel)
g.addText(Title, labelsArray)
firstOpened = False
pathsArray = data.returnPathsToFiles()
g.composeGraph(pathsArray)
g.showGraph()
window()