<commit_before>
define [
  "backbone",
  "common/has_properties",
], (Backbone, HasProperties) ->


  class LinearMapper extends HasProperties
    initialize: (attrs, options) ->
      super(attrs, options)

      @register_property('mapper_state', @_mapper_state, true)
      @add_dependencies('mapper_state', this, ['source_range', 'target_range'])
      @add_dependencies('mapper_state', @get('source_range'), ['start', 'end'])
      @add_dependencies('mapper_state', @get('target_range'), ['start', 'end'])

    map_to_target: (x) ->
      [scale, offset] = @get('mapper_state')
      return scale * x + offset

    v_map_to_target: (xs) ->
      [scale, offset] = @get('mapper_state')
      result = new Float64Array(xs.length)
      for x, idx in xs
        result[idx] = scale * x + offset
      return result

    map_from_target: (xprime) ->
      [scale, offset] = @get('mapper_state')
      return (xprime - offset) / scale

    v_map_from_target: (xprimes) ->
      [scale, offset] = @get('mapper_state')
      result = new Float64Array(xprimes.length)
      for xprime, idx in xprimes
        result[idx] = (xprime - offset) / scale
      return result

    _mapper_state: () ->
      #
      #  (t1 - t0)       (t1 - t0)
      #  --------- * x - --------- * s0 + t0
      #  (s1 - s0)       (s1 - s0)
      #
      # [  scale  ]     [    offset    ]
      #
      source_start = @get('source_range').get('start')
      source_end   = @get('source_range').get('end')
      target_start = @get('target_range').get('start')
      target_end   = @get('target_range').get('end')
      scale = (target_end - target_start)/(source_end - source_start)
      offset = -(scale * source_start) + target_start
      return [scale, offset]

  class LinearMappers extends Backbone.Collection
    model: LinearMapper

  return {
    "Model": LinearMapper,
    "Collection": new LinearMappers()
  }<commit_msg>Updated log_mapper<commit_after>
define [
  "backbone",
  "common/has_properties",
], (Backbone, HasProperties) ->


  class LogMapper extends HasProperties
    initialize: (attrs, options) ->
      super(attrs, options)

      @register_property('mapper_state', @_mapper_state, true)
      @add_dependencies('mapper_state', this, ['source_range', 'target_range'])
      @add_dependencies('mapper_state', @get('source_range'), ['start', 'end'])
      @add_dependencies('mapper_state', @get('target_range'), ['start', 'end'])

    map_to_target: (x) ->
      [scale, offset] = @get('mapper_state')
      return scale * x + offset

    v_map_to_target: (xs) ->
      [scale, offset, inter_scale, inter_offset] = @get('mapper_state')
      
      intermediate = new Float64Array(xs.length)
      result = new Float64Array(xs.length)

      if inter_scale == 0
        intermediate = xs.map (i) -> i * 0
      else
          
        try
          mask1 = xs.map (i) -> i <= 0
          mask2 = xs.map (i) -> isNaN(i)

          mask = (mask1[i] | mask2[i] for i in [0...xs.length])

          mask = mask.reduce (x, y) -> x || y
          
          if mask == 1
            xs[mask] = 1

          intermediate = xs.map (i) -> (Math.log(i) - inter_offset) / inter_scale
          
          for x, idx in intermediate
            if isNaN(intermediate[idx])
              throw "NaN"
            if isFinite(intermediate[idx]) == False
              throw "Infinite"

        catch error
          intermediate = xs.map (i) -> i * 0

      for x, idx in xs
        result[idx] = intermediate[idx] * scale + offset
      
      return result

    map_from_target: (xprime) ->
      [scale, offset] = @get('mapper_state')
      return (xprime - offset) / scale

    v_map_from_target: (xprimes) ->
      [scale, offset] = @get('mapper_state')
      result = new Float64Array(xprimes.length)
      for xprime, idx in xprimes
        result[idx] = (xprime - offset) / scale
      return result

    _get_safe_scale: (orig_start, orig_end) ->
      if orig_start < 0
        start = 0
      else
        start = orig_start

      if orig_end < 0
        end = 0
      else
        end = orig_end

      if start == end
        if start == 0
          start = 1
          end = 10
        else
          log_val = Math.log(start) / Math.log(10)
          start = Math.pow(10, Math.floor(log_val))

          if Math.ceil(log_val) != Math.floor(log_val)
            end = Math.pow(10, Math.ceil(log_val))
          else
            end = Math.pow(10, Math.ceil(log_val) + 1)

      return [start, end]

    _mapper_state: () ->
      #
      #  (t1 - t0)       (t1 - t0)
      #  --------- * x - --------- * s0 + t0
      #  (s1 - s0)       (s1 - s0)
      #
      # [  scale  ]     [    offset    ]
      #
      source_start = @get('source_range').get('start')
      source_end   = @get('source_range').get('end')
      target_start = @get('target_range').get('start')
      target_end   = @get('target_range').get('end')

      #scale = (target_end - target_start)/(source_end - source_start)
      #offset = -(scale * source_start) + target_start
      
      screen_range = target_start - target_end

      [start, end] = @_get_safe_scale(source_start, source_end)
      
      if start == 0
        inter_scale = Math.log(end)
        inter_offset = 0
      else
        inter_scale = Math.log(end) - Math.log(start)
        inter_offset = Math.log(start)

      scale = screen_range
      offset = target_start
      return [scale, offset, inter_scale, inter_offset]


  class LogMappers extends Backbone.Collection
    model: LogMapper

  return {
    "Model": LogMapper,
    "Collection": new LogMappers()
  }
{'fraction_non_alphanumeric': 0.13939825529642158, 'fraction_numerical': 0.010681858643403952, 'mean_word_length': 2.13329615170106, 'pattern_counts': {'":': 4, '<': 6, '<?xml version=': 0, '>': 24, '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': '2758370', 'n_tokens_mistral': 1891, 'n_tokens_neox': 1811, 'n_words': 408}