Skip to content

Commit

Permalink
attempt to fix tests, remove unused cumulative function
Browse files Browse the repository at this point in the history
  • Loading branch information
ehinman committed Jul 24, 2024
1 parent 2f57764 commit ed786e2
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 81 deletions.
49 changes: 1 addition & 48 deletions hyswap/cumulative.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
16 changes: 6 additions & 10 deletions hyswap/exceedance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions hyswap/percentiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions hyswap/rasterhydrograph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions hyswap/runoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 0 additions & 17 deletions tests/test_cumulative.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit ed786e2

Please sign in to comment.