test_algorithm.py 47 KB
Newer Older
Philip ABBET's avatar
Philip ABBET committed
1
2
3
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

Samuel GAIST's avatar
Samuel GAIST committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
###################################################################################
#                                                                                 #
# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/               #
# Contact: beat.support@idiap.ch                                                  #
#                                                                                 #
# Redistribution and use in source and binary forms, with or without              #
# modification, are permitted provided that the following conditions are met:     #
#                                                                                 #
# 1. Redistributions of source code must retain the above copyright notice, this  #
# list of conditions and the following disclaimer.                                #
#                                                                                 #
# 2. Redistributions in binary form must reproduce the above copyright notice,    #
# this list of conditions and the following disclaimer in the documentation       #
# and/or other materials provided with the distribution.                          #
#                                                                                 #
# 3. Neither the name of the copyright holder nor the names of its contributors   #
# may be used to endorse or promote products derived from this software without   #
# specific prior written permission.                                              #
#                                                                                 #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   #
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE          #
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE    #
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL      #
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR      #
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER      #
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,   #
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   #
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.            #
#                                                                                 #
###################################################################################
Philip ABBET's avatar
Philip ABBET committed
35
36
37


import unittest
38
39
40
41
import six
import os
import glob
import tempfile
42
43
import logging

44
import numpy as np
Philip ABBET's avatar
Philip ABBET committed
45
46

from ..algorithm import Algorithm
47
48
49
50
51
52
53
54
55
56
57
58
59
from ..data_loaders import DataLoaderList
from ..data_loaders import DataLoader
from ..dataformat import DataFormat
from ..data import CachedDataSink
from ..data import CachedDataSource
from ..inputs import Input
from ..inputs import InputGroup
from ..inputs import InputList
from ..outputs import Output
from ..outputs import OutputList
from ..outputs import SynchronizationListener

from .mocks import MockDataSink
60
61
from .mocks import MockLoggingHandler

Philip ABBET's avatar
Philip ABBET committed
62
63
64
from . import prefix


65
# ----------------------------------------------------------
Philip ABBET's avatar
Philip ABBET committed
66
67
68
69


class TestLegacyAPI_Loading(unittest.TestCase):
    def test_load_unknown_algorithm(self):
70
        algorithm = Algorithm(prefix, "legacy/unknown/1")
Philip ABBET's avatar
Philip ABBET committed
71
        self.assertFalse(algorithm.valid)
72
        self.assertTrue(algorithm.errors[0].find("file not found") != -1)
Philip ABBET's avatar
Philip ABBET committed
73
74

    def test_raises_syntax_error(self):
75
        algorithm = Algorithm(prefix, "legacy/syntax_error/1")
Philip ABBET's avatar
Philip ABBET committed
76
77
78
        self.assertTrue(algorithm.valid)

        with self.assertRaises(SyntaxError):
79
            algorithm.runner()
Philip ABBET's avatar
Philip ABBET committed
80
81

    def test_raises_runtime_error(self):
82
        algorithm = Algorithm(prefix, "legacy/syntax_error/1")
Philip ABBET's avatar
Philip ABBET committed
83
84
85
        self.assertTrue(algorithm.valid)

        with self.assertRaises(RuntimeError):
86
            algorithm.runner(exc=RuntimeError)
Philip ABBET's avatar
Philip ABBET committed
87
88

    def test_missing_class_raises(self):
89
        algorithm = Algorithm(prefix, "legacy/no_class/1")
Philip ABBET's avatar
Philip ABBET committed
90
91
92
        self.assertTrue(algorithm.valid)

        with self.assertRaises(AttributeError):
93
            algorithm.runner()
Philip ABBET's avatar
Philip ABBET committed
94

95
96
97
98
99
100
101
    def test_duplicate_key_error(self):
        algorithm = Algorithm(prefix, "legacy/duplicate_key_error/1")
        self.assertFalse(algorithm.valid)
        self.assertNotEqual(
            algorithm.errors[0].find("Algorithm declaration file invalid"), -1
        )

Philip ABBET's avatar
Philip ABBET committed
102
    def test_load_valid_algorithm(self):
103
104
        algorithm = Algorithm(prefix, "legacy/valid_algorithm/1")
        self.assertEqual(algorithm.name, "legacy/valid_algorithm/1")
Philip ABBET's avatar
Philip ABBET committed
105
106
107
        self.assertTrue(algorithm.valid)

        self.assertFalse(algorithm.errors)
108
109
        self.assertFalse(algorithm.results)  # it is not an analyzer
        self.assertFalse(algorithm.parameters)  # does not contain parameters
Philip ABBET's avatar
Philip ABBET committed
110
111
112
113
114
115
116
        self.assertTrue(algorithm.input_map)
        self.assertTrue(algorithm.output_map)
        self.assertEqual(algorithm.schema_version, 1)
        self.assertEqual(algorithm.api_version, 1)
        self.assertEqual(algorithm.type, Algorithm.LEGACY)

        runnable = algorithm.runner()
117
        self.assertTrue(runnable.ready)  # no setup
118
        self.assertTrue(runnable.prepared)
Philip ABBET's avatar
Philip ABBET committed
119
120

    def test_load_valid_analyser(self):
121
122
        algorithm = Algorithm(prefix, "legacy/valid_analysis/1")
        self.assertEqual(algorithm.name, "legacy/valid_analysis/1")
