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 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
Next Next commit
Add random seed into position_jitter()
  • Loading branch information
RYangazov committed Nov 6, 2023
commit e962602a3bcdbd84161784f9517e81e2533a973b
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class JitterDodgePos(aesthetics: Aesthetics, groupCount: Int, width: Double?, ji
private val myDodgePosHelper: PositionAdjustment

init {
myJitterPosHelper = JitterPos(jitterWidth, jitterHeight)
myJitterPosHelper = JitterPos(jitterWidth, jitterHeight, seed = null)
myDodgePosHelper = DodgePos(aesthetics, groupCount, width)
}

Expand Down
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?) : PositionAdjustment {
alshan marked this conversation as resolved.
Show resolved Hide resolved

//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