static/js/numeric.js
/**
 * Log-transform numbers in an array.
 * @function arrLog
 * @param {number[]} arr - input array
 * @param {boolean} clip - clip or mask non-positive values
 * @return {number[]} log-transformed array
 * @description The challenge is the non-positive values. If directly applying
 * Math.log, they become -Inf (for 0) or NaN (for negative numbers). One needs
 * to deal with them.
 * @see matplotlib.scale.LogScale
 */
Log-transform numbers in an array.
Log - transform numbers in an array .
function arrLog(arr, clip) {
  const n = arr.length;
  let x;
  if (!clip) {
    const res = [];
    for (let i = 0; i < n; i++) {
      x = arr[i];
      if (x > 0) {
        res.push(Math.log(x));
      }
    }
    return res;
  }
  else {
    const res = Array(n).fill();
    for (let i = 0; i < n; i++) {
      x = arr[i];
      res[i] = (x > 0) ? Math.log(x) : -1000;
    }
    return res;
  }
}
function arrLog ( arr , clip ) { const n = arr . length ; let x ; if ( ! clip ) { const res = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { x = arr [ i ] ; if ( x > 0 ) { res . push ( Math . log ( x ) ) ; } } return res ; } else { const res = Array ( n ) . fill ( ) ; for ( let i = 0 ; i < n ; i ++ ) { x = arr [ i ] ; res [ i ] = ( x > 0 ) ? Math . log ( x ) : - 1000 ; } return res ; } }
Log-transform numbers in an array.
Log - transform numbers in an array .
// mask non-positive numbers (i.e., drop them from array) // clip non-positive numbers (i.e., return a very small number) // this number is -1000, to be consistent with matplotlib: // https://github.com/matplotlib/matplotlib/blob/v3.5.1/lib/matplotlib/ // scale.py#L236