def test_check_signal_dict(self):
        valid_signal = {'event': {'parameter_1': ['value1']}}
        invalid_signal = {'non_existing_signal_name': {'parameter_2': ['value2']}}

        self.assertTrue(ConfigurationChecker.check_signal_dict(valid_signal))

        with self.assertRaises(ModuleNotFoundError):
            ConfigurationChecker.check_signal_dict(invalid_signal)
    def test_check_neuron_dict(self):
        valid_neuron = {'say': {'message': ['test message']}}
        invalid_neuron = {'not_existing_neuron': {'message': ['test message']}}

        self.assertTrue(ConfigurationChecker.check_neuron_dict(valid_neuron))

        with self.assertRaises(ModuleNotFoundError):
            ConfigurationChecker.check_neuron_dict(invalid_neuron)
    def test_check_signal_dict(self):
        valid_signal_with_order = {'order': 'test_order'}
        valid_signal_with_event = {'event': '0 * * * *'}
        invalid_signal = {'invalid_option': 'test_order'}

        self.assertTrue(
            ConfigurationChecker.check_signal_dict(valid_signal_with_order))
        self.assertTrue(
            ConfigurationChecker.check_signal_dict(valid_signal_with_event))

        with self.assertRaises(NoValidSignal):
            ConfigurationChecker.check_signal_dict(invalid_signal)
    def test_check_synapes(self):
        synapse_1 = Synapse(name="test")
        synapse_2 = Synapse(name="test2")
        synapse_3 = Synapse(name="test")

        valid_synapse_list = [synapse_1, synapse_2]
        invalid_synapse_list = [synapse_1, synapse_3]

        self.assertTrue(ConfigurationChecker.check_synapes(valid_synapse_list))

        with self.assertRaises(MultipleSameSynapseName):
            ConfigurationChecker.check_synapes(invalid_synapse_list)
    def test_check_synapes(self):
        synapse_1 = Synapse(name="test")
        synapse_2 = Synapse(name="test2")
        synapse_3 = Synapse(name="test")

        valid_synapse_list = [synapse_1, synapse_2]
        invalid_synapse_list = [synapse_1, synapse_3]

        self.assertTrue(ConfigurationChecker.check_synapes(valid_synapse_list))

        with self.assertRaises(MultipleSameSynapseName):
            ConfigurationChecker.check_synapes(invalid_synapse_list)
Example #6
0
    def test_check_signal_dict(self):
        valid_signal = {'event': {'parameter_1': ['value1']}}
        invalid_signal = {
            'non_existing_signal_name': {
                'parameter_2': ['value2']
            }
        }

        self.assertTrue(ConfigurationChecker.check_signal_dict(valid_signal))

        with self.assertRaises(ModuleNotFoundError):
            ConfigurationChecker.check_signal_dict(invalid_signal)
Example #7
0
    def _get_signals(cls, signals_dict):
        """
        Get a list of Signal object from a signals dict

        :param signals_dict: Signal name or dictionary of Signal_name/Signal_parameters
        :type signals_dict: String or dict
        :return: A list of Event and/or Order
        :rtype: List

        :Example:

            signals = cls._get_signals(synapses_dict["signals"])

        .. seealso:: Event, Order
        .. warnings:: Class method and Private
        """
        signals = list()
        for signal_dict in signals_dict:
            if ConfigurationChecker().check_signal_dict(signal_dict):
                for signal_name in signal_dict:
                    new_signal = Signal(name=signal_name,
                                        parameters=signal_dict[signal_name])
                    signals.append(new_signal)

        return signals
Example #8
0
    def _get_neurons(cls, neurons_dict, settings):
        """
        Get a list of Neuron object from a neuron dict

        :param neurons_dict: Neuron name or dictionary of Neuron_name/Neuron_parameters
        :type neurons_dict: String or dict
        :param settings:  The Settings with the global variables
        :return: A list of Neurons
        :rtype: List

        :Example:

            neurons = cls._get_neurons(synapses_dict["neurons"])

        .. seealso:: Neuron
        .. warnings:: Static and Private
        """

        neurons = list()
        for neuron_dict in neurons_dict:
            if ConfigurationChecker().check_neuron_dict(neuron_dict):
                if isinstance(neuron_dict, dict):
                    for neuron_name in neuron_dict:
                        new_neuron = Neuron(
                            name=neuron_name,
                            parameters=neuron_dict[neuron_name])
                        neurons.append(new_neuron)
                else:
                    new_neuron = Neuron(name=neuron_dict)
                    neurons.append(new_neuron)
        return neurons
