<commit_before>import {RangeEnum, ScaleInterpolate, SignalRef, TimeInterval, TimeIntervalStep} from 'vega-typings';
import {isString, toSet} from 'vega-util';
import * as CHANNEL from './channel';
import {Channel, CHANNELS, isColorChannel} from './channel';
import {DateTime} from './datetime';
import * as log from './log';
import {SelectionExtent} from './selection';
import * as TYPE from './type';
import {Type, TYPE_INDEX} from './type';
import {contains, Flag, keys} from './util';

export namespace ScaleType {
  // Continuous - Quantitative
  export const LINEAR: 'linear' = 'linear';
  export const LOG: 'log' = 'log';
  export const POW: 'pow' = 'pow';
  export const SQRT: 'sqrt' = 'sqrt';
  export const SYMLOG: 'symlog' = 'symlog';

  export const IDENTITY: 'identity' = 'identity';

  export const SEQUENTIAL: 'sequential' = 'sequential';

  // Continuous - Time
  export const TIME: 'time' = 'time';
  export const UTC: 'utc' = 'utc';

  // Discretizing scales
  export const QUANTILE: 'quantile' = 'quantile';
  export const QUANTIZE: 'quantize' = 'quantize';
  export const THRESHOLD: 'threshold' = 'threshold';
  export const BIN_ORDINAL: 'bin-ordinal' = 'bin-ordinal';

  // Discrete scales
  export const ORDINAL: 'ordinal' = 'ordinal';
  export const POINT: 'point' = 'point';
  export const BAND: 'band' = 'band';
}

export type ScaleType =
  | typeof ScaleType.LINEAR
  | typeof ScaleType.LOG
  | typeof ScaleType.POW
  | typeof ScaleType.SQRT
  | typeof ScaleType.SYMLOG
  | typeof ScaleType.IDENTITY
  | typeof ScaleType.SEQUENTIAL
  | typeof ScaleType.TIME
  | typeof ScaleType.UTC
  | typeof ScaleType.QUANTILE
  | typeof ScaleType.QUANTIZE
  | typeof ScaleType.THRESHOLD
  | typeof ScaleType.BIN_ORDINAL
  | typeof ScaleType.ORDINAL
  | typeof ScaleType.POINT
  | typeof ScaleType.BAND;

/**
 * Index for scale categories -- only scale of the same categories can be merged together.
 * Current implementation is trying to be conservative and avoid merging scale type that might not work together
 */
const SCALE_CATEGORY_INDEX: Record<ScaleType, ScaleType | 'numeric' | 'ordinal-position' | 'discretizing'> = {
  linear: 'numeric',
  log: 'numeric',
  pow: 'numeric',
  sqrt: 'numeric',
  symlog: 'numeric',
  identity: 'numeric',
  sequential: 'numeric',
  time: 'time',
  utc: 'time',
  ordinal: 'ordinal',
  'bin-ordinal': 'bin-ordinal', // TODO: should bin-ordinal support merging with other
  point: 'ordinal-position',
  band: 'ordinal-position',
  quantile: 'discretizing',
  quantize: 'discretizing',
  threshold: 'discretizing'
};

export const SCALE_TYPES = keys(SCALE_CATEGORY_INDEX) as ScaleType[];

/**
 * Whether the two given scale types can be merged together.
 */
export function scaleCompatible(scaleType1: ScaleType, scaleType2: ScaleType) {
  const scaleCategory1 = SCALE_CATEGORY_INDEX[scaleType1];
  const scaleCategory2 = SCALE_CATEGORY_INDEX[scaleType2];
  return (
    scaleCategory1 === scaleCategory2 ||
    (scaleCategory1 === 'ordinal-position' && scaleCategory2 === 'time') ||
    (scaleCategory2 === 'ordinal-position' && scaleCategory1 === 'time')
  );
}

/**
 * Index for scale precedence -- high score = higher priority for merging.
 */
const SCALE_PRECEDENCE_INDEX: Record<ScaleType, number> = {
  // numeric
  linear: 0,
  log: 1,
  pow: 1,
  sqrt: 1,
  symlog: 1,
  identity: 1,
  sequential: 1,
  // time
  time: 0,
  utc: 0,
  // ordinal-position -- these have higher precedence than continuous scales as they support more types of data
  point: 10,
  band: 11, // band has higher precedence as it is better for interaction
  // non grouped types
  ordinal: 0,
  'bin-ordinal': 0,
  quantile: 0,
  quantize: 0,
  threshold: 0
};

