-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaes.py
More file actions
137 lines (120 loc) · 4.37 KB
/
aes.py
File metadata and controls
137 lines (120 loc) · 4.37 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
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
from typing import Dict, Optional, Tuple, Union
from python_ggplot.core.objects import AxisKind, GGException
from python_ggplot.gg.datamancer_pandas_compat import VectorCol, VNull
from python_ggplot.gg.scales.base import (
GGScale,
GGScaleData,
LinearAndTransformScaleData,
LinearDataScale,
ScaleType,
TransformedDataScale,
scale_type_to_cls,
)
from python_ggplot.gg.types import Aesthetics, gg_col_const
from tests.test_plots import gg_col
_AES_PARAM_TO_SCALE_ARGS: Dict[str, Tuple[ScaleType, Optional[AxisKind]]] = {
"x": (ScaleType.LINEAR_DATA, AxisKind.X),
"y": (ScaleType.LINEAR_DATA, AxisKind.Y),
"xintercept": (ScaleType.LINEAR_DATA, AxisKind.X),
"yintercept": (ScaleType.LINEAR_DATA, AxisKind.Y),
"color": (ScaleType.COLOR, None),
"fill": (ScaleType.COLOR, None),
"shape": (ScaleType.SHAPE, None),
"size": (ScaleType.SIZE, None),
"x_min": (ScaleType.LINEAR_DATA, AxisKind.X),
"x_max": (ScaleType.LINEAR_DATA, AxisKind.X),
"y_min": (ScaleType.LINEAR_DATA, AxisKind.Y),
"y_max": (ScaleType.LINEAR_DATA, AxisKind.Y),
"width": (ScaleType.LINEAR_DATA, AxisKind.X),
"height": (ScaleType.LINEAR_DATA, AxisKind.Y),
"text": (ScaleType.TEXT, None),
"yridges": (ScaleType.LINEAR_DATA, AxisKind.Y),
"weight": (ScaleType.LINEAR_DATA, AxisKind.Y),
}
def _init_field(arg_: str, arg_value_: Optional[str]):
if arg_value_ is None:
return None
has_discreteness = has_factor(arg_)
(scale_type, axis_kind) = _AES_PARAM_TO_SCALE_ARGS[arg_]
scale_cls = scale_type_to_cls(scale_type)
if scale_cls in (LinearDataScale, TransformedDataScale):
if axis_kind is None:
raise GGException("expected axis type")
data = LinearAndTransformScaleData(axis_kind=axis_kind)
if isinstance(arg_value_, (str, gg_col)):
col = VectorCol(col_name=arg_value_)
else:
col = VectorCol(col_name=gg_col_const(arg_value_))
return scale_cls(
gg_data=GGScaleData(
col=col,
has_discreteness=has_discreteness,
value_kind=VNull(), # TODO this will be fixed eventually
),
data=data,
)
else:
return scale_cls(
gg_data=GGScaleData(
col=VectorCol(arg_value_),
has_discreteness=has_discreteness,
value_kind=VNull(), # TODO this will be fixed eventually
)
)
def has_factor(arg_: str) -> bool:
# TODO macro + AST magic from nim
# skip for now, but we have to deal with this one day
return False
def _init_aes(data: Dict[str, Optional[str]]) -> Aesthetics:
aes_data: Dict[str, Optional[GGScale]] = {}
for arg_, arg_value_ in data.items():
new_scale = _init_field(arg_, arg_value_) # type: ignore
aes_data[arg_] = new_scale
return Aesthetics(**aes_data)
def aes(
x: Optional[Union[str, gg_col]] = None,
y: Optional[Union[str, gg_col]] = None,
xintercept: Optional[Union[str, gg_col]] = None,
yintercept: Optional[Union[str, gg_col]] = None,
color: Optional[str] = None,
fill: Optional[str] = None,
shape: Optional[str] = None,
size: Optional[str] = None,
xmin: Optional[str | float] = None,
xmax: Optional[str | float] = None,
ymin: Optional[str | float] = None,
ymax: Optional[str | float] = None,
width: Optional[str] = None,
height: Optional[str] = None,
text: Optional[str] = None,
yridges: Optional[str] = None,
weight: Optional[str] = None,
):
"""
We dont want the public API to take in **kwargs
that is very hard for users to read
we'd rather take them in and call a private function underneath
The nature of this has to be a bit more dynamic because the origin is using macro
otherwise it will be a good amount of re-work around the logic
TODO: provide Literal definitions for input types
"""
data: Dict[str, Optional[str]] = {
"x": x,
"y": y,
"xintercept": xintercept,
"yintercept": yintercept,
"color": color,
"fill": fill,
"shape": shape,
"size": size,
"x_min": xmin,
"x_max": xmax,
"y_min": ymin,
"y_max": ymax,
"width": width,
"height": height,
"text": text,
"y_ridges": yridges,
"weight": weight,
}
return _init_aes(data)