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

Summary stat #803

Merged
merged 26 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7886cbc
First implementation of the summary stat.
ASmirnov-HORIS Jun 16, 2023
d95a062
Update API for the stat_summary() function.
ASmirnov-HORIS Jun 20, 2023
b28bf7f
Small refactor in SummaryStat.
ASmirnov-HORIS Jun 21, 2023
6ce963b
Remove extra enum class from the SummaryStatUtil.
ASmirnov-HORIS Jun 21, 2023
b7a8f33
Remove SummaryStatUtil.
ASmirnov-HORIS Jun 21, 2023
05345ee
Use references instead of lambdas for the SummaryCalculator.
ASmirnov-HORIS Jun 21, 2023
756b626
Replace SummaryCalculator by the SummaryStatUtil.
ASmirnov-HORIS Jun 21, 2023
3b2bca7
Refactor functions in SummaryStatUtil.
ASmirnov-HORIS Jun 22, 2023
1f03da4
Refactor summary stat options in StatProto.
ASmirnov-HORIS Jun 22, 2023
baccb49
Fix statData emptiness case in the SummaryStat.
ASmirnov-HORIS Jun 22, 2023
c9e154c
Further code refactoring.
ASmirnov-HORIS Jun 22, 2023
0082f74
Small fixes.
ASmirnov-HORIS Jun 22, 2023
cbabbe3
Add new stat variables and use them in the SummaryStat.
ASmirnov-HORIS Jun 23, 2023
7fea03b
Add prefix to min/max stats in stat_summary().
ASmirnov-HORIS Jun 23, 2023
0ecc6ec
Change API of the summary_stat() - add 'quantiles' parameter.
ASmirnov-HORIS Jun 26, 2023
722b43a
Tiny refactor in SummaryStat and AggregateFunctions.
ASmirnov-HORIS Jun 27, 2023
e30b93b
Use AggregateFunctions in the FiveNumberSummary.
ASmirnov-HORIS Jun 27, 2023
f076938
Add tests for AggregateFunctions.
ASmirnov-HORIS Jun 27, 2023
2313872
Replace parameter fun_map by usual aesthetics list for the stat_summa…
ASmirnov-HORIS Jun 29, 2023
4bba413
Small fixes in code for summary stat.
ASmirnov-HORIS Jun 29, 2023
9cfeea5
Merge branch 'master' into summary-stats
ASmirnov-HORIS Jun 29, 2023
4586451
Add getMapping() method to the Flipped stat context.
ASmirnov-HORIS Jun 29, 2023
9dcdd79
Add docstrings to the stat_summary() function.
ASmirnov-HORIS Jun 29, 2023
37526eb
Add demo notebook for stat_summary().
ASmirnov-HORIS Jun 29, 2023
0a1d636
Mention stat_summary() in the future_changes.
ASmirnov-HORIS Jun 29, 2023
a392ba1
Refactor StatContext.
ASmirnov-HORIS Jun 30, 2023
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
First implementation of the summary stat.
  • Loading branch information
ASmirnov-HORIS committed Jun 16, 2023
commit 7886cbc392658546faf7f220fccdaf52e986ba7f
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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 jetbrains.datalore.plot.base.stat

import jetbrains.datalore.plot.base.Aes
import jetbrains.datalore.plot.base.DataFrame
import jetbrains.datalore.plot.base.StatContext
import jetbrains.datalore.plot.base.data.TransformVar
import jetbrains.datalore.plot.common.data.SeriesUtil

class SummaryStat(
private val yAgg: (SummaryStatUtil.SummaryCalculator) -> Double,
private val minAgg: (SummaryStatUtil.SummaryCalculator) -> Double,
private val maxAgg: (SummaryStatUtil.SummaryCalculator) -> Double,
private val middleAgg: (SummaryStatUtil.SummaryCalculator) -> Double,
private val lowerAgg: (SummaryStatUtil.SummaryCalculator) -> Double,
private val upperAgg: (SummaryStatUtil.SummaryCalculator) -> Double
) : BaseStat(DEF_MAPPING) {

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

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

val ys = data.getNumeric(TransformVar.Y)
val xs = if (data.has(TransformVar.X)) {
data.getNumeric(TransformVar.X)
} else {
List(ys.size) { 0.0 }
}

val statData = buildStat(xs, ys)

val builder = DataFrame.Builder()
for ((variable, series) in statData) {
builder.putNumeric(variable, series)
}
return builder.build()
}

