Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the ability to skip baseline sims in Docker-based implementations #52

Merged
merged 4 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions buildstockbatch/cloud/docker_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ def _prep_jobs_for_batch(self, tmppath):
self.validate_buildstock_csv(self.project_filename, df)
building_ids = df.index.tolist()
n_datapoints = len(building_ids)
n_sims = n_datapoints * (len(self.cfg.get("upgrades", [])) + 1)
if self.skip_baseline_sims:
lathanh marked this conversation as resolved.
Show resolved Hide resolved
n_sims = n_datapoints * len(self.cfg.get("upgrades", []))
else:
n_sims = n_datapoints * (len(self.cfg.get("upgrades", [])) + 1)
logger.debug("Total number of simulations = {}".format(n_sims))

# This is the maximum number of jobs that can be in an array
Expand All @@ -327,9 +330,12 @@ def _prep_jobs_for_batch(self, tmppath):
logger.debug("Number of simulations per array job = {}".format(n_sims_per_job))

# Create list of (building ID, upgrade to apply) pairs for all simulations to run.
baseline_sims = zip(building_ids, itertools.repeat(None))
upgrade_sims = itertools.product(building_ids, range(len(self.cfg.get("upgrades", []))))
all_sims = list(itertools.chain(baseline_sims, upgrade_sims))
if not self.skip_baseline_sims:
baseline_sims = zip(building_ids, itertools.repeat(None))
all_sims = list(itertools.chain(baseline_sims, upgrade_sims))
else:
all_sims = list(upgrade_sims)
random.shuffle(all_sims)
all_sims_iter = iter(all_sims)

Expand Down
46 changes: 46 additions & 0 deletions buildstockbatch/test/test_docker_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,52 @@
resources_dir = os.path.join(here, "test_inputs", "test_openstudio_buildstock", "resources")


@docker_available
def test_skip_baseline_sims_false(basic_residential_project_file, mocker):
lathanh marked this conversation as resolved.
Show resolved Hide resolved
"""Test that no "skip_sims" baseline configuration does not skip baseline sims."""
project_filename, results_dir = basic_residential_project_file()

mocker.patch.object(DockerBatchBase, "results_dir", results_dir)
sampler_property_mock = mocker.patch.object(DockerBatchBase, "sampler", new_callable=PropertyMock)
sampler_mock = mocker.MagicMock()
sampler_property_mock.return_value = sampler_mock
# Hard-coded sampling output includes 5 buildings.
sampler_mock.run_sampling = MagicMock(return_value=os.path.join(resources_dir, "buildstock_good.csv"))

dbb = DockerBatchBase(project_filename)
dbb.batch_array_size = 3
DockerBatchBase.validate_project = MagicMock(return_value=True)

with tempfile.TemporaryDirectory(prefix="bsb_") as tmpdir:
_, batch_info = dbb._run_batch_prep(pathlib.Path(tmpdir))

assert batch_info.n_sims == 10


@docker_available
def test_skip_baseline_sims_true(basic_residential_project_file, mocker):
"""Test that ``skip_sims: true`` baseline configuration skips baseline sims."""
project_filename, results_dir = basic_residential_project_file({"baseline": {"skip_sims": True}})

mocker.patch.object(DockerBatchBase, "results_dir", results_dir)
sampler_property_mock = mocker.patch.object(DockerBatchBase, "sampler", new_callable=PropertyMock)
sampler_mock = mocker.MagicMock()
sampler_property_mock.return_value = sampler_mock
# Hard-coded sampling output includes 5 buildings.
sampler_mock.run_sampling = MagicMock(return_value=os.path.join(resources_dir, "buildstock_good.csv"))

dbb = DockerBatchBase(project_filename)
dbb.batch_array_size = 3
DockerBatchBase.validate_project = MagicMock(return_value=True)

print(f"dbb.skip_baseline_sims={dbb.skip_baseline_sims}")

with tempfile.TemporaryDirectory(prefix="bsb_") as tmpdir:
_, batch_info = dbb._run_batch_prep(pathlib.Path(tmpdir))

assert batch_info.n_sims == 5


@docker_available
def test_run_batch_prep(basic_residential_project_file, mocker):
"""Test that samples are created and bundled into batches correctly."""
Expand Down