-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
executable file
·107 lines (84 loc) · 2.61 KB
/
graph.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
import matplotlib.pyplot as plt
import glob
import os
import argparse
# File select index out of range exception
class ValueOutOfRange(Exception):
def __str__(self):
return 'Value out of range'
data_path = None
parser = argparse.ArgumentParser()
parser.add_argument('--data', '-d', nargs='?', action='store', dest='data_path', metavar='data file')
parser.add_argument('--version', '-v', action='version', version='v1.0')
args = parser.parse_args()
try:
if args.data_path is None:
data_path = os.path.dirname(os.path.abspath(__file__)) + '/../data/*.TXT'
else:
sel = 0
data_path = args.data_path
# Find for data in data/ file
data_files = glob.glob(data_path)
# List found files
if args.data_path is None:
for i, fn in enumerate(data_files):
print('[', i + 1, '] ', os.path.basename(fn))
# Select file
sel = None
while sel not in range(len(data_files)) or type(sel) != int:
try:
sel = int(input('Select file > ')) - 1
# Exit if entered value is 0
if sel == -1:
exit()
if sel not in range(len(data_files)):
raise ValueOutOfRange()
except ValueOutOfRange as e:
print(e)
except ValueError as e:
print(e)
# Open data file
try:
f = open(data_files[sel], 'r')
except Exception as e:
print(e)
# HYPERPARAMETERS
DATA_LINE = 3
ZERO_LINE = 0
# Parse data
try:
for i, line in enumerate(f):
if i == DATA_LINE:
data = line.split(':')
if i == ZERO_LINE:
zero_line = line.split(':')
f.close() # Close data file
except Exception as e:
print(e)
f.close() # Close data file
# Parse tare value
for item in zero_line:
try:
tare = int(item)
except:
pass
print("Tare, zero value: ", tare)
# Convert to integers and append to int_data list
int_data = data
for i, item in enumerate(data):
try:
int_data[i] = int(data[i])
except:
del int_data[i]
del [int_data[len(int_data) - 1]]
# Apply tare
for i, d in enumerate(int_data):
int_data[i] = abs(d) - abs(tare) # With tare
int_data[i] /= 20000 # *20000 to convert to Kh¡g and *9.8 for Newtopns
plt.plot(range(len(int_data)), int_data, 'y')
plt.grid()
plt.xlabel("Lecture")
plt.ylabel("Kgf")
plt.show()
except KeyboardInterrupt as e:
print("\nBye!")