Class: URBANopt::Reporting::DefaultReports::FeatureReport

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

Overview

FeatureReport generates two types of reports in a simulation_dir. The default_feature_reports measure writes a ‘default_feature_reports.json’ file containing information on all features in the simulation. It also writes a ‘default_feature_reports.csv’ containing timeseries data for all features in the simulation. The DefaultPostProcessor reads these feature reports and aggregates them to create a ScenarioReport.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ FeatureReport

Each FeatureReport object corresponds to a single Feature.

parameters:

hash - Hash - A hash which may contain a deserialized feature_report.



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
92
93
94
95
96
97
98
99
100
101
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 64

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]
  @feature_type = hash[:feature_type]
  @timesteps_per_hour = hash[:timesteps_per_hour]
  @simulation_status = hash[:simulation_status]
  @timeseries_csv = TimeseriesCSV.new(hash[:timeseries_csv])
  @timeseries_csv.run_dir_name(@directory_name)
  @location = Location.new(hash[:location])
  @program = Program.new(hash[:program])
  # design_parameters to add later
  @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

  @distributed_generation = DistributedGeneration.new(hash[:distributed_generation])

  @power_distribution = PowerDistribution.new(hash[:power_distribution])

  @thermal_storage = ThermalStorage.new(hash[:thermal_storage])

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

  # initialize feature report file name to be saved.
  @file_name = 'default_feature_report'
end

Instance Attribute Details

#construction_costsObject

Returns the value of attribute construction_costs.



56
57
58
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 56

def construction_costs
  @construction_costs
end

#design_parametersObject

Returns the value of attribute design_parameters.



56
57
58
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 56

def design_parameters
  @design_parameters
end

#directory_nameObject

Returns the value of attribute directory_name.



56
57
58
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 56

def directory_name
  @directory_name
end

#distributed_generationObject

Returns the value of attribute distributed_generation.



56
57
58
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 56

def distributed_generation
  @distributed_generation
end

#feature_typeObject

Returns the value of attribute feature_type.



56
57
58
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 56

def feature_type
  @feature_type
end

#idObject

Returns the value of attribute id.



56
57
58
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 56

def id
  @id
end

#locationObject

Returns the value of attribute location.



56
57
58
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 56

def location
  @location
end

#nameObject

Returns the value of attribute name.



56
57
58
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 56

def name
  @name
end

#power_distributionObject

Returns the value of attribute power_distribution.



56
57
58
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 56

def power_distribution
  @power_distribution
end

#programObject

Returns the value of attribute program.



56
57
58
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 56

def program
  @program
end

#reporting_periodsObject

Returns the value of attribute reporting_periods.



56
57
58
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 56

def reporting_periods
  @reporting_periods
end

#simulation_statusObject

Returns the value of attribute simulation_status.



56
57
58
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 56

def simulation_status
  @simulation_status
end

#thermal_storageObject

Returns the value of attribute thermal_storage.



56
57
58
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 56

def thermal_storage
  @thermal_storage
end

#timeseries_csvObject

Returns the value of attribute timeseries_csv.



56
57
58
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 56

def timeseries_csv
  @timeseries_csv
end

#timesteps_per_hourObject

Returns the value of attribute timesteps_per_hour.



56
57
58
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 56

def timesteps_per_hour
  @timesteps_per_hour
end

Class Method Details

.from_simulation_dir(simulation_dir) ⇒ Object

Return an Array of FeatureReports for the simulation_dir as multiple Features can be simulated together in a single simulation directory.

  • Ensure that simulation_dir include only one feature.

  • Read in the reports written by measure if they exist.

parameters:

simulation_dir - SimulationDirOSW - A simulation directory from an OSW simulation, must include ‘default_feature_reports’ measure.



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

