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

Unified DATA and MAPPING options for plot and layer. #110

Merged
merged 1 commit into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import jetbrains.datalore.plot.builder.assemble.PosProvider
import jetbrains.datalore.plot.builder.assemble.TypedScaleProviderMap
import jetbrains.datalore.plot.builder.assemble.geom.DefaultAesAutoMapper
import jetbrains.datalore.plot.builder.sampling.Sampling
import jetbrains.datalore.plot.config.Option.Layer.DATA
import jetbrains.datalore.plot.config.Option.Layer.GEOM
import jetbrains.datalore.plot.config.Option.Layer.MAPPING
import jetbrains.datalore.plot.config.Option.Layer.SHOW_LEGEND
import jetbrains.datalore.plot.config.Option.Layer.STAT
import jetbrains.datalore.plot.config.Option.Layer.TOOLTIP
import jetbrains.datalore.plot.config.Option.PlotBase.DATA
import jetbrains.datalore.plot.config.Option.PlotBase.MAPPING

class LayerConfig constructor(
layerOptions: Map<*, *>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ object Option {
}
}

object Plot {
object PlotBase {
const val DATA = "data"
// ToDo: merge 'data' options
const val MAPPING = "mapping"
}

object Plot {
const val LAYERS = "layers"
const val SCALES = "scales"
const val TITLE = "ggtitle"
Expand All @@ -76,8 +78,6 @@ object Option {
object Layer {
const val GEOM = "geom"
const val STAT = "stat"
const val DATA = "data"
const val MAPPING = "mapping"
const val POS = "position"
const val SAMPLING = "sampling"
const val SHOW_LEGEND = "show_legend"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import jetbrains.datalore.plot.builder.assemble.TypedScaleProviderMap
import jetbrains.datalore.plot.config.Option.Meta
import jetbrains.datalore.plot.config.Option.Meta.Kind
import jetbrains.datalore.plot.config.Option.Plot.COORD
import jetbrains.datalore.plot.config.Option.Plot.DATA
import jetbrains.datalore.plot.config.Option.Plot.FACET
import jetbrains.datalore.plot.config.Option.Plot.LAYERS
import jetbrains.datalore.plot.config.Option.Plot.MAPPING
import jetbrains.datalore.plot.config.Option.Plot.SCALES
import jetbrains.datalore.plot.config.Option.Plot.TITLE
import jetbrains.datalore.plot.config.Option.Plot.TITLE_TEXT
import jetbrains.datalore.plot.config.Option.PlotBase.DATA
import jetbrains.datalore.plot.config.Option.PlotBase.MAPPING

abstract class PlotConfig(opts: Map<String, Any>) : OptionsAccessor(
opts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ package jetbrains.datalore.plot.config.transform
import jetbrains.datalore.plot.config.Option.GGBunch
import jetbrains.datalore.plot.config.Option.Layer
import jetbrains.datalore.plot.config.Option.Plot
import jetbrains.datalore.plot.config.Option.Plot.DATA
import jetbrains.datalore.plot.config.Option.PlotBase.DATA
import jetbrains.datalore.plot.config.transform.SpecSelector.Companion.from

object PlotSpecTransformUtil {
val GGBUNCH_KEY_PARTS = arrayOf(GGBunch.ITEMS, GGBunch.Item.FEATURE_SPEC)
private val PLOT_WITH_LAYERS_TARGETS = listOf(
PlotSpecTransformUtil.TargetSpec.PLOT,
PlotSpecTransformUtil.TargetSpec.LAYER,
PlotSpecTransformUtil.TargetSpec.GEOM,
PlotSpecTransformUtil.TargetSpec.STAT
TargetSpec.PLOT,
TargetSpec.LAYER,
TargetSpec.GEOM,
TargetSpec.STAT
)

fun getDataSpecFinders(isGGBunch: Boolean): List<SpecFinder> {
Expand Down Expand Up @@ -78,10 +78,10 @@ object PlotSpecTransformUtil {
private fun selectorKeys(target: TargetSpec, isGGBunch: Boolean): Array<String> {
var keys: Array<String>
when (target) {
PlotSpecTransformUtil.TargetSpec.PLOT -> keys = arrayOf()
PlotSpecTransformUtil.TargetSpec.LAYER -> keys = arrayOf(Plot.LAYERS)
PlotSpecTransformUtil.TargetSpec.GEOM -> keys = arrayOf(Plot.LAYERS, Layer.GEOM)
PlotSpecTransformUtil.TargetSpec.STAT -> keys = arrayOf(Plot.LAYERS, Layer.STAT)
TargetSpec.PLOT -> keys = arrayOf()
TargetSpec.LAYER -> keys = arrayOf(Plot.LAYERS)
TargetSpec.GEOM -> keys = arrayOf(Plot.LAYERS, Layer.GEOM)
TargetSpec.STAT -> keys = arrayOf(Plot.LAYERS, Layer.STAT)
}

if (isGGBunch) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
package jetbrains.datalore.plot.server.config.transform

import jetbrains.datalore.plot.config.*
import jetbrains.datalore.plot.config.Option.Layer
import jetbrains.datalore.plot.config.Option.Meta.DATA_META
import jetbrains.datalore.plot.config.Option.Meta.SeriesAnnotation
import jetbrains.datalore.plot.config.Option.Meta.SeriesAnnotation.ANNOTATION
import jetbrains.datalore.plot.config.Option.Meta.SeriesAnnotation.VARIABLE
import jetbrains.datalore.plot.config.Option.Plot
import jetbrains.datalore.plot.config.Option.PlotBase.MAPPING
import jetbrains.datalore.plot.config.Option.Scale
import jetbrains.datalore.plot.config.transform.SpecChange
import jetbrains.datalore.plot.config.transform.SpecChangeContext
Expand All @@ -21,7 +21,7 @@ class DiscreteScaleFromAnnotationChange : SpecChange {
override fun apply(spec: MutableMap<String, Any>, ctx: SpecChangeContext) {
val annotationScales = spec
.sections(Plot.LAYERS)!!
.filter { it.has(DATA_META, SeriesAnnotation.TAG) && it.has(Layer.MAPPING) }
.filter { it.has(DATA_META, SeriesAnnotation.TAG) && it.has(MAPPING) }
.flatMap(::scalesFromAnnotation)

if (annotationScales.isNotEmpty()) {
Expand All @@ -31,7 +31,7 @@ class DiscreteScaleFromAnnotationChange : SpecChange {

companion object {
private fun scalesFromAnnotation(layer: Map<*, *>): List<Map<*, *>> {
val mapping = layer.section(Layer.MAPPING)!!.entries.associateBy({ it.value }, { it.key as String })
val mapping = layer.section(MAPPING)!!.entries.associateBy({ it.value }, { it.key as String })

return layer.sections(DATA_META, SeriesAnnotation.TAG)!!
.filter { it.read(ANNOTATION) == SeriesAnnotation.DISCRETE }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import jetbrains.datalore.plot.config.*
import jetbrains.datalore.plot.config.GeoPositionsDataUtil.MAP_GEOMETRY_COLUMN
import jetbrains.datalore.plot.config.GeoPositionsDataUtil.MAP_JOIN_ID_COLUMN
import jetbrains.datalore.plot.config.Option.Geom.Choropleth.GEO_POSITIONS
import jetbrains.datalore.plot.config.Option.Layer.DATA
import jetbrains.datalore.plot.config.Option.Layer.MAPPING
import jetbrains.datalore.plot.config.Option.Mapping.MAP_ID
import jetbrains.datalore.plot.config.Option.Meta.DATA_META
import jetbrains.datalore.plot.config.Option.Meta.GeoDataFrame
import jetbrains.datalore.plot.config.Option.Meta.GeoDataFrame.GEOMETRY
import jetbrains.datalore.plot.config.Option.Plot
import jetbrains.datalore.plot.config.Option.PlotBase.DATA
import jetbrains.datalore.plot.config.Option.PlotBase.MAPPING
import jetbrains.datalore.plot.config.transform.SpecChange
import jetbrains.datalore.plot.config.transform.SpecChangeContext
import jetbrains.datalore.plot.config.transform.SpecSelector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import jetbrains.datalore.base.gcommon.base.Preconditions.checkArgument
import jetbrains.datalore.base.spatial.MercatorUtils.checkLat
import jetbrains.datalore.base.spatial.MercatorUtils.checkLon
import jetbrains.datalore.plot.config.Option.GeomName
import jetbrains.datalore.plot.config.Option.Layer.DATA
import jetbrains.datalore.plot.config.Option.Layer.GEOM
import jetbrains.datalore.plot.config.Option.Layer.MAPPING
import jetbrains.datalore.plot.config.Option.Mapping
import jetbrains.datalore.plot.config.Option.Plot
import jetbrains.datalore.plot.config.Option.PlotBase.DATA
import jetbrains.datalore.plot.config.Option.PlotBase.MAPPING
import jetbrains.datalore.plot.config.transform.SpecChange
import jetbrains.datalore.plot.config.transform.SpecChangeContext
import jetbrains.datalore.plot.config.transform.SpecFinder
Expand Down Expand Up @@ -62,7 +62,7 @@ class LonLatSpecInMappingSpecChange : SpecChange {
}

if (dataSpec == null) {
val list = ctx.getSpecsAbsolute(Plot.DATA)
val list = ctx.getSpecsAbsolute(DATA)
if (list.isNotEmpty() && list[0].keys.containsAll(keys)) {
dataSpec = list[0] as MutableMap<String, Any>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ package jetbrains.datalore.plot.server.config.transform
import jetbrains.datalore.plot.config.GeoPositionsDataUtil.MAP_JOIN
import jetbrains.datalore.plot.config.GeoPositionsDataUtil.MAP_JOIN_COLUMN
import jetbrains.datalore.plot.config.Option
import jetbrains.datalore.plot.config.Option.Layer.MAPPING
import jetbrains.datalore.plot.config.Option.Mapping.MAP_ID
import jetbrains.datalore.plot.config.Option.Meta.MAP_DATA_META
import jetbrains.datalore.plot.config.Option.PlotBase.MAPPING
import jetbrains.datalore.plot.config.list
import jetbrains.datalore.plot.config.transform.SpecChange
import jetbrains.datalore.plot.config.transform.SpecChangeContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package jetbrains.datalore.plot.server.config.transform

import jetbrains.datalore.plot.config.Option.Layer
import jetbrains.datalore.plot.config.Option.Plot
import jetbrains.datalore.plot.config.Option.PlotBase
import jetbrains.datalore.plot.config.transform.PlotSpecTransform
import jetbrains.datalore.plot.config.transform.SpecSelector
import jetbrains.datalore.plot.config.transform.migration.MoveGeomPropertiesToLayerMigration
Expand All @@ -25,15 +26,15 @@ object PlotConfigServerSideTransforms {
fun entryTransform(): PlotSpecTransform {
return PlotSpecTransform.builderForRawSpec()
.change(
SpecSelector.of(Plot.DATA),
SpecSelector.of(PlotBase.DATA),
NumericDataVectorSpecChange()
)
.change(
SpecSelector.of(Plot.LAYERS, Layer.DATA),
SpecSelector.of(Plot.LAYERS, PlotBase.DATA),
NumericDataVectorSpecChange()
)
.change(
SpecSelector.of(Plot.LAYERS, Layer.GEOM, Layer.DATA),
SpecSelector.of(Plot.LAYERS, Layer.GEOM, PlotBase.DATA),
NumericDataVectorSpecChange()
) // ToDo: remove (and tests)
.change(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

package jetbrains.datalore.plot.server.config.transform

import jetbrains.datalore.plot.config.Option.Plot.DATA
import jetbrains.datalore.plot.config.Option.Plot.LAYERS
import jetbrains.datalore.plot.config.Option.Plot.MAPPING
import jetbrains.datalore.plot.config.Option.PlotBase.DATA
import jetbrains.datalore.plot.config.Option.PlotBase.MAPPING
import jetbrains.datalore.plot.config.transform.SpecChange
import jetbrains.datalore.plot.config.transform.SpecChangeContext
import jetbrains.datalore.plot.config.transform.SpecSelector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import jetbrains.datalore.plot.base.Aes
import jetbrains.datalore.plot.builder.map.GeoPositionField
import jetbrains.datalore.plot.config.LayerConfig
import jetbrains.datalore.plot.config.Option
import jetbrains.datalore.plot.config.Option.PlotBase.DATA
import jetbrains.datalore.plot.config.Option.PlotBase.MAPPING
import jetbrains.datalore.plot.config.PlotConfigClientSideUtil
import jetbrains.datalore.plot.server.config.PlotConfigServerSide
import kotlin.test.Test
Expand All @@ -28,7 +30,7 @@ class GeomInteractionBuilderCreationTest {
Aes.COLOR.name to listOf('a')
)
val plotOpts = mutableMapOf(
Option.Plot.MAPPING to mappedData,
MAPPING to mappedData,
Option.Plot.LAYERS to listOf(
mapOf(
Option.Layer.GEOM to Option.GeomName.HISTOGRAM
Expand Down Expand Up @@ -57,7 +59,7 @@ class GeomInteractionBuilderCreationTest {
Aes.MAP_ID.name to listOf('a')
)
val plotOpts = mutableMapOf(
Option.Plot.MAPPING to mappedData,
MAPPING to mappedData,
Option.Plot.LAYERS to listOf(
mapOf(
Option.Layer.GEOM to Option.GeomName.POINT
Expand Down Expand Up @@ -87,7 +89,7 @@ class GeomInteractionBuilderCreationTest {
)

val plotOpts = mutableMapOf(
Option.Plot.MAPPING to mappedData,
MAPPING to mappedData,
Option.Plot.LAYERS to listOf(
mapOf(
Option.Layer.GEOM to Option.GeomName.POLYGON
Expand Down Expand Up @@ -120,7 +122,7 @@ class GeomInteractionBuilderCreationTest {
)

val plotOpts = mutableMapOf(
Option.Plot.MAPPING to mappedData,
MAPPING to mappedData,
Option.Plot.LAYERS to listOf(
mapOf(
Option.Layer.GEOM to Option.GeomName.HISTOGRAM
Expand Down Expand Up @@ -166,9 +168,9 @@ class GeomInteractionBuilderCreationTest {
Option.Plot.LAYERS to listOf(
mutableMapOf(
Option.Layer.GEOM to Option.GeomName.POLYGON,
Option.Layer.DATA to geomData,
DATA to geomData,
Option.Meta.DATA_META to GEO_DATA_FRAME_META,
Option.Layer.MAPPING to mapOf(Aes.FILL.name to "name")
MAPPING to mapOf(Aes.FILL.name to "name")
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
package jetbrains.datalore.plot.server.config.transform
import jetbrains.datalore.plot.base.Aes
import jetbrains.datalore.plot.config.Option
import jetbrains.datalore.plot.config.Option.Layer
import jetbrains.datalore.plot.config.Option.Mapping.toOption
import jetbrains.datalore.plot.config.Option.Meta
import jetbrains.datalore.plot.config.Option.Meta.SeriesAnnotation
import jetbrains.datalore.plot.config.Option.Meta.SeriesAnnotation.DISCRETE
import jetbrains.datalore.plot.config.Option.Plot
import jetbrains.datalore.plot.config.Option.PlotBase.MAPPING
import jetbrains.datalore.plot.config.read
import jetbrains.datalore.plot.config.sections
import jetbrains.datalore.plot.config.transform.SpecChangeContext
Expand All @@ -27,7 +28,7 @@ class DiscreteScaleFromAnnotationChangeTest {
val plotSpec = dict {
layers(
dict {
write(Layer.MAPPING, toOption(Aes.COLOR)) { varName }
write(MAPPING, toOption(Aes.COLOR)) { varName }
}
)
}
Expand All @@ -42,12 +43,12 @@ class DiscreteScaleFromAnnotationChangeTest {
val plotSpec = dict {
layers(
dict {
write(Layer.MAPPING, toOption(Aes.COLOR)) { varName }
write(MAPPING, toOption(Aes.COLOR)) { varName }
write(Meta.DATA_META, SeriesAnnotation.TAG) {
list(
dict {
write(SeriesAnnotation.VARIABLE) { varName }
write(SeriesAnnotation.ANNOTATION) { "discrete" }
write(SeriesAnnotation.ANNOTATION) { DISCRETE }
}
)
}
Expand Down Expand Up @@ -75,12 +76,12 @@ class DiscreteScaleFromAnnotationChangeTest {
)
layers(
dict {
write(Layer.MAPPING, toOption(Aes.COLOR)) { varName }
write(MAPPING, toOption(Aes.COLOR)) { varName }
write(Meta.DATA_META, SeriesAnnotation.TAG) {
list(
dict {
write(SeriesAnnotation.VARIABLE) { varName }
write(SeriesAnnotation.ANNOTATION) { "discrete" }
write(SeriesAnnotation.ANNOTATION) { DISCRETE }
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

package jetbrains.datalore.plot.config.transform.encode

import jetbrains.datalore.plot.config.Option.Plot
import jetbrains.datalore.plot.config.Option.PlotBase.DATA
import jetbrains.datalore.plot.config.transform.PlotSpecTransform
import jetbrains.datalore.plot.config.transform.PlotSpecTransformUtil
import jetbrains.datalore.plot.config.transform.SpecChange

object DataSpecEncodeTransforms {
private fun addDataChanges(builder: PlotSpecTransform.Builder, change: SpecChange, isGGBunch: Boolean): PlotSpecTransform.Builder {

val specSelectors = PlotSpecTransformUtil.getPlotAndLayersSpecSelectors(isGGBunch, Plot.DATA)
val specSelectors = PlotSpecTransformUtil.getPlotAndLayersSpecSelectors(isGGBunch, DATA)
for (specSelector in specSelectors) {
builder.change(specSelector, change)
}
Expand Down
7 changes: 5 additions & 2 deletions plot-config/src/jvmTest/kotlin/plot/config/TestUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package jetbrains.datalore.plot.config

import jetbrains.datalore.plot.config.Option.PlotBase.DATA
import kotlin.test.assertEquals

object TestUtil {
Expand All @@ -18,26 +19,28 @@ object TestUtil {
}

fun getPlotData(plotSpec: Map<String, Any>): Map<String, Any> {
return getMap(plotSpec, Option.Plot.DATA)
return getMap(plotSpec, DATA)
}

fun getLayerData(plotSpec: Map<String, Any>, layer: Int): Map<String, Any> {
return layerDataList(plotSpec)[layer]
}

private fun layerDataList(plotSpec: Map<String, Any>): List<Map<String, Any>> {
@Suppress("UNCHECKED_CAST")
val layers = plotSpec[Option.Plot.LAYERS] as List<Map<String, Any>>

val result = ArrayList<Map<String, Any>>()
for (layer in layers) {
val layerData = HashMap(getMap(layer, Option.Layer.DATA))
val layerData = HashMap(getMap(layer, DATA))
result.add(layerData)
}
return result
}

private fun getMap(opts: Map<String, Any>, key: String): Map<String, Any> {
val map = opts[key]
@Suppress("UNCHECKED_CAST")
return map as? Map<String, Any> ?: emptyMap()
}

Expand Down
Loading