All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.jetbrains.datalore.plot.base.stat.BaseStat.kt Maven / Gradle / Ivy

There is a newer version: 4.5.3-alpha1
Show newest version
/*
 * 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.base.stat

import jetbrains.datalore.plot.base.Aes
import jetbrains.datalore.plot.base.DataFrame
import jetbrains.datalore.plot.base.Stat
import jetbrains.datalore.plot.base.data.TransformVar

abstract class BaseStat(private val defaultMappings: Map, DataFrame.Variable>) : Stat {

    override fun hasDefaultMapping(aes: Aes<*>): Boolean {
        return defaultMappings.containsKey(aes)
    }

    override fun getDefaultMapping(aes: Aes<*>): DataFrame.Variable {
        if (defaultMappings.containsKey(aes)) {
            return defaultMappings[aes]!!
        }
        throw IllegalArgumentException("Stat " + this::class.simpleName + " has no default mapping for aes: " + aes)
    }

    protected fun hasRequiredValues(data: DataFrame, vararg aes: Aes<*>): Boolean {
        for (requiredAes in aes) {
            val variable = TransformVar.forAes(requiredAes)
            if (data.hasNoOrEmpty(variable)) {
                return false
            }
        }
        return true
    }

    protected fun withEmptyStatValues(): DataFrame {
        val newData = DataFrame.Builder()
        for (aes in Aes.values()) {
            if (hasDefaultMapping(aes)) {
                newData.put(getDefaultMapping(aes), emptyList())
            }
        }
        return newData.build()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy