-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.py
More file actions
423 lines (331 loc) · 10.1 KB
/
objects.py
File metadata and controls
423 lines (331 loc) · 10.1 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import math
from dataclasses import dataclass, field, fields
from enum import Enum, auto
from typing import (
TYPE_CHECKING,
Any,
Generic,
List,
Literal,
Optional,
Type,
TypeVar,
Union,
)
from python_ggplot.common.objects import Freezable
from python_ggplot.core.common import linspace
from python_ggplot.graphics.cairo_backend import CairoBackend
if TYPE_CHECKING:
from python_ggplot.core.objects import Scale
Z = TypeVar("Z", bound="GGEnum")
class GGEnum(Enum):
@staticmethod
def _generate_next_value_(name, start, count, last_values): # type: ignore
return name.lower()
@classmethod
def is_possible_value(cls, value: str):
return value in [item.value for item in cls]
@classmethod
def value_to_name(cls, value: str):
# TODO low priority we could cache this if it matters
data = {item.value: item.name for item in cls}
return data.get(data)
@classmethod
def value_to_item(cls: Type[Z], value: str) -> Z:
# TODO low priority we could cache this if it matters
data = {item.value: item for item in cls}
try:
return data[value]
except KeyError as e:
raise GGException("enum type does not exist") from e
@classmethod
def eitem(cls: Type[Z], value: str) -> Z:
# TODO this is a bad name, but very convinient
# remove or keep?
# named it "eitem" to be able to regex it out
return cls.value_to_item(value)
class Duration:
"""
Port from nim lang
see here https://github.com/nim-lang/Nim/blob/version-2-2/lib/pure/times.nim#L662
"""
def __init__(self, seconds: int, nanoseconds: int = 0):
extra_seconds, normalized_nanoseconds = divmod(nanoseconds, 1_000_000_000)
self.total_seconds = seconds + extra_seconds
self.nanoseconds = normalized_nanoseconds
def in_seconds(self) -> int:
return self.total_seconds
def in_milliseconds(self) -> int:
return (self.total_seconds * 1_000) + (self.nanoseconds // 1_000_000)
def init_duration(
nanoseconds: int = 0,
microseconds: int = 0,
milliseconds: int = 0,
seconds: int = 0,
minutes: int = 0,
hours: int = 0,
days: int = 0,
weeks: int = 0,
) -> Duration:
# TODO this needs many unit tests
total_seconds = (
weeks * 7 * 24 * 60 * 60
+ days * 24 * 60 * 60
+ hours * 60 * 60
+ minutes * 60
+ seconds
+ milliseconds // 1_000
+ microseconds // 1_000_000
+ nanoseconds // 1_000_000_000
)
total_nanoseconds = (
(milliseconds % 1_000) * 1_000_000
+ (microseconds % 1_000_000) * 1_000
+ (nanoseconds % 1_000_000_000)
)
return Duration(total_seconds, total_nanoseconds)
class MarkerKind(GGEnum):
CIRCLE = auto()
CROSS = auto()
TRIANGLE = auto()
RHOMBUS = auto()
RECTANGLE = auto()
ROTCROSS = auto()
UPSIDEDOWN_TRIANGLE = auto()
EMPTY_CIRCLE = auto()
EMPTY_RECTANGLE = auto()
EMPTY_RHOMBUS = auto()
class FileTypeKind(GGEnum):
SVG = auto()
PNG = auto()
PDF = auto()
VEGA = auto()
TEX = auto()
@dataclass
class Image:
fname: str
width: int
height: int
ftype: FileTypeKind
backend: CairoBackend
@dataclass
class HueConfig:
hue_start: float = 15.0
chroma: float = 100.0
luminance: float = 65.0
class LineType(GGEnum):
NONE_TYPE = auto()
SOLID = auto()
DASHED = auto()
DOTTED = auto()
DOT_DASH = auto()
LONG_DASH = auto()
TWO_DASH = auto()
class ErrorBarKind(GGEnum):
LINES = auto()
LINEST = auto()
class TextAlignKind(GGEnum):
LEFT = auto()
CENTER = auto()
RIGHT = auto()
class CFontSlant(GGEnum):
NORMAL = auto()
ITALIC = auto()
OBLIQUE = auto()
# TODO Move all color logic in chroma
@dataclass
class Color(Freezable):
r: float
g: float
b: float
a: float = 1.0
def new_color_with_alpha(self, alpha: float):
return Color(r=self.r, g=self.g, b=self.b, a=alpha)
def __eq__(self, o: Any) -> bool:
return self.r == o.r and self.g == o.g and self.b == o.b and self.a == o.a
def to_rgba(self) -> "ColorRGBA":
return ColorRGBA(
r=int(self.r * 255),
b=int(self.b * 255),
g=int(self.g * 255),
a=self.a,
)
@dataclass
class ColorRGBA(Freezable):
r: int
g: int
b: int
a: float = 1.0
def to_color(self):
return Color(
r=self.r / 255,
b=self.b / 255,
g=self.g / 255,
a=self.a,
)
def __eq__(self, o: Any) -> bool:
return self.r == o.r and self.g == o.g and self.b == o.b and self.a == o.a
@dataclass
class ColorHCL:
h: float
c: float
l: float
def to_rgb(self) -> Color:
from python_ggplot.core.chroma import hcl_to_rgb_via_luv_and_xyz
return Color(**hcl_to_rgb_via_luv_and_xyz(self.h, self.c, self.l))
@staticmethod
def gg_color_hue(num: int, hue_config: Optional[HueConfig] = None) -> List["Color"]:
if not hue_config:
hue_config = HueConfig()
# the colors are slighly off, but moslty fine
hues = linspace(
hue_config.hue_start, hue_config.hue_start + 360.0, num + 1, endpoint=True
)
colors = [
ColorHCL(h=h, c=hue_config.chroma, l=hue_config.luminance).to_rgb()
for h in hues
]
return colors
@dataclass
class Font:
family: str = "sans-serif"
size: float = 12.0
bold: bool = False
slant: CFontSlant = CFontSlant.NORMAL
color: Color = field(default_factory=lambda: Color(r=0.0, g=0.0, b=0.0, a=1.0))
align_kind: TextAlignKind = TextAlignKind.CENTER
@dataclass
class Gradient:
colors: List[Color]
rotation: float = 0.0
class AxisKind(GGEnum):
X = auto()
Y = auto()
def opposite(self) -> "AxisKind":
if self == AxisKind.X:
return AxisKind.Y
elif self == AxisKind.Y:
return AxisKind.X
else:
raise GGException("Invalid axis")
T = TypeVar("T", int, float)
@dataclass
class Point(Generic[T]):
x: T
y: T
def relative_to_scale(self, scale_x: "Scale", scale_y: "Scale") -> "Point[T]":
point: Point[T] = Point(x=self.x * scale_x.high, y=self.y * scale_y.high) # type: ignore
return point
@dataclass
class Style:
line_width: float = 1.0
color: Color = field(default_factory=lambda: Color(r=0.0, g=0.0, b=0.0, a=0.0))
size: float = 0.0
line_type: LineType = LineType.NONE_TYPE
fill_color: Optional[Color] = field(
default_factory=lambda: Color(r=0.0, g=0.0, b=0.0, a=0.0)
)
marker: Optional[MarkerKind] = MarkerKind.CIRCLE
error_bar_kind: Optional[ErrorBarKind] = ErrorBarKind.LINES
gradient: Optional[Gradient] = None
font: Optional[Font] = None
def __rich_repr__(self):
"""
TODO make this generic?
"""
exclude_field = "gradient"
for field in fields(self):
if field.name != exclude_field:
yield field.name, getattr(self, field.name)
# this by default would print the whole set, one item at a time
if self.gradient is not None:
yield f"gradient -> min: {self.gradient.colors[0]} max: {self.gradient.colors[-1]} count: {len(self.gradient.colors)}"
class CompositeKind(GGEnum):
ERROR_BAR = auto()
class TickKind(GGEnum):
ONE_SIDE = auto()
BOTH_SIDES = auto()
class OptionError(Exception):
pass
K = TypeVar("K")
def either(a: Optional[K], b: Optional[K]):
if a is not None:
return a
if b is not None:
return b
raise OptionError("Both options are None.")
@dataclass
class Scale:
low: float
high: float
def merge(self, other: "Scale") -> "Scale":
if not (self.is_empty() and math.isclose(self.low, 0)):
return Scale(
low=min(self.low, other.low),
high=max(self.high, other.high),
)
else:
return other
def is_empty(self) -> bool:
return math.isclose(self.low, self.high)
def __eq__(self, o) -> bool: # type: ignore
return math.isclose(self.low, o.low) and math.isclose(self.high, o.high) # type: ignore
def normalise_pos(self, val: float, reverse: bool = False) -> float:
rel_pos = (float(val) - self.low) / (self.high - self.low)
if reverse:
return 1 - rel_pos
else:
return rel_pos
class GGException(Exception):
pass
class UnitType(GGEnum):
POINT = auto()
CENTIMETER = auto()
INCH = auto()
RELATIVE = auto()
DATA = auto()
STR_WIDTH = auto()
STR_HEIGHT = auto()
ABSTRACT = auto()
def is_length(self):
return self.value in (
UnitType.POINT.value,
UnitType.CENTIMETER.value,
UnitType.INCH.value,
)
def is_absolute(self):
return self.value in (
UnitType.POINT.value,
UnitType.CENTIMETER.value,
UnitType.INCH.value,
UnitType.STR_HEIGHT.value,
UnitType.STR_WIDTH.value,
)
TextType = Union[Literal[UnitType.STR_HEIGHT], Literal[UnitType.STR_WIDTH]]
LengthType = Union[
Literal[UnitType.POINT],
Literal[UnitType.CENTIMETER],
Literal[UnitType.INCH],
]
# TODO low priority this is defined in 2 places, core.objects core.common
# do general cleanup later
GREY92_DICT = {"r": 0.92, "g": 0.92, "b": 0.92, "a": 1.0}
GREY20_DICT = {"r": 0.20, "g": 0.20, "b": 0.20, "a": 1.0}
BLACK_DICT = {"r": 0.0, "g": 0.0, "b": 0.0, "a": 1.0}
WHITE_DICT = {"r": 1.0, "g": 1.0, "b": 1.0, "a": 1.0}
TRANSPARENT_DICT = {"r": 0.0, "g": 0.0, "b": 0.0, "a": 0.0}
GREY92 = Color(r=0.92, g=0.92, b=0.92, a=1.0)
GREY20 = Color(r=0.20, g=0.20, b=0.20, a=1.0)
GREY35 = Color(r=0.35, g=0.35, b=0.35, a=1.0)
BLACK = Color(r=0.0, g=0.0, b=0.0, a=1.0)
WHITE = Color(r=1.0, g=1.0, b=1.0, a=1.0)
TRANSPARENT = Color(r=0.0, g=0.0, b=0.0, a=0.0)
@dataclass
class TexOptions:
use_te_x: bool
tex_template: Optional[str]
standalone: bool
only_tik_z: bool
caption: Optional[str]
label: Optional[str]
placement: str