Philip ABBET's avatar
Philip ABBET committed
123
124
125
        self.assertTrue(algorithm.valid)

        self.assertFalse(algorithm.errors)
126
127
        self.assertFalse(algorithm.output_map)  # it is an analyzer
        self.assertFalse(algorithm.parameters)  # does not contain parameters
Philip ABBET's avatar
Philip ABBET committed
128
129
130
131
132
133
134
135
        self.assertTrue(algorithm.input_map)
        self.assertTrue(algorithm.results)
        self.assertEqual(algorithm.schema_version, 1)
        self.assertEqual(algorithm.api_version, 1)
        self.assertEqual(algorithm.type, Algorithm.LEGACY)

        self.assertEqual(len(algorithm.results), 2)

136
137
138
139
        self.assertTrue("c" in algorithm.results)
        self.assertEqual(
            algorithm.results["c"], dict(type="plot/scatter/1", display=False)
        )
Philip ABBET's avatar
Philip ABBET committed
140

141
142
        self.assertTrue("v" in algorithm.results)
        self.assertEqual(algorithm.results["v"], dict(type="float32", display=True))
Philip ABBET's avatar
Philip ABBET committed
143
144

        runnable = algorithm.runner()
145
        self.assertTrue(runnable.ready)  # no setup
146
        self.assertTrue(runnable.prepared)
Philip ABBET's avatar
Philip ABBET committed
147
148

    def test_hash(self):
149
        algorithm = Algorithm(prefix, "legacy/valid_analysis/1")
Philip ABBET's avatar
Philip ABBET committed
150
151
152
        self.assertTrue(algorithm.valid)
        self.assertEqual(len(algorithm.hash()), 64)

153
        v2 = Algorithm(prefix, "legacy/valid_analysis/2")  # only doc changes
Philip ABBET's avatar
Philip ABBET committed
154
155
156
157
        self.assertTrue(algorithm.valid)
        self.assertEqual(algorithm.hash(), v2.hash())

    def test_no_description(self):
158
        algorithm = Algorithm(prefix, "legacy/no_doc/1")
Philip ABBET's avatar
Philip ABBET committed
159
160
161
162
        self.assertTrue(algorithm.valid)
        self.assertTrue(algorithm.description is None)
        self.assertTrue(algorithm.documentation is None)

163
        description = "This is my descriptor"
Philip ABBET's avatar
Philip ABBET committed
164
165
166
167
168
169
        algorithm.description = description

        self.assertTrue(isinstance(algorithm.description, six.string_types))
        self.assertEqual(algorithm.description, description)

    def test_description(self):
170
        algorithm = Algorithm(prefix, "legacy/valid_analysis/2")  # only doc changes
Philip ABBET's avatar
Philip ABBET committed
171
172
173
174
175
176
        self.assertTrue(algorithm.valid)
        self.assertTrue(isinstance(algorithm.description, six.string_types))
        self.assertTrue(len(algorithm.description) > 0)
        self.assertTrue(algorithm.documentation is None)

    def test_analyzer_format(self):
177
        algorithm_name = "legacy/valid_analysis/1"
Philip ABBET's avatar
Philip ABBET committed
178
179
180
181
182
183
        algorithm = Algorithm(prefix, algorithm_name)
        self.assertTrue(algorithm.valid)

        dataformat = algorithm.result_dataformat()
        self.assertTrue(dataformat.valid)

184
        self.assertEqual(dataformat.name, "analysis:" + algorithm_name)
Philip ABBET's avatar
Philip ABBET committed
185
186


187
# ----------------------------------------------------------
Philip ABBET's avatar
Philip ABBET committed
188
189
190
191


class TestSequentialAPI_Loading(unittest.TestCase):
    def test_load_valid_algorithm(self):
192
193
        algorithm = Algorithm(prefix, "sequential/valid_algorithm/1")
        self.assertEqual(algorithm.name, "sequential/valid_algorithm/1")
Philip ABBET's avatar
Philip ABBET committed
194
195
196
        self.assertTrue(algorithm.valid)

        self.assertFalse(algorithm.errors)
197
198
        self.assertFalse(algorithm.results)  # it is not an analyzer
        self.assertFalse(algorithm.parameters)  # does not contain parameters
Philip ABBET's avatar
Philip ABBET committed
199
200
201
202
203
204
205
206
        self.assertTrue(algorithm.input_map)
        self.assertTrue(algorithm.output_map)
        self.assertEqual(algorithm.schema_version, 2)
        self.assertEqual(algorithm.api_version, 2)
        self.assertEqual(algorithm.type, Algorithm.SEQUENTIAL)

        runnable = algorithm.runner()
        self.assertFalse(runnable.ready)
207
        self.assertFalse(runnable.prepared)
Philip ABBET's avatar
Philip ABBET committed
208
209

    def test_load_valid_analyser(self):
210
211
        algorithm = Algorithm(prefix, "sequential/valid_analysis/1")
        self.assertEqual(algorithm.name, "sequential/valid_analysis/1")
Philip ABBET's avatar
Philip ABBET committed
212
213
214
        self.assertTrue(algorithm.valid)

        self.assertFalse(algorithm.errors)
215
216
        self.assertFalse(algorithm.output_map)  # it is an analyzer
        self.assertFalse(algorithm.parameters)  # does not contain parameters
