commonMain.jetbrains.datalore.plot.config.SamplingConfig.kt Maven / Gradle / Ivy
/*
* 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.plot.builder.sampling.Sampling
internal object SamplingConfig {
fun create(sampling: Any): List {
// sampling is specified per geom layer:
// xxx_geom(..., sampling=sampling_random(100,seed=3)...)
// or
// xxx_geom(..., sampling=sampling_random(100,seed=3) + sampling_pick(10)...)
if (sampling is MutableMap<*, *> && ConfigUtil.isFeatureList(sampling)) {
@Suppress("UNCHECKED_CAST")
val samplingList = ConfigUtil.featuresInFeatureList(sampling as MutableMap)
val result = ArrayList()
for (o in samplingList) {
result.add(createOne(o))
}
return result
}
return listOf(createOne(sampling))
}
private fun createOne(sampling: Any): Sampling {
if (sampling is Map<*, *>) {
@Suppress("UNCHECKED_CAST")
return SamplingProto.createSampling(
ConfigUtil.featureName(sampling), sampling as Map
)
} else if (sampling is String) {
return SamplingProto.createSampling(sampling, emptyMap())
}
throw IllegalArgumentException("Incorrect sampling specification type: '${sampling::class.simpleName}'")
}
}