Skip to content

Commit

Permalink
Refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
scorebot committed Sep 28, 2024
1 parent b40f469 commit 0226a55
Show file tree
Hide file tree
Showing 85 changed files with 1,844 additions and 972 deletions.
7 changes: 4 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ scalacOptions := Seq(

scalacOptions in(Compile, doc) := Seq("-no-link-warnings")

scalaVersion := "2.12.18"
scalaVersion := "2.13.14"

crossScalaVersions := Seq("2.12.18", "2.11.12", "2.13.14", "3.1.3")
crossScalaVersions := Seq("2.13.14", "2.11.12", "2.12.18", "3.1.3")

libraryDependencies ++= {
Seq(
"org.apache.commons" % "commons-lang3" % "3.15.0",
"org.apache.commons" % "commons-math3" % "3.6.1",
"org.apache.commons" % "commons-text" % "1.10.0",
"org.apache.commons" % "commons-text" % "1.12.0",
"io.spray" %% "spray-json" % "1.3.6",
"org.scalatest" %% "scalatest" % "3.2.15" % "test",
"junit" % "junit" % "4.13.2" % "test"
Expand Down
21 changes: 11 additions & 10 deletions src/main/scala/org/pmml4s/common/DataType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.pmml4s.common

import org.pmml4s.data.DataVal
import org.pmml4s.util.Utils
import org.pmml4s.xml.ValTags

Expand All @@ -24,7 +25,7 @@ import org.pmml4s.xml.ValTags
sealed abstract class DataType extends DataTypeLike {
def dataType: DataType = this

def toVal(s: String): Any = Utils.toVal(s, this)
def toVal(s: String): DataVal = Utils.toDataVal(s, this)
}

/**
Expand Down Expand Up @@ -140,7 +141,7 @@ case object DateTimeType extends DateTimeType
* since midnight. The time 00:00 is represented by the number 0. No negative values are allowed.
*/
class TimeSecondsType private() extends TimeType {
override def toString = ValTags.TIMESECONDS
override def toString: String = ValTags.TIMESECONDS
}

case object TimeSecondsType extends TimeSecondsType
Expand All @@ -156,10 +157,10 @@ case class DateDaySinceYearType(aYear: Int) extends DateType {
}

case object DateDaySinceYearType {
val DateDaySinceYear0Type = DateDaySinceYearType(0)
val DateDaySinceYear1960Type = DateDaySinceYearType(1960)
val DateDaySinceYear1970Type = DateDaySinceYearType(1970)
val DateDaySinceYear1980Type = DateDaySinceYearType(1980)
val DateDaySinceYear0Type: DateDaySinceYearType = DateDaySinceYearType(0)
val DateDaySinceYear1960Type: DateDaySinceYearType = DateDaySinceYearType(1960)
val DateDaySinceYear1970Type: DateDaySinceYearType = DateDaySinceYearType(1970)
val DateDaySinceYear1980Type: DateDaySinceYearType = DateDaySinceYearType(1980)
}

/**
Expand All @@ -174,10 +175,10 @@ case class DateTimeSecondSinceYearType(aYear: Int) extends DateTimeType {
}

case object DateTimeSecondSinceYearType {
val DateTimeSecondSinceYear0Type = DateTimeSecondSinceYearType(0)
val DateTimeSecondSinceYear1960Type = DateTimeSecondSinceYearType(1960)
val DateTimeSecondSinceYear1970Type = DateTimeSecondSinceYearType(1970)
val DateTimeSecondSinceYear1980Type = DateTimeSecondSinceYearType(1980)
val DateTimeSecondSinceYear0Type: DateTimeSecondSinceYearType = DateTimeSecondSinceYearType(0)
val DateTimeSecondSinceYear1960Type: DateTimeSecondSinceYearType = DateTimeSecondSinceYearType(1960)
val DateTimeSecondSinceYear1970Type: DateTimeSecondSinceYearType = DateTimeSecondSinceYearType(1970)
val DateTimeSecondSinceYear1980Type: DateTimeSecondSinceYearType = DateTimeSecondSinceYearType(1980)
}

/**
Expand Down
22 changes: 12 additions & 10 deletions src/main/scala/org/pmml4s/common/ScoreDistribution.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
*/
package org.pmml4s.common

import org.pmml4s.data.DataVal

/**
* Comprises a method to list predicted values in a classification trees structure.
*/
class ScoreDistribution(
val value: Any,
val value: DataVal,
val recordCount: Double,
val confidence: Option[Double],
val probability: Option[Double]) extends PmmlElement {
Expand All @@ -34,19 +36,15 @@ class ScoreDistribution(
trait HasScoreDistributions {
def scoreDistributions: ScoreDistributions

def getConfidence(value: Any): Double = scoreDistributions.valueToDistribution.get(value).flatMap(_.confidence) getOrElse
Double.NaN

def getProbability(value: Any): Double = scoreDistributions.valueToDistribution.get(value).flatMap(_.probability) getOrElse
Double.NaN
def getConfidence(value: DataVal): Double = scoreDistributions.confidences.getOrElse(value, Double.NaN)

def getScoreDistribution(value: Any): Option[ScoreDistribution] = scoreDistributions.valueToDistribution.get(value)
def getProbability(value: DataVal): Double = scoreDistributions.probabilities.getOrElse(value, Double.NaN)

def probabilities: Map[Any, Double] = scoreDistributions.valueToDistribution.map(x => (x._1, x._2.probability.getOrElse(Double.NaN))).toMap
def probabilities: Map[DataVal, Double] = scoreDistributions.probabilities
}

class ScoreDistributions(val scoreDistributions: Array[ScoreDistribution]) extends PmmlElement {
val valueToDistribution: Map[Any, ScoreDistribution] = {
val valueToDistribution: Map[DataVal, ScoreDistribution] = {
val total = scoreDistributions.map(_.recordCount).sum

// Compute probabilities if not present.
Expand All @@ -59,9 +57,13 @@ class ScoreDistributions(val scoreDistributions: Array[ScoreDistribution]) exten
} else scoreDistributions).map(x => (x.value, x)).toMap
}

val probabilities: Map[DataVal, Double] = valueToDistribution.map(x => (x._1, x._2.probability.getOrElse(Double.NaN)))

val confidences: Map[DataVal, Double] = valueToDistribution.map(x => (x._1, x._2.confidence.getOrElse(Double.NaN)))

def this() = {
this(Array.empty)
}

def classes: Array[Any] = scoreDistributions.map(_.value)
def classes: Array[DataVal] = scoreDistributions.map(_.value)
}
21 changes: 11 additions & 10 deletions src/main/scala/org/pmml4s/common/Table.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,46 @@
*/
package org.pmml4s.common

import org.pmml4s.data.DataVal
import org.pmml4s.xml.ElemTags.{INLINE_TABLE, TABLE_LOCATOR}

sealed trait Table extends PmmlElement {
def find(inputs: Map[String, Any], output: String): Option[Any]
def find(inputs: Map[String, DataVal], output: String): Option[DataVal]

def apply(i: Int): Row

def dim: (Int, Int)
}

object Table {
val values = Set(TABLE_LOCATOR, INLINE_TABLE)
val values: Set[String] = Set(TABLE_LOCATOR, INLINE_TABLE)

def contains(s: String) = values.contains(s)
def contains(s: String): Boolean = values.contains(s)
}

class InlineTable(val rows: Array[Row]) extends Table {
override def find(inputs: Map[String, Any], output: String): Option[Any] = {
val r = rows.find(x => inputs.forall(p => x.elements.get(p._1) == Some(p._2)))
override def find(inputs: Map[String, DataVal], output: String): Option[DataVal] = {
val r = rows.find(x => inputs.forall(p => x.elements.get(p._1).contains(p._2)))
if (r.isDefined) r.get.elements.get(output) else None
}

override def apply(i: Int): Row = rows(i)

override def dim: (Int, Int) = (rows.size, if (rows.nonEmpty) rows.head.size else 0)
override def dim: (Int, Int) = (rows.length, if (rows.nonEmpty) rows.head.size else 0)
}

class TableLocator extends Table {
override def find(inputs: Map[String, Any], output: String): Option[Any] = ???
override def find(inputs: Map[String, DataVal], output: String): Option[DataVal] = ???

override def apply(i: Int): Row = ???

override def dim: (Int, Int) = ???
}

class Row(val elements: Map[String, Any]) extends PmmlElement {
class Row(val elements: Map[String, DataVal]) extends PmmlElement {
def size: Int = elements.size

def apply(name: String): Any = elements(name)
def apply(name: String): DataVal = elements(name)

def get(name: String): Option[Any] = elements.get(name)
def get(name: String): Option[DataVal] = elements.get(name)
}
17 changes: 9 additions & 8 deletions src/main/scala/org/pmml4s/common/Value.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.pmml4s.common

import org.pmml4s.common.Property.Property
import org.pmml4s.data.DataVal
import org.pmml4s.util.Utils

import scala.collection.mutable
Expand All @@ -26,15 +27,15 @@ class Value(
val property: Property = Property.valid) extends PmmlElement

object Value {
def distinguish(values: Array[Value], dataType: DataType): (Array[Any], Set[Any], Set[Any], Map[Any, String]) = {
val valid = mutable.ArrayBuilder.make[Any]
def distinguish(values: Array[Value], dataType: DataType): (Array[DataVal], Set[DataVal], Set[DataVal], Map[DataVal, String]) = {
val valid = mutable.ArrayBuilder.make[DataVal]
valid.sizeHint(values)
val invalid = new mutable.HashSet[Any]()
val missing = new mutable.HashSet[Any]()
val labels = new mutable.HashMap[Any, String]()
val invalid = new mutable.HashSet[DataVal]()
val missing = new mutable.HashSet[DataVal]()
val labels = new mutable.HashMap[DataVal, String]()

for (v <- values) {
val a = Utils.toVal(v.value, dataType)
val a = Utils.toDataVal(v.value, dataType)
v.property match {
case Property.valid => valid += a
case Property.invalid => invalid += a
Expand Down Expand Up @@ -67,6 +68,6 @@ object Value {
* explicitly define values which are interpreted as missing values.
*/
object Property extends Enumeration {
type Property = Value
val valid, invalid, missing = Value
type Property = this.Value
val valid, invalid, missing = this.Value
}
4 changes: 2 additions & 2 deletions src/main/scala/org/pmml4s/common/Vector.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017-2019 AutoDeploy AI
* Copyright (c) 2017-2024 AutoDeploy AI
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,7 +27,7 @@ trait Vector[@specialized(Int, Long, Float, Double) V] extends java.io.Serializa

def length: Int = size

def toArray(implicit cm: ClassTag[V]) = {
def toArray(implicit cm: ClassTag[V]): Array[V] = {
val result = new Array[V](length)
var i = 0
while (i < length) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/scala/org/pmml4s/common/elements.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package org.pmml4s.common

import org.pmml4s.common.MiningFunction.MiningFunction
import org.pmml4s.model.Model
import org.pmml4s.util.StringUtils

import scala.collection.immutable

Expand All @@ -27,7 +28,7 @@ trait HasVersion {
def version: String = parent.version

/** Returns PMML version as a double value */
def dVersion: Double = version.toDouble
def dVersion: Double = StringUtils.asDouble(version)
}

case class Extension(extender: Option[String], name: Option[String], value: Option[Any], content: Option[Any])
Expand Down Expand Up @@ -57,7 +58,7 @@ trait PmmlElement extends HasExtensions with Serializable
class Application(
val name: String,
val version: Option[String]) extends PmmlElement {
override def toString = if (version.isDefined) (name + " " + version.get) else name
override def toString: String = if (version.isDefined) (name + " " + version.get) else name
}

/**
Expand Down
Loading

0 comments on commit 0226a55

Please sign in to comment.