class CooCoo::Grapher

Public Class Methods

new() click to toggle source
# File lib/coo-coo/grapher.rb, line 5
def initialize()
end

Public Instance Methods

pen_color(x) click to toggle source
# File lib/coo-coo/grapher.rb, line 75
def pen_color(x)
  x = pen_scale(x)
  color = NMatrix[[ 0, 0, 0 ]]

  if x > 0.01
    color = NMatrix[[ 1, 0, 0 ]]
  elsif x < 0.01
    color = NMatrix[[ 0, 0, 1 ]]
  end

  '#' + color.to_a.collect { |n| (n.abs * 255).to_i.to_s(16).rjust(2, "0") }.join
end
pen_scale(x) click to toggle source
# File lib/coo-coo/grapher.rb, line 103
def pen_scale(x)
  x / 10.0
end
populate(name, network, edge_widths = nil) click to toggle source
# File lib/coo-coo/grapher.rb, line 8
def populate(name, network, edge_widths = nil)
  Dot::Graph.new(:digraph, :label => name, :ranksep => 3) do |g|
    populate_inputs(network.num_inputs, g)
    populate_layers(network.layers, edge_widths, g)
    populate_outputs(network.num_outputs, network.num_layers - 1, edge_widths, g)
  end
end
populate_inputs(num, g) click to toggle source
# File lib/coo-coo/grapher.rb, line 22
def populate_inputs(num, g)
  g.add_subgraph("cluster_inputs", :label => "Inputs", :rank => "same") do |sg|
    inputs = num.times.collect do |i|
      "input_#{i}"
    end

    sg.add_block("") do |ssg|
      inputs.each_with_index do |name, i|
        ssg.add_node(name, :label => i)
      end
      ssg.add_edge(inputs, :style => "invis")
    end
  end
end
populate_layer(layer, layer_index, edge_widths, g) click to toggle source
# File lib/coo-coo/grapher.rb, line 37
def populate_layer(layer, layer_index, edge_widths, g)
  g.add_subgraph("cluster_layer_#{layer_index}", :label => "Layer #{layer_index}") do |sg|
    sg.add_subgraph("layer_#{layer_index}", :rank => "same") do |ssg|
      nodes = layer.neurons.each_with_index.collect do |n, ni|
        name = "neuron_#{layer_index}_#{ni}"
        populate_neuron_node(name, ni, ssg)
        name
      end
      ssg.add_edge(nodes, :style => "invis")
    end

    layer.neurons.each_with_index do |n, ni|
      populate_neuron_link(n, ni, layer_index, edge_widths, sg)
    end
  end
end
populate_layers(layers, edge_widths, g) click to toggle source
# File lib/coo-coo/grapher.rb, line 16
def populate_layers(layers, edge_widths, g)
  layers.each_with_index do |l, i|
    populate_layer(l, i, edge_widths && edge_widths[i], g)
  end
end
populate_neuron_node(neuron_id, neuron_index, g) click to toggle source
# File lib/coo-coo/grapher.rb, line 54
def populate_neuron_node(neuron_id, neuron_index, g)
  g.add_node(neuron_id, :label => neuron_index)
end
populate_outputs(num_outputs, last_layer, edge_widths, g) click to toggle source
# File lib/coo-coo/grapher.rb, line 88
def populate_outputs(num_outputs, last_layer, edge_widths, g)
  g.add_subgraph("cluster_outputs", :label => "Outputs") do |sg|
    num_outputs.times do |o|
      sg.add_node("output_#{o}", :label => o)
    end
  end
  
  num_outputs.times do |o|
    w = edge_widths && edge_widths[-1][o] || 1.0
    g.add_edge([ "neuron_#{last_layer}_#{o}", "output_#{o}" ],
               :penwidth => pen_scale(w.abs),
               :pencolor => pen_color(w))
  end
end