JetBrains / lets-plot-kotlin

Grammar of Graphics for Kotlin
https://lets-plot.org/kotlin/
MIT License
419 stars 36 forks source link

Bug in empty plot: IndexOutOfBoundsException #194

Closed Unaimend closed 10 months ago

Unaimend commented 12 months ago

Sry for the not-so-descriptive title. Anyway, akin to R I tried following code which I copied and changed from [1]

%use lets-plot
fun meshgridPoints(x:List<Double>, y:List<Double>):List<Pair<Double, Double>> {
  val xSer = y.flatMap { x }
  var yInd = -1
  val ySer = y.flatMap {
      yInd++
      List(x.size) {y[yInd]}
  }
  return xSer.zip(ySer)
}

val gridPoints = meshgridPoints(
    generateSequence(0.0, {it + 6.0/50} ).takeWhile { it <= 5.0 }.toList(),
    generateSequence(0.0, {it + 6.0/40} ).takeWhile { it <= 5.0 }.toList())
fun g(x:Double, y:Double): Double {
    return sin(x).pow(10) + cos(10 + y * x) * cos(x)
}

val X = gridPoints.map { it.first }
val Y = gridPoints.map { it.second }
val Z = gridPoints.map { g(it.first, it.second) }

val p = letsPlot {x=X; y=Y; fill=Z}
p + geomTile() + scaleFillHue()
p

You might notice that I added a p in the last line, akin to R. This results in the following error

Internal error: IndexOutOfBoundsException: index: 0, size 0.

Removing the p again results in working code. I filed this issue due to the quite undescriptive error message.

Thank you for the nice library and your time Thomas

Discussion on Kotlin Slack: https://kotlinlang.slack.com/archives/C05333T208Y/p1689348252503279

[1] https://nbviewer.org/github/JetBrains/lets-plot-kotlin/blob/master/docs/examples/jupyter-notebooks/contours.ipynb

alshan commented 12 months ago

Thank you for reporting this. Please note however that by adding p as a last expr. in cell you discard the value of the expression next to the last. Effectively, you are trying to visualize letsPlot {x=X; y=Y; fill=Z} which is of course an empty plot (no layers).

ileasile commented 12 months ago

@alshan yes, there was normal error message before (api=4.2.0, lib=3.0.0, js=3.0.0 and api=4.3.0, lib=3.1.0, js=3.1.0 are ok, latest version is broken)

image
alshan commented 10 months ago

Fixed in 4.4.2