Example #9
0
    def _get_signals(cls, signals_dict):
        """
        Get a list of Signal object from a signals dict

        :param signals_dict: Signal name or dictionary of Signal_name/Signal_parameters
        :type signals_dict: String or dict
        :return: A list of Event and/or Order
        :rtype: List

        :Example:

            signals = cls._get_signals(synapses_dict["signals"])

        .. seealso:: Event, Order
        .. warnings:: Class method and Private
        """
        # print signals_dict
        signals = list()
        for signal_dict in signals_dict:
            if ConfigurationChecker().check_signal_dict(signal_dict):
                # print "Signals dict ok"
                event_or_order = cls._get_event_or_order_from_dict(signal_dict)
                signals.append(event_or_order)

        return signals
    def test_check_synape_dict(self):
        valid_synapse_dict = {
            'signals': [{'order': 'test_order'}],
            'neurons': [{'say': {'message': ['test message']}}],
            'name': 'test'
        }

        synapse_dict_without_name = {
            'signals': [{'order': 'test_order'}],
            'neurons': [{'say': {'message': ['test message']}}]
        }

        synapse_dict_without_neurons = {
            'signals': [{'order': 'test_order'}],
            'name': 'test'
        }

        synapse_dict_without_signals = {
            'neurons': [{'say': {'message': ['test message']}}],
            'name': 'test'
        }

        self.assertTrue(ConfigurationChecker.check_synape_dict(valid_synapse_dict))

        with self.assertRaises(NoSynapeName):
            ConfigurationChecker.check_synape_dict(synapse_dict_without_name)

        with self.assertRaises(NoSynapeNeurons):
            ConfigurationChecker.check_synape_dict(synapse_dict_without_neurons)

        with self.assertRaises(NoSynapeSignals):
            ConfigurationChecker.check_synape_dict(synapse_dict_without_signals)
Example #11
0
    def load_brain(self):
        """
        Class Methods which loads default or the provided YAML file and return a Brain
        :return: The loaded Brain
        :rtype: Brain

        :Example:

            brain = BrainLoader.load_brain(file_path="/var/tmp/brain.yml")

        .. seealso:: Brain
        .. warnings:: Class Method
        """

        # Instantiate a brain
        brain = Brain()

        # get the brain with dict
        dict_brain = self.get_yaml_config()

        brain.brain_yaml = dict_brain
        # create list of Synapse
        synapses = list()
        for synapses_dict in dict_brain:
            if "includes" not in synapses_dict:  # we don't need to check includes as it's not a synapse
                if ConfigurationChecker().check_synape_dict(synapses_dict):
                    name = synapses_dict["name"]
                    neurons = self._get_neurons(synapses_dict["neurons"],
                                                self.settings)
                    signals = self._get_signals(synapses_dict["signals"])
                    new_synapse = Synapse(name=name,
                                          neurons=neurons,
                                          signals=signals)
                    synapses.append(new_synapse)
        brain.synapses = synapses
        if self.file_path is None:
            brain.brain_file = self._get_root_brain_path()
        else:
            brain.brain_file = self.file_path
        # check that no synapse have the same name than another
        if not ConfigurationChecker().check_synapes(synapses):
            brain = None

        return brain
