Esempio n. 1
0
    def create_namelist_from_dict(self):
        """
        Creates one major namelist from the given dictionary.

        Raises
        ------
        ParfileGenerationError
            - If the original dictionary is not empty after everything should be popped
            - If there is an inconsistency between array sizes of dictionary items
        """
        for namelist, items in namelist_items.items():
            # update container
            self.container.update({namelist: {}})
            # loop over names for this specific namelist
            for name, dtypes in items:
                obj = self._get_and_check_item(namelist, name, dtypes)
                if obj is not None:
                    obj = transform_to_list(obj)
                    self.container[namelist].update({name: obj})
            # if this namelist is still empty, remove it
            if self.container[namelist] == {}:
                self.container.pop(namelist)
        # account for parameters separately
        params = self.parfile_dict.pop("parameters", {})
        if len(params) != 0:
            self.container["equilibriumlist"].update({"use_defaults": [False]})
            for name, param in params.items():
                params.update({name: transform_to_list(param)})
            self.container.update({"paramlist": params})

        # here the original dictionary should be empty,
        # something went wrong if it isn't
        if len(self.parfile_dict) != 0:
            raise ParfileGenerationError(self.parfile_dict)

        # update container for number of runs, all items are lists
        for namelist, items in self.container.items():
            for key, values in items.items():
                if len(values) == 1:
                    values_list = values * self.nb_runs
                elif len(values) == self.nb_runs:
                    values_list = values
                else:
                    raise ParfileGenerationError(items, self.nb_runs, key)
                # make sure that sigma has complex values
                if key == "sigma":
                    values_list = [complex(value) for value in values_list]
                self.container.get(namelist).update({key: values_list})
Esempio n. 2
0
    def _get_and_check_item(self, namelist, name, allowed_dtypes):
        """
        Does typechecking on the various dictionary keys supplied to the parfile
        generator. Pops the key from the dictionary.

        Parameters
        ----------
        namelist : str
            One of the namelists ("gridlist", "savelist", etc.)
        name : str
            The key to check.
        allowed_dtypes : class, tuple
            Allowed types for that particular key. Either a single value or a tuple.

        Raises
        ------
        TypeError
            If the types do not match, e.g. if "gridpoints" is specified as a float
            value when it should be an integer.

        Returns
        -------
        item : any
            The item popped from the dictionary corresponding to the given key.
        """
        item = self.parfile_dict.pop(name, None)
        if item is not None:
            item_aslist = transform_to_list(item)
            for obj in item_aslist:
                if not isinstance(obj, allowed_dtypes):
                    raise TypeError(
                        f"namelist '{namelist}' expected item '{name}' to be of "
                        f"type {allowed_dtypes} but got {type(obj)}. \n"
                        f"item value = {item}")
        return item
Esempio n. 3
0
def _validate_parfiles(files):
    """
    Validates a list of parfiles.

    Parameters
    ----------
    files : (list of) str, (list of) ~os.PathLike
        Paths to the parfiles.

    Raises
    ------
    FileNotFoundError
        If one of the parfiles is not found.

    Returns
    -------
    files_list : list
        A list of resolved filepaths for the parfiles.

    """
    files_list = transform_to_list(files)
    files_list = [Path(file).resolve() for file in files_list]
    for file in files_list:
        if not file.is_file():
            raise FileNotFoundError(f"Parfile was not found: {file}")
    return files_list
Esempio n. 4
0
def test_none_tolist():
    result = toolbox.transform_to_list(None)
    assert isinstance(result, list)
    assert result[0] is None
Esempio n. 5
0
def test_list_tolist():
    result = toolbox.transform_to_list([1, 2, 3, 4])
    assert isinstance(result, list)
    assert result == [1, 2, 3, 4]
Esempio n. 6
0
def test_numpy_tolist():
    result = toolbox.transform_to_list(np.linspace(0, 1, 6))
    assert isinstance(result, list)
    assert result == pytest.approx([0, 0.2, 0.4, 0.6, 0.8, 1])
Esempio n. 7
0
def test_cmplx_tolist():
    result = toolbox.transform_to_list(1 + 2j)
    assert isinstance(result, list)
    assert result == [1 + 2j]
Esempio n. 8
0
def test_float_tolist():
    result = toolbox.transform_to_list(5)
    assert isinstance(result, list)
    assert result == [5]