<jupyter_start><jupyter_text>Importing some utilities<jupyter_code>import sys
sys.path.append('../python/')
from json2graph import jsonFile2graph
import networkx as nx
from vocabulary import Vocabulary
from graphUtils import plot_graph, graph2data, data2graph
from neuralmodel import GNN_MoRec

# generator must belong to {VIATRA,RANDOMEMF,ALLOY,RAND}
generator = 'RAND'
# modelType must belong to {Ecore,RDS,Yakindu}
modelType = 'Ecore'<jupyter_output><empty_output><jupyter_text>Loading vocabularies and train/test/val used in the paper Loading the vocabularies. These objects contains a dictionary that associates each node type or edge type to an integer between $[0\dots n-1]$ where $n$ is the length of the vocabulary.<jupyter_code>import json

vocab_edges = Vocabulary()
vocab_nodes = Vocabulary()

with open('../Vocabularies/vocab_edges-'+modelType+'-'+generator+'.json') as json_file:
    data = json.load(json_file)
    vocab_edges.word2id_names = data
    vocab_edges.id2word_names = {y:x for x,y in data.items()}
    
with open('../Vocabularies/vocab_nodes-'+modelType+'-'+generator+'.json') as json_file:
    data = json.load(json_file)
    vocab_nodes.word2id_names = data
    vocab_nodes.id2word_names = {y:x for x,y in data.items()}<jupyter_output><empty_output><jupyter_text>Loading train/test and val splits used in the paper.<jupyter_code>import torch

train = torch.load('../TrainTestValSplits/Train-'+modelType+'-'+generator)
val = torch.load('../TrainTestValSplits/Val-'+modelType+'-'+generator)
test = torch.load('../TrainTestValSplits/Test-'+modelType+'-'+generator)<jupyter_output><empty_output><jupyter_text>Loading the trained model Loading that model was used to report the results in the paper:<jupyter_code>model2 = GNN_MoRec(64,64,0.0,vocab_nodes,vocab_edges).cpu()
path_to_model = '../models/'+modelType+'/'+modelType+'-'+generator+'-GNN'
checkpoint = torch.load(path_to_model,map_location=torch.device('cpu'))
model2.load_state_dict(checkpoint['model_state_dict'])

epoch = checkpoint['epoch']
loss = checkpoint['loss']

model2.eval()<jupyter_output><empty_output><jupyter_text>Performing C2ST Evaluating the model over the test set and reporting the accuracy.<jupyter_code>from torch_geometric.data import DataLoader
test_loader = DataLoader(test, batch_size=1, num_workers = 5, shuffle=True)

model2.eval()
count = 0
i0 = 0
i1 = 0
for data in test_loader:
    
    pred = model2(data.x.cpu(), data.edge_index.cpu(),
          torch.squeeze(data.edge_attr,dim=1).cpu(),data.batch.cpu())
    if pred[0].item() > 0.5:
        pred = 1
    else:
        pred = 0
    if pred == data.y.long().item():
        count = count + 1
    
print('Acc', count/len(test_loader))<jupyter_output><empty_output><jupyter_text>Performing C2ST using the accuracy and the length of the test set.<jupyter_code>from C2ST import C2ST_pvalue

acc =  count/len(test_loader)
n_test = len(test_loader)
print('p-value:', C2ST_pvalue(acc,n_test))
print('samples', n_test)<jupyter_output><empty_output><jupyter_text>Interpreting the test For all graph in the test set that is synthetic and the model is sure that it is synthetic, the attention map is printed over it.<jupyter_code>from interpretation import heatMap, plot_graph_attention, importantSubgraph, getMapAttention
i = 0
for data in test:
    G = data2graph(data,vocab_nodes,vocab_edges)
    batch = torch.zeros(len(G)).long()
    atts = model2.getAttentions(data.x.cpu(), data.edge_index.cpu(),
          torch.squeeze(data.edge_attr.cpu(),dim=1),batch.cpu())
    map_colors = getMapAttention(G,atts)
    
    pred = model2(data.x.cpu(), data.edge_index.cpu(),
          torch.squeeze(data.edge_attr.cpu(),dim=1),batch.cpu())
    if pred[0].item() < 0.1 and data.y.item() == 0:
        plot_graph_attention(G,map_colors)
        #plot_graph_attention(importantSubgraph(G, atts.detach().cpu().numpy(), 0.2, 2),map_colors)
        #heatMap(G,atts,str(i),'./interpretation/'+modelType+'/'+generator+'/')
        #heatMap(importantSubgraph(G, atts.detach().cpu().numpy(), 0.2, 2),atts,str(i),
        #        './interpretation/'+modelType+'/'+generator+'/subgraph/')
        i = i + 1
        print('--'*50)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13771741720276387, 'fraction_numerical': 0.01405766023350012, 'mean_word_length': 5.066473988439307, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '554562', 'license': 'MIT', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1469, 'n_words': 301, 'path': 'notebooks/Paper-GNN.ipynb', 'repo_name': 'Antolin1/TCRMG-GNN', 'size': '4197'}