-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotting.py
145 lines (128 loc) · 3.56 KB
/
plotting.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
from typing import Callable, Optional
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from partitioning import flatten
def hist(
real,
gen,
title: str= '',
xlabel: str= '',
ylabel: str= 'density',
*,
bins: Optional[int|str|list[float]] = 'auto',
binwidth: Optional[float] = None,
ax: Optional[plt.Axes] = None,
):
"""
"""
real = pd.DataFrame(flatten(real))
real['type'] = 'real'
gen = pd.DataFrame(flatten(gen))
gen['type'] = 'generated'
data = pd.concat([real, gen], axis=0)
data.columns = ['x', 'type']
# display(data)
sns.histplot(
data, x='x', hue='type',
alpha=0.5, stat='density', multiple='layer',
common_bins=True, common_norm=False, binwidth=binwidth, bins=bins,
ax=ax
)
sns.move_legend(
ax, "lower center",
bbox_to_anchor=(.5, 1.1), ncol=3, title=None, frameon=False,
)
# sns.histplot(gen, color='red', alpha=0.5, label='Generated', ax=plt.gca())
# plt.legend()
_finish_plot(title, xlabel, ylabel, ax)
# if not isinstance(bins, str):
# print(f'bins: {bins}')
def scatter(
score_df: pd.DataFrame,
title: str= '',
xlabel: str= 'Group',
ylabel: str= 'Score',
ax: Optional[plt.Axes] = None,
):
"""
"""
sns.scatterplot(data=score_df, x='group', y='score', hue='type')
_finish_plot(title, xlabel, ylabel, ax)
def line(
score_df: pd.DataFrame,
title: str= '',
xlabel: str= 'Group',
ylabel: str= 'Score',
ax: Optional[plt.Axes] = None,
):
"""
"""
sns.lineplot(
data=score_df, x='group', y='score', hue='type',
errorbar=("pi", 0.95)
)
_finish_plot(title, xlabel, ylabel, ax)
def error_divergence_plot(
loss_horizons: list[tuple[float, np.ndarray]],
horizon_length: int,
title: str= '',
xlabel: str= 'Prediction Horizon',
ylabel: str= 'Error',
ax: Optional[plt.Axes] = None,
):
"""
"""
labels = [f"{period}-{period+horizon_length}"
for period in range(
0,
horizon_length*len(loss_horizons),
horizon_length
)
]
l1s = np.array([l[0] for l in loss_horizons])
cis = np.array([l[1] for l in loss_horizons]).T
sns.lineplot(
x=labels,
y=l1s,
ax=ax
)
if ax is None:
ax = plt
ax.errorbar(
x=labels, y=cis.mean(axis=0), yerr=np.diff(cis, axis=0),
fmt='none')
_finish_plot(title, xlabel, ylabel, ax)
def hist_subplots(
plot_fns: dict[str, Callable[[str, plt.Axes], None]],
figsize: Optional[tuple[int, int]] = None,
):
"""
"""
fig, axs = plt.subplots(np.ceil(len(plot_fns) / 2).astype(int), 2, figsize=figsize)
axs = axs.reshape(-1)
for i, (name, fn) in enumerate(plot_fns.items()):
fn(name, axs[i])
if i > 0:
axs[i].get_legend().remove()
lines_labels = [ax.get_legend_handles_labels() for ax in axs]
lines, labels = [sum(lol, []) for lol in zip(*lines_labels)]
fig.legend(lines, labels)
plt.tight_layout()
def _finish_plot(
title: str = '',
xlabel: str = 'Group',
ylabel: str = 'Score',
ax: Optional[plt.Axes] = None,
):
"""
"""
if ax is not None:
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.set_title(title)
else:
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)