Esempio n. 1
0
def test_finalize(Pd):
    """ Test the finalize function in the controller module
    """
    from os import path
    Pd.setup()
    Pd.execute(env_vars={"SLURM_ARRAY_TASK_ID": "1"})
    folder = path.join(reporoot, "tests", "data", "Pd", "manual")
    _mimic_vasp(folder, Pd.root, "S1.1")

    Pd.extract()
    Pd.split()
    Pd.finalize()

    from matdb.io import load_dict_from_h5
    from matdb import __version__
    import h5py

    str_ver = []
    for item in __version__:
        str_ver.append(str(item))
    mdb_ver = ".".join(str_ver)
    target = path.join(Pd.root, "final_{}.h5".format(mdb_ver))
    with h5py.File(target, "r") as hf:
        loaded_final = load_dict_from_h5(hf)
    assert path.isfile(target)
Esempio n. 2
0
def test_hdf5_in_out():
    """Tests the writing of dictionaries to hdf5 and reading back out.
    """

    import h5py
    from matdb.io import load_dict_from_h5, save_dict_to_h5
    from os import remove

    dict_1_in = {
        "a": {
            "B": np.int64(1),
            "C": np.int64(3),
            "D": {
                "temp": np.array([10, 11, 12])
            }
        },
        "n": np.array([3, 2]),
        "t": np.int64(5)
    }
    dict_2_in = {"a": np.int64(10), "b": np.array([1, 2, 10])}

    hf = h5py.File("temp.h5", "w")
    save_dict_to_h5(hf, dict_1_in, "/")
    hf.close()

    hf = h5py.File("temp.h5", "r")
    out = load_dict_from_h5(hf)
    hf.close()
    assert compare_nested_dicts(dict_1_in, out)
    remove("temp.h5")

    hf = h5py.File("temp.h5", "w")
    save_dict_to_h5(hf, dict_2_in, "/")
    hf.close()

    hf = h5py.File("temp.h5", "r")
    out = load_dict_from_h5(hf)
    hf.close()
    assert compare_dicts(dict_2_in, out)
    remove("temp.h5")

    hf = h5py.File("temp.h5", "w")

    with pytest.raises(ValueError):
        save_dict_to_h5(hf, {"a": remove}, "/")
    hf.close()
    remove("temp.h5")
Esempio n. 3
0
    def read(self, target, **kwargs):
        """Reads an atoms object in from file.
        
        Args:
            target (str): The path to the target file.
            kwargs (dict): A dictionary of arguments to pass to the ase 
              read function.
        """
        frmt = target.split('.')[-1]
        if frmt == "h5" or frmt == "hdf5":
            from matdb.io import load_dict_from_h5
            with h5py.File(target, "r") as hf:
                data = load_dict_from_h5(hf)
            # If the data was read in from an hdf5 file written by the
            # AtomsList object then each atom will have a tag with it. We check
            # for this by looking for the word 'atom' inside the first key, if
            # it's present we assume that all the contents of the file are an
            # atoms list. If it's not then we assume this is a single atoms
            # object.

            #NB! It is possible that the atoms list object could be an *empty*
            #AtomsList that was written to disk. In that case, just use an empty
            #list.
            if len(data) == 0:
                atoms = []
            elif "atom" in list(data.keys())[0]:
                if isinstance(list(data.values())[0], dict):
                    atoms = [Atoms(**d) for d in data.values()]
                elif isinstance(list(data.values())[0], string_types):
                    atoms = [Atoms(d) for d in data.values()]
                else:  #pragma: no cover
                    msg.err(
                        "The data format {} isn't supported for reading AtomLists "
                        "from hdf5 files.".format(type((data.values())[0])))
            else:
                atoms = [Atoms(target, **kwargs)]
            if len(self) > 0:
                self.extend(atoms)
            else:
                self.__init__(atoms)
        else:
            atoms = [Atoms(d) for d in io.read(target, index=':', **kwargs)]
            if len(self) > 0:
                self.extend(atoms)
            else:
                self.__init__(atoms)
Esempio n. 4
0
    def read(self, target="atoms.h5", **kwargs):
        """Reads an atoms object in from file.

        Args:
            target (str): The path to the target file. Default "atoms.h5".
        """

        frmt = target.split('.')[-1]
        if frmt == "h5" or frmt == "hdf5":
            from matdb.io import load_dict_from_h5
            with h5py.File(target, "r") as hf:
                data = load_dict_from_h5(hf)
            if "atom" in list(data.keys())[0]:
                data = data[list(data.keys())[0]]
            self.__init__(**data)
            if "calc" in data:
                calc = getattr(calculators, _calc_name_converter(data["calc"]))
                args = data["calc_args"] if "calc_args" in data else None
                kwargs = data["calc_kwargs"] if 'calc_kwargs' in data else None
                if args is not None:
                    if kwargs is not None:
                        calc = calc(self, data["folder"],
                                    data["calc_contr_dir"],
                                    data["calc_ran_seed"], *args, **kwargs)
                    else:  # pragma: no cover (all calculators require key words at this time)
                        calc = calc(self, data["folder"],
                                    data["calc_contr_dir"],
                                    data["calc_ran_seed"], *args)
                else:  #pragma: no cover This case has never come up in
                    #testing, however we wil keep it here to be
                    #verbose.
                    if kwargs is not None:
                        calc = calc(self, data["folder"],
                                    data["calc_contr_dir"],
                                    data["calc_ran_seed"], **kwargs)
                    else:
                        calc = calc(self, data["folder"],
                                    data["calc_contr_dir"],
                                    data["calc_ran_seed"])
                self.set_calculator(calc)

        else:
            self.__init__(io.read(target, **kwargs))
Esempio n. 5
0
def _fix_precomp(db, purge=False):
    """Reconstitutes the `pre_comp_atoms.h5` file by copying the existing
    `atoms.h5`, and applying some modifications to the atoms object.

    Args:
        db (matdb.database.Group): a group to create pre_comp files for.
        purge (bool): when True, remove the `atoms.h5` files after the pre-comp
          file is created.
    """
    for aid, apath in db.configs.items():
        target = path.join(apath, "atoms.h5")
        with h5py.File(target, "r") as hf:
            data = load_dict_from_h5(hf)
        data["pbc"] = np.array([1, 1, 1], dtype=bool)

        newtarg = path.join(apath, "pre_comp_atoms.h5")
        with h5py.File(newtarg, "w") as hf:
            save_dict_to_h5(hf, data, '/')

        if path.isfile(newtarg) and purge:
            remove(target)