-
Notifications
You must be signed in to change notification settings - Fork 0
/
raben_stats.py
executable file
·141 lines (126 loc) · 4.71 KB
/
raben_stats.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat May 2 09:10:50 2020
@author: juliaschopp
"""
import pandas
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
from matplotlib.ticker import FuncFormatter
import matplotlib.dates as mdates
from raben_helper import *
def get_stats(df, event, night=False):
"""
Input: df and event (str), optional: night (bool), default False,
to indicate whether data should be split for day & night
Output:
if night=False:
prints mean, max and min of event, returns count of event per day (df)
if night=True:
prints mean of event for day vs. night, returns tuple of 3 dfs:
count of event at night, count of event at daytime, complete count
"""
if not night:
data = df.loc[df["Ereignis"] == event].groupby("Datum")
count = data["Ereignis"].count()
mean = count.mean()
maxi = count.max()
mini = count.min()
print("Statistik für " + event + " : Im Schnitt " \
+ str(mean) + " mal, max: " + str(maxi) + " , min: " + str(mini))
return count
else:
data_night = df.loc[(df["Ereignis"] == event) & (df["Nacht"] == True)].groupby("Datum")
data_day = df.loc[(df["Ereignis"] == event) & (df["Nacht"] == False)].groupby("Datum")
data_complete = get_stats(df, event, night=False)
res = (data_night["Ereignis"].count(), data_day["Ereignis"].count(), data_complete)
print(event + " nachts im Schnitt: " + str(res[0].mean()) + " , tags: "\
+ str(res[1].mean()))
return res
def plot_stats(df, events_plot): # to do: add y-label, title, ...
"""
Input: Df, list of events which should be plotted,
night, a boolean which indicates whether night and day should be plotted
seperately
Output: A plot
"""
days = mdates.DayLocator()
days_fmt = mdates.DateFormatter('%d.%m.')
fig, ax = plt.subplots(figsize=(10,8))
for e in events_plot:
print(e)
dates = [mdates.date2num(i) for i in get_stats(df,e).keys()]
values = list(get_stats(df,e))
ax.plot(dates, values, label=e)
ax.xaxis.set_major_locator(days)
ax.xaxis.set_major_formatter(days_fmt)
fig.autofmt_xdate()
plt.legend(loc='upper right')
plt.show()
def plot_day_night(df, event): # to do: add y-label, title,...
"""
Input:
df, event(str)
Output:
plots number of event per day for day, night and total
"""
days = mdates.DayLocator()
days_fmt = mdates.DateFormatter('%d.%m.')
fig, ax = plt.subplots(figsize=(10,8))
data = get_stats(df, event, night=True)
labels = ["Nachts", "Tags", "Gesamt"]
count = 0
for e in data:
dates = [mdates.date2num(i) for i in e.keys()]
values = list(e)
ax.plot(dates, values, label=labels[count])
count += 1
ax.xaxis.set_major_locator(days)
ax.xaxis.set_major_formatter(days_fmt)
fig.autofmt_xdate()
plt.legend(loc='upper right')
plt.show()
def get_sleeping_stats(df):
"""
Calculates the sleeping statistics for the whole timespan (as in df)
__________
Parameters
----------
df (pandas df)
Returns
-------
total_time : total sleeping time.
time_per_day : total time divided by number of days
"""
total_time = 0
filtered = df.loc[(df.Ereignis == "Einschlafen") | (df.Ereignis=="Aufwachen")]
inds = list(filtered.index)
if df.iloc[inds[0],2] != "Einschlafen":
inds.pop(0)
if df.iloc[inds[-1],2] != "Aufwachen":
inds.pop()
timestamps = [df.iloc[ind,1] for ind in inds]
while len(timestamps) != 0:
sleeptime = timestamps.pop(0)
waketime = timestamps.pop(0)
total_time += abs(waketime - sleeptime)
time_per_day = total_time/len(df.groupby("Datum"))
print("Gesamt: " + str(total_time//60) + " Stunden " + str(total_time % 60) \
+ " Minuten verteilt auf " + str(len(df.groupby("Datum"))) + " Tage, also " \
+ str(int(time_per_day//60)) + " Stunden " + str(int(time_per_day%60)) + " Minuten" \
+ " pro Tag.")
return total_time, time_per_day
#--- Variables & Go
file = 'rabeneltern.csv'
colnames = ["Datum", "Zeit", "Ereignis"]
events = ["Stillen", "Brei", "Aufwachen", "Einschlafen"]
dates = [("20.03.2020", "29.03.2020"), ("30.03.2020", "07.04.2020"), \
("19.04.2020", "28.04.2020")]
events_plot = ["Stillen", "Aufwachen", "Einschlafen"] # for plot_stats
df = create_df(file, colnames, rounded=False, timespan=dates[2])
stats = get_stats(df, "Aufwachen")
stats2 = get_stats(df, "Stillen", night=True)
#plot_stats(df, events_plot)
#plot_day_night(df, "Stillen")