Class: URBANopt::Reporting::DefaultReports::ScenarioReport

Inherits:
Object
  • Object
show all
Defined in:
lib/urbanopt/reporting/default_reports/scenario_report.rb

Overview

ScenarioReport can generate two types of reports from a scenario. The first is a JSON format saved to ‘default_scenario_report.json’. The second is a CSV format saved to ‘default_scenario_report.csv’.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ ScenarioReport

ScenarioReport class intializes the scenario report attributes: :id , :name , :directory_name, :timesteps_per_hour , :number_of_not_started_simulations , :number_of_started_simulations , :number_of_complete_simulations , :number_of_failed_simulations , :timeseries_csv , :location , :program , :construction_costs , :reporting_periods , :feature_reports

Each ScenarioReport object corresponds to a single Scenario.

parameters:

hash - Hash - A hash of a previously serialized ScenarioReport.



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
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 66

def initialize(hash = {})
  hash.delete_if { |k, v| v.nil? }
  hash = defaults.merge(hash)

  @id = hash[:id]
  @name = hash[:name]
  @directory_name = hash[:directory_name]
  @timesteps_per_hour = hash[:timesteps_per_hour]
  @number_of_not_started_simulations = hash[:number_of_not_started_simulations]
  @number_of_started_simulations = hash[:number_of_started_simulations]
  @number_of_complete_simulations = hash[:number_of_complete_simulations]
  @number_of_failed_simulations = hash[:number_of_failed_simulations]
  @timeseries_csv = TimeseriesCSV.new(hash[:timeseries_csv])
  @location = Location.new(hash[:location])
  @program = Program.new(hash[:program])
  @distributed_generation = DistributedGeneration.new(hash[:distributed_generation] || {})

  @construction_costs = []
  hash[:construction_costs].each do |cc|
    @constructiion_costs << ConstructionCost.new(cc)
  end

  @reporting_periods = []
  hash[:reporting_periods].each do |rp|
    @reporting_periods << ReportingPeriod.new(rp)
  end

  # feature_report is intialized here to be used in the add_feature_report method
  @feature_reports = []
  hash[:feature_reports].each do |fr|
    @feature_reports << FeatureReport.new(fr)
  end

  @file_name = 'default_scenario_report'

  # initialize class variables @@validator and @@schema
  @@validator ||= Validator.new
  @@schema ||= @@validator.schema

  # initialize @@logger
  @@logger ||= URBANopt::Reporting::DefaultReports.logger
end

Instance Attribute Details

#construction_costsObject

Returns the value of attribute construction_costs.



53
54
55
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 53

def construction_costs
  @construction_costs
end

#directory_nameObject

Returns the value of attribute directory_name.



53
54
55
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 53

def directory_name
  @directory_name
end

#distributed_generationObject

Returns the value of attribute distributed_generation.



53
54
55
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 53

def distributed_generation
  @distributed_generation
end

#feature_reportsObject

Returns the value of attribute feature_reports.



53
54
55
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 53

def feature_reports
  @feature_reports
end

#idObject

Returns the value of attribute id.



53
54
55
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 53

def id
  @id
end

#locationObject

Returns the value of attribute location.



53
54
55
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 53

def location
  @location
end

#nameObject

Returns the value of attribute name.



53
54
55
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 53

def name
  @name
end

#number_of_complete_simulationsObject

Returns the value of attribute number_of_complete_simulations.



53
54
55
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 53

def number_of_complete_simulations
  @number_of_complete_simulations
end

#number_of_failed_simulationsObject

Returns the value of attribute number_of_failed_simulations.



53
54
55
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 53

def number_of_failed_simulations
  @number_of_failed_simulations
end

#number_of_not_started_simulationsObject

Returns the value of attribute number_of_not_started_simulations.



53
54
55
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 53

def number_of_not_started_simulations
  @number_of_not_started_simulations
end

#number_of_started_simulationsObject

Returns the value of attribute number_of_started_simulations.



53
54
55
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 53

def number_of_started_simulations
  @number_of_started_simulations
