Skip to content

Commit

Permalink
Merge pull request #254 from NREL/mdahlhau/report_measure_rubocop
Browse files Browse the repository at this point in the history
Rubocop fixes to reporting measures
  • Loading branch information
mdahlhausen authored Nov 12, 2024
2 parents 25d4c59 + 00bc03e commit 4d7baa7
Show file tree
Hide file tree
Showing 38 changed files with 1,768 additions and 38,452 deletions.
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Author pull request checklist:
- [ ] Changes reflected in example `.yml` files
- [ ] Changes reflected in `README.md` files
- [ ] Added 'See ComStock License' language to first two lines of each code file
- [ ] Implements corresponding measure tests and indexing path in `test/measure_tests.txt` or/and `test/resource_measure_tests.txt`
- [ ] Implements corresponding measure tests and indexing path in `test/reporting_measure_tests.txt`, `test/workflow_measure_tests.txt`, or `test/upgrade_measure_tests.txt`
- [ ] All new and existing tests pass the CI

### Review Checklist
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require 'rubocop/rake_task'
desc 'Run measure tests'
namespace :unit_tests do
desc 'Run all measure tests'
task :all_tests => [:measure_tests, :workflow_measure_tests, :upgrade_measure_tests] do
task all_tests: [:reporting_measure_tests, :workflow_measure_tests, :upgrade_measure_tests] do
puts 'Running all measure tests:'
end

Expand Down
196 changes: 91 additions & 105 deletions measures/ApplyUpgrade/measure.rb

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions measures/ApplyUpgrade/measure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<schema_version>3.1</schema_version>
<name>apply_upgrade</name>
<uid>33f1654c-f734-43d1-b35d-9d2856e41b5a</uid>
<version_id>f1b876c8-3df9-47d9-8924-f1d82ff2fed3</version_id>
<version_modified>2024-05-13T17:34:09Z</version_modified>
<version_id>8cfbda76-bb88-4453-8ead-e69831e3c246</version_id>
<version_modified>2024-10-28T20:45:30Z</version_modified>
<xml_checksum>9339BE01</xml_checksum>
<class_name>ApplyUpgrade</class_name>
<display_name>Apply Upgrade</display_name>
Expand Down Expand Up @@ -37105,7 +37105,7 @@
<filename>measure.rb</filename>
<filetype>rb</filetype>
<usage_type>script</usage_type>
<checksum>781E3A4E</checksum>
<checksum>B21C73C0</checksum>
</file>
</files>
</measure>
95 changes: 42 additions & 53 deletions measures/BuildExistingModel/measure.rb
Original file line number Diff line number Diff line change
@@ -1,80 +1,73 @@
# ComStock™, Copyright (c) 2023 Alliance for Sustainable Energy, LLC. All rights reserved.
# See top level LICENSE.txt file for license terms.

# see the URL below for information on how to write OpenStudio measures
# http://nrel.github.io/OpenStudio-user-documentation/measures/measure_writing_guide/

require 'csv'
require 'openstudio'
require 'json'

# start the measure
class BuildExistingModel < OpenStudio::Measure::ModelMeasure

# human readable name
def name
return "Build Existing Model"
'Build Existing Model'
end

# human readable description
def description
return "Builds the OpenStudio Model for an existing building."
'Builds the OpenStudio Model for an existing building.'
end

# human readable description of modeling approach
def modeler_description
return "Builds the OpenStudio Model using the sampling csv file, which contains the specified parameters for each existing building. Based on the supplied building number, those parameters are used to run the OpenStudio measures with appropriate arguments and build up the OpenStudio model."
'Builds the OpenStudio Model using the sampling csv file, which contains the specified parameters for each existing building. Based on the supplied building number, those parameters are used to run the OpenStudio measures with appropriate arguments and build up the OpenStudio model.'
end

# define the arguments that the user will input
def arguments(model)
def arguments(_model)
args = OpenStudio::Measure::OSArgumentVector.new

