Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate running time from status and time using python

Tags:

python

I have a circulation pump that I check wither it's on or off on and this is not by any fixed interval what so ever. For a single day that could give me a dataset looking like this where 'value' represents the pump being on or off.

data=(
 {'value': 0, 'time': datetime.datetime(2011, 1, 18, 7, 58, 25)},
 {'value': 1, 'time': datetime.datetime(2011, 1, 18, 8, 0, 3)},
 {'value': 0, 'time': datetime.datetime(2011, 1, 18, 8, 32, 10)},
 {'value': 0, 'time': datetime.datetime(2011, 1, 18, 9, 22, 7)},
 {'value': 1, 'time': datetime.datetime(2011, 1, 18, 9, 30, 58)},
 {'value': 1, 'time': datetime.datetime(2011, 1, 18, 12, 2, 23)},
 {'value': 0, 'time': datetime.datetime(2011, 1, 18, 15, 43, 11)},
 {'value': 1, 'time': datetime.datetime(2011, 1, 18, 20, 14, 55)})

The format is not that important and can be changed.

What I do want to know is how to calculate how many minutes ( or timespan or whatever) the 'value' has been 0 or 1 (or ON or OFF)?

This is just a small sample of the data, it stretches over several years so there could be a lot. I have been using numpy/mathplotlib for plotting some graphs and there might be something in numpy to do this but I'm not good enough at it.

Edit

What I would like to see as an output to this would be a sum of the time in the different states. Something like...

0 04:42:13  
1 07:34:17
like image 978
kmpm Avatar asked Nov 05 '22 04:11

kmpm


1 Answers

It really depends on how you're going to treat this data points, are they representative of what? Generally, to know when switch occur you could use itertools.groupby like this:

>>> from itertools import groupby
>>> for i, grp in groupby(data, key=lambda x: x['value']):
    lst = [x['time'] for x in grp]
    print(i, max(lst) - min(lst))


0 0:00:00
1 0:00:00
0 0:49:57
1 2:31:25
0 0:00:00
1 0:00:00

This is the example of minimal time you can be sure your system was up or down (assuming no interruptions between measurement).

Once you decide how to treat your points, modification to this algorithm would be trivial.


EDIT: since you only need sums of up/down-time, here is the simpler version:

>>> sums = {0:datetime.timedelta(0), 1:datetime.timedelta(0)}
>>> for cur, nex in zip(data, data[1:]):
    sums[cur['value']] += nex['time'] - cur['time']


>>> for i, j in sums.items():
    print(i, j)


0 5:32:10
1 6:44:20

If you expect long-periods of continuous up/down-time, you might still benefit of the itertools.groupby. This is py3k version, so it won't be particularly efficient in py2k.

like image 66
SilentGhost Avatar answered Nov 11 '22 05:11

SilentGhost