Commit 7a001bba authored by Romain Reuillon's avatar Romain Reuillon
Browse files

Merge branch 'dev' of ssh://gitlab.openmole.org:10000/openmole/openmole into dev

parents 98334e7b efa240b9
Pipeline #1193 failed with stages
in 26 minutes and 24 seconds
......@@ -521,7 +521,7 @@ lazy val fileSource = OsgiProject(pluginDir, "org.openmole.plugin.source.file",
/* Task */
def allTask = Seq(toolsTask, external, netLogo, netLogo5, netLogo6, jvm, scala, template, systemexec, container, r, scilab, python, gama, cormas, spatial, timing)
def allTask = Seq(toolsTask, external, netLogo, netLogo5, netLogo6, jvm, scala, template, systemexec, container, r, scilab, python, julia, gama, cormas, spatial, timing)
lazy val toolsTask = OsgiProject(pluginDir, "org.openmole.plugin.task.tools", imports = Seq("*")) dependsOn (openmoleDSL) settings (pluginSettings: _*)
......@@ -552,6 +552,8 @@ lazy val scilab = OsgiProject(pluginDir, "org.openmole.plugin.task.scilab", impo
lazy val python = OsgiProject(pluginDir, "org.openmole.plugin.task.python", imports = Seq("*")) dependsOn(container, json) settings (pluginSettings: _*)
lazy val julia = OsgiProject(pluginDir, "org.openmole.plugin.task.julia", imports = Seq("*")) dependsOn(container, json) settings (pluginSettings: _*)
lazy val gama = OsgiProject(pluginDir, "org.openmole.plugin.task.gama", imports = Seq("*")) dependsOn (container) settings (pluginSettings: _*)
lazy val cormas = OsgiProject(pluginDir, "org.openmole.plugin.task.cormas", imports = Seq("*")) dependsOn (container, json) settings (pluginSettings: _*) settings(
......
package org.openmole.plugin.task.julia
import org.openmole.core.highlight.HighLight
import org.openmole.core.pluginregistry.PluginRegistry
import org.osgi.framework.{ BundleActivator, BundleContext }
class Activator extends BundleActivator {
override def stop(context: BundleContext): Unit =
PluginRegistry.unregister(this)
override def start(context: BundleContext): Unit = {
import org.openmole.core.highlight.HighLight._
val keyWords: Vector[HighLight] =
Vector(
TaskHighLight(objectName(JuliaTask))
)
PluginRegistry.register(this, Vector(this.getClass.getPackage), highLight = keyWords)
}
}
\ No newline at end of file
package org.openmole.plugin.task.julia
import monocle.macros._
import org.openmole.core.dsl._
import org.openmole.core.dsl.extension._
import org.openmole.core.fileservice.FileService
import org.openmole.core.networkservice.NetworkService
import org.openmole.core.preference.Preference
import org.openmole.core.serializer.SerializerService
import org.openmole.core.threadprovider.ThreadProvider
import org.openmole.core.workflow.builder._
import org.openmole.core.workflow.task.TaskExecutionContext
import org.openmole.core.workflow.tools.OptionalArgument
import org.openmole.core.workflow.validation.ValidateTask
import org.openmole.core.workspace.{TmpDirectory, Workspace}
import org.openmole.plugin.task.container._
import org.openmole.plugin.task.external._
import org.openmole.plugin.tool.json._
import org.openmole.tool.outputredirection.OutputRedirection
import org.openmole.plugin.task.container
/**
* https://docs.julialang.org/en/v1/base/numbers/#Base.isone
*
* https://gist.github.com/silgon/0ba43e00e0749cdf4f8d244e67cd9d6a
*/
object JuliaTask {
implicit def isTask: InputOutputBuilder[JuliaTask] = InputOutputBuilder(JuliaTask.config)
implicit def isExternal: ExternalBuilder[JuliaTask] = ExternalBuilder(JuliaTask.external)
implicit def isInfo = InfoBuilder(info)
implicit def isMapped = MappedInputOutputBuilder(JuliaTask.mapped)
def installCommands(install: Seq[String], libraries: Seq[String]): Vector[String] = {
(install ++ Seq("julia -e 'using Pkg; Pkg.add.([ " + libraries.map { l "\""+l+"\"" }.mkString(",")+"])'" )).toVector
}
def apply(
script: RunnableScript,
arguments: OptionalArgument[String] = None,
libraries: Seq[String] = Seq.empty,
install: Seq[String] = Seq.empty,
workDirectory: OptionalArgument[String] = None,
hostFiles: Seq[HostFile] = Vector.empty,
environmentVariables: Seq[EnvironmentVariable] = Vector.empty,
errorOnReturnValue: Boolean = true,
returnValue: OptionalArgument[Val[Int]] = None,
stdOut: OptionalArgument[Val[String]] = None,
stdErr: OptionalArgument[Val[String]] = None,
containerSystem: ContainerSystem = ContainerSystem.default,
installContainerSystem: ContainerSystem = ContainerSystem.default)(implicit name: sourcecode.Name, definitionScope: DefinitionScope, newFile: TmpDirectory, workspace: Workspace, preference: Preference, fileService: FileService, threadProvider: ThreadProvider, outputRedirection: OutputRedirection, networkService: NetworkService, serializerService: SerializerService) = {
new JuliaTask(
script = script,
arguments = arguments.option,
image = ContainerTask.prepare(installContainerSystem, DockerImage("julia"), installCommands(install, Seq("JSON")++libraries)),
errorOnReturnValue = errorOnReturnValue,
returnValue = returnValue,
stdOut = stdOut,
stdErr = stdErr,
hostFiles = hostFiles,
environmentVariables = environmentVariables,
containerSystem = containerSystem,
config = InputOutputConfig(),
external = External(),
info = InfoConfig(),
mapped = MappedInputOutputConfig()
) set (outputs += (Seq(returnValue.option, stdOut.option, stdErr.option).flatten: _*))
}
}
@Lenses case class JuliaTask(
script: RunnableScript,
image: PreparedImage,
arguments: Option[String],
errorOnReturnValue: Boolean,
returnValue: Option[Val[Int]],
stdOut: Option[Val[String]],
stdErr: Option[Val[String]],
hostFiles: Seq[HostFile],
environmentVariables: Seq[EnvironmentVariable],
containerSystem: ContainerSystem,
config: InputOutputConfig,
external: External,
info: InfoConfig,
mapped: MappedInputOutputConfig) extends Task with ValidateTask {
lazy val containerPoolKey = ContainerTask.newCacheKey
override def validate = container.validateContainer(Vector(), environmentVariables, external)
override def process(executionContext: TaskExecutionContext) = FromContext { p
import org.json4s.jackson.JsonMethods._
import p._
import Mapped.noFile
def writeInputsJSON(file: File): Unit = {
def values = noFile(mapped.inputs).map { m => p.context(m.v) }
file.content = compact(render(toJSONValue(values.toArray)))
}
def readOutputJSON(file: File) = {
import org.json4s._
import org.json4s.jackson.JsonMethods._
val outputValues = parse(file.content)
(outputValues.asInstanceOf[JArray].arr zip noFile(mapped.outputs).map(_.v)).map { case (jvalue, v) jValueToVariable(jvalue, v) }
}
def inputMapping(dicoName: String): String =
noFile(mapped.inputs).map {
case m s"${m.name} = $dicoName[\"${m.name}\"]"
}.mkString("\n")
def outputMapping: String =
s"""Dict(${noFile(mapped.outputs).map { m ⇒ "\""+m.name+"\" => "+m.v }.mkString(",")})"""
val resultContext: Context = p.newFile.withTmpFile("script", ".jl") { scriptFile
p.newFile.withTmpFile("inputs", ".json") { jsonInputs
def inputArrayName = "_generateddata_"
def scriptName = "_generatescript_.jl"
def inputJSONName = "_inputs_.json"
def outputJSONName = "_outputs_.json"
writeInputsJSON(jsonInputs)
scriptFile.content =
s"""
|import JSON
|$inputArrayName = "/$inputJSONName" |> open |> JSON.parse
|${inputMapping(inputArrayName)}
|${RunnableScript.content(script)}
|write(open("/$outputJSONName","w"),JSON.json($outputMapping))
""".stripMargin
val outputFile = Val[File]("outputFile", Namespace("JuliaTask"))
val argumentsValue = arguments.map(" " + _).getOrElse("")
def containerTask =
ContainerTask(
containerSystem = containerSystem,
image = image,
command = s"julia $scriptName" + argumentsValue,
workDirectory = None,
relativePathRoot = None,
errorOnReturnValue = errorOnReturnValue,
returnValue = returnValue,
hostFiles = hostFiles,
environmentVariables = environmentVariables,
reuseContainer = true,
stdOut = stdOut,
stdErr = stdErr,
config = InputOutputConfig(),
external = external,
info = info,
containerPoolKey = containerPoolKey) set (
resources += (scriptFile, scriptName, true),
resources += (jsonInputs, inputJSONName, true),
outputFiles += (outputJSONName, outputFile),
Mapped.files(mapped.inputs).map { case m inputFiles +=[ContainerTask] (m.v, m.name, true) },
Mapped.files(mapped.outputs).map { case m outputFiles +=[ContainerTask] (m.name, m.v) }
)
val resultContext = containerTask.process(executionContext).from(p.context)(p.random, p.newFile, p.fileService)
resultContext ++ readOutputJSON(resultContext(outputFile))
}
}
resultContext
}
}
package org.openmole.tool.statistics
import org.openmole.tool.types.ToDouble
trait StatisticsPackage extends Stat { stat
implicit class StatisticIterableOfDoubleDecorator[T](s: Seq[T])(implicit td: ToDouble[T]) {
def median: Double = stat.median(s.map(td.apply))
def medianAbsoluteDeviation = stat.medianAbsoluteDeviation(s.map(td.apply))
def average = stat.average(s.map(td.apply))
def variance = stat.variance(s.map(td.apply))
def meanSquaredError = stat.meanSquaredError(s.map(td.apply))
def standardDeviation = stat.standardDeviation(s.map(td.apply))
def rootMeanSquaredError = stat.rootMeanSquaredError(s.map(td.apply))
def normalize = stat.normalize(s.map(td.apply))
def absoluteDistance[T2](to: Seq[T2])(implicit td2: ToDouble[T2]) = stat.absoluteDistance(s.map(td.apply), to.map(td2.apply))
def squareDistance[T2](to: Seq[T2])(implicit td2: ToDouble[T2]) = stat.squareDistance(s.map(td.apply), to.map(td2.apply))
def dynamicTimeWarpingDistance[T2](to: Seq[T2], fast: Boolean = true)(implicit td2: ToDouble[T2]) = stat.dynamicTimeWarpingDistance(s.map(td.apply), to.map(td2.apply))
}
implicit def statisticArrayOfDoubleDecorator[T: ToDouble](s: Array[T]) = new StatisticIterableOfDoubleDecorator(s.toVector)
}
......@@ -16,31 +16,4 @@
*/
package org.openmole.tool
package statistics {
import org.openmole.tool.types.ToDouble
trait StatisticsPackage extends Stat { stat
implicit class StatisticIterableOfDoubleDecorator[T](s: Seq[T])(implicit td: ToDouble[T]) {
def median: Double = stat.median(s.map(td.apply))
def medianAbsoluteDeviation = stat.medianAbsoluteDeviation(s.map(td.apply))
def average = stat.average(s.map(td.apply))
def variance = stat.variance(s.map(td.apply))
def meanSquaredError = stat.meanSquaredError(s.map(td.apply))
def standardDeviation = stat.standardDeviation(s.map(td.apply))
def rootMeanSquaredError = stat.rootMeanSquaredError(s.map(td.apply))
def normalize = stat.normalize(s.map(td.apply))
def absoluteDistance[T2](to: Seq[T2])(implicit td2: ToDouble[T2]) = stat.absoluteDistance(s.map(td.apply), to.map(td2.apply))
def squareDistance[T2](to: Seq[T2])(implicit td2: ToDouble[T2]) = stat.squareDistance(s.map(td.apply), to.map(td2.apply))
def dynamicTimeWarpingDistance[T2](to: Seq[T2], fast: Boolean = true)(implicit td2: ToDouble[T2]) = stat.dynamicTimeWarpingDistance(s.map(td.apply), to.map(td2.apply))
}
implicit def statisticArrayOfDoubleDecorator[T: ToDouble](s: Array[T]) = new StatisticIterableOfDoubleDecorator(s.toVector)
}
}
package object statistics extends StatisticsPackage
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment