-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.ua
308 lines (269 loc) · 9.46 KB
/
lib.ua
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
# Experimental!
# Uiua data plotting library
~ "git: github.com/Omnikar/uiua-math" ~ FFTConvolve Gaussian
# Apply a color to an alpha map
C ↚ ⍜(⊣°⍉)×∧↯⇌△,⬚1↙₄
# Round to one significant figure
AutoRound ↚ ⍜×⁅𝄈ⁿ10¯⌊⊸ₙ10
Num ↚ °0type
Char ↚ °1type
Box ↚ °2type
# TODO: Document this more
# The following fields can infer their values based on data:
# - `Min`
# - `Max`
# - `GridlineInterval`
# The following fields provide defaults for unspecified series-specific values:
# - `DrawDots`
# - `DotSize`
# - `DrawLines`
# - `LineWidth`
┌─╴PlotConfig
~{
Min: °2°¤△⟜Num ← ⊟.∞
Max: °2°¤△⟜Num ← ⊟.∞
Size: °2°¤△⟜Num ← 512_512
BgColor: ⍣°3°4°¤△⟜Num ← ↯3 1
AxisWidth: °[]△⟜Num ← 2
AxisColor: ⍣°3°4°¤△⟜Num ← ↯3 0
GridlineWidth: °[]△⟜Num ← 1
GridlineColor: ⍣°3°4°¤△⟜Num ← ↯3 0.7
GridlineInterval: °2°¤△⟜Num ← ⊟.∞
NumberSize: °[]△⟜Num ← 15
NumberColor: ⍣°3°4°¤△⟜Num ← ↯3 0.3
XLabel: °[W]△⟜Char ← ""
YLabel: °[W]△⟜Char ← ""
AxisLabelSize: °[]△⟜Num ← 20
AxisLabelColor: ⍣°3°4°¤△⟜Num ← ↯3 0.2
PlotLabelSize: °[]△⟜Num ← 15
PlotLabelColor: ⍣°3°4°¤△⟜Num ← ↯3 0.2
BarWidth: °[]△⟜Num ← 40
BarLabels: °[W]△⟜∵◇Char⟜Box ← {}
BarLabelSize: °[]△⟜Num ← 15
BarLabelColor: ⍣°3°4°¤△⟜Num ← ↯3 0.2
# Series-specific global defaults
DrawDots: °[]△⟜Num ← 1
DotSize: °[]△⟜Num ← 40
DrawLines: °[]△⟜Num ← 1
LineWidth: °[]△⟜Num ← 8
}
# Pixels per unit
Ppu ← ÷-⊃Min⊃Max Size
Bounds ← ⌅([⊃Min Max]|⟜[⊃Min Max]|⍜Max◌⍜Min◌⊙°⊟)
XBounds ← ⊢⍉Bounds
YBounds ← ⊣⍉Bounds
└─╴
PC ↚ PlotConfig
┌─╴Data
~{
Values
Color: ⍣°3°4°¤△⟜Num ← ↯4 ∞
Label: °[W]△⟜Char ← ""
DrawDots: °[]△⟜Num ← ∞
DotSize: °[]△⟜Num ← ∞
DrawLines: °[]△⟜Num ← ∞
LineWidth: °[]△⟜Num ← ∞
}
└─╴
# Create a `Data` instance that draws only dots
Scatter ← Data!(°⊸DrawDots 1 °⊸DrawLines 0 New)
# Create a `Data` instance that draws only lines
Line ← Data!(°⊸DrawLines 1 °⊸DrawDots 0 New)
# Stack one RGBA image atop another
Stack ← ⍜°⍉⊂ ↧⊸↥0÷⊃(+×𝄐⤚¬|+⊃×(×𝄐׬)|𝄐⍜∩¬×) ∩⍜°⍉°𝄈⊂
# Image ? PlotConfig
Background ← ∧↯⊙⬚1↙₄ PC!⊃Size BgColor
# Image ? PlotConfig
Axes ← (
-⌊÷2⟜⇡⊸PC~AxisWidth
×PC!⊃Min Ppu,
⊙(°⊟PC~Size,)°⊟⊞-
∩(°⊚⁅▽⊸≥0▽)⊓⤚>⊸<
∩⬚0↙⊙:°⊟◡⋅⋅PC~Size
C PC~AxisColor:⇌𝄈⊞↥)
# Image ? PlotConfig
Gridlines ← (
⊸PC!⊃Min Ppu
⊃(×PC~GridlineInterval⋅:
| ∩+⊙:∩⇡∩°⊟PC~Size,×)
∩(⌵⍜⊙+◿⤙÷2)⊙:°⊟
∩⌞≤÷2◡⋅⋅PC~GridlineWidth
C PC~GridlineColor:⇌𝄈⊞↥)
# Image ? PlotConfig
AxisNums ← ⬚∘(
PC!⊃GridlineInterval⊃Min⊃Max⊃Ppu Size°◌
⊓⌈⌊⤚∩⌞÷⊙⊓⤚∘⊙⇌
∩°⊟₆°⊟⍉⊟₆
∩⊓(
⤙×▽⊸≠0⍜-(⇡+1)
⍜×⁅𝄈ⁿ10⬚0⊗@.⇌°⋕ # Round values to precision of original factor
⍚(∧⌝↘⊟⊸¯⊟.⁅÷8⟜layout) PC~NumberSize°◌⊃°⋕(×𝄈-)
)∘
⊓⍚⍉⊙⊙⍚⇌⍚⇌
∩(♭₂∧◇(⬚0↥⬚0↙⁅⊟⊙:¯+⌊÷2⤚⧻)⊙⊙⊙[])
↥∩⇌∩⌞⬚0↙⇌PC~Size°◌⍉
C PC~NumberColor°◌)
# Legend ? Data PlotConfig
Legend ← (
⊙⊸¤
≡PC!Data!⊃⋅PlotLabelSize⊃Color⊃°◇°Label⋅PlotLabelColor
⍚(≡⊂⊓(C:≡⬚0↙⌊×3/2⊸⧻¬↯⟜⊚|C:layout))⤚∘
PC~PlotLabelSize⤙⊙⊓(⊢PC~Size)⊓0[]⤚≡◇(⧻⊢)
⟜⊃(⊕□𝄈▽°⊏⇌⊂◌∧(⨬⋅⊓-+₁⊓𝄈-⋅⊓1⊂: ⊙◡< ⊙+)⤙⊓:∘)⋅⋅∘
⟜(≡◇∧≡⬚0↙≡⊟¯⌊÷2⊸+⤚≡◇(⧻⊢)⍚/◇≡(⊂⊂)¤⍉↯4↯⟜⊚)
⟜(/(⊂⊂)⍉↯4𝄈↯0𝄈⊟⊡2△,)
∧⌝↘⊟⊸¯⌊÷2)
# Convolution kernels
# Rounding dot size up to an odd number gives
# the post-convolution array the correct shape
# Kernel ? Size
CircleKernel ↚ ⁿ4↧1×2√₁₆Gaussian⤙÷40⍜(÷2+1)⁅
# Kernel ? Size
XKernel ↚ (
0.3:¤↥⊸⇌⊞=.⊸⇡⍜(÷2+1)⁅×0.3
/↥≡↻♭₂⊞⊟.-÷2-1⟜⇡⍜(÷2+1)⌊×)
# Kernel ? Size
BoxKernel ↚ ¬↯⟜⊚⍜(÷2+1)⁅×0.3
# Non-rounded non-bound-checked pixel coordinates of points
# Coords ? Points PlotConfig
PointsToPx ↚ ≡⇌×⊙-∩¤PC!⊃Ppu Min:
# Image ? Data PlotConfig
Dots ← (
⤚⤙(⁅PointsToPx Data~Values)
⇌⬚0↙:°⊚ ▽⊸∊♭₂⤚⇡⇌PC~Size
CircleKernel Data~DotSize,
↘¯⟜↘⌊÷2△⟜FFTConvolve
↧1C Data~Color:)
# Image ? Data PlotConfig
Lines ← (
⤚⤙(⧈∘2≡/ℂPointsToPx Data~Values)
¤/ℂ°⍉⇡⇌PC~Size
⇌/↧≡/(⌵-×↥0↧1÷∩(/+×∩⊟∩°ℂ)⤙⤙◠:∩⌞-)
¬↥0↧1-÷10Data~LineWidth,×0.7
C Data~Color:)
# Plot a single series
# Image ? Data PlotConfig
SingleSeries ← /Stack[
⋅◌⍥◠Dots⊸Data~DrawDots
⍥◠Lines⊸Data~DrawLines]
# Bars ? Data PlotConfig
SingleBars ← (
⤚PC~YBounds𝄈⊙⊸Data~Values
⟜⊃(↥⊙↧°⊟)⊢
↥⊃(↥0-↥0⊢|⌵↧0-↧0⊣|¯↧0-↧0◌)
∩×,⊙(⊣PC~Ppu,)
⬚0≡⌝↘⁅:⬚0≡↯⊙1⁅≡⊟⊙PC~BarWidth⊙,
PC!⊃BarWidth Size,
⁅+÷2-⊙(⊸⊡1×÷⟜⇡⧻,⊢)
⇌⬚0↙⇌PC~Size:/↥⬚0≡≡⌝↘
C Data~Color:)
Bars ← (
⍜⬚0≡Data~Values∘
≡SingleBars⊙⊸¤
PC~BarWidth:
⁅×⇌-÷2-1⟜⇡⧻,
/Stack≡≡↻)
# Generate a `Data` instance that plots a given function
# Data ? XBounds
Func! ← (
⤚/-⊙PC!(Size New)
≡⊟⟜∵^0𝄈⊂⊣⤙⍜×/⍜-(⇡⁅)÷5÷⊙⊢
Data!(°⊸DrawDots0New))
# Colors from Desmos graphing calculator
# https://www.desmos.com/api/v1.7/docs/index.html#document-colors
DefaultColors ← ≡⊂÷255[
199_68_64
45_112_179
56_140_70
96_66_166
250_126_25
0_0_0
]1
# Infer values for a config field using
# a function that operates on input data
# ? FieldGetter InferenceFunction
Infer‼ ↚ ⟜(⍜(∩♭^0|⍜∩▽◌⤚∘⊸=∞):^1)
# Inherit defaults from a PlotConfig to
# the DataConfigs of some data
# ? DataConfigGetter PlotConfigGetter
Inherit‼ ↚ ⍜:Infer‼≡^0(≡⊙∘^1)
ColorAutoInfer ← (
≡⍜Data~Color⬚∞↙₄
⍜:Infer‼≡Data~Color⋅(⊏◿⧻,⇡⧻,DefaultColors)
)
PreChart ← (
⊙⟜Background
⟜(±⊸⧻▽>0⊸≡(⧻Data~Label)
⨬⋅⟜(°△[0⊙4]⊢PC~Size)(⤚(⍜⊙(⊣PC~Size)-⧻)⊸Legend))
⊙(>0⊸[∩⧻PC!⊃YLabel XLabel]⟜PC~Size
⍜⊙PC~Size-×+⟜+⌊⊸÷4 PC~AxisLabelSize,))
PostChart ← (
PC!⊃AxisLabelColor⊃AxisLabelSize⊃XLabel YLabel:
⊙∩⌞(∧⌝↘⊟⊸¯⌊÷4⟜layout)
⊡1△:⊙(⟜⧻,)∩⌞(⤸1⍉⊞×)⬚1↙₄
⊙𝄈⊂:⤸1⊙⇌∩(↻⌊÷2-⊃⊙⧻⬚0↙)
Stack𝄈⊂↙×1_¯1⇌:≡⊂⬚0↙⧻,)
# TODO: Document this more
# Create a line/scatter plot with a list of `Data`
# Each `Data~Values` should be a list of 2D coordinates,
# stored as a single rank 2 array.
# Use `⬚ fill` to provide a custom `PlotConfig`.
# Image ? Data
Plot ← (
ColorAutoInfer ⊙⍣°◌PC ♭₂
PC!Infer‼GridlineInterval(AutoRound÷5-⊃/↧/↥/◇⊂⍚Data~Values)
PC!Infer‼Min(-÷5GridlineInterval,/↧/◇⊂⍚Data~Values)
PC!Infer‼Max(+÷5GridlineInterval,/↥/◇⊂⍚Data~Values)
Inherit‼Data~DrawDots PC~DrawDots
Inherit‼Data~DotSize PC~DotSize
Inherit‼Data~DrawLines PC~DrawLines
Inherit‼Data~LineWidth PC~LineWidth
PreChart
/Stack⊸[
/Stack⇌≡SingleSeries⊙¤
⊙⟜(AxisNums|Axes|Gridlines)]
PostChart)
# TODO: Document this more
# Create a bar chart with a list of `Data`
# Each `Data~Values` should be a list of bar heights.
# Use `⬚ fill` to provide a custom `PlotConfig`.
# Image ? Data
BarChart ← (
⊙⍜PC~XBounds⋅1_1 ♭₂ ⊙⍣°◌PC
PC!Infer‼YBounds(⊟⊓⌞↧↥0∩(+⊸÷20)⊃/↧/↥♭⬚0≡Data~Values)
PC!Infer‼(⊣GridlineInterval|AutoRound÷5/-⋅⊸YBounds)
ColorAutoInfer
⊙⍜(⊢PC~GridlineInterval)⋅∞
PreChart
◠(⬚""↙⊙⊸PC~BarLabels/↥≡(⧻Data~Values)
⍚⌝↘⁅÷8⟜⍚layoutPC~BarLabelSize,
×÷⟜⇡⧻,⊢PC~Size,
⁅+÷2-⊙⊸⊡₁≡◇(⧻⊢),
≡⬚0↙⊢PC~Size,/↥⬚0≡◇≡⌝↘
C PC~BarLabelColor:)
⊙(⍜⊙(⊣PC~Size)-⧻,)
⊂⊙:/Stack ⊸[⊙::Bars⊙⟜(AxisNums|Axes|Gridlines)]
PostChart)
# TODO: Document this more
# Create a histogram with a single set of `Data`
# `Data~Values` should be a list of numbers.
# Use `⬚ fill` to provide a custom `PlotConfig`.
# Histogram bucket intervals can be specified
# through the x component of `GridlineInterval`
# in the `PlotConfig`.
# Image ? Data
Histogram ← (
⊙⍣°◌PC
PC!Infer‼(⊢GridlineInterval|AutoRound÷10-⊃/↧/↥Data~Values)
PC!Infer‼XBounds(⍜÷⁅⊢GridlineInterval,⊟⊃/↧/↥Data~Values)
⍜♭₂ColorAutoInfer
⍜Data~Values(
⊢PC!⊃GridlineInterval XBounds,
°⊂⍜÷(⍜-(⇡⁅+1)°⊟)
⬚0↙⧻:°⊚⊗1⊸⊞≥▽⊸≥⊙:)
⍜¤PreChart
⊙°⊸PC~BarWidth:⌈⊢×PC!⊃Ppu GridlineInterval,
PC!Infer‼YBounds(⊟0⁅×1.04/↥Data~Values)
PC!Infer‼(⊣GridlineInterval|AutoRound÷5/-⋅⊸YBounds)
/Stack⊸[⊙::SingleBars⊙⟜(AxisNums|Axes|Gridlines)]
PostChart)