Skip to content

Commit

Permalink
add some of the notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
AFg6K7h4fhy2 committed Jan 22, 2025
1 parent d85f49c commit 85c0dbf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
File renamed without changes.
2 changes: 2 additions & 0 deletions forecasttools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import polars as pl

from forecasttools.daily_to_epiweekly import df_aggregate_to_epiweekly
from forecasttools.idata_to_tidy import convert_idata_forecast_to_tidydraws
from forecasttools.idata_w_dates_to_df import (
add_time_coords_to_idata_dimension,
add_time_coords_to_idata_dimensions,
Expand Down Expand Up @@ -95,4 +96,5 @@
"generate_time_range_for_dim",
"validate_iter_has_expected_types",
"ensure_listlike",
"convert_idata_forecast_to_tidydraws"
]
4 changes: 2 additions & 2 deletions forecasttools/idata_to_tidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ def convert_idata_forecast_to_tidydraws(
value_name="value"
)
group_pols_df = group_pols_df.with_columns(
pl.col("variable").map_elements(lambda x: re.sub(r"\[.*\]", "", x)).alias("variable")
pl.col("variable").map_elements(
lambda x: re.sub(r"\[.*\]", "", x)).alias("variable")
)
group_pols_df = group_pols_df.with_columns(
((pl.col("draw") - 1) % group_pols_df["draw"].n_unique() + 1).alias(".iteration")
)
group_pols_df = group_pols_df.rename({"chain": ".chain", "draw": ".draw"})
tidy_dfs[group] = group_pols_df.select([".chain", ".draw", ".iteration", "variable", "value"])

return tidy_dfs
41 changes: 41 additions & 0 deletions notebooks/pyrenew_dates_to_tidy.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: Add Dates To Pyrenew Idata And Use In Tidy-Verse
format: gfm
engine: jupyter
---

_The following notebook illustrates the addition of dates to an external `idata` object before demonstrating the tidy-usable capabilities in R._

__Load In Packages And External Pyrenew InferenceData Object__

```{python}
#| echo: true
import forecasttools
import arviz as az
import xarray as xr
xr.set_options(display_expand_data=False, display_expand_attrs=False)
pyrenew_idata_path = "../assets/external/inference_data_1.nc"
pyrenew_idata = az.from_netcdf(pyrenew_idata_path)
pyrenew_idata
```

__Define Groups To Save And Convert__

```{python}
#| echo: true
pyrenew_groups = ["posterior_predictive"]
tidy_usable_groups = forecasttools.convert_idata_forecast_to_tidydraws(
idata=pyrenew_idata,
groups=pyrenew_groups
)
# show variables
print(tidy_usable_groups["posterior_predictive"]["variable"].unique())
# show output
tidy_usable_groups
```

0 comments on commit 85c0dbf

Please sign in to comment.