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

Fixed the case where no tuner is specified #18

Merged
merged 1 commit into from
Jun 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 40 additions & 29 deletions src/main/scala/io/github/jsarni/CaraModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,46 @@ final class CaraModel(yamlPath: String, dataset: Dataset[_], savePath: String, o
private def generateModel(caraPipeline: CaraPipeline) : Try[Pipeline] = Try {
val pipeline = caraPipeline.pipeline
val evaluator = caraPipeline.evaluator
val tuningStage = caraPipeline.tuner.tuningStage
val methodeName = "set" + caraPipeline.tuner.paramName
val model = tuningStage match {
case "CrossValidator" => {
val paramValue = caraPipeline.tuner.paramValue.toInt
val crossValidatorModel = new CrossValidator()
.setEstimator(pipeline)
.setEvaluator(evaluator)
.setEstimatorParamMaps(new ParamGridBuilder().build())
.setParallelism(2)

crossValidatorModel.getClass.getMethod(methodeName, paramValue.getClass )
.invoke(crossValidatorModel,paramValue.asInstanceOf[java.lang.Integer])

new Pipeline().setStages(Array(crossValidatorModel))
}
case "TrainValidationSplit" => {
val paramValue = caraPipeline.tuner.paramValue.toDouble
val validationSplitModel = new TrainValidationSplit()
.setEstimator(pipeline)
.setEvaluator(evaluator)
.setEstimatorParamMaps(new ParamGridBuilder().build())
.setParallelism(2)

validationSplitModel.getClass.getMethod(methodeName, paramValue.getClass )
.invoke(validationSplitModel,paramValue.asInstanceOf[java.lang.Double])

new Pipeline().setStages(Array(validationSplitModel))
}
val model = caraPipeline.tuner match {
case Some(tuner) =>
val methodeName = "set" + tuner.paramName
tuner.tuningStage match {
case "CrossValidator" => {
val paramValue = tuner.paramValue.toInt
val crossValidatorModel = new CrossValidator()

.setEstimator(pipeline)

.setEvaluator(evaluator)

.setEstimatorParamMaps(new ParamGridBuilder().build())

.setParallelism(2)
crossValidatorModel.getClass.getMethod(methodeName, paramValue.getClass )

.invoke(crossValidatorModel,paramValue.asInstanceOf[java.lang.Integer])
new Pipeline().setStages(Array(crossValidatorModel))
}
case "TrainValidationSplit" => {
val paramValue = tuner.paramValue.toDouble
val validationSplitModel = new TrainValidationSplit()

.setEstimator(pipeline)

.setEvaluator(evaluator)

.setEstimatorParamMaps(new ParamGridBuilder().build())

.setParallelism(2)
validationSplitModel.getClass.getMethod(methodeName, paramValue.getClass )

.invoke(validationSplitModel,paramValue.asInstanceOf[java.lang.Double])
new Pipeline().setStages(Array(validationSplitModel))
}

}
case None =>
pipeline
}
model
}
Expand Down
13 changes: 10 additions & 3 deletions src/main/scala/io/github/jsarni/PipelineParser/CaraParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class CaraParser(caraYaml: CaraYamlReader) extends ParserUtils with CaraStageMap
for {
pipeline <- parsePipeline()
evaluator <- parseEvaluator()
tunerDesc <- parseTuner()
} yield CaraPipeline(pipeline, evaluator, tunerDesc)
hasTuner <- hasTuner()
tunerDescOpt = if (!hasTuner) None else Some(parseTuner().get)
} yield CaraPipeline(pipeline, evaluator, tunerDescOpt)
}

private[PipelineParser] def parsePipeline(): Try[Pipeline] = {
Expand Down Expand Up @@ -92,12 +93,18 @@ class CaraParser(caraYaml: CaraYamlReader) extends ParserUtils with CaraStageMap
}
}

private[PipelineParser] def hasTuner(): Try[Boolean] =
for {
content <- contentTry
hasTuner = content.at(s"/CaraPipeline").iterator().asScala.toList.filter(_.has("tuner")).nonEmpty
} yield hasTuner

private[PipelineParser] def extractTuner(fileContent: JsonNode): Try[TuningStageDescription] = {

val tunersList = fileContent.at(s"/CaraPipeline").iterator().asScala.toList.filter(_.has("tuner"))

tunersList.length match {
case l if l <= 1 =>
case l if l == 1 =>
val tunerJson = tunersList.head
val tunerName = tunerJson.at("/tuner").textValue()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import io.github.jsarni.CaraStage.TuningStage.TuningStageDescription
import org.apache.spark.ml.Pipeline
import org.apache.spark.ml.evaluation.Evaluator

case class CaraPipeline(pipeline: Pipeline, evaluator: Evaluator, tuner: TuningStageDescription)
case class CaraPipeline(pipeline: Pipeline, evaluator: Evaluator, tuner: Option[TuningStageDescription])
4 changes: 2 additions & 2 deletions src/test/scala/io/github/jsarni/CaraModelTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class CaraModelTest extends TestBase {
val caraModel = new CaraModel("YamlPath", spark.emptyDataFrame, "savePath")(spark)
val pipeline = new Pipeline()
.setStages(Array(lr))
val crossCaraPipeline = CaraPipeline(pipeline, crossEvaluator, crossTuner)
val splitCaraPipeline = CaraPipeline(pipeline, splitEvaluator, splitTuner)
val crossCaraPipeline = CaraPipeline(pipeline, crossEvaluator, Some(crossTuner))
val splitCaraPipeline = CaraPipeline(pipeline, splitEvaluator, Some(splitTuner))
val method = PrivateMethod[Try[Pipeline]]('generateModel)

val crossModel = caraModel.invokePrivate(method(crossCaraPipeline))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,6 @@ class CaraParserTest extends TestBase {
res.get.evaluator.isInstanceOf[RegressionEvaluator] shouldBe true
res.get.pipeline.getStages.map(_.extractParamMap().toSeq.map(_.value)).head should contain theSameElementsAs
exprectedRes.getStages.map(_.extractParamMap().toSeq.map(_.value)).head
res.get.tuner shouldBe TuningStageDescription("CrossValidator", "NumFolds", "3")
res.get.tuner shouldBe Some(TuningStageDescription("CrossValidator", "NumFolds", "3"))
}
}