-
Notifications
You must be signed in to change notification settings - Fork 45
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
Add rf_agg_approx_quantiles function #429
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
456914d
Add DataFrame tileStats extension
vpipkt c1f10aa
Implemented three alternate approaches for discussion, from using bui…
metasim feaf097
Revert "Add DataFrame tileStats extension"
vpipkt 3f90067
Update tests to prefer use of rf_agg_approx_quantiles implementation
vpipkt df11ac1
Merge pull request #57 from s22s/feature/tile-quantile-opts
vpipkt f466e38
Merge remote-tracking branch 'locationtech/develop' into feature/tile…
vpipkt 2c244aa
Add documentation, python bindings for rf_agg_approx_quantiles
vpipkt 4589fe1
Remove incorrect Expression and SQL registry
vpipkt 539865c
Update Python bindings and test
vpipkt b267216
Merge remote-tracking branch 'locationtech/develop' into feature/tile…
vpipkt d73e255
Fix mistakes in merge with develop
vpipkt 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
88 changes: 88 additions & 0 deletions
88
...a/org/locationtech/rasterframes/expressions/aggregates/ApproxCellQuantilesAggregate.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,88 @@ | ||
/* | ||
* This software is licensed under the Apache 2 license, quoted below. | ||
* | ||
* Copyright 2019 Astraea, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* [http://www.apache.org/licenses/LICENSE-2.0] | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.locationtech.rasterframes.expressions.aggregates | ||
|
||
import geotrellis.raster.{Tile, isNoData} | ||
import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder | ||
import org.apache.spark.sql.catalyst.util.QuantileSummaries | ||
import org.apache.spark.sql.expressions.{MutableAggregationBuffer, UserDefinedAggregateFunction} | ||
import org.apache.spark.sql.{Column, Encoder, Row, TypedColumn, types} | ||
import org.apache.spark.sql.types.{DataTypes, StructField, StructType} | ||
import org.locationtech.rasterframes.TileType | ||
import org.locationtech.rasterframes.encoders.CatalystSerializer._ | ||
import org.locationtech.rasterframes.expressions.accessors.ExtractTile | ||
|
||
|
||
case class ApproxCellQuantilesAggregate(probabilities: Seq[Double], relativeError: Double) extends UserDefinedAggregateFunction { | ||
import org.locationtech.rasterframes.encoders.StandardSerializers.quantileSerializer | ||
|
||
override def inputSchema: StructType = StructType(Seq( | ||
StructField("value", TileType, true) | ||
)) | ||
|
||
override def bufferSchema: StructType = StructType(Seq( | ||
StructField("buffer", schemaOf[QuantileSummaries], false) | ||
)) | ||
|
||
override def dataType: types.DataType = DataTypes.createArrayType(DataTypes.DoubleType) | ||
|
||
override def deterministic: Boolean = true | ||
|
||
override def initialize(buffer: MutableAggregationBuffer): Unit = | ||
buffer.update(0, new QuantileSummaries(QuantileSummaries.defaultCompressThreshold, relativeError).toRow) | ||
|
||
override def update(buffer: MutableAggregationBuffer, input: Row): Unit = { | ||
val qs = buffer.getStruct(0).to[QuantileSummaries] | ||
if (!input.isNullAt(0)) { | ||
val tile = input.getAs[Tile](0) | ||
var result = qs | ||
tile.foreachDouble(d => if (!isNoData(d)) result = result.insert(d)) | ||
buffer.update(0, result.toRow) | ||
} | ||
else buffer | ||
} | ||
|
||
override def merge(buffer1: MutableAggregationBuffer, buffer2: Row): Unit = { | ||
val left = buffer1.getStruct(0).to[QuantileSummaries] | ||
val right = buffer2.getStruct(0).to[QuantileSummaries] | ||
val merged = left.compress().merge(right.compress()) | ||
buffer1.update(0, merged.toRow) | ||
} | ||
|
||
override def evaluate(buffer: Row): Seq[Double] = { | ||
val summaries = buffer.getStruct(0).to[QuantileSummaries] | ||
probabilities.flatMap(summaries.query) | ||
} | ||
} | ||
|
||
object ApproxCellQuantilesAggregate { | ||
private implicit def doubleSeqEncoder: Encoder[Seq[Double]] = ExpressionEncoder() | ||
|
||
def apply( | ||
tile: Column, | ||
probabilities: Seq[Double], | ||
relativeError: Double = 0.00001): TypedColumn[Any, Seq[Double]] = { | ||
new ApproxCellQuantilesAggregate(probabilities, relativeError)(ExtractTile(tile)) | ||
.as(s"rf_agg_approx_quantiles") | ||
.as[Seq[Double]] | ||
} | ||
} |
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
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
78 changes: 78 additions & 0 deletions
78
core/src/test/scala/org/locationtech/rasterframes/RasterFramesStatsSpec.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,78 @@ | ||
/* | ||
* This software is licensed under the Apache 2 license, quoted below. | ||
* | ||
* Copyright 2018 Astraea, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* [http://www.apache.org/licenses/LICENSE-2.0] | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.locationtech.rasterframes | ||
|
||
import org.locationtech.rasterframes.RasterFunctions | ||
import org.apache.spark.sql.functions.{col, explode} | ||
|
||
class RasterFramesStatsSpec extends TestEnvironment with TestData { | ||
|
||
import spark.implicits._ | ||
|
||
val df = TestData.sampleGeoTiff | ||
.toDF() | ||
.withColumn("tilePlus2", rf_local_add(col("tile"), 2)) | ||
|
||
|
||
describe("Tile quantiles through built-in functions") { | ||
|
||
it("should compute approx percentiles for a single tile col") { | ||
// Use "explode" | ||
val result = df | ||
.select(rf_explode_tiles($"tile")) | ||
.stat | ||
.approxQuantile("tile", Array(0.10, 0.50, 0.90), 0.00001) | ||
|
||
result.length should be(3) | ||
|
||
// computing externally with numpy we arrive at 7963, 10068, 12160 for these quantiles | ||
result should contain inOrderOnly(7963.0, 10068.0, 12160.0) | ||
|
||
// Use "to_array" and built-in explode | ||
val result2 = df | ||
.select(explode(rf_tile_to_array_double($"tile")) as "tile") | ||
.stat | ||
.approxQuantile("tile", Array(0.10, 0.50, 0.90), 0.00001) | ||
|
||
result2.length should be(3) | ||
|
||
// computing externally with numpy we arrive at 7963, 10068, 12160 for these quantiles | ||
result2 should contain inOrderOnly(7963.0, 10068.0, 12160.0) | ||
|
||
} | ||
} | ||
|
||
describe("Tile quantiles through custom aggregate") { | ||
it("should compute approx percentiles for a single tile col") { | ||
val result = df | ||
.select(rf_agg_approx_quantiles($"tile", Seq(0.1, 0.5, 0.9))) | ||
.first() | ||
|
||
result.length should be(3) | ||
|
||
// computing externally with numpy we arrive at 7963, 10068, 12160 for these quantiles | ||
result should contain inOrderOnly(7963.0, 10068.0, 12160.0) | ||
} | ||
|
||
} | ||
} | ||
|
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
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
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
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
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
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.
Compiler is complaining about this. Since the function returns
Unit
this line could be omitted?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.
Yeh, that was me not thinking "mutably". It can go.