test_algorithm.py 43.5 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
# ----------------------------------------------------------
Philip ABBET's avatar
Philip ABBET committed
239
240
241
242


class TestAutonomousAPI_Loading(unittest.TestCase):
    def test_load_valid_algorithm(self):
243
244
        algorithm = Algorithm(prefix, "autonomous/valid_algorithm/1")
        self.assertEqual(algorithm.name, "autonomous/valid_algorithm/1")
Philip ABBET's avatar
Philip ABBET committed
245
246
247
        self.assertTrue(algorithm.valid)

        self.assertFalse(algorithm.errors)
248
249
        self.assertFalse(algorithm.results)  # it is not an analyzer
        self.assertFalse(algorithm.parameters)  # does not contain parameters
Philip ABBET's avatar
Philip ABBET committed
250
251
252
253
254
255
256
257
        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)
258
        self.assertFalse(runnable.prepared)
Philip ABBET's avatar
Philip ABBET committed
259
260

    def test_load_valid_analyser(self):
261
262
        algorithm = Algorithm(prefix, "autonomous/valid_analysis/1")
        self.assertEqual(algorithm.name, "autonomous/valid_analysis/1")
Philip ABBET's avatar
Philip ABBET committed
263
264
265
        self.assertTrue(algorithm.valid)

        self.assertFalse(algorithm.errors)
266
267
        self.assertFalse(algorithm.output_map)  # it is 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.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)

276
277
278
279
        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
280

281
282
        self.assertTrue("v" in algorithm.results)
        self.assertEqual(algorithm.results["v"], dict(type="float32", display=True))
Philip ABBET's avatar
Philip ABBET committed
283
284
285

        runnable = algorithm.runner()
        self.assertFalse(runnable.ready)
286
        self.assertFalse(runnable.prepared)
Philip ABBET's avatar
Philip ABBET committed
287
288


289
# ----------------------------------------------------------
Philip ABBET's avatar
Philip ABBET committed
290
291
292


class TestLegacyAPI_Setup(unittest.TestCase):
293
    def __init__(self, methodName="runTest"):
294
        super(TestLegacyAPI_Setup, self).__init__(methodName)
295
        self.username = "legacy"
296

Philip ABBET's avatar
Philip ABBET committed
297
    def test_setup_crashing_algorithm(self):
298
        algorithm = Algorithm(prefix, self.username + "/setup_crash/1")
Philip ABBET's avatar
Philip ABBET committed
299
300
301
302
303
304
305
306
307
        self.assertTrue(algorithm.valid)

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

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

    def test_setup_failing_algorithm(self):
308
        algorithm = Algorithm(prefix, self.username + "/setup_fail/1")
Philip ABBET's avatar
Philip ABBET committed
309
310
311
312
313
314
315
316
        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):
317
        algorithm = Algorithm(prefix, self.username + "/no_setup/1")
Philip ABBET's avatar
Philip ABBET committed
318
319
320
321
322
323
324
325
        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):
326
        algorithm = Algorithm(prefix, self.username + "/parametrized/1")
Philip ABBET's avatar
Philip ABBET committed
327
328
329
330
331
332
333
        self.assertTrue(algorithm.valid)

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

334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
        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
356
357

    def test_setup_parametrized_algorithm_customized_values(self):
358
        algorithm = Algorithm(prefix, self.username + "/parametrized/1")
Philip ABBET's avatar
Philip ABBET committed
359
360
361
362
363
        self.assertTrue(algorithm.valid)

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

364
365
366
367
368
369
370
371
372
373
374
375
        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
376
377
        self.assertTrue(runnable.ready)

378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
        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
400
401

    def test_setup_parametrized_algorithm_out_of_range_value(self):
402
        algorithm = Algorithm(prefix, self.username + "/parametrized/1")
Philip ABBET's avatar
Philip ABBET committed
403
404
405
406
407
408
        self.assertTrue(algorithm.valid)

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

        with self.assertRaises(ValueError):
409
            self.assertTrue(runnable.setup(dict(int8_range=100)))
Philip ABBET's avatar
Philip ABBET committed
410
411

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

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

        with self.assertRaises(ValueError):
419
            self.assertTrue(runnable.setup(dict(string_value="unknown")))
420
421


422
# ----------------------------------------------------------
423
424
425


class TestSequentialAPI_Setup(TestLegacyAPI_Setup):
426
    def __init__(self, methodName="runTest"):
427
        super(TestSequentialAPI_Setup, self).__init__(methodName)
428
        self.username = "sequential"
429
430


431
# ----------------------------------------------------------
432
433
434


class TestAutonomousAPI_Setup(TestLegacyAPI_Setup):
435
    def __init__(self, methodName="runTest"):
436
        super(TestAutonomousAPI_Setup, self).__init__(methodName)
437
        self.username = "sequential"
438
439


440
# ----------------------------------------------------------
441
442
443
444


class TestLegacyAPI_Prepare(unittest.TestCase):
    def test_always_prepared(self):
