Class: OpenStudio::Analysis::Translator::Excel
- Inherits:
-
Object
- Object
- OpenStudio::Analysis::Translator::Excel
- Defined in:
- lib/openstudio/analysis/translator/excel.rb
Instance Attribute Summary collapse
-
#export_path ⇒ Object
readonly
Returns the value of attribute export_path.
-
#measure_path ⇒ Object
readonly
Returns the value of attribute measure_path.
-
#models ⇒ Object
readonly
Returns the value of attribute models.
-
#name ⇒ Object
readonly
remove these once we have classes to construct the JSON file.
-
#number_of_samples ⇒ Object
readonly
Returns the value of attribute number_of_samples.
-
#template_json ⇒ Object
readonly
Returns the value of attribute template_json.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
-
#weather_files ⇒ Object
readonly
Returns the value of attribute weather_files.
Instance Method Summary collapse
-
#initialize(xls_filename) ⇒ Excel
constructor
pass in the filename to read.
- #process ⇒ Object
- #save_analysis ⇒ Object
-
#save_variable_json(filename) ⇒ Object
Save off the legacy format of the JSON file.
-
#translate_to_analysis_json_template ⇒ Object
TODO: move this into a new class that helps construct this file.
- #validate_analysis ⇒ Object
Constructor Details
#initialize(xls_filename) ⇒ Excel
pass in the filename to read
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 20 def initialize(xls_filename) @root_path = File.(File.dirname(xls_filename)) @xls = nil # try to read the spreadsheet as a roo object if File.exists?(xls_filename) @xls = Roo::Spreadsheet.open(xls_filename) else raise "File #{xls_filename} does not exist" end # Initialize some other instance variables @weather_files = [] @models = [] @other_files = [] @export_path = "./export" @measure_path = "./measures" @number_of_samples = 0 @template_json = nil end |
Instance Attribute Details
#export_path ⇒ Object (readonly)
Returns the value of attribute export_path.
9 10 11 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 9 def export_path @export_path end |
#measure_path ⇒ Object (readonly)
Returns the value of attribute measure_path.
8 9 10 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 8 def measure_path @measure_path end |
#models ⇒ Object (readonly)
Returns the value of attribute models.
6 7 8 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 6 def models @models end |
#name ⇒ Object (readonly)
remove these once we have classes to construct the JSON file
13 14 15 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 13 def name @name end |
#number_of_samples ⇒ Object (readonly)
Returns the value of attribute number_of_samples.
14 15 16 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 14 def number_of_samples @number_of_samples end |
#template_json ⇒ Object (readonly)
Returns the value of attribute template_json.
15 16 17 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 15 def template_json @template_json end |
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
5 6 7 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 5 def variables @variables end |
#weather_files ⇒ Object (readonly)
Returns the value of attribute weather_files.
7 8 9 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 7 def weather_files @weather_files end |
Instance Method Details
#process ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 41 def process @setup = parse_setup() @variables = parse_variables() # call validate to make sure everything that is needed exists (i.e. directories) validate_analysis() end |
#save_analysis ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 128 def save_analysis # save the format in the OpenStudio analysis json format template without # the correct weather files or models @template_json = translate_to_analysis_json_template() #validate_template_json # iterate over each model and save the zip and json @models.each do |model| save_analysis_zip(model) analysis_json = create_analysis_json(@template_json, model) end end |
#save_variable_json(filename) ⇒ Object
Save off the legacy format of the JSON file
50 51 52 53 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 50 def save_variable_json(filename) FileUtils.rm_f(filename) if File.exists?(filename) File.open(filename, 'w') { |f| f << JSON.pretty_generate(@variables) } end |
#translate_to_analysis_json_template ⇒ Object
TODO: move this into a new class that helps construct this file
143 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 143 def translate_to_analysis_json_template # Load in the templates for constructing the JSON file template_root = File.join(File.dirname(__FILE__), "../../templates") analysis_template = ERB.new(File.open("#{template_root}/analysis.json.erb", 'r').read) workflow_template = ERB.new(File.open("#{template_root}/workflow_item.json.erb", 'r').read) uncertain_variable_template = ERB.new(File.open("#{template_root}/uncertain_variable.json.erb", 'r').read) discrete_uncertain_variable_template = ERB.new(File.open("#{template_root}/discrete_uncertain_variable.json.erb", 'r').read) static_variable_template = ERB.new(File.open("#{template_root}/static_variable.json.erb", 'r').read) pivot_variable_template = ERB.new(File.open("#{template_root}/pivot_variable.json.erb", 'r').read) argument_template = ERB.new(File.open("#{template_root}/argument.json.erb", 'r').read) # Templated analysis json file (this is what is returned) puts "Analysis name is #{@name}" openstudio_analysis_json = JSON.parse(analysis_template.result(get_binding)) @measure_index = -1 @variables['data'].each do |measure| # With OpenStudio server we need to create the workflow with all the measure instances if measure['enabled'] && measure['name'] != 'baseline' @measure_index += 1 puts " Adding measure item '#{measure['name']}'" @measure = measure @measure['measure_file_name_dir'] = @measure['measure_file_name'].underscore # Grab the measure json file out of the right directory wf = JSON.parse(workflow_template.result(get_binding)) # add in the variables measure['variables'].each do |variable| @variable = variable # Determine if row is suppose to be an argument or a variable to be perturbed. if @variable['variable_type'] == 'argument' ag = nil if @variable['method'] == 'static' if !@variable['distribution']['static_value'] raise "can not have an argument that is not a static value defined in which to set the argument" end # add this as an argument case @variable['type'] when "Double" @static_value = @variable['distribution']['static_value'].to_f when "Integer" @static_value = @variable['distribution']['static_value'].to_i when "String", "Choice" @static_value = @variable['distribution']['static_value'].inspect when "Bool" if @variable['distribution']['static_value'].downcase == "true" @static_value = true else @static_value = false end else raise "Unknown variable type of #{@variable['type']}" end ag = JSON.parse(argument_template.result(get_binding)) end raise "Argument '#{@variable['name']}' did not process. Most likely it did not have all parameters defined." if ag.nil? wf['arguments'] << ag else # must be a variable vr = nil if @variable['method'] == 'static' # add this as an argument vr = JSON.parse(static_variable_template.result(get_binding)) elsif @variable['method'] == 'lhs' if @variable['type'] == 'enum' @values_and_weights = @variable['distribution']['enumerations'].map { |v| {value: v} }.to_json vr =JSON.parse(discrete_uncertain_variable_template.result(get_binding)) else vr = JSON.parse(uncertain_variable_template.result(get_binding)) end elsif @variable['method'] == 'pivot' @values_and_weights = @variable['distribution']['enumerations'].map { |v| {value: v} }.to_json vr =JSON.parse(pivot_variable_template.result(get_binding)) end raise "variable was nil after processing" if vr.nil? wf['variables'] << vr end end openstudio_analysis_json['analysis']['problem']['workflow'] << wf end end openstudio_analysis_json end |
#validate_analysis ⇒ Object
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 55 def validate_analysis # Setup the paths and do some error checking raise "Measures directory '#{@measure_path}' does not exist" unless Dir.exists?(@measure_path) @models.uniq! raise "No seed models defined in spreadsheet" if @models.empty? @models.each do |model| raise "Seed model does not exist: #{model[:path]}" unless File.exists?(model[:path]) end @weather_files.uniq! raise "No weather files found based on what is in the spreadsheet" if @weather_files.empty? @weather_files.each do |wf| raise "Weather file does not exist: #{wf}" unless File.exists?(wf) end # This can be a directory as well @other_files.each do |of| raise "Other files do not exist for: #{of[:path]}" unless File.exists?(of[:path]) end FileUtils.mkdir_p(@export_path) # verify that all continuous variables have all the data needed and create a name maps variable_names = [] @variables['data'].each do |measure| if measure['enabled'] && measure['name'] != 'baseline' measure['variables'].each do |variable| # Determine if row is suppose to be an argument or a variable to be perturbed. if variable['variable_type'] == 'variable' variable_names << variable['display_name'] if variable['method'] == 'static' # add this as an argument # check something elsif variable['method'] == 'lhs' if variable['type'] == 'enum' # check something else # must be an integer or double if variable['distribution']['min'].nil? || variable['distribution']['min'] == "" raise "Variable #{measure['name']}:#{variable['name']} must have a minimum" end if variable['distribution']['max'].nil? || variable['distribution']['max'] == "" raise "Variable #{measure['name']}:#{variable['name']} must have a maximum" end if variable['distribution']['mean'].nil? || variable['distribution']['mean'] == "" raise "Variable #{measure['name']}:#{variable['name']} must have a mean" end if variable['distribution']['stddev'].nil? || variable['distribution']['stddev'] == "" raise "Variable #{measure['name']}:#{variable['name']} must have a stddev" end if variable['distribution']['min'] > variable['distribution']['max'] raise "Variable min is greater than variable max for #{measure['name']}:#{variable['name']}" end end elsif variable['method'] == 'pivot' # check something end end end end end dupes = variable_names.find_all { |e| variable_names.count(e) > 1 }.uniq if dupes.count > 0 raise "duplicate variable names found in list #{dupes.inspect}" end # most of the checks will raise a runtime exception, so this true will never be called true end |