Ejemplo n.º 1
0
def batch_analysis(n_vents):
    # Genera n vent casualmente
    vent_list = []
    # Seleziona tra le simulazioni salvate
    for sim in os.listdir(("sparse/")):
        if "sparse_sim_c" in sim:
            vent_list.append(sim[13:-4])
    vent_list = random.sample(vent_list, n_vents)
    metrics_matrix = []
    print("Vent to analyze:", vent_list)
    for vent in vent_list:
        propagation = Propagation()
        # Esecuzione algoritmo
        sparse_matrix = propagation.trivector(vent)
        # Esportazione in ASCII e calcolo metriche
        metrics_list = metrics.compute(vent, "trivector", sparse_matrix)
        metrics_matrix.append(metrics_list)
        print("vent:", vent, " analyzed")

    dataframe = pd.DataFrame(metrics_matrix,
                             columns=[
                                 "PPV", "PPVF", "TPR", "TPRF", "F1", "F1F",
                                 "THREAT", "THREATF"
                             ])
    print(dataframe)
    print("\n")
    print(dataframe.describe())
Ejemplo n.º 2
0
def realsim_cmd(id_vent: int, real_class: str, neighbor_method: str,
                radius: int):
    id_vent = str(id_vent - 1)
    propagation = Propagation()
    id_vents = [id_vent]
    neighbor = ""
    # Se la classe è 0 allora bisogna fare Unify
    if real_class == "0":
        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
        sparse_matrix_c, sparse_matrix_d = propagation.real(
            id_vents, real_class, neighbor)
        # Esportazione in ASCII Grid
        for i in range(len(id_vents)):
            id_vents[i] = int(id_vents[i]) + 1
        mc.ascii_creator(id_vents, "ucsim" + neighbor, sparse_matrix_c)
        mc.ascii_creator(id_vents, "udsim" + neighbor, sparse_matrix_d)
        return
    else:
        if len(id_vents) > 1:
            raise ValueError("multiple vents not supported for class != 0")
        sparse_matrix = propagation.real(id_vents, real_class, neighbor)
        # Esportazione in ASCII Grid
        mc.ascii_creator([str(id_vent + 1)], neighbor + real_class,
                         sparse_matrix)
        return
Ejemplo n.º 3
0
def get_ppv_list(G, vent_list):
    p = Propagation()
    p.set_Graph(G)

    ppv_list = []
    for vent in vent_list:
        sparse_matrix = p.trivector([vent])
        ppv = compute([vent], "", sparse_matrix, G)[0]
        ppv_list.append(ppv)

    return ppv_list
Ejemplo n.º 4
0
def get_tpr_list(G, vent_list):
    p = Propagation()
    p.set_Graph(G)

    tpr_list = []
    for vent in vent_list:
        sparse_matrix = p.trivector([vent])
        tpr = compute([vent], "", sparse_matrix, G)[2]
        tpr_list.append(tpr)

    return tpr_list
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
    def __init__(self, id_nodes: list, edges: list, real_vect: list, max_age = 5):
        # Id node
        self.id_nodes = id_nodes
        # Lista dei pesi degli archi
        self.edges = np.array(edges)
        # Real vector
        self.real_vect = real_vect
        # Valore di fitness della soluzione
        self.fitness = 0
        # Età massima della soluzione
        self.max_age = max_age

        # Vita della soluzione
        self.age = np.random.randint(0, (2 * max_age) / 3)

        self.propagation = Propagation()
Ejemplo n.º 7
0
 def analyse(self, threshold, window=100):
     prop = Propagation(self.signals, threshold, window)
     self.spikes = prop.getspikes()
     temp = self.spikes[0]
     self.spikes[0] = self.spikes[1]
     self.spikes[1] = temp
     self.spikes[0] = numpy.array(self.spikes[0])
     self.spikes[1] = numpy.array(self.spikes[1])
     print str(len(self.spikes[0]))
     self.peaks[0] = findpeaks(self.spikes[0])
     self.peaks[1] = findpeaks(self.spikes[1])
     times = getspeeds(self.peaks[0], self.peaks[1])
     self.times = [
         x * window / self.signals[0].sampling_rate.magnitude for x in times
     ]
     print self.times
     return self.times
