Skip to content

Commit

Permalink
add minor numpy to python change in trajectories to quant; example fi…
Browse files Browse the repository at this point in the history
…le additions
  • Loading branch information
AFg6K7h4fhy2 committed Oct 8, 2024
1 parent d61adb3 commit 2a60571
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion forecasttools/to_flusight.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def calculate_epidate(date):
def get_flusight_target_end_dates(
reference_date: str, horizons=None
) -> pl.DataFrame:
# default horizons
# set default horizons in case of no specification
if horizons is None:
horizons = list(range(-1, 4))
reference_date_dt = datetime.strptime(reference_date, "%Y-%m-%d")
Expand Down
4 changes: 3 additions & 1 deletion forecasttools/trajectories_to_quantiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def trajectories_to_quantiles(
# set default quantiles
if quantiles is None:
quantiles = (
[0.01, 0.025] + list(np.arange(0.05, 1.0, 0.05)) + [0.975, 0.99]
[0.01, 0.025]
+ [0.05 * elt for elt in range(1, 20)]
+ [0.975, 0.99]
)
# group trajectories based on timepoint_cols and id_cols
group_cols = (
Expand Down
24 changes: 21 additions & 3 deletions notebooks/example_fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,31 @@

# %% IMPORTS


import polars as pl

import forecasttools

# %% EXAMINING FILE PATH BEING RETURNED

# example dataframe and location table
example_dict = {
"date": ["2024-10-08", "2024-10-08", "2024-10-08"],
"location": ["AL", "AK", "US"],
}
example_df = pl.from_dict(example_dict)

# call loc_abbr_to_flusight_code()
recoded_df = forecasttools.loc_abbr_to_flusight_code(
df=example_df, location_col="location"
)

# received warning (original implementation)
# MapWithoutReturnDtypeWarning: Calling `map_elements` without specifying `return_dtype` can lead to unpredictable results. Specify `return_dtype` to silence this warning.

# no longer receives warning after using expr.replace()


# %% USING POLARS EXPR.REPLACE()

# example dataframe and location table
Expand All @@ -22,15 +43,12 @@
}
example_df = pl.from_dict(example_dict)
loc_table = forecasttools.location_table
print(example_df)
print(loc_table)

# replace "location" values with codes from loc_table
new_df = example_df.with_columns(
location=pl.col("location").replace(
old=loc_table["short_name"], new=loc_table["location_code"]
)
)
print(new_df)

# %%

0 comments on commit 2a60571

Please sign in to comment.