pwdata 0.5.9.dev0__tar.gz → 0.5.9.dev1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pwdata might be problematic. Click here for more details.

Files changed (94) hide show
  1. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/PKG-INFO +1 -1
  2. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/config.py +7 -0
  3. pwdata-0.5.9.dev1/pwdata/elec_extendedxyz.py +297 -0
  4. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/electrolytes/dataset_preprocess.py +170 -10
  5. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/electrolytes/fragment_graph.py +54 -0
  6. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/electrolytes/models.py +9 -0
  7. pwdata-0.5.9.dev1/pwdata/electrolytes/neutral_registry.py +248 -0
  8. pwdata-0.5.9.dev1/pwdata/electrolytes/neutral_smiles.py +246 -0
  9. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/electrolytes/preprocess.py +98 -11
  10. pwdata-0.5.9.dev1/pwdata/electrolytes/topology_repair.py +214 -0
  11. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/image.py +40 -1
  12. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/utils/constant.py +8 -4
  13. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata.egg-info/PKG-INFO +1 -1
  14. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata.egg-info/SOURCES.txt +4 -0
  15. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/setup.py +1 -1
  16. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/LICENSE +0 -0
  17. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/README.md +0 -0
  18. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/__init__.py +0 -0
  19. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/atomconfig.py +0 -0
  20. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/build/__init__.py +0 -0
  21. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/build/cell.py +0 -0
  22. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/build/geometry.py +0 -0
  23. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/build/supercells.py +0 -0
  24. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/build/write_struc.py +0 -0
  25. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/calculators/__init__.py +0 -0
  26. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/calculators/const.py +0 -0
  27. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/calculators/unitconvert_lmps.py +0 -0
  28. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/calculators/units.py +0 -0
  29. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/check_envs.py +0 -0
  30. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/convert_files.py +0 -0
  31. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/cp2kdata.py +0 -0
  32. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/datasets_saver.py +0 -0
  33. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/deepmd.py +0 -0
  34. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/dump.py +0 -0
  35. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/electrolytes/__init__.py +0 -0
  36. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/electrolytes/charge_inference.py +0 -0
  37. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/electrolytes/extxyz_io.py +0 -0
  38. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/electrolytes/signatures.py +0 -0
  39. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/extendedxyz.py +0 -0
  40. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/__init__.py +0 -0
  41. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/common/__init__.py +0 -0
  42. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/common/registry.py +0 -0
  43. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/common/typing.py +0 -0
  44. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/common/utils.py +0 -0
  45. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/datasets/__init__.py +0 -0
  46. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/datasets/_utils.py +0 -0
  47. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/datasets/ase_datasets.py +0 -0
  48. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/datasets/base_dataset.py +0 -0
  49. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/datasets/embeddings/__init__.py +0 -0
  50. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/datasets/embeddings/atomic_radii.py +0 -0
  51. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/datasets/embeddings/continuous_embeddings.py +0 -0
  52. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/datasets/embeddings/khot_embeddings.py +0 -0
  53. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/datasets/embeddings/qmof_khot_embeddings.py +0 -0
  54. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/datasets/lmdb_database.py +0 -0
  55. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/datasets/target_metadata_guesser.py +0 -0
  56. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/modules/__init__.py +0 -0
  57. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/modules/transforms.py +0 -0
  58. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fairchem/preprocessing/__init__.py +0 -0
  59. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/fragment_charge.py +0 -0
  60. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/lammpsdata.py +0 -0
  61. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/lmps.py +0 -0
  62. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/main.py +0 -0
  63. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/meta.py +0 -0
  64. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/meta_omol.py +0 -0
  65. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/molecule.py +0 -0
  66. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/movement.py +0 -0
  67. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/movement_saver.py +0 -0
  68. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/outcar.py +0 -0
  69. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/pertub/__init__.py +0 -0
  70. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/pertub/perturbation.py +0 -0
  71. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/pertub/scale.py +0 -0
  72. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/poscar.py +0 -0
  73. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/pwmlff.py +0 -0
  74. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/test/__init__.py +0 -0
  75. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/test/bamboo2lmdb.py +0 -0
  76. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/test/extact_outcar_with_ml.py +0 -0
  77. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/test/lmdb2aselmdb.py +0 -0
  78. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/test/matpbe2lmdb.py +0 -0
  79. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/test/matrcan2lmdb.py +0 -0
  80. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/test/mdtrajs2config.py +0 -0
  81. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/test/mptraj2lmdb.py +0 -0
  82. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/test/oc25.py +0 -0
  83. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/test/omol25.py +0 -0
  84. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/test/outcar2mvms.py +0 -0
  85. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/test/readasedb.py +0 -0
  86. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/test/surf_test.py +0 -0
  87. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/test.py +0 -0
  88. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/utils/__init__.py +0 -0
  89. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata/utils/format_change.py +0 -0
  90. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata.egg-info/dependency_links.txt +0 -0
  91. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata.egg-info/entry_points.txt +0 -0
  92. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata.egg-info/requires.txt +0 -0
  93. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/pwdata.egg-info/top_level.txt +0 -0
  94. {pwdata-0.5.9.dev0 → pwdata-0.5.9.dev1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pwdata
3
- Version: 0.5.9.dev0
3
+ Version: 0.5.9.dev1
4
4
  Summary: pwdata is a data pre-processing tool for MatPL, which can be used to extract features and labels. It also provides convenient interfaces for data conversion between different software.
5
5
  Home-page: https://github.com/LonxunQuantum/pwdata
6
6
  Author: LonxunQuantum
@@ -17,6 +17,7 @@ from pwdata.deepmd import DPNPY, DPRAW
17
17
  from pwdata.pwmlff import PWNPY
18
18
  from pwdata.movement_saver import save_to_movement
19
19
  from pwdata.extendedxyz import EXTXYZ, save_to_extxyz
20
+ from pwdata.elec_extendedxyz import ELEC_EXTXYZ, save_to_elec_extxyz
20
21
  from pwdata.datasets_saver import save_to_dataset, get_pw, save_to_raw, save_to_npy
21
22
  from pwdata.build.write_struc import write_config, write_vasp, write_lammps
22
23
  from pwdata.meta import META
@@ -50,6 +51,8 @@ class Save_Data(object):
50
51
  self.image_data = OUTCAR(data_path)
51
52
  elif format.lower() == "extxyz":
52
53
  self.image_data = EXTXYZ(data_path)
54
+ elif format.lower() == "elec-xyz":
55
+ self.image_data = ELEC_EXTXYZ(data_path)
53
56
  elif format.lower() == "vasp/xml":
54
57
  pass
55
58
  elif format.lower() == 'cp2k/md':
@@ -186,6 +189,8 @@ class Config(object):
186
189
  image = OUTCAR(data_path).image_list[index]
187
190
  elif format.lower() == "extxyz":
188
191
  image = EXTXYZ(data_path).image_list[index]
192
+ elif format.lower() == "elec-xyz":
193
+ image = ELEC_EXTXYZ(data_path, index=index).image_list
189
194
  elif format.lower() == "vasp/xml":
190
195
  image = None
191
196
  elif format.lower() == 'cp2k/md':
@@ -269,6 +274,8 @@ class Config(object):
269
274
  save_to_movement(image, data_path, **kwargs)
270
275
  elif format.lower() == "extxyz":
271
276
  save_to_extxyz(image, data_path, **kwargs)
277
+ elif format.lower() == "elec-xyz":
278
+ save_to_elec_extxyz(image, data_path, **kwargs)
272
279
  elif format.lower() == "pwmlff/npy":
273
280
  save_to_dataset(image, data_path=data_path, **kwargs)
274
281
  else:
@@ -0,0 +1,297 @@
1
+ import os
2
+ import re
3
+ from collections import Counter
4
+
5
+ import numpy as np
6
+
7
+ from pwdata.extendedxyz import get_xyz_content, read_properties_data
8
+ from pwdata.image import Image
9
+
10
+
11
+ GLOBAL_ELEMENT_ORDER = [
12
+ "H", "C", "N", "O", "F", "P", "S", "B", "Si",
13
+ "Cl", "Br", "I", "Li", "Na", "K", "Mg", "Ca", "Fe", "Zn",
14
+ ]
15
+ CATION_ELEMENT_ORDER = ["Li", "Na", "K", "Mg", "Ca", "Zn", "Fe"]
16
+
17
+ FLOAT_FIELDS = (
18
+ "number_density_atom_A3",
19
+ "positive_charge_concentration_mol_L",
20
+ "cell_volume_A3",
21
+ "mass_density_g_cm3",
22
+ "negative_charge_concentration_mol_L",
23
+ "total_salt_concentration_mol_L",
24
+ "ionic_strength_mol_L",
25
+ "salt_to_solvent_ratio",
26
+ "total_formal_charge_e",
27
+ )
28
+ INT_FIELDS = (
29
+ "neutral_fragment_count",
30
+ "cation_fragment_count",
31
+ "anion_fragment_count",
32
+ )
33
+ BOOL_FIELDS = (
34
+ "global_features_valid",
35
+ "fragment_features_valid",
36
+ "formal_charge_valid",
37
+ "recognition_failed",
38
+ "salt_to_solvent_ratio_valid",
39
+ )
40
+ METADATA_FIELDS = (
41
+ "global_element_order",
42
+ "element_fraction",
43
+ "cation_element_order",
44
+ "cation_concentration_mol_L",
45
+ "cell_volume_A3",
46
+ "mass_density_g_cm3",
47
+ "number_density_atom_A3",
48
+ "neutral_fragment_count",
49
+ "cation_fragment_count",
50
+ "anion_fragment_count",
51
+ "positive_charge_concentration_mol_L",
52
+ "negative_charge_concentration_mol_L",
53
+ "total_salt_concentration_mol_L",
54
+ "ionic_strength_mol_L",
55
+ "salt_to_solvent_ratio",
56
+ "total_formal_charge_e",
57
+ "global_features_valid",
58
+ "fragment_features_valid",
59
+ "formal_charge_valid",
60
+ "salt_to_solvent_ratio_valid",
61
+ "recognition_failed",
62
+ )
63
+
64
+
65
+ class ELEC_EXTXYZ(object):
66
+ def __init__(self, xyz_file, index=None) -> None:
67
+ self.image_list = read_elec_structures(xyz_file, index=index)
68
+
69
+ def get(self):
70
+ return self.image_list
71
+
72
+
73
+ def format_metadata_value(value):
74
+ if isinstance(value, (bool, np.bool_)):
75
+ return "T" if bool(value) else "F"
76
+ if isinstance(value, (list, tuple, np.ndarray)):
77
+ return '"{}"'.format(" ".join(str(item) for item in value))
78
+ return str(value)
79
+
80
+
81
+ def get_elec_xyz_content(image):
82
+ metadata = {field: getattr(image, field, None) for field in METADATA_FIELDS}
83
+ validate_ordered_arrays(metadata)
84
+
85
+ content = get_xyz_content(image)
86
+ first_newline = content.find("\n")
87
+ second_newline = content.find("\n", first_newline + 1)
88
+ header = content[first_newline + 1:second_newline].rstrip()
89
+ fields = [
90
+ "{}={}".format(field, format_metadata_value(metadata[field]))
91
+ for field in METADATA_FIELDS
92
+ if metadata[field] is not None
93
+ ]
94
+ if fields:
95
+ header = "{} {}".format(header, " ".join(fields))
96
+ return content[:first_newline + 1] + header + content[second_newline:]
97
+
98
+
99
+ def save_to_elec_extxyz(
100
+ image_data_all,
101
+ data_path,
102
+ data_name=None,
103
+ random=False,
104
+ seed=None,
105
+ retain_raw=False,
106
+ write_patthen="a",
107
+ ):
108
+ if not os.path.exists(data_path):
109
+ os.makedirs(data_path)
110
+ if data_name is None:
111
+ data_name = "elec-extendedxyz.xyz"
112
+ elif ".xyz" not in data_name:
113
+ data_name += ".xyz"
114
+
115
+ indices = np.arange(len(image_data_all))
116
+ if seed is not None:
117
+ np.random.seed(seed)
118
+ if random:
119
+ np.random.shuffle(indices)
120
+
121
+ with open(os.path.join(data_path, data_name), write_patthen) as output:
122
+ for index in indices:
123
+ output.write(get_elec_xyz_content(image_data_all[index]))
124
+
125
+
126
+ def parse_bool(field_name, value):
127
+ normalized = value.strip().lower()
128
+ if normalized in {"t", "true"}:
129
+ return True
130
+ if normalized in {"f", "false"}:
131
+ return False
132
+ raise ValueError(
133
+ "{} must be T/F or True/False, got {!r}".format(field_name, value)
134
+ )
135
+
136
+
137
+ def validate_ordered_arrays(metadata):
138
+ pairs = (
139
+ ("element_fraction", "global_element_order", GLOBAL_ELEMENT_ORDER),
140
+ (
141
+ "cation_concentration_mol_L",
142
+ "cation_element_order",
143
+ CATION_ELEMENT_ORDER,
144
+ ),
145
+ )
146
+ for array_field, order_field, expected_order in pairs:
147
+ order = metadata.get(order_field)
148
+ values = metadata.get(array_field)
149
+ if order is not None and order != expected_order:
150
+ raise ValueError(
151
+ "{} must equal the fixed order {}, got {}".format(
152
+ order_field, expected_order, order
153
+ )
154
+ )
155
+ if values is not None and order is None:
156
+ raise ValueError(
157
+ "{} requires {} in the same frame".format(
158
+ array_field, order_field
159
+ )
160
+ )
161
+ if values is not None:
162
+ try:
163
+ numeric_values = np.asarray(values, dtype=np.float64)
164
+ except (TypeError, ValueError) as error:
165
+ raise ValueError(
166
+ "{} must contain only numeric values".format(array_field)
167
+ ) from error
168
+ expected_shape = (len(expected_order),)
169
+ if numeric_values.shape != expected_shape:
170
+ raise ValueError(
171
+ "{} shape {} does not match {} expected shape {}".format(
172
+ array_field,
173
+ numeric_values.shape,
174
+ order_field,
175
+ expected_shape,
176
+ )
177
+ )
178
+
179
+
180
+ def parse_elec_metadata(result_dict):
181
+ metadata = {}
182
+ if "global_element_order" in result_dict:
183
+ metadata["global_element_order"] = result_dict["global_element_order"].split()
184
+ if "element_fraction" in result_dict:
185
+ metadata["element_fraction"] = np.asarray(
186
+ result_dict["element_fraction"].split(), dtype=np.float64
187
+ )
188
+ if "cation_element_order" in result_dict:
189
+ metadata["cation_element_order"] = result_dict["cation_element_order"].split()
190
+ if "cation_concentration_mol_l" in result_dict:
191
+ metadata["cation_concentration_mol_L"] = np.asarray(
192
+ result_dict["cation_concentration_mol_l"].split(), dtype=np.float64
193
+ )
194
+ for field in FLOAT_FIELDS:
195
+ key = field.lower()
196
+ if key in result_dict:
197
+ metadata[field] = float(result_dict[key])
198
+ for field in INT_FIELDS:
199
+ key = field.lower()
200
+ if key in result_dict:
201
+ metadata[field] = int(result_dict[key])
202
+ for field in BOOL_FIELDS:
203
+ key = field.lower()
204
+ if key in result_dict:
205
+ metadata[field] = parse_bool(field, result_dict[key])
206
+ if "fragment_charge_status" in result_dict:
207
+ metadata["fragment_charge_status"] = result_dict["fragment_charge_status"]
208
+ validate_ordered_arrays(metadata)
209
+ return metadata
210
+
211
+
212
+ def read_elec_structures(file_name, index=None):
213
+ if index is not None and not isinstance(index, (int, slice)):
214
+ raise TypeError("elec-xyz index must be an int, slice, or None")
215
+
216
+ image_list = []
217
+ stream_index = index if isinstance(index, int) and index >= 0 else None
218
+ frame_index = 0
219
+ with open(file_name, "r") as file:
220
+ while True:
221
+ num_lines_per_frame = file.readline().strip()
222
+ if not num_lines_per_frame:
223
+ break
224
+ num_lines_per_frame = int(num_lines_per_frame)
225
+ if num_lines_per_frame <= 0:
226
+ raise ValueError("Number of lines per frame should be a positive integer")
227
+ lines = [file.readline().strip()]
228
+ lines.extend(file.readline().strip() for _ in range(num_lines_per_frame))
229
+ image = read_one_elec_structure_from_lines(lines)
230
+ if stream_index is None:
231
+ image_list.append(image)
232
+ elif frame_index == stream_index:
233
+ image_list.append(image)
234
+ break
235
+ frame_index += 1
236
+ if not image_list:
237
+ if stream_index is not None:
238
+ raise IndexError("elec-xyz index {} is out of range".format(index))
239
+ raise ValueError("elec-xyz file {} parsing failed".format(file_name))
240
+ if index is not None and stream_index is None:
241
+ selected = image_list[index]
242
+ image_list = selected if isinstance(selected, list) else [selected]
243
+ return image_list
244
+
245
+
246
+ def read_one_elec_structure_from_lines(lines):
247
+ image = Image()
248
+ image.format = "elec-xyz"
249
+ image.atom_nums = len(lines) - 1
250
+
251
+ pattern = r'(\w+)=("[^"]*"|\S+)'
252
+ matches = re.findall(pattern, lines[0])
253
+ result_dict = {key.lower(): value.strip('"') for key, value in matches}
254
+
255
+ image.Ep = float(result_dict["energy"])
256
+ properties = result_dict["properties"].split(":")
257
+ atom_types_image, position, force, bec, fragment, charge = read_properties_data(
258
+ image.atom_nums, properties, lines[1:]
259
+ )
260
+ image.force = force
261
+ image.position = position
262
+ image.atom_types_image = atom_types_image
263
+ image.bec = bec
264
+ image.fragment = fragment
265
+ image.charge = charge
266
+
267
+ metadata = parse_elec_metadata(result_dict)
268
+ for field, value in metadata.items():
269
+ setattr(image, field, value)
270
+
271
+ lattice = [float(item) for item in result_dict["lattice"].split()]
272
+ if len(lattice) != 9:
273
+ raise ValueError("lattice in elec-xyz file should have 9 elements")
274
+ image.lattice = np.asarray(lattice).reshape(3, 3)
275
+ image.cartesian = True
276
+
277
+ element_counts = Counter(atom_types_image)
278
+ image.atom_type = np.asarray(list(element_counts.keys()))
279
+ image.atom_type_num = np.asarray(list(element_counts.values()))
280
+
281
+ if "virial" in result_dict:
282
+ virial = [float(item) for item in result_dict["virial"].split()]
283
+ if len(virial) != 9:
284
+ raise ValueError("virial in elec-xyz file should have 9 elements")
285
+ image.virial = np.asarray(virial).reshape(3, 3)
286
+ elif "stress" in result_dict:
287
+ stress = [float(item) for item in result_dict["stress"].split()]
288
+ if len(stress) != 9:
289
+ raise ValueError("stress in elec-xyz file should have 9 elements")
290
+ volume = np.abs(np.linalg.det(image.lattice))
291
+ image.virial = -volume * np.asarray(stress).reshape(3, 3)
292
+
293
+ atomic_energy, _, _, _ = np.linalg.lstsq(
294
+ [image.atom_type_num], np.asarray([image.Ep]), rcond=1e-3
295
+ )
296
+ image.atomic_energy = np.repeat(atomic_energy, image.atom_type_num)
297
+ return image