<commit_before>module Seer

  # Geomap creates a map of a country, continent, or region, with colors and values assigned to
  # specific regions. Values are displayed as a color scale, and you can specify optional hovertext 
  # for regions.
  #
  # =USAGE=
  # 
  # In your view:
  #
  #   <div id="my_geomap_container" class="chart"></div>
  #
  #   <%= Seer::visualize(
  #         @widgets,
  #         :as => :geomap,
  #         :in_element => 'my_geomap_container',
  #         :series => {
  #           :series_label => 'name',
  #           :data_label => '# widgets',
  #           :data_method => 'quantity'
  #         },
  #         :chart_options => {
  #           :data_mode => 'regions',
  #           :region => 'US',
  #         }  
  #       )
  #   -%>
  #
  # ==@widgets==
  #
  # A collection of objects (ActiveRecord or otherwise) that must respond to the
  # following methods:
  #
  #   latitude  # => returns the latitude in decimal format
  #   longitude # => returns the longitude in decimal format
  #   geocoded? # => result of latitude && longitude
  #
  # For details on the chart options, see the Google API docs at 
  # http://code.google.com/apis/visualization/documentation/gallery/geomap.html
  #
  class Geomap
  
    include Seer::Chart

    COUNTRY_CODES = ['world', 'AX', 'AF', 'AL', 'DZ', 'AS', 'AD', 'AO', 'AI', 'AQ', 'AG', 'AR', 'AM', 'AW', 'AU', 'AT', 'AZ', 'BS', 'BH', 'BD', 'BB', 'BY', 'BE', 'BZ', 'BJ', 'BM', 'BT', 'BO', 'BA', 'BW', 'BV', 'BR', 'IO', 'BN', 'BG', 'BF', 'BI', 'KH', 'CM', 'CA', 'CV', 'KY', 'CF', 'TD', 'CL', 'CN', 'CX', 'CC', 'CO', 'KM', 'CD', 'CG', 'CK', 'CR', 'CI', 'HR', 'CU', 'CY', 'CZ', 'DK', 'DJ', 'DM', 'DO', 'EC', 'EG', 'SV', 'GQ', 'ER', 'EE', 'ET', 'FK', 'FO', 'FJ', 'FI', 'FR', 'GF', 'PF', 'TF', 'GA', 'GM', 'GE', 'DE', 'GH', 'GI', 'GR', 'GL', 'GD', 'GP', 'GU', 'GT', 'GN', 'GW', 'GY', 'HT', 'HM', 'HN', 'HK', 'HU', 'IS', 'IN', 'ID', 'IR', 'IQ', 'IE', 'IL', 'IT', 'JM', 'JP', 'JO', 'KZ', 'KE', 'KI', 'KP', 'KR', 'KW', 'KG', 'LA', 'LV', 'LB', 'LS', 'LR', 'LY', 'LI', 'LT', 'LU', 'MO', 'MK', 'MG', 'MW', 'MY', 'MV', 'ML', 'MT', 'MH', 'MQ', 'MR', 'MU', 'YT', 'MX', 'FM', 'MD', 'MC', 'MN', 'MS', 'MA', 'MZ', 'MM', 'NA', 'NR', 'NP', 'NL', 'AN', 'NC', 'NZ', 'NI', 'NE', 'NG', 'NU', 'NF', 'MP', 'NO', 'OM', 'PK', 'PW', 'PS', 'PA', 'PG', 'PY', 'PE', 'PH', 'PN', 'PL', 'PT', 'PR', 'QA', 'RE', 'RO', 'RU', 'RW', 'SH', 'KN', 'LC', 'PM', 'VC', 'WS', 'SM', 'ST', 'SA', 'SN', 'CS', 'SC', 'SL', 'SG', 'SK', 'SI', 'SB', 'SO', 'ZA', 'GS', 'ES', 'LK', 'SD', 'SR', 'SJ', 'SZ', 'SE', 'CH', 'SY', 'TW', 'TJ', 'TZ', 'TH', 'TL', 'TG', 'TK', 'TO', 'TT', 'TN', 'TR', 'TM', 'TC', 'TV', 'UG', 'UA', 'AE', 'GB', 'US', 'us_metro', 'UM', 'UY', 'UZ', 'VU', 'VA', 'VE', 'VN', 'VG', 'VI', 'WF', 'EH', 'YE', 'ZM', 'ZW', '005', '013', '021', '002', '017', '015', '018', '030', '034', '035', '143', '145', '151', '154', '155', '039']
  
    # Chart options accessors
    attr_accessor :data_mode, :enable_tooltip, :height, :legend_background_color, :legend_font_size, :legend_text_color, :legend, :region, :show_legend, :show_zoom_out, :title_color, :title_font_size, :title_x, :title_y, :title, :tooltip_font_size, :tooltip_height, :tooltip_width, :width, :zoom_out_label
    
    # Graph data
    attr_accessor :data, :data_label, :data_method, :data_table, :label_method
    
    def initialize(args={}) #:nodoc:
    
      # Standard options
      args.each{ |method,arg| self.send("#{method}=",arg) if self.respond_to?(method) }

      # Chart options
      args[:chart_options].each{ |method, arg| self.send("#{method}=",arg) if self.respond_to?(method) }

      # Handle defaults
      @colors ||= args[:chart_options][:colors] || DEFAULT_COLORS
      @legend ||= args[:chart_options][:legend] || DEFAULT_LEGEND_LOCATION
      @height ||= args[:chart_options][:height] || DEFAULT_HEIGHT
      @width  ||= args[:chart_options][:width]  || DEFAULT_WIDTH

    end
  
    def data_columns #:nodoc:
      _data_columns = "data.addRows(#{data_table.size});"
      if self.data_mode == 'markers'
        _data_columns << %{
          data.addColumn('number', 'LATITUDE');
          data.addColumn('number', 'LONGITUDE');
          data.addColumn('number', '#{data_method}');
          data.addColumn('string', '#{label_method}');
        }
      else
        _data_columns << "          data.addColumn('string', '#{label_method}');"
        _data_columns << "          data.addColumn('number', '#{data_method}');"
        _data_columns
      end
      _data_columns
    end
    
    def data_mode=(mode) #:nodoc:
      raise ArgumentError, "Invalid data mode option: #{mode}. Must be one of 'regions' or 'markers'." unless ['regions', 'markers'].include?(mode)
      @data_mode = mode
    end

    def data_table #:nodoc:
      @data_table = []
      data.each_with_index do |datum, column|
        next unless datum.geocoded?
        if data_mode == "markers"
          @data_table << [
            "            data.setValue(#{column}, 0, #{datum.latitude});\r",
            "            data.setValue(#{column}, 1, #{datum.longitude});\r",
            "            data.setValue(#{column}, 2, #{datum.send(data_method)});\r",
            "            data.setValue(#{column}, 3, '#{datum.send(label_method)}');\r"
          ]
        else # Regions
          @data_table << [
            "            data.setValue(#{column}, 0, '#{datum.name}');\r",
            "            data.setValue(#{column}, 1, #{datum.send(data_method)});\r"
          ]
        end
      end
      @data_table
    end
  
    # Because Google is not consistent in their @#!$ API...
    def formatted_colors
      "[#{@colors.map{|color| "'#{color.gsub(/\#/,'0x')}'"} * ','}]"
    end<commit_msg>filtering geocoded data before going into each-loop. now your columns are not counted if record is not geocoded. Otherwise it will throw an error if you start with an index wich is not zero.<commit_after>module Seer

  # Geomap creates a map of a country, continent, or region, with colors and values assigned to
  # specific regions. Values are displayed as a color scale, and you can specify optional hovertext 
  # for regions.
  #
  # =USAGE=
  # 
  # In your view:
  #
  #   <div id="my_geomap_container" class="chart"></div>
  #
  #   <%= Seer::visualize(
  #         @widgets,
  #         :as => :geomap,
  #         :in_element => 'my_geomap_container',
  #         :series => {
  #           :series_label => 'name',
  #           :data_label => '# widgets',
  #           :data_method => 'quantity'
  #         },
  #         :chart_options => {
  #           :data_mode => 'regions',
  #           :region => 'US',
  #         }  
  #       )
  #   -%>
  #
  # ==@widgets==
  #
  # A collection of objects (ActiveRecord or otherwise) that must respond to the
  # following methods:
  #
  #   latitude  # => returns the latitude in decimal format
  #   longitude # => returns the longitude in decimal format
  #   geocoded? # => result of latitude && longitude
  #
  # For details on the chart options, see the Google API docs at 
  # http://code.google.com/apis/visualization/documentation/gallery/geomap.html
  #
  class Geomap
  
    include Seer::Chart

    COUNTRY_CODES = ['world', 'AX', 'AF', 'AL', 'DZ', 'AS', 'AD', 'AO', 'AI', 'AQ', 'AG', 'AR', 'AM', 'AW', 'AU', 'AT', 'AZ', 'BS', 'BH', 'BD', 'BB', 'BY', 'BE', 'BZ', 'BJ', 'BM', 'BT', 'BO', 'BA', 'BW', 'BV', 'BR', 'IO', 'BN', 'BG', 'BF', 'BI', 'KH', 'CM', 'CA', 'CV', 'KY', 'CF', 'TD', 'CL', 'CN', 'CX', 'CC', 'CO', 'KM', 'CD', 'CG', 'CK', 'CR', 'CI', 'HR', 'CU', 'CY', 'CZ', 'DK', 'DJ', 'DM', 'DO', 'EC', 'EG', 'SV', 'GQ', 'ER', 'EE', 'ET', 'FK', 'FO', 'FJ', 'FI', 'FR', 'GF', 'PF', 'TF', 'GA', 'GM', 'GE', 'DE', 'GH', 'GI', 'GR', 'GL', 'GD', 'GP', 'GU', 'GT', 'GN', 'GW', 'GY', 'HT', 'HM', 'HN', 'HK', 'HU', 'IS', 'IN', 'ID', 'IR', 'IQ', 'IE', 'IL', 'IT', 'JM', 'JP', 'JO', 'KZ', 'KE', 'KI', 'KP', 'KR', 'KW', 'KG', 'LA', 'LV', 'LB', 'LS', 'LR', 'LY', 'LI', 'LT', 'LU', 'MO', 'MK', 'MG', 'MW', 'MY', 'MV', 'ML', 'MT', 'MH', 'MQ', 'MR', 'MU', 'YT', 'MX', 'FM', 'MD', 'MC', 'MN', 'MS', 'MA', 'MZ', 'MM', 'NA', 'NR', 'NP', 'NL', 'AN', 'NC', 'NZ', 'NI', 'NE', 'NG', 'NU', 'NF', 'MP', 'NO', 'OM', 'PK', 'PW', 'PS', 'PA', 'PG', 'PY', 'PE', 'PH', 'PN', 'PL', 'PT', 'PR', 'QA', 'RE', 'RO', 'RU', 'RW', 'SH', 'KN', 'LC', 'PM', 'VC', 'WS', 'SM', 'ST', 'SA', 'SN', 'CS', 'SC', 'SL', 'SG', 'SK', 'SI', 'SB', 'SO', 'ZA', 'GS', 'ES', 'LK', 'SD', 'SR', 'SJ', 'SZ', 'SE', 'CH', 'SY', 'TW', 'TJ', 'TZ', 'TH', 'TL', 'TG', 'TK', 'TO', 'TT', 'TN', 'TR', 'TM', 'TC', 'TV', 'UG', 'UA', 'AE', 'GB', 'US', 'us_metro', 'UM', 'UY', 'UZ', 'VU', 'VA', 'VE', 'VN', 'VG', 'VI', 'WF', 'EH', 'YE', 'ZM', 'ZW', '005', '013', '021', '002', '017', '015', '018', '030', '034', '035', '143', '145', '151', '154', '155', '039']
  
    # Chart options accessors
    attr_accessor :data_mode, :enable_tooltip, :height, :legend_background_color, :legend_font_size, :legend_text_color, :legend, :region, :show_legend, :show_zoom_out, :title_color, :title_font_size, :title_x, :title_y, :title, :tooltip_font_size, :tooltip_height, :tooltip_width, :width, :zoom_out_label
    
    # Graph data
    attr_accessor :data, :data_label, :data_method, :data_table, :label_method
    
    def initialize(args={}) #:nodoc:
    
      # Standard options
      args.each{ |method,arg| self.send("#{method}=",arg) if self.respond_to?(method) }

      # Chart options
      args[:chart_options].each{ |method, arg| self.send("#{method}=",arg) if self.respond_to?(method) }

      # Handle defaults
      @colors ||= args[:chart_options][:colors] || DEFAULT_COLORS
      @legend ||= args[:chart_options][:legend] || DEFAULT_LEGEND_LOCATION
      @height ||= args[:chart_options][:height] || DEFAULT_HEIGHT
      @width  ||= args[:chart_options][:width]  || DEFAULT_WIDTH

    end
  
    def data_columns #:nodoc:
      _data_columns = "data.addRows(#{data_table.size});"
      if self.data_mode == 'markers'
        _data_columns << %{
          data.addColumn('number', 'LATITUDE');
          data.addColumn('number', 'LONGITUDE');
          data.addColumn('number', '#{data_method}');
          data.addColumn('string', '#{label_method}');
        }
      else
        _data_columns << "          data.addColumn('string', '#{label_method}');"
        _data_columns << "          data.addColumn('number', '#{data_method}');"
        _data_columns
      end
      _data_columns
    end
    
    def data_mode=(mode) #:nodoc:
      raise ArgumentError, "Invalid data mode option: #{mode}. Must be one of 'regions' or 'markers'." unless ['regions', 'markers'].include?(mode)
      @data_mode = mode
    end

    def data_table #:nodoc:
      @data_table = []
      data.select{ |d| d.geocoded? }.each_with_index do |datum, column|
        if data_mode == "markers"
          @data_table << [
            "            data.setValue(#{column}, 0, #{datum.latitude});\r",
            "            data.setValue(#{column}, 1, #{datum.longitude});\r",
            "            data.setValue(#{column}, 2, #{datum.send(data_method)});\r",
            "            data.setValue(#{column}, 3, '#{datum.send(label_method)}');\r"
          ]
        else # Regions
          @data_table << [
            "            data.setValue(#{column}, 0, '#{datum.name}');\r",
            "            data.setValue(#{column}, 1, #{datum.send(data_method)});\r"
          ]
        end
      end
      @data_table
    end
  
    # Because Google is not consistent in their @#!$ API...
    def formatted_colors
      "[#{@colors.map{|color| "'#{color.gsub(/\#/,'0x')}'"} * ','}]"
    end
{'fraction_non_alphanumeric': 0.24191461836998707, 'fraction_numerical': 0.009486847779215178, 'mean_word_length': 2.9401970778117565, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 33, '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': '1955614', 'n_tokens_mistral': 4536, 'n_tokens_neox': 4305, 'n_words': 1110}