end

#programObject

Returns the value of attribute program.



53
54
55
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 53

def program
  @program
end

#reporting_periodsObject

Returns the value of attribute reporting_periods.



53
54
55
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 53

def reporting_periods
  @reporting_periods
end

#timeseries_csvObject

Returns the value of attribute timeseries_csv.



53
54
55
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 53

def timeseries_csv
  @timeseries_csv
end

#timesteps_per_hourObject

Returns the value of attribute timesteps_per_hour.



53
54
55
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 53

def timesteps_per_hour
  @timesteps_per_hour
end

Instance Method Details

#add_feature_report(feature_report) ⇒ Object

Add feature reports to each other.

  • check if a feature report have been already added.

  • check feature simulation status

  • merge timeseries_csv information

  • merge program information

  • merge construction_cost information

  • merge reporting_periods information

  • add the array of feature_reports

  • scenario report location takes the location of the first feature in the list

parmeters:

feature_report - FeatureReport - An object of FeatureReport class.



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
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 252

def add_feature_report(feature_report)
  # check if the timesteps_per_hour are identical
  if @timesteps_per_hour.nil? || @timesteps_per_hour == ''
    @timesteps_per_hour = feature_report.timesteps_per_hour
  else
    if feature_report.timesteps_per_hour.is_a?(Integer) && feature_report.timesteps_per_hour != @timesteps_per_hour
      raise "FeatureReport timesteps_per_hour = '#{feature_report.timesteps_per_hour}' does not match scenario timesteps_per_hour '#{@timesteps_per_hour}'"
    end
  end

  # check if first report_report_datetime are identical.
  if @timeseries_csv.first_report_datetime.nil? || @timeseries_csv.first_report_datetime == ''
    @timeseries_csv.first_report_datetime = feature_report.timeseries_csv.first_report_datetime
  else
    if feature_report.timeseries_csv.first_report_datetime != @timeseries_csv.first_report_datetime
      raise "first_report_datetime '#{@first_report_datetime}' does not match other.first_report_datetime '#{feature_report.timeseries_csv.first_report_datetime}'"
    end
  end

  # check that we have not already added this feature
  id = feature_report.id
  @feature_reports.each do |existing_feature_report|
    if existing_feature_report.id == id
      raise "FeatureReport with id = '#{id}' has already been added"
    end
  end

  # check feature simulation status
  if feature_report.simulation_status == 'Not Started'
    @number_of_not_started_simulations += 1
  elsif feature_report.simulation_status == 'Started'
    @number_of_started_simulations += 1
  elsif feature_report.simulation_status == 'Complete'
    @number_of_complete_simulations += 1
  elsif feature_report.simulation_status == 'Failed'
    @number_of_failed_simulations += 1
  else
    raise "Unknown feature_report simulation_status = '#{feature_report.simulation_status}'"
  end

  # merge timeseries_csv information
  @timeseries_csv.add_timeseries_csv(feature_report.timeseries_csv)

  @timeseries_csv.run_dir_name(@directory_name)

  # merge program information
  @program.add_program(feature_report.program)

  # merge construction_cost information
  @construction_costs = ConstructionCost.merge_construction_costs(@construction_costs, feature_report.construction_costs)

  # merge reporting_periods information
  @reporting_periods = ReportingPeriod.merge_reporting_periods(@reporting_periods, feature_report.reporting_periods)

  @distributed_generation = DistributedGeneration.merge_distributed_generation(@distributed_generation, feature_report.distributed_generation)

  # add feature_report
  @feature_reports << feature_report

  # scenario report location takes the location of the first feature in the list
  @location = feature_reports[0].location
end

#csv_pathObject

Gets the saved CSV file path.



141
142
143
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 141

def csv_path
  File.join(@directory_name, @file_name + '.csv')
end

#defaultsObject

Assigns default values if values do not exist.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 112

