-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #429 from s22s/feature/tile-quantile
Add rf_agg_approx_quantiles function
- Loading branch information
Showing
10 changed files
with
259 additions
and
8 deletions.
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