class Charty::Plotters::LinePlotter

Constants

DEFAULT_ERROR_BAR_LEVELS
VALID_ERROR_BAR_METHODS

Attributes

err_kws[R]
err_style[R]
error_bar[R]
estimator[R]
n_boot[R]
sort[R]
x_scale[R]
y_scale[R]

Public Class Methods

new(data: nil, variables: {}, **options, &block) click to toggle source
Calls superclass method
# File lib/charty/plotters/line_plotter.rb, line 70
def initialize(data: nil, variables: {}, **options, &block)
  x, y, color, style, size = variables.values_at(:x, :y, :color, :style, :size)
  super(x, y, color, style, size, data: data, **options, &block)

  @comp_data = nil
end

Public Instance Methods

err_params=(val) click to toggle source

parameters to draw error bars/bands

# File lib/charty/plotters/line_plotter.rb, line 146
def err_params=(val)
  unless val.nil?
    raise NotImplementedError,
          "Specifying `err_params` is not supported"
  end
end
err_style=(val) click to toggle source
# File lib/charty/plotters/line_plotter.rb, line 131
def err_style=(val)
  @err_style = check_err_style(val)
end
error_bar=(val) click to toggle source

The method and level to calculate error bars/bands

# File lib/charty/plotters/line_plotter.rb, line 154
def error_bar=(val)
  @error_bar = check_error_bar(val)
end
estimator=(estimator) click to toggle source
# File lib/charty/plotters/line_plotter.rb, line 79
def estimator=(estimator)
  @estimator = check_estimator(estimator)
end
n_boot=(n_boot) click to toggle source
# File lib/charty/plotters/line_plotter.rb, line 105
def n_boot=(n_boot)
  @n_boot = check_n_boot(n_boot)
end
sort=(val) click to toggle source
# File lib/charty/plotters/line_plotter.rb, line 127
def sort=(val)
  @sort = check_boolean(val, :sort)
end
x_scale=(val) click to toggle source
# File lib/charty/plotters/line_plotter.rb, line 216
def x_scale=(val)
  @x_scale = check_axis_scale(val, :x)
end
y_scale=(val) click to toggle source
# File lib/charty/plotters/line_plotter.rb, line 222
def y_scale=(val)
  @y_scale = check_axis_scale(val, :y)
end

Private Instance Methods

annotate_axes(backend) click to toggle source
# File lib/charty/plotters/line_plotter.rb, line 320
        def annotate_axes(backend)
  backend.set_title(self.title) if self.title

  xlabel = self.x_label || self.variables[:x]
  ylabel = self.y_label || self.variables[:y]
  backend.set_xlabel(xlabel) unless xlabel.nil?
  backend.set_ylabel(ylabel) unless ylabel.nil?

  backend.set_xscale(x_scale)
  backend.set_yscale(y_scale)
end
check_axis_scale(val, axis) click to toggle source
# File lib/charty/plotters/line_plotter.rb, line 226
        def check_axis_scale(val, axis)
  case val
  when :linear, "linear", :log, "log"
    val.to_sym
  else
    raise ArgumentError,
          "The value of `#{axis}_scale` is worng: %p" % val,
          caller
  end
end
check_err_style(val) click to toggle source
# File lib/charty/plotters/line_plotter.rb, line 135
        def check_err_style(val)
  case val
  when :bars, "bars", :band, "band"
    val.to_sym
  else
    raise ArgumentError,
          "Invalid value for err_style (%p for :band or :bars)" % val
  end
end
check_error_bar(val) click to toggle source
# File lib/charty/plotters/line_plotter.rb, line 169
        def check_error_bar(val)
  case val
  when nil
    return [nil, nil]
  when ->(x) { x.respond_to?(:call) }
    return [val, nil]
  when *VALID_ERROR_BAR_METHODS
    method = val.to_sym
    level = nil
  when Array
    if val.length != 2
      raise ArgumentError,
            "The `error_bar` array has the wrong number of items " +
            "(%d for 2)" % val.length
    end
    method, level = *val
  else
    raise ArgumentError,
          "Unable to recognize the value for `error_bar`: %p" % val
  end

  case method
  when *VALID_ERROR_BAR_METHODS
    method = method.to_sym
  else
    error_message = "The value for method in `error_bar` array must be in %p, but %p was passed" % [
      DEFAULT_ERROR_BAR_LEVELS.keys,
      method
    ]
    raise ArgumentError, error_message
  end

  case level
  when Numeric
    # nothing to do
  when nil
    level = DEFAULT_ERROR_BAR_LEVELS[method]
  else
    raise ArgumentError,
          "The value of level in `error_bar` array must be a number "
  end

  [method, level]
