<jupyter_start><jupyter_text>Grid search for q and p<jupyter_code>import subprocess

def run_node2vec(input_file, output_file, p=1, q=1, is_weighted=False, delimiter='" "'):
    cmd = f"python main.py --p {p} --q {q} --input {input_file} --output {output_file} --delimiter {delimiter}"
    if is_weighted: cmd += " --weighted"
    subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)
# This can be true only if the embeddings are available (generated after running a grid search once)
RUN_NODE2VEC_FOR_GRID_SEARCH = True

grid = {
    'q': [0.25, 0.50, 1, 2, 4],
    'p': [0.25, 0.50, 1, 2, 4]
}

evaluations = []

for p in grid['p']:
    for q in grid['q']:
        print(f"p: {p} q: {q}")
            
        emb_file =  f"../emb/{FILE_NAME}_q_{q}_p_{p}.emb"

        if RUN_NODE2VEC_FOR_GRID_SEARCH:
            if DATASET_USED == Datasets.PPI:
                run_node2vec(INPUT_FILE, emb_file, p, q, True)
            elif DATASET_USED == Datasets.CORA:
                run_node2vec(INPUT_FILE, emb_file, p, q, True, ",")
            elif DATASET_USED == Datasets.TERR:
                run_node2vec(INPUT_FILE, emb_file, p, q, False, ",")

        if DATASET_USED == Datasets.PPI:
            df = get_ppi_dataset(emb_file)
        if DATASET_USED == Datasets.TERR:
            df = get_cora_dataset(emb_file)
        elif DATASET_USED == Datasets.CORA:
            df = get_cora_dataset(emb_file)
            
        micro, macro = evaluate(df, [0.5])
        
        evaluations.append((q, p, micro, macro))
# Max micro and macro scores
micro = [x[2][0] for x in evaluations]
macro = [x[3][0] for x in evaluations]

max_micro_index = micro.index(max(micro))
max_macro_index = macro.index(max(macro))

print(evaluations[max_micro_index])
print(evaluations[max_macro_index])
class Scores(Enum):
    MACRO = 1
    MICRO = 2
    
q = [np.log2(x[0]) for x in evaluations]
p = [np.log2(x[1]) for x in evaluations]
micro = [x[2][0] for x in evaluations]
macro = [x[3][0] for x in evaluations]

def plot_grid(p, q, scores, score_enum):
    q = np.array(q)
    p = np.array(p)
    scores = np.array(scores)
    
    if score_enum == Scores.MICRO:
        exp_name =  "micro_score"
        label = "Micro F$_1$"
    elif score_enum == Scores.MACRO:
        exp_name =  "macro_score"
        label = "Macro F$_1$"

    fig = plt.figure(figsize=(8, 16))
    ax = fig.add_subplot(111, projection='3d')

    
    min_lim = min(scores)-2
    max_lim = max(scores)+2
    ax.bar3d(q, p, min_lim*np.ones(len(scores)),
              1, 1, scores-min_lim)
    
    ax.set(zlim=(min_lim, max_lim))

    ax.set_xlabel('$\log_2(q)$')
    ax.set_ylabel('$\log_2(p)$')
    ax.set_zlabel(label)
    plt.savefig(f"../images/classification/grid_{exp_name}_{FILE_NAME}.png", dpi=500)

plot_grid(p, q, micro, Scores.MICRO)
plot_grid(p, q, macro, Scores.MACRO)<jupyter_output><empty_output><jupyter_text>Scalability (Not used in presentation)<jupyter_code>import time
import networkx as nx


NODES_NUM_LIST = [10**x for x in range(2, 7)]
DEGREE = 10
wall_time_experiments = []

def export_graph(G, relative_path):
    G_int = nx.convert_node_labels_to_integers(G, first_label = 0)
    nx.readwrite.edgelist.write_edgelist(G = G_int, path = relative_path, data = False)

for nodes_num in NODES_NUM_LIST:
    G = nx.generators.random_graphs.erdos_renyi_graph(nodes_num, DEGREE/nodes_num)

    input_relative_path = f"../graph/scalability/nodesNum_{nodes_num}_scalability.edgelist"
    export_graph(G, input_relative_path)
    output_relative_path = f"../graph/scalability/nodesNum_{nodes_num}_scalability.emb"
    
    start_time = time.time()
    run_node2vec(input_relative_path, output_relative_path)
    total_time = time.time() - start_time
    
    wall_time_experiments.append(total_time)
    print(f"Wall time for {nodes_num} nodes: {total_time}")
def plot_scalability_exp(nodes_num_list, wall_times):
    fig, ax = plt.subplots(figsize=(12, 6))
    
    plt.plot(nodes_num_list, wall_times, marker='o')
        
    plt.xlabel("$\log_10$ nodes")
    plt.ylabel("time (seconds)")
    plt.savefig(f"../images/classification/scalability.png", dpi=300)
    
plot_scalability_exp(np.log(NODES_NUM_LIST), np.log(wall_time_experiments))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13559322033898305, 'fraction_numerical': 0.018126177024482108, 'mean_word_length': 3.366906474820144, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '569247', 'license': 'MIT', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1606, 'n_words': 318, 'path': 'src/classification.ipynb', 'repo_name': 'dmitrijsk/node2vec', 'size': '4248'}