building_id = OpenStudio::Ruleset::OSArgument.makeIntegerArgument("building_id", true)
building_id.setDisplayName("Building ID")
building_id.setDescription("The building number (between 1 and the number of samples).")
building_id = OpenStudio::Ruleset::OSArgument.makeIntegerArgument('building_id', true)
building_id.setDisplayName('Building ID')
building_id.setDescription('The building number (between 1 and the number of samples).')
args << building_id

number_of_buildings_represented = OpenStudio::Ruleset::OSArgument.makeIntegerArgument("number_of_buildings_represented", false)
number_of_buildings_represented.setDisplayName("Number of Buildings Represented")
number_of_buildings_represented.setDescription("The total number of buildings represented by the existing building models.")
number_of_buildings_represented = OpenStudio::Ruleset::OSArgument.makeIntegerArgument(
'number_of_buildings_represented', false
)
number_of_buildings_represented.setDisplayName('Number of Buildings Represented')
number_of_buildings_represented.setDescription('The total number of buildings represented by the existing building models.')
args << number_of_buildings_represented

workflow_json = OpenStudio::Ruleset::OSArgument.makeStringArgument("workflow_json", false)
workflow_json.setDisplayName("Workflow JSON")
workflow_json.setDescription("The name of the JSON file (in the resources dir) that dictates the order in which measures are to be run. If not provided, the order specified in resources/options_lookup.tsv will be used.")
workflow_json = OpenStudio::Ruleset::OSArgument.makeStringArgument('workflow_json', false)
workflow_json.setDisplayName('Workflow JSON')
workflow_json.setDescription('The name of the JSON file (in the resources dir) that dictates the order in which measures are to be run. If not provided, the order specified in resources/options_lookup.tsv will be used.')
args << workflow_json

return args
args
end

# define what happens when the measure is run
def run(model, runner, user_arguments)
super(model, runner, user_arguments)

# use the built-in error checking
if !runner.validateUserArguments(arguments(model), user_arguments)
return false
end
return false unless runner.validateUserArguments(arguments(model), user_arguments)

building_id = runner.getIntegerArgumentValue("building_id",user_arguments)
number_of_buildings_represented = runner.getOptionalIntegerArgumentValue("number_of_buildings_represented",user_arguments)
workflow_json = runner.getOptionalStringArgumentValue("workflow_json", user_arguments)
building_id = runner.getIntegerArgumentValue('building_id', user_arguments)
number_of_buildings_represented = runner.getOptionalIntegerArgumentValue('number_of_buildings_represented',
user_arguments)
workflow_json = runner.getOptionalStringArgumentValue('workflow_json', user_arguments)

# Get file/dir paths
runner.registerInfo "Running in worker container `#{ENV['HOSTNAME']}`"
resources_dir = File.absolute_path(File.join(File.dirname(__FILE__), "..", "..", "lib", "resources")) # Should have been uploaded per 'Additional Analysis Files' in PAT
characteristics_dir = File.absolute_path(File.join(File.dirname(__FILE__), "..", "..", "lib", "housing_characteristics")) # Should have been uploaded per 'Additional Analysis Files' in PAT
buildstock_file = File.join(resources_dir, "buildstock.rb")
measures_dirs = [File.join(resources_dir, "measures")]
lookup_file = File.join(resources_dir, "options_lookup.tsv")
resources_dir = File.absolute_path(File.join(File.dirname(__FILE__), '..', '..', 'lib', 'resources')) # Should have been uploaded per 'Additional Analysis Files' in PAT
characteristics_dir = File.absolute_path(File.join(File.dirname(__FILE__), '..', '..', 'lib', 'housing_characteristics')) # Should have been uploaded per 'Additional Analysis Files' in PAT
buildstock_file = File.join(resources_dir, 'buildstock.rb')
measures_dirs = [File.join(resources_dir, 'measures')]
lookup_file = File.join(resources_dir, 'options_lookup.tsv')
# FIXME: Temporary
buildstock_csv = File.absolute_path(File.join(characteristics_dir, "buildstock.csv")) # Should have been generated by the Worker Initialization Script (run_sampling.rb) or provided by the project
if workflow_json.is_initialized
workflow_json = File.join(resources_dir, workflow_json.get)
else
workflow_json = nil
end
buildstock_csv = File.absolute_path(File.join(characteristics_dir, 'buildstock.csv')) # Should have been generated by the Worker Initialization Script (run_sampling.rb) or provided by the project
workflow_json = (File.join(resources_dir, workflow_json.get) if workflow_json.is_initialized)

