-
Notifications
You must be signed in to change notification settings - Fork 2
/
_utils.py
76 lines (65 loc) · 2.61 KB
/
_utils.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
import typing
import pandas as pd
from bisect import bisect
import matplotlib.pyplot as plt
import seaborn as sns
def quick_plot_setup(use_tex=True) -> None:
"""
Applies a quick setup for publication ready plots.
This style has been used for LIPICs. Maybe other template need different styles.
:return: None
"""
sns.set_theme()
plt.rcParams.update({
"text.usetex": use_tex,
"font.family": "serif",
"font.serif": ["Helvetica"]})
def convert_to_interval_on_values(table: pd.DataFrame, on_column: str, interval,
values) -> pd.DataFrame:
data = {c: [] for c in table.columns}
for i, row in table.iterrows():
lb, ub = interval(row[on_column])
i_ = bisect(values, lb)
for v in values[i_:]:
if v > ub:
break
for c in table.columns:
if c == on_column:
data[c].append(v)
else:
data[c].append(row[c])
return pd.DataFrame(data)
def convert_to_interval(table: pd.DataFrame,
on_column: str,
interval,
round_: typing.Callable[[float], float] = lambda x: x) \
-> pd.DataFrame:
values = []
for v in table[on_column].unique():
lb, ub = interval(v)
values.append(round_(lb))
values.append(round_(ub))
values = list(set(values))
values.sort()
return convert_to_interval_on_values(table, on_column, interval, values)
def convert_to_percentage_interval(table: pd.DataFrame,
on_column: str,
percentage,
round_: typing.Callable[[float], float] = lambda x: x
) -> pd.DataFrame:
"""
A helpful function to convert the x-axis to +/- P% intervals. This is necessary
if your x-values are sparse such that no aggregation can be performed.
Depending on the values, the size of the table can strongly increase making it
terribly slow. Use the 'round_' parameter to specify a desired resolution.
This also allows a logarithmic resolution.
:param table: The table containing the data
:param on_column: Will be performed on this column (x-axis)
:param percentage: The percentage to +/-
:param round_: A function to reduce the resolution and increase the processing speed.
:return:
"""
def interval(v):
d = (percentage / 100) * v
return round_(v - d), round_(v + d)
return convert_to_interval(table, on_column, interval, round_)