Load an hdf5 file and recreate the PyTTa objects.
(fileName: str)
| 955 | |
| 956 | |
| 957 | def _h5_load(fileName: str): |
| 958 | """ |
| 959 | Load an hdf5 file and recreate the PyTTa objects. |
| 960 | """ |
| 961 | # Checking if the file is an hdf5 file |
| 962 | if fileName.split('.')[-1] != 'hdf5': |
| 963 | raise ValueError("_h5_load function only works with *.hdf5 files") |
| 964 | f = h5py.File(fileName, 'r') |
| 965 | |
| 966 | # Check if it is a PyTTa-like hdf5 file |
| 967 | try: |
| 968 | if 'GENERATED_BY' not in f.attrs.keys() or \ |
| 969 | f.attrs['GENERATED_BY'] != "PyTTa": |
| 970 | raise NotImplementedError |
| 971 | except: |
| 972 | # raise NotImplementedError("Only PyTTa-like hdf5 files can be loaded.") |
| 973 | warn(DeprecationWarning("'GENERATED_BY' tag couldn't be found in " + |
| 974 | "the .hdf5 file. Still trying to load " + |
| 975 | "because of legacy PyTTa HDF5 files.")) |
| 976 | |
| 977 | loadedObjects = {} |
| 978 | objCount = 0 # Counter for loaded objects |
| 979 | totCount = 0 # Counter for total groups |
| 980 | for PyTTaObjName, PyTTaObjH5Group in f.items(): |
| 981 | totCount += 1 |
| 982 | try: |
| 983 | loadedObjects[PyTTaObjName] = __h5_unpack(PyTTaObjH5Group) |
| 984 | objCount += 1 |
| 985 | except NotImplementedError: |
| 986 | print("Skipping hdf5 group named {} as it ".format(PyTTaObjName) + |
| 987 | "isn't an PyTTa object group.") |
| 988 | f.close() |
| 989 | # Final message |
| 990 | plural1 = 's' if objCount > 1 else '' |
| 991 | plural2 = 's' if totCount > 1 else '' |
| 992 | print('Imported {} PyTTa object-like group'.format(objCount) + plural1 + |
| 993 | ' of {} group'.format(totCount) + plural2 + |
| 994 | ' inside the hdf5 file.') |
| 995 | return loadedObjects |
| 996 | |
| 997 | |
| 998 | def __h5_unpack(objH5Group): |
no test coverage detected