Philip ABBET's avatar
Philip ABBET committed
217
218
219
220
221
222
223
224
        self.assertTrue(algorithm.input_map)
        self.assertTrue(algorithm.results)
        self.assertEqual(algorithm.schema_version, 2)
        self.assertEqual(algorithm.api_version, 2)
        self.assertEqual(algorithm.type, Algorithm.SEQUENTIAL)

        self.assertEqual(len(algorithm.results), 2)

225
226
227
228
        self.assertTrue("c" in algorithm.results)
        self.assertEqual(
            algorithm.results["c"], dict(type="plot/scatter/1", display=False)
        )
Philip ABBET's avatar
Philip ABBET committed
229

230
231
        self.assertTrue("v" in algorithm.results)
        self.assertEqual(algorithm.results["v"], dict(type="float32", display=True))
Philip ABBET's avatar
Philip ABBET committed
232
233
234

        runnable = algorithm.runner()
        self.assertFalse(runnable.ready)
235
        self.assertFalse(runnable.prepared)
Philip ABBET's avatar
Philip ABBET committed
236

237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
    def test_load_valid_loop_user(self):
        algorithm = Algorithm(prefix, "sequential/loop_user/1")
        self.assertEqual(algorithm.name, "sequential/loop_user/1")
        self.assertTrue(algorithm.valid)

        self.assertFalse(algorithm.errors)
        self.assertFalse(algorithm.results)  # it is not an analyzer
        self.assertFalse(algorithm.parameters)  # does not contain parameters
        self.assertTrue(algorithm.input_map)
        self.assertTrue(algorithm.output_map)
        self.assertEqual(algorithm.schema_version, 3)
        self.assertEqual(algorithm.api_version, 2)
        self.assertEqual(algorithm.type, Algorithm.SEQUENTIAL_LOOP_USER)

        runnable = algorithm.runner()
        self.assertTrue(runnable.ready)
        self.assertTrue(runnable.prepared)

Philip ABBET's avatar
Philip ABBET committed
255

256
# ----------------------------------------------------------
Philip ABBET's avatar
Philip ABBET committed
257
258
259
260


class TestAutonomousAPI_Loading(unittest.TestCase):
    def test_load_valid_algorithm(self):
261
262
        algorithm = Algorithm(prefix, "autonomous/valid_algorithm/1")
        self.assertEqual(algorithm.name, "autonomous/valid_algorithm/1")
Philip ABBET's avatar
Philip ABBET committed
263
264
265
        self.assertTrue(algorithm.valid)

        self.assertFalse(algorithm.errors)
266
267
        self.assertFalse(algorithm.results)  # it is not an analyzer
        self.assertFalse(algorithm.parameters)  # does not contain parameters
Philip ABBET's avatar
Philip ABBET committed
268
269
270
271
272
273
274
275
        self.assertTrue(algorithm.input_map)
        self.assertTrue(algorithm.output_map)
        self.assertEqual(algorithm.schema_version, 2)
        self.assertEqual(algorithm.api_version, 2)
        self.assertEqual(algorithm.type, Algorithm.AUTONOMOUS)

        runnable = algorithm.runner()
        self.assertFalse(runnable.ready)
276
        self.assertFalse(runnable.prepared)
Philip ABBET's avatar
Philip ABBET committed
277
278

    def test_load_valid_analyser(self):
279
280
        algorithm = Algorithm(prefix, "autonomous/valid_analysis/1")
        self.assertEqual(algorithm.name, "autonomous/valid_analysis/1")
Philip ABBET's avatar
Philip ABBET committed
281
282
283
        self.assertTrue(algorithm.valid)

        self.assertFalse(algorithm.errors)
284
285
        self.assertFalse(algorithm.output_map)  # it is an analyzer
        self.assertFalse(algorithm.parameters)  # does not contain parameters
Philip ABBET's avatar
Philip ABBET committed
286
287
288
289
290
291
292
293
        self.assertTrue(algorithm.input_map)
        self.assertTrue(algorithm.results)
        self.assertEqual(algorithm.schema_version, 2)
        self.assertEqual(algorithm.api_version, 2)
        self.assertEqual(algorithm.type, Algorithm.AUTONOMOUS)

        self.assertEqual(len(algorithm.results), 2)

294
295
296
297
        self.assertTrue("c" in algorithm.results)
        self.assertEqual(
            algorithm.results["c"], dict(type="plot/scatter/1", display=False)
        )
Philip ABBET's avatar
Philip ABBET committed
298

299
300
        self.assertTrue("v" in algorithm.results)
        self.assertEqual(algorithm.results["v"], dict(type="float32", display=True))
Philip ABBET's avatar
Philip ABBET committed
301
302
303

        runnable = algorithm.runner()
        self.assertFalse(runnable.ready)
304
        self.assertFalse(runnable.prepared)
Philip ABBET's avatar
Philip ABBET committed
305

306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
    def test_load_valid_loop_user(self):
        algorithm = Algorithm(prefix, "autonomous/loop_user/1")
        self.assertEqual(algorithm.name, "autonomous/loop_user/1")
        self.assertTrue(algorithm.valid)

        self.assertFalse(algorithm.errors)
        self.assertFalse(algorithm.results)  # it is not an analyzer
        self.assertFalse(algorithm.parameters)  # does not contain parameters
        self.assertTrue(algorithm.input_map)
        self.assertTrue(algorithm.output_map)
        self.assertEqual(algorithm.schema_version, 3)
        self.assertEqual(algorithm.api_version, 2)
        self.assertEqual(algorithm.type, Algorithm.AUTONOMOUS_LOOP_USER)

        runnable = algorithm.runner()
        self.assertTrue(runnable.ready)
        self.assertTrue(runnable.prepared)

