<commit_before>/*
 * Copyright 2017 Spotify AB.
 *
 * 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.
 */

package com.spotify.featran.transformers

import java.util.{TreeMap => JTreeMap}

import com.spotify.featran.FeatureBuilder
import com.twitter.algebird.{Aggregator, QTree, QTreeAggregator, QTreeSemigroup}

import scala.collection.JavaConverters._

object QuantileDiscretizer {
  /**
   * Transform a column of continuous features to n columns of binned categorical features. The
   * number of bins is set by the `numBuckets` parameter.
   *
   * The bin ranges are chosen using the Algebird's QTree approximate data structure. The precision
   * of the approximation can be controlled with the `k` parameter.
   *
   * Missing values are transformed to zero vectors.
   */
  def apply(name: String, numBuckets: Int = 2, k: Int = QTreeAggregator.DefaultK)
  : Transformer[Double, QTree[Double], JTreeMap[Double, Int]] =
    new QuantileDiscretizer(name, numBuckets, k)
}

private class QuantileDiscretizer(name: String, val numBuckets: Int, val k: Int)
  extends Transformer[Double, QTree[Double], JTreeMap[Double, Int]](name) {
  require(numBuckets >= 2, "numBuckets must be >= 2")
  implicit val sg = new QTreeSemigroup[Double](k)
  override val aggregator: Aggregator[Double, QTree[Double], JTreeMap[Double, Int]] =
    Aggregators.from[Double](QTree(_)).to { qt =>
      val m = new JTreeMap[Double, Int]()  // upper bound -> offset
      val interval = 1.0 / numBuckets
      for (i <- 1 until numBuckets) {
        val (l, u) = qt.quantileBounds(interval * i)
        val k = l + (u - l) / 2
        if (!m.containsKey(k)) { // in case of too few distinct values
          m.put(k, i - 1)
        }<commit_msg>fix double overflow in QuantileDiscretizer<commit_after>/*
 * Copyright 2017 Spotify AB.
 *
 * 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.
 */

package com.spotify.featran.transformers

import java.util.{TreeMap => JTreeMap}

import com.spotify.featran.FeatureBuilder
import com.twitter.algebird.{Aggregator, QTree, QTreeAggregator, QTreeSemigroup}

import scala.collection.JavaConverters._

object QuantileDiscretizer {
  /**
   * Transform a column of continuous features to n columns of binned categorical features. The
   * number of bins is set by the `numBuckets` parameter.
   *
   * The bin ranges are chosen using the Algebird's QTree approximate data structure. The precision
   * of the approximation can be controlled with the `k` parameter.
   *
   * Missing values are transformed to zero vectors.
   */
  def apply(name: String, numBuckets: Int = 2, k: Int = QTreeAggregator.DefaultK)
  : Transformer[Double, QTree[Double], JTreeMap[Double, Int]] =
    new QuantileDiscretizer(name, numBuckets, k)
}

private class QuantileDiscretizer(name: String, val numBuckets: Int, val k: Int)
  extends Transformer[Double, QTree[Double], JTreeMap[Double, Int]](name) {
  require(numBuckets >= 2, "numBuckets must be >= 2")
  implicit val sg = new QTreeSemigroup[Double](k)
  override val aggregator: Aggregator[Double, QTree[Double], JTreeMap[Double, Int]] =
    Aggregators.from[Double](QTree(_)).to { qt =>
      val m = new JTreeMap[Double, Int]()  // upper bound -> offset
      val interval = 1.0 / numBuckets
      for (i <- 1 until numBuckets) {
        val (l, u) = qt.quantileBounds(interval * i)
        val k = l / 2 + u / 2 // (l + u) might overflow
        if (!m.containsKey(k)) { // in case of too few distinct values
          m.put(k, i - 1)
        }
{'fraction_non_alphanumeric': 0.09811738648947951, 'fraction_numerical': 0.007308970099667774, 'mean_word_length': 3.5940996948118005, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6746590', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1337, 'n_words': 498}