2
2
import typing as t
3
3
from copy import deepcopy
4
4
5
- def multi_to_pair (multi : int , max_multi : int ) -> t .Tuple [int , int ]:
6
- """
7
- Convert a linear index `multi` to 2D coordinates (x, y) within a square grid.
8
-
9
- Parameters:
10
- - multi (int): Linear index to be converted.
11
- - max_multi (int): Maximum allowed value for the linear index.
12
-
13
- Returns:
14
- - Tuple[int, int]: Two-dimensional coordinates (x, y) within the grid.
15
- """
16
- if 1 <= multi <= max_multi :
17
- # Calculate x and y values based on the input multi
18
- x = (multi - 1 ) % int (max_multi ** .5 ) + 1
19
- y = (multi - 1 ) // int (max_multi ** .5 ) + 1
20
-
21
- x , y = x - 1 , y - 1 # convert to 0-indexed
22
- return x , y
23
- else :
24
- raise ValueError ("Input multi must be in the range 1 to max_multi." )
25
-
26
-
27
5
def run_performance_eval (
28
- nauty_path : str = "/Users/kieran/projects/genGrouper/packages/nauty2_8_8" ,
6
+ nauty_path : str = "/Users/kieran/projects/molGrouper/packages/nauty2_8_8" ,
7
+ node_defs = None ,
29
8
n_runs : int = 3 ,
30
9
max_nodes : int = 6 ,
31
- max_ports : int = 4 ,
32
- n_colors : int = 3 ,
33
10
verbose : bool = False
34
11
):
35
12
"""
36
13
Run performance evaluation of the genGrouper generation featrure with growing sized graphs.
37
14
"""
38
15
import os
39
16
import time
40
- from genGrouper . edge_coloring import process_nauty_vcolg_output
17
+ from genGrouper import exhaustive_generate
41
18
import random
19
+ import itertools
20
+
21
+ if node_defs is None :
22
+ raise ValueError ("node_defs must be provided." )
23
+
24
+ # Time the performance of the generation algorithm
25
+ performance = {combo : { n :[] for n in range (1 , max_nodes + 1 )} for combo in itertools .combinations (node_defs , len (node_defs ) - 1 )}
26
+ for def_combo in performance :
27
+ for n_nodes in range (1 , max_nodes + 1 ):
28
+ total_time = 0
29
+ for i in range (n_runs ):
30
+ random .seed (0 )
31
+ start_time = time .time ()
32
+ space = exhaustive_generate (
33
+ n_nodes ,
34
+ set (def_combo ),
35
+ nauty_path = nauty_path ,
36
+ input_file_path = "" ,
37
+ num_procs = 16 ,
38
+ verbose = verbose )
39
+ end_time = time .time ()
40
+ total_time += end_time - start_time
41
+ performance [def_combo ][n_nodes ].append (total_time )
42
+ print (f"Number of generated graphs: { len (space )} " )
43
+ print ("" )
44
+ # plot_performance(performance, max_nodes)
45
+ return performance
46
+
47
+ def plot_performance (performance , max_nodes ):
48
+ import matplotlib .pyplot as plt
49
+ import numpy as np
50
+ import itertools
51
+
52
+ # Prepare data for the plot
53
+ fig , ax = plt .subplots ()
54
+
55
+ # Use a single colormap
56
+ cmap = plt .get_cmap ('inferno' )
57
+ norm = plt .Normalize ()
58
+
59
+ # Iterate through the performance data to plot
60
+ for i , combo in enumerate (performance ):
61
+ for n_nodes in performance [combo ]:
62
+ times = performance [combo ][n_nodes ]
63
+ mean_time = np .mean (times )
64
+ color = cmap (norm (mean_time ))
65
+
66
+ ax .scatter (
67
+ n_nodes ,
68
+ i ,
69
+ c = color
70
+ )
71
+
72
+ ax .set_xlabel ("Number of nodes" )
73
+ ax .set_ylabel ("Node type combination" )
74
+
75
+ # Adjust y-axis labels to display the node combinations
76
+ ax .set_yticks (range (len (performance )))
77
+ ax .set_yticklabels ([str (combo ) for combo in performance ])
78
+
79
+ # Add the color bar
80
+ sm = plt .cm .ScalarMappable (cmap = cmap , norm = norm )
81
+ sm .set_array ([]) # Only needed for the color bar
82
+ plt .colorbar (sm , ax = ax , label = 'Mean Time (s)' )
83
+
84
+ plt .savefig ("performance.png" )
85
+
42
86
43
- max_nodes = list (range (2 , max_nodes ))
44
- generate_random_ports = lambda max_ports : [str (i ) for i in range (random .randint (1 , max_ports ))]
45
- node_types = {chr (i + 65 ): generate_random_ports (max_ports ) for i in range (n_colors )}
46
-
47
- for n in max_nodes :
48
- total_time = 0
49
- for i in range (n_runs ):
50
- os .system (f"{ nauty_path } /geng { n } -ctf > /Users/kieran/projects/genGrouper/geng_out.txt" )
51
- os .system (f"{ nauty_path } /vcolg /Users/kieran/projects/genGrouper/geng_out.txt -T -m{ n_colors } > vcolg_out.txt" )
52
- # time the edge_coloring.py
53
- start = time .time ()
54
- out = process_nauty_vcolg_output ('/Users/kieran/projects/genGrouper/vcolg_out.txt' , node_types , verbose = False )
55
- end = time .time ()
56
- total_time += end - start
57
- print (f"Average time for { n } nodes: { total_time / n_runs } " )
58
87
59
88
def convert_edges_to_nodetype (G ):
60
89
new_G = deepcopy (G )
@@ -71,3 +100,26 @@ def convert_edges_to_nodetype(G):
71
100
new_G .edges [edge ]['ports' ][i ] = (f'{ srcnode_type } .{ srcport } ' , f'{ dstnode_type } .{ dstport } ' )
72
101
73
102
return new_G
103
+
104
+
105
+ def multi_to_pair (multi : int , max_multi : int ) -> t .Tuple [int , int ]:
106
+ """
107
+ Convert a linear index `multi` to 2D coordinates (x, y) within a square grid.
108
+
109
+ Parameters:
110
+ - multi (int): Linear index to be converted.
111
+ - max_multi (int): Maximum allowed value for the linear index.
112
+
113
+ Returns:
114
+ - Tuple[int, int]: Two-dimensional coordinates (x, y) within the grid.
115
+ """
116
+ if 1 <= multi <= max_multi :
117
+ # Calculate x and y values based on the input multi
118
+ x = (multi - 1 ) % int (max_multi ** .5 ) + 1
119
+ y = (multi - 1 ) // int (max_multi ** .5 ) + 1
120
+
121
+ x , y = x - 1 , y - 1 # convert to 0-indexed
122
+ return x , y
123
+ else :
124
+ raise ValueError ("Input multi must be in the range 1 to max_multi." )
125
+
0 commit comments