<commit_before>/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.

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.
==============================================================================*/
/* tslint:disable:no-namespace variable-name */
module VZ.ChartHelpers {
  export interface Datum {
    wall_time: Date;
    step: number;
  }

  export interface Scalar {
    scalar: number;
    smoothed: number;
  }

  export type ScalarDatum = Datum & Scalar;

  export type DataFn = (run: string, tag: string) =>
      Promise<Array<Datum>>;

  export let Y_TOOLTIP_FORMATTER_PRECISION = 4;
  export let STEP_FORMATTER_PRECISION = 4;
  export let Y_AXIS_FORMATTER_PRECISION = 3;
  export let TOOLTIP_Y_PIXEL_OFFSET = 20;
  export let TOOLTIP_CIRCLE_SIZE = 4;
  export let NAN_SYMBOL_SIZE = 6;

  export interface Point {
    x: number;  // pixel space
    y: number;  // pixel space
    datum: ScalarDatum;
    dataset: Plottable.Dataset;
  }

  /* Create a formatter function that will switch between exponential and
   * regular display depending on the scale of the number being formatted,
   * and show `digits` significant digits.
   */
  export function multiscaleFormatter(digits: number): ((v: number) => string) {
    return (v: number) => {
      let absv = Math.abs(v);
      if (absv < 1E-15) {
        // Sometimes zero-like values get an annoying representation
        absv = 0;
      }
      let f: (x: number) => string;
      if (absv >= 1E4) {
        f = d3.format('.' + digits + 'e');
      } else if (absv > 0 && absv < 0.01) {
        f = d3.format('.' + digits + 'e');
      } else {
        f = d3.format('.' + digits + 'g');
      }
      return f(v);
    };
  }

  /* Compute an appropriate domain given an array of all the values that are
   * going to be displayed. If ignoreOutliers is true, it will ignore the
   * lowest 10% and highest 10% of the data when computing a domain.
   * It has n log n performance when ignoreOutliers is true, as it needs to
   * sort the data.
   */
  export function computeDomain(values: number[], ignoreOutliers: boolean) {
    if (values.length === 0) {
      return [-0.1, 1.1];
    }
    let a: number;
    let b: number;
    if (ignoreOutliers) {
      let sorted = _.sortBy(values);
      a = d3.quantile(sorted, 0.10);
      b = d3.quantile(sorted, 0.90);
    } else {
      a = d3.min(values);
      b = d3.max(values);
    }
    // When the data all fits into the unit interval, we switch to a consistent
    // domain for unit data. This is helpful for proportional parameters like
    // error rates or % of queue that is full. This way, users can meaningfully
    // compare charts and see information at a glance (if the value is always
    // 1, it appears at top of the chart, 0 is bottom, etc.)
    if (a >= 0 && b <= 1) {
      return [-0.1, 1.1];
    }
    let padding = (b - a) * 0.20;
    let domain = [a - padding, b + padding];
    domain = d3.scale.linear().domain(domain).nice().domain();
    return domain;
  }

  export function accessorize(key: string): Plottable.Accessor<number> {
    return (d: any, index: number, dataset: Plottable.Dataset) => d[key];
  }

  export interface XComponents {
    /* tslint:disable */
    scale: Plottable.Scales.Linear|Plottable.Scales.Time,
        axis: Plottable.Axes.Numeric|Plottable.Axes.Time,
        accessor: Plottable.Accessor<number|Date>,
    /* tslint:enable */
  }

  export let stepFormatter =
      Plottable.Formatters.siSuffix(STEP_FORMATTER_PRECISION);
  export function stepX(): XComponents {<commit_msg>Scalar dashboard charts no longer try to choose range [0,1] when all data in that interval.<commit_after>/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.

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.
==============================================================================*/
/* tslint:disable:no-namespace variable-name */
module VZ.ChartHelpers {
  export interface Datum {
    wall_time: Date;
    step: number;
  }

  export interface Scalar {
    scalar: number;
    smoothed: number;
  }

  export type ScalarDatum = Datum & Scalar;

  export type DataFn = (run: string, tag: string) =>
      Promise<Array<Datum>>;

  export let Y_TOOLTIP_FORMATTER_PRECISION = 4;
  export let STEP_FORMATTER_PRECISION = 4;
  export let Y_AXIS_FORMATTER_PRECISION = 3;
  export let TOOLTIP_Y_PIXEL_OFFSET = 20;
  export let TOOLTIP_CIRCLE_SIZE = 4;
  export let NAN_SYMBOL_SIZE = 6;

  export interface Point {
    x: number;  // pixel space
    y: number;  // pixel space
    datum: ScalarDatum;
    dataset: Plottable.Dataset;
  }

  /* Create a formatter function that will switch between exponential and
   * regular display depending on the scale of the number being formatted,
   * and show `digits` significant digits.
   */
  export function multiscaleFormatter(digits: number): ((v: number) => string) {
    return (v: number) => {
      let absv = Math.abs(v);
      if (absv < 1E-15) {
        // Sometimes zero-like values get an annoying representation
        absv = 0;
      }
      let f: (x: number) => string;
      if (absv >= 1E4) {
        f = d3.format('.' + digits + 'e');
      } else if (absv > 0 && absv < 0.01) {
        f = d3.format('.' + digits + 'e');
      } else {
        f = d3.format('.' + digits + 'g');
      }
      return f(v);
    };
  }

  /* Compute an appropriate domain given an array of all the values that are
   * going to be displayed. If ignoreOutliers is true, it will ignore the
   * lowest 10% and highest 10% of the data when computing a domain.
   * It has n log n performance when ignoreOutliers is true, as it needs to
   * sort the data.
   */
  export function computeDomain(values: number[], ignoreOutliers: boolean) {
    if (values.length === 0) {
      return [-0.1, 1.1];
    }
    let a: number;
    let b: number;
    if (ignoreOutliers) {
      let sorted = _.sortBy(values);
      a = d3.quantile(sorted, 0.10);
      b = d3.quantile(sorted, 0.90);
    } else {
      a = d3.min(values);
      b = d3.max(values);
    }

    // If the values are in unit range, try to give them a clean [0, 1] range,
    // but if all the values are less than 0.1 then we will construct a smaller
    // power-of-ten window for them.
    if (a >= 0 && b <= 1) {
      if (b === 0) {
        return [-0.1, 1.1];
      }
      let powerOf10 = Math.pow(10, Math.ceil(Math.log(b) / Math.log(10)));
      let offset = powerOf10 / 10;
      return [-offset, powerOf10 + offset];
    }

    let padding = (b - a) * 0.20;
    let domain = [a - padding, b + padding];
    domain = d3.scale.linear().domain(domain).nice().domain();
    return domain;
  }

  export function accessorize(key: string): Plottable.Accessor<number> {
    return (d: any, index: number, dataset: Plottable.Dataset) => d[key];
  }

  export interface XComponents {
    /* tslint:disable */
    scale: Plottable.Scales.Linear|Plottable.Scales.Time,
        axis: Plottable.Axes.Numeric|Plottable.Axes.Time,
        accessor: Plottable.Accessor<number|Date>,
    /* tslint:enable */
  }

  export let stepFormatter =
      Plottable.Formatters.siSuffix(STEP_FORMATTER_PRECISION);
  export function stepX(): XComponents {
{'fraction_non_alphanumeric': 0.12521440823327615, 'fraction_numerical': 0.01654006370987503, 'mean_word_length': 2.8723908918406074, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4230699', 'n_tokens_mistral': 2652, 'n_tokens_neox': 2501, 'n_words': 889}