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
36 37 38 39 40 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 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 36 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.
23 24 25 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 23 def algorithm @algorithm end |
#analysis_name ⇒ Object (readonly)
Returns the value of attribute analysis_name.
31 32 33 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 31 def analysis_name @analysis_name end |
#aws_tags ⇒ Object (readonly)
Returns the value of attribute aws_tags.
26 27 28 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 26 def @aws_tags end |
#cluster_name ⇒ Object
Method to return the cluster name for backwards compatibility
295 296 297 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 295 def cluster_name @cluster_name end |
#export_path ⇒ Object (readonly)
Returns the value of attribute export_path.
20 21 22 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 20 def export_path @export_path end |
#measure_paths ⇒ Object (readonly)
Returns the value of attribute measure_paths.
16 17 18 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 16 def measure_paths @measure_paths end |
#models ⇒ Object (readonly)
Returns the value of attribute models.
14 15 16 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 14 def models @models end |
#name ⇒ Object
remove these once we have classes to construct the JSON file
29 30 31 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 29 def name @name end |
#outputs ⇒ Object (readonly)
Returns the value of attribute outputs.
13 14 15 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 13 def outputs @outputs end |
#problem ⇒ Object (readonly)
Returns the value of attribute problem.
24 25 26 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 24 def problem @problem end |
#run_setup ⇒ Object (readonly)
Returns the value of attribute run_setup.
25 26 27 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 25 def run_setup @run_setup end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
11 12 13 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 11 def settings @settings end |
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
12 13 14 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 12 def variables @variables end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
10 11 12 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 10 def version @version end |
#weather_files ⇒ Object (readonly)
Returns the value of attribute weather_files.
15 16 17 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 15 def weather_files @weather_files end |
#weather_paths ⇒ Object (readonly)
Returns the value of attribute weather_paths.
17 18 19 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 17 def weather_paths @weather_paths end |
#worker_finals ⇒ Object (readonly)
Returns the value of attribute worker_finals.
19 20 21 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 19 def worker_finals @worker_finals end |
#worker_inits ⇒ Object (readonly)
Returns the value of attribute worker_inits.
18 19 20 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 18 def worker_inits @worker_inits end |
Instance Method Details
#add_model(name, display_name, type, path) ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 90 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
285 286 287 288 289 290 291 292 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 285 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
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 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 217 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
86 87 88 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 86 def delete_models @models = [] end |
#process ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 70 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.
301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 301 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
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 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 |
# File 'lib/openstudio/analysis/translator/excel.rb', line 99 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 |