# Load buildstock file
require File.join(File.dirname(buildstock_file), File.basename(buildstock_file, File.extname(buildstock_file)))
Expand Down Expand Up @@ -127,37 +120,33 @@ def run(model, runner, user_arguments)
print_option_assignment(parameter_name, option_name, runner)
options_measure_args = get_measure_args_from_option_names(lookup_file, [option_name], parameter_name, runner)
options_measure_args[option_name].each do |measure_subdir, args_hash|
update_args_hash(measures, measure_subdir, args_hash, add_new = false)
update_args_hash(measures, measure_subdir, args_hash, false)
end
end

if not apply_measures(measures_dirs, measures, runner, model, workflow_json, "measures.osw", true)
return false
end
return false unless apply_measures(measures_dirs, measures, runner, model, workflow_json, 'measures.osw', true)

# Determine weight
if not number_of_buildings_represented.nil?
total_samples = 100 # Temporary
#total_samples = runner.analysis[:analysis][:problem][:algorithm][:number_of_samples].to_f
weight = number_of_buildings_represented.get / total_samples
register_value(runner, "weight", weight.to_s)
unless number_of_buildings_represented.nil?
total_samples = 100 # Temporary
# total_samples = runner.analysis[:analysis][:problem][:algorithm][:number_of_samples].to_f
weight = number_of_buildings_represented.get / total_samples
register_value(runner, 'weight', weight.to_s)
end

return true

true
end

def get_data_for_sample(buildstock_csv, building_id, runner)
CSV.foreach(buildstock_csv, headers:true) do |sample|
next if sample[0].to_i != building_id
return sample
CSV.foreach(buildstock_csv, headers: true) do |sample|
next if sample[0].to_i != building_id

return sample
end
# If we got this far, couldn't find the sample #
msg = "Could not find row for #{building_id.to_s} in #{File.basename(buildstock_csv).to_s}."
msg = "Could not find row for #{building_id} in #{File.basename(buildstock_csv)}."
runner.registerError(msg)
fail msg
raise msg
end

end

# register the measure to be used by the application
Expand Down
6 changes: 3 additions & 3 deletions measures/BuildExistingModel/measure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<schema_version>3.1</schema_version>
<name>build_existing_model</name>
<uid>943cde68-f4bf-4e2e-a984-ab70cba2669c</uid>
<version_id>4d6f7baa-1569-473a-83a7-e79947caf559</version_id>
<version_modified>2024-05-13T17:34:09Z</version_modified>
<version_id>841bac7c-1207-4cf6-9e1b-446dc3c8b7e6</version_id>
<version_modified>2024-10-28T20:47:10Z</version_modified>
<xml_checksum>9339BE01</xml_checksum>
<class_name>BuildExistingModel</class_name>
<display_name>Build Existing Model</display_name>
Expand Down Expand Up @@ -103,7 +103,7 @@
<filename>measure.rb</filename>
<filetype>rb</filetype>
<usage_type>script</usage_type>
<checksum>7E9E2913</checksum>
<checksum>9B94B178</checksum>
</file>
</files>
</measure>
Loading

0 comments on commit 4d7baa7

Please sign in to comment.