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

#disableObject



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

def disable
  update_attribute :percent_enabled, 0
end

#enable(percent_enabled: 100) ⇒ Object



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

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

#enabled?Boolean

Returns:

  • (Boolean)


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

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.



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

def name         ; read_attribute  :name        ; end

#name=(value) ⇒ Object



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

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



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

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_dataObject

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/lab_tech/experiment.rb', line 100

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

#runObject



123
124
125
126
127
# File 'app/models/lab_tech/experiment.rb', line 123

def run(*)
  increment_run_count
  provide_default_cleaner
  super
end

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



129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/lab_tech/experiment.rb', line 129

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



142
143
144
# File 'app/models/lab_tech/experiment.rb', line 142

def summarize_results
  puts "", summary, ""
end

#summaryObject



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

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