@@ -30,45 +30,57 @@ import org.apache.spark.util.StatCounter
3030 */
3131class DoubleRDDFunctions (self : RDD [Double ]) extends Logging with Serializable {
3232 /** Add up the elements in this RDD. */
33- def sum (): Double = {
33+ def sum (): Double = self.withScope {
3434 self.fold(0.0 )(_ + _)
3535 }
3636
3737 /**
3838 * Return a [[org.apache.spark.util.StatCounter ]] object that captures the mean, variance and
3939 * count of the RDD's elements in one operation.
4040 */
41- def stats (): StatCounter = {
41+ def stats (): StatCounter = self.withScope {
4242 self.mapPartitions(nums => Iterator (StatCounter (nums))).reduce((a, b) => a.merge(b))
4343 }
4444
4545 /** Compute the mean of this RDD's elements. */
46- def mean (): Double = stats().mean
46+ def mean (): Double = self.withScope {
47+ stats().mean
48+ }
4749
4850 /** Compute the variance of this RDD's elements. */
49- def variance (): Double = stats().variance
51+ def variance (): Double = self.withScope {
52+ stats().variance
53+ }
5054
5155 /** Compute the standard deviation of this RDD's elements. */
52- def stdev (): Double = stats().stdev
56+ def stdev (): Double = self.withScope {
57+ stats().stdev
58+ }
5359
5460 /**
5561 * Compute the sample standard deviation of this RDD's elements (which corrects for bias in
5662 * estimating the standard deviation by dividing by N-1 instead of N).
5763 */
58- def sampleStdev (): Double = stats().sampleStdev
64+ def sampleStdev (): Double = self.withScope {
65+ stats().sampleStdev
66+ }
5967
6068 /**
6169 * Compute the sample variance of this RDD's elements (which corrects for bias in
6270 * estimating the variance by dividing by N-1 instead of N).
6371 */
64- def sampleVariance (): Double = stats().sampleVariance
72+ def sampleVariance (): Double = self.withScope {
73+ stats().sampleVariance
74+ }
6575
6676 /**
6777 * :: Experimental ::
6878 * Approximate operation to return the mean within a timeout.
6979 */
7080 @ Experimental
71- def meanApprox (timeout : Long , confidence : Double = 0.95 ): PartialResult [BoundedDouble ] = {
81+ def meanApprox (
82+ timeout : Long ,
83+ confidence : Double = 0.95 ): PartialResult [BoundedDouble ] = self.withScope {
7284 val processPartition = (ctx : TaskContext , ns : Iterator [Double ]) => StatCounter (ns)
7385 val evaluator = new MeanEvaluator (self.partitions.length, confidence)
7486 self.context.runApproximateJob(self, processPartition, evaluator, timeout)
@@ -79,7 +91,9 @@ class DoubleRDDFunctions(self: RDD[Double]) extends Logging with Serializable {
7991 * Approximate operation to return the sum within a timeout.
8092 */
8193 @ Experimental
82- def sumApprox (timeout : Long , confidence : Double = 0.95 ): PartialResult [BoundedDouble ] = {
94+ def sumApprox (
95+ timeout : Long ,
96+ confidence : Double = 0.95 ): PartialResult [BoundedDouble ] = self.withScope {
8397 val processPartition = (ctx : TaskContext , ns : Iterator [Double ]) => StatCounter (ns)
8498 val evaluator = new SumEvaluator (self.partitions.length, confidence)
8599 self.context.runApproximateJob(self, processPartition, evaluator, timeout)
@@ -93,7 +107,7 @@ class DoubleRDDFunctions(self: RDD[Double]) extends Logging with Serializable {
93107 * If the RDD contains infinity, NaN throws an exception
94108 * If the elements in RDD do not vary (max == min) always returns a single bucket.
95109 */
96- def histogram (bucketCount : Int ): Pair [Array [Double ], Array [Long ]] = {
110+ def histogram (bucketCount : Int ): Pair [Array [Double ], Array [Long ]] = self.withScope {
97111 // Scala's built-in range has issues. See #SI-8782
98112 def customRange (min : Double , max : Double , steps : Int ): IndexedSeq [Double ] = {
99113 val span = max - min
@@ -140,7 +154,9 @@ class DoubleRDDFunctions(self: RDD[Double]) extends Logging with Serializable {
140154 * the maximum value of the last position and all NaN entries will be counted
141155 * in that bucket.
142156 */
143- def histogram (buckets : Array [Double ], evenBuckets : Boolean = false ): Array [Long ] = {
157+ def histogram (
158+ buckets : Array [Double ],
159+ evenBuckets : Boolean = false ): Array [Long ] = self.withScope {
144160 if (buckets.length < 2 ) {
145161 throw new IllegalArgumentException (" buckets array must have at least two elements" )
146162 }
0 commit comments