Skip to content

Commit

Permalink
update: fix missing bonds eqaulity check
Browse files Browse the repository at this point in the history
  • Loading branch information
VsevolodX committed Dec 11, 2024
1 parent 9e9f6d7 commit eec58e5
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/py/mat3ra/made/tools/bonds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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([])

Expand Down

0 comments on commit eec58e5

Please sign in to comment.