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

Improve json serde error in evalMetFromJson #380

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add test
  • Loading branch information
gerashegalov committed Aug 7, 2019
commit 601e677bbbe8bdfb44e96408c4f0fc13f71015be
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ case object ModelSelectorSummary {
*
* @param json encoded metrics
*/
private def evalMetFromJson(className: String, json: String): Try[EvaluationMetrics] = {
private[selector] def evalMetFromJson(className: String, json: String): Try[EvaluationMetrics] = {
def error(c: Class[_], t: Throwable): Try[MultiMetrics] = Failure[MultiMetrics] {
new IllegalArgumentException(s"Could not extract metrics of type " +
s"${classOf[MultiMetrics]} from:\n$json", t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,21 @@ class ModelSelectorSummaryTest extends FlatSpec with TestSparkContext {
decoded.holdoutEvaluation shouldEqual summary.holdoutEvaluation
}

it should "not hide the root cause of JSON parsing errors" in {
val evalMetrics = MultiClassificationMetrics(Precision = 0.1, Recall = 0.2, F1 = 0.3, Error = 0.4,
ThresholdMetrics = ThresholdMetrics(topNs = Seq(1, 2), thresholds = Seq(1.1, 1.2),
correctCounts = Map(1 -> Seq(100L)), incorrectCounts = Map(2 -> Seq(200L)),
noPredictionCounts = Map(3 -> Seq(300L))))

val evalMetricsJson = evalMetrics.toJson()
val roundTripEvalMetrics = ModelSelectorSummary.evalMetFromJson(
classOf[MultiClassificationMetrics].getName, evalMetricsJson).get
roundTripEvalMetrics shouldBe evalMetrics

val corruptJson = evalMetricsJson.replace(":", "=")
val thr = intercept[IllegalArgumentException](ModelSelectorSummary.evalMetFromJson(
classOf[MultiClassificationMetrics].getName, corruptJson).get)

thr.getCause.getMessage shouldEqual "Unsupported format. Supported formats: json, yaml"
}
}