Class: Stamina::Command::Infer

Inherits:
Object
  • Object
show all
Includes:
Robustness
Defined in:
lib/stamina-induction/stamina/command/infer.rb

Overview

Grammar inference, induces a DFA from a training sample using an chosen algorithm.

SYNOPSIS

#{program_name} #{command_name} sample.adl

OPTIONS #summarized_options

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#algorithmObject

Returns the value of attribute algorithm.



16
17
18
# File 'lib/stamina-induction/stamina/command/infer.rb', line 16

def algorithm
  @algorithm
end

#dropObject

Returns the value of attribute drop.



20
21
22
# File 'lib/stamina-induction/stamina/command/infer.rb', line 20

def drop
  @drop
end

#output_fileObject

Returns the value of attribute output_file.



21
22
23
# File 'lib/stamina-induction/stamina/command/infer.rb', line 21

def output_file
  @output_file
end

#scoreObject

Returns the value of attribute score.



18
19
20
# File 'lib/stamina-induction/stamina/command/infer.rb', line 18

def score
  @score
end

#takeObject

Returns the value of attribute take.



17
18
19
# File 'lib/stamina-induction/stamina/command/infer.rb', line 17

def take
  @take
end

#verboseObject

Returns the value of attribute verbose.



19
20
21
# File 'lib/stamina-induction/stamina/command/infer.rb', line 19

def verbose
  @verbose
end

Instance Method Details

#execute(args) ⇒ Object

Command execution

Raises:

  • (Quickl::Help)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/stamina-induction/stamina/command/infer.rb', line 93

def execute(args)
  raise Quickl::Help unless args.size == 1

  # Parses the sample
  $stderr << "Parsing sample...\n" if verbose
  sample = load_sample(assert_readable_file(args.first))

  # Induce the DFA
  dfa, tms = launch_induction(sample)

  # Flush result
  unless drop
    if output_file
      File.open(output_file, 'w') do |file|
        Stamina::ADL.print_automaton(dfa, file)
      end
    else
      Stamina::ADL.print_automaton(dfa, $stdout)
    end
  end

  # build meta information
  meta = {:algorithm   => algorithm,
          :sample      => File.basename(args.first),
          :take        => take,
          :sample_size => sample.size,
          :positive_count => sample.positive_count,
          :negative_count => sample.negative_count,
          :real_time   => tms.real,
          :total_time  => tms.total,
          :user_time   => tms.utime + tms.cutime,
          :system_time => tms.stime + tms.cstime}

  if score
    test = Stamina::ADL::parse_sample_file(score)
    classified_as = dfa.signature(test)
    reference = test.signature
    scoring = Scoring.scoring(classified_as, reference)
    meta.merge!(scoring.to_h)
  end

  # Display information
  puts meta.inspect
end

#launch_induction(sample) ⇒ Object

options



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/stamina-induction/stamina/command/infer.rb', line 62

def launch_induction(sample)
  require 'benchmark'

  algo_clazz = case algorithm
    when :rpni
      Stamina::Induction::RPNI
    when :bluefringe
      Stamina::Induction::BlueFringe
    else
      raise Quickl::InvalidOption, "Unknown induction algorithm: #{algo}"
  end

  dfa, tms = nil, nil
  tms = Benchmark.measure do
    dfa = algo_clazz.execute(sample, {:verbose => verbose})
  end
  [dfa, tms]
end

#load_sample(file) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/stamina-induction/stamina/command/infer.rb', line 81

def load_sample(file)
  sample = Stamina::ADL.parse_sample_file(file)
  if @take != 1.0
    sampled = Stamina::Sample.new
    sample.each_positive{|s| sampled << s if Kernel.rand < @take}
    sample.each_negative{|s| sampled << s if Kernel.rand < @take}
    sample = sampled
  end
  sample
end