Skip to content

Commit

Permalink
fix(sample): Add sample run
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkelkp committed Aug 20, 2024
1 parent 424abbf commit 635718b
Show file tree
Hide file tree
Showing 10 changed files with 11,249 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: set up Python
uses: actions/setup-python@v2
with:
python-version: 3.7
python-version: "3.10"
- name: install python dependencies
run: |
pip install -r dev-requirements.txt
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pollination-ladybug==0.2.3
pollination-ladybug-comfort==0.5.3
pollination-honeybee-radiance==0.22.41
pollination-ladybug-comfort==0.5.5
pollination-honeybee-radiance==0.22.60
pollination-honeybee-radiance-postprocess==0.0.65
pollination-honeybee-energy==0.7.3
pollination-lbt-honeybee==0.1.14
Expand Down
2,343 changes: 2,343 additions & 0 deletions samples/artifacts/USA_MA_Boston-Logan.Intl.AP.725090_TMY3.ddy

Large diffs are not rendered by default.

8,768 changes: 8,768 additions & 0 deletions samples/artifacts/USA_MA_Boston-Logan.Intl.AP.725090_TMY3.epw

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions samples/artifacts/clo.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.9
1 change: 1 addition & 0 deletions samples/artifacts/sample_model_grid.hbjson

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions samples/inputs/inputs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"wea": "BRA_PR_Curitiba.838420_INMET.epw",
"model": "sample_model_grid.hbjson",
"radiance-parameters": "-ab 2 -ad 512 -lw 2e-05 -dr 0"
}
1 change: 1 addition & 0 deletions samples/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pollination-io==1.0.3
116 changes: 116 additions & 0 deletions samples/run_jobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
"""Run sample runs on Pollination."""
from pathlib import Path
import json
import datetime
import time
import sys
import os
from requests.exceptions import HTTPError

from pollination_io.api.client import ApiClient
from pollination_io.interactors import NewJob, Recipe
from queenbee.job.job import JobStatusEnum


# get environment variables
api_key = os.environ['QB_POLLINATION_TOKEN']
recipe_tag = os.environ['TAG']
host = os.environ['HOST']

recipe_owner = 'ladybug-tools'
recipe_name = 'pmv-comfort-map'
project_owner = 'ladybug-tools'
project = 'pmv-comfort-map'

api_client = ApiClient(host, api_key)
recipe = Recipe(recipe_owner, recipe_name, recipe_tag, client=api_client)
recipe.add_to_project(f'{project_owner}/{project}')

# load recipe inputs for each sample run
samples_path = Path(__file__).parent.resolve().joinpath('sample_runs.json')
with open(samples_path, encoding='utf-8') as samples_json:
sample_runs = json.load(samples_json)

# create a new job
datetime_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
name = f'Samples (Scheduled by GitHub workflow: {datetime_now})'
new_study = NewJob(project_owner, project, recipe, name=name, client=api_client)

# get all unique artifacts
artifacts = set()
for sample_run in sample_runs:
for recipe_input, value in sample_run['artifacts'].items():
input_path = Path(__file__).parent.resolve().joinpath(value)
assert input_path.exists(), f'{input_path} does not exist.'
artifacts.add(value)

# upload unique artifacts
artifacts_path = {}
for artifact in artifacts:
input_path = Path(__file__).parent.resolve().joinpath(artifact)
artifact_path = new_study.upload_artifact(input_path)
artifacts_path[artifact] = artifact_path

# get recipe inputs for each run and upload artifact
study_inputs = []
for sample_run in sample_runs:
inputs = dict(sample_run['inputs'])
for recipe_input, value in sample_run['artifacts'].items():
inputs[recipe_input] = artifacts_path[value]
study_inputs.append(inputs)

# add the inputs to the study
new_study.arguments = study_inputs

# create the study
running_study = new_study.create()
if host == 'https://api.staging.pollination.cloud':
pollination_url = 'https://app.staging.pollination.cloud'
else:
pollination_url = 'https://app.pollination.cloud'
job_url = f'{pollination_url}/{running_study.owner}/projects/{running_study.project}/jobs/{running_study.id}'
print(job_url)

# wait for 5 seconds
time.sleep(5)

# check status of study
status = running_study.status.status
http_errors = 0
while True:
status_info = running_study.status
print('\t# ------------------ #')
print(f'\t# pending runs: {status_info.runs_pending}')
print(f'\t# running runs: {status_info.runs_running}')
print(f'\t# failed runs: {status_info.runs_failed}')
print(f'\t# completed runs: {status_info.runs_completed}')
if status in [
JobStatusEnum.pre_processing, JobStatusEnum.running, JobStatusEnum.created,
JobStatusEnum.unknown
]:
time.sleep(15)
try:
running_study.refresh()
except HTTPError as e:
status_code = e.response.status_code
print(str(e))
if status_code == 500:
http_errors += 1
if http_errors > 3:
# failed for than 3 times with no success
raise HTTPError(e)
# wait for additional 10 seconds
time.sleep(10)
else:
http_errors = 0
status = status_info.status
else:
# study is finished
time.sleep(2)
break

# return exit status
if status_info.runs_failed != 0:
sys.exit(1)
else:
sys.exit(0)
11 changes: 11 additions & 0 deletions samples/sample_runs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"artifacts": {
"model": "artifacts/sample_model_grid.hbjson",
"epw": "artifacts/USA_MA_Boston-Logan.Intl.AP.725090_TMY3.epw",
"ddy": "artifacts/USA_MA_Boston-Logan.Intl.AP.725090_TMY3.ddy",
"clo-value": "artifacts/clo.csv"
},
"inputs": {}
}
]

0 comments on commit 635718b

Please sign in to comment.