class CooCoo::DataSources::Xournal::BitmapStream

Attributes

canvas_klass[RW]
example_height[R]
example_width[R]
labels[R]
pen_scale[RW]
shuffle[RW]
training_documents[R]
use_color[R]

Public Class Methods

new(options = Hash.new) click to toggle source
# File lib/coo-coo/data_sources/xournal/bitmap_stream.rb, line 19
def initialize(options = Hash.new)
  @training_documents = Array.new
  @document_paths = Array.new
  @pen_scale = options.fetch(:pen_scale, 1.0)
  @example_width = options.fetch(:width, 28)
  @example_height = options.fetch(:height, 28)
  @num_labels = options[:num_labels]
  if options[:labels]
    @labels = File.read(options[:labels]).split("\n")
  else
    @labels = Array.new
  end
  @canvas_klass = options.fetch(:canvas, Drawing::CairoCanvas)
  @use_color = options.fetch(:use_color, false)
  @shuffle = options.fetch(:shuffle, 16)

  options[:training_documents].each do |td|
    add_training_document(td)
  end
end

Public Instance Methods

add_label(label) click to toggle source
# File lib/coo-coo/data_sources/xournal/bitmap_stream.rb, line 75
def add_label(label)
  @labels << label unless @labels.find_index(label)
  self
end
add_training_document(path_or_td) click to toggle source
# File lib/coo-coo/data_sources/xournal/bitmap_stream.rb, line 40
def add_training_document(path_or_td)
  td = case path_or_td
       when String then TrainingDocument.from_file(path_or_td)
       when Pathname then TrainingDocument.from_file(path_or_td.to_s)
       when TrainingDocument then path_or_td
       else raise ArgumentError.new("#{path_or_td.inspect} is not a String, Pathname, or TrainingDocument")
       end
  process_training_document(td)
  @document_paths << path_or_td unless td == path_or_td
  @training_documents << td
  self
end
decode_output(output) click to toggle source
# File lib/coo-coo/data_sources/xournal/bitmap_stream.rb, line 87
def decode_output(output)
  @labels[output.each.with_index.max[1]]
end
each(yield_canvas = false) { |encode_label(label), encode_strokes(strokes, yield_canvas)| ... } click to toggle source
# File lib/coo-coo/data_sources/xournal/bitmap_stream.rb, line 117
def each(yield_canvas = false, &block)
  return to_enum(__method__, yield_canvas) unless block_given?

  training_documents.each do |td|
    stroke_set = 0

    loop do
      td.each_example.each_slice(shuffle) do |slice|
        examples = slice.collect do |ex|
          strokes = ex.stroke_sets[stroke_set]
          [ ex.label, strokes ] unless strokes.nil? || strokes.empty?
        end.reject(&:nil?)

        raise StopIteration if examples.empty?

        examples.shuffle.each do |(label, strokes)|
          yield(encode_label(label), encode_strokes(strokes, yield_canvas))
        end
      end
      
      stroke_set += 1
    end
  end
end
encode_label(label) click to toggle source
# File lib/coo-coo/data_sources/xournal/bitmap_stream.rb, line 80
def encode_label(label)
  i = @labels.find_index(label)
  v = Vector.zeros(output_size)
  v[i] = 1.0
  v
end
encode_strokes(strokes, return_canvas = false) click to toggle source
# File lib/coo-coo/data_sources/xournal/bitmap_stream.rb, line 102
def encode_strokes(strokes, return_canvas = false)
  canvas = @canvas_klass.new(@example_width, @example_height)
  if pen_scale != 1.0
    strokes = strokes.collect { |s| s.scale(1.0, 1.0, pen_scale) }
  end
  
  encode_strokes_to_canvas(strokes, canvas)
  
  if return_canvas
    canvas.flush
  else
    canvas.to_vector(!@use_color) / 256.0
  end
end
encode_strokes_to_canvas(strokes, canvas) click to toggle source
# File lib/coo-coo/data_sources/xournal/bitmap_stream.rb, line 91
def encode_strokes_to_canvas(strokes, canvas)
  canvas.fill_color = 'white'
  canvas.stroke_color = 'white'
  canvas.rect(0, 0, @example_width, @example_height)
  ren = Renderer.new

  strokes.each do |stroke|
    ren.render_stroke(canvas, stroke, 0, 0, 1, 1, @example_width, @example_height)
  end
end
input_size() click to toggle source
# File lib/coo-coo/data_sources/xournal/bitmap_stream.rb, line 67
def input_size
  example_width * example_height * (@use_color ? 3 : 1)
end
output_size() click to toggle source
# File lib/coo-coo/data_sources/xournal/bitmap_stream.rb, line 71
def output_size
  Math.max(@labels.size, @num_labels)
end
process_training_document(td) click to toggle source
# File lib/coo-coo/data_sources/xournal/bitmap_stream.rb, line 53
def process_training_document(td)
  td.labels.each do |l|
    add_label(l)
  end
end
size() click to toggle source
# File lib/coo-coo/data_sources/xournal/bitmap_stream.rb, line 59
def size
  training_documents.reduce(0) do |total, td|
    total + td.each_example.reduce(0) do |subtotal, ex|
      subtotal + ex.size
    end
  end
end