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.
-
#analysis_name ⇒ Object
readonly
Returns the value of attribute analysis_name.
-
#aws_tags ⇒ Object
readonly
Returns the value of attribute aws_tags.
-
#cluster_name ⇒ Object
Method to return the cluster name for backwards compatibility.
-
#export_path ⇒ Object
readonly
Returns the value of attribute export_path.
-
#measure_paths ⇒ Object
readonly
Returns the value of attribute measure_paths.
-
#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.
-
#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.
-
#weather_paths ⇒ Object
readonly
Returns the value of attribute weather_paths.
-
#worker_finals ⇒ Object
readonly
Returns the value of attribute worker_finals.
-
#worker_inits ⇒ Object
readonly
Returns the value of attribute worker_inits.
Instance Method Summary collapse
- #add_model(name, display_name, type, path) ⇒ Object
-
#analyses ⇒ Object
Return an array of analyses objects of OpenStudio::Analysis::Formulation.
-
#analysis(seed_model = nil, append_model_name = false) ⇒ Object
convert the data in excel’s parsed data into an OpenStudio Analysis Object.
-
#delete_models ⇒ Object
Helper methods to remove models and add new ones programatically.
-
#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.
- #validate_analysis ⇒ Object
Constructor Details
#initialize(xls_filename) ⇒ Excel
pass in the filename to read
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 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 66 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 raise "File #{@xls_filename} does not exist" end # Initialize some other instance variables @version = '0.0.1' @analyses = [] # Array o OpenStudio::Analysis. Use method to access @name = nil @analysis_name = nil @settings = {} @weather_files = [] @weather_paths = [] @models = [] @other_files = [] @worker_inits = [] @worker_finals = [] @export_path = './export' @measure_paths = [] @number_of_samples = 0 # TODO: remove this @problem = {} @algorithm = {} @outputs = {} @run_setup = {} @aws_tags = [] end |
Instance Attribute Details
#algorithm ⇒ Object (readonly)
Returns the value of attribute algorithm.
53 54 55 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 53 def algorithm @algorithm end |
#analysis_name ⇒ Object (readonly)
Returns the value of attribute analysis_name.
61 62 63 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 61 def analysis_name @analysis_name end |
#aws_tags ⇒ Object (readonly)
Returns the value of attribute aws_tags.
56 57 58 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 56 def @aws_tags end |
#cluster_name ⇒ Object
Method to return the cluster name for backwards compatibility
325 326 327 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 325 def cluster_name @cluster_name end |
#export_path ⇒ Object (readonly)
Returns the value of attribute export_path.
50 51 52 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 50 def export_path @export_path end |
#measure_paths ⇒ Object (readonly)
Returns the value of attribute measure_paths.
46 47 48 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 46 def measure_paths @measure_paths end |
#models ⇒ Object (readonly)
Returns the value of attribute models.
44 45 46 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 44 def models @models end |
#name ⇒ Object
remove these once we have classes to construct the JSON file
59 60 61 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 59 def name @name end |
#outputs ⇒ Object (readonly)
Returns the value of attribute outputs.
43 44 45 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 43 def outputs @outputs end |
#problem ⇒ Object (readonly)
Returns the value of attribute problem.
54 55 56 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 54 def problem @problem end |
#run_setup ⇒ Object (readonly)
Returns the value of attribute run_setup.
55 56 57 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 55 def run_setup @run_setup end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
41 42 43 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 41 def settings @settings end |
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
42 43 44 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 42 def variables @variables end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
40 41 42 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 40 def version @version end |
#weather_files ⇒ Object (readonly)
Returns the value of attribute weather_files.
45 46 47 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 45 def weather_files @weather_files end |
#weather_paths ⇒ Object (readonly)
Returns the value of attribute weather_paths.
47 48 49 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 47 def weather_paths @weather_paths end |
#worker_finals ⇒ Object (readonly)
Returns the value of attribute worker_finals.
49 50 51 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 49 def worker_finals @worker_finals end |
#worker_inits ⇒ Object (readonly)
Returns the value of attribute worker_inits.
48 49 50 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 48 def worker_inits @worker_inits end |
Instance Method Details
#add_model(name, display_name, type, path) ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 120 def add_model(name, display_name, type, path) @models << { name: name, display_name: display_name, type: type, path: path } end |
#analyses ⇒ Object
Return an array of analyses objects of OpenStudio::Analysis::Formulation
315 316 317 318 319 320 321 322 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 315 def analyses as = [] @models.map do |model| as << analysis(model, @models.count > 1) end as end |
#analysis(seed_model = nil, append_model_name = false) ⇒ Object
convert the data in excel’s parsed data into an OpenStudio Analysis Object
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 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 247 def analysis(seed_model = nil, append_model_name = false) raise 'There are no seed models defined in the excel file. Please add one.' if @models.empty? raise "There are more than one seed models defined in the excel file. Call 'analyses' to return the array" if @models.size > 1 && seed_model.nil? seed_model = @models.first if seed_model.nil? # Use the programmatic interface to make the analysis # append the model name to the analysis name if requested (normally if there are more than 1 models in the spreadsheet) display_name = append_model_name ? @name + ' ' + seed_model[:display_name] : @name a = OpenStudio::Analysis.create(display_name) @variables['data'].each do |measure| next unless measure['enabled'] @measure_paths.each do |measure_path| measure_dir_to_add = "#{measure_path}/#{measure['measure_file_name_directory']}" if Dir.exist? measure_dir_to_add if File.exist? "#{measure_dir_to_add}/measure.rb" measure['local_path_to_measure'] = "#{measure_dir_to_add}/measure.rb" break else raise "Measure in directory '#{measure_dir_to_add}' did not contain a measure.rb file" end end end raise "Could not find measure '#{measure['name']}' in directory named '#{measure['measure_file_name_directory']}' in the measure paths '#{@measure_paths.join(', ')}'" unless measure['local_path_to_measure'] a.workflow.add_measure_from_excel(measure) end @other_files.each do |library| a.libraries.add(library[:path], library_name: library[:lib_zip_name]) end @worker_inits.each do |w| a.worker_inits.add(w[:path], args: w[:args]) end @worker_finals.each do |w| a.worker_finalizes.add(w[:path], args: w[:args]) end # Add in the outputs @outputs['output_variables'].each do |o| o = Hash[o.map { |k, v| [k.to_sym, v] }] a.add_output(o) end a.analysis_type = @problem['analysis_type'] @algorithm.each do |k, v| a.algorithm.set_attribute(k, v) end # clear out the seed files before adding new ones a.seed_model = seed_model[:path] # clear out the weather files before adding new ones a.weather_files.clear @weather_paths.each do |wp| a.weather_files.add_files(wp) end a end |
#delete_models ⇒ Object
Helper methods to remove models and add new ones programatically. Note that these should be moved into a general analysis class
116 117 118 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 116 def delete_models @models = [] end |
#process ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 100 def process @setup = parse_setup @version = Semantic::Version.new @version raise "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.
331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 331 def save_analysis analyses.each do |a| puts "Saving JSON and ZIP file for #{@name}:#{a.display_name}" json_file_name = "#{@export_path}/#{a.name}.json" FileUtils.rm_f(json_file_name) if File.exist?(json_file_name) # File.open(json_file_name, 'w') { |f| f << JSON.pretty_generate(new_analysis_json) } a.save json_file_name a.save_zip "#{File.dirname(json_file_name)}/#{File.basename(json_file_name, '.*')}.zip" end end |
#validate_analysis ⇒ Object
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 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 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 129 def validate_analysis # Setup the paths and do some error checking @measure_paths.each do |mp| raise "Measures directory '#{mp}' does not exist" unless Dir.exist?(mp) end @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.exist?(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.exist?(wf) end # This can be a directory as well @other_files.each do |f| raise "Other files do not exist for: #{f[:path]}" unless File.exist?(f[:path]) end @worker_inits.each do |f| raise "Worker initialization file does not exist for: #{f[:path]}" unless File.exist?(f[:path]) end @worker_finals.each do |f| raise "Worker finalization file does not exist for: #{f[:path]}" unless File.exist?(f[:path]) end FileUtils.mkdir_p(@export_path) # verify that the measure display names are unique # puts @variables.inspect measure_display_names = @variables['data'].map { |m| m['enabled'] ? m['display_name'] : nil }.compact measure_display_names_mult = measure_display_names.select { |m| measure_display_names.count(m) > 1 }.uniq if measure_display_names_mult && !measure_display_names_mult.empty? raise "Measure Display Names are not unique for '#{measure_display_names_mult.join('\', \'')}'" end # verify that all continuous variables have all the data needed and create a name map 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 variables have static values if variable['distribution']['static_value'].nil? || variable['distribution']['static_value'] == '' raise "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'] == '' raise "Variable #{measure['name']}:#{variable['name']} needs discrete values" end elsif variable['distribution']['type'] == 'integer_sequence' if variable['distribution']['mean'].nil? || variable['distribution']['mean'] == '' raise "Variable #{measure['name']}:#{variable['name']} must have a mean/mode" end 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 else 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 end if variable['distribution']['mean'].nil? || variable['distribution']['mean'] == '' raise "Variable #{measure['name']}:#{variable['name']} must have a mean/mode" end 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 unless variable['type'] == 'string' || variable['type'] =~ /bool/ if variable['distribution']['min'] > variable['distribution']['max'] raise "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 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 |