/**
 * Return scale categories -- only scale of the same categories can be merged together.
 */
export function scaleTypePrecedence(scaleType: ScaleType): number {
  return SCALE_PRECEDENCE_INDEX[scaleType];
}

export const CONTINUOUS_TO_CONTINUOUS_SCALES: ScaleType[] = ['linear', 'log', 'pow', 'sqrt', 'symlog', 'time', 'utc'];
const CONTINUOUS_TO_CONTINUOUS_INDEX = toSet(CONTINUOUS_TO_CONTINUOUS_SCALES);

export const QUANTITATIVE_SCALES: ScaleType[] = ['linear', 'log', 'pow', 'sqrt', 'symlog'];

const QUANTITATIVE_SCALES_INDEX = toSet(QUANTITATIVE_SCALES);

export function isQuantitative(type: ScaleType): type is 'linear' | 'log' | 'pow' | 'sqrt' | 'symlog' {
  return type in QUANTITATIVE_SCALES_INDEX;
}

export const CONTINUOUS_TO_DISCRETE_SCALES: ScaleType[] = ['quantile', 'quantize', 'threshold'];
const CONTINUOUS_TO_DISCRETE_INDEX = toSet(CONTINUOUS_TO_DISCRETE_SCALES);

export const CONTINUOUS_DOMAIN_SCALES: ScaleType[] = CONTINUOUS_TO_CONTINUOUS_SCALES.concat([
  'quantile',
  'quantize',
  'threshold'
]);
const CONTINUOUS_DOMAIN_INDEX = toSet(CONTINUOUS_DOMAIN_SCALES);

export const DISCRETE_DOMAIN_SCALES: ScaleType[] = ['ordinal', 'bin-ordinal', 'point', 'band'];
const DISCRETE_DOMAIN_INDEX = toSet(DISCRETE_DOMAIN_SCALES);

export const TIME_SCALE_TYPES: ScaleType[] = ['time', 'utc'];

