def find_all_triplets(graph,edge_attr_name): ''' this method runs on the graph and finds triplets of nodes x,i,j such that the edges x-i and x-j exist but the edge i-j doesn't''' diff_treshold.print_log("find all triplets start") triplets_lst = [] for node_x in graph.nodes(): #diff_treshold.print_log("find all triplets new node - " + str(node_x)) nei_lst = graph.neighbors(node_x) for i in range(0 , len(nei_lst)): node_i = nei_lst[i] panel_i = diff_treshold.get_panel_id_substr(node_i) node_i_neis = graph.neighbors(node_i) corr_i = graph.get_edge_data(node_x,node_i)[edge_attr_name] for j in range(i + 1, len(nei_lst)): if nei_lst[j] not in node_i_neis and panel_i != diff_treshold.get_panel_id_substr(nei_lst[j]): #node_x and node_i are neighbours, also node_x and node_j #but node_i and node_j not and they are from different panels corr_j = graph.get_edge_data(node_x,nei_lst[j])[edge_attr_name] var = (node_x,node_i,nei_lst[j],corr_i,corr_j) triplets_lst.append(var) #print("enter var") diff_treshold.print_log("find all triplets after loop return list") return triplets_lst
def analyze_trios(trios_lst, treshold): same_panel_high_corr = 0 same_panel_low_corr = 0 diff_panel_high_corr = 0 diff_panel_low_corr = 0 for trio in trios_lst: node1 = trio[1] node2 = trio[2] unconnected_corr = trio[5] diff_panel = False low_corr = unconnected_corr < treshold if diff_treshold.get_panel_id_substr(node1) != diff_treshold.get_panel_id_substr(node2): diff_panel = True if diff_panel: if low_corr: diff_panel_low_corr += 1 else: diff_panel_high_corr += 1 else: if low_corr: same_panel_low_corr += 1 else: same_panel_high_corr += 1 return (same_panel_high_corr,same_panel_low_corr,diff_panel_high_corr,diff_panel_low_corr)
def graph_corr_dist_between_pannels(graph, panel1, panel2, is_show, attr_name): ''' this method calculates the distribution of the correlation between 2 panels''' corr_dist = [] for u, v in graph.edges(): panel_a = diff_treshold.get_panel_id_substr(u) panel_b = diff_treshold.get_panel_id_substr(v) if (panel_a == panel1 and panel_b == panel2) or (panel_a == panel2 and panel_b == panel1): corr_dist.append(graph.get_edge_data(u, v)[attr_name]) plt.xlabel("correlation") plt.ylabel("frequency") plt.title("frequency for correlation between panels " + panel1 + " and " + panel2) plt.hist(corr_dist) fig_name = panel1 + "_" + panel2 + "_corr_dist_graph.png" if is_show: plt.savefig(fig_name) plt.show()
def graph_pie_chart_neigbours_panel(graph, is_pie, is_save): ''' this method calcs the histogram of neighbours's panel per each panel ''' n = len(diff_treshold.PANELS_LST) nei_mat = np.zeros( (n, n)) #matrix with cell for each two panels combination for u, v in graph.edges(): u_panel = diff_treshold.get_panel_id_substr(u) v_panel = diff_treshold.get_panel_id_substr(v) u_panel_idx = diff_treshold.PANELS_LST.index(u_panel) v_panel_idx = diff_treshold.PANELS_LST.index(v_panel) nei_mat[u_panel_idx][v_panel_idx] += 1 nei_mat[v_panel_idx][u_panel_idx] += 1 # symetry #print and save results labels = diff_treshold.PANELS_LST colors = ['gold', 'yellow', 'blue', 'red', 'green', 'brown'] for i in range(0, n): #set the plot plt.xlabel("panel ID") plt.ylabel("amount of neigbours") plt.title("histogram of neigbours of traits from panel P" + str(i + 1)) if is_pie: plt.pie(nei_mat[i], colors=colors, labels=labels, autopct='%1.1f%%') else: #bar plot plt.bar(np.arange(n), nei_mat[i], align='center') plt.xticks(np.arange(n), labels) if is_save: fig_name = "P" + str(i + 1) + "_neigbours_distribution_full_graph.png" plt.savefig(fig_name) plt.show()
def create_graph_from_network(network): ''' This method gets a network and creates a graph from it''' diff_treshold.print_log("create graph - start") g = nx.Graph() for key in network.keys(): g.add_node(key, panel_id = diff_treshold.get_panel_id_substr(key)) #g.add_nodes_from(network.keys()) diff_treshold.print_log("create graph - before adding edges") #create list of verteces neigbours_lst = [] for edge in network.keys(): for val in network[edge]: neigbours_lst.append((edge, val)) g.add_edges_from(neigbours_lst) diff_treshold.print_log("create graph - after adding edges, return graph") return g