end
check_estimator(value) click to toggle source
# File lib/charty/plotters/line_plotter.rb, line 83
        def check_estimator(value)
  case value
  when nil, false
    nil
  when :count, "count"
    :count
  when :mean, "mean"
    :mean
  when :median
    raise NotImplementedError,
          "median estimator has not been supported yet"
  when Proc
    raise NotImplementedError,
          "a callable estimator has not been supported yet"
  else
    raise ArgumentError,
          "invalid value for estimator (%p for :mean)" % value
  end
end
check_n_boot(value) click to toggle source
# File lib/charty/plotters/line_plotter.rb, line 109
        def check_n_boot(value)
  case value
  when Integer
    if value <= 0
      raise ArgumentError,
            "n_boot must be larger than zero, but %p is given" % value
    end
    value
  else
    raise ArgumentError,
          "invalid value for n_boot (%p for an integer > 0)" % value
  end
end
draw_lines(backend) click to toggle source
# File lib/charty/plotters/line_plotter.rb, line 242
        def draw_lines(backend)
  map_color(palette: palette, order: color_order, norm: color_norm)
  map_size(sizes: sizes, order: size_order, norm: size_norm)
  map_style(markers: markers, dashes: dashes, order: style_order)

  aggregator = EstimateAggregator.new(estimator, error_bar, n_boot, random)

  agg_var = :y
  grouper = :x
  grouping_vars = [:color, :size, :style]

  each_subset(grouping_vars, processed: true) do |sub_vars, sub_data|
    if self.sort
      sort_cols = [:units, :x, :y] & self.variables.keys
      sub_data = sub_data.sort_values(sort_cols)
    end

    # Perform axis scaling
    if x_scale != :linear
      sub_data[:x] = sub_data[:x].scale(x_scale)
    end
    if y_scale != :linear
      sub_data[:y] = sub_data[:y].scale(x_scale)
    end

    # Perform estimation and error calculation
    unless estimator.nil?
      if self.variables.include?(:units)
        raise "`estimator` is must be nil when specifying `units`"
      end

      grouped = sub_data.group_by(grouper, sort: self.sort)
      sub_data = grouped.apply(agg_var, &aggregator.method(:aggregate)).reset_index
    end

    # Perform axis inverse scaling
    if x_scale != :linear
      sub_data.column_names.each do |cn|
        if cn.start_with?("x")
          sub_data[cn] = sub_data[cn].scale_inverse(x_scale)
        end
      end
    end

    if y_scale != :linear
      sub_data.column_names.each do |cn|
        if cn.start_with?("y")
          sub_data[cn] = sub_data[cn].scale_inverse(x_scale)
        end
      end
    end

    unit_grouping = if self.variables.include?(:units)
                      sub_data.group_by(:units).each_group
                    else
                      { nil => sub_data }
                    end
    unit_grouping.each do |_unit_value, unit_data|
      ci_params = unless self.estimator.nil? || self.error_bar.nil?
                    {
                      style: self.err_style,
                      y_min: sub_data[:y_min],
                      y_max: sub_data[:y_max]
                    }
                  end
      backend.line(unit_data[:x], unit_data[:y], self.variables,
                   color: sub_vars[:color], color_mapper: @color_mapper,
                   size: sub_vars[:size], size_mapper: @size_mapper,
                   style: sub_vars[:style], style_mapper: @style_mapper,
                   ci_params: ci_params)
    end
  end

  if legend
    backend.add_line_plot_legend(@variables, @color_mapper, @size_mapper, @style_mapper, legend)
  end
end
render_plot(backend, **) click to toggle source
# File lib/charty/plotters/line_plotter.rb, line 237
        def render_plot(backend, **)
  draw_lines(backend)
  annotate_axes(backend)
end