def defaults
  hash = {}
  hash[:id] = nil.to_s
  hash[:name] = nil.to_s
  hash[:directory_name] = nil.to_s
  hash[:timesteps_per_hour] = nil # unknown
  hash[:number_of_not_started_simulations] = 0
  hash[:number_of_started_simulations] = 0
  hash[:number_of_complete_simulations] = 0
  hash[:number_of_failed_simulations] = 0
  hash[:timeseries_csv] = TimeseriesCSV.new.to_hash
  hash[:location] = Location.new.defaults
  hash[:program] = Program.new.to_hash
  hash[:construction_costs] = []
  hash[:reporting_periods] = []
  hash[:feature_reports] = []
  return hash
end

#json_pathObject

Gets the saved JSON file path.



134
135
136
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 134

def json_path
  File.join(@directory_name, @file_name + '.json')
end

#save(file_name = 'default_scenario_report') ⇒ Object

Saves the ‘default_scenario_report.json’ and ‘default_scenario_report.csv’ files

[parameters]: file_name - String - Assign a name to the saved scenario results file without an extension



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
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 150

def save(file_name = 'default_scenario_report')
  # reassign the initialize local variable @file_name to the file name input.
  @file_name = file_name

  # save the scenario reports csv and json data
  old_timeseries_path = nil
  if !@timeseries_csv.path.nil?
    old_timeseries_path = @timeseries_csv.path
  end

  @timeseries_csv.path = File.join(@directory_name, file_name + '.csv')
  @timeseries_csv.save_data

  hash = {}
  hash[:scenario_report] = to_hash
  hash[:feature_reports] = []
  @feature_reports.each do |feature_report|
    hash[:feature_reports] << feature_report.to_hash
  end

  json_name_path = File.join(@directory_name, file_name + '.json')

  File.open(json_name_path, 'w') do |f|
    f.puts JSON.pretty_generate(hash)
    # make sure data is written to the disk one way or the other
    begin
      f.fsync
    rescue StandardError
      f.flush
    end
  end

  if !old_timeseries_path.nil?
    @timeseries_csv.path = old_timeseries_path
  else
    @timeseries_csv.path = File.join(@directory_name, file_name + '.csv')
  end

  # save the feature reports csv and json data
  # @feature_reports.each do |feature_report|
  #  feature_report.save_feature_report()
  # end

  return true
end

#to_hashObject

Converts to a Hash equivalent for JSON serialization.

  • Exclude attributes with nil values.

  • Validate reporting_period hash properties against schema.



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
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 202

def to_hash
  result = {}
  result[:id] = @id if @id
  result[:name] = @name if @name
  result[:directory_name] = @directory_name if @directory_name
  result[:timesteps_per_hour] = @timesteps_per_hour if @timesteps_per_hour
  result[:number_of_not_started_simulations] = @number_of_not_started_simulations if @number_of_not_started_simulations
  result[:number_of_started_simulations] = @number_of_started_simulations if @number_of_started_simulations
  result[:number_of_complete_simulations] = @number_of_complete_simulations if @number_of_complete_simulations
  result[:number_of_failed_simulations] = @number_of_failed_simulations if @number_of_failed_simulations
  result[:timeseries_csv] = @timeseries_csv.to_hash if @timeseries_csv
  result[:location] = @location.to_hash if @location
  result[:program] = @program.to_hash if @program
  result[:distributed_generation] = @distributed_generation.to_hash if @distributed_generation

  result[:construction_costs] = []
  @construction_costs&.each { |cc| result[:construction_costs] << cc.to_hash }

  result[:reporting_periods] = []
  @reporting_periods&.each { |rp| result[:reporting_periods] << rp.to_hash }

  # result[:feature_reports] = []
  # @feature_reports.each { |fr| result[:feature_reports] << fr.to_hash } if @feature_reports

  # validate scenario_report properties against schema
  if @@validator.validate(@@schema[:definitions][:ScenarioReport][:properties], result).any?
    raise "scenario_report properties does not match schema: #{@@validator.validate(@@schema[:definitions][:ScenarioReport][:properties], result)}"
  end

  # have to use the module method because we have not yet initialized the class one
  @@logger.info("Scenario name: #{@name}")

  return result
end