Philip ABBET's avatar
Philip ABBET committed
324

325
# ----------------------------------------------------------
Philip ABBET's avatar
Philip ABBET committed
326
327


328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
class TestLoopAPI_Loading(unittest.TestCase):
    def test_load_valid_algorithm(self):
        algorithm = Algorithm(prefix, "autonomous/loop/1")
        self.assertEqual(algorithm.name, "autonomous/loop/1")
        self.assertTrue(algorithm.valid)

        self.assertFalse(algorithm.errors)
        self.assertFalse(algorithm.results)  # it is not an analyzer
        self.assertTrue(algorithm.parameters)
        self.assertTrue(algorithm.input_map)
        self.assertTrue(algorithm.output_map)
        self.assertEqual(algorithm.schema_version, 3)
        self.assertEqual(algorithm.api_version, 2)
        self.assertEqual(algorithm.type, Algorithm.LOOP)

        runnable = algorithm.runner()
        self.assertFalse(runnable.ready)
        self.assertTrue(
            runnable.prepared
        )  # loop/1 has no prepare method so is prepared


# ----------------------------------------------------------


class TestLoopUserAPI_Loading(unittest.TestCase):
    def test_load_valid_algorithm(self):
        algorithm = Algorithm(prefix, "autonomous/loop_user/1")
        self.assertEqual(algorithm.name, "autonomous/loop_user/1")
        self.assertTrue(algorithm.valid)

        self.assertFalse(algorithm.errors)
        self.assertFalse(algorithm.results)  # it is not an analyzer
        self.assertFalse(algorithm.parameters)  # loop_user/1 has no parameter
        self.assertTrue(algorithm.input_map)
        self.assertTrue(algorithm.output_map)
        self.assertEqual(algorithm.schema_version, 3)
        self.assertEqual(algorithm.api_version, 2)
        self.assertEqual(algorithm.type, Algorithm.AUTONOMOUS_LOOP_USER)

        runnable = algorithm.runner()
        self.assertTrue(runnable.ready)  # loop_user/1 has no setup method so is ready
        self.assertTrue(
            runnable.prepared
        )  # loop/1 has no prepare method so is prepared


# ----------------------------------------------------------


Philip ABBET's avatar
Philip ABBET committed
378
class TestLegacyAPI_Setup(unittest.TestCase):
379
    def __init__(self, methodName="runTest"):
380
        super(TestLegacyAPI_Setup, self).__init__(methodName)
381
        self.username = "legacy"
382

Philip ABBET's avatar
Philip ABBET committed
383
    def test_setup_crashing_algorithm(self):
384
        algorithm = Algorithm(prefix, self.username + "/setup_crash/1")
Philip ABBET's avatar
Philip ABBET committed
385
386
387
388
389
390
391
392
393
        self.assertTrue(algorithm.valid)

        runnable = algorithm.runner()
        self.assertFalse(runnable.ready)

        with self.assertRaises(NameError):
            runnable.setup({})

    def test_setup_failing_algorithm(self):
394
        algorithm = Algorithm(prefix, self.username + "/setup_fail/1")
Philip ABBET's avatar
Philip ABBET committed
395
396
397
398
399
400
401
402
        self.assertTrue(algorithm.valid)

        runnable = algorithm.runner()
        self.assertFalse(runnable.ready)
        self.assertFalse(runnable.setup({}))
        self.assertFalse(runnable.ready)

    def test_setup_nonparametrized_algorithm(self):
403
        algorithm = Algorithm(prefix, self.username + "/no_setup/1")
Philip ABBET's avatar
Philip ABBET committed
404
405
406
407
408
409
410
411
        self.assertTrue(algorithm.valid)

        runnable = algorithm.runner()
        self.assertTrue(runnable.ready)
        self.assertTrue(runnable.setup({}))
        self.assertTrue(runnable.ready)

    def test_setup_parametrized_algorithm_default_values(self):
412
        algorithm = Algorithm(prefix, self.username + "/parametrized/1")
Philip ABBET's avatar
Philip ABBET committed
413
414
415
416
417
418
419
        self.assertTrue(algorithm.valid)

        runnable = algorithm.runner()
        self.assertFalse(runnable.ready)
        self.assertTrue(runnable.setup({}))
        self.assertTrue(runnable.ready)

420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
        self.assertEqual(runnable.int8_value, 5)
        self.assertEqual(runnable.int8_range, 6)
        self.assertEqual(runnable.int16_value, 500)
        self.assertEqual(runnable.int16_range, 300)
        self.assertEqual(runnable.int32_value, 500000)
        self.assertEqual(runnable.int32_range, 500000)
        self.assertEqual(runnable.int64_value, 5000000000)
        self.assertEqual(runnable.int64_range, 5000000000)
        self.assertEqual(runnable.uint8_value, 5)
        self.assertEqual(runnable.uint8_range, 6)
        self.assertEqual(runnable.uint16_value, 500)
        self.assertEqual(runnable.uint16_range, 500)
        self.assertEqual(runnable.uint32_value, 500000)
        self.assertEqual(runnable.uint32_range, 500000)
        self.assertEqual(runnable.uint64_value, 5000000000)
        self.assertEqual(runnable.uint64_range, 5000000000)
        self.assertEqual(runnable.float32_value, 0.0)
        self.assertEqual(runnable.float32_range, 0.0)
        self.assertEqual(runnable.float64_value, 0.0)
        self.assertEqual(runnable.float64_range, 0.0)
        self.assertEqual(runnable.bool_value, True)
        self.assertEqual(runnable.string_value, "choice1")
