aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Odersky <jodersky@gmail.com>2012-11-01 16:53:55 +0100
committerJakob Odersky <jodersky@gmail.com>2012-11-01 16:53:55 +0100
commitb4f8c4d00673c05dbb5c738a131f4c8e5f69c2f8 (patch)
tree8f5dd68499a91ea02739efd7e4cd5dad4fc3980e
parentfd09111c47965b9af68879d3613e76695f8e7f0b (diff)
downloadscalam-b4f8c4d00673c05dbb5c738a131f4c8e5f69c2f8.tar.gz
scalam-b4f8c4d00673c05dbb5c738a131f4c8e5f69c2f8.tar.bz2
scalam-b4f8c4d00673c05dbb5c738a131f4c8e5f69c2f8.zip
add generic I/O support for plots and remove old save() method
-rw-r--r--src/main/scala/scalam/plotting/Plot.scala51
-rw-r--r--src/main/scala/scalam/plotting/Plotter.scala8
2 files changed, 19 insertions, 40 deletions
diff --git a/src/main/scala/scalam/plotting/Plot.scala b/src/main/scala/scalam/plotting/Plot.scala
index 45d755e..2a50d70 100644
--- a/src/main/scala/scalam/plotting/Plot.scala
+++ b/src/main/scala/scalam/plotting/Plot.scala
@@ -6,6 +6,7 @@ import scalax.file.Path
import scalam.plotting.styles._
import scala.collection.mutable.Map
import scala.collection.mutable.ListBuffer
+import scalam.io.Saveable
class Plot(
val dataSets: Seq[DataSet],
@@ -15,11 +16,7 @@ class Plot(
grid: Boolean = true,
legend: Boolean = true,
fontSize: Int = 10,
- styles: Seq[Style[StyleElement]] = Seq(),
- name: String = "plot" + Plot.next) {
-
- val directory = Path(name)
- val localPlotFile = Path("results.m")
+ styles: Seq[Style[StyleElement]] = Seq()) {
def preamble = {
val df = new java.text.SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss")
@@ -31,7 +28,7 @@ class Plot(
class RichDataSet(val id: Identifier, val localPath: Path, val underlying: DataSet)
- def richDataSets = {
+ val richDataSets = {
val knownIds = Map[Identifier, Int]()
def toRich(dataSet: DataSet) = {
val firstId = Identifier(dataSet.name)
@@ -87,43 +84,24 @@ class Plot(
roots.toList
}
- def save() = {
- for (d <- richDataSets) d.underlying.save(directory / d.localPath)
-
- val plotFile = (directory / localPlotFile)
- plotFile.createFile(createParents = true, failIfExists = false)
- for (processor <- plotFile.outputProcessor; out = processor.asOutput) {
- for (p <- preamble) out.write(p.line + "\n")
- for (r <- roots) out.write(r.line + "\n")
- }
-
- }
-
- def run() = {
- Process(
- "matlab -nodesktop -nosplash -r " + localPlotFile.path.takeWhile(_ != '.'),
- directory.fileOption,
- "" -> "") #> (directory / "log.txt").fileOption.get run
- }
-
}
object Plot {
+ val PlotFileName = "results.m"
+
private[this] var counter = -1
private def next = { counter += 1; counter }
- private def randomDataSet(length: Int) = {
- import scala.util.Random
- val data = for (i <- 0 until length) yield (i * 1.0, Random.nextDouble() * 10)
- val name = "a"
- DataSet(data, name)
- }
-
- val ds = Seq(
- DataSet(Seq((0.0, 1.0), (1.0, 1.0), (2.0, 1.0), (3.0, 0.0), (4.0, 1.0), (5.0, 1.0)), "temperature"),
- DataSet(Seq((0.0, 0.0), (1.0, 1.0), (2.0, 4.0), (3.0, 9.0)), """\alpha""")) ++ (0 to 10).map(_ => randomDataSet(10))
+ implicit val plotIsSaveable = (plot: Plot) => new Saveable {
+ def save(path: scalax.file.Path) = {
+ val plotFile = (path / PlotFileName)
+ plotFile.createFile(createParents = true, failIfExists = false)
+ for (processor <- plotFile.outputProcessor; out = processor.asOutput)
+ for (r <- plot.roots) out.write(r.line + "\n")
- val test = new Plot(ds, "title", "x", "y")
+ for (d <- plot.richDataSets) d.underlying.save(path / d.localPath)
+ }
+ }
private object m {
import scalam.m.ast._
@@ -150,5 +128,4 @@ object Plot {
def legend(dataSets: Seq[DataSet]) =
Function(Identifier("legend"), dataSets.map(d => StringLiteral(d.label)): _*)
}
-
} \ No newline at end of file
diff --git a/src/main/scala/scalam/plotting/Plotter.scala b/src/main/scala/scalam/plotting/Plotter.scala
index 6d240d9..cdc1ef3 100644
--- a/src/main/scala/scalam/plotting/Plotter.scala
+++ b/src/main/scala/scalam/plotting/Plotter.scala
@@ -5,6 +5,7 @@ import scalam.m.ast._
import scalam.plotting.styles._
import scalam.m.interpretation.MInterpreter
import scalam.m.interpretation.MatlabInterpreter
+import scalax.file.Path
trait Plotter {
import Plotter._
@@ -14,9 +15,10 @@ trait Plotter {
lazy val interpreter: MInterpreter = new MatlabInterpreter(pwd)
def plot(dataSets: Seq[DataSet], title: String, x: String, y: String, grid: Boolean = true, legend: Boolean = true)(implicit styles: Seq[Style[StyleElement]] = defaultStyles, fontSize: FontSize = defaultFontSize) = {
- val p = new Plot(dataSets, title, x, y, grid, legend, styles = styles, fontSize = fontSize.fontSize)
- p.save()
- val s = Function(Identifier("run"), StringLiteral((p.directory / p.localPlotFile).path))
+ val plot = new Plot(dataSets, title, x, y, grid, legend, styles = styles, fontSize = fontSize.fontSize)
+ val path = Path(Identifier.makeValid(title))
+ scalam.io.save(plot, path)
+ val s = Function(Identifier("run"), StringLiteral((path / Plot.PlotFileName).path))
println(s.line)
interpreter.evaluate(s)
}