From ed786e2a8364fe16ee49775c92c650706b9704d7 Mon Sep 17 00:00:00 2001 From: Elise Hinman Date: Wed, 24 Jul 2024 13:32:36 -0500 Subject: [PATCH] attempt to fix tests, remove unused cumulative function --- hyswap/cumulative.py | 49 +------------------------------------- hyswap/exceedance.py | 16 +++++-------- hyswap/percentiles.py | 3 +-- hyswap/rasterhydrograph.py | 3 +-- hyswap/runoff.py | 3 +-- tests/test_cumulative.py | 17 ------------- 6 files changed, 10 insertions(+), 81 deletions(-) diff --git a/hyswap/cumulative.py b/hyswap/cumulative.py index 1894e4c..06d2031 100644 --- a/hyswap/cumulative.py +++ b/hyswap/cumulative.py @@ -69,7 +69,7 @@ def calculate_daily_cumulative_values(df, data_column_name, years = df['index_year'].unique() # make an empty dataframe to hold cumulative values for each year - cdf = pd.DataFrame([]) + cdf = pd.DataFrame() selected_columns = [ data_column_name, 'index_month_day', @@ -100,50 +100,3 @@ def calculate_daily_cumulative_values(df, data_column_name, cdf = cdf[['index_month_day', 'index_year', 'index_doy', 'cumulative']] return cdf - -def _tidy_cumulative_dataframe(cdf, year_type): - """Tidy a cumulative dataframe. - - Parameters - ---------- - cdf : pandas.DataFrame - DataFrame containing cumulative values, rows are years, columns are - days of year. - year_type : str - The type of year to use. Must be one of 'calendar', 'water', or - 'climate'. Default is 'calendar' which starts the year on January 1 - and ends on December 31. 'water' starts the year on October 1 and - ends on September 30 of the following year which is the "water year". - For example, October 1, 2010 to September 30, 2011 is "water year - 2011". 'climate' years begin on April 1 and end on March 31 of the - following year, they are numbered by the ending year. For example, - April 1, 2010 to March 31, 2011 is "climate year 2011". - - Returns - ------- - cdf : pandas.DataFrame - DataFrame containing cumulative values, rows are dates, columns include - years, day of year (doy), and cumulative values. - """ - # convert cdf to dataframe organized with full dates on the index - cdf2 = cdf.stack().reset_index() - cdf2.columns = ["index_year", "index_doy", "cumulative"] - # create date column - if year_type == "calendar": - cdf2["date"] = pd.to_datetime( - cdf2["index_year"].astype(str) + "-" + - cdf2["index_doy"].astype(str), - format="%Y-%j") - elif year_type == "water": - cdf2["date"] = pd.to_datetime( - cdf2["index_year"].astype(str) + "-" + - cdf2["index_doy"].astype(str), - format="%Y-%j") + pd.DateOffset(days=273) - elif year_type == "climate": - cdf2["date"] = pd.to_datetime( - cdf2["index_year"].astype(str) + "-" + - cdf2["index_doy"].astype(str), - format="%Y-%j") + pd.DateOffset(days=90) - # set date to index - cdf2 = cdf2.set_index("date") - return cdf2 diff --git a/hyswap/exceedance.py b/hyswap/exceedance.py index 5095cf4..b8e74f8 100644 --- a/hyswap/exceedance.py +++ b/hyswap/exceedance.py @@ -39,20 +39,18 @@ def calculate_exceedance_probability_from_distribution(x, dist, distribution with a mean of 1 and a standard deviation of 0.25. .. doctest:: - :skipif: True # docstrings test fails with np.float64 - >>> exceedance.calculate_exceedance_probability_from_distribution( - ... 1, 'lognormal', 1, 0.25) - 0.6132049428659028 + >>> np.round(exceedance.calculate_exceedance_probability_from_distribution( + ... 1, 'lognormal', 1, 0.25), 3).item() + 0.613 Calculating the exceedance probability of a value of 1 from a normal distribution with a mean of 1 and a standard deviation of 0.25. .. doctest:: - :skipif: True # docstrings test fails with np.float64 >>> exceedance.calculate_exceedance_probability_from_distribution( - ... 1, 'normal', 1, 0.25) + ... 1, 'normal', 1, 0.25).item() 0.5 """ # type check @@ -119,20 +117,18 @@ def calculate_exceedance_probability_from_values(x, values_to_compare, of 1, 2, 3, and 4. .. doctest:: - :skipif: True # docstrings test fails with np.float64 >>> exceedance.calculate_exceedance_probability_from_values( - ... 1, [1, 2, 3, 4], method='linear') + ... 1, [1, 2, 3, 4], method='linear').item() 1.0 Calculating the exceedance probability of a value of 5 from a set of values of 1, 2, 3, and 4. .. doctest:: - :skipif: True # docstrings test fails with np.float64 >>> exceedance.calculate_exceedance_probability_from_values( - ... 5, [1, 2, 3, 4]) + ... 5, [1, 2, 3, 4]).item() 0.0 Fetch some data from NWIS and calculate the exceedance probability diff --git a/hyswap/percentiles.py b/hyswap/percentiles.py index c82ef66..ba42b79 100644 --- a/hyswap/percentiles.py +++ b/hyswap/percentiles.py @@ -611,14 +611,13 @@ def calculate_fixed_percentile_from_value(value, percentile_df): Calculate the percentile associated with a value from some synthetic data. .. doctest:: - :skipif: True # docstrings test fails with np.float64 >>> data = pd.DataFrame({'values': np.arange(1001), ... 'date': pd.date_range('2020-01-01', '2022-09-27')}).set_index('date') # noqa: E501 >>> pcts_df = percentiles.calculate_fixed_percentile_thresholds( ... data, 'values', percentiles=[5, 10, 25, 50, 75, 90, 95]) >>> new_percentile = percentiles.calculate_fixed_percentile_from_value( - ... 500, pcts_df) + ... 500, pcts_df).item() >>> new_percentile 50.0 diff --git a/hyswap/rasterhydrograph.py b/hyswap/rasterhydrograph.py index 93487fe..932f913 100644 --- a/hyswap/rasterhydrograph.py +++ b/hyswap/rasterhydrograph.py @@ -62,12 +62,11 @@ def format_data(df, data_column_name, date_column_name=None, Formatting synthetic daily data for a raster hydrograph. .. doctest:: - :skipif: True # docstrings test fails with np.float64 >>> df = pd.DataFrame({'date': pd.date_range('1/1/2010', '12/31/2010'), ... 'data': np.random.rand(365)}) >>> df_formatted = rasterhydrograph.format_data(df, 'data', 'date') - >>> df_formatted.index[0] + >>> df_formatted.index[0].item() 2010 >>> len(df_formatted.columns) 365 diff --git a/hyswap/runoff.py b/hyswap/runoff.py index 079a415..af1dbe7 100644 --- a/hyswap/runoff.py +++ b/hyswap/runoff.py @@ -28,10 +28,9 @@ def convert_cfs_to_runoff(cfs, drainage_area, frequency="annual"): Convert 14 cfs to mm/yr for a 250 km2 drainage area. .. doctest:: - :skipif: True # docstrings test fails with np.float64 >>> mmyr = runoff.convert_cfs_to_runoff(14, 250) - >>> np.round(mmyr) + >>> np.round(mmyr).item() 50.0 """ # convert frequency string to value diff --git a/tests/test_cumulative.py b/tests/test_cumulative.py index 4b3a9bf..af9901e 100644 --- a/tests/test_cumulative.py +++ b/tests/test_cumulative.py @@ -4,23 +4,6 @@ import pandas as pd from hyswap import cumulative - -def test_tidy_cumulative_dataframe(): - """Test the _tidy_cumulative_dataframe function.""" - # make a dataframe - cdf = pd.DataFrame( - index=np.arange(2000, 2003), - columns=np.arange(1, 367), - data=np.arange(1, 1099).reshape(3, 366)) - # test the function - cdf = cumulative._tidy_cumulative_dataframe(cdf, 'calendar') - assert cdf.shape == (1098, 3) - assert cdf.columns.tolist() == ['index_year', 'index_doy', 'cumulative'] - assert cdf.index.year.unique().tolist() == [2000, 2001, 2002, 2003] - assert cdf['index_doy'].tolist() == list(range(1, 367)) * 3 - assert cdf['cumulative'].tolist() == list(range(1, 1099)) - - class TestDailyCumulativeValues: def test_calculate_daily_cumulative_values(self):