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.



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

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.



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

def construction_costs
  @construction_costs
end

#design_parametersObject

Returns the value of attribute design_parameters.



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

def design_parameters
  @design_parameters
end

#directory_nameObject

Returns the value of attribute directory_name.



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

def directory_name
  @directory_name
end

#distributed_generationObject

Returns the value of attribute distributed_generation.



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

def distributed_generation
  @distributed_generation
end

#feature_typeObject

Returns the value of attribute feature_type.



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

def feature_type
  @feature_type
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#locationObject

Returns the value of attribute location.



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

def location
  @location
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#power_distributionObject

Returns the value of attribute power_distribution.



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

def power_distribution
  @power_distribution
end

#programObject

Returns the value of attribute program.



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

def program
  @program
end

#reporting_periodsObject

Returns the value of attribute reporting_periods.



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

def reporting_periods
  @reporting_periods
end

#simulation_statusObject

Returns the value of attribute simulation_status.



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

def simulation_status
  @simulation_status
end

#thermal_storageObject

Returns the value of attribute thermal_storage.



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

def thermal_storage
  @thermal_storage
end

#timeseries_csvObject

Returns the value of attribute timeseries_csv.



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

def timeseries_csv
  @timeseries_csv
end

#timesteps_per_hourObject

Returns the value of attribute timesteps_per_hour.



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

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.



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

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.



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

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_feature_report(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 report results file without an extension



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

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

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

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

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

  # feature_hash
  feature_hash = to_hash

  json_name_path = File.join(@directory_name, 'feature_reports', 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, 'feature_reports', file_name + '.csv')
  end
  return true
end

#to_hashObject

Convert to a Hash equivalent for JSON serialization

  • Exclude attributes with nil values.

  • Validate feature_report hash properties against schema.



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

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