Skip to content

Commit

Permalink
jupyter support for evilplot (#334)
Browse files Browse the repository at this point in the history
* include almond jupyter support in rainier-plot

* fix build

* aesthetics

* warnings

* formatting

* example ipynb
  • Loading branch information
avibryant authored Mar 5, 2019
1 parent 0ec119e commit 5c1b39c
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ target/
.DS_Store
.bloop/
.metals/
.ipynb_checkpoints/
99 changes: 99 additions & 0 deletions Example.ipynb

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ lazy val V = new {
val scalacheck = "1.14.0"
val scalatest = "3.0.5"
val flogger = "0.3.1"
val almond = "0.3.0"
val scala = "2.12.8"
val shadedAsm = "0.2.1"
}

Expand All @@ -95,8 +97,15 @@ lazy val rainierPlot = project.
dependsOn(rainierCore).
settings(commonSettings).
settings(
resolvers += Resolver.bintrayRepo("cibotech", "public"),
libraryDependencies += "com.cibo" %% "evilplot" % V.evilplot)
resolvers ++=
Seq(
Resolver.bintrayRepo("cibotech", "public"),
"jitpack" at "https://jitpack.io"),
libraryDependencies ++=
Seq(
"com.cibo" %% "evilplot" % V.evilplot,
"sh.almond" %% "interpreter-api" % V.almond)
)

lazy val rainierCats = project.
in(file("rainier-cats")).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ object EvilTracePlot {
()
}

/**
* Render plots to a PNG byte array
*
* @param plots A collection of plots, possibly produced by `traces` or `pairs`
* @param extent The size of the image to be rendered
*/
def renderBytes(plots: List[List[com.cibo.evilplot.plot.Plot]],
extent: com.cibo.evilplot.geometry.Extent): Array[Byte] = {
val baos = new java.io.ByteArrayOutputStream
javax.imageio.ImageIO
.write(
com.cibo.evilplot.plot.Facets(plots).render(extent).asBufferedImage,
"png",
baos)
val array = baos.toByteArray
baos.close
array
}

/**
* Autocorrelation function
*
Expand Down
27 changes: 27 additions & 0 deletions rainier-plot/src/main/scala/com/stripe/rainier/plot/Jupyter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.stripe.rainier.plot

import almond.interpreter.api._
import com.cibo.evilplot.geometry._

object Jupyter {
def traces(out: Seq[Map[String, Double]],
truth: Map[String, Double] = Map(),
lagMax: Int = 40,
numBars: Int = 50)(implicit outputHandler: OutputHandler): Unit =
DisplayData
.png(
EvilTracePlot.renderBytes(
EvilTracePlot.traces(out, truth, lagMax, numBars),
Extent(1200, out.head.keys.size * 300.0)))
.show()

def pairs(out: Seq[Map[String, Double]],
truth: Map[String, Double] = Map(),
numBars: Int = 30)(implicit outputHandler: OutputHandler): Unit =
DisplayData
.png(
EvilTracePlot.renderBytes(EvilTracePlot.pairs(out, truth, numBars),
Extent(out.head.keys.size * 300.0,
out.head.keys.size * 300.0)))
.show()
}

0 comments on commit 5c1b39c

Please sign in to comment.