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

#comparatorObject

INSTANCE METHODS #####



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

def comparator
  @_scientist_comparator
end

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



63
64
65
66
67
68
69
70
71
72
# File 'app/models/lab_tech/experiment.rb', line 63

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



74
75
76
# File 'app/models/lab_tech/experiment.rb', line 74

def diff(&block)
  @diff_with = block
end

#disableObject



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

def disable
  update_attribute :percent_enabled, 0
end

#enable(percent_enabled: 100) ⇒ Object



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

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

#enabled?Boolean

Returns:

  • (Boolean)


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

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.



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

def name         ; read_attribute  :name        ; end

#name=(value) ⇒ Object



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

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



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

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 )
end

#purge_dataObject

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/lab_tech/experiment.rb', line 104

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



127
128
129
130
131
# File 'app/models/lab_tech/experiment.rb', line 127

def run(*)
  increment_run_count
  provide_default_cleaner
  super
end

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



133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/models/lab_tech/experiment.rb', line 133

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



146
147
148
# File 'app/models/lab_tech/experiment.rb', line 146

def summarize_results
  puts "", summary, ""
end

#summaryObject



150
151
152
153
# File 'app/models/lab_tech/experiment.rb', line 150

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