From 6751d8e585abc57a77d6abe4f230ca66e9b4cf07 Mon Sep 17 00:00:00 2001 From: avi-stripe Date: Tue, 5 Mar 2019 14:52:12 -0800 Subject: [PATCH] writeCSV (#326) --- .../scala/com/stripe/rainier/repl/package.scala | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rainier-core/src/main/scala/com/stripe/rainier/repl/package.scala b/rainier-core/src/main/scala/com/stripe/rainier/repl/package.scala index db1de6693..15406891a 100644 --- a/rainier-core/src/main/scala/com/stripe/rainier/repl/package.scala +++ b/rainier-core/src/main/scala/com/stripe/rainier/repl/package.scala @@ -1,6 +1,7 @@ package com.stripe.rainier import com.stripe.rainier.sampler._ +import java.io._ package object repl { def plot1D[N](seq: Seq[N])(implicit num: Numeric[N]): Unit = { @@ -15,5 +16,20 @@ package object repl { .mkString("\n")) } + def writeCSV(path: String, seq: Seq[Map[String, Double]]): Unit = { + val fieldNames = seq.map(_.keys.toSet).reduce(_ ++ _).toList + val pw = new PrintWriter(new File(path)) + pw.write(fieldNames.mkString(",")) + seq.foreach { row => + pw.write("\n") + fieldNames.tail.foreach { f => + pw.write(row.get(f).map(_.toString).getOrElse("")) + pw.write(",") + } + pw.write(row.get(fieldNames.head).map(_.toString).getOrElse("")) + } + pw.close + } + implicit val rng: RNG = RNG.default }