Created
September 22, 2020 04:21
-
-
Save lgommans/0d6fb2d21204849ee5e5d1915791bd28 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
if '-h' in sys.argv or '--help' in sys.argv or len(sys.argv) < 2: | |
print(''' | |
Usage: {} run | |
(the run keyword just indicates you read/know this) | |
This script calculates what, as far as I've been able to come up with and find, | |
is hopefully the best possible average value during a time series when the | |
times are neither regular nor random. In either case, you could just average | |
the values, but if there is bias (like if you check your freezer more often | |
after you noticed the temperature is off), this script should correct for that | |
bias. The result will not be correct, but it's hopefully the least wrong in | |
the given scenario. | |
Expects stdin formatted as: unixtime,value\\nunixtime,value\\n (etc.) | |
Example: | |
0,0 | |
10,5 | |
11,99 | |
value | |
0 5 99 | |
||-----------||-------------||-| | |
1 2 3 4 5 6 7 8 9 10 11 | |
time | |
Will result in 7 because time 0-5 is considered to be of value zero, time | |
5-10.5 is considered to be of value 5, and 10.5-11 is considered to be of value | |
99. Over 11 seconds, that total comes to 5*0 + 5*5.5 + 99*0.5 = 77, divided by 11 is | |
an average of 7. | |
'''.lstrip().format(sys.argv[0])) | |
exit(1) | |
first = True | |
total = 0 | |
starttime = None | |
for line in sys.stdin: | |
if ',' not in line: # blank line, maybe a comment, idk | |
continue | |
t, val = map(float, line.strip().split(',')) | |
if first: | |
starttime = t | |
lasttime = t | |
lastvalue = val | |
first = False | |
continue | |
timepassed = t - lasttime | |
total += lastvalue * (timepassed / 2) | |
total += val * (timepassed / 2) | |
lasttime = t | |
lastvalue = val | |
print(total / (lasttime - starttime)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment