/*
Function interface for aggregating a fixed number of data points.  This
returns the result of aggregating points "datapts", breaking out the results
by the fields named in "decomps" using bucketizers "bucketizers".  See
Aggregator for details.
354842
function skAggregate(datapts, decomps, bucketizers)
{
	var aggregator;

	aggregator = new skAggregator({
	    'decomps': decomps,
	    'bucketizers': bucketizers
	});
	datapts.forEach(function (p) { aggregator.aggregate(p); });
	aggregator.end();
	aggregator.read(0);
	return (aggregator.result());
}
837887
private DataPoints abs(final DataPoints points) {
    // TODO(cl) - Using an array as the size function may not return the exact
    // results and we should figure a way to avoid copying data anyway.
    final List<DataPoint> dps = new ArrayList<DataPoint>();

    final SeekableView view = points.iterator();
    while (view.hasNext()) {
      DataPoint pt = view.next();
      if (pt.isInteger()) {
        dps.add(MutableDataPoint.ofLongValue(
            pt.timestamp(), Math.abs(pt.longValue())));
      } else {
        dps.add(MutableDataPoint.ofDoubleValue(
            pt.timestamp(), Math.abs(pt.doubleValue())));
      }
    }
    final DataPoint[] results = new DataPoint[dps.size()];
    dps.toArray(results);
    return new PostAggregatedDataPoints(points, results);
  }
368161
private Double aggregate(Datasource ds, Collection<Double> values) {
        return ((values.size() / m_intervalsPer) > ds.getXff()) ? ds.getAggregationFuction().apply(values) : Double.NaN;
    }
1349846
@Override
  public void aggregate(final ByteBuffer buf, final int position)
  {
    for (int i = 0; i < valueSelectors.length; i++) {
      values[i] = valueSelectors[i].getDouble();
    }
    final IndexedInts keys = keySelector.getRow();
    // Wrapping memory and ArrayOfDoublesSketch is inexpensive compared to sketch operations.
    // Maintaining a cache of wrapped objects per buffer position like in Theta sketch aggregator
    // might might be considered, but it would increase complexity including relocate() support.
    final WritableMemory mem = WritableMemory.wrap(buf, ByteOrder.LITTLE_ENDIAN);
    final WritableMemory region = mem.writableRegion(position, maxIntermediateSize);
    final Lock lock = stripedLock.getAt(lockIndex(position)).writeLock();
    lock.lock();
    try {
      final ArrayOfDoublesUpdatableSketch sketch = ArrayOfDoublesSketches.wrapUpdatableSketch(region);
      for (int i = 0, keysSize = keys.size(); i < keysSize; i++) {
        final String key = keySelector.lookupName(keys.get(i));
        sketch.update(key, values);
      }
    }
    finally {
      lock.unlock();
    }
  }
158804
function pointsArray (buf, offset, numPoints) {
    var pointsData = []
    for (var i = offset; i < offset + numPoints * 12; i += 12) {
      pointsData.push([buf.readFloatLE(i + 0), buf.readFloatLE(i + 4), buf.readFloatLE(i + 8)])
    }
    return pointsData
  }
806654
function generateData(numDimensions, numPoints) {
  const data = tf.tidy(() => {
    return tf.linspace(0, 1, numPoints * numDimensions)
        .reshape([numPoints, numDimensions])
        .add(tf.randomUniform([numPoints, numDimensions]));
  });
  return data;
}
680198
function aggregateMetrics(currentTime, metricData) {
    var aggregateKey;

    /**
     * Place aggregate data into the specified slot.  If the current zoom
     * level matches the aggregate level, the data is emitted to keep the
     * display in sync.
     *
     * @param {Number} index
     * The desired slot for the aggregate.
     *
     * @param {Object} data
     * The aggregate data.
     *
     * @returns {void}
     */
    var setAggregateData =
      function setAggregateData(index, data) {
        this._aggregation[aggregateKey].data[index] = data;

        // if this view (current or not) is scrolled, adjust it
        if (this._aggregation[aggregateKey].scrollOffset) {
          this._aggregation[aggregateKey].scrollOffset--;
        }

        // emit to keep the display in sync
        if (this.isCurrentZoom(aggregateKey)) {
          if (this.isScrolled()) {
            this.emit("refreshMetrics");
          } else {
            this.emit("metrics", data);
          }
        }
      }.bind(this);

    /**
     * Given the current time time index, add any missing logical
     * time slots to have a complete picture of data.
     *
     * @param {Number} currentTimeIndex
     * The time index currently being processed.
     *
     * @returns {void}
     */
    var addMissingTimeSlots =
      function addMissingTimeSlots(currentTimeIndex) {
        var aggregateIndex = this._aggregation[aggregateKey].data.length;
        while (aggregateIndex < currentTimeIndex) {
          setAggregateData(aggregateIndex++, this.emptyAverage);
        }
      }.bind(this);

    /**
     * After having detected a new sampling, aggregate the relevant data points
     *
     * @param {Object[]} rows
     * The array reference.
     *
     * @param {Number} startIndex
     * The starting index to derive an average.
     *
     * @param {Number} endIndex
     * The ending index to derive an average.
     *
     * @returns {void}
     */
    var getAveragedAggregate =
      function getAveragedAggregate(rows, startIndex, endIndex) {
        var averagedAggregate = getInitializedAverage(metricData);

        // this is the number of elements we will aggregate
        var aggregateCount = endIndex - startIndex + 1;

        // you can compute an average of a set of numbers two ways
        // first, you can add all the numbers together and then divide by the count
        // second, you call divide each number by the count and add the quotients
        // the first method is more accurate, however you can overflow an accumulator
        // and result with NaN
        // the second method is employed here to ensure no overflows

        for (var dataKey in metricData) {
          for (var dataMetricKey in metricData[dataKey]) {
            for (var rowIndex = startIndex; rowIndex <= endIndex; rowIndex++) {
              averagedAggregate[dataKey][dataMetricKey] +=
                rows[rowIndex][dataKey][dataMetricKey] / aggregateCount;
            }

            // after the average is done, truncate the averages to one decimal point
            averagedAggregate[dataKey][dataMetricKey] =
              +averagedAggregate[dataKey][dataMetricKey].toFixed(1);
          }
        }

        return averagedAggregate;
      };

    /**
     * Process one row of metric data into aggregate.
     *
     * @param {Number} rowIndex
     * The index of the row being processed.
     *
     * @param {Object[]} rows
     * The array reference.
     *
     * @returns {void}
     */
    var processRow = function processRow(rowIndex, rows) {
      var averagedAggregate;
      var lastTimeIndex = this._aggregation[aggregateKey].lastTimeIndex;

      // get the time index of the aggregate
      var currentTimeIndex =
        convertElapsedTimeToTimeIndex(currentTime, this._startTime, +aggregateKey);

      // when the time index changes, average the data for the time band
      if (currentTimeIndex !== lastTimeIndex) {
        // except for the first one
        if (lastTimeIndex !== undefined) {
          // add in any missing logical time slots
          addMissingTimeSlots.call(this, lastTimeIndex);

          // get the average across the discovered time band
          averagedAggregate = getAveragedAggregate(
            rows,
            this._aggregation[aggregateKey].lastAggregateIndex,
            rowIndex - 1
          );

          // place the average
          setAggregateData(lastTimeIndex, averagedAggregate);

          // now we know where the next aggregate begins
          this._aggregation[aggregateKey].lastAggregateIndex = rowIndex;
        }

        // level-break
        this._aggregation[aggregateKey].lastTimeIndex = currentTimeIndex;
      }
    }.bind(this);

    // iterate over the configured aggregation time buckets
    for (aggregateKey in this._aggregation) {
      // iterate through metrics, beginning where we left off
      processRow(this._lastAggregationIndex, this._metrics);
    }

    // remember where we will begin again
    this._lastAggregationIndex++;
  }
682556
async function aggregateBoltDependencies(data) {
  let componentDependencies = [];
  let componentsWithoutDeps = data;

  componentsWithoutDeps.forEach(item => {
    if (item.deps) {
      componentDependencies.push([...item.deps]);
    }
  });

  componentDependencies = flattenDeep(componentDependencies);

  componentDependencies = componentDependencies.filter(function(x, i, a) {
    if (x !== '@bolt/build-tools' && a.indexOf(x) === i) {
      return x;
    }
  });

  let globalDepsSrc = await Promise.all(componentDependencies.map(getPkgInfo));

  componentsWithoutDeps = componentsWithoutDeps.concat(globalDepsSrc);

  var uniqueComponentsWithDeps = removeDuplicateObjectsFromArray(
    componentsWithoutDeps,
    'name',
  );

  return uniqueComponentsWithDeps;
}
832766
static StatsData aggregateStats(Collection<StatsData> dataSet) {
        StatsData combined = new StatsData();
        for (StatsData stats : dataSet) {
            if (stats.count == 0) {
                continue;
            }
            if (combined.total == 0) {
                combined.min = stats.min;
                combined.max = stats.max;
            }
            combined.total = combined.total + stats.total;
            combined.count = combined.count + stats.count;
            combined.mean = combined.total / combined.count;
            combined.min = Math.min(combined.min, stats.min);
            combined.max = Math.max(combined.max, stats.max);

            // Sum of the squares of the difference from the mean
            double meanDifference = stats.mean - combined.mean;
            combined.varianceNumeratorSum += stats.varianceNumeratorSum + stats.count * meanDifference * meanDifference;
        }
        return combined;
    }
1034748
function AggregatorFactory(aggregatorFn) {
	return metrics => {
		if (metrics.length === 0) return;
		const result = {
			help: metrics[0].help,
			name: metrics[0].name,
			type: metrics[0].type,
			values: [],
			aggregator: metrics[0].aggregator
		};
		// Gather metrics by metricName and labels.
		const byLabels = new Grouper();
		metrics.forEach(metric => {
			metric.values.forEach(value => {
				const key = hashObject(value.labels);
				byLabels.add(`${value.metricName}_${key}`, value);
			});
		});
		// Apply aggregator function to gathered metrics.
		byLabels.forEach(values => {
			if (values.length === 0) return;
			const valObj = {
				value: aggregatorFn(values),
				labels: values[0].labels
			};
			if (values[0].metricName) {
				valObj.metricName = values[0].metricName;
			}
			// NB: Timestamps are omitted.
			result.values.push(valObj);
		});
		return result;
	};
}
151484
private void decompose(int scalar, int[] decomposed)
        {
            for (int i = 0 ; i < decomposed.length ; i++)
            {
                int avg = generator.clusteringDescendantAverages[i];
                decomposed[i] = scalar / avg;
                scalar %= avg;
            }
            for (int i = lastRow.length - 1 ; i > 0 ; i--)
            {
                int avg = generator.clusteringComponentAverages[i];
                if (decomposed[i] >= avg)
                {
                    decomposed[i - 1] += decomposed[i] / avg;
                    decomposed[i] %= avg;
                }
            }
        }
1359565
function addAllToAggregation(aObjects) {
			for (var i = 0, len = aObjects.length; i < len; i++) {
				var vObject = aObjects[i];
				if ( Array.isArray(vObject) ) {
					addAllToAggregation(vObject);
				} else {
					that[oKeyInfo._sMutator](makeObject(vObject, oKeyInfo, oScope));
				}
			}
		}
1039008
public static String getAggregatedResultHuman(Map<String, LinkedHashSet<String>> aggregateResultMap){

        StringBuilder res = new StringBuilder();

        for (Entry<String, LinkedHashSet<String>> entry : aggregateResultMap
                .entrySet()) {
            LinkedHashSet<String> valueSet = entry.getValue(); 
            res.append("[" + entry.getKey() + " COUNT: " +valueSet.size() + " ]:\n");
            for(String str: valueSet){
                res.append("\t" + str + "\n");
            }
            res.append("###################################\n\n");
        }
        
        return res.toString();
        
    }
1173634
def _aggregate_dataset(self, obj, xcoords, ycoords):
        
        dim_labels = obj.dimensions(label=True)
        vdims = obj.dimensions()[2:]
        xdim, ydim = dim_labels[:2]
        shape = (len(ycoords), len(xcoords))
        nsamples = np.product(shape)
        grid_data = {xdim: xcoords, ydim: ycoords}

        ys, xs = cartesian_product([ycoords, xcoords], copy=True)
        data = {xdim: xs, ydim: ys}
        for vdim in vdims:
            values = np.empty(nsamples)
            values[:] = np.NaN
            data[vdim.name] = values
        dtype = default_datatype
        dense_data = Dataset(data, kdims=obj.kdims, vdims=obj.vdims, datatype=[dtype])
        concat_data = obj.interface.concatenate([dense_data, obj], datatype=dtype)
        reindexed = concat_data.reindex([xdim, ydim], vdims)
        if not reindexed:
            agg = reindexed
        elif pd:
            df = PandasInterface.as_dframe(reindexed)
            df = df.groupby([xdim, ydim], sort=False).first().reset_index()
            agg = reindexed.clone(df)
        else:
            agg = reindexed.aggregate([xdim, ydim], reduce_fn)

        # Convert data to a gridded dataset
        for vdim in vdims:
            grid_data[vdim.name] = agg.dimension_values(vdim).reshape(shape)
        return agg.clone(grid_data, kdims=[xdim, ydim], vdims=vdims,
                         datatype=self.p.datatype)
106470
function AggregateByKey(sc, dependencies, reducer, combiner, init, args) {
  Dataset.call(this, sc, dependencies);
  this.combiner = combiner;
  this.reducer = reducer;
  this.init = init;
  this.args = args;
  this.shuffling = true;
  this.executed = false;
  this.buffer = {};
  this.type = 'AggregateByKey';
}
371259
def get_aggregation(self, name):
        '''
        
        '''
        agg = self.aggregations[name]
        if 'buckets' in agg:
            return agg['buckets']
        else:
            return agg
341498
def aggregate(self, *args, **kwargs):
        
        obj = self._clone()
        obj._facets = [] #reset facets
        from django.db.models import Avg, Max, Min

        for arg in args:
            if isinstance(arg, (Avg, Max, Min)):

                field, djfield = self._django_to_es_field(arg.lookup)
                if not djfield:
                    obj = obj.annotate(field)
                from pyes.facets import StatisticalFacet

                obj = obj.annotate(StatisticalFacet(field, field))
        facets = obj.get_facets()
        #collecting results
        result = {}
        for name, values in facets.items():
            for k, v in values.items():
                if k.startswith("_"):
                    continue
                result[u'%s__%s' % (name, k)] = v

        return result