Ejemplo n.º 8
0
def cut_cmd(id_vent, list_edges: list, neighbor_method, radius):
    edges_to_cut = []
    for edges in list_edges:
        edges_to_cut.append(edges.split(','))
    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]

    propagation.cut_edges(edges_to_cut)
    # Esportazione in ASCII e calcolo metriche
    sparse_matrix = propagation.trivector(id_vents)
    G = propagation.get_Graph()
    utility.visualize_and_metrics(id_vents, propagation_method, neighbor,
                                  sparse_matrix, G, False)
    mc.ascii_barrier(id_vent, propagation_method + neighbor, edges_to_cut)
Ejemplo n.º 9
0
def parameter_analysis(id_vent):
    print("Vent to analyze:", id_vent)
    metrics_matrix = []
    parameter_list = [0.001, 0.0001, 0.01]
    # Algoritmo eseguito con i parametri di default
    for parameter in parameter_list:
        propagation = Propagation()
        # Setta i parametri
        propagation.set_trivector(parameter)
        # Esecuzione algoritmo
        sparse_matrix = propagation.trivector(id_vent)
        # Esportazione in ASCII e calcolo metriche
        metrics_list = metrics.compute(id_vent, "trivector", sparse_matrix)
        metrics_matrix.append(metrics_list)

    dataframe = pd.DataFrame(metrics_matrix,
                             columns=[
                                 "PPV", "PPVF", "TPR", "TPRF", "F1", "F1F",
                                 "THREAT", "THREATF"
                             ])
    print(dataframe)
    print("\n")
Ejemplo n.º 10
0
def immunological_train_cmd(id_vent: int, size: int, step: int,
                            population_len: int, rho: int, epochs: int):
    # Lista vents/nodes del sottografo
    id_nodes, id_vents = utility.get_node_vent_chessboard(id_vent, size, step)
    print("Calcolo chessboard terminato!")

    # Creazione del vettore dei real vect
    real_vect_list = []
    for vent in id_vents:
        real_vect_list.append(
            np.load("Data/real_vectors/" + str(vent) + ".npy"))
    # Creazione del vettore dei trivector
    p = Propagation()
    tri_vect_list = []
    for node in id_nodes:
        tri_vect_list.append(p.trivector_train(node))

    # Real vect e tri vect unificati
    real_vect = np.zeros(5820)
    tri_vect = np.zeros(5820)
    for i in range(5820):
        for vect in tri_vect_list:
            if vect[i] > 0:
                tri_vect[i] = 1
                break
        for vect in real_vect_list:
            if vect[i] > 0:
                real_vect[i] = 1
                break

    # Creazione del sottografo
    SG = ga.get_trivector_subgraph(tri_vect, real_vect)

    print("Init immunological Algorithm")
    # Algoritmo immunologico
    imm = Immunological_algorithm(id_vents, id_nodes, SG.edges, population_len,
                                  rho)
    imm.start(epochs)
Ejemplo n.º 11
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)
Ejemplo n.º 12
0
def eruption_cmd(id_vent, volume, n_days, threshold, header=False):
    propagation = Propagation()
    # Setta i parametri
    propagation.set_eruption(volume, n_days, threshold)
    # Esecuzione algoritmo
    sparse_matrix = propagation.eruption([id_vent])
    # Esportazione in ASCII e calcolo metriche
    G = propagation.get_Graph()
    utility.visualize_and_metrics([id_vent], "eruption", "", sparse_matrix, G,
                                  header)
Ejemplo n.º 13
0
def montecarlo_cmd(id_vent, n_epochs, second_chance, header=False):
    if id_vent == 0:
        return
    propagation = Propagation()
    # Setta i parametri
    propagation.set_montecarlo(n_epochs, second_chance)
    # Esecuzione algoritmo
    sparse_matrix = propagation.montecarlo([id_vent])
    # Esportazione in ASCII e calcolo metriche
    G = propagation.get_Graph()
    utility.visualize_and_metrics([id_vent], "montecarlo", "", sparse_matrix,
                                  G, header)
