Skip to content

Instantly share code, notes, and snippets.

@RCura
Created February 16, 2013 23:36
Show Gist options
  • Save RCura/bbc50a890b2b70aba241 to your computer and use it in GitHub Desktop.
Save RCura/bbc50a890b2b70aba241 to your computer and use it in GitHub Desktop.
tmp
package fr.geocite.simprocess.hook.xylinecharts
import org.openmole.core.model.data._
import fr.geocite.simprocess.javafx.ui.{ filter, IVariableData }
import org.openmole.misc.exception.UserBadDataError
import filter.FilterLine
import scala.collection.JavaConversions._
import spray.json._
import scala.Array
import javafx.collections.FXCollections
import collection.{ mutable, GenTraversableOnce }
import collection.mutable.ListBuffer
import org.codehaus.groovy.runtime.ArrayUtil
case class XYZ_JSON(key: String, values: Seq[Map[String, Double]], color: String)
object MyJsonProtocol extends DefaultJsonProtocol {
implicit val colorFormat = jsonFormat3(XYZ_JSON)
}
class XYZVariableData[A <: Array[_]](var data: Iterable[Variable[A]]) extends IVariableData[A] {
// data contain list of Variable = list of column
var listFiltre = FXCollections.observableArrayList[FilterLine]()
var mappedVar = Map[String, String] { "X" -> ""; "Y" -> ""; "Z" -> "" }
def getRows(): Int = {
println("size = " + data.size)
println("name of var = " + this.filterByType[Array[Double]]().map { v ⇒ v.prototype.name })
println("name of var = " + this.filterByType[Array[Long]]().map { v ⇒ v.prototype.name })
println("name of var = " + this.filterByType[Array[java.lang.Long]]().map { v ⇒ v.prototype.name })
if (!data.isEmpty) {
val variable = data.toList.head
val varToTest: Array[_] = variable.prototype.`type`.runtimeClass.cast(variable.value).asInstanceOf[Array[_]]
varToTest.size
} else 0
}
// Each time pushData is called, i overwrite (old data) with (old data + new data)
def pushData(pushedData: Iterable[Variable[A]]) = {
//if data is empty, we push all new data by direct concatenation
if (data.isEmpty) {
data = data ++ pushedData
} else {
// We create a new iterable of Variables with contain concatenated values from
// recent and also old Variable
data = (data zip pushedData).map {
case (d, pd) ⇒
// We test that data arrive in the same order, so we need to verify headers !!
if (d.prototype.name == pd.prototype.name) {
val v: Array[_] = d.value.asInstanceOf[Array[_]] ++ pd.value.asInstanceOf[Array[_]]
Variable[A](d.prototype, v.asInstanceOf[A])
} else throw new UserBadDataError("variable headers are not equal to existant XYVariableData header ")
}
}
}
def createColumnNamesList: List[String] =
{
val columnsToAdd = new ListBuffer[String]
val tmpXCol = mappedVar.get("X") match {
case Some(x: String) ⇒ x
case _ ⇒ ""
}
if (tmpXCol != "") {
columnsToAdd += tmpXCol
} else { columnsToAdd += "empty" }
val tmpYCol = mappedVar.get("Y") match {
case Some(x: String) ⇒ x
case _ ⇒ ""
}
if (tmpYCol != "") {
columnsToAdd += tmpYCol
} else { columnsToAdd += "empty" }
val tmpZCol = mappedVar.get("Z") match {
case Some(x: String) ⇒ x
case _ ⇒ ""
}
if (tmpZCol != "") {
columnsToAdd += tmpZCol
} else { columnsToAdd += "empty" }
val tmpColumnsToAdd = new ListBuffer[String]
if (!listFiltre.isEmpty) {
listFiltre.foreach { tmpColumnsToAdd += _.name.toString }
tmpColumnsToAdd.distinct.foreach { columnsToAdd += }
}
return columnsToAdd.toList
}
def createFiltredColumnMap: List[Map[String, Array[Double]]] = {
val columnsList = createColumnNamesList
val emptyColumn: Map[String, Array[Double]] = Map("empty" -> Array.fill(getRows()) { 0.0 })
var filtredColumnMap = mutable.IndexedSeq[Map[String, Array[Double]]]()
columnsList.foreach {
colname ⇒
colname
if (colname == "empty") {
filtredColumnMap = filtredColumnMap :+ emptyColumn
} else {
val colVariable = getVariableFromColumn(colname.toString)
val colValue = get[Array[Double]](colVariable) match {
case Some(i: Array[Double]) ⇒ i
case None ⇒ throw new UserBadDataError("column type not equal ")
}
println("colname : " + colname + " / colValue : " + colValue.mkString(","))
filtredColumnMap = filtredColumnMap :+ Map(colname -> colValue)
}
}
return filtredColumnMap.toList
}
def applyFilters: List[Array[Double]] = {
val dataToFilter = createFiltredColumnMap
val columnsIndex = dataToFilter.map { _.keys.toList }.map { _.apply(0) }.zipWithIndex.toMap
var finalDataTable = dataToFilter.map { _.values.toList }.map { _.apply(0) }
listFiltre.foreach { currentFilter ⇒
currentFilter
val filterColumnName = currentFilter.name
val filterMin = currentFilter.getS1Value()
val filterMax = currentFilter.getS2Value()
val columnToFilterIndex = columnsIndex(filterColumnName)
val tmpData = finalDataTable.transpose.filter {
v ⇒ (v.apply(columnToFilterIndex) >= filterMin) && (v.apply(columnToFilterIndex) <= filterMax)
}
finalDataTable = tmpData.transpose.map { _.toArray }
}
return finalDataTable.transpose.map { _.toArray }.toList
}
import MyJsonProtocol._
def callDataJson(): String = {
val X = mappedVar.get("X") match {
case Some(x: String) ⇒ x
case _ ⇒ ""
}
val Y = mappedVar.get("Y") match {
case Some(x: String) ⇒ x
case _ ⇒ ""
}
val Z = mappedVar.get("Z") match {
case Some(x: String) ⇒ x
case _ ⇒ ""
}
if (X != "" || Y != "" || Z != "") {
val filtredTable = applyFilters.transpose.map { _.toArray }
val vX: Array[_] = if (filtredTable.apply(0).distinct == 0.0) { Array.fill(getRows()) { "" } } else { filtredTable.apply(0) }
val vY: Array[_] = if (filtredTable.apply(1).distinct == 0.0) { Array.fill(getRows()) { "" } } else { filtredTable.apply(1) }
val vZ: Array[_] = if (filtredTable.apply(2).distinct == 0.0) { Array.fill(getRows()) { "" } } else { filtredTable.apply(2) }
val XYZTable = List(vX, vY, vZ).transpose.distinct
val xyzMappedTable = XYZTable.map { v ⇒
Map[String, Double]("x" -> v(0), "y" -> v(1), "size" -> v(2))
}.toIndexedSeq
val JsonData = XYZ_JSON("1", xyzMappedTable, "#F88C12").toJson
return JsonData.toString()
} else
"{}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment