From adda8064164c0ce8bf09c91a9362641590cd9bfd Mon Sep 17 00:00:00 2001 From: jrudz Date: Thu, 16 May 2024 11:28:22 +0200 Subject: [PATCH] fix for inhomogeneous labels --- atomisticparsers/utils/mdanalysis.py | 18 ++++++--- atomisticparsers/utils/parsers.py | 60 ---------------------------- tests/test_gromacsparser.py | 3 -- tests/test_lammpsparser.py | 5 +-- 4 files changed, 14 insertions(+), 72 deletions(-) diff --git a/atomisticparsers/utils/mdanalysis.py b/atomisticparsers/utils/mdanalysis.py index 13deb9e6..f9057caf 100644 --- a/atomisticparsers/utils/mdanalysis.py +++ b/atomisticparsers/utils/mdanalysis.py @@ -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, ) ) diff --git a/atomisticparsers/utils/parsers.py b/atomisticparsers/utils/parsers.py index e6a44f27..723f0d3a 100644 --- a/atomisticparsers/utils/parsers.py +++ b/atomisticparsers/utils/parsers.py @@ -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) @@ -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: @@ -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: diff --git a/tests/test_gromacsparser.py b/tests/test_gromacsparser.py index 440e5071..f8dc1e83 100644 --- a/tests/test_gromacsparser.py +++ b/tests/test_gromacsparser.py @@ -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 ( diff --git a/tests/test_lammpsparser.py b/tests/test_lammpsparser.py index 5d83b2b4..dc718225 100644 --- a/tests/test_lammpsparser.py +++ b/tests/test_lammpsparser.py @@ -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