<commit_before>"""Test Matplotlib ConversionInterface"""
from pytest import raises
from unyt._on_demand_imports import _matplotlib, NotAModule
from unyt import s, K
from unyt.exceptions import UnitConversionError


def test_label():
    if isinstance(_matplotlib.pyplot, NotAModule):
        return
    plt = _matplotlib.pyplot
    x = [0, 1, 2] * s
    y = [3, 4, 5] * K
    _, ax = plt.subplots()
    ax.plot(x, y)
    expected_xlabel = "$\\left(\\rm{s}\\right)$"
    assert ax.xaxis.get_label().get_text() == expected_xlabel
    expected_ylabel = "$\\left(\\rm{K}\\right)$"
    assert ax.yaxis.get_label().get_text() == expected_ylabel


def test_convert_unit():
    if isinstance(_matplotlib.pyplot, NotAModule):
        return
    plt = _matplotlib.pyplot
    x = [0, 1, 2] * s
    y = [1000, 2000, 3000] * K
    _, ax = plt.subplots()
    ax.plot(x, y, yunits="Celcius")
    expected = y.to("Celcius")
    line = ax.lines[0]
    original_y_array = line.get_data()[1]
    converted_y_array = line.convert_yunits(original_y_array)
    results = converted_y_array == expected
    assert results.all()


def test_convert_equivalency():
    if isinstance(_matplotlib.pyplot, NotAModule):
        return
    plt = _matplotlib.pyplot
    x = [0, 1, 2] * s
    y = [1000, 2000, 3000] * K
    _, ax = plt.subplots()
    ax.plot(x, y, yunits=("J", "thermal"))
    expected = y.to("J", "thermal")
    line = ax.lines[0]
    original_y_array = line.get_data()[1]
    converted_y_array = line.convert_yunits(original_y_array)
    results = converted_y_array == expected
    assert results.all()


def test_dimensionless():
    if isinstance(_matplotlib.pyplot, NotAModule):
        return
    plt = _matplotlib.pyplot
    x = [0, 1, 2] * s
    y = [3, 4, 5] * K / K
    ax = plt.figure().add_subplot()
    ax.plot(x, y)
    expected_ylabel = ""
    assert ax.yaxis.get_label().get_text() == expected_ylabel


def test_conversionerror():
    if isinstance(_matplotlib.pyplot, NotAModule):
        return
    plt = _matplotlib.pyplot
    x = [0, 1, 2] * s
    y = [3, 4, 5] * K
    ax = plt.figure().add_subplot()
    ax.plot(x, y)
    ax.xaxis.callbacks.exception_handler = None
    with raises((_matplotlib.units.ConversionError, UnitConversionError)):
        ax.xaxis.set_units("V")
<commit_msg>rewrite of test_mpl_interface to address review comments<commit_after>"""Test Matplotlib ConversionInterface"""
import numpy as np
import pytest
from unyt._on_demand_imports import _matplotlib, NotAModule
from unyt import s, K
from unyt.exceptions import UnitConversionError


check_matplotlib = pytest.mark.skipif(
    isinstance(_matplotlib.pyplot, NotAModule), reason="matplotlib not installed"
)


@pytest.fixture
def ax(scope="module"):
    yield _matplotlib.pyplot.figure().add_subplot()
    _matplotlib.pyplot.close()


@check_matplotlib
def test_label(ax):
    x = [0, 1, 2] * s
    y = [3, 4, 5] * K
    ax.clear()
    ax.plot(x, y)
    expected_xlabel = "$\\left(\\rm{s}\\right)$"
    assert ax.xaxis.get_label().get_text() == expected_xlabel
    expected_ylabel = "$\\left(\\rm{K}\\right)$"
    assert ax.yaxis.get_label().get_text() == expected_ylabel


@check_matplotlib
def test_convert_unit(ax):
    x = [0, 1, 2] * s
    y = [1000, 2000, 3000] * K
    ax.clear()
    ax.plot(x, y, yunits="Celcius")
    expected = y.to("Celcius")
    line = ax.lines[0]
    original_y_array = line.get_data()[1]
    converted_y_array = line.convert_yunits(original_y_array)
    results = converted_y_array == expected
    assert results.all()


@check_matplotlib
def test_convert_equivalency(ax):
    x = [0, 1, 2] * s
    y = [1000, 2000, 3000] * K
    ax.clear()
    ax.plot(x, y, yunits=("J", "thermal"))
    expected = y.to("J", "thermal")
    line = ax.lines[0]
    original_y_array = line.get_data()[1]
    converted_y_array = line.convert_yunits(original_y_array)
    results = converted_y_array == expected
    assert results.all()


@check_matplotlib
def test_dimensionless(ax):
    x = [0, 1, 2] * s
    y = [3, 4, 5] * K / K
    ax.clear()
    ax.plot(x, y)
    expected_ylabel = ""
    assert ax.yaxis.get_label().get_text() == expected_ylabel


@check_matplotlib
def test_conversionerror(ax):
    x = [0, 1, 2] * s
    y = [3, 4, 5] * K
    ax.clear()
    ax.plot(x, y)
    ax.xaxis.callbacks.exception_handler = None
    with pytest.raises((_matplotlib.units.ConversionError, UnitConversionError)):
        ax.xaxis.set_units("V")


@check_matplotlib
def test_ndarray_label(ax):
    x = [0, 1, 2] * s
    y = np.arange(3, 6)
    ax.clear()
    ax.plot(x, y)
    expected_xlabel = "$\\left(\\rm{s}\\right)$"
    assert ax.xaxis.get_label().get_text() == expected_xlabel
    expected_ylabel = ""
    assert ax.yaxis.get_label().get_text() == expected_ylabel


@check_matplotlib
def test_list_label(ax):
    x = [0, 1, 2] * s
    y = [3, 4, 5]
    ax.clear()
    ax.plot(x, y)
    expected_xlabel = "$\\left(\\rm{s}\\right)$"
    assert ax.xaxis.get_label().get_text() == expected_xlabel
    expected_ylabel = ""
    assert ax.yaxis.get_label().get_text() == expected_ylabel

{'fraction_non_alphanumeric': 0.1464039408866995, 'fraction_numerical': 0.022660098522167486, 'mean_word_length': 3.2691337258200166, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6697794', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1913, 'n_words': 402}