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
Remove extra enum class from the SummaryStatUtil.
  • Loading branch information
ASmirnov-HORIS committed Jun 21, 2023
commit 6ce963b4e9937f3655f99298b8cf79b399f33061
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SummaryStat(
}

val statValues: Map<Aes<*>, MutableList<Double>> = DEF_MAPPING.keys.associateWith { mutableListOf() }
val defaultAggFun = SummaryStatUtil.AggFun.NAN.aggFun
val defaultAggFun: (SummaryStatUtil.Calculator) -> Double = { calc -> calc.nan }
for ((x, bin) in binnedData) {
val calc = SummaryStatUtil.Calculator(bin)
for (aes in statValues.keys) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,88 +18,35 @@ object SummaryStatUtil {
class Calculator(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()
}
val nan = Double.NaN
val count by lazy { sortedValues.size.toDouble() }
val sum by lazy { sortedValues.sum() }
val mean by lazy {
if (sortedValues.isEmpty()) {
Double.NaN
} else if (sortedValues.size == 1) {
sortedValues.first()
} else {
sum / sortedValues.size
}
return max!!
}

fun q1(): Double {
if (q1 == null) {
q1 = quantile(0.25)
val median by lazy { quantile(0.5) }
val min by lazy {
if (sortedValues.isEmpty()) {
Double.NaN
} else {
sortedValues.first()
}
return q1!!
}

fun q3(): Double {
if (q3 == null) {
q3 = quantile(0.75)
val max by lazy {
if (sortedValues.isEmpty()) {
Double.NaN
} else {
sortedValues.last()
}
return q3!!
}
val q1 by lazy { quantile(0.25) }
val q3 by lazy { quantile(0.75) }

fun quantile(p: Double): Double {
if (sortedValues.isEmpty()) {
Expand All @@ -117,16 +64,4 @@ object SummaryStatUtil {
}
}
}

enum class AggFun(val aggFun: (Calculator) -> Double) {
NAN({ calc -> calc.nan() }),
COUNT({ calc -> calc.count() }),
SUM({ calc -> calc.sum() }),
MEAN({ calc -> calc.mean() }),
MEDIAN({ calc -> calc.median() }),
MIN({ calc -> calc.min() }),
MAX({ calc -> calc.max() }),
Q1({ calc -> calc.q1() }),
Q3({ calc -> calc.q3() }),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,15 @@ object StatProto {
} else {
opts.getStringDef(option, default).let {
when (it.lowercase()) {
"nan" -> SummaryStatUtil.AggFun.NAN.aggFun
"count" -> SummaryStatUtil.AggFun.COUNT.aggFun
"sum" -> SummaryStatUtil.AggFun.SUM.aggFun
"mean" -> SummaryStatUtil.AggFun.MEAN.aggFun
"median" -> SummaryStatUtil.AggFun.MEDIAN.aggFun
"min" -> SummaryStatUtil.AggFun.MIN.aggFun
"max" -> SummaryStatUtil.AggFun.MAX.aggFun
"q1" -> SummaryStatUtil.AggFun.Q1.aggFun
"q3" -> SummaryStatUtil.AggFun.Q3.aggFun
"nan" -> { calc -> calc.nan }
"count" -> { calc -> calc.count }
"sum" -> { calc -> calc.sum }
"mean" -> { calc -> calc.mean }
"median" -> { calc -> calc.median }
"min" -> { calc -> calc.min }
"max" -> { calc -> calc.max }
"q1" -> { calc -> calc.q1 }
"q3" -> { calc -> calc.q3 }
else -> throw IllegalArgumentException(
"Unsupported function name: '$it'\n" +
"Use one of: nan, count, sum, mean, median, min, max, q1, q3."
Expand Down