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

Reordering #411

Merged
merged 11 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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 'sum' as aggregate operation for position='stack'.
  • Loading branch information
OLarionova-HORIS committed Jul 27, 2021
commit 0e30f7e43bb2f20b1f5788501f82f36a6a68d263
36 changes: 18 additions & 18 deletions docs/examples/jupyter-notebooks-dev/ordering_examples.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ class GeomLayerBuilder {
statCtx,
varsWithoutBinding = emptyList(),
orderOptions = emptyList(),
aggregateOperation = null,
::println
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ object DataProcessing {
statCtx: StatContext,
varsWithoutBinding: List<String>,
orderOptions: List<OrderOptionUtil.OrderOption>,
aggregateOperation: ((List<Double?>) -> Double?)?,
messageConsumer: Consumer<String>
): DataAndGroupingContext {
if (stat === Stats.IDENTITY) {
Expand All @@ -82,7 +83,7 @@ object DataProcessing {
if (sd.isEmpty) {
continue
}
groupMerger.initOrderSpecs(orderOptions, sd.variables(), bindings)
groupMerger.initOrderSpecs(orderOptions, sd.variables(), bindings, aggregateOperation)

val curGroupSizeAfterStat = sd.rowCount()

Expand Down Expand Up @@ -126,7 +127,7 @@ object DataProcessing {

// set ordering specifications
val orderSpecs = orderOptions.map { orderOption ->
OrderOptionUtil.createOrderSpec(resultSeries.keys, bindings, orderOption)
OrderOptionUtil.createOrderSpec(resultSeries.keys, bindings, orderOption, aggregateOperation)
}
addOrderSpecs(orderSpecs)

Expand All @@ -152,15 +153,16 @@ object DataProcessing {
fun initOrderSpecs(
orderOptions: List<OrderOptionUtil.OrderOption>,
variables: Set<Variable>,
bindings: List<VarBinding>
bindings: List<VarBinding>,
aggregateOperation: ((List<Double?>) -> Double?)?
) {
if (myOrderSpecs != null) return
myOrderSpecs = orderOptions
.filter { orderOption ->
// no need to reorder groups by X
bindings.find { it.variable.name == orderOption.variableName && it.aes == Aes.X } == null
}
.map { OrderOptionUtil.createOrderSpec(variables, bindings, it) }
.map { OrderOptionUtil.createOrderSpec(variables, bindings, it, aggregateOperation) }
}

fun getResultSeries(): HashMap<Variable, MutableList<Any?>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import jetbrains.datalore.plot.base.Aes
import jetbrains.datalore.plot.base.DataFrame
import jetbrains.datalore.plot.builder.VarBinding
import jetbrains.datalore.plot.builder.sampling.method.SamplingUtil
import jetbrains.datalore.plot.common.data.SeriesUtil

object OrderOptionUtil {
class OrderOption internal constructor(
Expand All @@ -27,7 +26,7 @@ object OrderOptionUtil {
if (orderBy == null && order == null) {
return null
}
require(order == null || (order is Number && order.toInt() in listOf(-1, 1))){
require(order == null || (order is Number && order.toInt() in listOf(-1, 1))) {
"Unsupported `order` value: $order. Use 1 (ascending) or -1 (descending)."
}

Expand Down Expand Up @@ -56,7 +55,8 @@ object OrderOptionUtil {
fun createOrderSpec(
variables: Set<DataFrame.Variable>,
varBindings: List<VarBinding>,
orderOption: OrderOption
orderOption: OrderOption,
aggregateOperation: ((List<Double?>) -> Double?)?
): DataFrame.OrderSpec {
fun getVariableByName(varName: String): DataFrame.Variable {
return variables.find { it.name == varName }
Expand All @@ -73,21 +73,14 @@ object OrderOptionUtil {
getVariableByName(orderOption.variableName)
}

// TODO Need to define the aggregate operation
val aggregateOperation =
if (orderOption.byVariable != null && orderOption.byVariable != orderOption.variableName) {
// Use ordering by the 'order_by' variable with the specified aggregation
{ v: List<Double?> -> SeriesUtil.mean(v, defaultValue = null) }
} else {
// Use ordering by the 'variable' without aggregation
null
}

return DataFrame.OrderSpec(
variable,
orderOption.byVariable?.let(::getVariableByName) ?: getVariableByName(orderOption.variableName),
orderOption.getOrderDir(),
aggregateOperation
aggregateOperation.takeIf {
// Use the aggregation for ordering by the specified 'order_by' variable
orderOption.byVariable != null && orderOption.byVariable != orderOption.variableName
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import jetbrains.datalore.plot.builder.data.OrderOptionUtil.OrderOption.Companio
import jetbrains.datalore.plot.builder.data.OrderOptionUtil.createOrderSpec
import jetbrains.datalore.plot.builder.sampling.Sampling
import jetbrains.datalore.plot.builder.tooltip.TooltipSpecification
import jetbrains.datalore.plot.common.data.SeriesUtil
import jetbrains.datalore.plot.config.ConfigUtil.createAesMapping
import jetbrains.datalore.plot.config.DataMetaUtil.createDataFrame
import jetbrains.datalore.plot.config.DataMetaUtil.inheritToNonDiscrete
import jetbrains.datalore.plot.config.Option.Geom.Choropleth.GEO_POSITIONS
import jetbrains.datalore.plot.config.Option.Layer.GEOM
import jetbrains.datalore.plot.config.Option.Layer.MAP_JOIN
import jetbrains.datalore.plot.config.Option.Layer.NONE
import jetbrains.datalore.plot.config.Option.Layer.POS
import jetbrains.datalore.plot.config.Option.Layer.SHOW_LEGEND
import jetbrains.datalore.plot.config.Option.Layer.STAT
import jetbrains.datalore.plot.config.Option.Layer.TOOLTIPS
Expand Down Expand Up @@ -87,6 +89,11 @@ class LayerConfig(
val orderOptions: List<OrderOption>
get() = myOrderOptions

val aggregateOperation: ((List<Double?>) -> Double?) = when (getString(POS)) {
PosProto.STACK -> SeriesUtil::sum
else -> { v: List<Double?> -> SeriesUtil.mean(v, defaultValue = null) }
}

init {
val (layerMappings, layerData) = createDataFrame(
options = this,
Expand Down Expand Up @@ -223,7 +230,9 @@ class LayerConfig(
.values.toList()

myCombinedData = if (clientSide) {
val orderSpecs = myOrderOptions.map { createOrderSpec(combinedData.variables(), varBindings, it) }
val orderSpecs = myOrderOptions.map {
createOrderSpec(combinedData.variables(), varBindings, it, aggregateOperation)
}
DataFrame.Builder(combinedData).addOrderSpecs(orderSpecs).build()
} else {
combinedData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import jetbrains.datalore.plot.builder.assemble.PosProvider
internal object PosProto {
// position adjustments
private const val IDENTITY = "identity"
private const val STACK = "stack"
internal const val STACK = "stack"
private const val DODGE = "dodge"
private const val FILL = "fill"
private const val NUDGE = "nudge"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ open class PlotConfigServerSide(opts: Map<String, Any>) : PlotConfig(opts) {
facets,
statCtx,
varsWithoutBinding,
layerConfig.orderOptions
layerConfig.orderOptions,
layerConfig.aggregateOperation
) { message ->
layerIndexAndSamplingMessage(
layerIndex,
Expand Down