Philip ABBET's avatar
Philip ABBET committed
442
443

    def test_setup_parametrized_algorithm_customized_values(self):
444
        algorithm = Algorithm(prefix, self.username + "/parametrized/1")
Philip ABBET's avatar
Philip ABBET committed
445
446
447
448
449
        self.assertTrue(algorithm.valid)

        runnable = algorithm.runner()
        self.assertFalse(runnable.ready)

450
451
452
453
454
455
456
457
458
459
460
461
        self.assertTrue(
            runnable.setup(
                dict(
                    int8_value=10,
                    int8_range=10,
                    float32_value=5.0,
                    float32_range=5.0,
                    bool_value=False,
                    string_value="choice2",
                )
            )
        )
Philip ABBET's avatar
Philip ABBET committed
462
463
        self.assertTrue(runnable.ready)

464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
        self.assertEqual(runnable.int8_value, 10)
        self.assertEqual(runnable.int8_range, 10)
        self.assertEqual(runnable.int16_value, 500)
        self.assertEqual(runnable.int16_range, 300)
        self.assertEqual(runnable.int32_value, 500000)
        self.assertEqual(runnable.int32_range, 500000)
        self.assertEqual(runnable.int64_value, 5000000000)
        self.assertEqual(runnable.int64_range, 5000000000)
        self.assertEqual(runnable.uint8_value, 5)
        self.assertEqual(runnable.uint8_range, 6)
        self.assertEqual(runnable.uint16_value, 500)
        self.assertEqual(runnable.uint16_range, 500)
        self.assertEqual(runnable.uint32_value, 500000)
        self.assertEqual(runnable.uint32_range, 500000)
        self.assertEqual(runnable.uint64_value, 5000000000)
        self.assertEqual(runnable.uint64_range, 5000000000)
        self.assertEqual(runnable.float32_value, 5.0)
        self.assertEqual(runnable.float32_range, 5.0)
        self.assertEqual(runnable.float64_value, 0.0)
        self.assertEqual(runnable.float64_range, 0.0)
        self.assertEqual(runnable.bool_value, False)
        self.assertEqual(runnable.string_value, "choice2")
Philip ABBET's avatar
Philip ABBET committed
486
487

    def test_setup_parametrized_algorithm_out_of_range_value(self):
488
        algorithm = Algorithm(prefix, self.username + "/parametrized/1")
Philip ABBET's avatar
Philip ABBET committed
489
490
491
492
493
494
        self.assertTrue(algorithm.valid)

        runnable = algorithm.runner()
        self.assertFalse(runnable.ready)

        with self.assertRaises(ValueError):
495
            self.assertTrue(runnable.setup(dict(int8_range=100)))
Philip ABBET's avatar
Philip ABBET committed
496
497

    def test_setup_parametrized_algorithm_invalid_choice_value(self):
498
        algorithm = Algorithm(prefix, self.username + "/parametrized/1")
Philip ABBET's avatar
Philip ABBET committed
499
500
501
502
503
504
        self.assertTrue(algorithm.valid)

        runnable = algorithm.runner()
        self.assertFalse(runnable.ready)

        with self.assertRaises(ValueError):
505
            self.assertTrue(runnable.setup(dict(string_value="unknown")))
506
507


508
# ----------------------------------------------------------
509
510
511


class TestSequentialAPI_Setup(TestLegacyAPI_Setup):
512
    def __init__(self, methodName="runTest"):
513
        super(TestSequentialAPI_Setup, self).__init__(methodName)
514
        self.username = "sequential"
515
516


517
# ----------------------------------------------------------
518
519
520


class TestAutonomousAPI_Setup(TestLegacyAPI_Setup):
521
    def __init__(self, methodName="runTest"):
522
        super(TestAutonomousAPI_Setup, self).__init__(methodName)
523
        self.username = "sequential"
524
525


526
# ----------------------------------------------------------
527
528
529
530


class TestLegacyAPI_Prepare(unittest.TestCase):
    def test_always_prepared(self):
531
        algorithm = Algorithm(prefix, "legacy/valid_algorithm/1")
532
533
534
535
536
537
538
        self.assertTrue(algorithm.valid)

        runnable = algorithm.runner()
        self.assertTrue(runnable.prepared)
        self.assertTrue(runnable.prepare(DataLoaderList()))
        self.assertTrue(runnable.prepared)

539
    def test_prepare_warning(self):
540
        log_handler = MockLoggingHandler(level="DEBUG")
541
542
543
        logging.getLogger().addHandler(log_handler)
        log_messages = log_handler.messages

544
        algorithm = Algorithm(prefix, "legacy/prepare/1")
545
546
547
548
549
550
551
        self.assertTrue(algorithm.valid)

        runnable = algorithm.runner()
        self.assertTrue(runnable.prepared)
        self.assertTrue(runnable.prepare(DataLoaderList()))
        self.assertTrue(runnable.prepared)

552
        info_len = len(log_messages["warning"])
553
        self.assertEqual(info_len, 1)
554
555
556
557
        self.assertEqual(
            log_messages["warning"][info_len - 1],
            "Prepare is a reserved function name starting with API V2",
        )
