-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-22346][ML] VectorSizeHint Transformer for using VectorAssembler in StructuredSteaming #19746
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
24cc417
Added VectorSizeHint Transformer in ml.feature.
MrBago 2e76297
PR feedback.
MrBago 136d8f8
Error when size-hint size does not match existing metadata.
MrBago e117c15
Drop redundant test.
MrBago 591dcd2
PR feedback.
MrBago b30e3b1
More PR feedback.
MrBago cafa875
PR feedback.
MrBago 7021552
Update VectorSizeHintSuite to test optimistic param:w.
MrBago d63f077
Updated documentation for
MrBago 9c3dcec
Fix typo.
MrBago 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
Error when size-hint size does not match existing metadata.
- Loading branch information
commit 136d8f8f05e6430a65f55fd35d6011643371e9c4
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -39,47 +39,75 @@ class VectorSizeHintSuite | |
| test("Adding size to column of vectors.") { | ||
|
|
||
| val size = 3 | ||
| val vectorColName = "vector" | ||
| val denseVector = Vectors.dense(1, 2, 3) | ||
| val sparseVector = Vectors.sparse(size, Array(), Array()) | ||
|
|
||
| val data = Seq(denseVector, denseVector, sparseVector).map(Tuple1.apply) | ||
| val dataFrame = data.toDF("vector") | ||
|
|
||
| val transformer = new VectorSizeHint() | ||
| .setInputCol("vector") | ||
| .setSize(3) | ||
| .setHandleInvalid("error") | ||
| val withSize = transformer.transform(dataFrame) | ||
| val dataFrame = data.toDF(vectorColName) | ||
| assert( | ||
| AttributeGroup.fromStructField(withSize.schema("vector")).size == size, | ||
| AttributeGroup.fromStructField(dataFrame.schema(vectorColName)).size == -1, | ||
| "Transformer did not add expected size data.") | ||
|
|
||
| for (handleInvalid <- VectorSizeHint.supportedHandleInvalids) { | ||
| val transformer = new VectorSizeHint() | ||
| .setInputCol(vectorColName) | ||
| .setSize(size) | ||
| .setHandleInvalid(handleInvalid) | ||
| val withSize = transformer.transform(dataFrame) | ||
| assert( | ||
| AttributeGroup.fromStructField(withSize.schema(vectorColName)).size == size, | ||
| "Transformer did not add expected size data.") | ||
| withSize.collect | ||
|
||
| } | ||
| } | ||
|
|
||
| test("Size hint preserves attributes.") { | ||
|
|
||
| case class Foo(x: Double, y: Double, z: Double) | ||
| val size = 3 | ||
| val vectorColName = "vector" | ||
| val data = Seq((1, 2, 3), (2, 3, 3)) | ||
| val boo = data.toDF("x", "y", "z") | ||
| val dataFrame = data.toDF("x", "y", "z") | ||
|
|
||
| val assembler = new VectorAssembler() | ||
| .setInputCols(Array("x", "y", "z")) | ||
| .setOutputCol("vector") | ||
| val dataFrameWithMeatadata = assembler.transform(boo) | ||
| val group = AttributeGroup.fromStructField(dataFrameWithMeatadata.schema("vector")) | ||
| .setOutputCol(vectorColName) | ||
| val dataFrameWithMetadata = assembler.transform(dataFrame) | ||
| val group = AttributeGroup.fromStructField(dataFrameWithMetadata.schema(vectorColName)) | ||
|
|
||
| for (handleInvalid <- Seq("error", "skip", "optimistic")) { | ||
| for (handleInvalid <- VectorSizeHint.supportedHandleInvalids) { | ||
| val transformer = new VectorSizeHint() | ||
| .setInputCol("vector") | ||
| .setSize(3) | ||
| .setInputCol(vectorColName) | ||
| .setSize(size) | ||
| .setHandleInvalid(handleInvalid) | ||
| val withSize = transformer.transform(dataFrameWithMeatadata) | ||
| val withSize = transformer.transform(dataFrameWithMetadata) | ||
|
|
||
| val newGroup = AttributeGroup.fromStructField(withSize.schema("vector")) | ||
| val newGroup = AttributeGroup.fromStructField(withSize.schema(vectorColName)) | ||
| assert(newGroup.size === size, "Transformer did not add expected size data.") | ||
| assert( | ||
| newGroup.attributes.get.deep === group.attributes.get.deep, | ||
| "SizeHintTransformer did not preserve attributes.") | ||
| withSize.collect | ||
| } | ||
| } | ||
|
|
||
| test("Size miss-match between current and target size raises an error.") { | ||
|
||
| val size = 4 | ||
| val vectorColName = "vector" | ||
| val data = Seq((1, 2, 3), (2, 3, 3)) | ||
| val dataFrame = data.toDF("x", "y", "z") | ||
|
|
||
| val assembler = new VectorAssembler() | ||
| .setInputCols(Array("x", "y", "z")) | ||
| .setOutputCol(vectorColName) | ||
| val dataFrameWithMetadata = assembler.transform(dataFrame) | ||
|
|
||
| for (handleInvalid <- VectorSizeHint.supportedHandleInvalids) { | ||
| val transformer = new VectorSizeHint() | ||
| .setInputCol(vectorColName) | ||
| .setSize(size) | ||
| .setHandleInvalid(handleInvalid) | ||
| intercept[SparkException](transformer.transform(dataFrameWithMetadata)) | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
Usually, SparkException is used for exceptions within tasks. I'd use IllegalArgumentException.