def make_compatible_distance_matrices(dm1, dm2, lookup=None):
 dm1_ids = dm1[0]
 dm1_data = dm1[1]
 dm2_ids = dm2[0]
 dm2_data = dm2[1]
 if lookup:
  try:
   dm1_ids = [lookup[e] for e in dm1_ids]
   dm2_ids = [lookup[e] for e in dm2_ids]
  except KeyError as e:
   raise KeyError(('All entries in both DMs must be in lookup if a lookup is provided. Missing: %s' % str(e)))
 order = [e for e in dm1_ids if (e in dm2_ids)]
 if (len(order) == 0):
  return (([], []), ([], []))
 matrices = []
 for (ids, distance_matrix) in [(dm1_ids, dm1_data), (dm2_ids, dm2_data)]:
  indices = [ids.index(element) for element in order]
  out = distance_matrix[indices][:, indices]
  matrices.append(out)
 return ((order, matrices[0]), (order, matrices[1]))

def make_compatible_distance_matrices ( dm1 , dm2 , lookup = None ) : dm1_ids = dm1 [ 0 ] dm1_data = dm1 [ 1 ] dm2_ids = dm2 [ 0 ] dm2_data = dm2 [ 1 ] if lookup : try : dm1_ids = [ lookup [ e ] for e in dm1_ids ] dm2_ids = [ lookup [ e ] for e in dm2_ids ] except KeyError as e : raise KeyError ( ( 'All entries in both DMs must be in lookup if a lookup is provided. Missing: %s' % str ( e ) ) ) order = [ e for e in dm1_ids if ( e in dm2_ids ) ] if ( len ( order ) == 0 ) : return ( ( [ ] , [ ] ) , ( [ ] , [ ] ) ) matrices = [ ] for ( ids , distance_matrix ) in [ ( dm1_ids , dm1_data ) , ( dm2_ids , dm2_data ) ] : indices = [ ids . index ( element ) for element in order ] out = distance_matrix [ indices ] [ : , indices ] matrices . append ( out ) return ( ( order , matrices [ 0 ] ) , ( order , matrices [ 1 ] ) )
intersect distance matrices and sort the values .