445
        algorithm = Algorithm(prefix, "legacy/valid_algorithm/1")
446
447
448
449
450
451
452
        self.assertTrue(algorithm.valid)

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

453
    def test_prepare_warning(self):
454
        log_handler = MockLoggingHandler(level="DEBUG")
455
456
457
        logging.getLogger().addHandler(log_handler)
        log_messages = log_handler.messages

458
        algorithm = Algorithm(prefix, "legacy/prepare/1")
459
460
461
462
463
464
465
        self.assertTrue(algorithm.valid)

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

466
        info_len = len(log_messages["warning"])
467
        self.assertEqual(info_len, 1)
468
469
470
471
        self.assertEqual(
            log_messages["warning"][info_len - 1],
            "Prepare is a reserved function name starting with API V2",
        )
472

473

474
# ----------------------------------------------------------
475
476


477
478
class TestSequentialAPI_Prepare(unittest.TestCase):
    def __init__(self, methodName="runTest"):
479
        super(TestSequentialAPI_Prepare, self).__init__(methodName)
480
        self.username = "sequential"
481
482

    def test_prepare_crashing_algorithm(self):
483
        algorithm = Algorithm(prefix, self.username + "/prepare_crash/1")
484
485
486
487
488
489
490
491
492
493
494
        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):
495
        algorithm = Algorithm(prefix, self.username + "/prepare_fail/1")
496
497
498
499
500
501
502
503
504
505
        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):
506
        algorithm = Algorithm(prefix, self.username + "/no_prepare/1")
507
508
509
510
511
512
513
514
515
516
        self.assertTrue(algorithm.valid)

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

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


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


class TestAutonomousAPI_Prepare(TestSequentialAPI_Prepare):
521
    def __init__(self, methodName="runTest"):
522
        super(TestAutonomousAPI_Prepare, self).__init__(methodName)
523
        self.username = "autonomous"
524
525


526
# ----------------------------------------------------------
527
528
529
530
531
532
533
534
535
536


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]
537
            filenames += glob.glob(basename + "*")
538
539
540
541
542
            for filename in filenames:
                if os.path.exists(filename):
                    os.unlink(filename)

    def writeData(self, input_name, indices, start_value):
543
544
545
546
        testfile = tempfile.NamedTemporaryFile(
            prefix=__name__ + input_name, suffix=".data"
        )
        testfile.close()  # preserve only the name
547
548
        self.filenames[input_name] = testfile.name

549
        dataformat = DataFormat(prefix, "user/single_integer/1")
550
551
552
        self.assertTrue(dataformat.valid)

        data_sink = CachedDataSink()
553
554
555
556
557
        self.assertTrue(
            data_sink.setup(
                self.filenames[input_name], dataformat, indices[0][0], indices[-1][1]
            )
        )
558
559
560
561
562
563
564
565
566
567
568
569
570
571

        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


572
# ----------------------------------------------------------
573
574
575
576


class TestLegacyAPI_Process(TestExecutionBase):
    def create_io(self, infos):
577
        dataformat = DataFormat(prefix, "user/single_integer/1")
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592

        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:
593
                data_source = CachedDataSource()
594
595
596
597
598
599
600
                data_source.setup(self.filenames[input_name], prefix)
                group.add(Input(input_name, dataformat, data_source))

        # Outputs
        outputs = OutputList()

        data_sink = MockDataSink(dataformat)
601
        outputs.add(Output("out", data_sink, synchronization_listener))
602
603
604

        return (inputs, outputs, data_sink)

605
    def test_process_crashing_algorithm(self):
606
        algorithm = Algorithm(prefix, "legacy/process_crash/1")
607
608
609
610
611
612
613
614
        self.assertTrue(algorithm.valid)

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

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

615
    def test_one_group_of_one_input(self):
616
        self.writeData("in", [(0, 0), (1, 1), (2, 2), (3, 3)], 1000)
617

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

620
        algorithm = Algorithm(prefix, "legacy/echo/1")
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
        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):
639
640
        self.writeData("in1", [(0, 0), (1, 1), (2, 2), (3, 3)], 1000)
        self.writeData("in2", [(0, 1), (2, 3)], 2000)
641

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

644
        algorithm = Algorithm(prefix, "legacy/add/1")
645
646
        runner = algorithm.runner()

647
        self.assertTrue(runner.setup({"sync": "in1"}))
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679

        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):
680
681
        self.writeData("in1", [(0, 0), (1, 1), (2, 2), (3, 3)], 1000)
        self.writeData("in2", [(0, 1), (2, 3)], 2000)
682

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

685
        algorithm = Algorithm(prefix, "legacy/add/1")
686
687
        runner = algorithm.runner()

688
        self.assertTrue(runner.setup({"sync": "in2"}))
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710

        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):
711
712
713
714
715
716
717
718
        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")
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
        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)


752
# ----------------------------------------------------------
753
754
755
756


class TestSequentialAPI_Process(TestExecutionBase):
    def create_io(self, infos):