558

559

560
# ----------------------------------------------------------
561
562


563
564
class TestSequentialAPI_Prepare(unittest.TestCase):
    def __init__(self, methodName="runTest"):
565
        super(TestSequentialAPI_Prepare, self).__init__(methodName)
566
        self.username = "sequential"
567
568

    def test_prepare_crashing_algorithm(self):
569
        algorithm = Algorithm(prefix, self.username + "/prepare_crash/1")
570
571
572
573
574
575
576
577
578
579
580
        self.assertTrue(algorithm.valid)

        runnable = algorithm.runner()
        self.assertTrue(runnable.ready)

        self.assertFalse(runnable.prepared)

        with self.assertRaises(NameError):
            runnable.prepare(DataLoaderList())

    def test_prepare_failing_algorithm(self):
581
        algorithm = Algorithm(prefix, self.username + "/prepare_fail/1")
582
583
584
585
586
587
588
589
590
591
        self.assertTrue(algorithm.valid)

        runnable = algorithm.runner()
        self.assertTrue(runnable.ready)

        self.assertFalse(runnable.prepared)
        self.assertFalse(runnable.prepare(DataLoaderList()))
        self.assertFalse(runnable.prepared)

    def test_no_preparation_algorithm(self):
592
        algorithm = Algorithm(prefix, self.username + "/no_prepare/1")
593
594
595
596
597
598
599
600
601
602
        self.assertTrue(algorithm.valid)

        runnable = algorithm.runner()
        self.assertTrue(runnable.ready)

        self.assertTrue(runnable.prepared)
        self.assertTrue(runnable.prepare(DataLoaderList()))
        self.assertTrue(runnable.prepared)


603
# ----------------------------------------------------------
604
605
606


class TestAutonomousAPI_Prepare(TestSequentialAPI_Prepare):
607
    def __init__(self, methodName="runTest"):
608
        super(TestAutonomousAPI_Prepare, self).__init__(methodName)
609
        self.username = "autonomous"
610
611


612
# ----------------------------------------------------------
613
614
615
616
617
618
619
620
621
622


class TestExecutionBase(unittest.TestCase):
    def setUp(self):
        self.filenames = {}

    def tearDown(self):
        for f in self.filenames.values():
            basename, ext = os.path.splitext(f)
            filenames = [f]
623
            filenames += glob.glob(basename + "*")
624
625
626
627
628
            for filename in filenames:
                if os.path.exists(filename):
                    os.unlink(filename)

    def writeData(self, input_name, indices, start_value):
629
630
631
632
        testfile = tempfile.NamedTemporaryFile(
            prefix=__name__ + input_name, suffix=".data"
        )
        testfile.close()  # preserve only the name
633
634
        self.filenames[input_name] = testfile.name

635
        dataformat = DataFormat(prefix, "user/single_integer/1")
636
637
638
        self.assertTrue(dataformat.valid)

        data_sink = CachedDataSink()
639
640
641
642
643
        self.assertTrue(
            data_sink.setup(
                self.filenames[input_name], dataformat, indices[0][0], indices[-1][1]
            )
        )
644
645
646
647
648
649
650
651
652
653
654
655
656
657

        for i in indices:
            data = dataformat.type()
            data.value = np.int32(start_value + i[0])
            data_sink.write(data, i[0], i[1])

        (nb_bytes, duration) = data_sink.statistics()
        self.assertTrue(nb_bytes > 0)
        self.assertTrue(duration > 0)

        data_sink.close()
        del data_sink


658
# ----------------------------------------------------------
659
660
661
662


class TestLegacyAPI_Process(TestExecutionBase):
    def create_io(self, infos):
663
        dataformat = DataFormat(prefix, "user/single_integer/1")
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678

        synchronization_listener = SynchronizationListener()

        # Inputs
        inputs = InputList()

        for group_name, group_inputs in infos.items():
            if len(inputs) == 0:
                group = InputGroup(group_name, synchronization_listener, True)
            else:
                group = InputGroup(group_name, None, False)

            inputs.add(group)

            for input_name in group_inputs:
679
                data_source = CachedDataSource()
680
681
682
683
684
685
686
                data_source.setup(self.filenames[input_name], prefix)
                group.add(Input(input_name, dataformat, data_source))

        # Outputs
        outputs = OutputList()

        data_sink = MockDataSink(dataformat)
687
        outputs.add(Output("out", data_sink, synchronization_listener))
688
689
690

        return (inputs, outputs, data_sink)

691
    def test_process_crashing_algorithm(self):
692
        algorithm = Algorithm(prefix, "legacy/process_crash/1")
693
694
695
696
697
698
699
700
        self.assertTrue(algorithm.valid)

        runnable = algorithm.runner()
        self.assertTrue(runnable.ready)

        with self.assertRaises(NameError):
            runnable.process(inputs=InputList(), outputs=OutputList())

701
    def test_one_group_of_one_input(self):
702
        self.writeData("in", [(0, 0), (1, 1), (2, 2), (3, 3)], 1000)
703

704
        (inputs, outputs, data_sink) = self.create_io({"group1": ["in"]})
705

