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

Math transformers part 2 of 2 #257

Merged
merged 20 commits into from
Apr 4, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,70 @@ trait RichNumericFeature {
def -[N](v: N)(implicit n: Numeric[N]): FeatureLike[Real] =
f.transformWith(new ScalarSubtractTransformer[I, N](scalar = v))

/**
* Take the absolute value of the feature
* @return transformed feature
*/
def abs(): FeatureLike[Real] =
f.transformWith(new AbsoluteValueTransformer[I]())

/**
* Take the ceil of the feature value
* @return transformed feature
*/
def ceil(): FeatureLike[Integral] =
f.transformWith(new CeilTransformer[I]())

/**
* Take the floor of the feature value
* @return transformed feature
*/
def floor(): FeatureLike[Integral] =
f.transformWith(new FloorTransformer[I]())

/**
* Round the feature value
* @return transformed feature
*/
def round(): FeatureLike[Integral] =
f.transformWith(new RoundTransformer[I]())

/**
* Round the feature value
* @return transformed feature
*/
def round(digits: Int): FeatureLike[Real] =
f.transformWith(new RoundDigitsTransformer[I](digits = digits))

/**
* Exp transformer: returns Euler's number `e` raised to the power of feature value
* @return transformed feature
*/
def exp(): FeatureLike[Real] =
f.transformWith(new ExpTransformer[I]())

/**
* Square root transformer
* @return transformed feature
*/
def sqrt(): FeatureLike[Real] =
f.transformWith(new SqrtTransformer[I]())

/**
* Square root transformer
* @return transformed feature
*/
def log(base: Double): FeatureLike[Real] =
f.transformWith(new LogTransformer[I](base = base))

/**
* Square root transformer
* @return transformed feature
*/
def power(power: Double): FeatureLike[Real] =
f.transformWith(new PowerTransformer[I](power = power))


}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,173 @@ class ScalarDivideTransformer[I <: OPNumeric[_], N]
override def transformFn: I => Real = (i: I) => i.toDouble.map(_ / n.toDouble(scalar)).filter(Number.isValid).toReal
}


/**
* Absolute value transformer
*
* @param uid uid for instance
* @param tti type tag for input
* @tparam I input feature type
*/
class AbsoluteValueTransformer[I <: OPNumeric[_]]
(
uid: String = UID[AbsoluteValueTransformer[_]]
)(
implicit override val tti: TypeTag[I]
) extends UnaryTransformer[I, Real](operationName = "abs", uid = uid){
override def transformFn: I => Real = (i: I) => i.toDouble.map(math.abs).toReal
}

/**
* Ceil transformer
*
* @param uid uid for instance
* @param tti type tag for input
* @tparam I input feature type
*/
class CeilTransformer[I <: OPNumeric[_]]
(
uid: String = UID[CeilTransformer[_]]
)(
implicit override val tti: TypeTag[I]
) extends UnaryTransformer[I, Integral](operationName = "ceil", uid = uid){
override def transformFn: I => Integral = (i: I) => i.toDouble.map(v => math.round(math.ceil(v))).toIntegral
}


/**
* Floor transformer
*
* @param uid uid for instance
* @param tti type tag for input
* @tparam I input feature type
*/
class FloorTransformer[I <: OPNumeric[_]]
(
uid: String = UID[FloorTransformer[_]]
)(
implicit override val tti: TypeTag[I]
) extends UnaryTransformer[I, Integral](operationName = "floor", uid = uid){
override def transformFn: I => Integral = (i: I) => i.toDouble.map(v => math.round(math.floor(v))).toIntegral
}


/**
* Round transformer
*
* @param uid uid for instance
* @param tti type tag for input
* @tparam I input feature type
*/
class RoundTransformer[I <: OPNumeric[_]]
(
uid: String = UID[RoundTransformer[_]]
)(
implicit override val tti: TypeTag[I]
) extends UnaryTransformer[I, Integral](operationName = "round", uid = uid){
override def transformFn: I => Integral = (i: I) => i.toDouble.map(v => math.round(v)).toIntegral
}


/**
* Exp transformer: returns Euler's number `e` raised to the power of feature value
*
* @param uid uid for instance
* @param tti type tag for input
* @tparam I input feature type
*/
class ExpTransformer[I <: OPNumeric[_]]
(
uid: String = UID[ExpTransformer[_]]
)(
implicit override val tti: TypeTag[I]
) extends UnaryTransformer[I, Real](operationName = "exp", uid = uid){
override def transformFn: I => Real = (i: I) => i.toDouble.map(math.exp).filter(Number.isValid).toReal
}


