Class: Jkr::Trial

Inherits:
Object
  • Object
show all
Defined in:
lib/jkr/trial.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/jkr/trial.rb', line 8

def params
  @params
end

Class Method Details

.make_trials(resultset_dir, plan) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jkr/trial.rb', line 9

def self.make_trials(resultset_dir, plan)
  var_combs = [{}]
  plan.vars.each do |key, vals|
    var_combs = vals.map {|val|
      var_combs.map do |var_comb|
        var_comb.dup.merge(key => val)
      end
    }.flatten
  end

  params_list = var_combs.map{|var_comb| plan.params.merge(var_comb)}

  plan.param_filters.each do |filter|
    params_list = params_list.select(&filter)
  end

  params_list = params_list * plan.routine_nr_run

  params_list.map do |params|
    result_dir = Utils.reserve_next_dir(resultset_dir)
    Trial.new(result_dir, plan, params)
  end
end

.pretty_time(seconds) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/jkr/trial.rb', line 33

def self.pretty_time(seconds)
  hour = seconds / 3600
  min = (seconds - hour * 3600) / 60
  sec = seconds - hour * 3600 - min * 60

  sprintf("%02d:%02d:%02d", hour, min, sec)
end

.run(env, plan, delete_files_on_error = true) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jkr/trial.rb', line 41

def self.run(env, plan, delete_files_on_error = true)
  plan_suffix = File.basename(plan.file_path, ".plan")
  plan_suffix += "_#{plan.short_desc}" if plan.short_desc
  resultset_dir = Utils.reserve_next_dir(env.jkr_result_dir, plan_suffix)
  plan.resultset_dir = resultset_dir
  FileUtils.mkdir_p(File.join(resultset_dir, "plan"))
  FileUtils.mkdir_p(File.join(resultset_dir, "script"))

  begin
    trials = self.make_trials(resultset_dir, plan)

    plan_dest_dir = File.join(resultset_dir, "plan")
    script_dest_dir = File.join(resultset_dir, "script")

    _plan = plan
    begin
      if _plan == plan # copy main plan file
        FileUtils.copy_file(_plan.file_path,
                            File.expand_path(File.basename(_plan.file_path), resultset_dir))
      else
        FileUtils.copy_file(_plan.file_path,
                            File.expand_path(File.basename(_plan.file_path),
                                           plan_dest_dir))
      end

      plan.used_scripts.each do |script_path|
        FileUtils.copy_file(script_path,
                            File.expand_path(File.basename(script_path),
                                             script_dest_dir))
      end
    end while _plan = _plan.base_plan

    params = plan.params.merge(plan.vars)
    plan.freeze

    # show estimated execution time if available
    if plan.exec_time_estimate
      puts("")
      puts("== estimated execution time: #{pretty_time(plan.exec_time_estimate.call(plan))} ==")
      puts("")
    end

    plan.do_prep()
    trials.each do |trial|
      trial.run
    end
    plan.do_cleanup()
  rescue Exception => err
    if delete_files_on_error
      FileUtils.rm_rf(resultset_dir)
    end
    raise err
  end
end

Instance Method Details

#runObject



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
# File 'lib/jkr/trial.rb', line 103

def run()
  plan = @plan
  File.open("#{@result_dir}/params.json", "w") do |f|
    f.puts(@params.to_json)
  end

  # reset plan.metastore
  plan.metastore.clear

  # define utility functions for plan.routine object
  Jkr::TrialUtils.define_routine_utils(@result_dir, @plan, @params)

  @plan.metastore[:trial_start_time] = Time.now
  @plan.do_routine(@plan, @params)
  @plan.metastore[:trial_end_time] = Time.now

  Jkr::TrialUtils.undef_routine_utils(@plan)

  # save plan.metastore
  Marshal.dump(@plan.metastore,
               File.open("#{@result_dir}/metastore.msh", "w"))
  File.open("#{@result_dir}/metastore.json", "w") do |f|
    f.puts(@plan.metastore.to_json)
  end
end