706
        algorithm = Algorithm(prefix, "legacy/echo/1")
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
        runner = algorithm.runner()

        group = inputs.main_group

        while group.hasMoreData():
            group.restricted_access = False
            group.next()
            group.restricted_access = True
            self.assertTrue(runner.process(inputs=inputs, outputs=outputs))

        self.assertEqual(len(data_sink.written), 4)

        for i in range(0, 4):
            self.assertEqual(data_sink.written[i].start, i)
            self.assertEqual(data_sink.written[i].end, i)
            self.assertEqual(data_sink.written[i].data.value, 1000 + i)

    def test_one_group_of_two_inputs_1(self):
725
726
        self.writeData("in1", [(0, 0), (1, 1), (2, 2), (3, 3)], 1000)
        self.writeData("in2", [(0, 1), (2, 3)], 2000)
727

728
        (inputs, outputs, data_sink) = self.create_io({"group1": ["in1", "in2"]})
729

730
        algorithm = Algorithm(prefix, "legacy/add/1")
731
732
        runner = algorithm.runner()

733
        self.assertTrue(runner.setup({"sync": "in1"}))
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765

        group = inputs.main_group

        while group.hasMoreData():
            group.restricted_access = False
            group.next()
            group.restricted_access = True
            self.assertTrue(runner.process(inputs=inputs, outputs=outputs))

        self.assertEqual(len(data_sink.written), 4)

        data_unit = data_sink.written[0]
        self.assertEqual(data_unit.start, 0)
        self.assertEqual(data_unit.end, 0)
        self.assertEqual(data_unit.data.value, 3000)

        data_unit = data_sink.written[1]
        self.assertEqual(data_unit.start, 1)
        self.assertEqual(data_unit.end, 1)
        self.assertEqual(data_unit.data.value, 3001)

        data_unit = data_sink.written[2]
        self.assertEqual(data_unit.start, 2)
        self.assertEqual(data_unit.end, 2)
        self.assertEqual(data_unit.data.value, 3004)

        data_unit = data_sink.written[3]
        self.assertEqual(data_unit.start, 3)
        self.assertEqual(data_unit.end, 3)
        self.assertEqual(data_unit.data.value, 3005)

    def test_one_group_of_two_inputs_2(self):
766
767
        self.writeData("in1", [(0, 0), (1, 1), (2, 2), (3, 3)], 1000)
        self.writeData("in2", [(0, 1), (2, 3)], 2000)
768

769
        (inputs, outputs, data_sink) = self.create_io({"group1": ["in1", "in2"]})
770

771
        algorithm = Algorithm(prefix, "legacy/add/1")
772
773
        runner = algorithm.runner()

774
        self.assertTrue(runner.setup({"sync": "in2"}))
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796

        group = inputs.main_group

        while group.hasMoreData():
            group.restricted_access = False
            group.next()
            group.restricted_access = True
            self.assertTrue(runner.process(inputs=inputs, outputs=outputs))

        self.assertEqual(len(data_sink.written), 2)

        data_unit = data_sink.written[0]
        self.assertEqual(data_unit.start, 0)
        self.assertEqual(data_unit.end, 1)
        self.assertEqual(data_unit.data.value, 3001)

        data_unit = data_sink.written[1]
        self.assertEqual(data_unit.start, 2)
        self.assertEqual(data_unit.end, 3)
        self.assertEqual(data_unit.data.value, 3005)

    def test_two_groups_of_one_input(self):
797
798
799
800
801
802
803
804
        self.writeData("in1", [(0, 0), (1, 1), (2, 2), (3, 3)], 1000)
        self.writeData("in2", [(0, 3)], 2000)

        (inputs, outputs, data_sink) = self.create_io(
            {"group1": ["in1"], "group2": ["in2"]}
        )

        algorithm = Algorithm(prefix, "legacy/add2/1")
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
        runner = algorithm.runner()

        group = inputs.main_group

        while group.hasMoreData():
            group.restricted_access = False
            group.next()
            group.restricted_access = True
            self.assertTrue(runner.process(inputs=inputs, outputs=outputs))

        self.assertEqual(len(data_sink.written), 4)

        data_unit = data_sink.written[0]
        self.assertEqual(data_unit.start, 0)
        self.assertEqual(data_unit.end, 0)
        self.assertEqual(data_unit.data.value, 3000)

        data_unit = data_sink.written[1]
        self.assertEqual(data_unit.start, 1)
        self.assertEqual(data_unit.end, 1)
        self.assertEqual(data_unit.data.value, 3001)

        data_unit = data_sink.written[2]
        self.assertEqual(data_unit.start, 2)
        self.assertEqual(data_unit.end, 2)
        self.assertEqual(data_unit.data.value, 3002)

        data_unit = data_sink.written[3]
        self.assertEqual(data_unit.start, 3)
        self.assertEqual(data_unit.end, 3)
        self.assertEqual(data_unit.data.value, 3003)


838
# ----------------------------------------------------------
839
840
841
842


class TestSequentialAPI_Process(TestExecutionBase):
    def create_io(self, infos):
843
        dataformat = DataFormat(prefix, "user/single_integer/1")
844
845
846
847
848
849
850
851
852
853
854
855

        synchronization_listener = SynchronizationListener()

        # Data loaders & inputs
        inputs = None
        data_loaders = DataLoaderList()

        for group_name, group_inputs in infos.items():
            if inputs is None:
                inputs = InputGroup(group_name, synchronization_listener, True)

                for input_name in group_inputs:
856
                    data_source = CachedDataSource()
857
858
859
860
861
862
863
864
                    data_source.setup(self.filenames[input_name], prefix)
                    inputs.add(Input(input_name, dataformat, data_source))

            else:
                data_loader = DataLoader(group_name)
                data_loaders.add(data_loader)

                for input_name in group_inputs:
