def exercise_fixed_vertices(n_trials=10): cm = cluster_manager(n_vertices=2, fixed_vertex_lists=[[0]]) assert cm.clusters == [[0], [1]] cm = cluster_manager(n_vertices=2, fixed_vertex_lists=[[1]]) assert cm.clusters == [[1], [0]] edge_list = [(0,1),(1,2),(2,3),(1,3)] edge_sets = construct_edge_sets(n_vertices=4, edge_list=edge_list) for fixed_vertex in [0,1]: for optimize in [False, True]: for connects in [[(1,2),(2,3)], [(2,3),(1,2)], [(2,1),(3,2)]]: cm = cluster_manager(n_vertices=4, fixed_vertex_lists=[[fixed_vertex]]) for i,j in connects: cm.connect_vertices(i=i, j=j, optimize=optimize) if (fixed_vertex == 0): if (connects[0] == (2,1)): if (not optimize): assert cm.clusters == [[0], [], [], [3, 2, 1]] else: assert cm.clusters == [[0], [], [2, 1, 3], []] else: if (not optimize or connects[0][0] == 1): assert cm.clusters == [[0], [1,2,3], [], []] else: assert cm.clusters == [[0], [], [2,3,1], []] cm.tidy() assert cm.clusters == [[0], [1,2,3]] else: assert cm.clusters == [[1,2,3], [0], [], []] cm.tidy() assert cm.clusters == [[1,2,3], [0]] cm.construct_spanning_trees(edge_sets=edge_sets) assert cm.clusters == [[0,1,2,3]] assert cm.hinge_edges == [(-1,1)] assert cm.loop_edges == [] # from scitbx.graph import test_cases_tardy_pdb tc = test_cases_tardy_pdb.test_cases[5] assert tc.tag == "tyr_with_h" tt = tc.tardy_tree_construct(fixed_vertex_lists=[[0,16,17]]) assert tt.cluster_manager.clusters == [ [0,1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19], [11], [20]] try: tc.tardy_tree_construct(fixed_vertex_lists=[[0],[1]]) except RuntimeError, e: assert str(e) == \ "connect_clusters(): fixed vertex lists in same connected tree."
def exercise_cluster_manager(): cm = cluster_manager(n_vertices=0) assert cm.cluster_indices == [] assert cm.clusters == [] cm = cluster_manager(n_vertices=5) for p in xrange(2): assert cm.cluster_indices == [0,1,2,3,4] assert cm.clusters == [[0],[1],[2],[3],[4]] cm.connect_vertices(i=0, j=0, optimize=True) for p in xrange(2): cm.connect_vertices(i=1, j=3, optimize=True) assert cm.cluster_indices == [0,1,2,1,4] assert cm.clusters == [[0],[1,3],[2],[],[4]] for p in xrange(2): cm.connect_vertices(i=0, j=3, optimize=True) assert cm.cluster_indices == [1,1,2,1,4] for q in xrange(2): assert cm.clusters == [[],[1,3,0],[2],[],[4]] cm.refresh_indices() cm.connect_vertices(i=2, j=4, optimize=True) assert cm.clusters == [[],[1,3,0],[2,4],[],[]] assert cm.cluster_indices == [1,1,2,1,2] cm.connect_vertices(i=2, j=3, optimize=True) assert cm.clusters == [[],[1,3,0,2,4],[],[],[]] assert cm.cluster_indices == [1,1,1,1,1] cm.tidy() assert cm.clusters == [[0,1,2,3,4]] assert cm.cluster_indices == [0,0,0,0,0] # cm = cluster_manager(n_vertices=6) cm.connect_vertices(i=3, j=0, optimize=True) cm.connect_vertices(i=2, j=4, optimize=True) cm.connect_vertices(i=1, j=2, optimize=True) cm.tidy() assert cm.clusters == [[1,2,4],[0,3],[5]] assert cm.cluster_indices == [1,0,0,1,0,2] edges = [(0,1), (0,2), (3,4), (4,5)] cm.connect_vertices(i=4, j=5, optimize=True) cm.tidy() assert cm.clusters == [[1,2,4,5],[0,3]] assert cm.cluster_indices == [1,0,0,1,0,0] es = construct_edge_sets(n_vertices=6, edge_list=edges) cm.construct_spanning_trees(edge_sets=es) assert cm.clusters == [[0,1,2,4,5],[3]] assert cm.cluster_indices == [0,0,0,1,0,0] assert cm.hinge_edges == [(-1,1), (1,0)] assert cm.loop_edges == [(2,0), (4,3)] assert cm.roots() == [0] assert cm.tree_ids() == [0,0] cm.find_loop_edge_bendings(edge_sets=es) assert cm.loop_edge_bendings == [(1,2), (3,5)] # cm.cluster_indices = [1,0,0,1,0,0] cm.clusters = [[1,2,4,5],[0,3]] cm.hinge_edges = None cm.loop_edges = None cm.loop_edge_bendings = None cm.merge_clusters_with_multiple_connections(edge_sets=es) assert cm.clusters == [[0,1,2,3,4,5]] assert cm.cluster_indices == [0,0,0,0,0,0] # sio = StringIO() assert cm.show_summary(out=sio, prefix=">") is cm assert not show_diff(sio.getvalue(), """\ >number of fixed vertex lists: 0 >number of fixed vertices: 0 >number of clusters: 1 >merge clusters with multiple connections: 2 passes >number of hinge edges: None >number of loop edges: None >number of loop edge bendings: None >number of fixed hinges: None """) # cm = cluster_manager(n_vertices=3, all_in_one_rigid_body=True) assert cm.clusters == [[0,1,2]] assert cm.cluster_indices == [0,0,0] cm.tidy() cm.construct_spanning_trees(edge_sets=None) assert cm.clusters == [[0,1,2]] assert cm.cluster_indices == [0,0,0] assert cm.hinge_edges == [(-1,0)] assert cm.loop_edges == [] assert cm.roots() == [0] assert cm.tree_ids() == [0] cm.find_loop_edge_bendings(edge_sets=None) assert cm.loop_edge_bendings == []
def run(args): assert args in [[], ["python"], ["c++"]] # sgno_list_by_index_list_by_cs = {} from cctbx import sgtbx from cctbx import miller for symbols in sgtbx.space_group_symbol_iterator(): psgi = sgtbx.space_group_info(symbols.universal_hermann_mauguin()) \ .primitive_setting() p_indices = miller.index_generator( space_group_type=psgi.type(), anomalous_flag=False, max_index=[4]*3).to_array() # 4 is the smallest value leading to correct results; any larger # value will work, too, but will make this procedure slower p1_indices = miller.expand_to_p1_iselection( space_group=psgi.group(), anomalous_flag=False, indices=p_indices, build_iselection=False).indices from cctbx.array_family import flex sort_perm = flex.sort_permutation( data=miller.index_span(p1_indices).pack(p1_indices)) p1_indices = p1_indices.select(sort_perm) index_list = tuple(p1_indices) sgno = psgi.type().number() sgno_list_by_index_list = sgno_list_by_index_list_by_cs \ .setdefault(symbols.crystal_system(), {}) sgno_list_by_index_list.setdefault(index_list, []).append(sgno) from scitbx.graph import tardy_tree cluster_manager = tardy_tree.cluster_manager(n_vertices=231) for cs,sgno_list_by_index_list in sgno_list_by_index_list_by_cs.items(): for sgno_list in sgno_list_by_index_list.values(): i = sgno_list[0] for j in sgno_list[1:]: cluster_manager.connect_vertices(i=i, j=j, optimize=True) cluster_manager.tidy() # # everything below is just to format the results # if (args == []): for cluster in cluster_manager.clusters: if (len(cluster) == 1): break print cluster else: note = ("""\ Output of: cctbx/examples/find_sys_abs_equiv_space_groups.py %s If you have to edit this table, please send email to: [email protected] """ % args[0]).splitlines() # if (args == ["python"]): print "space_group_numbers = [" for line in note: print " #", line ci = cluster_manager.cluster_indices cl = cluster_manager.clusters for sgno in xrange(231): cluster = list(cl[ci[sgno]]) cluster.remove(sgno) if (len(cluster) == 0): s = "None" else: s = str(tuple(cluster)) if (sgno == 230): comma = "" else: comma = "," print " %s%s" % (s, comma) print "]" else: print """\ #ifndef CCTBX_SGTBX_SYS_ABS_EQUIV_H #define CCTBX_SGTBX_SYS_ABS_EQUIV_H namespace cctbx { namespace sgtbx { namespace sys_abs_equiv { """ data = [] ci = cluster_manager.cluster_indices cl = cluster_manager.clusters for line in note: print " //", line for sgno in xrange(231): cluster = list(cl[ci[sgno]]) cluster.remove(sgno) if (len(cluster) == 0): data.append("0") else: cid = "data_%03d" % sgno data.append(cid) print " static const unsigned %s[] = {%d, %s};" % ( cid, len(cluster), ", ".join([str(i) for i in cluster])) print "" print " static const unsigned* space_group_numbers[] = {" print " ", ",\n ".join(data) print """\
def run(args): assert args in [[], ["python"], ["c++"]] # sgno_list_by_index_list_by_cs = {} from cctbx import sgtbx from cctbx import miller for symbols in sgtbx.space_group_symbol_iterator(): psgi = sgtbx.space_group_info(symbols.universal_hermann_mauguin()) \ .primitive_setting() p_indices = miller.index_generator(space_group_type=psgi.type(), anomalous_flag=False, max_index=[4] * 3).to_array() # 4 is the smallest value leading to correct results; any larger # value will work, too, but will make this procedure slower p1_indices = miller.expand_to_p1_iselection( space_group=psgi.group(), anomalous_flag=False, indices=p_indices, build_iselection=False).indices from cctbx.array_family import flex sort_perm = flex.sort_permutation( data=miller.index_span(p1_indices).pack(p1_indices)) p1_indices = p1_indices.select(sort_perm) index_list = tuple(p1_indices) sgno = psgi.type().number() sgno_list_by_index_list = sgno_list_by_index_list_by_cs \ .setdefault(symbols.crystal_system(), {}) sgno_list_by_index_list.setdefault(index_list, []).append(sgno) from scitbx.graph import tardy_tree cluster_manager = tardy_tree.cluster_manager(n_vertices=231) for cs, sgno_list_by_index_list in sgno_list_by_index_list_by_cs.items(): for sgno_list in sgno_list_by_index_list.values(): i = sgno_list[0] for j in sgno_list[1:]: cluster_manager.connect_vertices(i=i, j=j, optimize=True) cluster_manager.tidy() # # everything below is just to format the results # if (args == []): for cluster in cluster_manager.clusters: if (len(cluster) == 1): break print(cluster) else: note = ("""\ Output of: cctbx/examples/find_sys_abs_equiv_space_groups.py %s If you have to edit this table, please send email to: [email protected] """ % args[0]).splitlines() # if (args == ["python"]): print("space_group_numbers = [") for line in note: print(" #", line) ci = cluster_manager.cluster_indices cl = cluster_manager.clusters for sgno in range(231): cluster = list(cl[ci[sgno]]) cluster.remove(sgno) if (len(cluster) == 0): s = "None" else: s = str(tuple(cluster)) if (sgno == 230): comma = "" else: comma = "," print(" %s%s" % (s, comma)) print("]") else: print("""\ #ifndef CCTBX_SGTBX_SYS_ABS_EQUIV_H #define CCTBX_SGTBX_SYS_ABS_EQUIV_H namespace cctbx { namespace sgtbx { namespace sys_abs_equiv { """) data = [] ci = cluster_manager.cluster_indices cl = cluster_manager.clusters for line in note: print(" //", line) for sgno in range(231): cluster = list(cl[ci[sgno]]) cluster.remove(sgno) if (len(cluster) == 0): data.append("0") else: cid = "data_%03d" % sgno data.append(cid) print(" static const unsigned %s[] = {%d, %s};" % (cid, len(cluster), ", ".join( [str(i) for i in cluster]))) print("") print(" static const unsigned* space_group_numbers[] = {") print(" ", ",\n ".join(data)) print("""\ }; }}} #endif // GUARD""")
def exercise_fixed_vertices(n_trials=10): cm = cluster_manager(n_vertices=2, fixed_vertex_lists=[[0]]) assert cm.clusters == [[0], [1]] cm = cluster_manager(n_vertices=2, fixed_vertex_lists=[[1]]) assert cm.clusters == [[1], [0]] edge_list = [(0, 1), (1, 2), (2, 3), (1, 3)] edge_sets = construct_edge_sets(n_vertices=4, edge_list=edge_list) for fixed_vertex in [0, 1]: for optimize in [False, True]: for connects in [[(1, 2), (2, 3)], [(2, 3), (1, 2)], [(2, 1), (3, 2)]]: cm = cluster_manager(n_vertices=4, fixed_vertex_lists=[[fixed_vertex]]) for i, j in connects: cm.connect_vertices(i=i, j=j, optimize=optimize) if (fixed_vertex == 0): if (connects[0] == (2, 1)): if (not optimize): assert cm.clusters == [[0], [], [], [3, 2, 1]] else: assert cm.clusters == [[0], [], [2, 1, 3], []] else: if (not optimize or connects[0][0] == 1): assert cm.clusters == [[0], [1, 2, 3], [], []] else: assert cm.clusters == [[0], [], [2, 3, 1], []] cm.tidy() assert cm.clusters == [[0], [1, 2, 3]] else: assert cm.clusters == [[1, 2, 3], [0], [], []] cm.tidy() assert cm.clusters == [[1, 2, 3], [0]] cm.construct_spanning_trees(edge_sets=edge_sets) assert cm.clusters == [[0, 1, 2, 3]] assert cm.hinge_edges == [(-1, 1)] assert cm.loop_edges == [] # from scitbx.graph import test_cases_tardy_pdb tc = test_cases_tardy_pdb.test_cases[5] assert tc.tag == "tyr_with_h" tt = tc.tardy_tree_construct(fixed_vertex_lists=[[0, 16, 17]]) assert tt.cluster_manager.clusters == [[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19 ], [11], [20]] try: tc.tardy_tree_construct(fixed_vertex_lists=[[0], [1]]) except RuntimeError as e: assert str(e) == \ "connect_clusters(): fixed vertex lists in same connected tree." else: raise Exception_expected try: tc.tardy_tree_construct(fixed_vertex_lists=[[0], [10]]) except RuntimeError as e: assert str(e) == \ "determine_weighted_order_for_construct_spanning_tree():" \ " fixed vertex lists in same connected tree." else: raise Exception_expected try: tc.tardy_tree_construct(fixed_vertex_lists=[[0], [11]]) except RuntimeError as e: assert str(e) == \ "construct_spanning_trees():" \ " fixed vertex lists in same connected tree." else: raise Exception_expected # for tc in test_cases: if (max(tc.tree_ids1) == 0): continue tt = construct(n_vertices=tc.n_vertices, edge_list=tc.edge_list) tt.build_tree() cm = tt.cluster_manager cl = cm.clusters ti = cm.tree_ids() assert ti[0] != ti[-1] for lfvl0 in range(1, len(cl[0]) + 1): for lfvl1 in range(1, len(cl[-1]) + 1): for i_trial in range(n_trials): fvl0 = random_permutation(cl[0])[:lfvl0] fvl1 = random_permutation(cl[-1])[:lfvl1] ttf = construct(n_vertices=tc.n_vertices, edge_list=tc.edge_list, fixed_vertex_lists=[fvl0, fvl1]).build_tree() cmf = ttf.cluster_manager cif = cmf.cluster_indices fvgci = cmf.fixed_vertices_given_cluster_index_dict() assert len(fvgci) == len(cmf.fixed_vertex_lists) for fixed_vertices in cmf.fixed_vertex_lists: assert len(set([cif[i] for i in fixed_vertices])) == 1 assert fvgci[cif[fixed_vertices[0]]] is fixed_vertices # for fixed_vertex in [0, 1]: tt = construct(n_vertices=2, edge_list=[(0, 1)], fixed_vertex_lists=[[fixed_vertex]]).build_tree() assert tt.cluster_manager.clusters == [[0, 1]] # for fixed_vertex in [0, 1, 2]: tt = construct(n_vertices=3, edge_list=[(0, 1), (1, 2)], fixed_vertex_lists=[[fixed_vertex]]).build_tree() assert tt.cluster_manager.clusters == [[0, 1, 2]] # el = [(8, 9), (7, 9), (3, 7), (8, 11), (8, 12), (12, 14), (13, 15), (7, 13), (1, 6), (4, 6), (0, 5)] tt = construct(n_vertices=16, edge_list=el, fixed_vertices=()) assert tt.cluster_manager.fixed_vertex_lists == () tt = construct(n_vertices=16, edge_list=el, fixed_vertices=(12, 6, 4, 7, 9, 5)) assert tt.cluster_manager.fixed_vertex_lists == [[12, 7, 9], [6, 4], [5]]