test_script.py 8.91 KB
Newer Older
1 2
'''Tests for bob.measure scripts'''

3
import sys
4 5 6 7
import filecmp
import click
from click.testing import CliRunner
import bob.io.base.test_utils
8
from .script import commands
9 10 11 12

def test_metrics():
    dev1 = bob.io.base.test_utils.datafile('dev-1.txt', 'bob.measure')
    runner = CliRunner()
13
    result = runner.invoke(commands.metrics, [dev1])
14 15 16 17
    with runner.isolated_filesystem():
        with open('tmp', 'w') as f:
            f.write(result.output)
        test_ref = bob.io.base.test_utils.datafile('test_m1.txt', 'bob.measure')
18
        assert result.exit_code == 0, (result.exit_code, result.output)
19 20 21 22 23 24

    dev2 = bob.io.base.test_utils.datafile('dev-2.txt', 'bob.measure')
    test1 = bob.io.base.test_utils.datafile('test-1.txt', 'bob.measure')
    test2 = bob.io.base.test_utils.datafile('test-2.txt', 'bob.measure')
    with runner.isolated_filesystem():
        result = runner.invoke(
25
            commands.metrics, ['-e', dev1, test1, dev2, test2]
26 27 28 29
        )
        with open('tmp', 'w') as f:
            f.write(result.output)
        test_ref = bob.io.base.test_utils.datafile('test_m2.txt', 'bob.measure')
30
        assert result.exit_code == 0
31 32
    with runner.isolated_filesystem():
        result = runner.invoke(
33
            commands.metrics, ['-e', '-l', 'tmp', dev1, test1, dev2, test2, '-lg',
34
                              'A,B']
35
        )
36 37
        assert result.exit_code == 0, (result.exit_code, result.output)

38 39
    with runner.isolated_filesystem():
        result = runner.invoke(
40
            commands.metrics, ['-l', 'tmp', dev1, dev2]
41
        )
42
        assert result.exit_code == 0, (result.exit_code, result.output)
43 44 45 46 47

def test_roc():
    dev1 = bob.io.base.test_utils.datafile('dev-1.txt', 'bob.measure')
    runner = CliRunner()
    with runner.isolated_filesystem():
48
        result = runner.invoke(commands.roc, ['--output', 'test.pdf',dev1])
49 50 51 52 53 54 55
        if result.output:
            click.echo(result.output)
        assert result.exit_code == 0
    dev2 = bob.io.base.test_utils.datafile('dev-2.txt', 'bob.measure')
    test1 = bob.io.base.test_utils.datafile('test-1.txt', 'bob.measure')
    test2 = bob.io.base.test_utils.datafile('test-2.txt', 'bob.measure')
    with runner.isolated_filesystem():
56
        result = runner.invoke(commands.roc, ['--split', '--output',
57
                                              'test.pdf', '-e',
58
                                              '-ts', 'A,',
59 60 61
                                              dev1, test1, dev2, test2])
        if result.output:
            click.echo(result.output)
62
        assert result.exit_code == 0, (result.exit_code, result.output)
63

64
    with runner.isolated_filesystem():
65
        result = runner.invoke(commands.roc, ['-e', '--output',
66
                                              'test.pdf', '--legends', 'A,B',
67 68 69
                                              dev1, test1, dev2, test2])
        if result.output:
            click.echo(result.output)
70
        assert result.exit_code == 0, (result.exit_code, result.output)
71 72


73 74 75 76
def test_det():
    dev1 = bob.io.base.test_utils.datafile('dev-1.txt', 'bob.measure')
    runner = CliRunner()
    with runner.isolated_filesystem():
77
        result = runner.invoke(commands.det, [dev1])
78 79 80 81 82 83
        if result.output:
            click.echo(result.output)
        assert result.exit_code == 0
    dev2 = bob.io.base.test_utils.datafile('dev-2.txt', 'bob.measure')
    test1 = bob.io.base.test_utils.datafile('test-1.txt', 'bob.measure')
    test2 = bob.io.base.test_utils.datafile('test-2.txt', 'bob.measure')
84
    with runner.isolated_filesystem():
85
        result = runner.invoke(commands.det, ['-e', '--split', '--output',
86
                                              'test.pdf', '--legends', 'A,B',
87
                                              '-lc', 'upper-right',
88 89 90
                                              dev1, test1, dev2, test2])
        if result.output:
            click.echo(result.output)
91 92
        assert result.exit_code == 0, (result.exit_code, result.output)

93
    with runner.isolated_filesystem():
94
        result = runner.invoke(commands.det, ['--output',
95
                                              'test.pdf', '-e',
96 97 98
                                              dev1, test1, dev2, test2])
        if result.output:
            click.echo(result.output)
99
        assert result.exit_code == 0, (result.exit_code, result.output)
100 101 102 103 104 105