private fun buildStat(
xs: List<Double?>,
ys: List<Double?>
): MutableMap<DataFrame.Variable, List<Double>> {
val xyPairs = SeriesUtil.filterFinite(xs, ys)
.let { (xs, ys) -> xs zip ys }
if (xyPairs.isEmpty()) {
return mutableMapOf()
}

val binnedData: MutableMap<Double, MutableList<Double>> = HashMap()
for ((x, y) in xyPairs) {
binnedData.getOrPut(x) { ArrayList() }.add(y)
}

val statX = ArrayList<Double>()
val statY = ArrayList<Double>()
val statMin = ArrayList<Double>()
val statMax = ArrayList<Double>()
val statMiddle = ArrayList<Double>()
val statLower = ArrayList<Double>()
val statUpper = ArrayList<Double>()

for ((x, bin) in binnedData) {
val calc = SummaryStatUtil.SummaryCalculator(bin)
statX.add(x)
statY.add(yAgg(calc))
statMin.add(minAgg(calc))
statMax.add(maxAgg(calc))
statMiddle.add(middleAgg(calc))
statLower.add(lowerAgg(calc))
statUpper.add(upperAgg(calc))
}

return mutableMapOf(
Stats.X to statX,
Stats.Y to statY,
Stats.Y_MIN to statMin,
Stats.Y_MAX to statMax,
Stats.MIDDLE to statMiddle,
Stats.LOWER to statLower,
Stats.UPPER to statUpper,
)
}

