<commit_before>"""tests for windrose_utils."""
import datetime
from io import BytesIO

import pytest
from pandas import read_csv
from metpy.units import units
from pyiem.windrose_utils import windrose, _get_timeinfo
from pyiem.util import utc


def faux_data():
    """Generate some data for plotting."""
    basevalid = utc(2015, 1, 1, 6)
    valid = [basevalid]
    sknt = [None]
    drct = [None]
    for s in range(360):
        valid.append(basevalid + datetime.timedelta(days=s, hours=1))
        # Keep the max speed at ~24kts
        sknt.append(s / 13.0)
        drct.append(s)
    return valid, sknt, drct


def test_timeinfo():
    """Exercise the _get_timeinfo method"""
    res = _get_timeinfo(range(1, 10), "hour", 24, None)
    assert res["labeltext"] == "(1, 2, 3, 4, 5, 6, 7, 8, 9)"
    res = _get_timeinfo([1], "month", 1, None)
    assert res["sqltext"] == " and extract(month from valid) = 1 "


def test_windrose_without_units():
    """Ensure that we can deal with provided bins."""
    valid, sknt, drct = faux_data()
    res = windrose(
        "AMW2",
        sknt=sknt,
        drct=drct,
        valid=valid,
        months=[4, 5, 6],
        bins=[10, 20, 40],
        justdata=True,
    )
    # python2-3 hackery here
    sio = BytesIO()
    sio.write(res.encode("ascii"))
    sio.seek(0)
    df = read_csv(sio, skiprows=range(0, 8), index_col="Direction")
    assert len(df.columns) == 4
    assert abs(df.sum(axis=0).sum() - 100.0) < 0.1


def test_windrose_with_units():
    """Ensure that we can deal with provided bins."""
    valid, sknt, drct = faux_data()
    res = windrose(
        "AMW2",
        sknt=sknt,
        drct=drct,
        valid=valid,
        months=[4, 5, 6],
        bins=[10, 20, 40] * units("mph"),
    )
    assert res


@pytest.mark.mpl_image_compare(tolerance=0.1)
def test_windrose_month_limiter():
    """Test that we can filter by month."""
    valid, sknt, drct = faux_data()
    fig = windrose(
        "AMW2",
        sknt=sknt,
        drct=drct,
        valid=valid,
        months=[4, 5, 6],
        nogenerated=True,
    )
    return fig<commit_msg>fix pandas 1.1.0 sum quirk<commit_after>"""tests for windrose_utils."""
import datetime
from io import BytesIO

import pytest
from pandas import read_csv
from metpy.units import units
from pyiem.windrose_utils import windrose, _get_timeinfo
from pyiem.util import utc


def faux_data():
    """Generate some data for plotting."""
    basevalid = utc(2015, 1, 1, 6)
    valid = [basevalid]
    sknt = [None]
    drct = [None]
    for s in range(360):
        valid.append(basevalid + datetime.timedelta(days=s, hours=1))
        # Keep the max speed at ~24kts
        sknt.append(s / 13.0)
        drct.append(s)
    return valid, sknt, drct


def test_timeinfo():
    """Exercise the _get_timeinfo method"""
    res = _get_timeinfo(range(1, 10), "hour", 24, None)
    assert res["labeltext"] == "(1, 2, 3, 4, 5, 6, 7, 8, 9)"
    res = _get_timeinfo([1], "month", 1, None)
    assert res["sqltext"] == " and extract(month from valid) = 1 "


def test_windrose_without_units():
    """Ensure that we can deal with provided bins."""
    valid, sknt, drct = faux_data()
    res = windrose(
        "AMW2",
        sknt=sknt,
        drct=drct,
        valid=valid,
        months=[4, 5, 6],
        bins=[10, 20, 40],
        justdata=True,
    )
    # python2-3 hackery here
    sio = BytesIO()
    sio.write(res.encode("ascii"))
    sio.seek(0)
    df = read_csv(
        sio,
        skiprows=range(0, 8),
        index_col="Direction",
        delimiter=" *, *",
        engine="python",
    )
    assert len(df.columns) == 4
    assert abs(df.sum(axis=0).sum() - 100.0) < 0.1


def test_windrose_with_units():
    """Ensure that we can deal with provided bins."""
    valid, sknt, drct = faux_data()
    res = windrose(
        "AMW2",
        sknt=sknt,
        drct=drct,
        valid=valid,
        months=[4, 5, 6],
        bins=[10, 20, 40] * units("mph"),
    )
    assert res


@pytest.mark.mpl_image_compare(tolerance=0.1)
def test_windrose_month_limiter():
    """Test that we can filter by month."""
    valid, sknt, drct = faux_data()
    fig = windrose(
        "AMW2",
        sknt=sknt,
        drct=drct,
        valid=valid,
        months=[4, 5, 6],
        nogenerated=True,
    )
    return fig
{'fraction_non_alphanumeric': 0.13982464236271344, 'fraction_numerical': 0.03391785879095524, 'mean_word_length': 2.533007334963325, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3075490', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1546, 'n_words': 410}