Ejemplo n.º 14
0
class Immunological_solution:
    def __init__(self, id_nodes: list, edges: list, real_vect: list, max_age = 5):
        # Id node
        self.id_nodes = id_nodes
        # Lista dei pesi degli archi
        self.edges = np.array(edges)
        # Real vector
        self.real_vect = real_vect
        # Valore di fitness della soluzione
        self.fitness = 0
        # Età massima della soluzione
        self.max_age = max_age

        # Vita della soluzione
        self.age = np.random.randint(0, (2 * max_age) / 3)

        self.propagation = Propagation()

    def __fbeta_score(self, tri_vect, idx: int):
        tp, fp, tn, fn = 0, 0, 0, 0

        for i in range(0, len(self.real_vect[idx])):
            if tri_vect[i] > 0 and self.real_vect[idx][i] == 1:  # tp
                tp += 1
            if tri_vect[i] > 0 and self.real_vect[idx][i] == 0:  # fp
                fp += 1
            if tri_vect[i] == 0 and self.real_vect[idx][i] == 0:   # tn
                tn += 1
            if tri_vect[i] == 0 and self.real_vect[idx][i] == 1:   # fn 
                fn += 1

        ppv = tp / (tp + fp)
        tpr = tp / (tp + fn)
        # beta grande! 
        beta = 3
        fbeta_score = (1 + beta**2) * ppv * tpr / ((beta**2 * ppv) + tpr)
        return fbeta_score

    def __update_weights(self, edges_dict: dict):
        # Prendere il grafo
        G = self.propagation.get_Graph()
        # Aggiornare i pesi
        i = 0
        for u, v in edges_dict:
            G.edges[u, v]['prop_weight'] = float(self.edges[i])
            i += 1
        # Settare il grafo
        self.propagation.set_Graph(G)

    def compute_fitness(self, edges_dict: dict):
        # Aggiornare i pesi
        self.__update_weights(edges_dict)
        # Trivector
        fit_list = []
        for i in range(len(self.id_nodes)):
            tri_vect = self.propagation.trivector_train(self.id_nodes[i])
            # f_beta score
            fit_list.append(self.__fbeta_score(tri_vect, i))
        
        self.fitness = np.mean(fit_list)

    def hypermutation(self, rho):
        alpha = math.exp(-rho * self.fitness)
        number_of_mutation = math.floor((alpha * len(self.edges)))

        std = (1 - self.fitness) / 5
        for _ in range(number_of_mutation):
            r = np.random.randint(0, len(self.edges)-1)
            w = np.random.normal(self.edges[r], std)
            if w < 0:
                w = 0
            if w > 1:
                w = 1
            self.edges[r] = w

    def increment_age(self):
        self.age += 1

    def set_random_age(self):
        self.age = np.random.randint(0, (2 * self.max_age) / 3)

    def set_fitness(self, fitness):
        self.fitness = fitness
Ejemplo n.º 15
0
def test_active_error(G, test_seeds, gold_infect_list):
    propagation = Propagation(G, beta, max_infect_rate)
    test_infect_list = propagation.infect_by_rate(test_seeds, 0)
    res = active_error(G, gold_infect_list, test_infect_list)
    return res
Ejemplo n.º 16
0
def run_propagation(G, seed_num):
    propagation = Propagation(G, beta, max_infect_rate)
    seeds = propagation.choice_seeds(seed_num)
    infected_list = propagation.infect_by_rate(seeds, 0)
    #infected_list = propagation.infect_by_step(seeds, 0) ;
    return [seeds, infected_list]
Ejemplo n.º 17
0
            num) + ".abf"
        print("Loading " + fileName)
        try:
            r = neo.io.AxonIO(fileName)

            bl = r.read_block(lazy=False, cascade=True)
        except IOError:
            break

        signals = [None] * 2
        signals[0] = bl.segments[0].analogsignals[0]
        signals[1] = bl.segments[0].analogsignals[15]
        t = numpy.linspace(1, signals[0].sampling_rate.magnitude,
                           len(signals[0]))
        print("Data loaded")
        prop = Propagation(signals)
        spikes = prop.getspikes()

        #peaks1 = find_peaks_cwt(spikes[0],numpy.arange(11,12))
        #peaks2 = find_peaks_cwt(spikes[1], numpy.arange(11,12))

        spikes1 = array(spikes[0])
        spikes2 = array(spikes[1])

        xs = numpy.linspace(1,
                            signals[0].sampling_rate.magnitude,
                            num=len(spikes1))
        peaks1 = findpeaks(spikes[0])
        peaks2 = findpeaks(spikes[1])
        print "Length of peaks1: " + str(len(peaks1))
        print "Length of peaks2: " + str(len(peaks2))
Ejemplo n.º 18
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)