test_filelist.py 22.3 KB
Newer Older
Amir Mohammadi's avatar
Amir Mohammadi committed
1
2
3
4
5
6
7
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

"""A few checks at the Verification Filelist database.
"""

import os
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
8
import bob.io.base
Amir Mohammadi's avatar
Amir Mohammadi committed
9
import bob.io.base.test_utils
10
11
12
13
from bob.bio.base.database import (
    CSVDatasetDevEval,
    CSVToSampleLoader,
    CSVDatasetCrossValidation,
14
    AnnotationsLoader,
15
    LSTToSampleLoader,
16
)
17
import nose.tools
18
19
from bob.pipelines import DelayedSample, SampleSet
import numpy as np
20
from bob.bio.base.test.utils import atnt_database_directory
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
21
22
23
24
from bob.bio.base.pipelines.vanilla_biometrics import (
    Distance,
    VanillaBiometricsPipeline,
)
25
from bob.bio.base.database import FileListBioDatabase
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
26
27
28
from sklearn.preprocessing import FunctionTransformer
from sklearn.pipeline import make_pipeline
from bob.pipelines import wrap
Amir Mohammadi's avatar
Amir Mohammadi committed
29

30
31
32
33
34

legacy_example_dir = os.path.realpath(
    bob.io.base.test_utils.datafile(".", __name__, "data/example_filelist")
)

35
36
37
38
39
legacy2_example_dir = os.path.realpath(
    bob.io.base.test_utils.datafile(".", __name__, "data/example_filelist2")
)


40
41
42
example_dir = os.path.realpath(
    bob.io.base.test_utils.datafile(".", __name__, "data/example_csv_filelist")
)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
43
44
45
atnt_protocol_path = os.path.realpath(
    bob.io.base.test_utils.datafile(".", __name__, "data/atnt")
)
Amir Mohammadi's avatar
Amir Mohammadi committed
46

47
48
49
50
51
52
atnt_protocol_path_cross_validation = os.path.join(
    os.path.realpath(
        bob.io.base.test_utils.datafile(".", __name__, "data/atnt/cross_validation/")
    ),
    "metadata.csv",
)
53

Amir Mohammadi's avatar
Amir Mohammadi committed
54

55
56
57
58
59
def check_all_true(list_of_something, something):
    """
    Assert if list of `Something` contains `Something`
    """
    return np.alltrue([isinstance(s, something) for s in list_of_something])
Amir Mohammadi's avatar
Amir Mohammadi committed
60
61


62
def test_csv_file_list_dev_only():
Amir Mohammadi's avatar
Amir Mohammadi committed
63

64
65
66
    dataset = CSVDatasetDevEval(example_dir, "protocol_only_dev")
    assert len(dataset.background_model_samples()) == 8
    assert check_all_true(dataset.background_model_samples(), DelayedSample)
Amir Mohammadi's avatar
Amir Mohammadi committed
67

68
69
    assert len(dataset.references()) == 2
    assert check_all_true(dataset.references(), SampleSet)
Amir Mohammadi's avatar
Amir Mohammadi committed
70

71
72
    assert len(dataset.probes()) == 10
    assert check_all_true(dataset.probes(), SampleSet)
Amir Mohammadi's avatar
Amir Mohammadi committed
73
74


75
def test_csv_file_list_dev_only_metadata():
Amir Mohammadi's avatar
Amir Mohammadi committed
76

77
78
    dataset = CSVDatasetDevEval(example_dir, "protocol_only_dev_metadata")
    assert len(dataset.background_model_samples()) == 8
79

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
80
81
    assert check_all_true(dataset.background_model_samples(), DelayedSample)
    assert np.alltrue(
82
        ["metadata_1" in s.__dict__ for s in dataset.background_model_samples()]
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
83
84
    )
    assert np.alltrue(
85
        ["metadata_2" in s.__dict__ for s in dataset.background_model_samples()]
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
86
    )
Amir Mohammadi's avatar
Amir Mohammadi committed
87

88
89
    assert len(dataset.references()) == 2
    assert check_all_true(dataset.references(), SampleSet)
90
91
    assert np.alltrue(["metadata_1" in s.__dict__ for s in dataset.references()])
    assert np.alltrue(["metadata_2" in s.__dict__ for s in dataset.references()])
Amir Mohammadi's avatar
Amir Mohammadi committed
92

93
94
    assert len(dataset.probes()) == 10
    assert check_all_true(dataset.probes(), SampleSet)
95
96
    assert np.alltrue(["metadata_1" in s.__dict__ for s in dataset.probes()])
    assert np.alltrue(["metadata_2" in s.__dict__ for s in dataset.probes()])
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
97
    assert np.alltrue(["references" in s.__dict__ for s in dataset.probes()])
Amir Mohammadi's avatar
Amir Mohammadi committed
98
99


100
def test_csv_file_list_dev_eval():
Amir Mohammadi's avatar
Amir Mohammadi committed
101

102
103
104
105
106
107
108
109
110
111
112
    annotation_directory = os.path.realpath(
        bob.io.base.test_utils.datafile(
            ".", __name__, "data/example_csv_filelist/annotations"
        )
    )

    dataset = CSVDatasetDevEval(
        example_dir,
        "protocol_dev_eval",
        csv_to_sample_loader=CSVToSampleLoader(
            data_loader=bob.io.base.load,
113
            metadata_loader=AnnotationsLoader(
114
115
116
                annotation_directory=annotation_directory,
                annotation_extension=".pos",
                annotation_type="eyecenter",
117
118
119
120
121
            ),
            dataset_original_directory="",
            extension="",
        ),
    )
122
123
    assert len(dataset.background_model_samples()) == 8
    assert check_all_true(dataset.background_model_samples(), DelayedSample)
Amir Mohammadi's avatar
Amir Mohammadi committed
124

125
126
    assert len(dataset.references()) == 2
    assert check_all_true(dataset.references(), SampleSet)
Amir Mohammadi's avatar
Amir Mohammadi committed
127

128
    assert len(dataset.probes()) == 8
129
    assert check_all_true(dataset.references(), SampleSet)
Amir Mohammadi's avatar
Amir Mohammadi committed
130

131
132
    assert len(dataset.references(group="eval")) == 6
    assert check_all_true(dataset.references(group="eval"), SampleSet)
Amir Mohammadi's avatar
Amir Mohammadi committed
133

134
135
    assert len(dataset.probes(group="eval")) == 13
    assert check_all_true(dataset.probes(group="eval"), SampleSet)
Amir Mohammadi's avatar
Amir Mohammadi committed
136

137
138
139
140
141
142
143
144
145
146
147
148
149
    assert len(dataset.all_samples(groups=None)) == 47
    assert check_all_true(dataset.all_samples(groups=None), DelayedSample)

    # Check the annotations
    for s in dataset.all_samples(groups=None):
        assert isinstance(s.annotations, dict)

    assert len(dataset.reference_ids(group="dev")) == 2
    assert len(dataset.reference_ids(group="eval")) == 6

    assert len(dataset.groups()) == 3


150
151
152
153
154
155
156
157
158
159
160
161
162
163
def test_csv_file_list_dev_eval_sparse():

    annotation_directory = os.path.realpath(
        bob.io.base.test_utils.datafile(
            ".", __name__, "data/example_csv_filelist/annotations"
        )
    )

    dataset = CSVDatasetDevEval(
        example_dir,
        "protocol_dev_eval_sparse",
        csv_to_sample_loader=CSVToSampleLoader(
            data_loader=bob.io.base.load,
            metadata_loader=AnnotationsLoader(
164
165
166
                annotation_directory=annotation_directory,
                annotation_extension=".pos",
                annotation_type="eyecenter",
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
            ),
            dataset_original_directory="",
            extension="",
        ),
        is_sparse=True,
    )

    assert len(dataset.background_model_samples()) == 8
    assert check_all_true(dataset.background_model_samples(), DelayedSample)

    assert len(dataset.references()) == 2
    assert check_all_true(dataset.references(), SampleSet)

    probes = dataset.probes()
    assert len(probes) == 8

    # here, 1 comparisons comparison per probe
    for p in probes:
        assert len(p.references) == 1
    assert check_all_true(dataset.references(), SampleSet)

    assert len(dataset.references(group="eval")) == 6
    assert check_all_true(dataset.references(group="eval"), SampleSet)

    probes = dataset.probes(group="eval")
    assert len(probes) == 13
    assert check_all_true(probes, SampleSet)
    # Here, 1 comparison per probe, EXPECT THE FIRST ONE
    for i, p in enumerate(probes):
        if i == 0:
            assert len(p.references) == 2
        else:
            assert len(p.references) == 1

    assert len(dataset.all_samples(groups=None)) == 48
    assert check_all_true(dataset.all_samples(groups=None), DelayedSample)

    # Check the annotations
    for s in dataset.all_samples(groups=None):
        assert isinstance(s.annotations, dict)

    assert len(dataset.reference_ids(group="dev")) == 2
    assert len(dataset.reference_ids(group="eval")) == 6

    assert len(dataset.groups()) == 3


214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
def test_lst_file_list_dev_eval():

    dataset = CSVDatasetDevEval(
        legacy_example_dir,
        "",
        csv_to_sample_loader=LSTToSampleLoader(
            data_loader=bob.io.base.load, dataset_original_directory="", extension="",
        ),
    )

    assert len(dataset.background_model_samples()) == 8

    assert check_all_true(dataset.background_model_samples(), DelayedSample)

    assert len(dataset.references()) == 2
    assert check_all_true(dataset.references(), SampleSet)

    assert len(dataset.probes()) == 10
    assert check_all_true(dataset.references(), SampleSet)

    assert len(dataset.references(group="eval")) == 2
    assert check_all_true(dataset.references(group="eval"), SampleSet)

    assert len(dataset.probes(group="eval")) == 8
    assert check_all_true(dataset.probes(group="eval"), SampleSet)

    assert len(dataset.all_samples(groups=None)) == 42
241
242
    assert check_all_true(dataset.all_samples(groups=None), DelayedSample)

243
244
245
246
247
    assert len(dataset.reference_ids(group="dev")) == 2
    assert len(dataset.reference_ids(group="eval")) == 2

    assert len(dataset.groups()) == 3

Amir Mohammadi's avatar
Amir Mohammadi committed
248

249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
def test_lst_file_list_dev_eval_sparse():

    dataset = CSVDatasetDevEval(
        legacy_example_dir,
        "",
        csv_to_sample_loader=LSTToSampleLoader(
            data_loader=bob.io.base.load, dataset_original_directory="", extension="",
        ),
        is_sparse=True,
    )

    assert len(dataset.background_model_samples()) == 8

    assert check_all_true(dataset.background_model_samples(), DelayedSample)

    assert len(dataset.references()) == 2
    assert check_all_true(dataset.references(), SampleSet)

    assert len(dataset.probes()) == 8
    assert check_all_true(dataset.references(), SampleSet)

    assert len(dataset.references(group="eval")) == 2
    assert check_all_true(dataset.references(group="eval"), SampleSet)

    assert len(dataset.probes(group="eval")) == 8
    assert check_all_true(dataset.probes(group="eval"), SampleSet)

    assert len(dataset.all_samples(groups=None)) == 44
    assert check_all_true(dataset.all_samples(groups=None), DelayedSample)

    assert len(dataset.reference_ids(group="dev")) == 2
    assert len(dataset.reference_ids(group="eval")) == 2

    assert len(dataset.groups()) == 3


def test_lst_file_list_dev_sparse_filelist2():

    dataset = CSVDatasetDevEval(
        legacy2_example_dir,
        "",
        csv_to_sample_loader=LSTToSampleLoader(
            data_loader=bob.io.base.load, dataset_original_directory="", extension="",
        ),
        is_sparse=True,
    )

    assert len(dataset.references()) == 3
    assert check_all_true(dataset.references(), SampleSet)

    assert len(dataset.probes()) == 9
    assert check_all_true(dataset.references(), SampleSet)


303
def test_csv_file_list_atnt():
Amir Mohammadi's avatar
Amir Mohammadi committed
304

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
305
    dataset = CSVDatasetDevEval(atnt_protocol_path, "idiap_protocol")
306
307
308
    assert len(dataset.background_model_samples()) == 200
    assert len(dataset.references()) == 20
    assert len(dataset.probes()) == 100
309
310
    assert len(dataset.all_samples(groups=["train"])) == 200
    assert len(dataset.all_samples(groups=["dev"])) == 200
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
    assert len(dataset.all_samples(groups=None)) == 400


def data_loader(path):
    import bob.io.image

    return bob.io.base.load(path)


def test_csv_cross_validation_atnt():

    dataset = CSVDatasetCrossValidation(
        csv_file_name=atnt_protocol_path_cross_validation,
        random_state=0,
        test_size=0.8,
        csv_to_sample_loader=CSVToSampleLoader(
            data_loader=data_loader,
            dataset_original_directory=atnt_database_directory(),
            extension=".pgm",
        ),
    )
    assert len(dataset.background_model_samples()) == 80
    assert len(dataset.references("dev")) == 32
    assert len(dataset.probes("dev")) == 288
    assert len(dataset.all_samples(groups=None)) == 400
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
336
337


338
def run_experiment(dataset):
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
339
340
341
342
343
344
345
346
347
    def linearize(X):
        X = np.asarray(X)
        return np.reshape(X, (X.shape[0], -1))

    #### Testing it in a real recognition systems
    transformer = wrap(["sample"], make_pipeline(FunctionTransformer(linearize)))

    vanilla_biometrics_pipeline = VanillaBiometricsPipeline(transformer, Distance())

348
    return vanilla_biometrics_pipeline(
349
        dataset.background_model_samples(), dataset.references(), dataset.probes(),
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
350
351
    )

352
353
354
355
356
357
358
359
360
361
362
363
364
365

def test_atnt_experiment():

    dataset = CSVDatasetDevEval(
        dataset_protocol_path=atnt_protocol_path,
        protocol_name="idiap_protocol",
        csv_to_sample_loader=CSVToSampleLoader(
            data_loader=data_loader,
            dataset_original_directory=atnt_database_directory(),
            extension=".pgm",
        ),
    )

    scores = run_experiment(dataset)
366
367
    assert len(scores) == 100
    assert np.alltrue([len(s) == 20] for s in scores)
368
369
370
371
372
373
374


def test_atnt_experiment_cross_validation():

    samples_per_identity = 10
    total_identities = 40
    samples_for_enrollment = 1
375

376
    def run_cross_validation_experiment(test_size=0.9):
377
378
379
380
381
382
383
384
385
386
387
388
        dataset = CSVDatasetCrossValidation(
            csv_file_name=atnt_protocol_path_cross_validation,
            random_state=0,
            test_size=test_size,
            csv_to_sample_loader=CSVToSampleLoader(
                data_loader=data_loader,
                dataset_original_directory=atnt_database_directory(),
                extension=".pgm",
            ),
        )

        scores = run_experiment(dataset)
389
390
391
392
393
394
        assert len(scores) == int(
            total_identities
            * test_size
            * (samples_per_identity - samples_for_enrollment)
        )

395
396
397
    run_cross_validation_experiment(test_size=0.9)
    run_cross_validation_experiment(test_size=0.8)
    run_cross_validation_experiment(test_size=0.5)
398
399
400
401
402
403


####
# Testing the Legacy file list
####

404

405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
def test_query():
    db = FileListBioDatabase(
        legacy_example_dir, "test", use_dense_probe_file_list=False
    )

    assert (
        len(db.groups()) == 5
    )  # 5 groups (dev, eval, world, optional_world_1, optional_world_2)

    assert len(db.client_ids()) == 6  # 6 client ids for world, dev and eval
    assert len(db.client_ids(groups="world")) == 2  # 2 client ids for world
    assert (
        len(db.client_ids(groups="optional_world_1")) == 2
    )  # 2 client ids for optional world 1
    assert (
        len(db.client_ids(groups="optional_world_2")) == 2
    )  # 2 client ids for optional world 2
    assert len(db.client_ids(groups="dev")) == 2  # 2 client ids for dev
    assert len(db.client_ids(groups="eval")) == 2  # 2 client ids for eval

    assert len(db.tclient_ids()) == 2  # 2 client ids for T-Norm score normalization
    assert len(db.zclient_ids()) == 2  # 2 client ids for Z-Norm score normalization

    assert len(db.model_ids_with_protocol()) == 6  # 6 model ids for world, dev and eval
    assert len(db.model_ids_with_protocol(groups="world")) == 2  # 2 model ids for world
    assert (
        len(db.model_ids_with_protocol(groups="optional_world_1")) == 2
    )  # 2 model ids for optional world 1
    assert (
        len(db.model_ids_with_protocol(groups="optional_world_2")) == 2
    )  # 2 model ids for optional world 2
    assert len(db.model_ids_with_protocol(groups="dev")) == 2  # 2 model ids for dev
    assert len(db.model_ids_with_protocol(groups="eval")) == 2  # 2 model ids for eval

    assert (
        len(db.tmodel_ids_with_protocol()) == 2
    )  # 2 model ids for T-Norm score normalization

    assert len(db.objects(groups="world")) == 8  # 8 samples in the world set

    assert (
        len(db.objects(groups="dev", purposes="enroll")) == 8
    )  # 8 samples for enrollment in the dev set
    assert (
        len(db.objects(groups="dev", purposes="enroll", model_ids="3")) == 4
    )  # 4 samples for to enroll model '3' in the dev set
    assert (
        len(db.objects(groups="dev", purposes="enroll", model_ids="7")) == 0
    )  # 0 samples for enrolling model '7' (it is a T-Norm model)
    assert (
        len(db.objects(groups="dev", purposes="probe")) == 8
    )  # 8 samples as probes in the dev set
    assert (
        len(db.objects(groups="dev", purposes="probe", classes="client")) == 8
    )  # 8 samples as client probes in the dev set
    assert (
        len(db.objects(groups="dev", purposes="probe", classes="impostor")) == 4
    )  # 4 samples as impostor probes in the dev set

    assert len(db.tobjects(groups="dev")) == 8  # 8 samples for enrolling T-norm models
    assert (
        len(db.tobjects(groups="dev", model_ids="7")) == 4
    )  # 4 samples for enrolling T-norm model '7'
    assert (
        len(db.tobjects(groups="dev", model_ids="3")) == 0
    )  # 0 samples for enrolling T-norm model '3' (no T-Norm model)
    assert len(db.zobjects(groups="dev")) == 8  # 8 samples for Z-norm impostor accesses

    assert db.client_id_from_model_id("1", group=None) == "1"
    assert db.client_id_from_model_id("3", group=None) == "3"
    assert db.client_id_from_model_id("6", group=None) == "6"
    assert db.client_id_from_t_model_id("7", group=None) == "7"


def test_query_protocol():
    db = FileListBioDatabase(
        os.path.dirname(legacy_example_dir),
        "test",
        protocol="example_filelist",
        use_dense_probe_file_list=False,
    )

    assert (
        len(db.groups()) == 5
    )  # 5 groups (dev, eval, world, optional_world_1, optional_world_2)

    assert len(db.client_ids()) == 6  # 6 client ids for world, dev and eval
    assert len(db.client_ids(groups="world",)) == 2  # 2 client ids for world
    assert (
        len(db.client_ids(groups="optional_world_1",)) == 2
    )  # 2 client ids for optional world 1
    assert (
        len(db.client_ids(groups="optional_world_2",)) == 2
    )  # 2 client ids for optional world 2
    assert len(db.client_ids(groups="dev",)) == 2  # 2 client ids for dev
    assert len(db.client_ids(groups="eval",)) == 2  # 2 client ids for eval

    assert len(db.tclient_ids()) == 2  # 2 client ids for T-Norm score normalization
    assert len(db.zclient_ids()) == 2  # 2 client ids for Z-Norm score normalization

    assert len(db.model_ids_with_protocol()) == 6  # 6 model ids for world, dev and eval
    assert (
        len(db.model_ids_with_protocol(groups="world",)) == 2
    )  # 2 model ids for world
    assert (
        len(db.model_ids_with_protocol(groups="optional_world_1",)) == 2
    )  # 2 model ids for optional world 1
    assert (
        len(db.model_ids_with_protocol(groups="optional_world_2",)) == 2
    )  # 2 model ids for optional world 2
    assert len(db.model_ids_with_protocol(groups="dev",)) == 2  # 2 model ids for dev
    assert len(db.model_ids_with_protocol(groups="eval",)) == 2  # 2 model ids for eval

    assert (
        len(db.tmodel_ids_with_protocol()) == 2
    )  # 2 model ids for T-Norm score normalization

    assert len(db.objects(groups="world",)) == 8  # 8 samples in the world set

    assert (
        len(db.objects(groups="dev", purposes="enroll",)) == 8
    )  # 8 samples for enrollment in the dev set
    assert (
        len(db.objects(groups="dev", purposes="enroll", model_ids="3",)) == 4
    )  # 4 samples for to enroll model '3' in the dev set
    assert (
        len(db.objects(groups="dev", purposes="enroll", model_ids="7",)) == 0
    )  # 0 samples for enrolling model '7' (it is a T-Norm model)
    assert (
        len(db.objects(groups="dev", purposes="probe",)) == 8
    )  # 8 samples as probes in the dev set
    assert (
        len(db.objects(groups="dev", purposes="probe", classes="client",)) == 8
    )  # 8 samples as client probes in the dev set
    assert (
        len(db.objects(groups="dev", purposes="probe", classes="impostor",)) == 4
    )  # 4 samples as impostor probes in the dev set

    assert len(db.tobjects(groups="dev",)) == 8  # 8 samples for enrolling T-norm models
    assert (
        len(db.tobjects(groups="dev", model_ids="7",)) == 4
    )  # 4 samples for enrolling T-norm model '7'
    assert (
        len(db.tobjects(groups="dev", model_ids="3",)) == 0
    )  # 0 samples for enrolling T-norm model '3' (no T-Norm model)
    assert len(db.zobjects(groups="dev")) == 8  # 8 samples for Z-norm impostor accesses

    assert db.client_id_from_model_id("1", group=None) == "1"
    assert db.client_id_from_model_id("3", group=None) == "3"
    assert db.client_id_from_model_id("6", group=None) == "6"
    assert db.client_id_from_t_model_id("7", group=None) == "7"

    # check other protocols
    assert len(db.objects(protocol="non-existent")) == 0

    prot = "example_filelist2"
    assert (
        len(db.model_ids_with_protocol(protocol=prot)) == 3
    )  # 3 model ids for dev only
    nose.tools.assert_raises(
        ValueError, db.model_ids_with_protocol, protocol=prot, groups="eval"
    )  # eval does not exist for this protocol
    assert len(db.objects(protocol=prot, groups="dev", purposes="enroll")) == 12
    assert len(db.objects(protocol=prot, groups="dev", purposes="probe")) == 9