Example #12
0
    def _get_neurons(cls, neurons_dict, settings):
        """
        Get a list of Neuron object from a neuron dict

        :param neurons_dict: Neuron name or dictionary of Neuron_name/Neuron_parameters
        :type neurons_dict: String or dict
        :param settings:  The Settings with the global variables
        :return: A list of Neurons
        :rtype: List

        :Example:

            neurons = cls._get_neurons(synapses_dict["neurons"])

        .. seealso:: Neuron
        .. warnings:: Static and Private
        """

        neurons = list()
        for neuron_dict in neurons_dict:
            if isinstance(neuron_dict, dict):
                if ConfigurationChecker().check_neuron_dict(neuron_dict):
                    # print "Neurons dict ok"
                    for neuron_name in neuron_dict:

                        name = neuron_name
                        parameters = neuron_dict[name]

                        # Update brackets with the global parameter if exist
                        parameters = cls._replace_global_variables(parameter=parameters,
                                                                   settings=settings)

                        new_neuron = Neuron(name=name, parameters=parameters)
                        neurons.append(new_neuron)
            else:
                # the neuron does not have parameter
                if ConfigurationChecker().check_neuron_dict(neuron_dict):
                    new_neuron = Neuron(name=neuron_dict)
                    neurons.append(new_neuron)

        return neurons
    def create_synapses(self):
        """
        curl -i -H "Content-Type: application/json" \
        --user admin:secret \
        -X POST \
        -d '{
          "name": "Say-hello",
          "signals": [
            {
              "order": "je suis nicolas"
            }
          ],
          "neurons": [
            {
              "say": {
                "message": "je sais"
              }
            }
          ]
        }' \
        http://127.0.0.1:5000/synapses
        :return:
        """
        if not request.get_json() or 'name' not in request.get_json():
            data = {
                "Error": "Wrong parameters, 'name' not set"
            }
            return jsonify(error=data), 400

        new_synapse = request.get_json()
        try:
            ConfigurationChecker().check_synape_dict(new_synapse)
        except (InvalidSynapeName, NoSynapeNeurons, NoSynapeSignals) as e:
            data = {
                "error": "%s" % e
            }
            return jsonify(data), 400

        try:
            name = new_synapse["name"]
            neurons = BrainLoader.get_neurons(new_synapse["neurons"], self.settings)
            signals = BrainLoader.get_signals(new_synapse["signals"])
            new_synapse_instance = Synapse(name=name, neurons=neurons, signals=signals)
            self.brain.synapses.append(new_synapse_instance)
            # TODO save the brain in yaml
            return jsonify(new_synapse_instance.serialize()), 201
        except KalliopeModuleNotFoundError as e:
            data = {
                "error": "%s" % e
            }
            return jsonify(data), 400
    def test_check_synape_dict(self):
        valid_synapse_dict = {
            'signals': [{
                'order': 'test_order'
            }],
            'neurons': [{
                'say': {
                    'message': ['test message']
                }
            }],
            'name': 'test'
        }

        synapse_dict_without_name = {
            'signals': [{
                'order': 'test_order'
            }],
            'neurons': [{
                'say': {
                    'message': ['test message']
                }
            }]
        }

        synapse_dict_without_neurons = {
            'signals': [{
                'order': 'test_order'
            }],
            'name': 'test'
        }

        synapse_dict_without_signals = {
            'neurons': [{
                'say': {
                    'message': ['test message']
                }
            }],
            'name': 'test'
        }

        self.assertTrue(
            ConfigurationChecker.check_synape_dict(valid_synapse_dict))

        with self.assertRaises(NoSynapeName):
            ConfigurationChecker.check_synape_dict(synapse_dict_without_name)

        with self.assertRaises(NoSynapeNeurons):
            ConfigurationChecker.check_synape_dict(
                synapse_dict_without_neurons)

        with self.assertRaises(NoSynapeSignals):
            ConfigurationChecker.check_synape_dict(
                synapse_dict_without_signals)
Example #15
0
    def _get_neurons(neurons_dict):
        """
        Get a list of Neuron object from a neuron dict

        :param neurons_dict: Neuron name or dictionary of Neuron_name/Neuron_parameters
        :type neurons_dict: String or dict
        :return: A list of Neurons
        :rtype: List

        :Example:

            neurons = cls._get_neurons(synapses_dict["neurons"])

        .. seealso:: Neuron
        .. warnings:: Static and Private
        """

        neurons = list()
        for neuron_dict in neurons_dict:
            if isinstance(neuron_dict, dict):
                if ConfigurationChecker().check_neuron_dict(neuron_dict):
                    # print "Neurons dict ok"
                    for neuron_name in neuron_dict:

                        name = neuron_name
                        parameters = neuron_dict[name]
                        # print parameters
                        new_neuron = Neuron(name=name, parameters=parameters)
                        neurons.append(new_neuron)
            else:
                # the neuron does not have parameter
                if ConfigurationChecker().check_neuron_dict(neuron_dict):
                    new_neuron = Neuron(name=neuron_dict)
                    neurons.append(new_neuron)

        return neurons
    def test_check_event_dict(self):
        valid_event = {"hour": "18", "minute": "16"}
        invalid_event = None
        invalid_event2 = ""
        invalid_event3 = {"notexisting": "12"}

        self.assertTrue(ConfigurationChecker.check_event_dict(valid_event))

        with self.assertRaises(NoEventPeriod):
            ConfigurationChecker.check_event_dict(invalid_event)
        with self.assertRaises(NoEventPeriod):
            ConfigurationChecker.check_event_dict(invalid_event2)
        with self.assertRaises(NoEventPeriod):
            ConfigurationChecker.check_event_dict(invalid_event3)