companion object {
const val DEF_Y_AGG_FUN = "mean"
const val DEF_MIN_AGG_FUN = "min"
const val DEF_MAX_AGG_FUN = "max"
const val DEF_MIDDLE_AGG_FUN = "nan"
const val DEF_LOWER_AGG_FUN = "nan"
const val DEF_UPPER_AGG_FUN = "nan"

private val DEF_MAPPING: Map<Aes<*>, DataFrame.Variable> = mapOf(
Aes.X to Stats.X,
Aes.Y to Stats.Y,
Aes.YMIN to Stats.Y_MIN,
Aes.YMAX to Stats.Y_MAX,
Aes.MIDDLE to Stats.MIDDLE,
Aes.LOWER to Stats.LOWER,
Aes.UPPER to Stats.UPPER
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* 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 jetbrains.datalore.plot.base.stat

import jetbrains.datalore.base.gcommon.collect.Ordering
import kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.round

object SummaryStatUtil {
fun getStandardAggFun(aggFunName: AggFun): (SummaryCalculator) -> Double {
return when (aggFunName) {
AggFun.NAN -> { calc -> calc.nan() }
AggFun.COUNT -> { calc -> calc.count() }
AggFun.SUM -> { calc -> calc.sum() }
AggFun.MEAN -> { calc -> calc.mean() }
AggFun.MEDIAN -> { calc -> calc.median() }
AggFun.MIN -> { calc -> calc.min() }
AggFun.MAX -> { calc -> calc.max() }
AggFun.Q1 -> { calc -> calc.q1() }
AggFun.Q3 -> { calc -> calc.q3() }
}
}

fun getQuantileAggFun(p: Double): (SummaryCalculator) -> Double {
return { calc -> calc.quantile(p) }
}

class SummaryCalculator(values: List<Double>) {
private val sortedValues: List<Double> = Ordering.natural<Double>().sortedCopy(values)

private var count: Double? = null
private var sum: Double? = null
private var mean: Double? = null
private var median: Double? = null
private var min: Double? = null
private var max: Double? = null
private var q1: Double? = null
private var q3: Double? = null

fun nan(): Double {
return Double.NaN
}

fun count(): Double {
if (count == null) {
count = sortedValues.size.toDouble()
}
return count!!
}

fun sum(): Double {
if (sum == null) {
sum = sortedValues.sum()
}
return sum!!
}

fun mean(): Double {
if (mean == null) {
mean = if (sortedValues.isEmpty()) {
Double.NaN
} else if (sortedValues.size == 1) {
sortedValues.first()
} else {
sum() / sortedValues.size
}
}
return mean!!
}

fun median(): Double {
if (median == null) {
median = quantile(0.5)
}
return median!!
}

fun min(): Double {
if (min == null) {
min = if (sortedValues.isEmpty()) {
Double.NaN
} else {
sortedValues.first()
}
}
return min!!
}

fun max(): Double {
if (max == null) {
max = if (sortedValues.isEmpty()) {
Double.NaN
} else {
sortedValues.last()
}
}
return max!!
}

fun q1(): Double {
if (q1 == null) {
q1 = quantile(0.25)
}
return q1!!
}

fun q3(): Double {
if (q3 == null) {
q3 = quantile(0.75)
}
return q3!!
}

fun quantile(p: Double): Double {
if (sortedValues.isEmpty()) {
return Double.NaN
}
if (sortedValues.size == 1) {
return sortedValues.first()
}
val place = p * (sortedValues.size - 1)
val i = round(place)
return if (place == i) {
sortedValues[place.toInt()]
} else {
(sortedValues[ceil(place).toInt()] + sortedValues[floor(place).toInt()]) / 2.0
}
}
}

enum class AggFun {
NAN,
COUNT,
SUM,
MEAN,
MEDIAN,
MIN,
MAX,
Q1,
Q3,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,15 @@ object Option {
const val DISTRIBUTION_PARAMETERS = "dparams"
const val LINE_QUANTILES = "quantiles"
}

object Summary {
const val FUN = "fun"
const val FUN_MIN = "fun_min"
const val FUN_MAX = "fun_max"
const val FUN_MIDDLE = "fun_middle"
const val FUN_LOWER = "fun_lower"
const val FUN_UPPER = "fun_upper"
}
}

object Pos {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ open class OptionsAccessor(
return options[option] != null
}

fun isNumber(option: String): Boolean {
val v = get(option) ?: return false
return v is Number
}

operator fun get(option: String): Any? {
return if (hasOwn(option)) {
options[option]
Expand All @@ -51,6 +56,10 @@ open class OptionsAccessor(
return get(option)?.toString()
}

fun getStringDef(option: String, def: String): String {
return getString(option) ?: def
}

fun getStringSafe(option: String): String {
return getString(option)
?: throw IllegalArgumentException("Can't get string value: option '$option' is not present.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ enum class StatKind {
QQ,
QQ2,
QQ_LINE,
QQ2_LINE;
QQ2_LINE,
SUMMARY;


companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import jetbrains.datalore.plot.config.Option.Stat.DensityRidges
import jetbrains.datalore.plot.config.Option.Stat.YDensity
import jetbrains.datalore.plot.config.Option.Stat.QQ
import jetbrains.datalore.plot.config.Option.Stat.QQLine
import jetbrains.datalore.plot.config.Option.Stat.Summary

object StatProto {

Expand Down Expand Up @@ -115,6 +116,8 @@ object StatProto {

StatKind.QQ2_LINE -> return configureQQ2LineStat(options)

StatKind.SUMMARY -> return configureSummaryStat(options)

else -> throw IllegalArgumentException("Unknown stat: '$statKind'")
}
}
Expand Down Expand Up @@ -401,4 +404,40 @@ object StatProto {

return Stats.qq2line(lineQuantiles ?: QQLineStat.DEF_LINE_QUANTILES)
}

private fun configureSummaryStat(options: OptionsAccessor): SummaryStat {
val getAggFun: (String, String) -> (SummaryStatUtil.SummaryCalculator) -> Double = { option, default ->
if (options.isNumber(option)) {
SummaryStatUtil.getQuantileAggFun(options.getDouble(option)!!)
} else {
val aggFunName = options.getStringDef(option, default).let {
when (it.lowercase()) {
"nan" -> SummaryStatUtil.AggFun.NAN
"count" -> SummaryStatUtil.AggFun.COUNT
"sum" -> SummaryStatUtil.AggFun.SUM
"mean" -> SummaryStatUtil.AggFun.MEAN
"median" -> SummaryStatUtil.AggFun.MEDIAN
"min" -> SummaryStatUtil.AggFun.MIN
"max" -> SummaryStatUtil.AggFun.MAX
"q1" -> SummaryStatUtil.AggFun.Q1
"q3" -> SummaryStatUtil.AggFun.Q3
else -> throw IllegalArgumentException(
"Unsupported function name: '$it'\n" +
"Use one of: nan, count, sum, mean, median, min, max, q1, q3."
)
}
}
SummaryStatUtil.getStandardAggFun(aggFunName)
}
}

val yAgg = getAggFun(Summary.FUN, SummaryStat.DEF_Y_AGG_FUN)
val minAgg = getAggFun(Summary.FUN_MIN, SummaryStat.DEF_MIN_AGG_FUN)
val maxAgg = getAggFun(Summary.FUN_MAX, SummaryStat.DEF_MAX_AGG_FUN)
val middleAgg = getAggFun(Summary.FUN_MIDDLE, SummaryStat.DEF_MIDDLE_AGG_FUN)
val lowerAgg = getAggFun(Summary.FUN_LOWER, SummaryStat.DEF_LOWER_AGG_FUN)
val upperAgg = getAggFun(Summary.FUN_UPPER, SummaryStat.DEF_UPPER_AGG_FUN)

return SummaryStat(yAgg, minAgg, maxAgg, middleAgg, lowerAgg, upperAgg)
}
}
Loading