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
Prev Previous commit
Next Next commit
Add 'pad' parameter to the StepGeom (for the eCDF).
  • Loading branch information
ASmirnov-HORIS committed Jul 27, 2023
commit 4159166719d9e14453f0660532206213f43ba780
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ECDF {
return listOf(
basic(),
withInterpolation(),
withGrouping()
)
}

Expand All @@ -29,7 +30,8 @@ class ECDF {
'layers': [
{
'geom': 'step',
'stat': 'ecdf'
'stat': 'ecdf',
'pad': true
}
]
}
Expand All @@ -55,7 +57,36 @@ class ECDF {
{
'geom': 'step',
'stat': 'ecdf',
'n': 10
'n': 10,
'pad': true,
'direction': 'vh'
}
]
}
""".trimIndent()

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

}

private fun withGrouping(): MutableMap<String, Any> {
val spec = """
{
'kind': 'plot',
'mapping': {
'x': 'sepal length (cm)',
'color': 'target'
},
'ggtitle': {
'text': 'With additional grouping'
},
'layers': [
{
'geom': 'step',
'stat': 'ecdf',
'pad': true
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@

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

import org.jetbrains.letsPlot.commons.geometry.DoubleVector
import org.jetbrains.letsPlot.core.plot.base.*
import org.jetbrains.letsPlot.core.plot.base.geom.util.GeomHelper
import org.jetbrains.letsPlot.core.plot.base.geom.util.GeomUtil
import org.jetbrains.letsPlot.core.plot.base.geom.util.LinesHelper
import org.jetbrains.letsPlot.core.plot.base.geom.util.TargetCollectorHelper
import org.jetbrains.letsPlot.core.plot.base.render.SvgRoot
import org.jetbrains.letsPlot.datamodel.svg.dom.SvgLineElement

class StepGeom : LineGeom() {
private var myDirection = DEF_DIRECTION
var padded = DEF_PADDED

fun setDirection(dir: String) {
myDirection = Direction.toDirection(dir)
Expand All @@ -33,10 +37,63 @@ class StepGeom : LineGeom() {

root.appendNodes(linePaths)

if (padded) {
GeomUtil.withDefined(dataPoints, Aes.X, Aes.Y).groupBy(DataPointAesthetics::group).forEach { (_, groupPoints) ->
for (line in getPads(groupPoints, pos, coord, ctx)) {
root.add(line)
}
}
}

val targetCollectorHelper = TargetCollectorHelper(GeomKind.STEP, ctx)
targetCollectorHelper.addPaths(pathDataList)
}

private fun getPads(
dataPoints: Iterable<DataPointAesthetics>,
pos: PositionAdjustment,
coord: CoordinateSystem,
ctx: GeomContext
): List<SvgLineElement> {
val pads: MutableList<SvgLineElement> = mutableListOf()

val helper = GeomHelper(pos, coord, ctx).createSvgElementHelper()
val viewPort = overallAesBounds(ctx)
val definedPoints = GeomUtil.withDefined(dataPoints, Aes.X, Aes.Y)
if (!definedPoints.any()) {
return pads
}

val startPoint = definedPoints.first()
val startX = startPoint.x()!!
val startY = startPoint.y()!!
val endPoint = definedPoints.last()
val endX = endPoint.x()!!
val endY = endPoint.y()!!

if (startY < 0 || endY > 1) {
return pads
}

helper.createLine(
DoubleVector(viewPort.left, 0.0),
DoubleVector(startX, 0.0),
startPoint
)?.let { pads.add(it) }
helper.createLine(
DoubleVector(startX, 0.0),
DoubleVector(startX, startY),
startPoint
)?.let { pads.add(it) }
helper.createLine(
DoubleVector(endX, 1.0),
DoubleVector(viewPort.right, 1.0),
endPoint
)?.let { pads.add(it) }

return pads
}

enum class Direction {
HV, VH;

Expand All @@ -55,6 +112,7 @@ class StepGeom : LineGeom() {
companion object {
// default
val DEF_DIRECTION = Direction.HV
const val DEF_PADDED = false

const val HANDLES_GROUPS = LineGeom.HANDLES_GROUPS
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ internal object GeomProviderFactory {
if (layerConfig.hasOwn(Option.Geom.Step.DIRECTION)) {
geom.setDirection(layerConfig.getString(Option.Geom.Step.DIRECTION)!!)
}
if (layerConfig.hasOwn(Option.Geom.Step.PADDED)) {
geom.padded = layerConfig.getBoolean(Option.Geom.Step.PADDED, StepGeom.DEF_PADDED)
}
geom
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ object Option {

object Step {
const val DIRECTION = "direction"
const val PADDED = "pad"
}

object Segment {
Expand Down