コード例 #1
0
def plot_2d_cmd(metric: str, id_vent: int, size: int, step: int):
    # Load dei grafi
    original_graph = utility.load_graph()
    immunological_graph = utility.load_graph(
        gexf_filename="immunological_graph.gexf")

    if metric == "precision":
        filename = "plot/" + str(id_vent) + "_" + str(size) + "_" + str(
            step) + "_precision2d.plt"
        if os.path.isfile(filename):
            id_vents, original_list, trained_list = visualize.load_plot2D_from_file(
                filename)
        else:
            # Lista vents/nodes del sottografo
            _, id_vents = utility.get_node_vent_chessboard(id_vent, size, step)
            for i in range(len(id_vents)):
                id_vents[i] = str(int(id_vents[i]) + 1)
            # Lista precision su grafo originale
            original_list = metrics.get_ppv_list(original_graph, id_vents)
            # Lista precision su grafo modificato
            trained_list = metrics.get_ppv_list(immunological_graph, id_vents)
            # Salvataggio dei risultati del trivector PRIMA l'addestramento
            visualize.save_plot2D_on_file(
                id_vents, original_list, trained_list, "plot/" + str(id_vent) +
                "_" + str(size) + "_" + str(step) + "_precision2d.plt")

    elif metric == "recall":
        filename = "plot/" + str(id_vent) + "_" + str(size) + "_" + str(
            step) + "_recall2d.plt"
        if os.path.isfile(filename):
            id_vents, original_list, trained_list = visualize.load_plot2D_from_file(
                filename)
        else:
            # Lista vents/nodes del sottografo
            _, id_vents = utility.get_node_vent_chessboard(id_vent, size, step)
            for i in range(len(id_vents)):
                id_vents[i] = str(int(id_vents[i]) + 1)
            # Lista recall su grafo originale
            original_list = metrics.get_tpr_list(original_graph, id_vents)
            # Lista recall su grafo trained
            trained_list = metrics.get_tpr_list(immunological_graph, id_vents)
            # Salvataggio dei risultati su file
            visualize.save_plot2D_on_file(
                id_vents, original_list, trained_list, "plot/" + str(id_vent) +
                "_" + str(size) + "_" + str(step) + "_recall2d.plt")

    sns.lineplot(id_vents, original_list, label="original")
    sns.lineplot(id_vents, trained_list, label="trained")
    plt.show()
コード例 #2
0
def get_node_from_idvent_in_graph(id_vent):
    # Ritorna le coordinate del vent nel DEM
    coord_vent = vent_in_dem(id_vent)
    G = utility.load_graph()
    # Date le coordinate del DEM, restituisce il nodo corrispondente
    node = get_id_from_coord(G, coord_vent)
    return node
コード例 #3
0
ファイル: map_creator.py プロジェクト: qcqnotfound/Graphlow
def ascii_barrier(id_vent, propagation_method, edges_list):
    utm_filename = "ASCII_grids/" + propagation_method + "_" + str(
        id_vent) + "_barrier.txt"
    # Creazione Matrice da stampare
    M = np.zeros((ROWS, COLS), dtype=int)
    G = utility.load_graph()
    for u, v in edges_list:
        coords = G.nodes[u]["coord_regions"].split("|")[0]
        coord_u_x, coord_u_y = conversion.cast_coord_attr(coords)
        M[coord_u_x][coord_u_y] = 1
        coords = G.nodes[v]["coord_regions"].split("|")[0]
        coord_v_x, coord_v_y = conversion.cast_coord_attr(coords)
        M[coord_v_x][coord_v_y] = 1
    # Scrittura del file
    with open(utm_filename, 'w') as utmfile:
        # Scrittura dell'header del file (northing...)
        for i in range(0, len(header)):
            utmfile.write(header[i] + "\n")
        # Scrittura valori nel DEM
        for x in range(0, ROWS):
            for y in range(0, COLS):
                if M[x][y] == 0:
                    utmfile.write("0" + " ")
                else:
                    utmfile.write(str(M[x][y]) + " ")
            utmfile.write("\n")
コード例 #4
0
def trivector_cmd(id_vent: str,
                  neighbor_method: str,
                  radius: int,
                  threshold: float,
                  graph: str,
                  header=False):
    propagation = Propagation()
    # Setta i parametri
    if os.path.isfile("graph_gexf/" + graph):
        propagation.set_Graph(utility.load_graph(graph))
    else:
        print("Graph doesn't exist, default are loaded")
    propagation.set_trivector(threshold)
    id_vents = []
    propagation_method = "trivector"
    neighbor = ""
    if neighbor_method != None:
        if neighbor_method == "moore" or neighbor_method == "neumann":
            id_vents = utility.get_neighborhood(id_vent, neighbor_method,
                                                radius)
            neighbor += "_" + neighbor_method + str(radius)
        else:
            print("You must specify a valid neighbor method: moore / neumann.")
            return None
    else:
        id_vents = [id_vent]

    # Esecuzione algoritmo
    sparse_matrix = propagation.trivector(id_vents)
    # Esportazione in ASCII e calcolo metriche
    G = propagation.get_Graph()
    utility.visualize_and_metrics(id_vents, propagation_method, neighbor,
                                  sparse_matrix, G, header)
コード例 #5
0
def get_trivector_subgraph(tri_vect, real_vect):
    G = utility.load_graph()
    # lista di nodi invasi dalla simulazione e da trivector.
    list_nodes = []
    # scorro il vettore della simulazione reale
    for i in range(
            0, len(real_vect)
    ):  # il nodo è invaso se l'i-esima posizione del vettore è maggiore di 0
        if real_vect[i] > 0:
            list_nodes.append(str(i))
    # espansione simulazione reale al vicinato
    tmp = []
    for u in list_nodes:
        for v in G.successors(u):
            if v not in list_nodes and v not in tmp:
                tmp.append(v)
    list_nodes += tmp
    # scorro il vettore di trivector
    for i in range(0, len(tri_vect)):
        if tri_vect[i] > 0 and str(
                i
        ) not in list_nodes:  # il nodo è invaso se l'i-esima posizione del vettore è maggiore di 0
            list_nodes.append(str(i))

    # creo un grafo copia di G
    sub_G = G.copy()

    # cancello tutti i nodi del grafo che non sia nella lista
    for node in G.nodes:
        if node not in list_nodes:
            sub_G.remove_node(node)

    return sub_G
コード例 #6
0
def norm_to_graph(np_vect):
    G = load_graph()
    for u in G.nodes():
        G.node[u]["current_flow"] = np_vect.item(int(u))

    export_graph(G, "norm_from_col_normalization_graph.gexf", False)
    return G
コード例 #7
0
def test(id_vent, distance=4):
    propagation = Propagation()
    propagation.trivector([id_vent])
    G = propagation.get_Graph()

    # Conversione vent
    id_node = conversion.get_node_from_idvent(int(id_vent))

    # Estrazione sottografo
    for i in range(len(G.nodes)):
        if G.nodes[str(i)]['current_flow'] == 0:
            G.remove_node(str(i))

    # Cambio pesi
    for u, v, data in G.edges(data=True):
        if data['trasmittance'] == 0:
            G.edges[u, v]['trasmittance'] = math.inf
        else:
            G.edges[u, v]['trasmittance'] = -math.log(data['trasmittance'])

    # Lista nodi città
    city_nodes = []
    for n in G.nodes:
        if G.nodes[n]['priority'] > 0:
            city_nodes.append(n)

    # Shortest paths
    shortest_paths = {}
    for n in city_nodes:
        shortest_paths[n] = nx.shortest_path(G,
                                             id_node,
                                             n,
                                             weight='trasmittance')

    cutted_edges = {}
    for n in shortest_paths:
        path = shortest_paths[n]
        for i in range(distance, len(path) - distance):
            edge_id = (path[i - 1], path[i])
            if edge_id not in cutted_edges:
                propagation.set_Graph(utility.load_graph())
                propagation.cut_edges([[path[i - 1], path[i]]])
                sparse_matrix = propagation.trivector([id_vent])
                G = propagation.get_Graph()
                risk = metrics.compute([id_vent], 'trivector', sparse_matrix,
                                       G)[-1]
                cutted_edges[edge_id] = risk
                print(edge_id, risk)

    min_risk = min(cutted_edges, key=cutted_edges.get)
    print(min_risk)
コード例 #8
0
ファイル: Propagation.py プロジェクト: qcqnotfound/Graphlow
    def __init__(self):
        # --- Parametri default ---
        # Trivector:
        self.tri_threshold = 0.001
        self.tri_t_min = 0.015
        self.tri_t_max = 0.015

        # Eruption:
        self.eru_volume = 1000
        self.eru_n_days = 7
        self.eru_threshold = 0.15

        # Montecarlo:
        self.prob_epoch = 100
        self.prob_second_chance = 0

        self.G = utility.load_graph()
コード例 #9
0
def autocut_cmd(id_vent: str, distance: int, neighbor_method: str, radius: int,
                dimension: int, mode: str):
    propagation = Propagation()
    id_vents = []
    propagation_method = "trivector"
    neighbor = ""
    if neighbor_method != None:
        if neighbor_method == "moore" or neighbor_method == "neumann":
            id_vents = utility.get_neighborhood(id_vent, neighbor_method,
                                                radius)
            neighbor += "_" + neighbor_method + str(radius)
        else:
            print("You must specify a valid neighbor method: moore / neumann.")
            return None
    else:
        id_vents = [id_vent]

    # Esecuzione algoritmo prima di tagliare
    no_cutted_sparse = propagation.trivector(id_vents)
    G = propagation.get_Graph()
    # Decommentare se si vuole stampare le metriche del trivector senza taglio
    # utility.visualize_and_metrics(id_vents, propagation_method, no_cutted_sparse, G, False)

    # selezione degli archi da tagliare
    list_edges = ga.get_edges_to_cut(G, id_vents, distance, dimension, mode)

    propagation.set_Graph(utility.load_graph())
    # taglio archi selezionati
    propagation.cut_edges(list_edges)

    # esecuzione trivector dopo il taglio degli archi
    cutted_sparse = propagation.trivector(id_vents)
    G = propagation.get_Graph()
    utility.visualize_and_metrics(id_vents, propagation_method, neighbor,
                                  cutted_sparse, G, False)
    mc.ascii_barrier(id_vent, propagation_method + neighbor, list_edges)