def test_epc():
    dev1 = bob.io.base.test_utils.datafile('dev-1.txt', 'bob.measure')
    test1 = bob.io.base.test_utils.datafile('test-1.txt', 'bob.measure')
    runner = CliRunner()
    with runner.isolated_filesystem():
106
        result = runner.invoke(commands.epc, [dev1, test1])
107 108
        if result.output:
            click.echo(result.output)
109 110
        assert result.exit_code == 0, (result.exit_code, result.output)

111 112 113
    dev2 = bob.io.base.test_utils.datafile('dev-2.txt', 'bob.measure')
    test2 = bob.io.base.test_utils.datafile('test-2.txt', 'bob.measure')
    with runner.isolated_filesystem():
114
        result = runner.invoke(commands.epc, ['--output', 'test.pdf',
115
                                              '--legends', 'A,B',
116
                                              '--titles', 'TA,TB',
117
                                              '-lc', 'upper-right',
118 119 120
                                              dev1, test1, dev2, test2])
        if result.output:
            click.echo(result.output)
121
        assert result.exit_code == 0, (result.exit_code, result.output)
122 123 124 125 126 127 128 129

def test_hist():
    dev1 = bob.io.base.test_utils.datafile('dev-1.txt', 'bob.measure')
    test1 = bob.io.base.test_utils.datafile('test-1.txt', 'bob.measure')
    dev2 = bob.io.base.test_utils.datafile('dev-2.txt', 'bob.measure')
    test2 = bob.io.base.test_utils.datafile('test-2.txt', 'bob.measure')
    runner = CliRunner()
    with runner.isolated_filesystem():
130
        result = runner.invoke(commands.hist, [dev1])
131 132
        if result.output:
            click.echo(result.output)
133
        assert result.exit_code == 0, (result.exit_code, result.output, result.exception)
134 135

    with runner.isolated_filesystem():
136 137
        result = runner.invoke(commands.hist, ['--criterion',
                                               'min-hter', '--no-line',
138
                                               '--output', 'HISTO.pdf',  '-b',
139
                                               '30,100', dev1, dev2])
140 141
        if result.output:
            click.echo(result.output)
142
        assert result.exit_code == 0, (result.exit_code, result.output)
143 144

    with runner.isolated_filesystem():
145
        result = runner.invoke(commands.hist, ['-e', '--criterion', 'eer','--output',
146
                                               'HISTO.pdf',  '-b',  '30,20',
147
                                               '-sp', 221, '-lg', 'A,B',
148 149 150
                                               dev1, test1, dev2, test2])
        if result.output:
            click.echo(result.output)
151
        assert result.exit_code == 0, (result.exit_code, result.output)
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
def test_hist_legends():
    dev1 = bob.io.base.test_utils.datafile('dev-1.txt', 'bob.measure')
    test1 = bob.io.base.test_utils.datafile('test-1.txt', 'bob.measure')
    dev2 = bob.io.base.test_utils.datafile('dev-2.txt', 'bob.measure')
    test2 = bob.io.base.test_utils.datafile('test-2.txt', 'bob.measure')
    runner = CliRunner()

    # share same legend for dev/eval of each system
    with runner.isolated_filesystem():
        result = runner.invoke(commands.hist, ['-e', '-sp', 221, '-lg', 'A,B',
                                               dev1, test1, dev2, test2])
        if result.output:
            click.echo(result.output)
        assert result.exit_code == 0, (result.exit_code, result.output)

    # individual legends for dev and eval
    with runner.isolated_filesystem():
        result = runner.invoke(commands.hist, ['-e', '-sp', 221, '-lg',
                                               'A,B,C,D',
                                               dev1, test1, dev2, test2])
        if result.output:
            click.echo(result.output)
        assert result.exit_code == 0, (result.exit_code, result.output)
176 177 178 179 180 181 182 183

def test_evaluate():
    dev1 = bob.io.base.test_utils.datafile('dev-1.txt', 'bob.measure')
    test1 = bob.io.base.test_utils.datafile('test-1.txt', 'bob.measure')
    dev2 = bob.io.base.test_utils.datafile('dev-2.txt', 'bob.measure')
    test2 = bob.io.base.test_utils.datafile('test-2.txt', 'bob.measure')
    runner = CliRunner()
    with runner.isolated_filesystem():
184
        result = runner.invoke(commands.evaluate, [dev1])
185
        assert result.exit_code == 0, (result.exit_code, result.output, result.exception)
186 187 188

    with runner.isolated_filesystem():
        result = runner.invoke(
189
            commands.evaluate, ['--output', 'my_plots.pdf',
190
                                '-n', 300, dev1, dev2])
191
        assert result.exit_code == 0, (result.exit_code, result.output)
192 193 194

    with runner.isolated_filesystem():
        result = runner.invoke(
195
            commands.evaluate, ['-e', dev1, test1, dev2, test2])
196
        assert result.exit_code == 0, (result.exit_code, result.output)