Skip to content

Commit 311f8fc

Browse files
Merge pull request #68 from hubverse-org/mc/pass-through-initial-xaxis-range/48
implements [add a pass-through initial_xaxis_range option to predtimechart-config.yml files]
2 parents d71edd6 + d6ee4a4 commit 311f8fc

File tree

6 files changed

+99
-9
lines changed

6 files changed

+99
-9
lines changed

src/hub_predtimechart/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.1"
1+
__version__ = "2.2.0"

src/hub_predtimechart/generate_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ def get_max_ref_date_or_first_config_ref_date(reference_dates):
7878
options['disclaimer'] = hub_config.disclaimer
7979

8080
# set `initial_xaxis_range`
81-
options['initial_xaxis_range'] = None
81+
options['initial_xaxis_range'] = hub_config.initial_xaxis_range if hub_config.initial_xaxis_range else None
8282

8383
return options

src/hub_predtimechart/hub_config_ptc.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pandas as pd
88
import polars as pl
99
import yaml
10-
from jsonschema import ValidationError, validate
10+
from jsonschema import ValidationError, validate, FormatChecker
1111

1212
from hub_predtimechart.hub_config import HubConfig
1313
from hub_predtimechart.ptc_schema import ptc_config_schema
@@ -23,6 +23,7 @@ class HubConfigPtc(HubConfig):
2323
- horizon_col_name: ""
2424
- initial_checked_models: ""
2525
- disclaimer: ""
26+
- initial_xaxis_range: "", or None if not passed
2627
- task_id_text: "". optional (None if not passed). keys: task_ids, values: value-to-text dict. ex:
2728
{'location': {'US': 'United States', '01': 'Alabama', ..., '78': 'Virgin Islands'}, ...}
2829
- target_data_file_name: either None (if the hub implements our new time-series target data standard) which means to
@@ -60,8 +61,9 @@ def __init__(self, hub_dir: Path, ptc_config_file: Path):
6061
self.horizon_col_name: str = ptc_config['horizon_col_name']
6162
self.initial_checked_models: str = ptc_config['initial_checked_models']
6263
self.disclaimer: str | None = ptc_config.get('disclaimer') # optional
63-
self.task_id_text: dict | None = ptc_config.get('task_id_text') # optional
64-
self.target_data_file_name: str | None = ptc_config.get('target_data_file_name') # optional
64+
self.initial_xaxis_range: str | None = ptc_config.get('initial_xaxis_range') # ""
65+
self.task_id_text: dict | None = ptc_config.get('task_id_text') # ""
66+
self.target_data_file_name: str | None = ptc_config.get('target_data_file_name') # ""
6567

