2424import math
2525from scitbx import matrix
2626import scitbx .cubicle_neighbors
27+ from cctbx .uctbx .near_minimum import find_near_minimum_settings , cell_distance
28+ import numpy as np
2729cubicles_max_memory_allocation_set (
2830 number_of_bytes = scitbx .cubicle_neighbors .cubicles_max_memory_allocation_get ())
2931
@@ -537,8 +539,6 @@ def nearest_setting(self, other,
537539 A new symmetry object representing 'other' in the setting
538540 that best matches self
539541 """
540- import numpy as np
541- from cctbx .uctbx .near_minimum import find_near_minimum_settings , cell_distance
542542
543543 # Get or compute cached nearly-reduced settings for self
544544 cache_key = ('_near_minimum_cache' , length_tolerance , angle_tolerance )
@@ -561,14 +561,13 @@ def nearest_setting(self, other,
561561 mc_other = other .minimum_cell ()
562562 uc_other = np .array (mc_other .unit_cell ().parameters ())
563563
564- # Find best matching nearly-reduced setting
565- best_idx = 0
566- best_dist = float ('inf' )
567- for i , setting in enumerate (settings ):
568- dist = cell_distance (uc_other , setting ['cell' ])
569- if dist < best_dist :
570- best_dist = dist
571- best_idx = i
564+ # Compute distances for each nearly-reduced setting
565+ distances = [cell_distance (uc_other , setting ['cell' ]) for setting in settings ]
566+ best_dist = min (distances )
567+ tolerance = 1e-6
568+ tied_indices = [i for i , d in enumerate (distances ) if abs (d - best_dist ) <= tolerance ]
569+ import random
570+ best_idx = random .choice (tied_indices )
572571
573572 # Get the transformation matrix and invert it for change_of_basis_op
574573 # Convention: P from find_near_minimum_settings satisfies P = inv(cb.c().r())
0 commit comments