def test_noztnorm():
    db = FileListBioDatabase(
        os.path.join(os.path.dirname(legacy_example_dir), "example_filelist2"), "test"
    )
    assert len(db.all_files())


def test_query_dense():
    db = FileListBioDatabase(legacy_example_dir, "test", use_dense_probe_file_list=True)

    assert len(db.objects(groups="world")) == 8  # 8 samples in the world set

    assert (
        len(db.objects(groups="dev", purposes="enroll")) == 8
    )  # 8 samples for enrollment in the dev set
    assert (
        len(db.objects(groups="dev", purposes="probe")) == 8
    )  # 8 samples as probes in the dev set


def test_annotation():
    db = FileListBioDatabase(
        legacy_example_dir,
        "test",
        use_dense_probe_file_list=False,
        annotation_directory=legacy_example_dir,
        annotation_type="named",
    )
    f = [o for o in db.objects() if o.path == "data/model4_session1_sample2"][0]
    annots = db.annotations(f)

    assert annots is not None
    assert "key1" in annots
    assert "key2" in annots
    assert annots["key1"] == (20, 10)
    assert annots["key2"] == (40, 30)


def test_multiple_extensions():
    # check that the old behavior still works
    db = FileListBioDatabase(
        legacy_example_dir,
        "test",
        use_dense_probe_file_list=False,
        original_directory=legacy_example_dir,
        original_extension=".pos",
    )
    file = bob.bio.base.database.BioFile(
        4, "data/model4_session1_sample2", "data/model4_session1_sample2"
    )
    file_name = db.original_file_name(file, True)
    assert file_name == os.path.join(legacy_example_dir, file.path + ".pos")

    # check that the new behavior works as well
    db = FileListBioDatabase(
        legacy_example_dir,
        "test",
        use_dense_probe_file_list=False,
        original_directory=legacy_example_dir,
        original_extension=[".jpg", ".pos"],
    )
    file_name = db.original_file_name(file)
    assert file_name == os.path.join(legacy_example_dir, file.path + ".pos")

    file = bob.bio.base.database.BioFile(
        4, "data/model4_session1_sample1", "data/model4_session1_sample1"
    )
    nose.tools.assert_raises(IOError, db.original_file_name, file, False)


def test_driver_api():
    from bob.db.base.script.dbmanage import main

    assert (
        main(
            (
                "bio_filelist dumplist --list-directory=%s --self-test"
                % legacy_example_dir
            ).split()
        )
        == 0
    )
    assert (
        main(
            (
                "bio_filelist dumplist --list-directory=%s --purpose=enroll --group=dev --class=client --self-test"
                % legacy_example_dir
            ).split()
        )
        == 0
    )
    assert (
        main(
            (
                "bio_filelist checkfiles --list-directory=%s --self-test"
                % legacy_example_dir
            ).split()
        )
        == 0
    )
671