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

Add python sampler for residential #308

Open
wants to merge 25 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
65 changes: 13 additions & 52 deletions buildstockbatch/sampler/residential_quota.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
:copyright: (c) 2020 by The Alliance for Sustainable Energy
:license: BSD-3
"""
import docker
import logging
import os
import pathlib
import shutil
import subprocess
import sys
import time
import pathlib

from .base import BuildStockSampler
from .downselect import DownselectSamplerBase
from .residential_sampler import sample_all
from buildstockbatch.exc import ValidationError

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -53,59 +53,20 @@ def validate_args(cls, project_filename, **kw):
raise ValidationError('The following sampler arguments are required: ' + ', '.join(expected_args))
return True

def _run_sampling_docker(self):
docker_client = docker.DockerClient.from_env()
def _run_sampling(self):
tick = time.time()
extra_kws = {}
if sys.platform.startswith('linux'):
extra_kws['user'] = f'{os.getuid()}:{os.getgid()}'
container_output = docker_client.containers.run(
self.parent().docker_image,
[
'ruby',
'resources/run_sampling.rb',
'-p', self.cfg['project_directory'],
'-n', str(self.n_datapoints),
'-o', 'buildstock.csv'
],
remove=True,
volumes={
self.buildstock_dir: {'bind': '/var/simdata/openstudio', 'mode': 'rw'}
},
name='buildstock_sampling',
**extra_kws
)
tick = time.time() - tick
for line in container_output.decode('utf-8').split('\n'):
logger.debug(line)
logger.debug('Sampling took {:.1f} seconds'.format(tick))
destination_filename = self.csv_path
if os.path.exists(destination_filename):
os.remove(destination_filename)
shutil.move(
os.path.join(self.buildstock_dir, 'resources', 'buildstock.csv'),
destination_filename
)
return destination_filename
buildstock_directory = pathlib.Path(self.cfg['buildstock_directory'])
sample_df = sample_all(buildstock_directory / self.cfg['project_directory'], self.n_datapoints)
print(self.csv_path)
sample_df.to_csv(self.csv_path, index=False)
logger.debug('Sampling took {:.1f} seconds'.format(time.time() - tick))
return self.csv_path

def _run_sampling_docker(self):
return self._run_sampling()

def _run_sampling_singularity(self):
args = [
'singularity',
'exec',
'--contain',
'--home', '{}:/buildstock'.format(self.buildstock_dir),
'--bind', '{}:/outbind'.format(os.path.dirname(self.csv_path)),
self.parent().singularity_image,
'ruby',
'resources/run_sampling.rb',
'-p', self.cfg['project_directory'],
'-n', str(self.n_datapoints),
'-o', '../../outbind/{}'.format(os.path.basename(self.csv_path))
]
logger.debug(f"Starting singularity sampling with command: {' '.join(args)}")
subprocess.run(args, check=True, env=os.environ, cwd=self.parent().output_dir)
logger.debug("Singularity sampling completed.")
return self.csv_path
return self._run_sampling()

def _run_sampling_local_openstudio(self):
subprocess.run(
Expand Down
2 changes: 2 additions & 0 deletions buildstockbatch/sampler/residential_sampler/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from .sampler import sample_all, verify # noqa F041
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Option=1 Option=2 Option=3 Option=4 Option=5 sampling_probability
0.2 0.2 0.2 0.2 0.2 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Dependency=Bedrooms Option=None Option=Standard Option=Premium sampling_probability
1 0.4 0.4 0.2 0.2
2 0.4 0.4 0.2 0.2
3 0.4 0.4 0.2 0.2
4 0.4 0.4 0.2 0.2
5 0.4 0.4 0.2 0.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Dependency=Ceiling Fan Option=Yes Option=No sampling_probability
None 0.9 0.1 0.4
Standard 0.8 0.2 0.4
Premium 0.1 0.9 0.2
Loading
Loading