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 initializes 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, :distributed_generation, :scenario_power_distribution, :qaqc_flags

Each ScenarioReport object corresponds to a single Scenario.

parameters:

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 47

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] || {})
  @scenario_power_distribution = ScenarioPowerDistribution.new(hash[:scenario_power_distribution] || {})
  @scenario_power_distribution_cost = ScenarioPowerDistributionCost.new(hash[:scenario_power_distribution_cost] || {})
  @qaqc_flags = QAQC.new(hash[:qaqc_flags])

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

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

  # feature_report is initialized 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.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

def construction_costs
  @construction_costs
end

#directory_nameObject

Returns the value of attribute directory_name.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

def directory_name
  @directory_name
end

#distributed_generationObject

Returns the value of attribute distributed_generation.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

def distributed_generation
  @distributed_generation
end

#feature_reportsObject

Returns the value of attribute feature_reports.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

def feature_reports
  @feature_reports
end

#idObject

Returns the value of attribute id.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

def id
  @id
end

#locationObject

Returns the value of attribute location.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

def location
  @location
end

#nameObject

Returns the value of attribute name.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

def name
  @name
end

#number_of_complete_simulationsObject

Returns the value of attribute number_of_complete_simulations.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

def number_of_complete_simulations
  @number_of_complete_simulations
end

#number_of_failed_simulationsObject

Returns the value of attribute number_of_failed_simulations.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

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.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

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.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

def number_of_started_simulations
  @number_of_started_simulations
end

#programObject

Returns the value of attribute program.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

def program
  @program
end

#qaqc_flagsObject

Returns the value of attribute qaqc_flags.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

def qaqc_flags
  @qaqc_flags
end

#reporting_periodsObject

Returns the value of attribute reporting_periods.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

def reporting_periods
  @reporting_periods
end

#scenario_power_distributionObject

Returns the value of attribute scenario_power_distribution.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

def scenario_power_distribution
  @scenario_power_distribution
end

#scenario_power_distribution_costObject

Returns the value of attribute scenario_power_distribution_cost.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

def scenario_power_distribution_cost
  @scenario_power_distribution_cost
end

#timeseries_csvObject

Returns the value of attribute timeseries_csv.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

def timeseries_csv
  @timeseries_csv
end

#timesteps_per_hourObject

Returns the value of attribute timesteps_per_hour.



31
32
33
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 31

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 qaqc_flags 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

parameters:

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
314
315
316
317
318
# 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
  case feature_report.simulation_status
  when 'Not Started'
    @number_of_not_started_simulations += 1
  when 'Started'
    @number_of_started_simulations += 1
  when 'Complete'
    @number_of_complete_simulations += 1
  when '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)

  # merge distributed_generation information
  @distributed_generation = DistributedGeneration.merge_distributed_generation(@distributed_generation, feature_report.distributed_generation)

  # merge qaqc_flags information
  @qaqc_flags.add_qaqc_flags(feature_report.qaqc_flags)

  # 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.



126
127
128
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 126

def csv_path
  File.join(@directory_name, "#{@file_name}.csv")
end

#defaultsObject

Assigns default values if values do not exist.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 96

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[:qaqc_flags] = QAQC.new.to_hash
  hash[:construction_costs] = []
  hash[:reporting_periods] = []
  hash[:feature_reports] = []
  return hash
end

#json_pathObject

Gets the saved JSON file path.



119
120
121
# File 'lib/urbanopt/reporting/default_reports/scenario_report.rb', line 119

def json_path
  File.join(@directory_name, "#{@file_name}.json")
end

#save(file_name = 'default_scenario_report', save_feature_reports = true, save_csv_reports = true) ⇒ 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



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

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

  if save_csv_reports == true
    # 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
  end

  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 save_csv_reports == true
    if !old_timeseries_path.nil?
      @timeseries_csv.path = old_timeseries_path
    else
      @timeseries_csv.path = File.join(@directory_name, "#{file_name}.csv")
    end
  end

  if save_feature_reports
    if file_name == 'default_scenario_report'
      file_name = 'default_feature_report'
    end
    # save the feature reports csv and json data
    @feature_reports.each do |feature_report|
      feature_report.save file_name
    end
  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.



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

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[:scenario_power_distribution] = @scenario_power_distribution.to_hash if @scenario_power_distribution
  result[:scenario_power_distribution_cost] = @scenario_power_distribution_cost.to_hash if @scenario_power_distribution_cost
  result[:qaqc_flags] = @qaqc_flags.to_hash if @qaqc_flags

  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
  unless @name == '' || @name.nil?
    @@logger.info("Scenario name: #{@name}")
  end

  return result
end