1.py 5.9 KB
Newer Older
André Anjos's avatar
André Anjos committed
1 2 3 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 35 36 37 38 39
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

###############################################################################
#                                                                             #
# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/           #
# Contact: beat.support@idiap.ch                                              #
#                                                                             #
# This file is part of the beat.core module of the BEAT platform.             #
#                                                                             #
# Commercial License Usage                                                    #
# Licensees holding valid commercial BEAT licenses may use this file in       #
# accordance with the terms contained in a written agreement between you      #
# and Idiap. For further information contact tto@idiap.ch                     #
#                                                                             #
# Alternatively, this file may be used under the terms of the GNU Affero      #
# Public License version 3 as published by the Free Software and appearing    #
# in the file LICENSE.AGPL included in the packaging of this file.            #
# The BEAT platform is distributed in the hope that it will be useful, but    #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  #
# or FITNESS FOR A PARTICULAR PURPOSE.                                        #
#                                                                             #
# You should have received a copy of the GNU Affero Public License along      #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/.           #
#                                                                             #
###############################################################################

import random
import numpy


class Double:

    def setup(self, root_folder, outputs, parameters):
        self.outputs = outputs
        random.seed(0) #so it is kept reproducible
        return True


40 41
    def done(self, last_data_index):
        return (last_data_index == 9)
André Anjos's avatar
André Anjos committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61


    def next(self):
        val1 = numpy.int32(random.randint(0, 1000))
        self.outputs['a'].write({
            'value': val1,
            })

        val2 = numpy.int32(random.randint(0, 1000))
        self.outputs['b'].write({
            'value': val2,
            })

        self.outputs['sum'].write({
            'value': val1 + val2,
            })

        return True


62

André Anjos's avatar
André Anjos committed
63 64 65 66 67 68 69 70
class Triple:

    def setup(self, root_folder, outputs, parameters):
        random.seed(0) #so it is kept reproducible
        self.outputs = outputs
        return True


71 72
    def done(self, last_data_index):
        return (last_data_index == 9)
André Anjos's avatar
André Anjos committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95


    def next(self):
        val1 = numpy.int32(random.randint(0, 1000))
        self.outputs['a'].write({
            'value': val1,
            })

        val2 = numpy.int32(random.randint(0, 1000))
        self.outputs['b'].write({
            'value': val2,
            })

        val3 = numpy.int32(random.randint(0, 1000))
        self.outputs['c'].write({
            'value': val3,
            })

        self.outputs['sum'].write({
            'value': val1 + val2 + val3,
            })

        return True
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111



class Labelled:

    def setup(self, root_folder, outputs, parameters):
        self.outputs = outputs
        self.remaining = [
            ['A', [1, 2, 3, 4, 5]],
            ['B', [10, 20, 30, 40, 50]],
            ['C', [100, 200, 300, 400, 500]],
        ]
        self.current_label = None
        return True


112 113
    def done(self, last_data_index):
        return (last_data_index == 14)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142


    def next(self):
        # Ensure that we are not done
        if len(self.remaining) == 0:
            return False

        # Retrieve the next label and value
        label = self.remaining[0][0]
        value = self.remaining[0][1][0]

        # Only write each label once on the output, with the correct range of indexes
        if self.current_label != label:
            self.outputs['label'].write({
                'value': label,
            }, self.outputs['label'].last_written_data_index + len(self.remaining[0][1]))
            self.current_label = label

        # Write the value
        self.outputs['value'].write({
            'value': numpy.int32(value),
        })

        # Remove the value (and if needed the label) from the list of remaining data
        self.remaining[0][1] = self.remaining[0][1][1:]
        if len(self.remaining[0][1]) == 0:
            self.remaining = self.remaining[1:]

        return True
143 144 145 146 147 148 149 150 151 152



class DifferentFrequencies:

    def setup(self, root_folder, outputs, parameters):
        self.outputs = outputs
        self.values_a = [(1, 0, 3), (2, 4, 7)]
        self.values_b = [(10, 0, 0), (20, 1, 1), (30, 2, 2), (40, 3, 3),
                         (50, 4, 4), (60, 5, 5), (70, 6, 6), (80, 7, 7)]
Philip ABBET's avatar
Philip ABBET committed
153 154 155

        self.next_index = 0

156 157 158
        return True


159 160
    def done(self, last_data_index):
        return (last_data_index == 7)
161 162 163


    def next(self):
Philip ABBET's avatar
Philip ABBET committed
164 165
        if self.outputs['b'].isConnected() and \
           (self.outputs['b'].last_written_data_index < self.next_index):
166 167 168 169 170 171 172 173

            self.outputs['b'].write({
                    'value': numpy.int32(self.values_b[0][0]),
                },
                end_data_index=self.values_b[0][2]
            )

            self.values_b = self.values_b[1:]
Philip ABBET's avatar
Philip ABBET committed
174 175 176 177

        if self.outputs['a'].isConnected() and \
           (self.outputs['a'].last_written_data_index < self.next_index):

178 179 180 181 182 183 184 185
            self.outputs['a'].write({
                    'value': numpy.int32(self.values_a[0][0]),
                },
                end_data_index=self.values_a[0][2]
            )

            self.values_a = self.values_a[1:]

Philip ABBET's avatar
Philip ABBET committed
186 187 188
        self.next_index = 1 + min([ x.last_written_data_index for x in self.outputs
                                                              if x.isConnected() ])

189
        return True