def self.from_simulation_dir(simulation_dir)
  result = []

  # simulation dir can include only one feature
  features = simulation_dir.features
  if features.size != 1
    raise 'FeatureReport cannot support multiple features per OSW'
  end

  # read in the reports written by measure
  default_feature_reports_json = nil
  default_feature_reports_csv = nil

  simulation_status = simulation_dir.simulation_status
  if simulation_status == 'Complete' || simulation_status == 'Failed'

    # read in the scenario reports JSON and CSV
    Dir.glob(File.join(simulation_dir.run_dir, '*_default_feature_reports/')).each do |dir|
      scenario_reports_json_path = File.join(dir, 'default_feature_reports.json')
      if File.exist?(scenario_reports_json_path)
        File.open(scenario_reports_json_path, 'r') do |file|
          default_feature_reports_json = JSON.parse(file.read, symbolize_names: true)
        end
      end
      scenario_reports_csv_path = File.join(dir, 'default_feature_reports.csv')
      if File.exist?(scenario_reports_csv_path)
        default_feature_reports_csv = scenario_reports_csv_path
      end
    end

  end

  # if we loaded the json
  if default_feature_reports_json # && default_feature_reports_json[:feature_reports]
    # default_feature_reports_json.each do |feature_report|
    # result << FeatureReport.new(feature_report)
    # end
    result << FeatureReport.new(default_feature_reports_json) # should we keep it as an array !? or each each report can only include 1 feature

  else
    # we did not find a report
    features.each do |feature|
      hash = {}
      hash[:id] = feature.id
      hash[:name] = feature.name
      hash[:directory_name] = simulation_dir.run_dir
      hash[:simulation_status] = simulation_status
      result << FeatureReport.new(hash)
    end
  end

  # validate feature_report json against schema
  if @@validator.validate(@@schema[:definitions][:FeatureReport][:properties], default_feature_reports_json).any?
    raise "default_feature_report_json properties does not match schema: #{@@validator.validate(@@schema[:definitions][:FeatureReport][:properties], default_feature_reports_json)}"
  end

  return result
end

Instance Method Details

#defaultsObject

Assign default values if values does not exist.



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 106

def defaults
  hash = {}
  hash[:timeseries_csv] = {}
  hash[:location] = {}
  hash[:program] = {}
  hash[:construction_costs] = []
  hash[:reporting_periods] = []
  hash[:distributed_generation] = {}
  hash[:power_distribution] = {}
  hash[:thermal_storage] = {}
  return hash
end

#save(file_name = 'default_feature_report') ⇒ Object

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

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



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

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

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

  # define the results_dir_path
  results_dir_path = File.join(@directory_name, 'feature_reports')
  # create feature reports directory
  Dir.mkdir(results_dir_path) unless Dir.exist?(File.join(@directory_name, 'feature_reports'))

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

  ## save json rport
  # feature_hash
  feature_hash = to_hash

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

  File.open(json_name_path, 'w') do |f|
    f.puts JSON.pretty_generate(feature_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

  return true
end

#save_csv_report(file_name = 'default_feature_report') ⇒ Object

Saves the ‘default_feature_report.csv’ file to the results directory

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



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 323

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

  # define the results_dir_path
  results_dir_path = File.join(@directory_name, 'feature_reports')
  # create feature reports directory
  Dir.mkdir(results_dir_path) unless Dir.exist?(File.join(@directory_name, 'feature_reports'))

  ## copy CSV report to the new feature_reports folder
  # get all folder names in the feature diectory
  directory_folders = Dir.glob "#{@directory_name}/*/"
  # copy the CSV report to the new feature_reports folder
  directory_folders.each do |f|
    if f.include? '_default_feature_reports'
      FileUtils.cp(File.join(f, 'default_feature_reports.csv'), File.join(results_dir_path, @file_name + '.csv'))
    end
  end
end

#save_feature_report(file_name = 'default_feature_report') ⇒ Object

Calls the individual functions to save ‘default_feature_report.json’ and ‘default_feature_report.csv’ For backward compatibility and ease of use

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



282
283
284
285
# File 'lib/urbanopt/reporting/default_reports/feature_report.rb', line 282

def save_feature_report(file_name = 'default_feature_report')
  save_json_report(file_name)
  save_csv_report(file_name)
end

#save_json_report(file_name = 'default_feature_report') ⇒ Object

Saves the ‘default_feature_report.json’ file to the results directory

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



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

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

  # define the results_dir_path
  results_dir_path = File.join(@directory_name, 'feature_reports')
  # create feature reports directory
  Dir.mkdir(results_dir_path) unless Dir.exist?(File.join(@directory_name, 'feature_reports'))

  ## save json rport
  # feature_hash
  feature_hash = to_hash

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

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

#to_hashObject

Convert to a Hash equivalent for JSON serialization

  • Exclude attributes with nil values.

  • Validate feature_report hash properties against schema.



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

def to_hash
  result = {}
  result[:id] = @id if @id
  result[:name] = @name if @name
  result[:directory_name] = @directory_name if @directory_name
  result[:feature_type] = @feature_type if @feature_type
  result[:timesteps_per_hour] = @timesteps_per_hour if @timesteps_per_hour
  result[:simulation_status] = @simulation_status if @simulation_status
  result[:timeseries_csv] = @timeseries_csv.to_hash

  result[:location] = @location.to_hash if @location

  result[:program] = @program.to_hash

  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[:distributed_generation] = @distributed_generation.to_hash if @distributed_generation

  result[:power_distribution] = @power_distribution.to_hash if @power_distribution

  result[:thermal_storage] = @thermal_storage.to_hash if @thermal_storage

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

  return result
end