Skip to content

Commit

Permalink
fix for inhomogeneous labels
Browse files Browse the repository at this point in the history
  • Loading branch information
jrudz committed May 16, 2024
1 parent 9d2596c commit adda806
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 72 deletions.
18 changes: 13 additions & 5 deletions atomisticparsers/utils/mdanalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,13 +462,21 @@ def get_interactions(self):
except Exception:
continue

for i in range(len(interaction)):
for inter in interaction:
atom_labels = None
try:
atom_labels = [
self.universe.atoms[ind].type for ind in inter.indices
]
except Exception:
self.logger.warning("Could not assign atom labels to interactions.")
interactions.append(
dict(
atom_labels=list(interaction[i].type),
parameters=float(interaction[i].value()),
atom_indices=interaction[i].indices,
type=interaction[i].btype,
atom_labels=atom_labels,
# parameters=float(inter.value()), ## This is not the parameter but rather the value of the interaction order parameter for a single frame
# TODO implement functions to get parameters for individual parsers
atom_indices=inter.indices,
type=inter.btype,
)
)

Expand Down
60 changes: 0 additions & 60 deletions atomisticparsers/utils/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,54 +182,6 @@ def parse_interactions(self, interactions: List[Dict], sec_model: MSection) -> N
if not interactions:
return

## OLD ##
# interaction_dict = {}
# for interaction_key in Interaction.m_def.all_quantities.keys():
# interaction_dict[interaction_key] = np.array(
# [interaction.get(interaction_key) for interaction in interactions],
# dtype=object,
# )
# interaction_dict = {key: val for key, val in interaction_dict.items()}
# interaction_types = (
# np.unique(interaction_dict["type"])
# if interaction_dict.get("type") is not None
# else []
# )
# for interaction_type in interaction_types:
# sec_interaction = Interaction()
# sec_model.contributions.append(sec_interaction)
# interaction_indices = np.where(
# interaction_dict["type"] == interaction_type
# )[0]
# sec_interaction.type = interaction_type
# sec_interaction.n_interactions = len(interaction_indices)
# sec_interaction.n_atoms
# for key, val in interaction_dict.items():
# if key == "type":
# continue
# interaction_vals = val[interaction_indices]
# if type(interaction_vals[0]).__name__ == "ndarray":
# interaction_vals = np.array(
# [vals.tolist() for vals in interaction_vals], dtype=object
# )
# if interaction_vals.all() is None:
# continue
# if key == "parameters":
# interaction_vals = interaction_vals.tolist()
# elif key == "n_atoms":
# interaction_vals = interaction_vals[0]
# if hasattr(sec_interaction, key):
# sec_interaction.m_set(
# sec_interaction.m_get_quantity_definition(key), interaction_vals
# )

# if not sec_interaction.n_atoms:
# sec_interaction.n_atoms = (
# len(sec_interaction.get("atom_indices")[0])
# if sec_interaction.get("atom_indices") is not None
# else None
# )
## NEW ##
def write_interaction_values(values):
sec_interaction = Interaction()
sec_model.contributions.append(sec_interaction)
Expand All @@ -238,17 +190,6 @@ def write_interaction_values(values):
[len(v) for v in values.get("atom_indices", [[0]])]
)
for key, val in values.items():
# TODO tempory fix: atom_labels, atom_indices not homogeneous
# fill in missing atom label with 'X', atom index with -1
# if key in ["atom_indices", "atom_labels"]:
# val = [
# (
# v
# + [-1 if key == "atom_indices" else "X"]
# * sec_interaction.n_atoms
# )[: sec_interaction.n_atoms]
# for v in val
# ]
quantity_def = sec_interaction.m_def.all_quantities.get(key)
if quantity_def:
try:
Expand All @@ -257,7 +198,6 @@ def write_interaction_values(values):
self.logger.error("Error setting metadata.", data={"key": key})

interactions.sort(key=lambda x: x.get("type"))
print(interactions)
current_type = interactions[0].get("type")
interaction_values: Dict[str, Any] = {}
for interaction in interactions:
Expand Down
3 changes: 0 additions & 3 deletions tests/test_gromacsparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,6 @@ def test_md_verbose(parser):
assert (
sec_method[0].force_field.model[0].contributions[6].atom_indices[100, 1] == 141
)
assert sec_method[0].force_field.model[0].contributions[6].parameters[
858
] == approx(0.9999996193044006)
assert sec_method[0].force_field.force_calculations.vdw_cutoff.magnitude == 1.2e-09
assert sec_method[0].force_field.force_calculations.vdw_cutoff.units == "meter"
assert (
Expand Down
5 changes: 1 addition & 4 deletions tests/test_lammpsparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ def test_nvt(parser):
assert sec_method.force_field.model[0].contributions[1].n_interactions == 666
assert sec_method.force_field.model[0].contributions[1].n_atoms == 2
assert sec_method.force_field.model[0].contributions[1].atom_indices[100, 1] == 103
assert sec_method.force_field.model[0].contributions[1].parameters[200] == approx(
1.1147454117684314
)
assert sec_method.force_field.model[0].contributions[1].atom_labels[350][0] == "2"
assert sec_method.force_field.model[0].contributions[1].atom_labels[350, 0] == "1"
assert (
sec_method.force_field.force_calculations.coulomb_cutoff.magnitude
== 1.2000000000000002e-08
Expand Down

0 comments on commit adda806

Please sign in to comment.