<commit_before>
# pylint: disable=wildcard-import, missing-docstring, protected-access
# pylint: disable=attribute-defined-outside-init, invalid-name, no-self-use
# pylint: disable=wrong-import-order, unused-wildcard-import

from .utils import *

from contact_map.plot_utils import *
try:
    from matplotlib.colors import LinearSegmentedColormap
except ImportError:
    HAS_MATPLOTLIB = False

@pytest.mark.parametrize("val", [0.5, 0.55, 0.6, 0.65, 0.7])
@pytest.mark.parametrize("map_type", ["name", "cmap"])
def test_ranged_colorbar_cmap(map_type, val):
    matplotlib = pytest.importorskip("matplotlib")
    norm = matplotlib.colors.Normalize(vmin=-1, vmax=1)
    default_cmap = matplotlib.pyplot.get_cmap('seismic')
    cmap = {
        'name': 'seismic',
        'cmap': default_cmap
    }[map_type]
    cbmin = 0.5
    cbmax = 0.7
    cb = ranged_colorbar(cmap, norm, cbmin, cbmax)
    # slight error from different discretizations
    atol = (norm.vmax - norm.vmin) / (cbmax - cbmin) / default_cmap.N
    assert np.allclose([atol], [0.0390625])  # to remind that this is small
    assert_allclose(cb.cmap(cb.norm(val)), default_cmap(norm(val)),
                    atol=atol)

@pytest.mark.parametrize("cmap", ['seismic', 'Blues'])
def test_is_cmap_diverging(cmap):
    if HAS_MATPLOTLIB:
        custom = LinearSegmentedColormap(
            'testCmap',
            segmentdata={
                'red':   [[0.0,  0.0, 0.0],
                          [0.5,  1.0, 1.0],
                          [1.0,  1.0, 1.0]],
                 'green': [[0.0,  0.0, 0.0],
                           [0.25, 0.0, 0.0],
                           [0.75, 1.0, 1.0],
                           [1.0,  1.0, 1.0]],
                 'blue':  [[0.0,  0.0, 0.0],
                           [0.5,  0.0, 0.0],
                           [1.0,  1.0, 1.0]]},
            N=256
        )
    else:
        custom = None
    cmap, expected = {
        'seismic': ('seismic', True),
        'Blues': ('Blues', False),
        'custom': (custom, False),
    }[cmap]
    if cmap == 'custom':
        with pytest.warns(UserWarning):
            if HAS_MATPLOTLIB:
                assert is_cmap_diverging(cmap) == expected
            else:
                pytest.skip()
    else:
        assert is_cmap_diverging(cmap) == expected
<commit_msg>include case where MATPLOTBLIB=True<commit_after>
# pylint: disable=wildcard-import, missing-docstring, protected-access
# pylint: disable=attribute-defined-outside-init, invalid-name, no-self-use
# pylint: disable=wrong-import-order, unused-wildcard-import

from .utils import *

from contact_map.plot_utils import *
try:
    from matplotlib.colors import LinearSegmentedColormap
except ImportError:
    HAS_MATPLOTLIB = False
else:
    HAS_MATPLOTLIB = True

@pytest.mark.parametrize("val", [0.5, 0.55, 0.6, 0.65, 0.7])
@pytest.mark.parametrize("map_type", ["name", "cmap"])
def test_ranged_colorbar_cmap(map_type, val):
    matplotlib = pytest.importorskip("matplotlib")
    norm = matplotlib.colors.Normalize(vmin=-1, vmax=1)
    default_cmap = matplotlib.pyplot.get_cmap('seismic')
    cmap = {
        'name': 'seismic',
        'cmap': default_cmap
    }[map_type]
    cbmin = 0.5
    cbmax = 0.7
    cb = ranged_colorbar(cmap, norm, cbmin, cbmax)
    # slight error from different discretizations
    atol = (norm.vmax - norm.vmin) / (cbmax - cbmin) / default_cmap.N
    assert np.allclose([atol], [0.0390625])  # to remind that this is small
    assert_allclose(cb.cmap(cb.norm(val)), default_cmap(norm(val)),
                    atol=atol)

@pytest.mark.parametrize("cmap", ['seismic', 'Blues'])
def test_is_cmap_diverging(cmap):
    if HAS_MATPLOTLIB:
        custom = LinearSegmentedColormap(
            'testCmap',
            segmentdata={
                'red':   [[0.0,  0.0, 0.0],
                          [0.5,  1.0, 1.0],
                          [1.0,  1.0, 1.0]],
                 'green': [[0.0,  0.0, 0.0],
                           [0.25, 0.0, 0.0],
                           [0.75, 1.0, 1.0],
                           [1.0,  1.0, 1.0]],
                 'blue':  [[0.0,  0.0, 0.0],
                           [0.5,  0.0, 0.0],
                           [1.0,  1.0, 1.0]]},
            N=256
        )
    else:
        custom = None
    cmap, expected = {
        'seismic': ('seismic', True),
        'Blues': ('Blues', False),
        'custom': (custom, False),
    }[cmap]
    if cmap == 'custom':
        with pytest.warns(UserWarning):
            if HAS_MATPLOTLIB:
                assert is_cmap_diverging(cmap) == expected
            else:
                pytest.skip()
    else:
        assert is_cmap_diverging(cmap) == expected

{'fraction_non_alphanumeric': 0.14556692242114236, 'fraction_numerical': 0.03878942881500426, 'mean_word_length': 1.9404761904761905, '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': '348907', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1690, 'n_words': 326}