From 16211410a5d151c54047040ba497edef82d26461 Mon Sep 17 00:00:00 2001 From: leahmcguire Date: Wed, 27 Mar 2019 16:13:54 -0700 Subject: [PATCH 1/7] converted math transformers to standard binary transformers --- .../op/dsl/RichNumericFeature.scala | 66 ++--------- .../impl/feature/MathTransformers.scala | 104 ++++++++++++++++++ 2 files changed, 112 insertions(+), 58 deletions(-) create mode 100644 core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala diff --git a/core/src/main/scala/com/salesforce/op/dsl/RichNumericFeature.scala b/core/src/main/scala/com/salesforce/op/dsl/RichNumericFeature.scala index 3c5006c264..1d73cf21de 100644 --- a/core/src/main/scala/com/salesforce/op/dsl/RichNumericFeature.scala +++ b/core/src/main/scala/com/salesforce/op/dsl/RichNumericFeature.scala @@ -71,22 +71,8 @@ trait RichNumericFeature { * @tparam I2 that feature output type * @return transformed feature */ - def /[I2 <: OPNumeric[_] : TypeTag](that: FeatureLike[I2]): FeatureLike[Real] = { - f.transformWith[I2, Real]( - stage = new BinaryLambdaTransformer[I, I2, Real]( - operationName = "divide", - transformFn = (i1: I, i2: I2) => { - val result = for { - x <- i1.toDouble - y <- i2.toDouble - } yield x / y - - result filter Number.isValid toReal - } - ), - f = that - ) - } + def /[I2 <: OPNumeric[_] : TypeTag](that: FeatureLike[I2]): FeatureLike[Real] = + new DivideTransformer[I, I2]().setInput(f, that).getOutput() /** * Apply Multiply transformer shortcut function @@ -102,22 +88,8 @@ trait RichNumericFeature { * @tparam I2 that feature output type * @return transformed feature */ - def *[I2 <: OPNumeric[_] : TypeTag](that: FeatureLike[I2]): FeatureLike[Real] = { - f.transformWith[I2, Real]( - stage = new BinaryLambdaTransformer[I, I2, Real]( - operationName = "multiply", - transformFn = (i1: I, i2: I2) => { - val result = for { - x <- i1.toDouble - y <- i2.toDouble - } yield x * y - - result filter Number.isValid toReal - } - ), - f = that - ) - } + def *[I2 <: OPNumeric[_] : TypeTag](that: FeatureLike[I2]): FeatureLike[Real] = + new MultiplyTransformer[I, I2]().setInput(f, that).getOutput() /** * Apply Plus transformer shortcut function @@ -133,15 +105,8 @@ trait RichNumericFeature { * @tparam I2 that feature output type * @return transformed feature */ - def +[I2 <: OPNumeric[_] : TypeTag](that: FeatureLike[I2]): FeatureLike[Real] = { - f.transformWith[I2, Real]( - stage = new BinaryLambdaTransformer[I, I2, Real]( - operationName = "plus", - transformFn = (i1: I, i2: I2) => (i1.toDouble -> i2.toDouble).map(_ + _).toReal - ), - f = that - ) - } + def +[I2 <: OPNumeric[_] : TypeTag](that: FeatureLike[I2]): FeatureLike[Real] = + new AdditionTransformer[I, I2]().setInput(f, that).getOutput() /** * Apply Minus transformer shortcut function @@ -157,23 +122,8 @@ trait RichNumericFeature { * @tparam I2 that feature output type * @return transformed feature */ - def -[I2 <: OPNumeric[_] : TypeTag](that: FeatureLike[I2]): FeatureLike[Real] = { - f.transformWith[I2, Real]( - stage = new BinaryLambdaTransformer[I, I2, Real]( - operationName = "minus", - transformFn = (i1: I, i2: I2) => { - val optZ = (i1.toDouble, i2.toDouble) match { - case (Some(x), Some(y)) => Some(x - y) - case (Some(x), None) => Some(x) - case (None, Some(y)) => Some(-y) - case (None, None) => None - } - optZ.toReal - } - ), - f = that - ) - } + def -[I2 <: OPNumeric[_] : TypeTag](that: FeatureLike[I2]): FeatureLike[Real] = + new SubtractionTransformer[I, I2]().setInput(f, that).getOutput() /** * Apply Divide scalar transformer shortcut function diff --git a/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala b/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala new file mode 100644 index 0000000000..320e98426b --- /dev/null +++ b/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala @@ -0,0 +1,104 @@ +package com.salesforce.op.stages.impl.feature + +import com.salesforce.op.UID +import com.salesforce.op.features.types._ +import com.salesforce.op.stages.base.binary.BinaryTransformer +import com.salesforce.op.utils.numeric.Number +import com.salesforce.op.utils.tuples.RichTuple._ + +import scala.reflect.runtime.universe.TypeTag + +/** + * Plus function truth table (Real as example): + * + * Real.empty + Real.empty = Real.empty + * Real.empty + Real(x) = Real(x) + * Real(x) + Real.empty = Real(x) + * Real(x) + Real(y) = Real(x + y) + **/ +class AdditionTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] +( + uid: String = UID[AdditionTransformer[_, _]] +)( + implicit override val tti1: TypeTag[I1], + override val tti2: TypeTag[I2] +) extends BinaryTransformer[I1, I2, Real](operationName = "addition", uid = uid){ + override def transformFn: (I1, I2) => Real = (i1: I1, i2: I2) => (i1.toDouble -> i2.toDouble).map(_ + _).toReal +} + +/** + * Minus function truth table (Real as example): + * + * Real.empty - Real.empty = Real.empty + * Real.empty - Real(x) = Real(-x) + * Real(x) - Real.empty = Real(x) + * Real(x) - Real(y) = Real(x - y) + */ +class SubtractionTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] +( + uid: String = UID[SubtractionTransformer[_, _]] +)( + implicit override val tti1: TypeTag[I1], + override val tti2: TypeTag[I2] +) extends BinaryTransformer[I1, I2, Real](operationName = "minus", uid = uid){ + override def transformFn: (I1, I2) => Real = (i1: I1, i2: I2) => { + val optZ = (i1.toDouble, i2.toDouble) match { + case (Some(x), Some(y)) => Some(x - y) + case (Some(x), None) => Some(x) + case (None, Some(y)) => Some(-y) + case (None, None) => None + } + optZ.toReal + } +} + + +/** + * Multiply function truth table (Real as example): + * + * Real.empty * Real.empty = Real.empty + * Real.empty * Real(x) = Real.empty + * Real(x) * Real.empty = Real.empty + * Real(x) * Real(y) = Real(x * y) filter ("is not NaN or Infinity") + */ +class MultiplyTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] +( + uid: String = UID[MultiplyTransformer[_, _]] +)( + implicit override val tti1: TypeTag[I1], + override val tti2: TypeTag[I2] +) extends BinaryTransformer[I1, I2, Real](operationName = "multiply", uid = uid){ + override def transformFn: (I1, I2) => Real = (i1: I1, i2: I2) => { + val result = for { + x <- i1.toDouble + y <- i2.toDouble + } yield x * y + + result filter Number.isValid toReal + } +} + +/** + * Divide function truth table (Real as example): + * + * Real.empty / Real.empty = Real.empty + * Real.empty / Real(x) = Real.empty + * Real(x) / Real.empty = Real.empty + * Real(x) / Real(y) = Real(x * y) filter ("is not NaN or Infinity") + */ +class DivideTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] +( + uid: String = UID[MultiplyTransformer[_, _]] +)( + implicit override val tti1: TypeTag[I1], + override val tti2: TypeTag[I2] +) extends BinaryTransformer[I1, I2, Real](operationName = "divide", uid = uid){ + override def transformFn: (I1, I2) => Real = (i1: I1, i2: I2) => { + val result = for { + x <- i1.toDouble + y <- i2.toDouble + } yield x / y + + result filter Number.isValid toReal + } +} From 5901083cab9968ea08f2ad617f860b3d81d986f8 Mon Sep 17 00:00:00 2001 From: leahmcguire Date: Thu, 28 Mar 2019 11:50:37 -0700 Subject: [PATCH 2/7] made concrete math transformers --- .../op/dsl/RichNumericFeature.scala | 50 +++------- .../impl/feature/MathTransformers.scala | 96 ++++++++++++++++++- .../op/dsl/RichNumericFeatureTest.scala | 52 ++++++++-- 3 files changed, 151 insertions(+), 47 deletions(-) diff --git a/core/src/main/scala/com/salesforce/op/dsl/RichNumericFeature.scala b/core/src/main/scala/com/salesforce/op/dsl/RichNumericFeature.scala index 1d73cf21de..fc821553bc 100644 --- a/core/src/main/scala/com/salesforce/op/dsl/RichNumericFeature.scala +++ b/core/src/main/scala/com/salesforce/op/dsl/RichNumericFeature.scala @@ -32,13 +32,9 @@ package com.salesforce.op.dsl import com.salesforce.op.features.FeatureLike import com.salesforce.op.features.types._ -import com.salesforce.op.stages.base.binary.BinaryLambdaTransformer -import com.salesforce.op.stages.base.unary.UnaryLambdaTransformer import com.salesforce.op.stages.impl.feature._ -import com.salesforce.op.stages.impl.preparators.{CorrelationType, CorrelationExclusion, SanityChecker} +import com.salesforce.op.stages.impl.preparators.{CorrelationExclusion, CorrelationType, SanityChecker} import com.salesforce.op.stages.impl.regression.IsotonicRegressionCalibrator -import com.salesforce.op.utils.tuples.RichTuple._ -import com.salesforce.op.utils.numeric.Number import scala.language.postfixOps import scala.reflect.ClassTag @@ -72,7 +68,7 @@ trait RichNumericFeature { * @return transformed feature */ def /[I2 <: OPNumeric[_] : TypeTag](that: FeatureLike[I2]): FeatureLike[Real] = - new DivideTransformer[I, I2]().setInput(f, that).getOutput() + f.transformWith(new DivideTransformer[I, I2](), that) /** * Apply Multiply transformer shortcut function @@ -89,7 +85,7 @@ trait RichNumericFeature { * @return transformed feature */ def *[I2 <: OPNumeric[_] : TypeTag](that: FeatureLike[I2]): FeatureLike[Real] = - new MultiplyTransformer[I, I2]().setInput(f, that).getOutput() + f.transformWith(new MultiplyTransformer[I, I2](), that) /** * Apply Plus transformer shortcut function @@ -106,7 +102,7 @@ trait RichNumericFeature { * @return transformed feature */ def +[I2 <: OPNumeric[_] : TypeTag](that: FeatureLike[I2]): FeatureLike[Real] = - new AdditionTransformer[I, I2]().setInput(f, that).getOutput() + f.transformWith(new AddTransformer[I, I2](), that) /** * Apply Minus transformer shortcut function @@ -123,7 +119,7 @@ trait RichNumericFeature { * @return transformed feature */ def -[I2 <: OPNumeric[_] : TypeTag](that: FeatureLike[I2]): FeatureLike[Real] = - new SubtractionTransformer[I, I2]().setInput(f, that).getOutput() + f.transformWith(new SubtractTransformer[I, I2](), that) /** * Apply Divide scalar transformer shortcut function @@ -133,13 +129,8 @@ trait RichNumericFeature { * @tparam N value type * @return transformed feature */ - def /[N](v: N)(implicit n: Numeric[N]): FeatureLike[Real] = { - f.transformWith( - new UnaryLambdaTransformer[I, Real]( - operationName = "divideS", - transformFn = r => r.toDouble.map(_ / n.toDouble(v)).filter(Number.isValid).toReal) - ) - } + def /[N](v: N)(implicit n: Numeric[N]): FeatureLike[Real] = + f.transformWith(new ScalarDivideTransformer(scalar = v)) /** * Apply Multiply scalar transformer shortcut function @@ -149,13 +140,8 @@ trait RichNumericFeature { * @tparam N value type * @return transformed feature */ - def *[N](v: N)(implicit n: Numeric[N]): FeatureLike[Real] = { - f.transformWith( - new UnaryLambdaTransformer[I, Real]( - operationName = "multiplyS", - transformFn = r => r.toDouble.map(_ * n.toDouble(v)).filter(Number.isValid).toReal) - ) - } + def *[N](v: N)(implicit n: Numeric[N]): FeatureLike[Real] = + f.transformWith(new ScalarMultiplyTransformer(scalar = v)) /** * Apply Plus scalar transformer shortcut function @@ -165,13 +151,8 @@ trait RichNumericFeature { * @tparam N value type * @return transformed feature */ - def +[N](v: N)(implicit n: Numeric[N]): FeatureLike[Real] = { - f.transformWith( - new UnaryLambdaTransformer[I, Real]( - operationName = "plusS", - transformFn = r => r.toDouble.map(_ + n.toDouble(v)).toReal) - ) - } + def +[N](v: N)(implicit n: Numeric[N]): FeatureLike[Real] = + f.transformWith(new ScalarAddTransformer[I, N](scalar = v)) /** * Apply Minus scalar transformer shortcut function @@ -181,13 +162,8 @@ trait RichNumericFeature { * @tparam N value type * @return transformed feature */ - def -[N](v: N)(implicit n: Numeric[N]): FeatureLike[Real] = { - f.transformWith( - new UnaryLambdaTransformer[I, Real]( - operationName = "minusS", - transformFn = r => r.toDouble.map(_ - n.toDouble(v)).toReal) - ) - } + def -[N](v: N)(implicit n: Numeric[N]): FeatureLike[Real] = + f.transformWith(new ScalarSubtractTransformer[I, N](scalar = v)) } diff --git a/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala b/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala index 320e98426b..21e173985d 100644 --- a/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala +++ b/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala @@ -3,6 +3,7 @@ package com.salesforce.op.stages.impl.feature import com.salesforce.op.UID import com.salesforce.op.features.types._ import com.salesforce.op.stages.base.binary.BinaryTransformer +import com.salesforce.op.stages.base.unary.UnaryTransformer import com.salesforce.op.utils.numeric.Number import com.salesforce.op.utils.tuples.RichTuple._ @@ -16,9 +17,9 @@ import scala.reflect.runtime.universe.TypeTag * Real(x) + Real.empty = Real(x) * Real(x) + Real(y) = Real(x + y) **/ -class AdditionTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] +class AddTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] ( - uid: String = UID[AdditionTransformer[_, _]] + uid: String = UID[AddTransformer[_, _]] )( implicit override val tti1: TypeTag[I1], override val tti2: TypeTag[I2] @@ -26,6 +27,28 @@ class AdditionTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] override def transformFn: (I1, I2) => Real = (i1: I1, i2: I2) => (i1.toDouble -> i2.toDouble).map(_ + _).toReal } +/** + * Scalar addition transformer + * + * @param scalar scalar value + * @param uid uid for instance + * @param tti type tag for input + * @param n value converter + * @tparam I input feature type + * @tparam N value type + */ +class ScalarAddTransformer[I <: OPNumeric[_], N] +( + val scalar: N, + uid: String = UID[ScalarAddTransformer[_, _]] +)( + implicit override val tti: TypeTag[I], + n: Numeric[N] +) extends UnaryTransformer[I, Real](operationName = "scalarAddition", uid = uid){ + override def transformFn: I => Real = (i: I) => i.toDouble.map(_ + n.toDouble(scalar)).toReal +} + + /** * Minus function truth table (Real as example): * @@ -34,9 +57,9 @@ class AdditionTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] * Real(x) - Real.empty = Real(x) * Real(x) - Real(y) = Real(x - y) */ -class SubtractionTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] +class SubtractTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] ( - uid: String = UID[SubtractionTransformer[_, _]] + uid: String = UID[SubtractTransformer[_, _]] )( implicit override val tti1: TypeTag[I1], override val tti2: TypeTag[I2] @@ -53,6 +76,27 @@ class SubtractionTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] } +/** + * Scalar subtract transformer + * + * @param scalar scalar value + * @param uid uid for instance + * @param tti type tag for input + * @param n value converter + * @tparam I input feature type + * @tparam N value type + */ +class ScalarSubtractTransformer[I <: OPNumeric[_], N] +( + val scalar: N, + uid: String = UID[ScalarSubtractTransformer[_, _]] +)( + implicit override val tti: TypeTag[I], + n: Numeric[N] +) extends UnaryTransformer[I, Real](operationName = "scalarSubtract", uid = uid){ + override def transformFn: I => Real = (i: I) => i.toDouble.map(_ - n.toDouble(scalar)).toReal +} + /** * Multiply function truth table (Real as example): * @@ -78,6 +122,28 @@ class MultiplyTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] } } +/** + * Scalar multiply transformer + * + * @param scalar scalar value + * @param uid uid for instance + * @param tti type tag for input + * @param n value converter + * @tparam I input feature type + * @tparam N value type + */ +class ScalarMultiplyTransformer[I <: OPNumeric[_], N] +( + val scalar: N, + uid: String = UID[ScalarMultiplyTransformer[_, _]] +)( + implicit override val tti: TypeTag[I], + n: Numeric[N] +) extends UnaryTransformer[I, Real](operationName = "scalarMultiply", uid = uid){ + override def transformFn: I => Real = (i: I) => i.toDouble.map(_ * n.toDouble(scalar)).filter(Number.isValid).toReal +} + + /** * Divide function truth table (Real as example): * @@ -102,3 +168,25 @@ class DivideTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] result filter Number.isValid toReal } } + + +/** + * Scalar divide transformer + * + * @param scalar scalar value + * @param uid uid for instance + * @param tti type tag for input + * @param n value converter + * @tparam I input feature type + * @tparam N value type + */ +class ScalarDivideTransformer[I <: OPNumeric[_], N] +( + val scalar: N, + uid: String = UID[ScalarDivideTransformer[_, _]] +)( + implicit override val tti: TypeTag[I], + n: Numeric[N] +) extends UnaryTransformer[I, Real](operationName = "scalarDivide", uid = uid){ + override def transformFn: I => Real = (i: I) => i.toDouble.map(_ / n.toDouble(scalar)).filter(Number.isValid).toReal +} diff --git a/core/src/test/scala/com/salesforce/op/dsl/RichNumericFeatureTest.scala b/core/src/test/scala/com/salesforce/op/dsl/RichNumericFeatureTest.scala index 962c0aaf25..001d8981a7 100644 --- a/core/src/test/scala/com/salesforce/op/dsl/RichNumericFeatureTest.scala +++ b/core/src/test/scala/com/salesforce/op/dsl/RichNumericFeatureTest.scala @@ -134,15 +134,55 @@ class RichNumericFeatureTest extends FlatSpec with FeatureTestBase with RichNume // TODO: add vectorize() test } - Spec[RichRealNNFeature] should "have tests" in { - // TODO: add tests + Spec[RichRealNNFeature] should "perform math functions correctly" in { + val checkAddition = testOp[RealNN, RealNN, Real](x => y => x + y) + checkAddition of(5.0.toRealNN, 2.0.toRealNN) expecting 7.0.toReal + + val checkSubtraction = testOp[RealNN, RealNN, Real](x => y => x - y) + checkSubtraction of(5.0.toRealNN, 2.0.toRealNN) expecting 3.0.toReal + + val checkMultiplication = testOp[RealNN, RealNN, Real](x => y => x * y) + checkMultiplication of(5.0.toRealNN, 2.0.toRealNN) expecting 10.toReal + checkMultiplication of(Double.NaN.toRealNN, 2.0.toRealNN) expecting Real.empty + + val checkDivision = testOp[RealNN, RealNN, Real](x => y => x / y) + checkDivision of(5.0.toRealNN, 2.0.toRealNN) expecting 2.5.toReal + checkDivision of(2.0.toRealNN, 0.0.toRealNN) expecting Real.empty } - Spec[RichBinaryFeature] should "have tests" in { - // TODO: add tests + Spec[RichBinaryFeature] should "perform math functions" in { + val checkAddition = testOp[Binary, Binary, Real](x => y => x + y) + checkAddition of(true.toBinary, true.toBinary) expecting 2.0.toReal + checkAddition of(true.toBinary, Binary.empty) expecting 1.0.toReal + + val checkSubtraction = testOp[Binary, Binary, Real](x => y => x - y) + checkSubtraction of(true.toBinary, true.toBinary) expecting 0.0.toReal + checkSubtraction of(true.toBinary, Binary.empty) expecting 1.0.toReal + + val checkMultiplication = testOp[Binary, Binary, Real](x => y => x * y) + checkMultiplication of(true.toBinary, true.toBinary) expecting 1.0.toReal + checkMultiplication of(true.toBinary, Binary.empty) expecting Real.empty + + val checkDivision = testOp[Binary, Binary, Real](x => y => x / y) + checkDivision of(true.toBinary, true.toBinary) expecting 1.0.toReal + checkDivision of(true.toBinary, false.toBinary) expecting Real.empty } - Spec[RichIntegralFeature[_]] should "have tests" in { - // TODO: add tests + Spec[RichIntegralFeature[_]] should "perform math functions" in { + val checkAddition = testOp[Integral, Integral, Real](x => y => x + y) + checkAddition of(5.toIntegral, 2.toIntegral) expecting 7.0.toReal + checkAddition of(Integral.empty, 2.toIntegral) expecting 2.0.toReal + + val checkSubtraction = testOp[Integral, Integral, Real](x => y => x - y) + checkSubtraction of(5.toIntegral, 2.toIntegral) expecting 3.0.toReal + checkSubtraction of(Integral.empty, 2.toIntegral) expecting (-2.0).toReal + + val checkMultiplication = testOp[Integral, Integral, Real](x => y => x * y) + checkMultiplication of(5.toIntegral, 2.toIntegral) expecting 10.toReal + checkMultiplication of(Integral.empty, 2.toIntegral) expecting Real.empty + + val checkDivision = testOp[Integral, Integral, Real](x => y => x / y) + checkDivision of(5.toIntegral, 2.toIntegral) expecting 2.5.toReal + checkDivision of(2.toIntegral, 0.toIntegral) expecting Real.empty } } From 0549f5f546a3cb2997e70fddcb1c659479b283f5 Mon Sep 17 00:00:00 2001 From: leahmcguire Date: Thu, 28 Mar 2019 12:48:13 -0700 Subject: [PATCH 3/7] fixed style --- .../impl/feature/MathTransformers.scala | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala b/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala index 21e173985d..0fb7f9b025 100644 --- a/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala +++ b/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala @@ -1,3 +1,33 @@ +/* + * Copyright (c) 2017, Salesforce.com, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + package com.salesforce.op.stages.impl.feature import com.salesforce.op.UID @@ -16,7 +46,7 @@ import scala.reflect.runtime.universe.TypeTag * Real.empty + Real(x) = Real(x) * Real(x) + Real.empty = Real(x) * Real(x) + Real(y) = Real(x + y) - **/ + */ class AddTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] ( uid: String = UID[AddTransformer[_, _]] From 69834ebf8466dd51e20dcc238b2b439e2ffb89a0 Mon Sep 17 00:00:00 2001 From: leahmcguire Date: Thu, 28 Mar 2019 15:52:13 -0700 Subject: [PATCH 4/7] put back names and added transformer spec tests --- .../impl/feature/MathTransformers.scala | 15 +-- .../impl/feature/MathTransformersTest.scala | 110 ++++++++++++++++++ 2 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 core/src/test/scala/com/salesforce/op/stages/impl/feature/MathTransformersTest.scala diff --git a/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala b/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala index 0fb7f9b025..9395eace8c 100644 --- a/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala +++ b/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala @@ -53,7 +53,7 @@ class AddTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] )( implicit override val tti1: TypeTag[I1], override val tti2: TypeTag[I2] -) extends BinaryTransformer[I1, I2, Real](operationName = "addition", uid = uid){ +) extends BinaryTransformer[I1, I2, Real](operationName = "plus", uid = uid){ override def transformFn: (I1, I2) => Real = (i1: I1, i2: I2) => (i1.toDouble -> i2.toDouble).map(_ + _).toReal } @@ -73,8 +73,8 @@ class ScalarAddTransformer[I <: OPNumeric[_], N] uid: String = UID[ScalarAddTransformer[_, _]] )( implicit override val tti: TypeTag[I], - n: Numeric[N] -) extends UnaryTransformer[I, Real](operationName = "scalarAddition", uid = uid){ + val n: Numeric[N] +) extends UnaryTransformer[I, Real](operationName = "scalarPlus", uid = uid){ override def transformFn: I => Real = (i: I) => i.toDouble.map(_ + n.toDouble(scalar)).toReal } @@ -122,8 +122,8 @@ class ScalarSubtractTransformer[I <: OPNumeric[_], N] uid: String = UID[ScalarSubtractTransformer[_, _]] )( implicit override val tti: TypeTag[I], - n: Numeric[N] -) extends UnaryTransformer[I, Real](operationName = "scalarSubtract", uid = uid){ + val n: Numeric[N] +) extends UnaryTransformer[I, Real](operationName = "scalarMinus", uid = uid){ override def transformFn: I => Real = (i: I) => i.toDouble.map(_ - n.toDouble(scalar)).toReal } @@ -168,7 +168,7 @@ class ScalarMultiplyTransformer[I <: OPNumeric[_], N] uid: String = UID[ScalarMultiplyTransformer[_, _]] )( implicit override val tti: TypeTag[I], - n: Numeric[N] + val n: Numeric[N] ) extends UnaryTransformer[I, Real](operationName = "scalarMultiply", uid = uid){ override def transformFn: I => Real = (i: I) => i.toDouble.map(_ * n.toDouble(scalar)).filter(Number.isValid).toReal } @@ -216,7 +216,8 @@ class ScalarDivideTransformer[I <: OPNumeric[_], N] uid: String = UID[ScalarDivideTransformer[_, _]] )( implicit override val tti: TypeTag[I], - n: Numeric[N] + val n: Numeric[N] ) extends UnaryTransformer[I, Real](operationName = "scalarDivide", uid = uid){ override def transformFn: I => Real = (i: I) => i.toDouble.map(_ / n.toDouble(scalar)).filter(Number.isValid).toReal } + diff --git a/core/src/test/scala/com/salesforce/op/stages/impl/feature/MathTransformersTest.scala b/core/src/test/scala/com/salesforce/op/stages/impl/feature/MathTransformersTest.scala new file mode 100644 index 0000000000..d58fbb8211 --- /dev/null +++ b/core/src/test/scala/com/salesforce/op/stages/impl/feature/MathTransformersTest.scala @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2017, Salesforce.com, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package com.salesforce.op.stages.impl.feature + +import com.salesforce.op.features.types._ +import com.salesforce.op.test.{OpTransformerSpec, TestFeatureBuilder} +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +class AddTransformerTest extends OpTransformerSpec[Real, AddTransformer[Real, Real]] { + val sample = Seq((Real(1.0), Real(2.0)), (Real(4.0), Real(4.0)), (Real.empty, Real(5.0)), + (Real(5.0), Real.empty), (Real(2.0), Real(0.0))) + val (inputData, f1, f2) = TestFeatureBuilder(sample) + val transformer: AddTransformer[Real, Real] = new AddTransformer[Real, Real]().setInput(f1, f2) + override val expectedResult: Seq[Real] = Seq(Real(3.0), Real(8.0), Real(5.0), Real(5.0), Real(2.0)) +} + +@RunWith(classOf[JUnitRunner]) +class ScalarAddTransformerTest extends OpTransformerSpec[Real, ScalarAddTransformer[Real, Double]] { + val sample = Seq(Real(1.0), Real(4.0), Real.empty, Real(-1.0), Real(2.0)) + val (inputData, f1) = TestFeatureBuilder(sample) + val transformer: ScalarAddTransformer[Real, Double] = new ScalarAddTransformer[Real, Double](5.0).setInput(f1) + override val expectedResult: Seq[Real] = Seq(Real(6.0), Real(9.0), Real.empty, Real(4.0), Real(7.0)) +} + +@RunWith(classOf[JUnitRunner]) +class SubtractTransformerTest extends OpTransformerSpec[Real, SubtractTransformer[Real, Real]] { + val sample = Seq((Real(1.0), Real(2.0)), (Real(4.0), Real(4.0)), (Real.empty, Real(5.0)), + (Real(5.0), Real.empty), (Real(2.0), Real(0.0))) + val (inputData, f1, f2) = TestFeatureBuilder(sample) + val transformer: SubtractTransformer[Real, Real] = new SubtractTransformer[Real, Real]().setInput(f1, f2) + override val expectedResult: Seq[Real] = Seq(Real(-1.0), Real(0.0), Real(-5.0), Real(5.0), Real(2.0)) +} + +@RunWith(classOf[JUnitRunner]) +class ScalarSubtractTransformerTest extends OpTransformerSpec[Real, ScalarSubtractTransformer[Real, Double]] { + val sample = Seq(Real(1.0), Real(4.0), Real.empty, Real(-1.0), Real(2.0)) + val (inputData, f1) = TestFeatureBuilder(sample) + val transformer: ScalarSubtractTransformer[Real, Double] = new ScalarSubtractTransformer[Real, Double](5.0).setInput(f1) + override val expectedResult: Seq[Real] = Seq(Real(-4.0), Real(-1.0), Real.empty, Real(-6.0), Real(-3.0)) +} + + +@RunWith(classOf[JUnitRunner]) +class MultiplyTransformerTest extends OpTransformerSpec[Real, MultiplyTransformer[Real, Real]] { + val sample = Seq((Real(1.0), Real(2.0)), (Real(4.0), Real(4.0)), (Real.empty, Real(5.0)), + (Real(5.0), Real.empty), (Real(2.0), Real(0.0))) + val (inputData, f1, f2) = TestFeatureBuilder(sample) + val transformer: MultiplyTransformer[Real, Real] = new MultiplyTransformer[Real, Real]().setInput(f1, f2) + override val expectedResult: Seq[Real] = Seq(Real(2.0), Real(16.0), Real.empty, Real.empty, Real(0.0)) +} + +@RunWith(classOf[JUnitRunner]) +class ScalarMultiplyTransformerTest extends OpTransformerSpec[Real, ScalarMultiplyTransformer[Real, Double]] { + val sample = Seq(Real(1.0), Real(4.0), Real.empty, Real(-1.0), Real(2.0)) + val (inputData, f1) = TestFeatureBuilder(sample) + val transformer: ScalarMultiplyTransformer[Real, Double] = new ScalarMultiplyTransformer[Real, Double](5.0).setInput(f1) + override val expectedResult: Seq[Real] = Seq(Real(5.0), Real(20.0), Real.empty, Real(-5.0), Real(10.0)) +} + +@RunWith(classOf[JUnitRunner]) +class DivideTransformerTest extends OpTransformerSpec[Real, DivideTransformer[Real, Real]] { + val sample = Seq((Real(1.0), Real(2.0)), (Real(4.0), Real(4.0)), (Real.empty, Real(5.0)), + (Real(5.0), Real.empty), (Real(2.0), Real(0.0))) + val (inputData, f1, f2) = TestFeatureBuilder(sample) + val transformer: DivideTransformer[Real, Real] = new DivideTransformer[Real, Real]().setInput(f1, f2) + override val expectedResult: Seq[Real] = Seq(Real(0.5), Real(1.0), Real.empty, Real.empty, Real.empty) +} + +@RunWith(classOf[JUnitRunner]) +class ScalarDivideTransformerTest extends OpTransformerSpec[Real, ScalarDivideTransformer[Real, Double]] { + val sample = Seq(Real(1.0), Real(4.0), Real.empty, Real(-1.0), Real(2.0)) + val (inputData, f1) = TestFeatureBuilder(sample) + val transformer: ScalarDivideTransformer[Real, Double] = new ScalarDivideTransformer[Real, Double](2.0).setInput(f1) + override val expectedResult: Seq[Real] = Seq(Real(0.5), Real(2.0), Real.empty, Real(-0.5), Real(1.0)) +} + + + + + From 9deaf8a58cc64873520ec14810302313ce21fb53 Mon Sep 17 00:00:00 2001 From: leahmcguire Date: Thu, 28 Mar 2019 15:58:04 -0700 Subject: [PATCH 5/7] style --- .../stages/impl/feature/MathTransformersTest.scala | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/core/src/test/scala/com/salesforce/op/stages/impl/feature/MathTransformersTest.scala b/core/src/test/scala/com/salesforce/op/stages/impl/feature/MathTransformersTest.scala index d58fbb8211..78a4df17ef 100644 --- a/core/src/test/scala/com/salesforce/op/stages/impl/feature/MathTransformersTest.scala +++ b/core/src/test/scala/com/salesforce/op/stages/impl/feature/MathTransformersTest.scala @@ -48,7 +48,8 @@ class AddTransformerTest extends OpTransformerSpec[Real, AddTransformer[Real, Re class ScalarAddTransformerTest extends OpTransformerSpec[Real, ScalarAddTransformer[Real, Double]] { val sample = Seq(Real(1.0), Real(4.0), Real.empty, Real(-1.0), Real(2.0)) val (inputData, f1) = TestFeatureBuilder(sample) - val transformer: ScalarAddTransformer[Real, Double] = new ScalarAddTransformer[Real, Double](5.0).setInput(f1) + val transformer: ScalarAddTransformer[Real, Double] = new ScalarAddTransformer[Real, Double](5.0) + .setInput(f1) override val expectedResult: Seq[Real] = Seq(Real(6.0), Real(9.0), Real.empty, Real(4.0), Real(7.0)) } @@ -65,7 +66,8 @@ class SubtractTransformerTest extends OpTransformerSpec[Real, SubtractTransforme class ScalarSubtractTransformerTest extends OpTransformerSpec[Real, ScalarSubtractTransformer[Real, Double]] { val sample = Seq(Real(1.0), Real(4.0), Real.empty, Real(-1.0), Real(2.0)) val (inputData, f1) = TestFeatureBuilder(sample) - val transformer: ScalarSubtractTransformer[Real, Double] = new ScalarSubtractTransformer[Real, Double](5.0).setInput(f1) + val transformer: ScalarSubtractTransformer[Real, Double] = new ScalarSubtractTransformer[Real, Double](5.0) + .setInput(f1) override val expectedResult: Seq[Real] = Seq(Real(-4.0), Real(-1.0), Real.empty, Real(-6.0), Real(-3.0)) } @@ -83,7 +85,8 @@ class MultiplyTransformerTest extends OpTransformerSpec[Real, MultiplyTransforme class ScalarMultiplyTransformerTest extends OpTransformerSpec[Real, ScalarMultiplyTransformer[Real, Double]] { val sample = Seq(Real(1.0), Real(4.0), Real.empty, Real(-1.0), Real(2.0)) val (inputData, f1) = TestFeatureBuilder(sample) - val transformer: ScalarMultiplyTransformer[Real, Double] = new ScalarMultiplyTransformer[Real, Double](5.0).setInput(f1) + val transformer: ScalarMultiplyTransformer[Real, Double] = new ScalarMultiplyTransformer[Real, Double](5.0) + .setInput(f1) override val expectedResult: Seq[Real] = Seq(Real(5.0), Real(20.0), Real.empty, Real(-5.0), Real(10.0)) } @@ -100,7 +103,8 @@ class DivideTransformerTest extends OpTransformerSpec[Real, DivideTransformer[Re class ScalarDivideTransformerTest extends OpTransformerSpec[Real, ScalarDivideTransformer[Real, Double]] { val sample = Seq(Real(1.0), Real(4.0), Real.empty, Real(-1.0), Real(2.0)) val (inputData, f1) = TestFeatureBuilder(sample) - val transformer: ScalarDivideTransformer[Real, Double] = new ScalarDivideTransformer[Real, Double](2.0).setInput(f1) + val transformer: ScalarDivideTransformer[Real, Double] = new ScalarDivideTransformer[Real, Double](2.0) + .setInput(f1) override val expectedResult: Seq[Real] = Seq(Real(0.5), Real(2.0), Real.empty, Real(-0.5), Real(1.0)) } From 07d97f570d765887a5e8cc04754dfac9b535bab2 Mon Sep 17 00:00:00 2001 From: leahmcguire Date: Thu, 28 Mar 2019 16:40:18 -0700 Subject: [PATCH 6/7] split tests into different files --- .../impl/feature/AddTransformerTest.scala | 53 ++++++++ .../impl/feature/DivideTransformerTest.scala | 47 ++++++++ .../impl/feature/MathTransformersTest.scala | 114 ------------------ .../feature/MultiplyTransformerTest.scala | 46 +++++++ .../feature/ScalarAddTransformerTest.scala | 47 ++++++++ .../feature/ScalarDivideTransformerTest.scala | 46 +++++++ .../ScalarMultiplyTransformerTest.scala | 45 +++++++ .../ScalarSubtractTransformerTest.scala | 44 +++++++ .../feature/SubtractTransformerTest.scala | 49 ++++++++ 9 files changed, 377 insertions(+), 114 deletions(-) create mode 100644 core/src/test/scala/com/salesforce/op/stages/impl/feature/AddTransformerTest.scala create mode 100644 core/src/test/scala/com/salesforce/op/stages/impl/feature/DivideTransformerTest.scala delete mode 100644 core/src/test/scala/com/salesforce/op/stages/impl/feature/MathTransformersTest.scala create mode 100644 core/src/test/scala/com/salesforce/op/stages/impl/feature/MultiplyTransformerTest.scala create mode 100644 core/src/test/scala/com/salesforce/op/stages/impl/feature/ScalarAddTransformerTest.scala create mode 100644 core/src/test/scala/com/salesforce/op/stages/impl/feature/ScalarDivideTransformerTest.scala create mode 100644 core/src/test/scala/com/salesforce/op/stages/impl/feature/ScalarMultiplyTransformerTest.scala create mode 100644 core/src/test/scala/com/salesforce/op/stages/impl/feature/ScalarSubtractTransformerTest.scala create mode 100644 core/src/test/scala/com/salesforce/op/stages/impl/feature/SubtractTransformerTest.scala diff --git a/core/src/test/scala/com/salesforce/op/stages/impl/feature/AddTransformerTest.scala b/core/src/test/scala/com/salesforce/op/stages/impl/feature/AddTransformerTest.scala new file mode 100644 index 0000000000..67ea7c87cd --- /dev/null +++ b/core/src/test/scala/com/salesforce/op/stages/impl/feature/AddTransformerTest.scala @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2017, Salesforce.com, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package com.salesforce.op.stages.impl.feature + +import com.salesforce.op.features.types._ +import com.salesforce.op.test.{OpTransformerSpec, TestFeatureBuilder} +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +class AddTransformerTest extends OpTransformerSpec[Real, AddTransformer[Real, Real]] { + val sample = Seq((Real(1.0), Real(2.0)), (Real(4.0), Real(4.0)), (Real.empty, Real(5.0)), + (Real(5.0), Real.empty), (Real(2.0), Real(0.0))) + val (inputData, f1, f2) = TestFeatureBuilder(sample) + val transformer: AddTransformer[Real, Real] = new AddTransformer[Real, Real]().setInput(f1, f2) + override val expectedResult: Seq[Real] = Seq(Real(3.0), Real(8.0), Real(5.0), Real(5.0), Real(2.0)) +} + + + + + + + + diff --git a/core/src/test/scala/com/salesforce/op/stages/impl/feature/DivideTransformerTest.scala b/core/src/test/scala/com/salesforce/op/stages/impl/feature/DivideTransformerTest.scala new file mode 100644 index 0000000000..00da5d5a55 --- /dev/null +++ b/core/src/test/scala/com/salesforce/op/stages/impl/feature/DivideTransformerTest.scala @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2017, Salesforce.com, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package com.salesforce.op.stages.impl.feature + +import com.salesforce.op.features.types._ +import com.salesforce.op.test.{OpTransformerSpec, TestFeatureBuilder} +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + + +@RunWith(classOf[JUnitRunner]) +class DivideTransformerTest extends OpTransformerSpec[Real, DivideTransformer[Real, Real]] { + val sample = Seq((Real(1.0), Real(2.0)), (Real(4.0), Real(4.0)), (Real.empty, Real(5.0)), + (Real(5.0), Real.empty), (Real(2.0), Real(0.0))) + val (inputData, f1, f2) = TestFeatureBuilder(sample) + val transformer: DivideTransformer[Real, Real] = new DivideTransformer[Real, Real]().setInput(f1, f2) + override val expectedResult: Seq[Real] = Seq(Real(0.5), Real(1.0), Real.empty, Real.empty, Real.empty) +} + diff --git a/core/src/test/scala/com/salesforce/op/stages/impl/feature/MathTransformersTest.scala b/core/src/test/scala/com/salesforce/op/stages/impl/feature/MathTransformersTest.scala deleted file mode 100644 index 78a4df17ef..0000000000 --- a/core/src/test/scala/com/salesforce/op/stages/impl/feature/MathTransformersTest.scala +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2017, Salesforce.com, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * * Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package com.salesforce.op.stages.impl.feature - -import com.salesforce.op.features.types._ -import com.salesforce.op.test.{OpTransformerSpec, TestFeatureBuilder} -import org.junit.runner.RunWith -import org.scalatest.junit.JUnitRunner - -@RunWith(classOf[JUnitRunner]) -class AddTransformerTest extends OpTransformerSpec[Real, AddTransformer[Real, Real]] { - val sample = Seq((Real(1.0), Real(2.0)), (Real(4.0), Real(4.0)), (Real.empty, Real(5.0)), - (Real(5.0), Real.empty), (Real(2.0), Real(0.0))) - val (inputData, f1, f2) = TestFeatureBuilder(sample) - val transformer: AddTransformer[Real, Real] = new AddTransformer[Real, Real]().setInput(f1, f2) - override val expectedResult: Seq[Real] = Seq(Real(3.0), Real(8.0), Real(5.0), Real(5.0), Real(2.0)) -} - -@RunWith(classOf[JUnitRunner]) -class ScalarAddTransformerTest extends OpTransformerSpec[Real, ScalarAddTransformer[Real, Double]] { - val sample = Seq(Real(1.0), Real(4.0), Real.empty, Real(-1.0), Real(2.0)) - val (inputData, f1) = TestFeatureBuilder(sample) - val transformer: ScalarAddTransformer[Real, Double] = new ScalarAddTransformer[Real, Double](5.0) - .setInput(f1) - override val expectedResult: Seq[Real] = Seq(Real(6.0), Real(9.0), Real.empty, Real(4.0), Real(7.0)) -} - -@RunWith(classOf[JUnitRunner]) -class SubtractTransformerTest extends OpTransformerSpec[Real, SubtractTransformer[Real, Real]] { - val sample = Seq((Real(1.0), Real(2.0)), (Real(4.0), Real(4.0)), (Real.empty, Real(5.0)), - (Real(5.0), Real.empty), (Real(2.0), Real(0.0))) - val (inputData, f1, f2) = TestFeatureBuilder(sample) - val transformer: SubtractTransformer[Real, Real] = new SubtractTransformer[Real, Real]().setInput(f1, f2) - override val expectedResult: Seq[Real] = Seq(Real(-1.0), Real(0.0), Real(-5.0), Real(5.0), Real(2.0)) -} - -@RunWith(classOf[JUnitRunner]) -class ScalarSubtractTransformerTest extends OpTransformerSpec[Real, ScalarSubtractTransformer[Real, Double]] { - val sample = Seq(Real(1.0), Real(4.0), Real.empty, Real(-1.0), Real(2.0)) - val (inputData, f1) = TestFeatureBuilder(sample) - val transformer: ScalarSubtractTransformer[Real, Double] = new ScalarSubtractTransformer[Real, Double](5.0) - .setInput(f1) - override val expectedResult: Seq[Real] = Seq(Real(-4.0), Real(-1.0), Real.empty, Real(-6.0), Real(-3.0)) -} - - -@RunWith(classOf[JUnitRunner]) -class MultiplyTransformerTest extends OpTransformerSpec[Real, MultiplyTransformer[Real, Real]] { - val sample = Seq((Real(1.0), Real(2.0)), (Real(4.0), Real(4.0)), (Real.empty, Real(5.0)), - (Real(5.0), Real.empty), (Real(2.0), Real(0.0))) - val (inputData, f1, f2) = TestFeatureBuilder(sample) - val transformer: MultiplyTransformer[Real, Real] = new MultiplyTransformer[Real, Real]().setInput(f1, f2) - override val expectedResult: Seq[Real] = Seq(Real(2.0), Real(16.0), Real.empty, Real.empty, Real(0.0)) -} - -@RunWith(classOf[JUnitRunner]) -class ScalarMultiplyTransformerTest extends OpTransformerSpec[Real, ScalarMultiplyTransformer[Real, Double]] { - val sample = Seq(Real(1.0), Real(4.0), Real.empty, Real(-1.0), Real(2.0)) - val (inputData, f1) = TestFeatureBuilder(sample) - val transformer: ScalarMultiplyTransformer[Real, Double] = new ScalarMultiplyTransformer[Real, Double](5.0) - .setInput(f1) - override val expectedResult: Seq[Real] = Seq(Real(5.0), Real(20.0), Real.empty, Real(-5.0), Real(10.0)) -} - -@RunWith(classOf[JUnitRunner]) -class DivideTransformerTest extends OpTransformerSpec[Real, DivideTransformer[Real, Real]] { - val sample = Seq((Real(1.0), Real(2.0)), (Real(4.0), Real(4.0)), (Real.empty, Real(5.0)), - (Real(5.0), Real.empty), (Real(2.0), Real(0.0))) - val (inputData, f1, f2) = TestFeatureBuilder(sample) - val transformer: DivideTransformer[Real, Real] = new DivideTransformer[Real, Real]().setInput(f1, f2) - override val expectedResult: Seq[Real] = Seq(Real(0.5), Real(1.0), Real.empty, Real.empty, Real.empty) -} - -@RunWith(classOf[JUnitRunner]) -class ScalarDivideTransformerTest extends OpTransformerSpec[Real, ScalarDivideTransformer[Real, Double]] { - val sample = Seq(Real(1.0), Real(4.0), Real.empty, Real(-1.0), Real(2.0)) - val (inputData, f1) = TestFeatureBuilder(sample) - val transformer: ScalarDivideTransformer[Real, Double] = new ScalarDivideTransformer[Real, Double](2.0) - .setInput(f1) - override val expectedResult: Seq[Real] = Seq(Real(0.5), Real(2.0), Real.empty, Real(-0.5), Real(1.0)) -} - - - - - diff --git a/core/src/test/scala/com/salesforce/op/stages/impl/feature/MultiplyTransformerTest.scala b/core/src/test/scala/com/salesforce/op/stages/impl/feature/MultiplyTransformerTest.scala new file mode 100644 index 0000000000..540cd2115a --- /dev/null +++ b/core/src/test/scala/com/salesforce/op/stages/impl/feature/MultiplyTransformerTest.scala @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2017, Salesforce.com, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package com.salesforce.op.stages.impl.feature + +import com.salesforce.op.features.types.Real +import com.salesforce.op.test.{OpTransformerSpec, TestFeatureBuilder} +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +class MultiplyTransformerTest extends OpTransformerSpec[Real, MultiplyTransformer[Real, Real]] { + val sample = Seq((Real(1.0), Real(2.0)), (Real(4.0), Real(4.0)), (Real.empty, Real(5.0)), + (Real(5.0), Real.empty), (Real(2.0), Real(0.0))) + val (inputData, f1, f2) = TestFeatureBuilder(sample) + val transformer: MultiplyTransformer[Real, Real] = new MultiplyTransformer[Real, Real]().setInput(f1, f2) + override val expectedResult: Seq[Real] = Seq(Real(2.0), Real(16.0), Real.empty, Real.empty, Real(0.0)) +} + diff --git a/core/src/test/scala/com/salesforce/op/stages/impl/feature/ScalarAddTransformerTest.scala b/core/src/test/scala/com/salesforce/op/stages/impl/feature/ScalarAddTransformerTest.scala new file mode 100644 index 0000000000..e89bd714d6 --- /dev/null +++ b/core/src/test/scala/com/salesforce/op/stages/impl/feature/ScalarAddTransformerTest.scala @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2017, Salesforce.com, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package com.salesforce.op.stages.impl.feature + +import com.salesforce.op.features.types._ +import com.salesforce.op.test.{OpTransformerSpec, TestFeatureBuilder} +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + + +@RunWith(classOf[JUnitRunner]) +class ScalarAddTransformerTest extends OpTransformerSpec[Real, ScalarAddTransformer[Real, Double]] { + val sample = Seq(Real(1.0), Real(4.0), Real.empty, Real(-1.0), Real(2.0)) + val (inputData, f1) = TestFeatureBuilder(sample) + val transformer: ScalarAddTransformer[Real, Double] = new ScalarAddTransformer[Real, Double](5.0) + .setInput(f1) + override val expectedResult: Seq[Real] = Seq(Real(6.0), Real(9.0), Real.empty, Real(4.0), Real(7.0)) +} + diff --git a/core/src/test/scala/com/salesforce/op/stages/impl/feature/ScalarDivideTransformerTest.scala b/core/src/test/scala/com/salesforce/op/stages/impl/feature/ScalarDivideTransformerTest.scala new file mode 100644 index 0000000000..0c81bf4618 --- /dev/null +++ b/core/src/test/scala/com/salesforce/op/stages/impl/feature/ScalarDivideTransformerTest.scala @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2017, Salesforce.com, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.salesforce.op.stages.impl.feature + +import com.salesforce.op.features.types.Real +import com.salesforce.op.test.{OpTransformerSpec, TestFeatureBuilder} +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +class ScalarDivideTransformerTest extends OpTransformerSpec[Real, ScalarDivideTransformer[Real, Double]] { + val sample = Seq(Real(1.0), Real(4.0), Real.empty, Real(-1.0), Real(2.0)) + val (inputData, f1) = TestFeatureBuilder(sample) + val transformer: ScalarDivideTransformer[Real, Double] = new ScalarDivideTransformer[Real, Double](2.0) + .setInput(f1) + override val expectedResult: Seq[Real] = Seq(Real(0.5), Real(2.0), Real.empty, Real(-0.5), Real(1.0)) +} + + diff --git a/core/src/test/scala/com/salesforce/op/stages/impl/feature/ScalarMultiplyTransformerTest.scala b/core/src/test/scala/com/salesforce/op/stages/impl/feature/ScalarMultiplyTransformerTest.scala new file mode 100644 index 0000000000..3dc951faf2 --- /dev/null +++ b/core/src/test/scala/com/salesforce/op/stages/impl/feature/ScalarMultiplyTransformerTest.scala @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2017, Salesforce.com, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package com.salesforce.op.stages.impl.feature + +import com.salesforce.op.features.types.Real +import com.salesforce.op.test.{OpTransformerSpec, TestFeatureBuilder} +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +class ScalarMultiplyTransformerTest extends OpTransformerSpec[Real, ScalarMultiplyTransformer[Real, Double]] { + val sample = Seq(Real(1.0), Real(4.0), Real.empty, Real(-1.0), Real(2.0)) + val (inputData, f1) = TestFeatureBuilder(sample) + val transformer: ScalarMultiplyTransformer[Real, Double] = new ScalarMultiplyTransformer[Real, Double](5.0) + .setInput(f1) + override val expectedResult: Seq[Real] = Seq(Real(5.0), Real(20.0), Real.empty, Real(-5.0), Real(10.0)) +} diff --git a/core/src/test/scala/com/salesforce/op/stages/impl/feature/ScalarSubtractTransformerTest.scala b/core/src/test/scala/com/salesforce/op/stages/impl/feature/ScalarSubtractTransformerTest.scala new file mode 100644 index 0000000000..faf3ed6f26 --- /dev/null +++ b/core/src/test/scala/com/salesforce/op/stages/impl/feature/ScalarSubtractTransformerTest.scala @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2017, Salesforce.com, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.salesforce.op.stages.impl.feature + +import com.salesforce.op.features.types.Real +import com.salesforce.op.test.{OpTransformerSpec, TestFeatureBuilder} +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +class ScalarSubtractTransformerTest extends OpTransformerSpec[Real, ScalarSubtractTransformer[Real, Double]] { + val sample = Seq(Real(1.0), Real(4.0), Real.empty, Real(-1.0), Real(2.0)) + val (inputData, f1) = TestFeatureBuilder(sample) + val transformer: ScalarSubtractTransformer[Real, Double] = new ScalarSubtractTransformer[Real, Double](5.0) + .setInput(f1) + override val expectedResult: Seq[Real] = Seq(Real(-4.0), Real(-1.0), Real.empty, Real(-6.0), Real(-3.0)) +} diff --git a/core/src/test/scala/com/salesforce/op/stages/impl/feature/SubtractTransformerTest.scala b/core/src/test/scala/com/salesforce/op/stages/impl/feature/SubtractTransformerTest.scala new file mode 100644 index 0000000000..11b85074f3 --- /dev/null +++ b/core/src/test/scala/com/salesforce/op/stages/impl/feature/SubtractTransformerTest.scala @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2017, Salesforce.com, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package com.salesforce.op.stages.impl.feature + +import com.salesforce.op.features.types.Real +import com.salesforce.op.test.{OpTransformerSpec, TestFeatureBuilder} +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + + +@RunWith(classOf[JUnitRunner]) +class SubtractTransformerTest extends OpTransformerSpec[Real, SubtractTransformer[Real, Real]] { + val sample = Seq((Real(1.0), Real(2.0)), (Real(4.0), Real(4.0)), (Real.empty, Real(5.0)), + (Real(5.0), Real.empty), (Real(2.0), Real(0.0))) + val (inputData, f1, f2) = TestFeatureBuilder(sample) + val transformer: SubtractTransformer[Real, Real] = new SubtractTransformer[Real, Real]().setInput(f1, f2) + override val expectedResult: Seq[Real] = Seq(Real(-1.0), Real(0.0), Real(-5.0), Real(5.0), Real(2.0)) +} + + + From 6fc525f39761a5a33b7e3eae32a52a61609cfa63 Mon Sep 17 00:00:00 2001 From: leahmcguire Date: Thu, 28 Mar 2019 18:23:05 -0700 Subject: [PATCH 7/7] fixed tests --- .../op/stages/impl/feature/MathTransformers.scala | 8 ++++---- .../op/stages/impl/feature/AliasTransformerTest.scala | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala b/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala index 9395eace8c..dbef03c44f 100644 --- a/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala +++ b/core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala @@ -74,7 +74,7 @@ class ScalarAddTransformer[I <: OPNumeric[_], N] )( implicit override val tti: TypeTag[I], val n: Numeric[N] -) extends UnaryTransformer[I, Real](operationName = "scalarPlus", uid = uid){ +) extends UnaryTransformer[I, Real](operationName = "plusS", uid = uid){ override def transformFn: I => Real = (i: I) => i.toDouble.map(_ + n.toDouble(scalar)).toReal } @@ -123,7 +123,7 @@ class ScalarSubtractTransformer[I <: OPNumeric[_], N] )( implicit override val tti: TypeTag[I], val n: Numeric[N] -) extends UnaryTransformer[I, Real](operationName = "scalarMinus", uid = uid){ +) extends UnaryTransformer[I, Real](operationName = "minusS", uid = uid){ override def transformFn: I => Real = (i: I) => i.toDouble.map(_ - n.toDouble(scalar)).toReal } @@ -169,7 +169,7 @@ class ScalarMultiplyTransformer[I <: OPNumeric[_], N] )( implicit override val tti: TypeTag[I], val n: Numeric[N] -) extends UnaryTransformer[I, Real](operationName = "scalarMultiply", uid = uid){ +) extends UnaryTransformer[I, Real](operationName = "multiplyS", uid = uid){ override def transformFn: I => Real = (i: I) => i.toDouble.map(_ * n.toDouble(scalar)).filter(Number.isValid).toReal } @@ -217,7 +217,7 @@ class ScalarDivideTransformer[I <: OPNumeric[_], N] )( implicit override val tti: TypeTag[I], val n: Numeric[N] -) extends UnaryTransformer[I, Real](operationName = "scalarDivide", uid = uid){ +) extends UnaryTransformer[I, Real](operationName = "divideS", uid = uid){ override def transformFn: I => Real = (i: I) => i.toDouble.map(_ / n.toDouble(scalar)).filter(Number.isValid).toReal } diff --git a/core/src/test/scala/com/salesforce/op/stages/impl/feature/AliasTransformerTest.scala b/core/src/test/scala/com/salesforce/op/stages/impl/feature/AliasTransformerTest.scala index 637545f4db..960736b147 100644 --- a/core/src/test/scala/com/salesforce/op/stages/impl/feature/AliasTransformerTest.scala +++ b/core/src/test/scala/com/salesforce/op/stages/impl/feature/AliasTransformerTest.scala @@ -57,8 +57,8 @@ class AliasTransformerTest extends OpTransformerSpec[RealNN, AliasTransformer[Re it should "have a shortcut that changes feature name on a derived feature" in { val feature = (f1 / f2).alias feature.name shouldBe "feature" - feature.originStage shouldBe a[BinaryLambdaTransformer[_, _, _]] - val origin = feature.originStage.asInstanceOf[BinaryLambdaTransformer[_, _, _]] + feature.originStage shouldBe a[DivideTransformer[_, _]] + val origin = feature.originStage.asInstanceOf[DivideTransformer[_, _]] val transformed = origin.transform(inputData) transformed.columns should contain (feature.name) transformed.collect(feature) shouldEqual sample.map { case (v1, v2) => (v1.v -> v2.v).map(_ / _).toRealNN(0.0) }