コード例 #10
0
from transformers import DistilBertTokenizer, AdamW
from sklearn.metrics import accuracy_score, f1_score, classification_report
import model
from tqdm import tqdm
import prepare_data
import config
import utility
import telegram_bot as bot

print("-" * 80)
device = 'cuda' if cuda.is_available() else 'cpu'
print("Available Device: {}".format(device))
print("-" * 60)

#############################################################################################
graph = utility.load_graph(config.generic_path)
print(graph)


#############################################################################################
# Defining the training function on the 80% of the dataset for tuning the distilbert model
def retrain(model, training_loader, loss_fn, optimizer):
    tr_loss = 0
    n_correct = 0
    nb_tr_steps = 0
    nb_tr_examples = 0
    model.train()
    bot.telegram_bot_sendtext("re-training started for : " +
                              config.generic_path)
    for _, data in enumerate(tqdm(training_loader, 0)):
        ids = data['ids'].to(device, dtype=torch.long)
コード例 #11
0
def plot_3d_cmd(metric: str, id_vent: int, size: int, step: int):
    # Load dei grafi
    original_graph = utility.load_graph()
    immunological_graph = utility.load_graph(
        gexf_filename="immunological_graph.gexf")

    if metric == "precision":
        filename = "plot/" + str(id_vent) + "_" + str(size) + "_" + str(
            step) + "_precision3d.plt"
        if os.path.isfile(filename):
            x_coords, y_coords, original_list, trained_list = visualize.load_plot3D_from_file(
                filename)
        else:
            # Lista vents/nodes del sottografo
            id_nodes, id_vents = utility.get_node_vent_chessboard(
                id_vent, size, step)
            # Coords
            x_coords = []
            y_coords = []
            for id_node in id_nodes:
                x, y = conversion.cast_coord_attr(
                    original_graph.nodes[id_node]['coord_regions'])
                x_coords.append(x)
                y_coords.append(y)
            # Lista precision su grafo originale
            original_list = metrics.get_ppv_list(original_graph, id_vents)
            # Lista precision su grafo trained
            trained_list = metrics.get_ppv_list(immunological_graph, id_vents)
            # Salvataggio dei risultati su file
            visualize.save_plot3D_on_file(
                x_coords, y_coords, original_list, trained_list,
                "plot/" + str(id_vent) + "_" + str(size) + "_" + str(step) +
                "_precision3d.plt")

    elif metric == "recall":
        filename = "plot/" + str(id_vent) + "_" + str(size) + "_" + str(
            step) + "_recall3d.plt"
        if os.path.isfile(filename):
            x_coords, y_coords, original_list, trained_list = visualize.load_plot3D_from_file(
                filename)
        else:
            # Lista vents/nodes del sottografo
            id_nodes, id_vents = utility.get_node_vent_chessboard(
                id_vent, size, step)
            # Coords
            x_coords = []
            y_coords = []
            for id_node in id_nodes:
                x, y = conversion.cast_coord_attr(
                    original_graph.nodes[id_node]['coord_regions'])
                x_coords.append(x)
                y_coords.append(y)
            # Lista recall su grafo originale
            original_list = metrics.get_tpr_list(original_graph, id_vents)
            # Lista recall su grafo trained
            trained_list = metrics.get_tpr_list(immunological_graph, id_vents)
            # Salvataggio dei risultati su file
            visualize.save_plot3D_on_file(
                x_coords, y_coords, original_list, trained_list,
                "plot/" + str(id_vent) + "_" + str(size) + "_" + str(step) +
                "_recall3d.plt")

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(x_coords,
               y_coords,
               original_list,
               color='red',
               marker='X',
               s=50)
    ax.scatter(x_coords,
               y_coords,
               trained_list,
               color='blue',
               marker='D',
               s=50)

    for i in range(len(trained_list)):
        if trained_list[i] > original_list[i]:
            color = 'blue'
        else:
            color = 'red'

        ax.plot([x_coords[i], x_coords[i]], [y_coords[i], y_coords[i]],
                [original_list[i], trained_list[i]],
                color=color)

    plt.show()
コード例 #12
0
def make_graphs():
    global CN_GRAPH, ER_GRAPH, UPA_GRAPH
    CN_GRAPH  = utility.load_graph(GRAPH_URL)    
    ER_GRAPH  = er_graph(1239, 0.002)    
    UPA_GRAPH = upa_graph(1239, 2)    
コード例 #13
0
from Genetic_algorithm import Genetic_algorithm
from Propagation import Propagation
import graph_algorithm as ga
import numpy as np
import utility
import networkx as nx
from scipy import sparse

G = utility.load_graph("graphlow.gexf")
for u in G.nodes():
    if "Ragalna" in G.nodes[u]['city_names']:
        G.nodes[u]['priority'] = .5

nx.write_gexf(G, "ragalna.gexf")