def test_box_convolution1(self):
        for box in [
                BoxConvolution('CONV-1', [1]),
                BoxConvolution('CONV-1', [1], True)
        ]:
            box.add(10)
            self.assertEqual(box.input(), 10)
            self.assertEqual(box.size(), 0)
            self.assertEqual(box.output(), 0)
            box.step()
            self.assertEqual(box.input(), 0)
            self.assertEqual(box.size(), 0)
            self.assertEqual(box.output(), 10)

            box.step()
            self.assertEqual(box.input(2), 10)
            self.assertEqual(box.size(2), 0)
            self.assertEqual(box.output(2), 0)

            self.assertEqual(box.input(1), 0)
            self.assertEqual(box.size(1), 0)
            self.assertEqual(box.output(1), 10)

            self.assertEqual(box.input(), 0)
            self.assertEqual(box.size(), 0)
            self.assertEqual(box.output(), 10)  # nothing has been removed
    def test_box_force_output10(self):
        for box in [
                BoxConvolution('CONV-10', 9 * [0] + [1]),
                BoxConvolution('CONV-10', 9 * [0] + [1], True)
        ]:
            inputs = [
                1, 2, 4, 8, 10, 12, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0,
                0, 0, 0, 0
            ]
            r = [
                1, 3, 7, 15, 25, 37, 47, 56, 64, 70, 74, 75, 71, 64, 54, 45,
                36, 28, 21, 15, 10, 6, 3, 1, 0, 0
            ]

            for i in range(4):
                box.add(inputs[i])
                box.step()
                box.remove(box.output())
            self.assertAlmostEqual(box.size(), r[3])
            self.assertAlmostEqual(box.output(), 0)

            box.force_output(10)
            self.assertAlmostEqual(box.size(), 15 - 10)

            box.force_output(10)
            self.assertAlmostEqual(box.size(), 0)
 def test_box_convolution_delay1(self):
     for box in [
             BoxConvolution('CONV-1', [1]),
             BoxConvolution('CONV-1', [1], True)
     ]:
         inputs = [1, 2, 4, 8, 10, 12, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
         r = [0 for x in inputs]
         outputs = [1, 2, 4, 8, 10, 12, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
         self.check_input_output(box, inputs, r, outputs)
 def test_box_convolution_delay10(self):
     for box in [
             BoxConvolution('CONV-10', 9 * [0] + [1]),
             BoxConvolution('CONV-10', 9 * [0] + [1], True)
     ]:
         inputs = [
             1, 2, 4, 8, 10, 12, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0,
             0, 0, 0, 0
         ]
         r = [
             1, 3, 7, 15, 25, 37, 47, 56, 64, 70, 74, 75, 71, 64, 54, 45,
             36, 28, 21, 15, 10, 6, 3, 1, 0, 0
         ]
         outputs = [
             0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 8, 10, 12, 10, 9, 8, 7, 6,
             5, 4, 3, 2, 1, 0
         ]
         self.check_input_output(box, inputs, r, outputs)
Example #5
0
    def __init__(self, parameters):
        self._parameters = dict(parameters)  # to not modify parameters
        self._integer = parameters['integer_flux']

        self._boxes = {
            'SE':
            BoxSource('SE'),
            'INCUB':
            BoxConvolution('INCUB', compute_khi_delay(self.delay('dm_incub')),
                           self._integer),
            'IR':
            BoxConvolution('IR', compute_khi_exp(self.delay('dm_r')),
                           self._integer),
            'IH':
            BoxConvolution('IH', compute_khi_exp(self.delay('dm_h')),
                           self._integer),
            'SM':
            BoxConvolution('SM', compute_khi_exp(self.delay('dm_sm')),
                           self._integer),
            'SI':
            BoxConvolution('SI', [
                0, 0.03, 0.03, 0.04, 0.05, 0.05, 0.05, 0.05, 0.1, 0.1, 0.1,
                0.1, 0.1, 0.1, 0.05, 0.03, 0.02
            ], self._integer),
            'SS':
            BoxConvolution('SS', compute_khi_exp(self.delay('dm_ss')),
                           self._integer),
            'R':
            BoxTarget('R'),
            'DC':
            BoxTarget('DC')
        }

        # src -> [targets]
        def lambda_coefficient(a, b=None):
            if isinstance(a, int):
                return lambda: a
            elif b == None:
                return lambda: self.coefficient(a)
            else:
                return lambda: self.coefficient(a) * self.coefficient(b)

        self._moves = {
            'INCUB': [('IH', lambda_coefficient('pc_ih')),
                      ('IR', lambda_coefficient('pc_ir'))],
            'IR': [('R', lambda_coefficient(1))],
            'IH': [('SM', lambda_coefficient('pc_sm')),
                   ('SI', lambda_coefficient('pc_si'))],
            'SM': [('SI', lambda_coefficient('pc_sm_si')),
                   ('DC', lambda_coefficient('pc_sm_dc')),
                   ('SS', lambda_coefficient('pc_sm_out', 'pc_h_ss')),
                   ('R', lambda_coefficient('pc_sm_out', 'pc_h_r'))],
            'SI': [('DC', lambda_coefficient('pc_si_dc')),
                   ('SS', lambda_coefficient('pc_si_out', 'pc_h_ss')),
                   ('R', lambda_coefficient('pc_si_out', 'pc_h_r'))],
            'SS': [('R', lambda_coefficient(1))]
        }

        self.time = -1  # first step should be t=0
        self.e0 = int(self.constant('population') * self.coefficient('kpe'))
        self.box('SE').add(self.e0 - self.constant('patient0'))
        self.box('INCUB').add(self.constant('patient0'))
 def test_box_int_convolution2(self):
     box = BoxConvolution('CONV-2', [0.4, 0.6], True)
     box.add(10)
     box.step()
     self.assertEqual(box.input(), 0)
     self.assertEqual(box.size(), int(0.6 * 10))
     self.assertEqual(box.output(), int(0.4 * 10))
     box.remove(int(0.4 * 10))
     box.add(20)
     box.step()
     self.assertEqual(box.input(), 0)
     self.assertEqual(box.size(),
                      int(0.6 * 10) + 20 - int(0.6 * 10) - int(0.4 * 20))
     self.assertEqual(box.output(), int(0.6 * 10) + int(0.4 * 20))
 def test_box_convolution2(self):
     box = BoxConvolution('CONV-2', [0.4, 0.6])
     box.add(10)
     box.step()
     self.assertEqual(box.input(), 0)
     self.assertEqual(box.size(), 0.6 * 10)
     self.assertEqual(box.output(), 0.4 * 10)
     box.remove(0.4 * 10)
     box.add(20)
     box.step()
     self.assertEqual(box.input(), 0)
     self.assertEqual(box.size(), (0.6 * 10) + 20 - (0.6 * 10) - (0.4 * 20))
     self.assertEqual(box.output(), 0.6 * 10 + 0.4 * 20)
    def test_box_queue_decrease_duration(self):
        box = BoxConvolution('DELAY', [0, 0, 0, 0, 1])
        inputs = [5] * 5 + [0] * 5
        r = [5, 10, 15, 20, 20, 15, 10, 5, 0, 0]
        outputs = [0, 0, 0, 0, 5, 5, 5, 5, 5, 0]
        self.check_input_output(box, inputs, r, outputs)

        inputs = [5] * 5 + [0] * 5
        r = [5, 10, 10, 10, 10, 5, 0, 0, 0, 0]
        outputs = [0, 0, 5, 5, 5, 5, 5, 0, 0, 0]
        for i in range(len(inputs)):
            if i == 2:
                box.set_output_coefficients([0, 0, 1])
            box.add(inputs[i])
            self.assertEqual(box.input(), inputs[i])
            box.step()
            self.assertEqual(box.size(), r[i])
            self.assertEqual(box.output(), outputs[i])
            box.remove(box.output())