From eec58e53cb70a57dda7947970aa86a3c5d2fb36e Mon Sep 17 00:00:00 2001 From: VsevolodX <79542055+VsevolodX@users.noreply.github.com> Date: Tue, 10 Dec 2024 18:39:46 -0800 Subject: [PATCH] update: fix missing bonds eqaulity check --- src/py/mat3ra/made/tools/bonds.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/py/mat3ra/made/tools/bonds.py b/src/py/mat3ra/made/tools/bonds.py index eac60267..036d3bb4 100644 --- a/src/py/mat3ra/made/tools/bonds.py +++ b/src/py/mat3ra/made/tools/bonds.py @@ -80,7 +80,7 @@ def find_missing_directions( max_bonds_to_add (int): Maximum number of bonds to add. Returns: - List[List[float]]: List of reconstructed bond vectors. + BondDirections: List of reconstructed bond vectors. """ max_coordination_number = max(len(bond_direction) for template in templates for bond_direction in template) @@ -94,15 +94,13 @@ def find_missing_directions( if self.size == 0: match_count = 0 else: - # TODO: optimize - dot_matrix = np.dot(template, self.T) - cosine_matrix = dot_matrix / (np.linalg.norm(template, axis=1)[:, None] * np.linalg.norm(self, axis=1)) - angles_matrix = np.arccos(np.clip(cosine_matrix, -1.0, 1.0)) + matches = [ + any(BondDirections(existing) == BondDirections(candidate) for existing in self) + for candidate in template + ] + match_count = sum(matches) - matches = np.any(angles_matrix < angle_tolerance, axis=1) - match_count = np.sum(matches) - - missing = template[~matches] if self.size != 0 else template + missing = [candidate for candidate, match in zip(template, matches) if not match] if match_count > best_match_count: best_match_count = match_count @@ -114,7 +112,7 @@ def find_missing_directions( max_bonds_to_add, max_coordination_number - len(self), ) - return BondDirections(best_missing[:num_bonds_to_add].tolist()) + return BondDirections(best_missing[:num_bonds_to_add]) return BondDirections([])