Class: LabTech::Experiment

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Scientist::Experiment
Defined in:
app/models/lab_tech/experiment.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.named(experiment_name_or_id) ⇒ Object

CLASS METHODS #####



44
45
46
47
48
49
50
51
52
53
# File 'app/models/lab_tech/experiment.rb', line 44

def self.named(experiment_name_or_id)
  case experiment_name_or_id
  when String  ; exp = find_or_create_by(name: experiment_name_or_id)
  when Integer ; exp = find(experiment_name_or_id)
  end
  yield exp if block_given?
  exp
rescue ActiveRecord::RecordNotUnique
  retry
end

Instance Method Details

#comparator ⇒ Object

INSTANCE METHODS #####



59
60
61
# File 'app/models/lab_tech/experiment.rb', line 59

def comparator
  @_scientist_comparator
end

#compare_mismatches(limit: nil, io: $stdout, &block) ⇒ Object

TODO: DRY up the io.puts structure between this and summarize_errors



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/lab_tech/experiment.rb', line 64

def compare_mismatches(limit: nil, io: $stdout, &block)
  mismatches = results.mismatched.includes(:observations)
  return if mismatches.empty?
  mismatches = mismatches.limit(limit) if limit

  io.puts
  io.puts "=" * 100
  io.puts "Comparing results for #{name}:"
  io.puts

  mismatches.each do |result|
    io.puts
    io.puts "-" * 100
    io.puts "Result ##{result.id}"
    result.compare_observations( io: io, &block )
    io.puts "-" * 100
  end

  io.puts
  io.puts "=" * 100
  io.puts
  nil
end

#disable ⇒ Object



88
89
90
# File 'app/models/lab_tech/experiment.rb', line 88

def disable
  update_attribute :percent_enabled, 0
end

#enable(percent_enabled: 100) ⇒ Object



98
99
100
# File 'app/models/lab_tech/experiment.rb', line 98

def enable(percent_enabled: 100)
  update_attribute :percent_enabled, percent_enabled
end

#enabled? ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
# File 'app/models/lab_tech/experiment.rb', line 92

def enabled?
  n = rand(100)
  fail "WTF, Ruby?" unless (0..99).cover?(n) # Paranoia? Indirect documentation? YOU DECIDE.
  n < percent_enabled
end

#name ⇒ Object

Oh, this is a fun one: apparently Scientist::Experiment#name is overriding the ActiveRecord attribute. Override it back.



104
# File 'app/models/lab_tech/experiment.rb', line 104

def name         ; read_attribute  :name        ; end

#name=(value) ⇒ Object



105
# File 'app/models/lab_tech/experiment.rb', line 105

def name=(value) ; write_attribute :name, value ; end

#pct_correct ⇒ Object



25
26
27
28
# File 'app/models/lab_tech/experiment.rb', line 25

def pct_correct
  return "N/A" if total_count.zero?
  format_pct( equivalent_count, total_count )
end

#pct_enabled ⇒ Object



21
22
23
# File 'app/models/lab_tech/experiment.rb', line 21

def pct_enabled
  format_pct( percent_enabled )
end

#publish(scientist_result) ⇒ Object



107
108
109
110
# File 'app/models/lab_tech/experiment.rb', line 107

def publish(scientist_result)
  return if Rails.env.test? && !LabTech.publish_results_in_test_mode?
  LabTech::Result.record_a_science( self, scientist_result )
end

#purge_data ⇒ Object

I don't encourage the willy-nilly destruction of experimental results... ...but sometimes you just need to start over.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/lab_tech/experiment.rb', line 114

def purge_data
  delete_and_count = ->(scope) {
    n0, n1 = 0, 0
    transaction do
      n0 = scope.count
      scope.delete_all
      n1 = scope.count
    end
    n0 - n1
  }

  n = delete_and_count.call( LabTech::Observation.where(result_id: self.result_ids) )
  m = delete_and_count.call( self.results )

  update_attributes(
    equivalent_count:  0,
    timed_out_count:   0,
    other_error_count: 0,
  )

  puts "Deleted #{m} result(s) and #{n} observations"
end

#run ⇒ Object



137
138
139
140
141
# File 'app/models/lab_tech/experiment.rb', line 137

def run(*)
  increment_run_count
  provide_default_cleaner
  super
end

#summarize_errors(limit: nil, io: $stdout) ⇒ Object

TODO: DRY up the io.puts structure between this and compare_mismatches



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/models/lab_tech/experiment.rb', line 144

def summarize_errors(limit: nil, io: $stdout)
  errors = results.other_error
  return if errors.empty?
  errors = errors.limit(limit) if limit

  io.puts
  io.puts "=" * 100
  io.puts "Comparing results for #{name}:"
  io.puts

  errors.each do |result|
    io.puts
    io.puts "-" * 100
    io.puts "Result ##{result.id}"
    result.candidates.each do |observation|
      puts "  * " + observation.exception_class + ":  " + observation.exception_message
    end
    io.puts "-" * 100
  end

  io.puts
  io.puts "=" * 100
  io.puts
  nil
end

#summarize_results ⇒ Object



170
171
172
# File 'app/models/lab_tech/experiment.rb', line 170

def summarize_results
  puts "", summary, ""
end

#summary ⇒ Object



174
175
176
177
# File 'app/models/lab_tech/experiment.rb', line 174

def summary
  reload
  LabTech::Summary.new(self)
end

#total_count ⇒ Object



30
31
32
# File 'app/models/lab_tech/experiment.rb', line 30

def total_count
  equivalent_count + timed_out_count + other_error_count
end