-
Notifications
You must be signed in to change notification settings - Fork 0
/
coreFunctions.py
1235 lines (1004 loc) · 47.4 KB
/
coreFunctions.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import statistics as statistics
import re as re
import datetime as datetime
import os as os
#Plotting libraries
import bokeh
import bokeh.plotting
import bokeh.models
from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.models import HoverTool, PointDrawTool, Span
from bokeh.layouts import widgetbox, column
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn
from bokeh.io import curdoc, reset_output
from screeninfo import get_monitors
import numpy as np
#GUI libraries
import tkinter as tk
from tkinter import ttk, filedialog, font, messagebox, Scale
#Local libraries
import coreClasses as coreC
#Return mean of temperatures in radius around given index
def adjacentMean(gui, index):
radius = int(gui.adjMeanRadE.get())
tempers = []
#Append temperatures surrounding provided index, pass if index out of range
for i in range(1, radius + 1):
try:
tempers.append(float(gui.masterList[index - i][gui.tempsCol]))
except:
pass
try:
tempers.append(float(gui.masterList[index + i][gui.tempsCol]))
except:
pass
#Append temperature at index itself
tempers.append(float(gui.masterList[index][gui.tempsCol]))
#Get mean of temperatures discoverd within radius
adjMean = statistics.mean(tempers)
return adjMean
def getTime(gui, index):
time = re.search("(\d+:\d+)", gui.masterList[index][gui.dateTimesCol])
return time.group(1)
def getDate(gui, index):
date = re.search("\d+\/\d+\/\d+", gui.masterList[index][gui.dateTimesCol])
return date.group(0)
#Check for significant upward or downward trend
def sigChange(gui, boutType, curIndex, stop):
indexThreshOn = int(round(float(gui.timeThreshAdvOnE.get())))
indexThreshOff = int(round(float(gui.timeThreshAdvOffE.get())))
#Looking for on bout
if boutType == "off":
#Check if at end of masterList
if (curIndex + indexThreshOn) < stop:
#Check if increasing
if adjacentMean(gui, (curIndex + indexThreshOn)) > adjacentMean(gui, curIndex):
#Check if increasing consistently over range
threshold = indexThreshOn
if checkInc(gui, curIndex, threshold):
return True
#Looking for off bout
else:
#Check if at end of masterList
if (curIndex + indexThreshOff) < stop:
#Check if decreasing
if adjacentMean(gui, (curIndex + indexThreshOff)) < adjacentMean(gui, curIndex):
#Check if decreasing consistently over range
threshold = indexThreshOff
if checkDec(gui, curIndex, threshold):
return True
#Return False if no significant trend detected
return False
#Check if temperature is decreasing over the defined time range
def checkDec(gui, index, threshold):
# leniency = int(gui.leniencyOffE.get())
leniency = 0
offense = 0
for i in range(0, threshold):
#Check if end of gui.masterList
if (index + i) < (len(gui.masterList) - 2):
if adjacentMean(gui, (index + i)) < adjacentMean(gui, (index + i + 1)):
offense += 1
if offense > leniency:
#Return false if increase detected
return False
return True
#Check if temperature is increasing over the defined time range
def checkInc(gui, index, threshold):
# leniency = int(gui.leniencyOnE.get())
leniency = 0
offense = 0
for i in range(0, threshold):
#Check if end of gui.masterList
if (index + i) < (len(gui.masterList) - 2):
if adjacentMean(gui, (index + i)) > adjacentMean(gui, (index + i + 1)):
offense += 1
if offense > leniency:
#Set inc to False if increase detected
return False
return True
#Find highest or lowest temperature in a given range of indices
def findVertexI(gui, vertType, leftLim, rightLim):
vertIndex = None
if rightLim >= len(gui.masterList):
rightLim = (len(gui.masterList) - 1)
if vertType == "offStart":
#Looking for maximum
tempMax = gui.masterList[leftLim][gui.tempsCol]
#Find highest temperature between left-side limit and right-side limit
for subIndex in range(leftLim, (rightLim + 1)):
if float(gui.masterList[subIndex][gui.tempsCol]) >= float(tempMax):
tempMax = gui.masterList[subIndex][gui.tempsCol]
vertIndex = subIndex
elif vertType == "onStart":
#Looking for minimum
tempMin = gui.masterList[leftLim][gui.tempsCol]
#Find lowest temperature between last vertex and right-side limit
for subIndex in range(leftLim, (rightLim + 1)):
try:
if float(gui.masterList[subIndex][gui.tempsCol]) <= float(tempMin):
tempMin = gui.masterList[subIndex][gui.tempsCol]
vertIndex = subIndex
except:
pass
#Return index of found vertex
if vertIndex is None:
vertIndex = rightLim
return vertIndex
#Returns slope between given index and index + set slope range
def checkSlopeSimple(gui, index, minSlope, slopeRange):
if minSlope == 0:
return True
temper1 = adjacentMean(gui, index)
temper2 = adjacentMean(gui, (index + slopeRange))
slope = ((temper2 - temper1) / slopeRange)
#Off-bout slopes are less than 0
if minSlope < 0:
if slope <= minSlope:
return True
#On-bout slopes are greater than 0
elif minSlope > 0:
if slope >= minSlope:
return True
return False
#Check if time passed to function is within daytime range
def checkDaytime(dayStart, nightStart, time):
daytime = True
#Convert dayStart time to float
day = re.search("(\d+)(:)(\d+)", dayStart)
dayNum = float(day.group(1)) + (float(day.group(3)) / 60)
#Convert nightStart time to float
night = re.search("(\d+)(:)(\d+)", nightStart)
nightNum = float(night.group(1)) + (float(night.group(3)) / 60)
#Convert time passed to float
curTime = re.search("(\d+)(:)(\d+)", time)
timeNum = float(curTime.group(1)) + (float(curTime.group(3)) / 60)
#Check if not in daytime range
if (timeNum > nightNum) or (timeNum < dayNum):
daytime = False
#Return true if time passed is within daytime range
return daytime
#Split data into day and night objects
def splitDays(gui, daysList, nightsList, modifier = 0):
#Increment time by modifier value
def modTime(oriTime):
if modifier is 0:
newTime = (" " + oriTime.strip())
else:
#Convert csv time value to datetime format
search = re.search("((\d+):(\d+))", oriTime)
hour = int(search.group(2))
minute = int(search.group(3))
time = datetime.datetime(1, 1, 1, hour, minute, 0)
#Add modifer and strip unnecessary info
newTime = (time + datetime.timedelta(minutes = modifier))
newTime = str(newTime)[11:-3]
if newTime[0] is "0":
newTime = newTime[1:]
#Return modified time
return(" " + newTime.strip())
#Set to False when end of list is encountered
dayValid = True
dayStart = modTime(gui.dayStart)
nightStart = modTime(gui.nightStart)
dayDur = getDayDur(dayStart, nightStart)
nightDur = (1440 - dayDur)
dayInterval = (1440 / gui.interval)
# cur = current
curDay = -1
curNight = -1
#Look for first day or night
for i in range(0, len(gui.masterList)):
#If dayStart found before nightStart
if re.search(dayStart, gui.masterList[i][gui.dateTimesCol]):
#Set start of first day to this index
curDay = i
#Set start of first night to day index + duration of daytime
curNight = (i + (dayDur / gui.interval))
#Check if this sets curNight past length of gui.masterList
if curNight > (len(gui.masterList) - 1):
dayValid = False
curNight = (len(gui.masterList) - 1)
daysList.append(coreC.block(gui, curDay, curNight - 1, True))
break
#If nightStart found before dayStart
elif re.search(nightStart, gui.masterList[i][gui.dateTimesCol]):
#Set start of first night to this index
curNight = i
#Set start of first day to night index + duration of nighttime
curDay = (i + (nightDur / gui.interval))
#Check if this sets curDay past length of gui.masterList
if curDay > (len(gui.masterList) - 1):
dayValid = False
curDay = (len(gui.masterList) - 1)
break
#Check if data starts at night and process to achieve uniformity going into following while `
#Catch partial day at start of gui.masterList
if curNight < curDay:
daysList.append(coreC.block(gui, 0, curNight - 1, True))
nightsList.append(coreC.block(gui, curNight, curDay - 1, not dayValid))
curNight += dayInterval
#Catch partial night at start of gui.masterList
elif curDay < curNight:
nightsList.append(coreC.block(gui, 0, curDay - 1, True))
#If neither dayStart or nightStart found, append partial day or night
elif curDay == curNight:
dayValid = False
if checkDaytime(dayStart, nightStart, gui.masterList[0][gui.dateTimesCol]):
daysList.append(coreC.block(gui, 0, (len(gui.masterList) - 1), True))
else:
nightsList.append(coreC.block(gui, 0, (len(gui.masterList) - 1), True))
#Save each day and night as object
while dayValid:
daysList.append(coreC.block(gui, curDay, curNight - 1, False))
curDay += dayInterval
#Make final night stop at end of gui.masterList
if curDay > len(gui.masterList):
curDay = (len(gui.masterList) - 1)
nightsList.append(coreC.block(gui, curNight, curDay - 1, True))
dayValid = False
break
else:
nightsList.append(coreC.block(gui, curNight, curDay - 1, False))
curNight += dayInterval
#Make final day stop at end of gui.masterList
if curNight > len(gui.masterList):
curNight = (len(gui.masterList) - 1)
daysList.append(coreC.block(gui, curDay, curNight - 1, True))
dayValid = False
#Address problem of start time skipping
if len(daysList) is 0 or len(nightsList) is 0:
if (modifier + 1) < gui.interval:
#Clear lists
daysList.clear()
nightsList.clear()
#Recursively call splitDays with incremented modifier
splitDays(gui, daysList, nightsList, (modifier + 1))
#If no days or nights still found, provide text warning
else:
messagebox.showwarning("Warning",
("If daytime or nighttime periods are not being properly detected, make sure the Data Time Interval provided on the Main tab is correct."))
#Retrieve duration of daytime in minutes
def getDayDur(dayStart, nightStart):
#Convert dayStart time to float
day = re.search("(\d+)(:)(\d+)", dayStart)
dayNum = float(day.group(1)) + (float(day.group(3)) / 60)
#Convert nightStart time to float
night = re.search("(\d+)(:)(\d+)", nightStart)
nightNum = float(night.group(1)) + (float(night.group(3)) / 60)
#Convert to minutes
dayDur = ((nightNum - dayNum) * 60)
return dayDur
#Check if initial slope meets user defined minimum
def checkSlopeRegression(gui, index, offBout, raw = False):
if offBout:
minSlope = float(gui.minSlopeOffE.get())
slopeRange = int(gui.slopeRangeOffE.get())
else:
minSlope = float(gui.minSlopeOnE.get())
slopeRange = int(gui.slopeRangeOnE.get())
if not raw:
if minSlope == 0:
return True
xs = []
ys = []
for i in range(0, (slopeRange + 1)):
#Check if end of gui.masterList
if (index + i) < (len(gui.masterList) - 2):
xs.append(gui.masterList[index + i][0])
ys.append(gui.masterList[index + i][2])
xArray = np.array(xs, dtype = np.float64)
yArray = np.array(ys, dtype = np.float64)
if len(xArray) == 0 or len(yArray) == 0:
return False
slope = (((statistics.mean(xArray) * statistics.mean(yArray)) - statistics.mean(xArray * yArray)) / ((statistics.mean(xArray) ** 2) - statistics.mean(xArray ** 2)))
if raw:
return slope
if offBout:
if slope >= minSlope:
return True
else:
if slope <= minSlope:
return True
return False
def checkValidSlope(gui, block, i, boutType, minSlope, slopeRange):
#Set start of search to index of next vertex if it exists
if (i + 1) < len(block.vertices):
start = (block.vertices[i + 1].index - slopeRange)
#If there is not another vertex, set start to end of block
else:
start = (block.stop - slopeRange)
valid = False
for x in range(start, block.vertices[i].index, -1):
if checkSlopeSimple(gui, x, minSlope, slopeRange):
valid = True
break
return valid
def resyncVertTypes(block):
type = block.vertices[0].vertType
for vertex in block.vertices:
vertex.vertType = type
if type == "offStart":
type = "onStart"
else:
type = "offStart"
def checkAlternatingTemps(block):
changeMade = True
while changeMade:
changeMade = False
resyncVertTypes(block)
for i in range(1, len(block.vertices)):
#(flag)
# print("type =", block.vertices[i].vertType)
# print("temp i - 1 =", block.vertices[i - 1].temp)
# print("temp i =", block.vertices[i].temp)
if block.vertices[i].vertType == "offStart":
if block.vertices[i - 1].temp > block.vertices[i].temp:
block.vertices.pop(i - 1)
changeMade = True
break
else:
if block.vertices[i - 1].temp < block.vertices[i].temp:
block.vertices.pop(i - 1)
changeMade = True
break
def sequentialDPWarn(gui, masterList):
#Check if data points are sequential and continuous
prevIndex = masterList[0][gui.dataPointCol]
first = True
lineNum = 1
for line in masterList:
if not first:
try:
if not int(line[gui.dataPointCol]) == (int(prevIndex) + 1):
messagebox.showwarning("Data Point Warning",
("Warning on line " + str(lineNum) + ". Data point number is not sequential with regrad to previous index. This could result in incorrect statistical output."))
return False
#If int conversion fails, just pass: will be detected with checkInFile
except ValueError:
pass
else:
first = False
lineNum += 1
prevIndex = line[gui.dataPointCol]
#Create masterList from input csv file and format appropriately
def prepareList(gui, sourceFile):
popCount = 0
popindices = []
masterList = []
#Open, read, and split csv file into masterList
csv = open(sourceFile, 'r')
lines = csv.readlines()
csv.close()
for line in lines:
masterList.append(line.split(","))
for i in range(0, (len(masterList) - 1)):
#Record indices of lines not conforming to expected format (This line deletes headers)
if not re.search("\d", masterList[i][gui.dataPointCol]):
popindices.append(i)
#If incubation temperature is absent, set equal to previous temperature
elif not re.search("(\d)+(\.)?((\d)?)+", masterList[i][gui.tempsCol]):
try:
masterList[i][gui.tempsCol] = float(masterList[i - 1][gui.tempsCol])
except:
masterList[i][gui.tempsCol] = 0
#Delete extra line created as a result of the above for loop
del masterList[-1]
#Remove lines not conforming to format
for index in popindices:
masterList.pop(index - popCount)
popCount += 1
#Clear formatting characters sometimes present on first position
search = re.search("\d+", masterList[0][gui.dataPointCol])
masterList[0][gui.dataPointCol] = search.group(0)
sequentialDPWarn(gui, masterList)
return masterList
def detectVertType(gui, userVertIs):
pass
#Extract datapoints corresponding to vertices placed by user
def extractDPsFromHTML(gui, file):
DPList = []
#Get lines of HTML file
with open(file, "r") as htmlFile:
htmlLines = htmlFile.readlines()
#Remove superfluous lines of table data if present
found = False
with open(file, "w") as htmlFile:
for line in htmlLines:
#Search for vertex data containing line
if not line.find("<div class") == -1:
#Retain the first
if not found:
found = True
htmlFile.write(line)
#Discard all others
else:
pass
#Retain all non-table data containing lines
else:
htmlFile.write(line)
#Now proceed with file purged of curropting data lines
with open(file, "r") as htmlFileClean:
htmlCont = htmlFileClean.read()
#Gets all data points and temperatures from table
tokens = re.finditer("((\d+\.)?\d+)</span>", htmlCont)
type = "DP"
for match in tokens:
if type == "DP":
#Add data point to list if not a duplicate
dataPoint = round(float(match.group(1)))
if dataPoint not in DPList:
DPList.append(dataPoint)
type = "temp"
#Ignore temperatures
else:
type = "DP"
#Return sorted list of datapoints
return sorted(DPList)
#Extract vertices corresponding to certain window of the input file
def extractVertsInRange(gui, totalVerts, startIndex, stopIndex):
subVerts = []
leftLim = 0
rightLim = 0
#Check if any vertices are present
if len(totalVerts) < 1:
return subVerts
#Check if stop index is lower than lowest present in vertex list, return empty list
if stopIndex < totalVerts[0].index:
return subVerts
#Check if start index is greater than greatest present in vertex list, return empty list
if startIndex > totalVerts[-1].index:
return subVerts
#Find first vertex over start index and set as left limit
for i in range(len(totalVerts)):
if totalVerts[i].index >= startIndex:
leftLim = i
break
#Find first vertex under stop index and set as right limit
for i in range((len(totalVerts)- 1), -1, -1):
if totalVerts[i].index <= stopIndex:
rightLim = i
break
#Return vertices in desired rane
subVerts = totalVerts[leftLim:(rightLim + 1)]
return subVerts
#Create vertices based on provied datapoints
def extractVertsFromDPs(gui):
vertices = []
userVertDPs = extractDPsFromHTML(gui, gui.vertexFileE.get())
for i in range(len(gui.masterList)):
#Search for gap between index value and corresponding datapoint
if int(gui.masterList[i][gui.dataPointCol]) == int(userVertDPs[0]):
#Delta is discrepency between index and data point number
delta = (userVertDPs[0] - i)
break
firstVertTemp = gui.masterList[userVertDPs[0] - delta][gui.tempsCol]
secondVertTemp = gui.masterList[userVertDPs[1] - delta][gui.tempsCol]
#Determine if first vertex is an offStart or onStart
if firstVertTemp > secondVertTemp:
vertType = "offStart"
else:
vertType = "onStart"
#(flag) this could lead to some issues due to invalid assumption
for dataPoint in userVertDPs:
index = (dataPoint - delta)
vertices.append(coreC.vert(index, gui.masterList[index][gui.tempsCol], vertType))
if vertType == "offStart":
vertType = "onStart"
elif vertType == "onStart":
vertType = "offStart"
return vertices
#Extract only section of masterList corresponding to user selected vertices
def sliceMasterList(gui, userVertDPs):
start = None
stop = None
firstVertI = None
#If no user vertices provided, return whole list
if len(userVertDPs) < 1:
return gui.masterList
#Get average distance between vertices
deltas = []
for x in range(len(userVertDPs) - 2):
deltas.append(userVertDPs[x + 1] - userVertDPs[x])
averageDelta = statistics.mean(deltas)
#Buffer is number of data points before the start and after the stop to be collected
buffer = round((averageDelta / 2))
found = False
for x in range(len(gui.masterList)):
#Find index number corrisponding to vertex data point
if int(gui.masterList[x][gui.dataPointCol]) == userVertDPs[0]:
firstVertI = x
#Start at vertex index minus buffer and move right until valid index encountered
for i in range((round(firstVertI - buffer)), firstVertI):
if not i < 0:
start = i
found = True
break
#Break once valid vertex found
if found:
break
if firstVertI is None:
return False
#Determine last possible index value
lastVertI = (firstVertI + (userVertDPs[-1] - userVertDPs[0]))
found = False
#Start at last vertex plus buffer and move left until valid index encountered
for i in range ((lastVertI + buffer), lastVertI, -1):
if not i >= len(gui.masterList):
stop = i
break
#Break once valid vertex found
if found:
break
#If no valid vertex found for start or stop
if start is None:
start = 0
if stop is None:
stop = (len(gui.masterList) - 1)
#Return reduced master list
splitList = gui.masterList[start:(stop + 1)]
return splitList
#Identify bouts and save as bout object
def getBouts(gui, block):
verts = block.vertices
#Return if no vertices provided
if verts == None or len(verts) < 2:
return
for i in range(0, (len(verts) - 1)):
#Append off-bout
if verts[i].vertType == "offStart":
block.bouts.append(coreC.bout(gui, verts[i].index, verts[i + 1].index, "off"))
block.offCount += 1
#Append on-bout
elif verts[i].vertType == "onStart":
block.bouts.append(coreC.bout(gui, verts[i].index, verts[i + 1].index, "on"))
block.onCount += 1
#Create DN (day/night) pair objects
def getPairs(gui, daysList, nightsList, pairsList):
#Set modifier based on if day or night comes first
if daysList[0].start > nightsList[0].start:
modifier = 1
else:
modifier = 0
for i in range(len(daysList)):
#If there is a night corrisponding with the day at index i
if (i + modifier) < (len(nightsList)):
#Create pair if both daytime and nightime are complete
if not daysList[i].partial and not nightsList[i + modifier].partial:
pairsList.append(coreC.block(gui, daysList[i].start, nightsList[i + modifier].stop, False))
#Warns user not to drag existing vertices while in edit mode
def dragWarn(gui):
if gui.dragWarnConfig.lower() == "yes":
if not messagebox.askyesno("WARNING", ("Do NOT drag existing vertices. To \"move\" a vertex, you must select it (left click), delete it (backspace), and place a new vertex (left click) where you wish. However, newly placed vertices CAN be dragged with no issues.\n\n Continue to show this warning in the future?")):
with open(os.path.join(gui.coreDir, "config_files", "defaultConfig.ini"), "w") as configFile:
# with open("./../config.ini", "w") as configFile:
gui.config.set("Main Settings", "drag_warn", "no")
gui.config.write(configFile)
gui.dragWarnConfig = "no"
#Warns user if a valid air temperature column is not detected
def airTempWarn(gui, dataPoint, type):
response = False
if gui.airWarnConfig.lower() == "yes":
if type == "missing":
if messagebox.askyesno("Air Temperature Warning", ("No air temperature detected for data point " + dataPoint + ". Air temperatures will not be plotted or included in statistical output.\n\n Continue to show this warning in the future?")):
response = True
elif type == "invalid":
if messagebox.askyesno("Air Temperature Warning", ("Invalid air temperature detected for data point " + dataPoint + ". Air temperatures will not be plotted or included in statistical output.\n\n Continue to show this warning in the future??")):
response = True
#Rewrite config file to disable air temperature warning
if response is False:
with open(os.path.join(gui.coreDir, "config_files", "defaultConfig.ini"), "w") as configFile:
# with open("./../config.ini", "w") as configFile:
gui.config.set("Main Settings", "air_temp_warn", "no")
gui.config.write(configFile)
gui.airWarnConfig = "no"
#Generate plot
def graph(gui, daysList, nightsList, masterBlock, plotVertices = True, showDragWarn = False):
bulkvertices = []
vertX = []
vertY = []
masterX = []
masterY = []
airX = []
airY = []
monResList = []
#Clears previous graphs from memory
reset_output()
if plotVertices:
#Add daytime period delimiting lines if appropriate
if gui.restrictSearchBV.get() is True:
for day in daysList:
if len(day.vertices) > 0:
#Add vertex at 0 to mark begining of day
bulkvertices.append(coreC.vert(day.vertices[0].index, np.nan, day.vertices[0].vertType))
#Get all vertices from bouts
bulkvertices += day.vertices
#Add vertex at 0 to mark end of day
bulkvertices.append(coreC.vert(day.vertices[-1].index, np.nan, day.vertices[-1].vertType))
#Else compile vertices for entire input file
else:
bulkvertices = masterBlock.vertices
#Compile index number and temperature of all vertices
for vertex in bulkvertices:
vertX.append(gui.masterList[vertex.index][gui.dataPointCol])
vertY.append(vertex.temp)
#Output to HTML file
output_file(gui.graphName)
else:
output_file(os.path.join(gui.coreDir, "misc_files", "tempGraph.html"))
#Create column data source for vertex data so it can be manipulated in the interactive plot
boutsSourceData = ColumnDataSource(data = dict(x = vertX, y = vertY))
#Compile index number and temperature for every line in input file
for line in gui.masterList:
masterX.append(int(line[gui.dataPointCol]))
masterY.append(float(line[gui.tempsCol]))
#Compile air temperature if a valid column is present
if gui.airValid is True:
airX.append(int(line[gui.dataPointCol]))
airY.append(float(line[gui.airTempCol]))
#Set plot dimensions
if gui.plotDimVar.get() is 1:
#Get monitor resolution if "auto" selected
for monitor in get_monitors():
monResList.append(str(monitor))
#Extract monitor width and height
resRE = re.search("(\d+)x(\d+)", monResList[0])
monX = int(resRE.group(1))
monY = int(resRE.group(2))
plotX = (monX - 200)
plotY = (monY - 200)
#Get user dimension values if "manual" selected
else:
plotX = int(gui.plotDimXE.get())
plotY = int(gui.plotDimYE.get())
#Initialize hover tool (provides information about individual datapoints)
hover = HoverTool(tooltips=[("Data Point", "$x{int}"), ("Temperature", "$y")])
#(flag) may not be neccessary
# for y in masterY:
# float(y)
#Get name of plot
if plotVertices:
plotName = gui.graphName[:-5]
else:
#Get input file root name
tempRE = re.search((".*\."), os.path.basename(os.path.normpath(gui.inputFileAdvE.get())))
#Assign default plot name
if tempRE:
plotName = tempRE.group(0)[:-1]
else:
plotName = os.path.basename(os.path.normpath(gui.inputFileAdvE.get()))
#Detect best y axis range
if gui.airValid is True:
yMin = (float(min(min(masterY), min(airY))) - 2)
yMax = (float(max(max(masterY), max(airY))) + 2)
else:
yMin = (float(min(masterY)) - 2)
yMax = (float(max(masterY)) + 2)
#Create core plot
plot = figure(tools = [hover, "box_zoom, wheel_zoom, pan, reset, save"],
x_range = [int(masterX[0]), int(masterX[len(masterX) - 1])],
y_range = [yMin, yMax],
# y_range = [float(min((float(min(airY)), float(min(masterY)))) - 2), (max(max(airY), max(masterY)) + 2)],
title = plotName,
x_axis_label= "Data Point", y_axis_label = "Temperature (C)", plot_width = plotX, plot_height = plotY)
#Add vertical lines delimiting days
if gui.showDayDelimsBV.get() is True:
for day in daysList:
vline = Span(location = int(gui.masterList[day.start][gui.dataPointCol]), dimension = "height",
line_color = gui.dayMarkerColorVar.get(), line_width = float(gui.dayMarkerSize_E.get()), line_alpha = 0.6)
plot.renderers.extend([vline])
#Get size settings from GUI
plot.title.text_font_size = (gui.titleFontSizeE.get() + "pt")
plot.axis.axis_label_text_font_size = (gui.axisTitleFontSizeE.get() + "pt")
plot.axis.major_label_text_font_size = (gui.axisLabelFontSizeE.get() + "pt")
plot.axis.major_tick_line_width = int(gui.axisTickSizeE.get())
plot.axis.minor_tick_line_width = int(gui.axisTickSizeE.get())
plot.axis.major_tick_out = int(gui.axisTickSizeE.get())
plot.axis.minor_tick_out = int(gui.axisTickSizeE.get())
#Append vertex x and y values to data dictionary
data = {"x": [], "y": []}
for x in vertX: data["x"].append(x)
for y in vertY: data["y"].append(y)
#Generate table with vertex information
src = ColumnDataSource(data)
columns = [
TableColumn(field = "x", title = "Data Point"),
TableColumn(field = "y", title = "Temperature")
]
data_table = DataTable(source = src, columns = columns, width = 500, height = 20000)
if gui.editModeBV.get():
#Show drag warning if applicable
if showDragWarn:
dragWarn(gui)
#Plot air temperatures
if gui.showAirTempBV.get() is True and gui.airValid:
# plot.circle(airX, airY, size = 5, color = "black", alpha = 1, legend = "Air Temeprature")
plot.line(airX, airY, line_width = float(gui.airTempSize_E.get()), color = gui.airTempColorVar.get(), line_alpha = 1, legend = "Air Temeprature")
#Plot egg temperatures
plot.circle(masterX, masterY, size = float(gui.eggTempPointSize_E.get()), color = gui.eggTempPointColorVar.get(), legend = "Egg Temperature")
plot.line(masterX, masterY, line_width = float(gui.eggTempLineSize_E.get()), color = gui.eggTempLineColorVar.get())
#Plot verticies as large circles if edit mode selected
renderer = plot.circle (
"x",
"y",
size = (float(gui.boutSize_E.get()) * 3),
color = "black",
fill_color = gui.boutColorVar.get(),
fill_alpha = 0.8,
legend = "Prediced Incubation Shift",
source = src
)
draw_tool = PointDrawTool(renderers = [renderer], empty_value = 1)
plot.add_tools(draw_tool)
plot.toolbar.active_drag = draw_tool
#Else plot verticies as lines
else:
#Plot air temperatures
if gui.showAirTempBV.get() is True and gui.airValid:
# plot.circle(airX, airY, size = 5, color = "black", alpha = 1, legend = "Air Temeprature")
plot.line(airX, airY, line_width = float(gui.airTempSize_E.get()), color = gui.airTempColorVar.get(), line_alpha = 1, legend = "Air Temeprature")
#Plot egg temperatures
plot.circle(masterX, masterY, size = float(gui.eggTempPointSize_E.get()), color = gui.eggTempPointColorVar.get(), legend = "Egg Temperature")
plot.line(masterX, masterY, line_width = float(gui.eggTempLineSize_E.get()), color = gui.eggTempLineColorVar.get())
#Add vertices
plot.line("x", "y", line_width = float(gui.boutSize_E.get()), legend = "Detected Bout", line_color = gui.boutColorVar.get(), source = boutsSourceData)
if gui.showGridBV.get() is False:
plot.grid.visible = False
plot.legend.label_text_font_size = (gui.legendFontSizeE.get() + "pt")
plot.background_fill_color = None
plot.border_fill_color = None
# plot.outline_color = None
#Generate plot
show(column(plot, widgetbox(data_table)))
#Create file containing information about individual bouts as well as by whole-day and comprehensive file summaries at the bottom
def outStats(gui, days, nights, pairsBlockGroup, masterBlock):
allTemps = []
allAirTemps = []
masterSuperCust = 0
masterSubCust = 0
if gui.makeOutputBool or gui.compileStatsBool:
#Acquire list of ALL temperatures for reporting statistics accross entire data set
for i in range(len(gui.masterList)):
allTemps.append(float(gui.masterList[i][gui.tempsCol]))
if gui.airValid is True:
allAirTemps.append(float(gui.masterList[i][gui.airTempCol]))
else:
allAirTemps.append(0)
#Get time exceeding critical temperatures
for temp in allTemps:
if temp > float(gui.superCustE.get()):
masterSuperCust += gui.interval
if temp < float(gui.subCustE.get()):
masterSubCust += gui.interval
#Output summary statistics
if gui.makeOutputBool:
file = gui.outName
#If "Ouptut statistics" is not selected but "Compile statistics" is, send the summary to compile statistics file
elif gui.compileStatsBool:
file = gui.compileName
#If either of these options is selected, generate the file stats summary
if gui.makeOutputBool or gui.compileStatsBool:
with open(file, "a") as summaryFile:
#Used to indictate scope of certain statistics
if gui.restrictSearchBV.get() is True:
qualifier = " (D),"
else:
qualifier = " (DN),"
#Print input file name first (remove path)
if file == gui.compileName:
print (os.path.basename(os.path.normpath(gui.inFile)), file = summaryFile)
else:
print ("Day and Cumulative Stats", file = summaryFile)
#Output headers
if gui.dayNumVar.get() : print ("Day Number,", end = "", file = summaryFile)
if gui.dateVar.get() : print ("Date,", end = "", file = summaryFile)
if gui.offCountVar.get() : print ("Off-bout Count" + qualifier, end = "", file = summaryFile)
if gui.offDurVar.get() : print ("Mean Off Duration (min)" + qualifier, end = "", file = summaryFile)
if gui.offDurSDVar.get() : print ("Off Dur StDev" + qualifier, end = "", file = summaryFile)
if gui.offDecVar.get() : print ("Mean Off Temp Drop" + qualifier, end = "", file = summaryFile)
if gui.offDecSDVar.get() : print ("Off Drop StDev" + qualifier, end = "", file = summaryFile)
if gui.meanOffTempVar.get() : print ("Mean Off-Bout Temp" + qualifier, end = "", file = summaryFile)
if gui.offTimeSumVar.get() : print ("Off-Bout Time Sum" + qualifier, end = "", file = summaryFile)
if gui.onCountVar.get() : print ("On-bout Count" + qualifier, end = "", file = summaryFile)
if gui.onDurVar .get() : print ("Mean On Duration (min)" + qualifier, end = "", file = summaryFile)
if gui.onDurSDVar.get() : print ("On Dur StDev" + qualifier, end = "", file = summaryFile)
if gui.onIncVar.get() : print ("Mean On Temp Rise" + qualifier, end = "", file = summaryFile)
if gui.onIncSDVar.get() : print ("On Rise StDev" + qualifier, end = "", file = summaryFile)
if gui.meanOnTempVar.get() : print ("Mean On-Bout Temp" + qualifier, end = "", file = summaryFile)
if gui.onTimeSumVar.get() : print ("On-Bout Time Sum" + qualifier, end = "", file = summaryFile)
if gui.superCustVar.get() : print ("Time above (minutes)", gui.superCustE.get(), "(DN),", end = "", file = summaryFile)
if gui.subCustVar.get() : print ("Time below (minutes)", gui.subCustE.get(), "(DN),", end = "", file = summaryFile)
if gui.boutsDroppedVar.get(): print ("Vertices Dropped" + qualifier, end = "", file = summaryFile)
if gui.meanTempDVar.get() : print ("Mean Daytime Egg Temp,", end = "", file = summaryFile)
if gui.meanTempDSDVar.get() : print ("Day Egg Temp StDev,", end = "", file = summaryFile)
if gui.medianTempDVar.get() : print ("Median Daytime Egg Temp,", end = "", file = summaryFile)
if gui.minTempDVar.get() : print ("Min Daytime Egg Temp,", end = "", file = summaryFile)
if gui.maxTempDVar.get() : print ("Max Daytime Egg Temp,", end = "", file = summaryFile)
if gui.meanTempNVar.get() : print ("Mean Nighttime Egg Temp,", end = "", file = summaryFile)