HYPE-Group4 / python-intermediate-rivercatchment

0 stars 0 forks source link

Feature branch yong #10

Closed YongchaoHuang closed 1 year ago

YongchaoHuang commented 1 year ago

Added 3 functions:

  1. daily_stats(data) in models.py
  2. daily_std(data) in models.py
  3. test_daily_std(test_data, test_index, test_columns, expected_data, expected_index, expected_columns) in test_models.py
YongchaoHuang commented 1 year ago

change details: In models.py:

def daily_stats(data):
    """Calculate the daily statistics of a 2D data array.
    Index must be np.datetime64 compatible format."""
    return data.groupby(data.index.date).describe()

# calculate daily standard deviation
def daily_std(data):
    """Calculate the daily standard deviation of a 2D data array.
    Index must be np.datetime64 compatible format."""
    return data.groupby(data.index.date).std()

in test_models.py:

# write tests for daily_std(data)
@pytest.mark.parametrize(
    "test_data, test_index, test_columns, expected_data, expected_index, expected_columns",
    [
        (
            [[0.0, 0.4], [0.0, 0.4], [0.0, 0.4], [0.0, 0.6], [0.2, 0.2], [0.0, 0.4], [0.0, 0.8], [0.2, 0.6]],
            [
                pd.to_datetime('2005-12-01 23:00:00'),
                pd.to_datetime('2005-12-01 23:15:00'),
                pd.to_datetime('2005-12-01 23:30:00'),
                pd.to_datetime('2005-12-01 23:45:00'),
                pd.to_datetime('2005-12-02 00:00:00'),
                pd.to_datetime('2005-12-02 00:15:00'),
                pd.to_datetime('2005-12-02 00:30:00'),
                pd.to_datetime('2005-12-02 00:45:00'),
            ],
            ['FP35', 'PL16'],
            [[0.0, 0.1], [0.11547, 0.258199]],
            [datetime.date(2005, 12, 1), datetime.date(2005, 12, 2)],
            ['FP35', 'PL16']
        ),
    ]
)
def test_daily_std(test_data, test_index, test_columns, expected_data, expected_index, expected_columns):
    """Test std function works with zeros and positive integers"""
    from catchment.models import daily_std

    pdt.assert_frame_equal(
        daily_std(pd.DataFrame(data=test_data, index=test_index, columns=test_columns)),
        pd.DataFrame(data=expected_data, index=expected_index, columns=expected_columns))

in views.py:


# visulise data using box plot
def boxplot(data_dict):
    """Display box plots of the given data.

    :param data_dict: Dictionary of name -> data to plot
    """

    num_plots = len(data_dict)
    fig = plt.figure(figsize=((3 * num_plots) + 1, 3.0))

    for i, (name, data) in enumerate(data_dict.items()):
        axes = fig.add_subplot(1, num_plots, i + 1)

        axes.set_ylabel(name)
        axes.boxplot(data)
        axes.legend(data.columns)

    fig.tight_layout()

    plt.show()

daily_stats(data) gives summary statistics to arrays (each column of pd dataframe); daily_std(data) calculates the std for each column; boxplot plots the boxplot for each column of a pd dataframe.

YongchaoHuang commented 1 year ago

14 solved.