Skip to content

Commit

Permalink
the code works with time updates
Browse files Browse the repository at this point in the history
  • Loading branch information
AFg6K7h4fhy2 committed Nov 25, 2024
1 parent a66e48e commit 7d343cc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
12 changes: 11 additions & 1 deletion forecasttools/idata_w_dates_to_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
scoringutils ready dataframes
"""

import itertools
from datetime import date, datetime, timedelta

import arviz as az
Expand Down Expand Up @@ -242,8 +243,17 @@ def add_time_coords_to_idata_dimensions(
forecasttools.validate_iter_has_expected_types(
dimensions, str, "dimensions"
)
# create tuples, the groups should have
# every combination of variables and
# dimensions
var_dim_combinations = list(itertools.product(variables, dimensions))
gvd_tuples = [
(group, var, dim)
for group in groups
for var, dim in var_dim_combinations
]
# iterate over (group, variable, dimension) triples
for group, variable, dimension in zip(groups, variables, dimensions):
for group, variable, dimension in gvd_tuples:
try:
idata = add_time_coords_to_idata_dimension(
idata=idata,
Expand Down
15 changes: 9 additions & 6 deletions notebooks/forecasttools_community_demo_2024-11-19.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ import forecasttools
import matplotlib.pyplot as plt
import xarray as xr
import numpy as np
from datetime import timedelta, datetime
from datetime import timedelta, datetime, date
# load example FluSight submission
submission = forecasttools.example_flusight_submission
Expand Down Expand Up @@ -212,9 +212,7 @@ idata_wo_dates["posterior_predictive"]["obs"]["obs_dim_0"]
__Intermediary Functions For Adding Dates Or Time__

```{python}
start_date_iso = "2022-08-01"
start_time_as_dt = forecasttools.validate_and_get_start_time(start_date_iso)
start_time_as_dt = date(2022, 8, 1)
variable_data = idata_wo_dates["posterior_predictive"]["obs"]
Expand Down Expand Up @@ -243,7 +241,7 @@ idata_w_dates_single = forecasttools.add_time_coords_to_idata_dimension(
group="posterior_predictive",
variable="obs",
dimension="obs_dim_0",
start_date_iso=start_date_iso,
start_date_iso=start_time_as_dt,
time_step=timedelta(weeks=1), # notice weeks
)
idata_w_dates_single.posterior_predictive
Expand All @@ -254,17 +252,22 @@ idata_w_dates_single["posterior_predictive"]["obs"]["obs_dim_0"].values[-15:]
```

```{python}
idata_w_dates_multiple = forecasttools.add_time_coords_to_idata_dimensions(
idata=idata_wo_dates,
groups=["posterior_predictive", "observed_data"],
variables="obs",
dimensions="obs_dim_0",
start_date_iso=start_date_iso,
start_date_iso=start_time_as_dt,
time_step=timedelta(days=1),
)
idata_w_dates_multiple.observed_data
```

```{python}
idata_w_dates_multiple["observed_data"]["obs"]["obs_dim_0"].values[-15:]
```

```{python}
idata_w_dates_multiple["posterior_predictive"]["obs"]["obs_dim_0"].values[-15:]
```

0 comments on commit 7d343cc

Please sign in to comment.