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

#__after_recording__(&block) ⇒ Object

This method is experimental and should probably not be depended on



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

def __after_recording__(&block)
  @after_recording = block
end

#comparatorObject



64
65
66
# File 'app/models/lab_tech/experiment.rb', line 64

def comparator
  @_scientist_comparator
end

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



68
69
70
71
72
73
74
75
76
77
# File 'app/models/lab_tech/experiment.rb', line 68

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

  display_results mismatches, label: "Comparing results for #{name}:", io: io do |result|
    io.puts "Result ##{result.id}"
    result.compare_observations( io: io, &block )
  end
end

#diff(&block) ⇒ Object



79
80
81
# File 'app/models/lab_tech/experiment.rb', line 79

def diff(&block)
  @diff_with = block
end

#disableObject



83
84
85
# File 'app/models/lab_tech/experiment.rb', line 83

def disable
  update_attribute :percent_enabled, 0
end

#enable(percent_enabled: 100) ⇒ Object



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

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

#enabled?Boolean

Returns:

  • (Boolean)


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

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

#nameObject

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



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

def name         ; read_attribute  :name        ; end

#name=(value) ⇒ Object



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

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

#pct_correctObject



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_enabledObject



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

def pct_enabled
  format_pct( percent_enabled )
end

#publish(scientist_result) ⇒ Object



102
103
104
105
106
107
108
# File 'app/models/lab_tech/experiment.rb', line 102

def publish(scientist_result)
  return if Rails.env.test? && !LabTech.publish_results_in_test_mode?

  LabTech::Result.record_a_science( self, scientist_result, diff_with: @diff_with ).tap do |result|
    @after_recording&.call result
  end
end

#purge_dataObject

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



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

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(
    equivalent_count:  0,
    timed_out_count:   0,
    other_error_count: 0,
  )

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

#runObject



135
136
137
138
139
# File 'app/models/lab_tech/experiment.rb', line 135

def run(*)
  increment_run_count
  provide_default_cleaner
  super
end

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



141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/models/lab_tech/experiment.rb', line 141

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

  display_results errors, label: "Summarizing errors for #{name}:", io: io do |result|
    io.puts "Result ##{result.id}"
    result.candidates.each do |observation|
      io.puts "  * " + observation.exception_class + ":  " + observation.exception_message
    end
  end
end

#summarize_resultsObject



154
155
156
# File 'app/models/lab_tech/experiment.rb', line 154

def summarize_results
  puts "", summary, ""
end

#summaryObject



158
159
160
161
# File 'app/models/lab_tech/experiment.rb', line 158

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

#total_countObject



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

def total_count
  equivalent_count + timed_out_count + other_error_count
end