|
| 1 | +from typing import Union, List |
| 2 | + |
1 | 3 | import dpdata
|
2 | 4 | import dpdata.deepmd.raw
|
3 | 5 | import dpdata.deepmd.comp
|
@@ -69,41 +71,164 @@ class DeePMDHDF5Format(Format):
|
69 | 71 | >>> import dpdata
|
70 | 72 | >>> dpdata.MultiSystems().from_deepmd_npy("data").to_deepmd_hdf5("data.hdf5")
|
71 | 73 | """
|
72 |
| - def from_system(self, file_name, type_map=None, **kwargs): |
73 |
| - s = file_name.split("#") |
74 |
| - name = s[1] if len(s) > 1 else "" |
75 |
| - with h5py.File(s[0], 'r') as f: |
76 |
| - return dpdata.deepmd.hdf5.to_system_data(f, name, type_map=type_map, labels=False) |
| 74 | + def _from_system(self, file_name: Union[str, h5py.Group, h5py.File], type_map: List[str], labels: bool): |
| 75 | + """Convert HDF5 file to System or LabeledSystem data. |
| 76 | + |
| 77 | + This method is used to switch from labeled or non-labeled options. |
| 78 | + |
| 79 | + Parameters |
| 80 | + ---------- |
| 81 | + file_name : str or h5py.Group or h5py.File |
| 82 | + file name of the HDF5 file or HDF5 object. If it is a string, |
| 83 | + hashtag is used to split path to the HDF5 file and the HDF5 group |
| 84 | + type_map : dict[str] |
| 85 | + type map |
| 86 | + labels : bool |
| 87 | + if Labeled |
| 88 | +
|
| 89 | + Returns |
| 90 | + ------- |
| 91 | + dict |
| 92 | + System or LabeledSystem data |
| 93 | +
|
| 94 | + Raises |
| 95 | + ------ |
| 96 | + TypeError |
| 97 | + file_name is not str or h5py.Group or h5py.File |
| 98 | + """ |
| 99 | + if isinstance(file_name, (h5py.Group, h5py.File)): |
| 100 | + return dpdata.deepmd.hdf5.to_system_data(file_name, "", type_map=type_map, labels=labels) |
| 101 | + elif isinstance(file_name, str): |
| 102 | + s = file_name.split("#") |
| 103 | + name = s[1] if len(s) > 1 else "" |
| 104 | + with h5py.File(s[0], 'r') as f: |
| 105 | + return dpdata.deepmd.hdf5.to_system_data(f, name, type_map=type_map, labels=labels) |
| 106 | + else: |
| 107 | + raise TypeError("Unsupported file_name") |
| 108 | + |
| 109 | + def from_system(self, |
| 110 | + file_name: Union[str, h5py.Group, h5py.File], |
| 111 | + type_map: List[str]=None, |
| 112 | + **kwargs) -> dict: |
| 113 | + """Convert HDF5 file to System data. |
| 114 | +
|
| 115 | + Parameters |
| 116 | + ---------- |
| 117 | + file_name : str or h5py.Group or h5py.File |
| 118 | + file name of the HDF5 file or HDF5 object. If it is a string, |
| 119 | + hashtag is used to split path to the HDF5 file and the HDF5 group |
| 120 | + type_map : dict[str] |
| 121 | + type map |
| 122 | +
|
| 123 | + Returns |
| 124 | + ------- |
| 125 | + dict |
| 126 | + System data |
| 127 | +
|
| 128 | + Raises |
| 129 | + ------ |
| 130 | + TypeError |
| 131 | + file_name is not str or h5py.Group or h5py.File |
| 132 | + """ |
| 133 | + return self._from_system(file_name, type_map=type_map, labels=False) |
| 134 | + |
| 135 | + def from_labeled_system(self, |
| 136 | + file_name: Union[str, h5py.Group, h5py.File], |
| 137 | + type_map: List[str]=None, |
| 138 | + **kwargs) -> dict: |
| 139 | + """Convert HDF5 file to LabeledSystem data. |
| 140 | +
|
| 141 | + Parameters |
| 142 | + ---------- |
| 143 | + file_name : str or h5py.Group or h5py.File |
| 144 | + file name of the HDF5 file or HDF5 object. If it is a string, |
| 145 | + hashtag is used to split path to the HDF5 file and the HDF5 group |
| 146 | + type_map : dict[str] |
| 147 | + type map |
| 148 | +
|
| 149 | + Returns |
| 150 | + ------- |
| 151 | + dict |
| 152 | + LabeledSystem data |
| 153 | +
|
| 154 | + Raises |
| 155 | + ------ |
| 156 | + TypeError |
| 157 | + file_name is not str or h5py.Group or h5py.File |
| 158 | + """ |
| 159 | + return self._from_system(file_name, type_map=type_map, labels=True) |
77 | 160 |
|
78 |
| - def from_labeled_system(self, file_name, type_map=None, **kwargs): |
79 |
| - s = file_name.split("#") |
80 |
| - name = s[1] if len(s) > 1 else "" |
81 |
| - with h5py.File(s[0], 'r') as f: |
82 |
| - return dpdata.deepmd.hdf5.to_system_data(f, name, type_map=type_map, labels=True) |
83 |
| - |
84 | 161 | def to_system(self,
|
85 | 162 | data : dict,
|
86 |
| - file_name : str, |
| 163 | + file_name: Union[str, h5py.Group, h5py.File], |
87 | 164 | set_size : int = 5000,
|
88 | 165 | comp_prec : np.dtype = np.float64,
|
89 | 166 | **kwargs):
|
90 |
| - s = file_name.split("#") |
91 |
| - name = s[1] if len(s) > 1 else "" |
92 |
| - mode = 'a' if name else 'w' |
93 |
| - with h5py.File(s[0], mode) as f: |
94 |
| - dpdata.deepmd.hdf5.dump(f, name, data, set_size = set_size, comp_prec = comp_prec) |
| 167 | + """Convert System data to HDF5 file. |
| 168 | + |
| 169 | + Parameters |
| 170 | + ---------- |
| 171 | + data : dict |
| 172 | + data dict |
| 173 | + file_name : str or h5py.Group or h5py.File |
| 174 | + file name of the HDF5 file or HDF5 object. If it is a string, |
| 175 | + hashtag is used to split path to the HDF5 file and the HDF5 group |
| 176 | + set_size : int, default=5000 |
| 177 | + set size |
| 178 | + comp_prec : np.dtype |
| 179 | + data precision |
| 180 | + """ |
| 181 | + if isinstance(file_name, (h5py.Group, h5py.File)): |
| 182 | + dpdata.deepmd.hdf5.dump(file_name, "", data, set_size = set_size, comp_prec = comp_prec) |
| 183 | + elif isinstance(file_name, str): |
| 184 | + s = file_name.split("#") |
| 185 | + name = s[1] if len(s) > 1 else "" |
| 186 | + with h5py.File(s[0], 'w') as f: |
| 187 | + dpdata.deepmd.hdf5.dump(f, name, data, set_size = set_size, comp_prec = comp_prec) |
| 188 | + else: |
| 189 | + raise TypeError("Unsupported file_name") |
95 | 190 |
|
96 | 191 | def from_multi_systems(self,
|
97 |
| - directory, |
98 |
| - **kwargs): |
| 192 | + directory: str, |
| 193 | + **kwargs) -> h5py.Group: |
| 194 | + """Generate HDF5 groups from a HDF5 file, which will be |
| 195 | + passed to `from_system`. |
| 196 | + |
| 197 | + Parameters |
| 198 | + ---------- |
| 199 | + directory : str |
| 200 | + HDF5 file name |
| 201 | +
|
| 202 | + Yields |
| 203 | + ------ |
| 204 | + h5py.Group |
| 205 | + a HDF5 group in the HDF5 file |
| 206 | + """ |
99 | 207 | with h5py.File(directory, 'r') as f:
|
100 |
| - return ["%s#%s" % (directory, ff) for ff in f.keys()] |
| 208 | + for ff in f.keys(): |
| 209 | + yield f[ff] |
101 | 210 |
|
102 | 211 | def to_multi_systems(self,
|
103 |
| - formulas, |
104 |
| - directory, |
105 |
| - **kwargs): |
106 |
| - return ["%s#%s" % (directory, ff) for ff in formulas] |
| 212 | + formulas: List[str], |
| 213 | + directory: str, |
| 214 | + **kwargs) -> h5py.Group: |
| 215 | + """Generate HDF5 groups, which will be passed to `to_system`. |
| 216 | + |
| 217 | + Parameters |
| 218 | + ---------- |
| 219 | + formulas : list[str] |
| 220 | + formulas of MultiSystems |
| 221 | + directory : str |
| 222 | + HDF5 file name |
| 223 | + |
| 224 | + Yields |
| 225 | + ------ |
| 226 | + h5py.Group |
| 227 | + a HDF5 group with the name of formula |
| 228 | + """ |
| 229 | + with h5py.File(directory, 'w') as f: |
| 230 | + for ff in formulas: |
| 231 | + yield f.create_group(ff) |
107 | 232 |
|
108 | 233 |
|
109 | 234 | @Driver.register("dp")
|
|
0 commit comments