Class: OpenStudio::Analysis::Translator::Excel
- Inherits:
-
Object
- Object
- OpenStudio::Analysis::Translator::Excel
- Defined in:
- lib/openstudio/analysis/translator/excel.rb
Instance Attribute Summary collapse
-
#algorithm ⇒ Object
readonly
Returns the value of attribute algorithm.
-
#cluster_name ⇒ Object
readonly
Returns the value of attribute cluster_name.
-
#export_path ⇒ Object
readonly
Returns the value of attribute export_path.
-
#machine_name ⇒ Object
readonly
Returns the value of attribute machine_name.
-
#measure_path ⇒ Object
readonly
Returns the value of attribute measure_path.
-
#models ⇒ Object
readonly
Returns the value of attribute models.
-
#name ⇒ Object
remove these once we have classes to construct the JSON file.
-
#outputs ⇒ Object
readonly
Returns the value of attribute outputs.
-
#problem ⇒ Object
readonly
Returns the value of attribute problem.
-
#run_setup ⇒ Object
readonly
Returns the value of attribute run_setup.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
-
#template_json ⇒ Object
readonly
Returns the value of attribute template_json.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
-
#weather_files ⇒ Object
readonly
Returns the value of attribute weather_files.
Instance Method Summary collapse
- #add_directory_to_zip(zipfile, local_directory, relative_zip_directory) ⇒ Object
- #create_analysis_hash ⇒ Object
- #deep_copy(o) ⇒ Object
-
#initialize(xls_filename) ⇒ Excel
constructor
pass in the filename to read.
- #process ⇒ Object
-
#save_analysis ⇒ Object
save_analysis will iterate over each model that is defined in the spreadsheet and save the zip and json file.
-
#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
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 27 def initialize(xls_filename) @xls_filename = xls_filename @root_path = File.(File.dirname(@xls_filename)) @xls = nil # try to read the spreadsheet as a roo object if File.exist?(@xls_filename) @xls = Roo::Spreadsheet.open(@xls_filename) else fail "File #{@xls_filename} does not exist" end # Initialize some other instance variables @version = '0.0.1' @name = nil @machine_name = nil @cluster_name = nil @settings = {} @weather_files = [] @models = [] @other_files = [] @export_path = './export' @measure_path = './measures' @number_of_samples = 0 # todo: remove this @problem = {} @algorithm = {} @template_json = nil @outputs = {} @run_setup = {} end |
Instance Attribute Details
#algorithm ⇒ Object (readonly)
Returns the value of attribute algorithm.
15 16 17 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 15 def algorithm @algorithm end |
#cluster_name ⇒ Object (readonly)
Returns the value of attribute cluster_name.
13 14 15 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 13 def cluster_name @cluster_name end |
#export_path ⇒ Object (readonly)
Returns the value of attribute export_path.
12 13 14 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 12 def export_path @export_path end |
#machine_name ⇒ Object (readonly)
Returns the value of attribute machine_name.
21 22 23 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 21 def machine_name @machine_name end |
#measure_path ⇒ Object (readonly)
Returns the value of attribute measure_path.
11 12 13 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 11 def measure_path @measure_path end |
#models ⇒ Object (readonly)
Returns the value of attribute models.
9 10 11 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 9 def models @models end |
#name ⇒ Object
remove these once we have classes to construct the JSON file
20 21 22 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 20 def name @name end |
#outputs ⇒ Object (readonly)
Returns the value of attribute outputs.
8 9 10 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 8 def outputs @outputs end |
#problem ⇒ Object (readonly)
Returns the value of attribute problem.
16 17 18 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 16 def problem @problem end |
#run_setup ⇒ Object (readonly)
Returns the value of attribute run_setup.
17 18 19 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 17 def run_setup @run_setup end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
6 7 8 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 6 def settings @settings end |
#template_json ⇒ Object (readonly)
Returns the value of attribute template_json.
22 23 24 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 22 def template_json @template_json end |
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
7 8 9 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 7 def variables @variables end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
5 6 7 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 5 def version @version end |
#weather_files ⇒ Object (readonly)
Returns the value of attribute weather_files.
10 11 12 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 10 def weather_files @weather_files end |
Instance Method Details
#add_directory_to_zip(zipfile, local_directory, relative_zip_directory) ⇒ Object
331 332 333 334 335 336 337 338 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 331 def add_directory_to_zip(zipfile, local_directory, relative_zip_directory) # pp "Add Directory #{local_directory}" Dir[File.join("#{local_directory}", '**', '**')].each do |file| # pp "Adding File #{file}" zipfile.add(file.sub(local_directory, relative_zip_directory), file) end zipfile end |
#create_analysis_hash ⇒ Object
163 164 165 166 167 168 169 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 163 def create_analysis_hash # save the format in the OpenStudio analysis json format template without # the correct weather files or models @template_json = translate_to_analysis_json_template @template_json end |
#deep_copy(o) ⇒ Object
402 403 404 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 402 def deep_copy(o) Marshal.load(Marshal.dump(o)) end |
#process ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 58 def process @setup = parse_setup @version = Semantic::Version.new @version fail "Spreadsheet version #{@version} is no longer supported. Please upgrade your spreadsheet to at least 0.1.9" if @version < '0.1.9' @variables = parse_variables @outputs = parse_outputs # call validate to make sure everything that is needed exists (i.e. directories) validate_analysis end |
#save_analysis ⇒ Object
save_analysis will iterate over each model that is defined in the spreadsheet and save the zip and json file.
173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 173 def save_analysis @template_json = create_analysis_hash # 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, @models.count > 1) end end |
#save_variable_json(filename) ⇒ Object
Save off the legacy format of the JSON file
73 74 75 76 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 73 def save_variable_json(filename) FileUtils.rm_f(filename) if File.exist?(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
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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 186 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) 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)) openstudio_analysis_json['analysis']['problem'].merge!(@problem) openstudio_analysis_json['analysis']['problem']['algorithm'].merge!(@algorithm) openstudio_analysis_json['analysis'].merge!(@outputs) @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_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['distribution']['static_value'].nil? || @variable['distribution']['static_value'] == 'null' puts " Warning: '#{measure['name']}:#{@variable['name']}' static value was empty or null, assuming optional and skipping" next end # add this as an argument case @variable['type'].downcase when 'double' @static_value = @variable['distribution']['static_value'].to_f when 'integer' @static_value = @variable['distribution']['static_value'].to_i # TODO: update openstudio export to write only Strings 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 fail "Unknown variable type of '#{@variable['type']}'" end ag = JSON.parse(argument_template.result(get_binding)) fail "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 [either pivot or normal variable] vr = nil # add this as an argument case @variable['type'].downcase when 'double' @static_value = @variable['distribution']['static_value'].to_f when 'integer' @static_value = @variable['distribution']['static_value'].to_i # TODO: update openstudio export to write only Strings 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 fail "Unknown variable type of '#{@variable['type']}'" end # TODO: remove enum and choice as this is not the variable type if @variable['type'] == 'enum' || @variable['type'].downcase == 'choice' @values_and_weights = @variable['distribution']['enumerations'].map { |v| {value: v} }.to_json vr = JSON.parse(discrete_uncertain_variable_template.result(get_binding)) elsif @variable['distribution']['type'] == 'discrete_uncertain' # puts @variable.inspect weights = nil if @variable['distribution']['discrete_weights'] && @variable['distribution']['discrete_weights'] != '' weights = eval(@variable['distribution']['discrete_weights']) end values = nil if variable['type'].downcase == 'bool' values = eval(@variable['distribution']['discrete_values']) values.map! { |v| v.to_s == 'true' } else values = eval(@variable['distribution']['discrete_values']) end if weights fail "Discrete variable '#{@variable['name']}' does not have equal length of values and weights" if values.size != weights.size @values_and_weights = values.zip(weights).map { |v, w| {value: v, weight: w} }.to_json else @values_and_weights = values.map { |v| {value: v} }.to_json end if @variable['variable_type'] == 'pivot' vr = JSON.parse(pivot_variable_template.result(get_binding)) else vr = JSON.parse(discrete_uncertain_variable_template.result(get_binding)) end else if @variable['variable_type'] == 'pivot' fail 'Currently unable to pivot on continuous variables... stay tuned.' else vr = JSON.parse(uncertain_variable_template.result(get_binding)) end end fail '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
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 78 def validate_analysis # Setup the paths and do some error checking fail "Measures directory '#{@measure_path}' does not exist" unless Dir.exist?(@measure_path) @models.uniq! fail 'No seed models defined in spreadsheet' if @models.empty? @models.each do |model| fail "Seed model does not exist: #{model[:path]}" unless File.exist?(model[:path]) end @weather_files.uniq! fail 'No weather files found based on what is in the spreadsheet' if @weather_files.empty? @weather_files.each do |wf| fail "Weather file does not exist: #{wf}" unless File.exist?(wf) end # This can be a directory as well @other_files.each do |of| fail "Other files do not exist for: #{of[:path]}" unless File.exist?(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['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'] # make sure that the variable has a static value if variable['distribution']['static_value'].nil? || variable['distribution']['static_value'] == '' fail "Variable #{measure['name']}:#{variable['name']} needs a static value" end if variable['type'] == 'enum' || variable['type'] == 'Choice' # check something else # must be an integer or double if variable['distribution']['type'] == 'discrete_uncertain' if variable['distribution']['discrete_values'].nil? || variable['distribution']['discrete_values'] == '' fail "Variable #{measure['name']}:#{variable['name']} needs discrete values" end else if variable['distribution']['mean'].nil? || variable['distribution']['mean'] == '' fail "Variable #{measure['name']}:#{variable['name']} must have a mean" end if variable['distribution']['stddev'].nil? || variable['distribution']['stddev'] == '' fail "Variable #{measure['name']}:#{variable['name']} must have a stddev" end end if variable['distribution']['mean'].nil? || variable['distribution']['mean'] == '' fail "Variable #{measure['name']}:#{variable['name']} must have a mean/mode" end if variable['distribution']['min'].nil? || variable['distribution']['min'] == '' fail "Variable #{measure['name']}:#{variable['name']} must have a minimum" end if variable['distribution']['max'].nil? || variable['distribution']['max'] == '' fail "Variable #{measure['name']}:#{variable['name']} must have a maximum" end unless variable['type'] == 'string' if variable['distribution']['min'] > variable['distribution']['max'] fail "Variable min is greater than variable max for #{measure['name']}:#{variable['name']}" end end end end end end end dupes = variable_names.select { |e| variable_names.count(e) > 1 }.uniq if dupes.count > 0 fail "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 |