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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
Replace SummaryCalculator by the SummaryStatUtil.
  • Loading branch information
ASmirnov-HORIS committed Jun 21, 2023
commit 756b626a054b991a09ca4231c15ceacf805f8bd9

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

package jetbrains.datalore.plot.base.stat

import jetbrains.datalore.base.gcommon.collect.Ordering
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 aggFunctionsMap: Map<Aes<*>, (SummaryCalculator) -> Double>
private val aggFunctionsMap: Map<Aes<*>, (List<Double>) -> Double>
) : BaseStat(DEF_MAPPING) {

override fun consumes(): List<Aes<*>> {
Expand Down Expand Up @@ -53,12 +54,13 @@ class SummaryStat(

val statValues: Map<Aes<*>, MutableList<Double>> = DEF_MAPPING.keys.associateWith { mutableListOf() }
for ((x, bin) in binnedData) {
val calc = SummaryCalculator(bin)
val sortedBin = Ordering.natural<Double>().sortedCopy(bin)
for (aes in statValues.keys) {
if (aes == Aes.X) {
statValues[aes]!!.add(x)
} else {
statValues[aes]!!.add(aggFunctionsMap.getOrElse(aes) { SummaryCalculator::nan }(calc))
val aggFunction = aggFunctionsMap.getOrElse(aes) { SummaryStatUtil.nan }
statValues[aes]!!.add(aggFunction(sortedBin))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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 kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.round

object SummaryStatUtil {
val nan: (List<Double>) -> Double = { Double.NaN }

fun count(sortedValues: List<Double>): Double {
return sortedValues.size.toDouble()
}

fun sum(sortedValues: List<Double>): Double {
return sortedValues.sum()
}

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

fun median(sortedValues: List<Double>): Double {
return quantile(0.5)(sortedValues)
}

fun min(sortedValues: List<Double>): Double {
return if (sortedValues.isEmpty()) {
Double.NaN
} else {
sortedValues.first()
}
}

fun max(sortedValues: List<Double>): Double {
return if (sortedValues.isEmpty()) {
Double.NaN
} else {
sortedValues.last()
}
}

fun q1(sortedValues: List<Double>): Double {
return quantile(0.25)(sortedValues)
}

fun q3(sortedValues: List<Double>): Double {
return quantile(0.75)(sortedValues)
}

fun quantile(p: Double): (List<Double>) -> Double {
return { sortedValues ->
if (sortedValues.isEmpty()) {
Double.NaN
} else if (sortedValues.size == 1) {
sortedValues.first()
} else {
val place = p * (sortedValues.size - 1)
val i = round(place)
if (place == i) {
sortedValues[place.toInt()]
} else {
(sortedValues[ceil(place).toInt()] + sortedValues[floor(place).toInt()]) / 2.0
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -408,22 +408,22 @@ object StatProto {
}

private fun configureSummaryStat(options: OptionsAccessor): SummaryStat {
fun getAggFunction(opts: OptionsAccessor, option: String, default: String): (SummaryCalculator) -> Double {
fun getAggFunction(opts: OptionsAccessor, option: String, default: String): (List<Double>) -> Double {
return if (opts.isNumber(option)) {
val p = opts.getDouble(option)!!
{ calc -> calc.quantile(p) }
SummaryStatUtil.quantile(p)
} else {
opts.getStringDef(option, default).let {
when (it.lowercase()) {
"nan" -> SummaryCalculator::nan
"count" -> SummaryCalculator::count
"sum" -> SummaryCalculator::sum
"mean" -> SummaryCalculator::mean
"median" -> SummaryCalculator::median
"min" -> SummaryCalculator::min
"max" -> SummaryCalculator::max
"q1" -> SummaryCalculator::q1
"q3" -> SummaryCalculator::q3
"nan" -> SummaryStatUtil.nan
"count" -> SummaryStatUtil::count
"sum" -> SummaryStatUtil::sum
"mean" -> SummaryStatUtil::mean
"median" -> SummaryStatUtil::median
"min" -> SummaryStatUtil::min
"max" -> SummaryStatUtil::max
"q1" -> SummaryStatUtil::q1
"q3" -> SummaryStatUtil::q3
else -> throw IllegalArgumentException(
"Unsupported function name: '$it'\n" +
"Use one of: nan, count, sum, mean, median, min, max, q1, q3."
Expand All @@ -439,7 +439,7 @@ object StatProto {
Aes.YMAX to getAggFunction(options, Summary.FUN_MAX, SummaryStat.DEF_MAX_AGG_FUN)
)

val additionalAggFunctions: MutableMap<Aes<*>, (SummaryCalculator) -> Double> = mutableMapOf()
val additionalAggFunctions: MutableMap<Aes<*>, (List<Double>) -> Double> = mutableMapOf()
val funMap: Map<String, Any> = if (options.hasOwn(Summary.FUN_MAP)) {
options.getMap(Summary.FUN_MAP)
} else {
Expand Down