Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support scala 2.13.x #132

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ licenses += ("BSD Simplified", url("https://opensource.org/licenses/BSD-3-C
organizationName := "CiBO Technologies, Inc."
organizationHomepage := Some(url("https://www.cibotechnologies.com"))

crossScalaVersions := Seq("2.12.12")
crossScalaVersions := Seq("2.13.4", "2.12.12")
scalaVersion := crossScalaVersions.value.head

sonatypeProjectHosting := Some(GitHubHosting("cibotech", "scalastan", "devops@cibotechnologies.com"))
Expand Down Expand Up @@ -40,3 +40,9 @@ libraryDependencies ++= Seq(
"ch.qos.logback" % "logback-classic" % "1.2.3" % "test,it"
)

libraryDependencies ++= {
if (scalaVersion.value.startsWith("2.13."))
Seq("org.scala-lang.modules" %% "scala-parallel-collections" % "1.0.2")
else
Seq()
}
17 changes: 17 additions & 0 deletions src/main/scala/com/cibo/scalastan/CompatParColls.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.cibo.scalastan

object CompatParColls {
val Converters = {
import Compat._

{
import scala.collection.parallel._

CollectionConverters
}
}

object Compat {
object CollectionConverters
}
}
2 changes: 1 addition & 1 deletion src/main/scala/com/cibo/scalastan/StanModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ trait StanModel extends StanCodeBlock with StanContext { self =>

private[scalastan] lazy val generate: StanFunctionDeclaration = StanFunctionDeclaration(
result,
inputs,
inputs.toSeq,
_code.results
)
}
Expand Down
20 changes: 10 additions & 10 deletions src/main/scala/com/cibo/scalastan/StanProgramBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ class StanProgramBuilder {
// Leave the current scope (collapsing the statements collected using the specified function).
def leave(f: Seq[StanStatement] => StanStatement): Unit = {
require(stack.size > 1)
val result = f(stack.remove(stack.size - 1))
val result = f(stack.remove(stack.size - 1).toSeq)
result.export(this)
stack.last += result
}

// Special handling for "else if" statements.
def handleElseIf(cond: StanValue[StanInt]): Unit = {
cond.export(this)
val inside = stack.remove(stack.size - 1)
val inside = stack.remove(stack.size - 1).toSeq
val ifStatement = stack.last.remove(stack.last.size - 1).asInstanceOf[StanIfStatement]
stack.last += ifStatement.copy(ifStatement.conds :+ (cond, StanBlock(inside)))
}

// Special handling for "else" statements.
def handleElse(): Unit = {
val inside = stack.remove(stack.size - 1)
val inside = stack.remove(stack.size - 1).toSeq
val ifStatement = stack.last.remove(stack.last.size - 1).asInstanceOf[StanIfStatement]
stack.last += ifStatement.copy(otherwise = Some(StanBlock(inside)))
}
Expand Down Expand Up @@ -110,16 +110,16 @@ class StanProgramBuilder {

lazy val results: StanBlock = {
require(stack.size == 1)
StanBlock(stack.last)
StanBlock(stack.last.toSeq)
}

lazy val program: StanProgram = StanProgram(
dataValues,
parameterValues,
functions.map(_.generate),
transformedData.map(_.generate),
transformedParameters.map(_.generate),
generatedQuantities.map(_.generate),
dataValues.toSeq,
parameterValues.toSeq,
functions.map(_.generate).toSeq,
transformedData.map(_.generate).toSeq,
transformedParameters.map(_.generate).toSeq,
generatedQuantities.map(_.generate).toSeq,
results
)

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/cibo/scalastan/StanResults.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
package com.cibo.scalastan

import java.io.{PrintStream, PrintWriter}

import com.cibo.scalastan.ast.{StanDataDeclaration, StanParameterDeclaration}
import com.cibo.scalastan.CompatParColls.Converters._

import scala.util.Try

Expand Down
1 change: 1 addition & 0 deletions src/main/scala/com/cibo/scalastan/StanType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package com.cibo.scalastan

import com.cibo.scalastan.ast.{StanUnknownInt, StanValue}
import com.cibo.scalastan.CompatParColls.Converters._

sealed trait StanType {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ abstract class StanAnalysis[T](root: StanStatement) {
val outLinks: Map[Int, Set[Int]] = getControlFlow(root)
val inLinks: Map[Int, Set[Int]] = outLinks.toSeq.flatMap { case (parent, children) =>
children.map(child => child -> parent)
}.groupBy(_._1).mapValues(vs => vs.map(_._2).toSet)
}.groupBy(_._1).mapValues(vs => vs.map(_._2).toSet).toMap

val inputs: Map[Int, Set[Int]] = if (forward) inLinks else outLinks // Inputs to a statement.
val outputs: Map[Int, Set[Int]] = if (forward) outLinks else inLinks // Outputs of a statement.
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/cibo/scalastan/data/DataSource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ case class DataSource private[data] (
) {

private lazy val raw: Map[String, DataValue] = rawSeq.map(v => v.name -> v).toMap
private lazy val reverseEnumerations: Map[String, Map[Int, String]] = enumerations.mapValues(_.map(_.swap).toMap)
private lazy val reverseEnumerations: Map[String, Map[Int, String]] = enumerations.mapValues(_.map(_.swap).toMap).toMap

/** Get the names of the data values. */
def names: Set[String] = raw.keySet
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/com/cibo/scalastan/run/CmdStanRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ package com.cibo.scalastan.run

import java.io._
import java.nio.file._

import com.cibo.scalastan._
import com.cibo.scalastan.CompatParColls.Converters._
import com.typesafe.scalalogging.LazyLogging

import scala.collection.JavaConverters._
Expand Down Expand Up @@ -223,9 +223,9 @@ class CmdStanRunner(
}
}.seq

val parameterChains = results.flatMap(_.iterations).groupBy(_._1).mapValues(_.map(_._2))
val parameterChains = results.flatMap(_.iterations).groupBy(_._1).mapValues(_.map(_._2)).toVector
val massMatrices = results.map(_.massMatrix)
StanResults(parameterChains, massMatrices, compiledModel, method)
StanResults(parameterChains.toMap, massMatrices, compiledModel, method)
}
}

2 changes: 1 addition & 1 deletion src/test/scala/com/cibo/scalastan/ScalaStanBaseSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ trait ScalaStanBaseSpec extends FunSpec with Matchers {
val mappedData: Map[String, Vector[Vector[Double]]] = data.flatten.groupBy(_._1).mapValues { grouped =>
val iterations = grouped.map { case (k, v) => v }
Vector.fill(chains)(iterations)
}
}.toMap
StanResults(mappedData, Vector.empty, model, method)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/com/cibo/scalastan/StanResultsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class StanResultsSpec extends ScalaStanBaseSpec {

val mappedData: Map[String, Vector[Vector[Double]]] = Seq(testData1, testData2).flatten.groupBy(_._1).mapValues(
grouped => Vector(grouped.map{ case(k, v) => v }.toVector)
)
).toMap
val results = StanResults(mappedData, Vector.empty, model, RunMethod.Sample())

describe("parameters") {
Expand Down