Skip to content

Commit

Permalink
Fix the ability to skip baseline sims in Docker-based implementations (
Browse files Browse the repository at this point in the history
  • Loading branch information
lathanh authored Jan 11, 2024
1 parent 8e03769 commit 7ef4514
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
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:
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
24 changes: 24 additions & 0 deletions buildstockbatch/test/test_docker_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import os
import pathlib
import pytest
import shutil
import tarfile
import tempfile
Expand All @@ -18,6 +19,29 @@
resources_dir = os.path.join(here, "test_inputs", "test_openstudio_buildstock", "resources")


@docker_available
@pytest.mark.parametrize("skip_baseline_sims,expected", [(False, 10), (True, 5)])
def test_skip_baseline_sims(basic_residential_project_file, mocker, skip_baseline_sims, expected):
"""Test "skip_sims" baseline configuration's effect on ``n_sims``"""
update_args = {"baseline": {"skip_sims": True}} if skip_baseline_sims else {}
project_filename, results_dir = basic_residential_project_file(update_args)

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 == expected


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

0 comments on commit 7ef4514

Please sign in to comment.