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
Prev Previous commit
Next Next commit
Add new stat variables and use them in the SummaryStat.
  • Loading branch information
ASmirnov-HORIS committed Jun 23, 2023
commit cbabbe3ee888525936080bd3591addfff4038c33
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ object Stats {
val THEORETICAL = DataFrame.Variable("..theoretical..", STAT, "theoretical")
val SE = DataFrame.Variable("..se..", STAT, "standard error")
val LEVEL = DataFrame.Variable("..level..", STAT, "level")
val MEAN = DataFrame.Variable("..mean..", STAT, "mean")
val MEDIAN = DataFrame.Variable("..median..", STAT, "median")
val QUANTILE = DataFrame.Variable("..quantile..", STAT, "quantile")
val QUANTILE_A = DataFrame.Variable("..qa..", STAT, "quantile a")
val QUANTILE_B = DataFrame.Variable("..qb..", STAT, "quantile b")
val QUANTILE_C = DataFrame.Variable("..qc..", STAT, "quantile c")
val LOWER = DataFrame.Variable("..lower..", STAT, "lower")
val MIDDLE = DataFrame.Variable("..middle..", STAT, "middle")
val UPPER = DataFrame.Variable("..upper..", STAT, "upper")
Expand Down Expand Up @@ -53,7 +58,12 @@ object Stats {
THEORETICAL,
SE,
LEVEL,
MEAN,
MEDIAN,
QUANTILE,
QUANTILE_A,
QUANTILE_B,
QUANTILE_C,
LOWER,
MIDDLE,
UPPER,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import jetbrains.datalore.plot.base.data.TransformVar
import jetbrains.datalore.plot.common.data.SeriesUtil

class SummaryStat(
private val aggFunctionsMap: Map<Aes<*>, (List<Double>) -> Double>
private val aggFunctionsMap: Map<DataFrame.Variable, (List<Double>) -> Double>
) : BaseStat(DEF_MAPPING) {

override fun consumes(): List<Aes<*>> {
Expand Down Expand Up @@ -57,28 +57,25 @@ class SummaryStat(
}

val statX = ArrayList<Double>()
val statAggValues: Map<Aes<*>, MutableList<Double>> = AGG_MAPPING.keys.associateWith { mutableListOf() }
val statAggValues: Map<DataFrame.Variable, MutableList<Double>> = aggFunctionsMap.keys.associateWith { mutableListOf() }
for ((x, bin) in binnedData) {
statX.add(x)
val sortedBin = Ordering.natural<Double>().sortedCopy(bin)
for ((aes, aggValues) in statAggValues) {
val aggFunction = aggFunctionsMap[aes] ?: SummaryUtil::nan
for ((statVar, aggValues) in statAggValues) {
val aggFunction = aggFunctionsMap[statVar] ?: SummaryUtil::nan
aggValues.add(aggFunction(sortedBin))
}
}

return mapOf(Stats.X to statX) + statAggValues.map { (aes, aggValues) -> Pair(AGG_MAPPING[aes]!!, aggValues) }.toMap()
return mapOf(Stats.X to statX) + statAggValues
}

companion object {
private val AGG_MAPPING: Map<Aes<*>, DataFrame.Variable> = mapOf(
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,
Aes.YMAX to Stats.Y_MAX
)
private val DEF_MAPPING: Map<Aes<*>, DataFrame.Variable> = mapOf(Aes.X to Stats.X) + AGG_MAPPING
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

package jetbrains.datalore.plot.config

import jetbrains.datalore.plot.base.Aes
import jetbrains.datalore.plot.base.DataFrame
import jetbrains.datalore.plot.base.GeomKind
import jetbrains.datalore.plot.base.Stat
import jetbrains.datalore.plot.base.stat.*
import jetbrains.datalore.plot.config.Option.Mapping
import jetbrains.datalore.plot.config.Option.Stat.Bin
import jetbrains.datalore.plot.config.Option.Stat.Bin2d
import jetbrains.datalore.plot.config.Option.Stat.Boxplot
Expand Down Expand Up @@ -409,22 +408,22 @@ object StatProto {

private fun configureSummaryStat(options: OptionsAccessor): SummaryStat {
val defaultAggFunctions = mapOf(
Aes.Y to (getAggFunction(options, Summary.FUN) ?: SummaryUtil::mean),
Aes.YMIN to (getAggFunction(options, Summary.FUN_MIN) ?: SummaryUtil::min),
Aes.YMAX to (getAggFunction(options, Summary.FUN_MAX) ?: SummaryUtil::max)
Stats.Y to (getAggFunction(options, Summary.FUN) ?: SummaryUtil::mean),
Stats.Y_MIN to (getAggFunction(options, Summary.FUN_MIN) ?: SummaryUtil::min),
Stats.Y_MAX to (getAggFunction(options, Summary.FUN_MAX) ?: SummaryUtil::max)
)

val additionalAggFunctions = configureAggFunMap(options.getMap(Summary.FUN_MAP))

return SummaryStat(defaultAggFunctions + additionalAggFunctions)
}

private fun configureAggFunMap(aggFunMap: Map<String, Any>): Map<Aes<*>, (List<Double>) -> Double> {
private fun configureAggFunMap(aggFunMap: Map<String, Any>): Map<DataFrame.Variable, (List<Double>) -> Double> {
val aggFunOptions = OptionsAccessor(aggFunMap)
return aggFunMap.keys.associate { aesName ->
return aggFunMap.keys.associate { statVarName ->
Pair(
Mapping.toAes(aesName),
getAggFunction(aggFunOptions, aesName) ?: SummaryUtil::nan
Stats.statVar("..$statVarName.."),
getAggFunction(aggFunOptions, statVarName) ?: SummaryUtil::nan
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ class Summary {
'layers': [
{
'geom': 'crossbar',
'mapping': {'middle': '..median..'},
'stat': 'summary',
'fun_map': {'middle': 'median'}
'fun_map': {'median': 'median'}
}
]
}
Expand Down
30 changes: 26 additions & 4 deletions python-package/lets_plot/plot/stat.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# 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.

from .core import aes
from .geom import _geom


#
# Stats - functions, drawing attention to the statistical transformation rather than the visual appearance.
#
Expand All @@ -16,15 +15,38 @@ def stat_summary(mapping=None, *, data=None, geom='pointrange',
fun=None, fun_min=None, fun_max=None, fun_map=None,
color_by=None, fill_by=None,
**other_args):
mapping_dict = mapping.as_dict() if mapping is not None else {}

quantile_agg_functions = {"qa": None, "qb": None, "qc": None}

def get_stat_name(agg_fun):
if isinstance(agg_fun, str) and agg_fun not in ["q1", "q2", "q3"]:
return agg_fun
else:
name = next((q for (q, f) in quantile_agg_functions.items() if f is None or f == agg_fun), None)
if name is None:
raise Exception("No more than three different quantiles can be used in fun_map parameter")
quantile_agg_functions[name] = agg_fun
return name

inner_fun_map = {}
fun_mapping_dict = {}
for aes_name, fun_name in (fun_map or {}).items():
stat_name = get_stat_name(fun_name)
inner_fun_map[stat_name] = fun_name
fun_mapping_dict[aes_name] = "..{0}..".format(stat_name)
inner_mapping_dict = {**fun_mapping_dict, **mapping_dict}
inner_mapping = aes(**inner_mapping_dict) if len(inner_mapping_dict.keys()) > 0 else None

return _geom(geom,
mapping=mapping,
mapping=inner_mapping,
data=data,
stat='summary',
position=position,
show_legend=show_legend,
sampling=sampling,
tooltips=tooltips,
orientation=orientation,
fun=fun, fun_min=fun_min, fun_max=fun_max, fun_map=fun_map,
fun=fun, fun_min=fun_min, fun_max=fun_max, fun_map=inner_fun_map,
color_by=color_by, fill_by=fill_by,
**other_args)