/**
* Square root transformer
*
* @param uid uid for instance
* @param tti type tag for input
* @tparam I input feature type
*/
class SqrtTransformer[I <: OPNumeric[_]]
(
uid: String = UID[SqrtTransformer[_]]
)(
implicit override val tti: TypeTag[I]
) extends UnaryTransformer[I, Real](operationName = "sqrt", uid = uid){
override def transformFn: I => Real = (i: I) => i.toDouble.map(math.sqrt).filter(Number.isValid).toReal
}

/**
* Log base N transformer
*
* @param base base for log 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 LogTransformer[I <: OPNumeric[_]]
(
val base: Double,
uid: String = UID[LogTransformer[_]]
)(
implicit override val tti: TypeTag[I]
) extends UnaryTransformer[I, Real](operationName = "log", uid = uid){
require(base > 0, "log base must be greater than 0")
override def transformFn: I => Real = (i: I) => {
def logN(v: Double): Double = math.log10(v) / math.log10(base)
i.toDouble.map(logN).filter(Number.isValid).toReal
}
}



/**
* Power transformer
*
* @param power power to multiply feature to
* @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 PowerTransformer[I <: OPNumeric[_]]
(
val power: Double,
uid: String = UID[PowerTransformer[_]]
)(
implicit override val tti: TypeTag[I]
) extends UnaryTransformer[I, Real](operationName = "power", uid = uid){
override def transformFn: I => Real = (i: I) =>
i.toDouble.map(v => math.pow(v, power)).filter(Number.isValid).toReal
}


/**
* Round digits transformer
*
* @param digits digits to round to
* @param uid uid for instance
* @param tti type tag for input
* @tparam I input feature type
*/
class RoundDigitsTransformer[I <: OPNumeric[_]]
(
val digits: Int,
uid: String = UID[RoundDigitsTransformer[_]]
)(
implicit override val tti: TypeTag[I]
) extends UnaryTransformer[I, Real](operationName = "roundDigits", uid = uid){
override def transformFn: I => Real = (in: I) => {
val scaler = math.pow(10, digits)
in.toDouble.map{ i => math.round(i * scaler) / scaler }.filter(Number.isValid).toReal
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 AbsoluteValueTransformerTest extends OpTransformerSpec[Real, AbsoluteValueTransformer[Real]] {
val sample = Seq(Real(-1.0), Real(-4.0), Real.empty, Real(5.0), Real(-5.5), Real(0.1), Real(2.0), Real(0.0))
val (inputData, f1) = TestFeatureBuilder(sample)
val transformer: AbsoluteValueTransformer[Real] = new AbsoluteValueTransformer[Real]().setInput(f1)
val expectedResult: Seq[Real] = Seq(Real(1.0), Real(4.0), Real.empty, Real(5.0),
Real(5.5), Real(0.1), Real(2.0), Real(0.0))

it should "have a working shortcut" in {
val f2 = f1.abs()
f2.originStage.isInstanceOf[AbsoluteValueTransformer[_]] shouldBe true
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove redundant trailing endlines in all test files

Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ class AddTransformerTest extends OpTransformerSpec[Real, AddTransformer[Real, Re
(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))
val expectedResult: Seq[Real] = Seq(Real(3.0), Real(8.0), Real(5.0), Real(5.0), Real(2.0))
}








Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 CeilTransformerTest extends OpTransformerSpec[Integral, CeilTransformer[Real]] {
val sample = Seq(Real(-1.3), Real(-4.9), Real.empty, Real(5.1), Real(-5.1), Real(0.1), Real(2.5), Real(0.4))
val (inputData, f1) = TestFeatureBuilder(sample)
val transformer: CeilTransformer[Real] = new CeilTransformer[Real]().setInput(f1)
override val expectedResult: Seq[Integral] = Seq(Integral(-1), Integral(-4), Integral.empty, Integral(6),
Integral(-5), Integral(1), Integral(3), Integral(1))

it should "have a working shortcut" in {
val f2 = f1.ceil()
f2.originStage.isInstanceOf[CeilTransformer[_]] shouldBe true
}
}








Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ class DivideTransformerTest extends OpTransformerSpec[Real, DivideTransformer[Re
(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)
val expectedResult: Seq[Real] = Seq(Real(0.5), Real(1.0), Real.empty, Real.empty, Real.empty)
}

Loading