Skip to content

Commit d91930e

Browse files
committed
rollback of changes to mean
1 parent c72335f commit d91930e

File tree

21 files changed

+184
-766
lines changed

21 files changed

+184
-766
lines changed

core/api/core.api

+20-126
Large diffs are not rendered by default.

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/describe.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface ColumnDescription {
2525
public val nulls: Int
2626
public val top: Any
2727
public val freq: Int
28-
public val mean: Number?
28+
public val mean: Double
2929
public val std: Double
3030
public val min: Any
3131
public val p25: Any

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/mean.kt

+28-432
Large diffs are not rendered by default.

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/TypeUtils.kt

-10
Original file line numberDiff line numberDiff line change
@@ -643,13 +643,3 @@ internal fun Iterable<Any>.classes(): Set<KClass<*>> = mapTo(mutableSetOf()) { i
643643
* @return A set of [KType] objects corresponding to the star-projected runtime types of elements in the iterable.
644644
*/
645645
internal fun Iterable<Any>.types(): Set<KType> = classes().mapTo(mutableSetOf()) { it.createStarProjectedType(false) }
646-
647-
/**
648-
* Casts [this]: [Number] to a [Double]. If [this] is `null`, returns [Double.NaN].
649-
*/
650-
internal fun Number?.asDoubleOrNaN(): Double = this as Double? ?: Double.NaN
651-
652-
/**
653-
* Casts [this]: [Number] to a [Float]. If [this] is `null`, returns [Float.NaN].
654-
*/
655-
internal fun Number?.asFloatOrNaN(): Float = this as Float? ?: Float.NaN

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/aggregators/Aggregator.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import kotlin.reflect.full.withNullability
1919
* will always return a [Return]`?`.
2020
*/
2121
@PublishedApi
22-
internal interface Aggregator<Value, Return> {
22+
internal interface Aggregator<in Value, out Return> {
2323

2424
/** The name of this aggregator. */
2525
val name: String

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/aggregators/AggregatorBase.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import kotlin.reflect.full.withNullability
1616
* @param name The name of this aggregator.
1717
* @param aggregator Functional argument for the [aggregate] function.
1818
*/
19-
internal abstract class AggregatorBase<Value, Return>(
19+
internal abstract class AggregatorBase<in Value, out Return>(
2020
override val name: String,
2121
protected val getReturnTypeOrNull: (type: KType, emptyInput: Boolean) -> KType?,
2222
protected val aggregator: (values: Iterable<Value>, type: KType) -> Return?,

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/aggregators/AggregatorOptionSwitch.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators
77
* @see AggregatorOptionSwitch2
88
*/
99
@PublishedApi
10-
internal class AggregatorOptionSwitch1<Param1, AggregatorType : Aggregator<*, *>>(
10+
internal class AggregatorOptionSwitch1<in Param1, out AggregatorType : Aggregator<*, *>>(
1111
val name: String,
1212
val getAggregator: (param1: Param1) -> AggregatorProvider<AggregatorType>,
1313
) {
@@ -28,7 +28,7 @@ internal class AggregatorOptionSwitch1<Param1, AggregatorType : Aggregator<*, *>
2828
* MyAggregator.Factory(param1)
2929
* }
3030
*/
31-
class Factory<Param1, AggregatorType : Aggregator<*, *>>(
31+
class Factory<in Param1, out AggregatorType : Aggregator<*, *>>(
3232
val getAggregator: (Param1) -> AggregatorProvider<AggregatorType>,
3333
) : Provider<AggregatorOptionSwitch1<Param1, AggregatorType>> by Provider({ name ->
3434
AggregatorOptionSwitch1(name, getAggregator)
@@ -42,7 +42,7 @@ internal class AggregatorOptionSwitch1<Param1, AggregatorType : Aggregator<*, *>
4242
* @see AggregatorOptionSwitch1
4343
*/
4444
@PublishedApi
45-
internal class AggregatorOptionSwitch2<Param1, Param2, AggregatorType : Aggregator<*, *>>(
45+
internal class AggregatorOptionSwitch2<in Param1, in Param2, out AggregatorType : Aggregator<*, *>>(
4646
val name: String,
4747
val getAggregator: (param1: Param1, param2: Param2) -> AggregatorProvider<AggregatorType>,
4848
) {
@@ -64,7 +64,7 @@ internal class AggregatorOptionSwitch2<Param1, Param2, AggregatorType : Aggregat
6464
* }
6565
* ```
6666
*/
67-
class Factory<Param1, Param2, AggregatorType : Aggregator<*, *>>(
67+
class Factory<in Param1, in Param2, out AggregatorType : Aggregator<*, *>>(
6868
val getAggregator: (Param1, Param2) -> AggregatorProvider<AggregatorType>,
6969
) : Provider<AggregatorOptionSwitch2<Param1, Param2, AggregatorType>> by Provider({ name ->
7070
AggregatorOptionSwitch2(name, getAggregator)

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/aggregators/AggregatorProvider.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import kotlin.reflect.KProperty
1010
* val myNamedValue by MyFactory
1111
* ```
1212
*/
13-
internal fun interface Provider<T> {
13+
internal fun interface Provider<out T> {
1414

1515
fun create(name: String): T
1616
}
@@ -25,4 +25,4 @@ internal operator fun <T> Provider<T>.getValue(obj: Any?, property: KProperty<*>
2525
* val myAggregator by MyAggregator.Factory
2626
* ```
2727
*/
28-
internal fun interface AggregatorProvider<AggregatorType : Aggregator<*, *>> : Provider<AggregatorType>
28+
internal fun interface AggregatorProvider<out AggregatorType : Aggregator<*, *>> : Provider<AggregatorType>

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/aggregators/Aggregators.kt

+31-25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators
22

3-
import org.jetbrains.kotlinx.dataframe.math.meanOrNull
4-
import org.jetbrains.kotlinx.dataframe.math.meanTypeResultOrNull
3+
import org.jetbrains.kotlinx.dataframe.math.mean
54
import org.jetbrains.kotlinx.dataframe.math.median
65
import org.jetbrains.kotlinx.dataframe.math.percentile
76
import org.jetbrains.kotlinx.dataframe.math.std
87
import org.jetbrains.kotlinx.dataframe.math.sum
9-
import java.math.BigDecimal
108
import kotlin.reflect.KType
119
import kotlin.reflect.full.withNullability
1210
import kotlin.reflect.typeOf
@@ -92,43 +90,51 @@ internal object Aggregators {
9290
getAggregator: (Param1, Param2) -> AggregatorProvider<AggregatorType>,
9391
) = AggregatorOptionSwitch2.Factory(getAggregator)
9492

95-
val min by twoStepPreservingType<Comparable<Any?>> { minOrNull() }
93+
// T: Comparable<T> -> T?
94+
val min by twoStepPreservingType<Comparable<Any?>> {
95+
minOrNull()
96+
}
9697

97-
val max by twoStepPreservingType<Comparable<Any?>> { maxOrNull() }
98+
// T: Comparable<T> -> T?
99+
val max by twoStepPreservingType<Comparable<Any?>> {
100+
maxOrNull()
101+
}
98102

103+
// T: Number? -> Double
99104
val std by withTwoOptions { skipNA: Boolean, ddof: Int ->
100105
flatteningChangingTypes<Number, Double>(
101-
getReturnTypeOrNull = { type, emptyInput -> typeOf<Double>().withNullability(emptyInput) },
102-
) { std(it, skipNA, ddof) }
106+
getReturnTypeOrNull = { _, emptyInput -> typeOf<Double>().withNullability(emptyInput) },
107+
) { type ->
108+
std(type, skipNA, ddof)
109+
}
103110
}
104111

105-
@Suppress("ClassName")
106-
object mean {
107-
val toNumber = withOneOption { skipNA: Boolean ->
108-
twoStepForNumbers(::meanTypeResultOrNull) { meanOrNull(it, skipNA) }
109-
}.create(mean::class.simpleName!!)
110-
111-
val toDouble = withOneOption { skipNA: Boolean ->
112-
twoStepForNumbers(::meanTypeResultOrNull) { meanOrNull(it, skipNA) as Double? }
113-
}.create(mean::class.simpleName!!)
114-
115-
val toBigDecimal =
116-
twoStepForNumbers(::meanTypeResultOrNull) {
117-
meanOrNull(it) as BigDecimal?
118-
}.create(mean::class.simpleName!!)
112+
// step one: T: Number? -> Double
113+
// step two: Double -> Double
114+
val mean by withOneOption { skipNA: Boolean ->
115+
twoStepChangingType(
116+
getReturnTypeOrNull = { _, _ -> typeOf<Double>() },
117+
stepOneAggregator = { type -> mean(type, skipNA) },
118+
stepTwoAggregator = { mean(skipNA) },
119+
)
119120
}
120121

122+
// T: Comparable<T>? -> T
121123
val percentile by withOneOption { percentile: Double ->
122-
flatteningChangingTypes<Comparable<Any?>, Comparable<Any?>>(preserveReturnTypeNullIfEmpty) { type ->
124+
flatteningPreservingTypes<Comparable<Any?>> { type ->
123125
percentile(percentile, type)
124126
}
125127
}
126128

127-
val median by flatteningPreservingTypes<Comparable<Any?>> {
128-
median(it)
129+
// T: Comparable<T>? -> T
130+
val median by flatteningPreservingTypes<Comparable<Any?>> { type ->
131+
median(type)
129132
}
130133

134+
// T: Number -> T
131135
val sum by twoStepForNumbers(
132136
getReturnTypeOrNull = { type, _ -> type.withNullability(false) },
133-
) { sum(it) }
137+
) { type ->
138+
sum(type)
139+
}
134140
}

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/aggregators/FlatteningAggregator.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import kotlin.reflect.full.withNullability
3030
* Note that it must be able to handle `null` values for the [Iterable] overload of [aggregate].
3131
* @param preservesType If `true`, [Value][Value]` == `[Return][Return].
3232
*/
33-
internal class FlatteningAggregator<Value, Return>(
33+
internal class FlatteningAggregator<in Value, out Return>(
3434
name: String,
3535
getReturnTypeOrNull: (type: KType, emptyInput: Boolean) -> KType?,
3636
aggregator: (values: Iterable<Value>, type: KType) -> Return?,
@@ -54,7 +54,7 @@ internal class FlatteningAggregator<Value, Return>(
5454
* @param aggregator Functional argument for the [aggregate] function.
5555
* @param preservesType If `true`, [Value][Value]` == `[Return][Return].
5656
*/
57-
class Factory<Value, Return>(
57+
class Factory<in Value, out Return>(
5858
private val getReturnTypeOrNull: (type: KType, emptyInput: Boolean) -> KType?,
5959
private val aggregator: (Iterable<Value>, KType) -> Return?,
6060
private val preservesType: Boolean,

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/aggregators/TwoStepAggregator.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import kotlin.reflect.full.withNullability
3737
* It is run on the results of [stepOneAggregator].
3838
* @param preservesType If `true`, [Value][Value]` == `[Return][Return].
3939
*/
40-
internal class TwoStepAggregator<Value, Return>(
40+
internal class TwoStepAggregator<in Value, out Return>(
4141
name: String,
4242
getReturnTypeOrNull: (type: KType, emptyInput: Boolean) -> KType?,
4343
stepOneAggregator: (values: Iterable<Value>, type: KType) -> Return?,
@@ -73,7 +73,7 @@ internal class TwoStepAggregator<Value, Return>(
7373
* It is run on the results of [stepOneAggregator].
7474
* @param preservesType If `true`, [Value][Value]` == `[Return][Return].
7575
*/
76-
class Factory<Value, Return>(
76+
class Factory<in Value, out Return>(
7777
private val getReturnTypeOrNull: (type: KType, emptyInput: Boolean) -> KType?,
7878
private val stepOneAggregator: (Iterable<Value>, KType) -> Return?,
7979
private val stepTwoAggregator: (Iterable<Return>, KType) -> Return?,

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/aggregators/TwoStepNumbersAggregator.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import kotlin.reflect.typeOf
3737
* While it takes a [Number] argument, you can assume that all values are of the same specific type, however,
3838
* this type can be different for different calls to [aggregator].
3939
*/
40-
internal class TwoStepNumbersAggregator<Return : Number>(
40+
internal class TwoStepNumbersAggregator<out Return : Number>(
4141
name: String,
4242
getReturnTypeOrNull: (type: KType, emptyInput: Boolean) -> KType?,
4343
aggregator: (values: Iterable<Number>, numberType: KType) -> Return?,
@@ -84,7 +84,7 @@ internal class TwoStepNumbersAggregator<Return : Number>(
8484

8585
override val preservesType = false
8686

87-
class Factory<Return : Number>(
87+
class Factory<out Return : Number>(
8888
private val getReturnTypeOrNull: (type: KType, emptyInput: Boolean) -> KType?,
8989
private val aggregate: Iterable<Number>.(numberType: KType) -> Return?,
9090
) : AggregatorProvider<TwoStepNumbersAggregator<Return>> by AggregatorProvider({ name ->

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/describe.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import org.jetbrains.kotlinx.dataframe.api.isNumber
1616
import org.jetbrains.kotlinx.dataframe.api.map
1717
import org.jetbrains.kotlinx.dataframe.api.maxOrNull
1818
import org.jetbrains.kotlinx.dataframe.api.mean
19-
import org.jetbrains.kotlinx.dataframe.api.meanOrNull
2019
import org.jetbrains.kotlinx.dataframe.api.medianOrNull
2120
import org.jetbrains.kotlinx.dataframe.api.minOrNull
2221
import org.jetbrains.kotlinx.dataframe.api.move
@@ -57,7 +56,7 @@ internal fun describeImpl(cols: List<AnyCol>): DataFrame<ColumnDescription> {
5756
?.key
5857
}
5958
if (hasNumericCols) {
60-
ColumnDescription::mean from { if (it.isNumber()) it.asNumbers().meanOrNull() else null }
59+
ColumnDescription::mean from { if (it.isNumber()) it.asNumbers().mean() else null }
6160
ColumnDescription::std from { if (it.isNumber()) it.asNumbers().std() else null }
6261
}
6362
if (hasComparableCols || hasNumericCols) {

0 commit comments

Comments
 (0)