-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
75 lines (62 loc) · 2 KB
/
analysis.py
File metadata and controls
75 lines (62 loc) · 2 KB
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
from math import ceil
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from orgparse import load
WEEK_ROOT = Path("/Users/kevin/org/weeks")
PROPERTIES = [
"Sleep",
"Exercise",
"Happiness",
"Wellbeing",
"Eating",
"Stress",
"Fasting",
]
MAX_WEEK = 8
FIRST_WEEKDAY_INDEX = 3
def load_data(
max_week: int = MAX_WEEK,
week_root: Path = WEEK_ROOT,
first_weekday_index: int = FIRST_WEEKDAY_INDEX,
) -> pd.DataFrame:
values = {}
for week in range(1, max_week + 1):
root = load(week_root / f"{week}.org")
for child_index in range(first_weekday_index, first_weekday_index + 7):
day_values = root.children[child_index].properties
day_values.update({"week": week})
day = (week - 1) * 7 + (child_index - first_weekday_index)
values[day] = day_values
df = pd.DataFrame(values)
return df.transpose()
def process_data(df: pd.DataFrame) -> pd.DataFrame:
df = df.replace("x", np.nan)
df = df.astype(np.float16)
for property in PROPERTIES:
averages = df.groupby("week")[property].mean().reset_index()
df = df.merge(averages, on="week", suffixes=["", "_weekly"])
return df
def plot_histograms(df: pd.DataFrame, columns: list[str] = PROPERTIES) -> None:
fig, ax = plt.subplots()
df[columns].hist(ax=ax)
fig.show()
def plot_trends(
df: pd.DataFrame, columns: list[str] = PROPERTIES, n_plot_cols: int = 4
) -> None:
fig, axs = plt.subplots(ceil(len(columns) / n_plot_cols), n_plot_cols)
axis_counter = 0
df2 = df.reset_index()
for property in PROPERTIES:
ax = axs[axis_counter // n_plot_cols][axis_counter % n_plot_cols]
df2.plot(x="index", y=property, ax=ax)
df2.plot(x="index", y=f"{property}_weekly", ax=ax)
axis_counter += 1
if property != "Fasting":
ax.set_ylim([0.0, 5.0])
fig.show()
df_raw = load_data()
df = process_data(df_raw)
plot_histograms(df)
plot_trends(df)