|
| 1 | +package org.locationtech.rasterframes.extensions |
| 2 | + |
| 3 | +import org.locationtech.rasterframes.stats._ |
| 4 | +import org.apache.spark.sql.DataFrame |
| 5 | +import org.apache.spark.sql.functions.col |
| 6 | + |
| 7 | +final class RasterFrameStatFunctions private[rasterframes](df: DataFrame) { |
| 8 | + |
| 9 | + /** |
| 10 | + * Calculates the approximate quantiles of a numerical column of a DataFrame. |
| 11 | + * |
| 12 | + * The result of this algorithm has the following deterministic bound: |
| 13 | + * If the DataFrame has N elements and if we request the quantile at probability `p` up to error |
| 14 | + * `err`, then the algorithm will return a sample `x` from the DataFrame so that the *exact* rank |
| 15 | + * of `x` is close to (p * N). |
| 16 | + * More precisely, |
| 17 | + * |
| 18 | + * {{{ |
| 19 | + * floor((p - err) * N) <= rank(x) <= ceil((p + err) * N) |
| 20 | + * }}} |
| 21 | + * |
| 22 | + * This method implements a variation of the Greenwald-Khanna algorithm (with some speed |
| 23 | + * optimizations). |
| 24 | + * The algorithm was first present in <a href="http://dx.doi.org/10.1145/375663.375670"> |
| 25 | + * Space-efficient Online Computation of Quantile Summaries</a> by Greenwald and Khanna. |
| 26 | + * |
| 27 | + * @param col the name of the numerical column |
| 28 | + * @param probabilities a list of quantile probabilities |
| 29 | + * Each number must belong to [0, 1]. |
| 30 | + * For example 0 is the minimum, 0.5 is the median, 1 is the maximum. |
| 31 | + * @param relativeError The relative target precision to achieve (greater than or equal to 0). |
| 32 | + * If set to zero, the exact quantiles are computed, which could be very expensive. |
| 33 | + * Note that values greater than 1 are accepted but give the same result as 1. |
| 34 | + * @return the approximate quantiles at the given probabilities |
| 35 | + * |
| 36 | + * @note null and NaN values will be removed from the numerical column before calculation. If |
| 37 | + * the dataframe is empty or the column only contains null or NaN, an empty array is returned. |
| 38 | + * |
| 39 | + * @since 2.0.0 |
| 40 | + */ |
| 41 | + def approxTileQuantile( |
| 42 | + col: String, |
| 43 | + probabilities: Array[Double], |
| 44 | + relativeError: Double): Array[Double] = { |
| 45 | + approxTileQuantile(Array(col), probabilities, relativeError).head |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Calculates the approximate quantiles of numerical columns of a DataFrame. |
| 50 | + * @see `approxQuantile(col:Str* approxQuantile)` for detailed description. |
| 51 | + * |
| 52 | + * @param cols the names of the numerical columns |
| 53 | + * @param probabilities a list of quantile probabilities |
| 54 | + * Each number must belong to [0, 1]. |
| 55 | + * For example 0 is the minimum, 0.5 is the median, 1 is the maximum. |
| 56 | + * @param relativeError The relative target precision to achieve (greater than or equal to 0). |
| 57 | + * If set to zero, the exact quantiles are computed, which could be very expensive. |
| 58 | + * Note that values greater than 1 are accepted but give the same result as 1. |
| 59 | + * @return the approximate quantiles at the given probabilities of each column |
| 60 | + * |
| 61 | + * @note null and NaN values will be ignored in numerical columns before calculation. For |
| 62 | + * columns only containing null or NaN values, an empty array is returned. |
| 63 | + * |
| 64 | + */ |
| 65 | + def approxTileQuantile( |
| 66 | + cols: Array[String], |
| 67 | + probabilities: Array[Double], |
| 68 | + relativeError: Double): Array[Array[Double]] = { |
| 69 | + multipleApproxQuantiles( |
| 70 | + df.select(cols.map(col): _*), |
| 71 | + cols, |
| 72 | + probabilities, |
| 73 | + relativeError).map(_.toArray).toArray |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments