Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECDF Stat #832

Merged
merged 17 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Basic version of the eCDF stat.
  • Loading branch information
ASmirnov-HORIS committed Jul 26, 2023
commit be14e42a67c44c6822d97fdea66a24343f30cbfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2023. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/

package demo.plot.common.model.plotConfig

import demo.plot.common.data.Iris
import demoAndTestShared.parsePlotSpec

class ECDF {
fun plotSpecList(): List<MutableMap<String, Any>> {
return listOf(
basic(),
)
}

private fun basic(): MutableMap<String, Any> {
val spec = """
{
'kind': 'plot',
'mapping': {
'x': 'sepal length (cm)'
},
'ggtitle': {
'text': 'Basic demo'
},
'layers': [
{
'geom': 'step',
'stat': 'ecdf'
}
]
}
""".trimIndent()

val plotSpec = HashMap(parsePlotSpec(spec))
plotSpec["data"] = Iris.df
return plotSpec

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2023. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/

package demo.plot.batik.plotConfig

import demo.plot.common.model.plotConfig.ECDF
import demo.common.batik.demoUtils.PlotSpecsDemoWindowBatik

fun main() {
with(ECDF()) {
PlotSpecsDemoWindowBatik(
"ECDF plot",
plotSpecList()
).open()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2023. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/

package org.jetbrains.letsPlot.core.plot.base.stat

import org.jetbrains.letsPlot.core.plot.base.Aes
import org.jetbrains.letsPlot.core.plot.base.DataFrame
import org.jetbrains.letsPlot.core.plot.base.StatContext
import org.jetbrains.letsPlot.core.plot.base.data.TransformVar
import org.jetbrains.letsPlot.core.plot.base.stat.math3.UniformDistribution

class ECDFStat : BaseStat(DEF_MAPPING) {

override fun consumes(): List<Aes<*>> {
return listOf(Aes.X)
}

override fun apply(data: DataFrame, statCtx: StatContext, messageConsumer: (s: String) -> Unit): DataFrame {
if (!hasRequiredValues(data, Aes.X)) {
return withEmptyStatValues()
}

val statData = buildStat(data.getNumeric(TransformVar.X))

return DataFrame.Builder()
.putNumeric(Stats.X, statData.getValue(Stats.X))
.putNumeric(Stats.Y, statData.getValue(Stats.Y))
.build()
}

private fun buildStat(
xs: List<Double?>
): MutableMap<DataFrame.Variable, List<Double>> {
val statX = xs.filter { it?.isFinite() ?: false }.map { it!! }.sorted()
val t = (1..statX.size).map { (it - 0.5) / statX.size }
val dist = UniformDistribution(0.0, 1.0)
val statY = t.map { dist.inverseCumulativeProbability(it) }

return mutableMapOf(
Stats.X to statX,
Stats.Y to statY
)
}

companion object {
private val DEF_MAPPING: Map<Aes<*>, DataFrame.Variable> = mapOf(
Aes.X to Stats.X,
Aes.Y to Stats.Y
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ enum class StatKind {
QQ2,
QQ_LINE,
QQ2_LINE,
ECDF,
SUMMARY,
SUMMARYBIN;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ object StatProto {

StatKind.QQ2_LINE -> return configureQQ2LineStat(options)

StatKind.ECDF -> return ECDFStat()

StatKind.SUMMARY -> return configureSummaryStat(options)

StatKind.SUMMARYBIN -> return configureSummaryBinStat(options)
Expand Down