-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5028 from NREL/5019-result_json
C++ CLI: Create results.json and handle analysis.json (create objectives.json)
- Loading branch information
Showing
22 changed files
with
688 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Just for convenience: having to VT the seb model which is at 1.11.5 | ||
# can be very time consuming, especially on debug builds | ||
|
||
require 'openstudio' | ||
|
||
model_path = File.join(__dir__, 'files/seb.osm') | ||
raise "#{model_path} not found" if !File.file?(model_path) | ||
|
||
# Starting at 3.7.0, no need to explicitly call vt, but still doing it | ||
# m = OpenStudio::Model::Model::load(model_path).get() | ||
vt = OpenStudio::OSVersion::VersionTranslator.new | ||
OpenStudio::Logger.instance.standardOutLogger.setLogLevel(OpenStudio::Debug) | ||
m = vt.loadModel(model_path).get() | ||
m.save(model_path, true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"analysis": { | ||
"display_name": "Test With Analysis JSON", | ||
"name": "analysis_json", | ||
"output_variables": [ | ||
{ | ||
"objective_function": true, | ||
"name": "FakeReport.net_site_energy", | ||
"objective_function_index": 0, | ||
"objective_function_target": 0, | ||
"objective_function_group": 1, | ||
"scaling_factor": 1.0, | ||
"display_name": "Net Site Energy, should be there", | ||
"display_name_short": "net_site_energy", | ||
"metadata_id": null, | ||
"visualize": true, | ||
"export": true, | ||
"variable_type": "double" | ||
}, | ||
{ | ||
"objective_function": false, | ||
"name": "FakeReport.net_site_energy", | ||
"objective_function_index": 1, | ||
"objective_function_target": 0, | ||
"objective_function_group": 1, | ||
"scaling_factor": 1.0, | ||
"display_name": "net_site_energy", | ||
"display_name_short": "net_site_energy", | ||
"metadata_id": null, | ||
"visualize": true, | ||
"export": true, | ||
"variable_type": "double" | ||
}, | ||
{ | ||
"objective_function": true, | ||
"name": "IsNonExisting.NonExisting", | ||
"objective_function_index": 2, | ||
"objective_function_target": 0, | ||
"objective_function_group": 1, | ||
"scaling_factor": 1.0, | ||
"display_name": "net_site_energy", | ||
"display_name_short": "net_site_energy", | ||
"metadata_id": null, | ||
"visualize": true, | ||
"export": true, | ||
"variable_type": "double" | ||
} | ||
] | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
resources/Examples/with_analysis/local/measures/FakeReport/measure.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'openstudio' | ||
|
||
class FakeReport < OpenStudio::Measure::ReportingMeasure | ||
|
||
def name | ||
return "Fake Report" | ||
end | ||
|
||
#define the arguments that the user will input | ||
def arguments(model = nil) | ||
args = OpenStudio::Measure::OSArgumentVector.new | ||
|
||
return args | ||
end #end the arguments method | ||
|
||
def outputs | ||
result = OpenStudio::Measure::OSOutputVector.new | ||
result << OpenStudio::Measure::OSOutput.makeDoubleOutput('net_site_energy', false) | ||
return result | ||
end | ||
|
||
#define what happens when the measure is run | ||
def run(runner, user_arguments) | ||
super(runner, user_arguments) | ||
|
||
#use the built-in error checking | ||
if not runner.validateUserArguments(arguments(), user_arguments) | ||
return false | ||
end | ||
|
||
# Register some constant values, so we can only do it during post | ||
# processing | ||
runner.registerValue("net_site_energy", "Net Site Energy", 167.1, "GJ") | ||
runner.registerValue("something!with.invalid_chars_", "Test Sanitizing", 1, "") | ||
runner.registerFinalCondition("Goodbye.") | ||
|
||
return true | ||
|
||
end | ||
|
||
end | ||
|
||
FakeReport.new.registerWithApplication |
57 changes: 57 additions & 0 deletions
57
resources/Examples/with_analysis/local/measures/FakeReport/measure.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?xml version="1.0"?> | ||
<measure> | ||
<schema_version>3.1</schema_version> | ||
<name>fake_report</name> | ||
<uid>ca6ba362-ea57-4236-b803-17e37b0c0817</uid> | ||
<version_id>32617b1d-91b1-4325-9a3a-0708b9853d29</version_id> | ||
<version_modified>2023-11-13T17:08:41Z</version_modified> | ||
<xml_checksum>B2AD275E</xml_checksum> | ||
<class_name>FakeReport</class_name> | ||
<display_name>Fake Report</display_name> | ||
<description>Change me</description> | ||
<modeler_description>Change me</modeler_description> | ||
<arguments /> | ||
<outputs> | ||
<output> | ||
<name>net_site_energy</name> | ||
<display_name>net_site_energy</display_name> | ||
<short_name>net_site_energy</short_name> | ||
<type>Double</type> | ||
<model_dependent>false</model_dependent> | ||
</output> | ||
</outputs> | ||
<provenances /> | ||
<tags> | ||
<tag>Reporting.QAQC</tag> | ||
</tags> | ||
<attributes> | ||
<attribute> | ||
<name>Measure Type</name> | ||
<value>ReportingMeasure</value> | ||
<datatype>string</datatype> | ||
</attribute> | ||
<attribute> | ||
<name>Measure Language</name> | ||
<value>Ruby</value> | ||
<datatype>string</datatype> | ||
</attribute> | ||
<attribute> | ||
<name>Uses SketchUp API</name> | ||
<value>false</value> | ||
<datatype>boolean</datatype> | ||
</attribute> | ||
</attributes> | ||
<files> | ||
<file> | ||
<version> | ||
<software_program>OpenStudio</software_program> | ||
<identifier>1.1.2</identifier> | ||
<min_compatible>1.1.2</min_compatible> | ||
</version> | ||
<filename>measure.rb</filename> | ||
<filetype>rb</filetype> | ||
<usage_type>script</usage_type> | ||
<checksum>4CFB5158</checksum> | ||
</file> | ||
</files> | ||
</measure> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"steps": [ | ||
{ | ||
"measure_dir_name": "FakeReport", | ||
"arguments": {} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import json | ||
import subprocess | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"is_labs", | ||
[pytest.param(True, id="labs"), pytest.param(False, id="classic")], | ||
) | ||
def test_run_with_analysis(osclipath, is_labs: bool): | ||
base_osw_path = Path("with_analysis.osw").resolve() | ||
assert base_osw_path.is_file(), f"{base_osw_path=} is not found" | ||
|
||
osw = json.loads(base_osw_path.read_text()) | ||
suffix = "labs" if is_labs else "classic" | ||
osw_path = base_osw_path.parent / f"with_analysis_{suffix}.osw" | ||
runDir = base_osw_path.parent / f"run_{suffix}" | ||
osw["run_directory"] = str(runDir) | ||
runDir.mkdir(exist_ok=True) | ||
with open(osw_path, "w") as f: | ||
json.dump(osw, fp=f, indent=2, sort_keys=True) | ||
|
||
# Fake having an in.idf or it won't run in the "classic" subcommand, doing it for labs too so that it's less | ||
# confusing | ||
# if not is_labs: | ||
with open(runDir / "in.idf", "w") as f: | ||
f.write("Building,;") | ||
|
||
command = [str(osclipath)] | ||
if not is_labs: | ||
command.append("classic") | ||
command += ["run", "--postprocess_only", "-w", str(osw_path)] | ||
lines = subprocess.check_output(command, encoding="utf-8").splitlines() | ||
|
||
assert runDir.exists() | ||
measure_attributes_path = runDir / "measure_attributes.json" | ||
assert measure_attributes_path.is_file() | ||
results_path = runDir / "results.json" | ||
assert results_path.is_file() | ||
objectives_path = runDir / "objectives.json" | ||
assert objectives_path.is_file() | ||
|
||
measure_attributes = json.loads(measure_attributes_path.read_text()) | ||
assert measure_attributes == { | ||
"FakeReport": {"applicable": True, "net_site_energy": 167.1, "something_with_invalid_chars": 1} | ||
} | ||
|
||
results = json.loads(results_path.read_text()) | ||
assert results == {"FakeReport": {"applicable": True, "net_site_energy": 167.1, "something_with_invalid_chars": 1}} | ||
|
||
objectives = json.loads(objectives_path.read_text()) | ||
assert objectives == { | ||
"objective_function_1": 167.1, | ||
"objective_function_3": 1.7976931348623157e308, | ||
"objective_function_group_1": 1.0, | ||
"objective_function_group_3": None, | ||
"objective_function_target_1": 0.0, | ||
"objective_function_target_3": None, | ||
"scaling_factor_1": 1.0, | ||
"scaling_factor_3": None, | ||
} | ||
|
||
data_point_out_path = runDir / "data_point_out.json" | ||
assert data_point_out_path.is_file() | ||
data_point_out = json.loads(data_point_out_path.read_text()) | ||
assert data_point_out == { | ||
"FakeReport": {"applicable": True, "net_site_energy": 167.1, "something_with_invalid_chars": 1} | ||
} | ||
|
||
expected_files_in_run_dir = { | ||
"data_point.zip", | ||
"finished.job", | ||
"in.idf", | ||
"measure_attributes.json", | ||
"objectives.json", | ||
"results.json", | ||
"run.log", | ||
"started.job", | ||
"data_point_out.json", | ||
} | ||
|
||
assert set([x.name for x in runDir.glob("*")]) == expected_files_in_run_dir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.