pwdata 0.5.7__tar.gz → 0.5.9.dev0__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.
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/PKG-INFO +1 -1
- pwdata-0.5.9.dev0/pwdata/electrolytes/__init__.py +19 -0
- pwdata-0.5.9.dev0/pwdata/electrolytes/charge_inference.py +347 -0
- pwdata-0.5.9.dev0/pwdata/electrolytes/dataset_preprocess.py +428 -0
- pwdata-0.5.9.dev0/pwdata/electrolytes/extxyz_io.py +151 -0
- pwdata-0.5.9.dev0/pwdata/electrolytes/fragment_graph.py +166 -0
- pwdata-0.5.9.dev0/pwdata/electrolytes/models.py +122 -0
- pwdata-0.5.9.dev0/pwdata/electrolytes/preprocess.py +546 -0
- pwdata-0.5.9.dev0/pwdata/electrolytes/signatures.py +67 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/extendedxyz.py +44 -14
- pwdata-0.5.9.dev0/pwdata/fragment_charge.py +529 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/image.py +12 -1
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/meta.py +54 -15
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata.egg-info/PKG-INFO +1 -1
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata.egg-info/SOURCES.txt +9 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/setup.py +1 -1
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/LICENSE +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/README.md +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/__init__.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/atomconfig.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/build/__init__.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/build/cell.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/build/geometry.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/build/supercells.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/build/write_struc.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/calculators/__init__.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/calculators/const.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/calculators/unitconvert_lmps.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/calculators/units.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/check_envs.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/config.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/convert_files.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/cp2kdata.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/datasets_saver.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/deepmd.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/dump.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/__init__.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/common/__init__.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/common/registry.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/common/typing.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/common/utils.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/datasets/__init__.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/datasets/_utils.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/datasets/ase_datasets.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/datasets/base_dataset.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/datasets/embeddings/__init__.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/datasets/embeddings/atomic_radii.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/datasets/embeddings/continuous_embeddings.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/datasets/embeddings/khot_embeddings.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/datasets/embeddings/qmof_khot_embeddings.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/datasets/lmdb_database.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/datasets/target_metadata_guesser.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/modules/__init__.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/modules/transforms.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/fairchem/preprocessing/__init__.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/lammpsdata.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/lmps.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/main.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/meta_omol.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/molecule.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/movement.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/movement_saver.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/outcar.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/pertub/__init__.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/pertub/perturbation.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/pertub/scale.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/poscar.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/pwmlff.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/test/__init__.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/test/bamboo2lmdb.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/test/extact_outcar_with_ml.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/test/lmdb2aselmdb.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/test/matpbe2lmdb.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/test/matrcan2lmdb.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/test/mdtrajs2config.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/test/mptraj2lmdb.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/test/oc25.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/test/omol25.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/test/outcar2mvms.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/test/readasedb.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/test/surf_test.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/test.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/utils/__init__.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/utils/constant.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata/utils/format_change.py +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata.egg-info/dependency_links.txt +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata.egg-info/entry_points.txt +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata.egg-info/requires.txt +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/pwdata.egg-info/top_level.txt +0 -0
- {pwdata-0.5.7 → pwdata-0.5.9.dev0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: pwdata
|
|
3
|
-
Version: 0.5.
|
|
3
|
+
Version: 0.5.9.dev0
|
|
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
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""Electrolyte fragment and formal-charge preprocessing."""
|
|
2
|
+
|
|
3
|
+
from .extxyz_io import iter_extxyz, write_annotated_frame
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"analyze_frame",
|
|
7
|
+
"iter_extxyz",
|
|
8
|
+
"preprocess_file",
|
|
9
|
+
"verify_output",
|
|
10
|
+
"write_annotated_frame",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def __getattr__(name):
|
|
15
|
+
if name in {"analyze_frame", "preprocess_file", "verify_output"}:
|
|
16
|
+
from . import preprocess
|
|
17
|
+
|
|
18
|
+
return getattr(preprocess, name)
|
|
19
|
+
raise AttributeError(name)
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
"""Charge rules, per-frame neutrality inference, and Fe/Zn residuals."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections import Counter, defaultdict
|
|
6
|
+
import os
|
|
7
|
+
from typing import Dict, List, Mapping, Optional, Sequence
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
import yaml
|
|
11
|
+
|
|
12
|
+
from .models import (
|
|
13
|
+
ChargeAssignment,
|
|
14
|
+
FragmentChargeInput,
|
|
15
|
+
FragmentSignature,
|
|
16
|
+
FrameChargeResult,
|
|
17
|
+
FrameEquation,
|
|
18
|
+
InferenceResult,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
DEFAULT_RULES_PATH = os.path.join(os.path.dirname(__file__), "fragment_charge_rules.yaml")
|
|
23
|
+
DEFAULT_OVERRIDES_PATH = os.path.join(os.path.dirname(__file__), "fragment_charge_overrides.yaml")
|
|
24
|
+
FIXED_METAL_CHARGES = {"Li": 1.0, "Na": 1.0, "K": 1.0, "Mg": 2.0, "Ca": 2.0}
|
|
25
|
+
VARIABLE_METALS = {"Fe", "Zn"}
|
|
26
|
+
TOLERANCE = 1.0e-8
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def load_rules(path: Optional[str] = None) -> dict:
|
|
30
|
+
with open(path or DEFAULT_RULES_PATH, "r", encoding="utf-8") as handle:
|
|
31
|
+
data = yaml.safe_load(handle) or {}
|
|
32
|
+
if "inference" not in data or "monatomic" not in data or "topology_rules" not in data:
|
|
33
|
+
raise ValueError("charge rules require inference, monatomic, and topology_rules sections")
|
|
34
|
+
return data
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def load_overrides(path: Optional[str] = None) -> Dict[str, ChargeAssignment]:
|
|
38
|
+
with open(path or DEFAULT_OVERRIDES_PATH, "r", encoding="utf-8") as handle:
|
|
39
|
+
data = yaml.safe_load(handle) or {}
|
|
40
|
+
assignments = {}
|
|
41
|
+
for signature_id, entry in (data.get("signatures") or {}).items():
|
|
42
|
+
if "charge" not in entry:
|
|
43
|
+
raise ValueError(f"override {signature_id} has no charge")
|
|
44
|
+
assignments[signature_id] = ChargeAssignment(float(entry["charge"]), "manual_override", "resolved")
|
|
45
|
+
return assignments
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def load_charge_registry(path: str) -> Dict[str, ChargeAssignment]:
|
|
49
|
+
"""Load only resolved entries from a generated or manually curated registry."""
|
|
50
|
+
with open(path, "r", encoding="utf-8") as handle:
|
|
51
|
+
data = yaml.safe_load(handle) or {}
|
|
52
|
+
assignments = {}
|
|
53
|
+
for signature_id, entry in (data.get("signatures") or {}).items():
|
|
54
|
+
if entry.get("charge") is None or entry.get("status", "resolved") != "resolved":
|
|
55
|
+
continue
|
|
56
|
+
assignments[signature_id] = ChargeAssignment(
|
|
57
|
+
float(entry["charge"]),
|
|
58
|
+
str(entry.get("source") or "fixed_registry"),
|
|
59
|
+
"resolved",
|
|
60
|
+
)
|
|
61
|
+
return assignments
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def match_builtin_rule(signature: FragmentSignature, rules: Optional[dict] = None) -> Optional[ChargeAssignment]:
|
|
65
|
+
rules = rules or load_rules()
|
|
66
|
+
monatomic = rules["monatomic"].get(signature.formula)
|
|
67
|
+
degree_counts = signature.invariants.get("degree_counts", {})
|
|
68
|
+
edge_counts = signature.invariants.get("edge_counts", {})
|
|
69
|
+
if monatomic is not None and degree_counts == {f"{signature.formula}:0": 1} and not edge_counts:
|
|
70
|
+
return ChargeAssignment(float(monatomic), f"monatomic_rule:{signature.formula}", "resolved")
|
|
71
|
+
for rule in rules["topology_rules"]:
|
|
72
|
+
if signature.formula != rule["formula"]:
|
|
73
|
+
continue
|
|
74
|
+
if degree_counts != rule["degree_counts"] or edge_counts != rule["edge_counts"]:
|
|
75
|
+
continue
|
|
76
|
+
return ChargeAssignment(float(rule["charge"]), f"topology_rule:{rule['name']}", "resolved")
|
|
77
|
+
return None
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _seed_values(seed_assignments: Mapping[str, object]) -> tuple:
|
|
81
|
+
charges: Dict[str, int] = {}
|
|
82
|
+
sources: Dict[str, str] = {}
|
|
83
|
+
for signature_id, assignment in seed_assignments.items():
|
|
84
|
+
if isinstance(assignment, ChargeAssignment):
|
|
85
|
+
raw_charge = assignment.charge
|
|
86
|
+
source = assignment.source
|
|
87
|
+
else:
|
|
88
|
+
raw_charge = float(assignment)
|
|
89
|
+
source = "seed"
|
|
90
|
+
rounded = int(round(raw_charge))
|
|
91
|
+
if abs(raw_charge - rounded) > TOLERANCE:
|
|
92
|
+
raise ValueError(f"signature inference seed must be integral: {signature_id}={raw_charge}")
|
|
93
|
+
charges[signature_id] = rounded
|
|
94
|
+
sources[signature_id] = source
|
|
95
|
+
return charges, sources
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _equation_state(equation: FrameEquation, charges: Mapping[str, int]) -> tuple:
|
|
99
|
+
target = float(equation.target)
|
|
100
|
+
unknown = {}
|
|
101
|
+
for signature_id, coefficient in equation.coefficients.items():
|
|
102
|
+
if signature_id in charges:
|
|
103
|
+
target -= coefficient * charges[signature_id]
|
|
104
|
+
else:
|
|
105
|
+
unknown[signature_id] = int(coefficient)
|
|
106
|
+
return unknown, target
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _record_conflict(
|
|
110
|
+
conflicts: Dict[tuple, Dict[str, object]],
|
|
111
|
+
frame_index: int,
|
|
112
|
+
reason: str,
|
|
113
|
+
detail: object,
|
|
114
|
+
) -> None:
|
|
115
|
+
key = (frame_index, reason, repr(detail))
|
|
116
|
+
conflicts.setdefault(
|
|
117
|
+
key,
|
|
118
|
+
{"frame_index": frame_index, "reason": reason, "detail": detail},
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _candidate_charge(
|
|
123
|
+
residual: float,
|
|
124
|
+
coefficient: int,
|
|
125
|
+
min_charge: int,
|
|
126
|
+
max_charge: int,
|
|
127
|
+
) -> tuple[Optional[int], Optional[str], object]:
|
|
128
|
+
candidate = residual / coefficient
|
|
129
|
+
rounded = int(round(candidate))
|
|
130
|
+
if abs(candidate - rounded) > TOLERANCE:
|
|
131
|
+
return None, "non_integral_single_unknown", candidate
|
|
132
|
+
if not min_charge <= rounded <= max_charge:
|
|
133
|
+
return None, "charge_out_of_range", rounded
|
|
134
|
+
return rounded, None, None
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _summarize_votes(
|
|
138
|
+
vote_counts: Mapping[int, int],
|
|
139
|
+
frame_examples: Mapping[int, Sequence[int]],
|
|
140
|
+
invalid_vote_count: int = 0,
|
|
141
|
+
invalid_frame_examples: Optional[Sequence[int]] = None,
|
|
142
|
+
) -> Dict[str, object]:
|
|
143
|
+
valid_vote_count = int(sum(vote_counts.values()))
|
|
144
|
+
attempted_vote_count = valid_vote_count + int(invalid_vote_count)
|
|
145
|
+
if vote_counts:
|
|
146
|
+
winner_charge, winning_vote_count = min(
|
|
147
|
+
vote_counts.items(),
|
|
148
|
+
key=lambda item: (-item[1], item[0]),
|
|
149
|
+
)
|
|
150
|
+
else:
|
|
151
|
+
winner_charge, winning_vote_count = None, 0
|
|
152
|
+
return {
|
|
153
|
+
"winning_charge": None if winner_charge is None else int(winner_charge),
|
|
154
|
+
"attempted_vote_count": attempted_vote_count,
|
|
155
|
+
"valid_vote_count": valid_vote_count,
|
|
156
|
+
"invalid_vote_count": int(invalid_vote_count),
|
|
157
|
+
"winning_vote_count": int(winning_vote_count),
|
|
158
|
+
"vote_confidence": float(winning_vote_count / attempted_vote_count) if attempted_vote_count else 0.0,
|
|
159
|
+
"vote_counts": {int(charge): int(count) for charge, count in sorted(vote_counts.items())},
|
|
160
|
+
"example_frame_indices": {
|
|
161
|
+
int(charge): [int(frame_index) for frame_index in examples]
|
|
162
|
+
for charge, examples in sorted(frame_examples.items())
|
|
163
|
+
},
|
|
164
|
+
"invalid_frame_examples": [
|
|
165
|
+
int(frame_index) for frame_index in (invalid_frame_examples or [])
|
|
166
|
+
],
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _single_unknown_vote_stats(
|
|
171
|
+
equations: Sequence[FrameEquation],
|
|
172
|
+
charges: Mapping[str, int],
|
|
173
|
+
min_charge: int,
|
|
174
|
+
max_charge: int,
|
|
175
|
+
conflicts: Dict[tuple, Dict[str, object]],
|
|
176
|
+
) -> Dict[str, Dict[str, object]]:
|
|
177
|
+
vote_counts: Dict[str, Counter] = defaultdict(Counter)
|
|
178
|
+
frame_examples: Dict[str, Dict[int, List[int]]] = defaultdict(lambda: defaultdict(list))
|
|
179
|
+
invalid_vote_counts: Counter = Counter()
|
|
180
|
+
invalid_frame_examples: Dict[str, List[int]] = defaultdict(list)
|
|
181
|
+
for equation in equations:
|
|
182
|
+
unknown, residual = _equation_state(equation, charges)
|
|
183
|
+
if not unknown:
|
|
184
|
+
if abs(residual) > TOLERANCE:
|
|
185
|
+
_record_conflict(conflicts, equation.frame_index, "known_charge_equation_conflict", residual)
|
|
186
|
+
continue
|
|
187
|
+
if len(unknown) != 1:
|
|
188
|
+
continue
|
|
189
|
+
signature_id, coefficient = next(iter(unknown.items()))
|
|
190
|
+
candidate, reason, detail = _candidate_charge(residual, coefficient, min_charge, max_charge)
|
|
191
|
+
if reason is not None:
|
|
192
|
+
invalid_vote_counts[signature_id] += 1
|
|
193
|
+
examples = invalid_frame_examples[signature_id]
|
|
194
|
+
if len(examples) < 20:
|
|
195
|
+
examples.append(equation.frame_index)
|
|
196
|
+
_record_conflict(conflicts, equation.frame_index, reason, detail)
|
|
197
|
+
continue
|
|
198
|
+
vote_counts[signature_id][candidate] += 1
|
|
199
|
+
examples = frame_examples[signature_id][candidate]
|
|
200
|
+
if len(examples) < 20:
|
|
201
|
+
examples.append(equation.frame_index)
|
|
202
|
+
signature_ids = set(vote_counts) | set(invalid_vote_counts)
|
|
203
|
+
return {
|
|
204
|
+
signature_id: _summarize_votes(
|
|
205
|
+
vote_counts[signature_id],
|
|
206
|
+
frame_examples[signature_id],
|
|
207
|
+
invalid_vote_counts[signature_id],
|
|
208
|
+
invalid_frame_examples[signature_id],
|
|
209
|
+
)
|
|
210
|
+
for signature_id in signature_ids
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def infer_signature_charges(
|
|
215
|
+
equations: Sequence[FrameEquation],
|
|
216
|
+
seed_assignments: Mapping[str, object],
|
|
217
|
+
min_charge: int = -4,
|
|
218
|
+
max_charge: int = 4,
|
|
219
|
+
min_vote_count: int = 1,
|
|
220
|
+
min_vote_fraction: float = 1.0,
|
|
221
|
+
) -> InferenceResult:
|
|
222
|
+
"""Solve shared signature charges from independent per-frame equations."""
|
|
223
|
+
if min_vote_count < 1:
|
|
224
|
+
raise ValueError("min_vote_count must be at least 1")
|
|
225
|
+
if not 0.5 < min_vote_fraction <= 1.0:
|
|
226
|
+
raise ValueError("min_vote_fraction must be in (0.5, 1.0]")
|
|
227
|
+
charges, sources = _seed_values(seed_assignments)
|
|
228
|
+
conflicts: Dict[tuple, Dict[str, object]] = {}
|
|
229
|
+
vote_stats: Dict[str, Dict[str, object]] = {}
|
|
230
|
+
|
|
231
|
+
changed = True
|
|
232
|
+
while changed:
|
|
233
|
+
changed = False
|
|
234
|
+
iteration_stats = _single_unknown_vote_stats(
|
|
235
|
+
equations,
|
|
236
|
+
charges,
|
|
237
|
+
min_charge,
|
|
238
|
+
max_charge,
|
|
239
|
+
conflicts,
|
|
240
|
+
)
|
|
241
|
+
vote_stats.update(iteration_stats)
|
|
242
|
+
accepted = []
|
|
243
|
+
for signature_id, stats in sorted(iteration_stats.items()):
|
|
244
|
+
if signature_id in charges:
|
|
245
|
+
continue
|
|
246
|
+
if stats["valid_vote_count"] < min_vote_count:
|
|
247
|
+
continue
|
|
248
|
+
if stats["winning_charge"] is None:
|
|
249
|
+
continue
|
|
250
|
+
if stats["vote_confidence"] + TOLERANCE < min_vote_fraction:
|
|
251
|
+
continue
|
|
252
|
+
accepted.append((signature_id, int(stats["winning_charge"])))
|
|
253
|
+
for signature_id, charge in accepted:
|
|
254
|
+
charges[signature_id] = charge
|
|
255
|
+
sources[signature_id] = "dataset_neutrality:frame_vote"
|
|
256
|
+
changed = True
|
|
257
|
+
|
|
258
|
+
for signature_id, stats in sorted(vote_stats.items()):
|
|
259
|
+
if not stats or len(stats.get("vote_counts", {})) <= 1:
|
|
260
|
+
continue
|
|
261
|
+
winner = int(stats["winning_charge"])
|
|
262
|
+
minority_examples = [
|
|
263
|
+
frame_index
|
|
264
|
+
for charge, frame_indices in stats["example_frame_indices"].items()
|
|
265
|
+
if int(charge) != winner
|
|
266
|
+
for frame_index in frame_indices
|
|
267
|
+
]
|
|
268
|
+
_record_conflict(
|
|
269
|
+
conflicts,
|
|
270
|
+
int(minority_examples[0] if minority_examples else -1),
|
|
271
|
+
"minority_frame_votes",
|
|
272
|
+
{
|
|
273
|
+
"signature_id": signature_id,
|
|
274
|
+
"winning_charge": winner,
|
|
275
|
+
"attempted_vote_count": stats["attempted_vote_count"],
|
|
276
|
+
"valid_vote_count": stats["valid_vote_count"],
|
|
277
|
+
"invalid_vote_count": stats["invalid_vote_count"],
|
|
278
|
+
"winning_vote_count": stats["winning_vote_count"],
|
|
279
|
+
"vote_confidence": stats["vote_confidence"],
|
|
280
|
+
"vote_counts": stats["vote_counts"],
|
|
281
|
+
"minority_frame_examples": minority_examples,
|
|
282
|
+
},
|
|
283
|
+
)
|
|
284
|
+
all_signatures = {signature_id for equation in equations for signature_id in equation.coefficients}
|
|
285
|
+
unresolved = all_signatures - set(charges)
|
|
286
|
+
return InferenceResult(charges, sources, unresolved, list(conflicts.values()), vote_stats)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def resolve_frame_charges(
|
|
290
|
+
fragments: Sequence[FragmentChargeInput],
|
|
291
|
+
registry: Mapping[str, object],
|
|
292
|
+
frame_total_charge: float = 0.0,
|
|
293
|
+
) -> FrameChargeResult:
|
|
294
|
+
"""Resolve one frame, assigning Fe or Zn only after all other charges."""
|
|
295
|
+
charges = [float("nan")] * len(fragments)
|
|
296
|
+
reasons: List[str] = []
|
|
297
|
+
fe_indices = []
|
|
298
|
+
zn_indices = []
|
|
299
|
+
unknown = []
|
|
300
|
+
for position, fragment in enumerate(fragments):
|
|
301
|
+
if fragment.fragment_id != position:
|
|
302
|
+
reasons.append("fragment_ids_must_be_contiguous")
|
|
303
|
+
assignment = registry.get(fragment.signature_id)
|
|
304
|
+
if assignment is not None:
|
|
305
|
+
charges[position] = float(assignment.charge if isinstance(assignment, ChargeAssignment) else assignment)
|
|
306
|
+
continue
|
|
307
|
+
if fragment.metal_element == "Fe":
|
|
308
|
+
fe_indices.append(position)
|
|
309
|
+
continue
|
|
310
|
+
if fragment.metal_element == "Zn":
|
|
311
|
+
zn_indices.append(position)
|
|
312
|
+
continue
|
|
313
|
+
if fragment.metal_element in FIXED_METAL_CHARGES:
|
|
314
|
+
charges[position] = FIXED_METAL_CHARGES[fragment.metal_element]
|
|
315
|
+
continue
|
|
316
|
+
unknown.append(fragment.signature_id)
|
|
317
|
+
|
|
318
|
+
if unknown:
|
|
319
|
+
reasons.append("unknown_fragment_charge:" + ",".join(sorted(set(unknown))))
|
|
320
|
+
if fe_indices and zn_indices:
|
|
321
|
+
reasons.append("frame_contains_both_fe_and_zn")
|
|
322
|
+
if len(fe_indices) > 1:
|
|
323
|
+
reasons.append("multiple_fe_atoms")
|
|
324
|
+
if reasons:
|
|
325
|
+
return FrameChargeResult(charges, "unresolved", reasons)
|
|
326
|
+
|
|
327
|
+
known_total = float(np.nansum(charges))
|
|
328
|
+
residual = float(frame_total_charge - known_total)
|
|
329
|
+
fe_charge = None
|
|
330
|
+
zn_total = None
|
|
331
|
+
zn_average = None
|
|
332
|
+
if fe_indices:
|
|
333
|
+
fe_charge = residual
|
|
334
|
+
charges[fe_indices[0]] = residual
|
|
335
|
+
elif zn_indices:
|
|
336
|
+
zn_total = residual
|
|
337
|
+
zn_average = residual / len(zn_indices)
|
|
338
|
+
for index in zn_indices:
|
|
339
|
+
charges[index] = zn_average
|
|
340
|
+
elif abs(residual) > TOLERANCE:
|
|
341
|
+
reasons.append("frame_charge_not_neutral")
|
|
342
|
+
|
|
343
|
+
if reasons or any(np.isnan(value) for value in charges):
|
|
344
|
+
return FrameChargeResult(charges, "unresolved", reasons, fe_charge, zn_total, zn_average)
|
|
345
|
+
if abs(sum(charges) - frame_total_charge) > TOLERANCE:
|
|
346
|
+
return FrameChargeResult(charges, "unresolved", ["resolved_charge_validation_failed"], fe_charge, zn_total, zn_average)
|
|
347
|
+
return FrameChargeResult(charges, "resolved", [], fe_charge, zn_total, zn_average)
|