-
Notifications
You must be signed in to change notification settings - Fork 0
/
filterGraph1.py
175 lines (123 loc) · 4.49 KB
/
filterGraph1.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
# A script for my fork of
# glucometerUtils (https://github.com/pat-nel87/glucometerutils/tree/customFeatures)
# Designed to work with output generated by --text argument added to dump
# Cleans up Data in txt file and generates matplotlib graph
# tested with freestyle precisionNeo driver only
#
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.dates import DayLocator, HourLocator, DateFormatter, drange
import numpy as np
from datetime import date, time, datetime
#globals
allReadings = []
dates = []
times = []
bloodSugar = []
def dateConvert(dates, times):
myList = []
for i in range(len(dates)):
myDate = date.fromisoformat(str(dates[i]))
myTime = time.fromisoformat(str(times[i]))
myDatetime = datetime.combine(myDate, myTime)
myList.append(myDatetime)
return myList
def graphList(myList, bloodSugar):
print("\n")
graphFile = input("Please enter a file name for graph: ")
graphFile = graphFile + ".png"
x = matplotlib.dates.date2num(myList)
y = bloodSugar
fig = matplotlib.pyplot.figure()
matplotlib.pyplot.plot_date(x, y, 'r', label="mg/dl")
fig.savefig(graphFile)
plt.show()
def graphQuery(filterList):
print("\n")
graphFile = input("Please enter a file name for graph: ")
graphFile = graphFile + ".png"
myDates = []
mySugars = []
for i in range(len(filterList)):
myDates.append(filterList[i][0])
mySugars.append(float(filterList[i][1]))
x = matplotlib.dates.date2num(myDates)
y = mySugars
fig = matplotlib.pyplot.figure()
matplotlib.pyplot.plot_date(x, y, 'o-', label="mg/dl")
fig.autofmt_xdate()
fig.savefig(graphFile)
plt.show()
def fileClean():
file = input("Enter Filename: ")
edit = open(file, "r")
edit.seek(0,0)
for line in edit:
lin = edit.readline()
try:
reading = [lin[1]]
for i in range(2,11):
reading[0] = reading[0] + lin[i]
dates.append(reading[0])
reading.append(lin[12])
for i in range(13,20):
reading[1] = reading[1] + lin[i]
times.append(reading[1])
reading.append(lin[23])
for i in range(24,28):
reading[2] = reading[2] + lin[i]
bloodSugar.append(reading[2])
allReadings.append(reading)
except IndexError:
print("Processing Completed")
break
def filterList(myList):
# creates new list of lists with 2 indices
# myList[n][0] datetime object
# myList[n][1] blood glucose reading
tempList = []
for i in range(len(myList)):
tempDate = date.fromisoformat(str(myList[i][0]))
tempTime = time.fromisoformat(str(myList[i][1]))
tempDateTime = datetime.combine(tempDate, tempTime)
tempList.append([tempDateTime, myList[i][2]])
#print(tempList[i])
return tempList
def preciseFilter(filterList):
# will filter by datetime
queryList = []
print("Please Select a Filtering Parameter", "\n",
"1: Year", "\n", "2: Month", "\n", "3: Day", "\n")
choice = input(":---> ")
choice = int(choice)
if choice == 1:
myYear = input("Enter a year: ")
for i in range(len(filterList)):
if filterList[i][0].year == int(myYear):
# print(filterList[i])
queryList.append(filterList[i])
elif choice == 2:
myMonth = input("Enter a month: ")
myYear = input("Enter a year: ")
for i in range(len(filterList)):
if filterList[i][0].year == int(myYear):
if filterList[i][0].month == int(myMonth):
# print(filterList[i])
queryList.append(filterList[i])
elif choice == 3:
myYear = input("Enter Year: ")
myMonth = input("Enter Month: ")
myDay = input("Enter Day: ")
for i in range(len(filterList)):
if filterList[i][0].year == int(myYear):
if filterList[i][0].month == int(myMonth):
if filterList[i][0].day == int(myDay):
# print(filterList[i])
queryList.append(filterList[i])
return queryList
fileClean()
filtered = filterList(allReadings)
queryList = preciseFilter(filtered)
#print(queryList[0][0])
#print(queryList[0][1])
graphQuery(queryList)