export function hasDiscreteDomain(type: ScaleType): type is 'ordinal' | 'bin-ordinal' | 'point' | 'band' {
  return type in DISCRETE_DOMAIN_INDEX;<commit_msg>fix: make test passes again<commit_after>import {RangeEnum, ScaleInterpolate, SignalRef, TimeInterval, TimeIntervalStep} from 'vega-typings';
import {isString, toSet} from 'vega-util';
import * as CHANNEL from './channel';
import {Channel, CHANNELS, isColorChannel} from './channel';
import {DateTime} from './datetime';
import * as log from './log';
import {SelectionExtent} from './selection';
import * as TYPE from './type';
import {Type, TYPE_INDEX} from './type';
import {contains, Flag, keys} from './util';

export namespace ScaleType {
  // Continuous - Quantitative
  export const LINEAR: 'linear' = 'linear';
  export const LOG: 'log' = 'log';
  export const POW: 'pow' = 'pow';
  export const SQRT: 'sqrt' = 'sqrt';
  export const SYMLOG: 'symlog' = 'symlog';

  export const IDENTITY: 'identity' = 'identity';

  export const SEQUENTIAL: 'sequential' = 'sequential';

  // Continuous - Time
  export const TIME: 'time' = 'time';
  export const UTC: 'utc' = 'utc';

  // Discretizing scales
  export const QUANTILE: 'quantile' = 'quantile';
  export const QUANTIZE: 'quantize' = 'quantize';
  export const THRESHOLD: 'threshold' = 'threshold';
  export const BIN_ORDINAL: 'bin-ordinal' = 'bin-ordinal';

  // Discrete scales
  export const ORDINAL: 'ordinal' = 'ordinal';
  export const POINT: 'point' = 'point';
  export const BAND: 'band' = 'band';
}

export type ScaleType =
  | typeof ScaleType.LINEAR
  | typeof ScaleType.LOG
  | typeof ScaleType.POW
  | typeof ScaleType.SQRT
  | typeof ScaleType.SYMLOG
  | typeof ScaleType.IDENTITY
  | typeof ScaleType.SEQUENTIAL
  | typeof ScaleType.TIME
  | typeof ScaleType.UTC
  | typeof ScaleType.QUANTILE
  | typeof ScaleType.QUANTIZE
  | typeof ScaleType.THRESHOLD
  | typeof ScaleType.BIN_ORDINAL
  | typeof ScaleType.ORDINAL
  | typeof ScaleType.POINT
  | typeof ScaleType.BAND;

/**
 * Index for scale categories -- only scale of the same categories can be merged together.
 * Current implementation is trying to be conservative and avoid merging scale type that might not work together
 */
const SCALE_CATEGORY_INDEX: Record<ScaleType, ScaleType | 'numeric' | 'ordinal-position' | 'discretizing'> = {
  linear: 'numeric',
  log: 'numeric',
  pow: 'numeric',
  sqrt: 'numeric',
  symlog: 'numeric',
  identity: 'numeric',
  sequential: 'numeric',
  time: 'time',
  utc: 'time',
  ordinal: 'ordinal',
  'bin-ordinal': 'bin-ordinal', // TODO: should bin-ordinal support merging with other
  point: 'ordinal-position',
  band: 'ordinal-position',
  quantile: 'discretizing',
  quantize: 'discretizing',
  threshold: 'discretizing'
};

export const SCALE_TYPES = keys(SCALE_CATEGORY_INDEX) as ScaleType[];

/**
 * Whether the two given scale types can be merged together.
 */
export function scaleCompatible(scaleType1: ScaleType, scaleType2: ScaleType) {
  const scaleCategory1 = SCALE_CATEGORY_INDEX[scaleType1];
  const scaleCategory2 = SCALE_CATEGORY_INDEX[scaleType2];
  return (
    scaleCategory1 === scaleCategory2 ||
    (scaleCategory1 === 'ordinal-position' && scaleCategory2 === 'time') ||
    (scaleCategory2 === 'ordinal-position' && scaleCategory1 === 'time')
  );
}

/**
 * Index for scale precedence -- high score = higher priority for merging.
 */
const SCALE_PRECEDENCE_INDEX: Record<ScaleType, number> = {
  // numeric
  linear: 0,
  log: 1,
  pow: 1,
  sqrt: 1,
  symlog: 1,
  identity: 1,
  sequential: 1,
  // time
  time: 0,
  utc: 0,
  // ordinal-position -- these have higher precedence than continuous scales as they support more types of data
  point: 10,
  band: 11, // band has higher precedence as it is better for interaction
  // non grouped types
  ordinal: 0,
  'bin-ordinal': 0,
  quantile: 0,
  quantize: 0,
  threshold: 0
};

/**
 * Return scale categories -- only scale of the same categories can be merged together.
 */
export function scaleTypePrecedence(scaleType: ScaleType): number {
  return SCALE_PRECEDENCE_INDEX[scaleType];
}

export const CONTINUOUS_TO_CONTINUOUS_SCALES: ScaleType[] = ['linear', 'log', 'pow', 'sqrt', 'symlog', 'time', 'utc'];
const CONTINUOUS_TO_CONTINUOUS_INDEX = toSet(CONTINUOUS_TO_CONTINUOUS_SCALES);

export const QUANTITATIVE_SCALES: ScaleType[] = ['linear', 'log', 'pow', 'sqrt', 'symlog'];

const QUANTITATIVE_SCALES_INDEX = toSet(QUANTITATIVE_SCALES);

export function isQuantitative(type: ScaleType): type is 'linear' | 'log' | 'pow' | 'sqrt' | 'symlog' {
  return type in QUANTITATIVE_SCALES_INDEX;
}

export const CONTINUOUS_TO_DISCRETE_SCALES: ScaleType[] = ['quantile', 'quantize', 'threshold'];
const CONTINUOUS_TO_DISCRETE_INDEX = toSet(CONTINUOUS_TO_DISCRETE_SCALES);

export const CONTINUOUS_DOMAIN_SCALES: ScaleType[] = CONTINUOUS_TO_CONTINUOUS_SCALES.concat([
  'quantile',
  'quantize',
  'threshold',
  'sequential',
  'identity'
]);
const CONTINUOUS_DOMAIN_INDEX = toSet(CONTINUOUS_DOMAIN_SCALES);

export const DISCRETE_DOMAIN_SCALES: ScaleType[] = ['ordinal', 'bin-ordinal', 'point', 'band'];
const DISCRETE_DOMAIN_INDEX = toSet(DISCRETE_DOMAIN_SCALES);

export const TIME_SCALE_TYPES: ScaleType[] = ['time', 'utc'];

export function hasDiscreteDomain(type: ScaleType): type is 'ordinal' | 'bin-ordinal' | 'point' | 'band' {
  return type in DISCRETE_DOMAIN_INDEX;
{'fraction_non_alphanumeric': 0.12793608009241433, 'fraction_numerical': 0.005775895263765884, 'mean_word_length': 4.755678670360111, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5951988', 'n_tokens_mistral': 3673, 'n_tokens_neox': 3479, 'n_words': 973}