Class: OpenStudio::Analysis::Translator::Excel

Inherits:
Object
  • Object
show all
Defined in:
lib/openstudio/analysis/translator/excel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xls_filename) ⇒ Excel

pass in the filename to read



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
57
58
59
60
61
62
# File 'lib/openstudio/analysis/translator/excel.rb', line 30

def initialize(xls_filename)
  @xls_filename = xls_filename
  @root_path = File.expand_path(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
  @analysis_name = nil
  @cluster_name = nil
  @settings = {}
  @weather_files = []
  @models = []
  @other_files = []
  @worker_inits = []
  @worker_finals = []
  @export_path = './export'
  @measure_paths = []
  @number_of_samples = 0 # todo: remove this
  @problem = {}
  @algorithm = {}
  @template_json = nil
  @outputs = {}
  @run_setup = {}
  @aws_tags = []
end

Instance Attribute Details

#algorithm ⇒ Object (readonly)

Returns the value of attribute algorithm.



17
18
19
# File 'lib/openstudio/analysis/translator/excel.rb', line 17

def algorithm
  @algorithm
end

#analysis_name ⇒ Object (readonly)

Returns the value of attribute analysis_name.



24
25
26
# File 'lib/openstudio/analysis/translator/excel.rb', line 24

def analysis_name
  @analysis_name
end

#aws_tags ⇒ Object (readonly)

Returns the value of attribute aws_tags.



20
21
22
# File 'lib/openstudio/analysis/translator/excel.rb', line 20

def aws_tags
  @aws_tags
end

#cluster_name ⇒ Object (readonly)

Returns the value of attribute cluster_name.



15
16
17
# File 'lib/openstudio/analysis/translator/excel.rb', line 15

def cluster_name
  @cluster_name
end

#export_path ⇒ Object (readonly)

Returns the value of attribute export_path.



14
15
16
# File 'lib/openstudio/analysis/translator/excel.rb', line 14

def export_path
  @export_path
end

#measure_paths ⇒ Object (readonly)

Returns the value of attribute measure_paths.



11
12
13
# File 'lib/openstudio/analysis/translator/excel.rb', line 11

def measure_paths
  @measure_paths
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



23
24
25
# File 'lib/openstudio/analysis/translator/excel.rb', line 23

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.



18
19
20
# File 'lib/openstudio/analysis/translator/excel.rb', line 18

def problem
  @problem
end

#run_setup ⇒ Object (readonly)

Returns the value of attribute run_setup.



19
20
21
# File 'lib/openstudio/analysis/translator/excel.rb', line 19

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.



25
26
27
# File 'lib/openstudio/analysis/translator/excel.rb', line 25

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

#worker_finals ⇒ Object (readonly)

Returns the value of attribute worker_finals.



13
14
15
# File 'lib/openstudio/analysis/translator/excel.rb', line 13

def worker_finals
  @worker_finals
end

#worker_inits ⇒ Object (readonly)

Returns the value of attribute worker_inits.



12
13
14
# File 'lib/openstudio/analysis/translator/excel.rb', line 12

def worker_inits
  @worker_inits
end

Instance Method Details

#add_directory_to_zip(zipfile, local_directory, relative_zip_directory) ⇒ Object



361
362
363
364
365
366
367
368
# File 'lib/openstudio/analysis/translator/excel.rb', line 361

def add_directory_to_zip(zipfile, local_directory, relative_zip_directory)
  # puts "Add Directory #{local_directory}"
  Dir[File.join("#{local_directory}", '**', '**')].each do |file|
    # puts "Adding File #{file}"
    zipfile.add(file.sub(local_directory, relative_zip_directory), file)
  end
  zipfile
end

#add_model(name, display_name, type, path) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/openstudio/analysis/translator/excel.rb', line 84

def add_model(name, display_name, type, path)
  @models << {
    name: name,
    display_name: display_name,
    type: type,
    path: path
  }
end

#create_analysis_hash ⇒ Object



202
203
204
205
206
207
208
209
210
211
# File 'lib/openstudio/analysis/translator/excel.rb', line 202

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

  # save the other format for now
  translate_to_analysis

  @template_json
end

#deep_copy(o) ⇒ Object



472
473
474
# File 'lib/openstudio/analysis/translator/excel.rb', line 472

def deep_copy(o)
  Marshal.load(Marshal.dump(o))
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



80
81
82
# File 'lib/openstudio/analysis/translator/excel.rb', line 80

def delete_models
  @models = []
end

#process ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/openstudio/analysis/translator/excel.rb', line 64

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.



215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/openstudio/analysis/translator/excel.rb', line 215

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|
    puts "Creating JSON and ZIP file for #{@name}:#{model[:display_name]}"
    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



94
95
96
97
# File 'lib/openstudio/analysis/translator/excel.rb', line 94

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 ⇒ Object

New method that uses the objects to hold the analysis



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/openstudio/analysis/translator/excel.rb', line 229

def translate_to_analysis
  # Use the programmatic interface to make the analysis json now
  analysis = OpenStudio::Analysis.create(@analysis_name)

  @variables['data'].each do |measure|
    next unless measure['enabled']

    # TODO: make this read the measure.rb file, then override any of the differences. That is call workflow.add_from_measure_path
    analysis.workflow.add_measure_from_excel(measure)
  end

  # Add in the outputs

  @outputs['output_variables'].each do |o|
    o = Hash[o.map { |k, v| [k.to_sym, v] }]
    analysis.add_output(o)
  end

  # Temp save of the json file
  # analysis.save("#{analysis.display_name}_api.json")

  analysis
end

#translate_to_analysis_json_template ⇒ Object



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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/openstudio/analysis/translator/excel.rb', line 253

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.read("#{template_root}/argument.json.erb"))

  # Templated analysis json file (this is what is returned)
  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']}' to analysis.json"
      @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 'string', 'choice'
              @static_value = @variable['distribution']['static_value'].inspect
            else
              @static_value = @variable['distribution']['static_value']
          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 'string', 'choice'
              @static_value = @variable['distribution']['static_value'].inspect
            else
              @static_value = @variable['distribution']['static_value']
          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
            if @variable['distribution']['discrete_weights']
              fail "Discrete variable '#{@variable['name']}' does not have equal length of values and weights" if @variable['distribution']['discrete_values'].size != @variable['distribution']['discrete_weights'].size
              @values_and_weights = @variable['distribution']['discrete_values'].zip(@variable['distribution']['discrete_weights']).map { |v, w| { value: v, weight: w } }.to_json
            else
              @values_and_weights = @variable['distribution']['discrete_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



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
# 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|
    fail "Measures directory '#{mp}' does not exist" unless Dir.exist?(mp)
  end

  @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 |f|
    fail "Other files do not exist for: #{f[:path]}" unless File.exist?(f[:path])
  end

  @worker_inits.each do |f|
    fail "Worker initialization file does not exist for: #{f[:path]}" unless File.exist?(f[:path])
  end

  @worker_finals.each do |f|
    fail "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?
    fail "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'] == ''
            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' || variable['type'] =~ /bool/
              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