예제 #1
0
    def test_get_nucleotide_signature(self):
        node = Nucleotide(
            name='cnn',
            inbound_nodes=['data'],
            incoming_keys_mapping={'data': {
                'image': 'inputs_cnn'
            }})
        node.incoming_keys = ['inputs1', 'inputs2', '_input_optional']
        node.generated_keys = ['predictions', '_predictions_optional']

        node.__doc__ = """
            Attributes
            ----------
            incoming_keys : list
                * inputs1 : inputs1 to node
                * inputs2 : inputs2 to node
                * inputs_wrong : wrong description
                * input_optional : optional inputs
            generated_keys : list
                * predictions : predictions 1
                * predictions_optional : optional predictions
            """
        args_must = {
            'inputs1': 'inputs1 to node',
            'inputs2': 'inputs2 to node',
            'input_optional': 'optional inputs'
        }
        returns_must = {
            'predictions': 'predictions 1',
            'predictions_optional': 'optional predictions'
        }
        args, returns = nucleotide_utils.get_nucleotide_signature(node)

        self.assertSetEqual({'inputs1', 'inputs2', 'input_optional'},
                            set(args))
        self.assertSetEqual({'predictions', 'predictions_optional'},
                            set(returns))
        self.assertDictEqual(args_must, args)
        self.assertDictEqual(returns_must, returns)

        node.__doc__ = None
        node.process = MagicMock(return_value=0)
        node.process.__doc__ = """
            Parameters
            ----------
            inputs1
                inputs1 to node
            inputs2
                inputs2 to node
            inputs_wrong
                wrong description
            input_optional
                optional inputs

            Returns
            -------
            predictions
                predictions 1
            predictions_optional
                optional predictions
            """
        args, returns = nucleotide_utils.get_nucleotide_signature(node)
        self.assertDictEqual(args_must, args)
        self.assertDictEqual(returns_must, returns)
예제 #2
0
    def _get_callbacks_and_incoming_nucleotides():
        def side_effect_callback_on_iteration_end(nucleotide: Nucleotide,
                                                  **inputs):
            return {
                k: '_'.join([nucleotide.name, k])
                for k in nucleotide.generated_keys_all
            }

        input_node1 = Nucleotide(name='input_node1')
        input_node1.generated_keys = ['output1', 'output2']
        input_node2 = Nucleotide(name='input_node2')
        input_node2.generated_keys = ['output1']
        input_node1.process = MagicMock(return_value=None)
        input_node2.process = MagicMock(return_value=None)

        callback1 = CoordinatorCallback(
            name='callback1',
            inbound_nodes=['input_node1'],
            incoming_keys_mapping={'input_node1': {
                'output1': 'input11'
            }})
        callback1.incoming_keys = ['input11']
        callback1.generated_keys = ['output11', 'output12']
        callback2 = CoordinatorCallback(
            name='callback2',
            inbound_nodes=['callback1', 'input_node2'],
            incoming_keys_mapping={
                'callback1': {
                    'output11': 'input22',
                },
                'input_node2': {
                    'output1': 'input21',
                }
            })
        callback2.incoming_keys = ['input21', '_input22']
        callback2.generated_keys = ['output21', 'output22']
        callback3 = CoordinatorCallback(
            name='callback3',
            inbound_nodes=['callback1', 'callback2'],
            incoming_keys_mapping={
                'callback1': {
                    'output11': 'input31',
                },
                'callback2': {
                    'output22': 'input32',
                }
            })
        callback3.incoming_keys = ['input31', 'input32']
        callback3.generated_keys = ['output31']

        callback1.on_iteration_end = MagicMock(side_effect=partial(
            side_effect_callback_on_iteration_end, nucleotide=callback1))
        callback2.on_iteration_end = MagicMock(side_effect=partial(
            side_effect_callback_on_iteration_end, nucleotide=callback2))
        callback3.on_iteration_end = MagicMock(side_effect=partial(
            side_effect_callback_on_iteration_end, nucleotide=callback3))

        callbacks = [callback1.build(), callback2.build(), callback3.build()]
        incoming_nucleotides = {
            'input_node1': input_node1.build(),
            'input_node2': input_node2.build()
        }

        return callbacks, incoming_nucleotides