865
                    cached_file = CachedDataSource()
866
867
868
869
870
871
872
                    cached_file.setup(self.filenames[input_name], prefix)
                    data_loader.add(input_name, cached_file)

        # Outputs
        outputs = OutputList()

        data_sink = MockDataSink(dataformat)
873
        outputs.add(Output("out", data_sink, synchronization_listener))
874
875
876

        return (data_loaders, inputs, outputs, data_sink)

877
    def test_process_crashing_algorithm(self):
878
        algorithm = Algorithm(prefix, "sequential/process_crash/1")
879
880
881
882
883
884
        self.assertTrue(algorithm.valid)

        runnable = algorithm.runner()
        self.assertTrue(runnable.ready)

        with self.assertRaises(NameError):
885
886
887
            runnable.process(
                inputs=InputList(), data_loaders=DataLoaderList(), outputs=OutputList()
            )
888

889
    def test_one_group_of_one_input(self):
890
        self.writeData("in", [(0, 0), (1, 1), (2, 2), (3, 3)], 1000)
891

892
        (data_loaders, inputs, outputs, data_sink) = self.create_io({"group1": ["in"]})
893

894
        algorithm = Algorithm(prefix, "sequential/echo/1")
895
896
897
898
899
900
901
902
        runner = algorithm.runner()

        self.assertTrue(runner.prepare(data_loaders=data_loaders))

        while inputs.hasMoreData():
            inputs.restricted_access = False
            inputs.next()
            inputs.restricted_access = True
903
904
905
906
907
            self.assertTrue(
                runner.process(
                    inputs=inputs, data_loaders=data_loaders, outputs=outputs
                )
            )
908
909
910
911
912
913
914
915
916

        self.assertEqual(len(data_sink.written), 4)

        for i in range(0, 4):
            self.assertEqual(data_sink.written[i].start, i)
            self.assertEqual(data_sink.written[i].end, i)
            self.assertEqual(data_sink.written[i].data.value, 1000 + i)

    def test_one_group_of_two_inputs_1(self):
917
918
        self.writeData("in1", [(0, 0), (1, 1), (2, 2), (3, 3)], 1000)
        self.writeData("in2", [(0, 1), (2, 3)], 2000)
919

920
921
922
        (data_loaders, inputs, outputs, data_sink) = self.create_io(
            {"group1": ["in1", "in2"]}
        )
923

924
        algorithm = Algorithm(prefix, "sequential/add/1")
925
926
        runner = algorithm.runner()

927
        self.assertTrue(runner.setup({"sync": "in1"}))
928
929
930
931
932
933
934

        self.assertTrue(runner.prepare(data_loaders=data_loaders))

        while inputs.hasMoreData():
            inputs.restricted_access = False
            inputs.next()
            inputs.restricted_access = True
935
936
937
938
939
            self.assertTrue(
                runner.process(
                    inputs=inputs, data_loaders=data_loaders, outputs=outputs
                )
            )
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963

        self.assertEqual(len(data_sink.written), 4)

        data_unit = data_sink.written[0]
        self.assertEqual(data_unit.start, 0)
        self.assertEqual(data_unit.end, 0)
        self.assertEqual(data_unit.data.value, 3000)

        data_unit = data_sink.written[1]
        self.assertEqual(data_unit.start, 1)
        self.assertEqual(data_unit.end, 1)
        self.assertEqual(data_unit.data.value, 3001)

        data_unit = data_sink.written[2]
        self.assertEqual(data_unit.start, 2)
        self.assertEqual(data_unit.end, 2)
        self.assertEqual(data_unit.data.value, 3004)

        data_unit = data_sink.written[3]
        self.assertEqual(data_unit.start, 3)
        self.assertEqual(data_unit.end, 3)
        self.assertEqual(data_unit.data.value, 3005)

    def test_one_group_of_two_inputs_2(self):
964
965
        self.writeData("in1", [(0, 0), (1, 1), (2, 2), (3, 3)], 1000)
        self.writeData("in2", [(0, 1), (2, 3)], 2000)
966

967
968
969
        (data_loaders, inputs, outputs, data_sink) = self.create_io(
            {"group1": ["in1", "in2"]}
        )
970

971
        algorithm = Algorithm(prefix, "sequential/add/1")
972
973
        runner = algorithm.runner()

974
        self.assertTrue(runner.setup({"sync": "in2"}))
975
976
977
978
979
980
981

        self.assertTrue(runner.prepare(data_loaders=data_loaders))

        while inputs.hasMoreData():
            inputs.restricted_access = False
            inputs.next()
            inputs.restricted_access = True
982
983
984
985
986
            self.assertTrue(
                runner.process(
                    inputs=inputs, data_loaders=data_loaders, outputs=outputs
                )
            )
987
988
989
990
991
992
993
994
995
996
997
998
999
1000

        self.assertEqual(len(data_sink.written), 2)

        data_unit = data_sink.written[0]
        self.assertEqual(data_unit.start, 0)
        self.assertEqual(data_unit.end, 1)
        self.assertEqual(data_unit.data.value, 3001)

        data_unit = data_sink.written[1]
        self.assertEqual(data_unit.start, 2)
        self.assertEqual(data_unit.end, 3)
        self.assertEqual(data_unit.data.value, 3005)

    def test_two_groups_of_one_input(self):