Write dataset to hdf5. Args: hdf5_name (str): Hdf5 dataset filename. hdf5_path (str): Dataset path in hdf5. write_data (ndarray): Data to write. is_overwrite (bool): Whether to overwrite dataset.
(hdf5_name, hdf5_path, write_data, is_overwrite=True)
| 64 | |
| 65 | |
| 66 | def write_hdf5(hdf5_name, hdf5_path, write_data, is_overwrite=True): |
| 67 | """Write dataset to hdf5. |
| 68 | |
| 69 | Args: |
| 70 | hdf5_name (str): Hdf5 dataset filename. |
| 71 | hdf5_path (str): Dataset path in hdf5. |
| 72 | write_data (ndarray): Data to write. |
| 73 | is_overwrite (bool): Whether to overwrite dataset. |
| 74 | |
| 75 | """ |
| 76 | # convert to numpy array |
| 77 | write_data = np.array(write_data) |
| 78 | |
| 79 | # check folder existence |
| 80 | folder_name, _ = os.path.split(hdf5_name) |
| 81 | if not os.path.exists(folder_name) and len(folder_name) != 0: |
| 82 | os.makedirs(folder_name) |
| 83 | |
| 84 | # check hdf5 existence |
| 85 | if os.path.exists(hdf5_name): |
| 86 | # if already exists, open with r+ mode |
| 87 | hdf5_file = h5py.File(hdf5_name, "r+") |
| 88 | # check dataset existence |
| 89 | if hdf5_path in hdf5_file: |
| 90 | if is_overwrite: |
| 91 | logging.warning("Dataset in hdf5 file already exists. " |
| 92 | "recreate dataset in hdf5.") |
| 93 | hdf5_file.__delitem__(hdf5_path) |
| 94 | else: |
| 95 | logging.error("Dataset in hdf5 file already exists. " |
| 96 | "if you want to overwrite, please set is_overwrite = True.") |
| 97 | hdf5_file.close() |
| 98 | sys.exit(1) |
| 99 | else: |
| 100 | # if not exists, open with w mode |
| 101 | hdf5_file = h5py.File(hdf5_name, "w") |
| 102 | |
| 103 | # write data to hdf5 |
| 104 | hdf5_file.create_dataset(hdf5_path, data=write_data) |
| 105 | hdf5_file.flush() |
| 106 | hdf5_file.close() |
| 107 | |
| 108 | |
| 109 | class HDF5ScpLoader(object): |
nothing calls this directly
no outgoing calls
no test coverage detected