gen_orig()   B
last analyzed

Complexity

Conditions 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
c 2
b 0
f 0
dl 0
loc 25
rs 8.8571
1
import os
2
from contextlib import contextmanager
3
4
import matplotlib as mpl
5
import matplotlib.pyplot as plt
6
import numpy as np
7
from click.testing import CliRunner
8
from matplotlib.colors import colorConverter
9
10
from replotlib import Axes
11
from replotlib.cli import main
12
13
PATH = os.path.dirname(os.path.realpath(__file__))
14
15
16
@contextmanager
17
def ignored(*exceptions):
18
    try:
19
        yield
20
    except exceptions:
21
        pass
22
23
24
def diff(file1, file2, ignore=None):
25
    if isinstance(ignore, str):
26
        ignore = [ignore]
27
    with open(file1) as f1, open(file2) as f2:
28
        for lineno, (line1, line2) in enumerate(zip(f1, f2), 1):
29
            ignoreline = False
30
            if ignore:
31
                for ig in ignore:
32
                    if ig in line1 and ig in line2:
33
                        ignoreline = True
34
                        break
35
            if not ignoreline and line1 != line2:
36
                return lineno
37
    return None
38
39
40
def test_json():
41
    gen_orig(PATH + '/test.rplt', 'json')
42
    runner = CliRunner()
43
    runner.invoke(main, ['-s', PATH + '/test.eps', PATH + '/test.rplt'])
44
    diff_from_origin = diff(PATH + '/test.eps', PATH + '/test_orig.eps',
45
                            ['CreationDate', 'Title'])
46
    assert diff_from_origin is None
47
48
49
def test_hdf5():
50
    gen_orig(PATH + '/test_h5.rplt', 'hdf5')
51
    runner = CliRunner()
52
    runner.invoke(main, ['-s', PATH + '/test_h5.eps', PATH + '/test_h5.rplt'])
53
    diff_from_origin = diff(PATH + '/test_h5.eps', PATH + '/test_h5_orig.eps',
54
                            ['CreationDate', 'Title'])
55
    assert diff_from_origin is None
56
57
58
def reset_mpl():
59
    mpl.rcParams.update(mpl.rcParamsDefault)
60
    colorConverter.cache = {}
61
62
63
def test_main():
64
    reset_mpl()
65
    runner = CliRunner()
66
    runner.invoke(main, [PATH + '/test.rplt'])
67
68
69
def test_main_save():
70
    reset_mpl()
71
    mpl.rcParams.update(mpl.rcParamsDefault)
72
    runner = CliRunner()
73
    runner.invoke(main, ['-s', PATH + '/test.eps', PATH + '/test.rplt'])
74
75
76
def test_main_style():
77
    reset_mpl()
78
    mpl.rcParams.update(mpl.rcParamsDefault)
79
    runner = CliRunner()
80
    runner.invoke(main, ['--style_file', PATH + '/style.json', '-s',
81
                         PATH + '/test_styl.eps', PATH + '/test.rplt'])
82
83
def test_main_bb():
84
    reset_mpl()
85
    mpl.rcParams.update(mpl.rcParamsDefault)
86
    runner = CliRunner()
87
    runner.invoke(main, ['--bb', '-s', PATH + '/test_bb.eps',
88
                         PATH + '/test.rplt'])
89
90
91
def gen_orig(test_file=PATH + '/test.rplt', file_type='json'):
92
    reset_mpl()
93
    plt.figure()
94
    style = {'nice_color': {'color': 'green'}}
95
    with ignored(OSError):
96
        os.remove(test_file)
97
    print(test_file, file_type)
98
    plt.rcParams['lines.linewidth'] = 3
99
    plt.rcParams['lines.color'] = 'k'
100
    ax = Axes(test_file, file_type=file_type, style=style)
101
    ax.rcParams = {'lines.linewidth': 3,
102
                   'lines.color': 'k'}
103
    style = {'strong_color': {'color': 'blue'},
104
             'errorbar': {'fmt': 'o'}}
105
    ax.style = style
106
    x = np.linspace(0, 1)
107
    ax.plot(x, x**2, color='c', label=r'$x^2$')
108
    ax.errorbar(x, x**2, x**2*0.1)
109
    ax.plot(x, x**3, style='strong_color')
110
    ax.plot(x, x**4, style='nice_color')
111
    ax.set_xlabel('x')
112
    ax.set_ylabel('y')
113
    ax.legend()
114
    plt.savefig(test_file.replace('.rplt', '_orig.eps'))
115
    reset_mpl()
116