-
Notifications
You must be signed in to change notification settings - Fork 393
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
Part 1 of 2 - Making concrete math transformers #255
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1621141
converted math transformers to standard binary transformers
leahmcguire 5901083
made concrete math transformers
leahmcguire b44dbcd
Merge branch 'master' into lm/mathTransformers
leahmcguire 0549f5f
fixed style
leahmcguire f0679af
Merge branch 'master' into lm/mathTransformers
tovbinm 69834eb
put back names and added transformer spec tests
leahmcguire 39b8f77
Merge branch 'lm/mathTransformers' of github.com:salesforce/Transmogr…
leahmcguire 9deaf8a
style
leahmcguire 07d97f5
split tests into different files
leahmcguire 6fc525f
fixed tests
leahmcguire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
222 changes: 222 additions & 0 deletions
222
core/src/main/scala/com/salesforce/op/stages/impl/feature/MathTransformers.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
/* | ||
* 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 | ||
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._ | ||
|
||
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 AddTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] | ||
( | ||
uid: String = UID[AddTransformer[_, _]] | ||
)( | ||
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 | ||
} | ||
|
||
/** | ||
* 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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
) 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): | ||
* | ||
* 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 SubtractTransformer[I1 <: OPNumeric[_], I2 <: OPNumeric[_]] | ||
( | ||
uid: String = UID[SubtractTransformer[_, _]] | ||
)( | ||
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 | ||
} | ||
} | ||
|
||
|
||
/** | ||
* 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): | ||
* | ||
* 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 | ||
} | ||
} | ||
|
||
/** | ||
* 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): | ||
* | ||
* 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 | ||
} | ||
} | ||
|
||
|
||
/** | ||
* 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 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you might need an endline here |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the operation name was
"plus"
is there a reason to change it (same with other transformers). I would rather keep it the same as before.