| 56 | |
| 57 | |
| 58 | class HumanDataCacheWriter(): |
| 59 | def __init__(self, |
| 60 | slice_size: int, |
| 61 | data_len: int, |
| 62 | keypoints_info: dict, |
| 63 | non_sliced_data: dict, |
| 64 | key_strict: bool = True): |
| 65 | self.slice_size = slice_size |
| 66 | self.data_len = data_len |
| 67 | self.keypoints_info = keypoints_info |
| 68 | self.non_sliced_data = non_sliced_data |
| 69 | self.sliced_data = {} |
| 70 | self.key_strict = key_strict |
| 71 | |
| 72 | def update_sliced_dict(self, sliced_dict): |
| 73 | self.sliced_data.update(sliced_dict) |
| 74 | |
| 75 | def dump(self, npz_path: str, overwrite: bool = True): |
| 76 | """Dump keys and items to an npz file. |
| 77 | |
| 78 | Args: |
| 79 | npz_path (str): |
| 80 | Path to a dumped npz file. |
| 81 | overwrite (bool, optional): |
| 82 | Whether to overwrite if there is already a file. |
| 83 | Defaults to True. |
| 84 | |
| 85 | Raises: |
| 86 | ValueError: |
| 87 | npz_path does not end with '.npz'. |
| 88 | FileExistsError: |
| 89 | When overwrite is False and file exists. |
| 90 | """ |
| 91 | if not check_path_suffix(npz_path, ['.npz']): |
| 92 | raise ValueError('Not an npz file.') |
| 93 | if not overwrite: |
| 94 | if check_path_existence(npz_path, 'file') == Existence.FileExist: |
| 95 | raise FileExistsError |
| 96 | dict_to_dump = { |
| 97 | 'slice_size': self.slice_size, |
| 98 | 'data_len': self.data_len, |
| 99 | 'keypoints_info': self.keypoints_info, |
| 100 | 'non_sliced_data': self.non_sliced_data, |
| 101 | 'key_strict': self.key_strict, |
| 102 | } |
| 103 | dict_to_dump.update(self.sliced_data) |
| 104 | np.savez_compressed(npz_path, **dict_to_dump) |