forked from JetBrains/lets-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatProto.kt
341 lines (297 loc) · 12.7 KB
/
StatProto.kt
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
/*
* Copyright (c) 2019. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package jetbrains.datalore.plot.config
import jetbrains.datalore.base.gcommon.base.Preconditions.checkArgument
import jetbrains.datalore.plot.base.Stat
import jetbrains.datalore.plot.base.stat.*
import jetbrains.datalore.plot.base.stat.BoxplotStat.Companion.P_COEF
import jetbrains.datalore.plot.base.stat.BoxplotStat.Companion.P_VARWIDTH
class StatProto {
internal fun defaultOptions(statName: String): Map<String, Any> {
checkArgument(DEFAULTS.containsKey(statName), "Unknown stat name: '$statName'")
return DEFAULTS[statName]!!
}
internal fun createStat(statKind: StatKind, options: Map<*, *>): Stat {
// ToDo: pass 'option accessor'
// ToDo: get rid of 'if'-s: everything must be present in the 'defaults'
// ToDo: get rid of XXXStatBuilder
// ToDo: see BIN2D
when (statKind) {
StatKind.IDENTITY -> return Stats.IDENTITY
StatKind.COUNT -> return Stats.count()
StatKind.BIN -> {
val binStat = Stats.bin()
if (options.containsKey("bins")) {
binStat.binCount((options["bins"] as Number).toInt())
}
if (options.containsKey("binwidth")) {
binStat.binWidth((options["binwidth"] as Number).toDouble())
}
if (options.containsKey("center")) {
binStat.center((options["center"] as Number).toDouble())
}
if (options.containsKey("boundary")) {
binStat.boundary((options["boundary"] as Number).toDouble())
}
return binStat.build()
}
StatKind.BIN2D -> {
val opts = OptionsAccessor.over(options)
val (binCountX, binCountY) = opts.getNumPair(Bin2dStat.P_BINS)
val (binWidthX, binWidthY) = opts.getNumQPair(Bin2dStat.P_BINWIDTH)
return Bin2dStat(
binCountX = binCountX.toInt(),
binCountY = binCountY.toInt(),
binWidthX = binWidthX?.toDouble(),
binWidthY = binWidthY?.toDouble(),
drop = opts.getBoolean(Bin2dStat.P_BINWIDTH, def = Bin2dStat.DEF_DROP)
)
}
StatKind.CONTOUR -> {
val contourStat = Stats.contour()
if (options.containsKey("bins")) {
contourStat.binCount((options["bins"] as Number).toInt())
}
if (options.containsKey("binwidth")) {
contourStat.binWidth((options["binwidth"] as Number).toDouble())
}
return contourStat.build()
}
StatKind.CONTOURF -> {
val contourfStat = Stats.contourf()
if (options.containsKey("bins")) {
contourfStat.binCount((options["bins"] as Number).toInt())
}
if (options.containsKey("binwidth")) {
contourfStat.binWidth((options["binwidth"] as Number).toDouble())
}
return contourfStat.build()
}
StatKind.SMOOTH -> return configureSmoothStat(options)
StatKind.CORR -> return configureCorrStat(options)
StatKind.BOXPLOT -> {
val boxplotStat = Stats.boxplot()
val opts = OptionsAccessor.over(options)
boxplotStat.setComputeWidth(opts.getBoolean(P_VARWIDTH))
boxplotStat.setWhiskerIQRRatio(opts.getDouble(P_COEF)!!)
return boxplotStat
}
StatKind.DENSITY -> {
val densityStat = Stats.density()
if (options.containsKey("kernel")) {
val method = options["kernel"] as String
densityStat.setKernel(DensityStatUtil.toKernel(method))
}
if (options.containsKey("bw")) {
val bw = options["bw"]
if (bw is Number) {
densityStat.setBandWidth(bw.toDouble())
} else {
densityStat.setBandWidthMethod(DensityStatUtil.toBandWidthMethod(bw as String))
}
}
if (options.containsKey("n")) {
densityStat.setN((options["n"] as Number).toInt())
}
if (options.containsKey("adjust")) {
densityStat.setAdjust((options["adjust"] as Number).toDouble())
}
return densityStat
}
StatKind.DENSITY2D -> {
val density2dStat = Stats.density2d()
return configureDensity2dStat(density2dStat, options)
}
StatKind.DENSITY2DF -> {
val density2dfStat = Stats.density2df()
return configureDensity2dStat(density2dfStat, options)
}
else -> throw IllegalArgumentException("Unknown stat: '$statKind'")
}
}
private fun configureSmoothStat(options: Map<*, *>): Stat {
// Params:
// method - smoothing method: lm, glm, gam, loess, rlm
// n (80) - number of points to evaluate smoother at
// se (TRUE ) - display confidence interval around smooth?
// level (0.95) - level of confidence interval to use
// deg ( >= 1 ) - degree of polynomial for regression
// seed - random seed for LOESS sampling
// max_n (1000) - maximum points in DF for LOESS
val stat = Stats.smooth()
if (options.containsKey("n")) {
stat.smootherPointCount = (options["n"] as Number).toInt()
}
if (options.containsKey("method")) {
val method = options["method"] as String
stat.smoothingMethod = when (method) {
"lm" -> SmoothStat.Method.LM
"loess", "lowess" -> SmoothStat.Method.LOESS
"glm" -> SmoothStat.Method.GLM
"gam" -> SmoothStat.Method.GAM
"rlm" -> SmoothStat.Method.RLM
else -> throw IllegalArgumentException("Unsupported smoother method: $method")
}
}
if (options.containsKey("level")) {
stat.confidenceLevel = (options["level"] as Number).toDouble()
}
if (options.containsKey("se")) {
val se = options["se"]
if (se is Boolean) {
stat.isDisplayConfidenceInterval = se
}
}
options["span"]?.let { stat.span = it.asDouble() }
options["deg"]?.let { stat.deg = it.asInt() }
options["seed"]?.let { stat.seed = it.asLong() }
options["max_n"]?.let { stat.loessCriticalSize = it.asInt() }
return stat
}
private fun configureCorrStat(options: Map<*, *>): Stat {
val stat = Stats.corr()
if (options.containsKey("method")) {
val method = options["method"] as String
stat.correlationMethod = when (method) {
"pearson" -> CorrelationStat.Method.PEARSON
else -> throw IllegalArgumentException("Unsupported correlation method: $method")
}
}
if (options.containsKey("type")) {
val type = options["type"] as String
stat.type = when (type) {
"full" -> CorrelationStat.Type.FULL
"upper" -> CorrelationStat.Type.UPPER
"lower" -> CorrelationStat.Type.LOWER
else -> throw IllegalArgumentException("Unsupported matrix type: $type. Only 'full', 'upper' and 'lower' are supported.")
}
}
stat.fillDiagonal = OptionsAccessor.over(options)
.getBoolean(CorrelationStat.FILL_DIAGONAL, def = CorrelationStat.DEF_FILL_DIAGONAL)
return stat
}
private fun configureDensity2dStat(stat: AbstractDensity2dStat, options: Map<*, *>): Stat {
if (options.containsKey("kernel")) {
val method = options["kernel"] as String
stat.setKernel(DensityStatUtil.toKernel(method))
}
if (options.containsKey("bw")) {
val bw = options["bw"]
if (bw is List<*>) {
for (i in bw.indices) {
val v = bw[i]
if (i == 0) {
stat.setBandWidthX((v as Number).toDouble())
} else {
stat.setBandWidthY((v as Number).toDouble())
break
}
}
} else if (bw is Number) {
stat.setBandWidthX(bw.toDouble())
stat.setBandWidthY(bw.toDouble())
} else if (bw is String) {
stat.bandWidthMethod = DensityStatUtil.toBandWidthMethod(bw)
}
}
if (options.containsKey("n")) {
val n = options["n"]
if (n is List<*>) {
for (i in n.indices) {
val v = n[i]
if (i == 0) {
stat.nx = (v as Number).toInt()
} else {
stat.ny = (v as Number).toInt()
break
}
}
} else if (n is Number) {
stat.nx = n.toInt()
stat.ny = n.toInt()
}
}
if (options.containsKey("adjust")) {
stat.adjust = (options["adjust"] as Number).toDouble()
}
if (options.containsKey("contour")) {
stat.isContour = options["contour"] as Boolean
}
if (options.containsKey("bins")) {
stat.setBinCount((options["bins"] as Number).toInt())
}
if (options.containsKey("binwidth")) {
stat.setBinWidth((options["binwidth"] as Number).toDouble())
}
return stat
}
private fun Any?.asDouble() = (this as Number).toDouble()
private fun Any?.asInt() = (this as Number).toInt()
private fun Any?.asLong() = (this as Number).toLong()
companion object {
private val DEFAULTS = HashMap<String, Map<String, Any>>()
// ToDo: add default geom
init {
DEFAULTS["identity"] = emptyMap()
DEFAULTS["count"] = emptyMap()
DEFAULTS["bin"] = createBinDefaults()
DEFAULTS["bin2d"] = createBin2dDefaults()
DEFAULTS["smooth"] = emptyMap()
DEFAULTS["contour"] = createContourDefaults()
DEFAULTS["contourf"] = createContourfDefaults()
DEFAULTS["boxplot"] = createBoxplotDefaults()
DEFAULTS["density"] = createDensityDefaults()
DEFAULTS["density2d"] = createDensity2dDefaults()
DEFAULTS["density2df"] = createDensity2dDefaults()
DEFAULTS["corr"] = emptyMap()
}
private fun createBinDefaults(): Map<String, Any> {
return mapOf(
"bins" to BinStatBuilder.DEF_BIN_COUNT
)
}
private fun createBin2dDefaults(): Map<String, Any> {
return mapOf(
Bin2dStat.P_BINS to listOf(Bin2dStat.DEF_BINS, Bin2dStat.DEF_BINS),
Bin2dStat.P_BINWIDTH to listOf(Bin2dStat.DEF_BINWIDTH, Bin2dStat.DEF_BINWIDTH),
Bin2dStat.P_DROP to Bin2dStat.DEF_DROP
)
}
private fun createContourDefaults(): Map<String, Any> {
return mapOf(
"bins" to ContourStatBuilder.DEF_BIN_COUNT
)
}
private fun createContourfDefaults(): Map<String, Any> {
return mapOf(
"bins" to ContourfStatBuilder.DEF_BIN_COUNT
)
}
private fun createBoxplotDefaults(): Map<String, Any> {
return mapOf(
P_COEF to BoxplotStat.DEF_WHISKER_IQR_RATIO,
P_VARWIDTH to BoxplotStat.DEF_COMPUTE_WIDTH
)
}
private fun createDensityDefaults(): Map<String, Any> {
return mapOf(
"n" to DensityStat.DEF_N,
"kernel" to DensityStat.DEF_KERNEL,
"bw" to DensityStat.DEF_BW,
"adjust" to DensityStat.DEF_ADJUST
)
}
private fun createDensity2dDefaults(): Map<String, Any> {
return mapOf(
"n" to AbstractDensity2dStat.DEF_N,
"kernel" to AbstractDensity2dStat.DEF_KERNEL,
"bw" to AbstractDensity2dStat.DEF_BW,
"adjust" to AbstractDensity2dStat.DEF_ADJUST,
"contour" to AbstractDensity2dStat.DEF_CONTOUR,
"bins" to AbstractDensity2dStat.DEF_BIN_COUNT
)
}
}
}