Skip to content

Commit

Permalink
add rubocop to rake tasks (#151)
Browse files Browse the repository at this point in the history
* add rubocop to rake tasks

* teeny tiny tweak to rakefile

* more rubocop settings

* rubocop auto_correct

* point to develop instead of released branches temporarily

Co-authored-by: kflemin <[email protected]>
  • Loading branch information
vtnate and kflemin authored May 5, 2022
1 parent 6b87f90 commit 84b2a16
Show file tree
Hide file tree
Showing 16 changed files with 1,261 additions and 1,232 deletions.
8 changes: 8 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

AllCops:
Exclude:
- 'spec/test_measures/**/*'
- 'example_project/resources/**/*'
require: rubocop-performance
inherit_from:
- http://s3.amazonaws.com/openstudio-resources/styles/rubocop_v4.yml
18 changes: 13 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
source 'http://rubygems.org'

ruby '~> 2.7.0'
gem 'rubocop', '~> 1.15.0', require: false
gem 'rubocop-checkstyle_formatter', '~> 0.4.0'
gem 'rubocop-performance', '~> 1.11.3'

# Local gems are useful when developing and integrating the various dependencies.
# To favor the use of local gems, set the following environment variable:
Expand All @@ -19,6 +22,11 @@ allow_local = ENV['FAVOR_LOCAL_GEMS']
# gem 'openstudio-extension', github: 'NREL/openstudio-extension-gem', branch: 'develop'
# end

gem 'openstudio-common-measures', github: 'NREL/openstudio-common-measures-gem', branch: 'develop'
gem 'openstudio-model-articulation', github: 'NREL/openstudio-model-articulation-gem', branch: 'develop'
gem 'openstudio-ee', github: 'NREL/openstudio-ee-gem', branch: 'develop'
gem 'openstudio-calibration', github: 'NREL/openstudio-calibration-gem', branch: 'develop'

# if allow_local && File.exist?('../urbanopt-core-gem')
# gem 'urbanopt-core', path: '../urbanopt-core-gem'
# elsif allow_local
Expand All @@ -42,13 +50,13 @@ else
end

# TODO: Uncomment and revert changes once gem is released
#if allow_local && File.exist?('../urbanopt-geojson-gem')
# if allow_local && File.exist?('../urbanopt-geojson-gem')
# gem 'urbanopt-geojson', path: '../urbanopt-geojson-gem'
#elsif allow_local
gem 'urbanopt-geojson', github: 'URBANopt/urbanopt-geojson-gem', branch: 'develop'
#else
# elsif allow_local
gem 'urbanopt-geojson', github: 'URBANopt/urbanopt-geojson-gem', branch: 'develop'
# else
# gem 'urbanopt-geojson', '~> 0.7.0'
#end
# end

if allow_local && File.exist?('../urbanopt-reopt-gem')
gem 'urbanopt-reopt', path: '../urbanopt-reopt-gem'
Expand Down
80 changes: 39 additions & 41 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@

require 'openstudio/extension'
require 'openstudio/extension/rake_task'
require 'rubocop/rake_task'
require 'urbanopt/scenario'
require 'urbanopt/geojson'
require 'urbanopt/reopt'
require 'urbanopt/reopt_scenario'
require_relative 'developer_nrel_key'

RuboCop::RakeTask.new

module URBANopt
module ExampleGeoJSONProject
class ExampleGeoJSONProject < OpenStudio::Extension::Extension

# number of datapoints(features) you want to run in parallel
# based on the number of available processors on your local machine.
# This does not seem to function, instead added line to code for runner.config file
Expand All @@ -55,15 +57,14 @@ module URBANopt

# Return the absolute path of the measures or empty string if there is none, can be used when configuring OSWs
def measures_dir
""
''
end

# Relevant files such as weather data, design days, etc.
# Return the absolute path of the files or nil if there is none, used when configuring OSWs
def files_dir
return File.absolute_path(File.join(@root_dir, 'weather'))
end

end
end
end
Expand Down Expand Up @@ -152,12 +153,12 @@ end
def configure_project
# write a runner.conf in project dir if it does not exist
# delete runner.conf to automatically regenerate it
options = {gemfile_path: File.join(root_dir, 'Gemfile'), bundle_install_path: File.join(root_dir, ".bundle/install"), num_parallel: 7}
options = { gemfile_path: File.join(root_dir, 'Gemfile'), bundle_install_path: File.join(root_dir, '.bundle/install'), num_parallel: 7 }

# write a runner.conf in project dir (if it does not already exist)
if !File.exists?(File.join(root_dir, 'runner.conf'))
puts "GENERATING runner.conf file"
OpenStudio::Extension::RunnerConfig.init(root_dir) # itinialize the file with default values
if !File.exist?(File.join(root_dir, 'runner.conf'))
puts 'GENERATING runner.conf file'
OpenStudio::Extension::RunnerConfig.init(root_dir) # itinialize the file with default values
run_config = OpenStudio::Extension::RunnerConfig.new(root_dir) # get the configs
# update paths
options.each do |key, val|
Expand All @@ -166,7 +167,7 @@ def configure_project
# save back to disk
run_config.save
else
puts "USING existing runner.conf file"
puts 'USING existing runner.conf file'
end
end

Expand All @@ -185,8 +186,7 @@ def visualize_scenarios
elsif File.exist?(File.join(scenario_folder, 'default_scenario_report.csv'))
scenario_folders << File.join(scenario_folder, 'default_scenario_report.csv')
scenario_report_exists = true
elsif
puts "\nERROR: Default reports not created for #{scenario_folder}. Please use 'process --default' to create default post processing reports for all scenarios first. Visualization not generated for #{scenario_folder}.\n"
elsif puts "\nERROR: Default reports not created for #{scenario_folder}. Please use 'process --default' to create default post processing reports for all scenarios first. Visualization not generated for #{scenario_folder}.\n"
end
end
if scenario_report_exists == true
Expand Down Expand Up @@ -216,7 +216,7 @@ def visualize_features(scenario_file)
scenario_name = File.basename(scenario_file, File.extname(scenario_file))
run_dir = File.join(root_dir, 'run', scenario_name.downcase)
feature_report_exists = false
csv = CSV.read(File.join(root_dir, scenario_file), :headers => true)
csv = CSV.read(File.join(root_dir, scenario_file), headers: true)
feature_names = csv['Feature Name']
feature_folders = []
# loop through building feature ids from scenario csv
Expand All @@ -228,8 +228,7 @@ def visualize_features(scenario_file)
elsif File.exist?(File.join(run_dir, feature, 'feature_reports/default_feature_report.csv'))
feature_report_exists = true
feature_folders << File.join(run_dir, feature, 'feature_reports/default_feature_report.csv')
elsif
puts "\nERROR: Default reports not created for #{feature}. Please use 'process --default' to create default post processing reports for all features first. Visualization not generated for #{feature}.\n"
elsif puts "\nERROR: Default reports not created for #{feature}. Please use 'process --default' to create default post processing reports for all features first. Visualization not generated for #{feature}.\n"
end
end
if feature_report_exists == true
Expand Down Expand Up @@ -492,24 +491,23 @@ task :post_process_reopt, [:json, :csv] do |t, args|
groundmount_photovoltaic = {}
feature_file = JSON.parse(File.read(File.join(root_dir, json)), symbolize_names: true)
feature_file[:features].each do |feature|
begin
# Add community photovoltaic if present in the Feature File
if feature[:properties][:district_system_type] == 'Community Photovoltaic'
community_photovoltaic << feature
# Add groundmount photovoltaic if present in the Feature File
elsif feature[:properties][:district_system_type] == 'Ground Mount Photovoltaic'
groundmount_photovoltaic[feature[:properties][:associated_building_id]] = feature[:properties][:footprint_area]
end
rescue
# Add community photovoltaic if present in the Feature File
case feature[:properties][:district_system_type]
when 'Community Photovoltaic'
community_photovoltaic << feature
# Add groundmount photovoltaic if present in the Feature File
when 'Ground Mount Photovoltaic'
groundmount_photovoltaic[feature[:properties][:associated_building_id]] = feature[:properties][:footprint_area]
end
rescue StandardError
end

# Run Aggregate Scenario
scenario_report_scenario = reopt_post_processor.run_scenario_report(scenario_report: scenario_report, save_name: 'scenario_report_reopt_global_optimization', run_resilience: true, community_photovoltaic: community_photovoltaic)

# Run features individually - this is an alternative approach to the previous step, in your analysis depending on project ojectives you maye only need to run one
scenario_report_features = reopt_post_processor.run_scenario_report_features(scenario_report: scenario_report, save_names_feature_reports: ['feature_report_reopt']* scenario_report.feature_reports.length, save_name_scenario_report: 'scenario_report_reopt_local_optimization', run_resilience: true,
keep_existing_output: false, groundmount_photovoltaic: groundmount_photovoltaic)
scenario_report_features = reopt_post_processor.run_scenario_report_features(scenario_report: scenario_report, save_names_feature_reports: ['feature_report_reopt'] * scenario_report.feature_reports.length, save_name_scenario_report: 'scenario_report_reopt_local_optimization', run_resilience: true,
keep_existing_output: false, groundmount_photovoltaic: groundmount_photovoltaic)
end

### Mixed
Expand Down Expand Up @@ -582,40 +580,40 @@ end
### All

desc 'Clear all scenarios'
task :clear_all => [:clear_baseline, :clear_high_efficiency, :clear_thermal_storage, :clear_flexible_hot_water, :clear_reopt, :clear_mixed] do
task clear_all: [:clear_baseline, :clear_high_efficiency, :clear_thermal_storage, :clear_flexible_hot_water, :clear_reopt, :clear_mixed] do
# clear all the scenarios
end

desc 'Run all scenarios'
task :run_all => [:run_baseline, :run_high_efficiency, :run_thermal_storage, :run_flexible_hot_water, :run_reopt, :run_mixed] do
task run_all: [:run_baseline, :run_high_efficiency, :run_thermal_storage, :run_flexible_hot_water, :run_reopt, :run_mixed] do
# run all the scenarios
end

desc 'Post process all scenarios'
task :post_process_all => [:post_process_baseline, :post_process_high_efficiency, :post_process_thermal_storage, :post_process_flexible_hot_water, :post_process_reopt, :post_process_mixed] do
task post_process_all: [:post_process_baseline, :post_process_high_efficiency, :post_process_thermal_storage, :post_process_flexible_hot_water, :post_process_reopt, :post_process_mixed] do
# post_process all the scenarios
end

desc 'Visualize all scenarios'
task :visualize_all do
# visualize all features within each scenario, then visualize across all scenarios
Rake::Task['visualize_features'].invoke("baseline_scenario.csv")
Rake::Task["visualize_features"].reenable
Rake::Task["visualize_features"].invoke("high_efficiency_scenario.csv")
Rake::Task["visualize_features"].reenable
Rake::Task["visualize_features"].invoke("thermal_storage_scenario.csv")
Rake::Task["visualize_features"].reenable
Rake::Task["visualize_features"].invoke("flexible_hot_water_scenario.csv")
Rake::Task["visualize_features"].reenable
Rake::Task["visualize_features"].invoke("reopt_scenario.csv")
Rake::Task["visualize_features"].reenable
Rake::Task["visualize_features"].invoke("mixed_scenario.csv")
Rake::Task["visualize_scenarios"].invoke()
Rake::Task['visualize_features'].invoke('baseline_scenario.csv')
Rake::Task['visualize_features'].reenable
Rake::Task['visualize_features'].invoke('high_efficiency_scenario.csv')
Rake::Task['visualize_features'].reenable
Rake::Task['visualize_features'].invoke('thermal_storage_scenario.csv')
Rake::Task['visualize_features'].reenable
Rake::Task['visualize_features'].invoke('flexible_hot_water_scenario.csv')
Rake::Task['visualize_features'].reenable
Rake::Task['visualize_features'].invoke('reopt_scenario.csv')
Rake::Task['visualize_features'].reenable
Rake::Task['visualize_features'].invoke('mixed_scenario.csv')
Rake::Task['visualize_scenarios'].invoke
end

desc 'Run and post process all scenarios'
task :update_all => [:run_all, :post_process_all, :visualize_all] do
task update_all: [:run_all, :post_process_all, :visualize_all] do
# run and post_process all the scenarios
end

task :default => :update_all
task default: :update_all
22 changes: 11 additions & 11 deletions example_project/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ allow_local = ENV['FAVOR_LOCAL_GEMS']
#

# TODO : Uncomment to revert changes once branch is merged
#if allow_local && File.exist?('../openstudio-common-measures-gem')
# if allow_local && File.exist?('../openstudio-common-measures-gem')
# gem 'openstudio-common-measures', path: '../openstudio-common-measures-gem'
#elsif allow_local
gem 'openstudio-common-measures', github: 'NREL/openstudio-common-measures-gem', branch: 'ev_enhancement'
#else
# elsif allow_local
gem 'openstudio-common-measures', github: 'NREL/openstudio-common-measures-gem', branch: 'develop'
# else
# gem 'openstudio-common-measures', '~> 0.5.0'
#end
# end

if allow_local && File.exist?('../openstudio-model-articulation-gem')
gem 'openstudio-model-articulation', path: '../openstudio-model-articulation-gem'
Expand Down Expand Up @@ -68,18 +68,18 @@ else
end

# TODO : Uncomment to revert changes once gem is released
#if allow_local && File.exists?('../urbanopt-geojson-gem')
# if allow_local && File.exists?('../urbanopt-geojson-gem')
# gem 'urbanopt-geojson', path: '../urbanopt-geojson-gem'
#elsif allow_local
gem 'urbanopt-geojson', github: 'URBANopt/urbanopt-geojson-gem', branch: 'develop'
#else
# elsif allow_local
gem 'urbanopt-geojson', github: 'URBANopt/urbanopt-geojson-gem', branch: 'develop'
# else
# gem 'urbanopt-geojson', '~> 0.7.0'
#end
# end

# NEVER put SCENARIO-GEM in this file...it will make all simulations fail due to the sqlite dependency
# gem 'urbanopt-scenario', github: 'URBANopt/urbanopt-scenario-gem', branch: 'develop'

if allow_local && File.exists?('../urbanopt-reporting-gem')
if allow_local && File.exist?('../urbanopt-reporting-gem')
gem 'urbanopt-reporting', path: '../urbanopt-reporting-gem'
elsif allow_local
gem 'urbanopt-reporting', github: 'URBANopt/urbanopt-reporting-gem', branch: 'develop'
Expand Down
Loading

0 comments on commit 84b2a16

Please sign in to comment.