|
| 1 | +import io |
| 2 | +import sys |
| 3 | +from pathlib import Path |
| 4 | +from time import time |
| 5 | + |
| 6 | +from SubgraphDetection import detect_subgraph |
| 7 | + |
| 8 | + |
| 9 | +def _build_config_grid(): |
| 10 | + return [ |
| 11 | + { |
| 12 | + "DETAILED_ISOMORPHIC_CHECK": DETAILED_ISOMORPHIC_CHECK, |
| 13 | + "SCOPE_BOUNDARY": SCOPE_BOUNDARY, |
| 14 | + "MAX_SUBGRAPH_NODE_NUMBER": MAX_SUBGRAPH_NODE_NUMBER, |
| 15 | + "MIN_SUBGRAPH_NODE_NUMBER": MIN_SUBGRAPH_NODE_NUMBER, |
| 16 | + "MIN_SUBGRAPH_INSTANCE_NUMBER": MIN_SUBGRAPH_INSTANCE_NUMBER, |
| 17 | + } |
| 18 | + for DETAILED_ISOMORPHIC_CHECK in (True, False) |
| 19 | + for SCOPE_BOUNDARY in (True, False) |
| 20 | + for MAX_SUBGRAPH_NODE_NUMBER in range(24, 49, 12) |
| 21 | + for MIN_SUBGRAPH_NODE_NUMBER in range(3, 6) |
| 22 | + for MIN_SUBGRAPH_INSTANCE_NUMBER in range(2, 5) |
| 23 | + ] |
| 24 | + |
| 25 | + |
| 26 | +def mean(array): |
| 27 | + return sum(array) / len(array) if array else 0 |
| 28 | + |
| 29 | + |
| 30 | +def write_result_header(): |
| 31 | + with open("./test_result.csv", "w") as f: |
| 32 | + f.write( |
| 33 | + ",".join( |
| 34 | + [ |
| 35 | + "DETAILED_ISOMORPHIC_CHECK", |
| 36 | + "SCOPE_BOUNDARY", |
| 37 | + "MAX_SUBGRAPH_NODE_NUMBER", |
| 38 | + "MIN_SUBGRAPH_NODE_NUMBER", |
| 39 | + "MIN_SUBGRAPH_INSTANCE_NUMBER", |
| 40 | + "pb_name", |
| 41 | + "time", |
| 42 | + "graph_size", |
| 43 | + "num_subgraph", |
| 44 | + "ave_num_subgraph_instance", |
| 45 | + "ave_subgraph_size", |
| 46 | + "MDL", |
| 47 | + "reduce_ratio", |
| 48 | + ] |
| 49 | + ) + "\n" |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +def write_result(test_result): |
| 54 | + with open("./test_result.csv", "a") as f: |
| 55 | + f.write(",".join(map(str, test_result[-1].values())) + "\n") |
| 56 | + |
| 57 | + |
| 58 | +def test_detect(): |
| 59 | + # Do not print! |
| 60 | + stdout, sys.stdout = sys.stdout, io.StringIO() |
| 61 | + |
| 62 | + config = _build_config_grid() |
| 63 | + result = [] |
| 64 | + write_result_header() |
| 65 | + |
| 66 | + for c in config: |
| 67 | + # test the config item on every pb fies we got |
| 68 | + for pb_file in Path("./pb").iterdir(): |
| 69 | + if pb_file.suffix == ".pb": |
| 70 | + time_st = time() |
| 71 | + res_check = detect_subgraph( |
| 72 | + graph_path=str(pb_file.absolute()), |
| 73 | + result_path=f"./result/{pb_file.name}.json", |
| 74 | + check_result=True, |
| 75 | + **c, |
| 76 | + ) |
| 77 | + time_use = time() - time_st |
| 78 | + result.append( |
| 79 | + { |
| 80 | + **c, |
| 81 | + "pb_name": pb_file.name, |
| 82 | + "time": time_use, |
| 83 | + "graph_size": res_check.graph_size, |
| 84 | + "num_subgraph": res_check.num_subgraph, |
| 85 | + "ave_num_subgraph_instance": mean(res_check.subgraph_count), |
| 86 | + "ave_subgraph_size": mean(res_check.subgraph_size), |
| 87 | + "MDL": res_check.MDL, |
| 88 | + "reduce_ratio": res_check.reduce_ratio |
| 89 | + } |
| 90 | + ) |
| 91 | + write_result(result) |
| 92 | + print(pb_file, round(time_use, 2), file=stdout) |
| 93 | + |
| 94 | + sys.stdout = stdout |
| 95 | + |
| 96 | + |
| 97 | +test_detect() |
0 commit comments