aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/scalam/plotting/DataSet.scala
blob: 381bf0073efbfc2738ae4fe9dfc3839e6818bd0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package scalam.plotting

import scalam.m.ast.Identifier
import scalax.file.Path
import breeze.linalg.{ Vector, DenseVector }
import scalam.io._
import scala.language.implicitConversions

case class DataSet(points: Seq[(Double, Double)], label: String, name: String) {

  lazy val (xs, ys) = points.unzip

  def save(path: Path) = {
    path.createFile(createParents = true, failIfExists = false)
    for (processor <- path.outputProcessor; out = processor.asOutput)
      for ((x, y) <- points) out.write(x + " " + y + "\n")
  }

}

object DataSet {

  def apply(points: Seq[(Double, Double)], label: String) = new DataSet(points, label, Identifier.makeValid(label))
  
  def apply(xs: Seq[Double], ys: Seq[Double], label: String) = new DataSet(xs zip ys, label, Identifier.makeValid(label))
  
  def apply(xs: Seq[Double], ys: Seq[Double], label: String, name: String) = new DataSet(xs zip ys, label, name)

  implicit def dataSetIsSaveable(ds: DataSet) = new Saveable {
    def save(out: scalax.file.Path) = for ((x, y) <- ds.points) yield out.write(x + " " + y + "\n")
  }

  implicit def dataSetIsLoadable = new Loadable[DataSet] {
    def load(in: scalax.file.Path) = new DataSet(Seq(), "", "")
  }

}