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

Jitter reproducibility in geom_jitter() #920

Merged
merged 7 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion future_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
### Changed

### Fixed

- Jitter reproducibility in geom_jitter [[#911](https://github.com/JetBrains/lets-plot/issues/911)].
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ import org.jetbrains.letsPlot.core.plot.base.GeomContext
import org.jetbrains.letsPlot.core.plot.base.PositionAdjustment
import kotlin.random.Random

internal class JitterPos(width: Double?, height: Double?) : PositionAdjustment {
internal class JitterPos(width: Double?, height: Double?, seed: Long? = null) : PositionAdjustment {

//uniform distribution
private val myWidth: Double
private val myHeight: Double
private val random: Random = seed?.let { Random(seed) } ?: Random.Default

init {
myWidth = width ?: DEF_JITTER_WIDTH
myHeight = height ?: DEF_JITTER_HEIGHT
}

override fun translate(v: DoubleVector, p: DataPointAesthetics, ctx: GeomContext): DoubleVector {
val x = (2 * Random.nextDouble() - 1) * myWidth * ctx.getResolution(Aes.X)
val y = (2 * Random.nextDouble() - 1) * myHeight * ctx.getResolution(Aes.Y)
val x = (2 * random.nextDouble() - 1) * myWidth * ctx.getResolution(Aes.X)
val y = (2 * random.nextDouble() - 1) * myHeight * ctx.getResolution(Aes.Y)
return v.add(DoubleVector(x, y))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ object PositionAdjustments {
return FillPos(aesthetics, vjust, stackingMode)
}

fun jitter(width: Double?, height: Double?): PositionAdjustment {
return JitterPos(width, height)
fun jitter(width: Double?, height: Double?, seed: Long?): PositionAdjustment {
return JitterPos(width, height, seed)
}

fun nudge(width: Double?, height: Double?): PositionAdjustment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ abstract class PosProvider {
}
}

fun jitter(width: Double?, height: Double?): PosProvider {
fun jitter(width: Double?, height: Double?, seed: Long?): PosProvider {
return object : PosProvider() {
override fun createPos(ctx: PosProviderContext): PositionAdjustment {
return PositionAdjustments.jitter(width, height)
return PositionAdjustments.jitter(width, height, seed)
}

override fun handlesGroups(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class GeomProto(val geomKind: GeomKind) {
Meta.NAME to PosProto.JITTER,
Pos.Jitter.WIDTH to layerOptions.getDouble(Geom.Jitter.WIDTH),
Pos.Jitter.HEIGHT to layerOptions.getDouble(Geom.Jitter.HEIGHT),
Pos.Jitter.SEED to layerOptions.getLong(Geom.Jitter.SEED)
)

Y_DOT_PLOT -> if (layerOptions.hasOwn(Geom.YDotplot.STACKGROUPS) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ object Option {
object Jitter {
const val WIDTH = "width"
const val HEIGHT = "height"
const val SEED = "seed"
}

object Step {
Expand Down Expand Up @@ -467,6 +468,7 @@ object Option {
object Jitter {
const val WIDTH = "width"
const val HEIGHT = "height"
const val SEED = "seed"
}

object Nudge {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ internal object PosProto {
FILL -> configureFillPosition(opts)
JITTER -> PosProvider.jitter(
opts.getDouble(Pos.Jitter.WIDTH),
opts.getDouble(Pos.Jitter.HEIGHT)
opts.getDouble(Pos.Jitter.HEIGHT),
opts.getLong(Pos.Jitter.SEED)
)

NUDGE -> PosProvider.nudge(
Expand Down
9 changes: 6 additions & 3 deletions python-package/lets_plot/plot/pos.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def position_dodgev(height=None):
return _pos('dodgev', height=height)


def position_jitter(width=None, height=None):
def position_jitter(width=None, height=None, seed=None):
"""
Adjust position by assigning random noise to points. Better for discrete values.

Expand All @@ -110,6 +110,9 @@ def position_jitter(width=None, height=None):
Jittering height.
The value of height is relative and typically ranges between 0 and 0.5.
Values that are greater than 0.5 lead to overlapping of the points.
seed : int
A random seed to make the jitter reproducible.
If None (the default value), the seed is initialised with a random value.

Returns
-------
Expand Down Expand Up @@ -137,10 +140,10 @@ def position_jitter(width=None, height=None):
ggplot({'x': x, 'y': y, 'c': c}, aes('x', 'y')) + \\
geom_point(aes(fill='c'), show_legend=False, \\
size=8, alpha=.5, shape=21, color='black', \\
position=position_jitter(width=.2, height=.2))
position=position_jitter(width=.2, height=.2, seed=42))

"""
return _pos('jitter', width=width, height=height)
return _pos('jitter', width=width, height=height, seed=seed)


def position_nudge(x=None, y=None):
Expand Down