757
        dataformat = DataFormat(prefix, "user/single_integer/1")
758
759
760
761
762
763
764
765
766
767
768
769

        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:
770
                    data_source = CachedDataSource()
771
772
773
774
775
776
777
778
                    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:
779
                    cached_file = CachedDataSource()
780
781
782
783
784
785
786
                    cached_file.setup(self.filenames[input_name], prefix)
                    data_loader.add(input_name, cached_file)

        # Outputs
        outputs = OutputList()

        data_sink = MockDataSink(dataformat)
787
        outputs.add(Output("out", data_sink, synchronization_listener))
788
789
790

        return (data_loaders, inputs, outputs, data_sink)

791
    def test_process_crashing_algorithm(self):
792
        algorithm = Algorithm(prefix, "sequential/process_crash/1")
793
794
795
796
797
798
        self.assertTrue(algorithm.valid)

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

        with self.assertRaises(NameError):
799
800
801
            runnable.process(
                inputs=InputList(), data_loaders=DataLoaderList(), outputs=OutputList()
            )
802

803
    def test_one_group_of_one_input(self):
804
        self.writeData("in", [(0, 0), (1, 1), (2, 2), (3, 3)], 1000)
805

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

808
        algorithm = Algorithm(prefix, "sequential/echo/1")
809
810
811
812
813
814
815
816
        runner = algorithm.runner()

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

        while inputs.hasMoreData():
            inputs.restricted_access = False
            inputs.next()
            inputs.restricted_access = True
817
818
819
820
821
            self.assertTrue(
                runner.process(
                    inputs=inputs, data_loaders=data_loaders, outputs=outputs
                )
            )
822
823
824
825
826
827
828
829
830

        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):
831
832
        self.writeData("in1", [(0, 0), (1, 1), (2, 2), (3, 3)], 1000)
        self.writeData("in2", [(0, 1), (2, 3)], 2000)
833

834
835
836
        (data_loaders, inputs, outputs, data_sink) = self.create_io(
            {"group1": ["in1", "in2"]}
        )
837

838
        algorithm = Algorithm(prefix, "sequential/add/1")
839
840
        runner = algorithm.runner()

841
        self.assertTrue(runner.setup({"sync": "in1"}))
842
843
844
845
846
847
848

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

        while inputs.hasMoreData():
            inputs.restricted_access = False
            inputs.next()
            inputs.restricted_access = True
849
850
851
852
853
            self.assertTrue(
                runner.process(
                    inputs=inputs, data_loaders=data_loaders, outputs=outputs
                )
            )
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877

        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):
878
879
        self.writeData("in1", [(0, 0), (1, 1), (2, 2), (3, 3)], 1000)
        self.writeData("in2", [(0, 1), (2, 3)], 2000)
880

881
882
883
        (data_loaders, inputs, outputs, data_sink) = self.create_io(
            {"group1": ["in1", "in2"]}
        )
884

885
        algorithm = Algorithm(prefix, "sequential/add/1")
886
887
        runner = algorithm.runner()

888
        self.assertTrue(runner.setup({"sync": "in2"}))
889
890
891
892
893
894
895

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

        while inputs.hasMoreData():
            inputs.restricted_access = False
            inputs.next()
            inputs.restricted_access = True
896
897
898
899
900
            self.assertTrue(
                runner.process(
                    inputs=inputs, data_loaders=data_loaders, outputs=outputs
                )
            )
901
902
903
904
905
906
907
908
909
910
911
912
913
914

        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):
915
916
917
918
919
920
921
922
        self.writeData("in1", [(0, 0), (1, 1), (2, 2), (3, 3)], 1000)
        self.writeData("in2", [(0, 3)], 2000)

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

        algorithm = Algorithm(prefix, "sequential/add2/1")
923
924
925
926
927
928
929
930
        runner = algorithm.runner()

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

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

        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)

    def test_two_groups_of_related_inputs(self):
960
961
962
963
964
965
966
967
968
969
970
        self.writeData("value", [(0, 0), (1, 1), (2, 2), (3, 3)], 0)  # 0, 1, 2, 3
        self.writeData("offset_index", [(0, 0), (1, 2), (3, 3)], 0)  # 0, 1, 3
        self.writeData(
            "offset", [(0, 0), (1, 5), (6, 10), (11, 11)], 2000
        )  # 2000, 2001, 2006, 2011

        (data_loaders, inputs, outputs, data_sink) = self.create_io(
            {"group1": ["value", "offset_index"], "group2": ["offset"]}
        )

        algorithm = Algorithm(prefix, "sequential/add3/1")
971
972
973
974
975
976
977
978
        runner = algorithm.runner()

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

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

        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, 2000)

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

        data_unit = data_sink.written[2]
        self.assertEqual(data_unit.start, 2)
        self.assertEqual(data_unit.end, 2)
        self.assertEqual(data_unit.data.value, 2003)
For faster browsing, not all history is shown. View entire blame