Esempio n. 1
0
    def test_bool(self):
        """Test converting bool to Bool"""

        import numpy as np
        # pylint: disable=assignment-from-no-return
        aiida_bool = to_aiida_type(True)
        self.assertIsInstance(aiida_bool, Bool)
        self.assertEqual(aiida_bool, True)

        # pylint: disable=assignment-from-no-return
        aiida_bool = to_aiida_type(np.bool_(True))
        self.assertIsInstance(aiida_bool, Bool)
        self.assertEqual(np.bool_(True), True)
Esempio n. 2
0
 def test_str(self):
     """Test converting string to Str"""
     string = 'hello world'
     # pylint: disable=assignment-from-no-return
     aiida_string = to_aiida_type(string)
     self.assertIsInstance(aiida_string, Str)
     self.assertEqual(aiida_string, string)
Esempio n. 3
0
    def test_int(self):
        """Test integer"""

        # pylint: disable=assignment-from-no-return
        aiida_int = to_aiida_type(1234567)
        self.assertEqual(aiida_int, 1234567)
        self.assertIsInstance(aiida_int, Int)
Esempio n. 4
0
    def _values_list_serializer(list_to_parse):
        '''
        Parses a list of objects to a list of node pks.
        This is done because aiida's List does not accept items with certain data structures
        (e.g. StructureData). In this way, we normalize the input to a list of pk, so that at
        each iteration we can access the value of the node.
        Note that if you modify/overwrite this method you should take a look to the `_next_val` method.

        :param list_to_parse: a list with aiida data object
        :return: an aiida List containing the pk of each element of list_to_parse
        '''

        parsed_list = []
        # Let's iterate over all values so that we can parse them all
        for obj in list_to_parse:

            # If the object is a python type, convert it to a node
            if not isinstance(obj, Node):
                obj = to_aiida_type(obj)

            # If it has just been converted to a node, or it was an unstored node
            # store it so that it gets a pk.
            if not obj.is_stored:
                obj.store()

            # Now that we are sure the node has a pk, we append it to the list
            parsed_list.append(obj.pk)

        return List(list=parsed_list)
Esempio n. 5
0
    def test_flot(self):
        """Test converting float to Float"""

        float_value = 3.14159265358
        # pylint: disable=assignment-from-no-return
        aiida_float = to_aiida_type(float_value)
        self.assertEqual(aiida_float, float_value)
        self.assertIsInstance(aiida_float, Float)
Esempio n. 6
0
    def test_dict(self):
        """Test converting dict to Dict"""

        data = {'foo': 'bar', 'bla': {'bar': 'foo'}}

        # pylint: disable=assignment-from-no-return
        aiida_dict = to_aiida_type(data)
        self.assertIsInstance(aiida_dict, Dict)
        self.assertEqual(aiida_dict.get_dict(), data)
Esempio n. 7
0
 def _create_inputs(self):
     return [{k: to_aiida_type(v)
              for k, v in param_dict.items()}
             for param_dict in self._parameters]
Esempio n. 8
0
    def run_process(self):
        """
        Merge the inputs namespace and added inputs, and launch the
        sub-process.
        """
        self.report("Merging inputs for the sub-process.")

        if isinstance(self.inputs.added_input_keys, orm.Str):
            added_input_keys = [self.inputs.added_input_keys.value]
            if not isinstance(self.inputs.added_input_values, orm.BaseType):
                raise InputValidationError(
                    "When 'added_input_keys' is given as 'Str', 'added_input_values'"
                    " must be a 'BaseType' instance.")
            added_input_values = [self.inputs.added_input_values.value]
        else:
            added_input_keys = self.inputs.added_input_keys.get_list()
            if not isinstance(self.inputs.added_input_values, orm.List):
                raise InputValidationError(
                    "When 'added_input_keys' is given as 'List', 'added_input_values'"
                    " must also be a 'List'.")
            added_input_values = self.inputs.added_input_values.get_list()

        if len(added_input_values) != len(added_input_keys):
            raise InputValidationError(
                "Lengths of 'added_input_values' and 'added_input_keys' do not match."
            )

        inputs = AttributeDict(self.inputs.inputs)

        def _get_or_create_sub_dict(in_dict, name):
            try:
                return in_dict[name]
            except KeyError:
                res = {}
                in_dict[name] = res
                return res

        def _get_or_create_port(in_attr_dict, name):
            try:
                return getattr(in_attr_dict, name)
            except AttributeError:
                res = AttributeDict()
                setattr(in_attr_dict, name, res)
                return res

        for key, value in zip(added_input_keys, added_input_values):
            full_port_path, *full_attr_path = key.split(':')
            *port_path, port_name = full_port_path.split('.')
            namespace = reduce(_get_or_create_port, port_path, inputs)
            if not full_attr_path:
                res_value = to_aiida_type(value)
            else:
                assert len(full_attr_path) == 1
                # Get or create the top-level dictionary.
                try:
                    res_dict = getattr(namespace, port_name).get_dict()
                except AttributeError:
                    res_dict = {}

                *sub_dict_path, attr_name = full_attr_path[0].split('.')
                sub_dict = reduce(_get_or_create_sub_dict, sub_dict_path,
                                  res_dict)
                sub_dict[attr_name] = value
                res_value = orm.Dict(dict=res_dict).store()

            setattr(namespace, port_name, res_value)

        self.report("Launching the sub-process.")
        return ToContext(sub_process=self.run_or_submit(
            load_object(self.inputs.sub_process.value), **inputs))