6668
# set model_id_to_metadata
6769
self.model_id_to_metadata: dict[str, dict] = {}
@@ -129,8 +131,9 @@ def _validate_predtimechart_config(ptc_config: dict, tasks: dict):
129131
:param tasks: dict loaded from a 'tasks.json' file
130132
:raises ValidationError: if `ptc_config` is not valid
131133
"""
132-
# validate against the JSON Schema and then do additional validations
133-
validate(ptc_config, ptc_config_schema)
134+
# validate against the JSON Schema and then do additional validations. enable format checking for
135+
# `initial_xaxis_range` `date` format
136+
validate(ptc_config, ptc_config_schema, format_checker=FormatChecker())
134137

135138
# validate: `rounds_idx`
136139
try:
@@ -167,7 +170,8 @@ def _validate_hub_ptc_compatibility(hub_config_ptc: HubConfigPtc):
167170
raise ValidationError(f"'designated_model' not found in model metadata schema's 'required' section")
168171

169172
# validate: all model_task entries have the same task_ids. frozenset lets us make a set of sets
170-
task_ids_all_model_tasks = [frozenset(model_task.task['task_ids'].keys()) for model_task in hub_config_ptc.model_tasks]
173+
task_ids_all_model_tasks = [frozenset(model_task.task['task_ids'].keys()) for model_task in
174+
hub_config_ptc.model_tasks]
171175
if len(set(task_ids_all_model_tasks)) != 1:
172176
raise ValidationError(f"not all model_task entries have the same task_ids")
173177

src/hub_predtimechart/ptc_schema.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,18 @@
3535
}
3636
},
3737
"disclaimer": {
38-
"description": "text providing any important information users should know",
38+
"description": "optional text providing any important information users should know",
3939
"type": "string",
4040
"minLength": 1
4141
},
42+
"initial_xaxis_range": {
43+
"description": "`array` of two dates in 'YYYY-MM-DD' format that specify the initial xaxis range to use. To not initialize the range, pass `null` for its value",
44+
"type": ["array", "null"],
45+
"minItems": 2,
46+
"maxItems": 2,
47+
"uniqueItems": True,
48+
"items": {"type": "string", "format": "date"}
49+
},
4250
"task_id_text": {
4351
"description": "optional mapping of predtimechart task id values to text. keys are task_ids and values are an object whose keys are ptc task_id `value`s and values are ptc task_id `text`s",
4452
"type": "object",

tests/hub_predtimechart/test_generate_options.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,13 @@ def test_generate_options_task_id_text_covid19_forecast_hub():
5454
exp_options = json.load(fp)
5555
act_options = ptc_options_for_hub(hub_config)
5656
assert act_options == exp_options
57+
58+
59+
def test_generate_options_initial_xaxis_range_covid19_forecast_hub():
60+
hub_dir = Path('tests/hubs/covid19-forecast-hub')
61+
hub_config = HubConfigPtc(hub_dir, hub_dir / 'hub-config/predtimechart-config-xaxis.yml')
62+
with open('tests/expected/covid19-forecast-hub/predtimechart-options.json') as fp:
63+
exp_options = json.load(fp)
64+
exp_options['initial_xaxis_range'] = hub_config.initial_xaxis_range
65+
act_options = ptc_options_for_hub(hub_config)
66+
assert act_options == exp_options
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
rounds_idx: 0
3+
reference_date_col_name: 'reference_date'
4+
target_date_col_name: 'target_end_date'
5+
horizon_col_name: 'horizon'
6+
initial_checked_models: [ ]
7+
task_id_text:
8+
location: # location,location_name
9+
"US": "United States"
10+
"01": "Alabama"
11+
"02": "Alaska"
12+
"04": "Arizona"
13+
"05": "Arkansas"
14+
"06": "California"
15+
"08": "Colorado"
16+
"09": "Connecticut"
17+
"10": "Delaware"
18+
"11": "District of Columbia"
19+
"12": "Florida"
20+
"13": "Georgia"
21+
"15": "Hawaii"
22+
"16": "Idaho"
23+
"17": "Illinois"
24+
"18": "Indiana"
25+
"19": "Iowa"
26+
"20": "Kansas"
27+
"21": "Kentucky"
28+
"22": "Louisiana"
29+
"23": "Maine"
30+
"24": "Maryland"
31+
"25": "Massachusetts"
32+
"26": "Michigan"
33+
"27": "Minnesota"
34+
"28": "Mississippi"
35+
"29": "Missouri"
36+
"30": "Montana"
37+
"31": "Nebraska"
38+
"32": "Nevada"
39+
"33": "New Hampshire"
40+
"34": "New Jersey"
41+
"35": "New Mexico"
42+
"36": "New York"
43+
"37": "North Carolina"
44+
"38": "North Dakota"
45+
"39": "Ohio"
46+
"40": "Oklahoma"
47+
"41": "Oregon"
48+
"42": "Pennsylvania"
49+
"44": "Rhode Island"
50+
"45": "South Carolina"
51+
"46": "South Dakota"
52+
"47": "Tennessee"
53+
"48": "Texas"
54+
"49": "Utah"
55+
"50": "Vermont"
56+
"51": "Virginia"
57+
"53": "Washington"
58+
"54": "West Virginia"
59+
"55": "Wisconsin"
60+
"56": "Wyoming"
61+
"60": "American Samoa"
62+
"66": "Guam"
63+
"69": "Northern Mariana Islands"
64+
"72": "Puerto Rico"
65+
"74": "U.S. Minor Outlying Islands"
66+
"78": "Virgin Islands"
67+
target_data_file_name: 'covid-hospital-admissions.csv'
68+
initial_xaxis_range: ['2024-10-22', '2025-10-22']

0 commit comments

Comments
 (0)