def triad_count(egonet, nodes): wf = open("triad_count.txt", "w") total_census = defaultdict(list) for n in nodes: G = nx.DiGraph() G.add_edges_from(egonet[n]) census, node_census = triadic.triadic_census(G) # output format : id(tab)201(tab)021C(tab)... wf.write(n) for k in census.keys(): wf.write('\t') wf.write(str(census[k])) #if k in total_census.keys(): total_census[k].append(census[k]) #else: # total_census[k] = census[k] wf.write('\n') G.clear() wf.close() return total_census
def analyze_triads(sn): if not sn.is_directed: snd = networkx.DiGraph(sn) else: snd = sn census, node_census = triadic.triadic_census(snd) for key in census.keys(): print '%s类型的三节点有%d个' % (key, census[key]) return census, node_census
def analyze_triads(sn): if not sn.is_directed: snd=networkx.DiGraph(sn) else: snd=sn census,node_census=triadic.triadic_census(snd) for key in census.keys(): print '%s类型的三节点有%d个'%(key,census[key]) return census,node_census
def zscore(egonet, total_census, nodes): # average and stanard deviation of triad count (census) is originally for a null model (ex. random network or legitimate user network) # In this example, I used average and standard deviation as 1 and 0.5 wf = open("zscore.txt", "w") average_census = dict() std_census = dict() for k in total_census.keys(): average_census[k] = 1 # numpy.mean(total_census[k]) std_census[k] = 0.5 # math.sqrt(numpy.var(total_census[k])) del total_census[k] for n in nodes: G = nx.DiGraph() G.add_edges_from(egonet[n]) census, node_census = triadic.triadic_census(G) # output format (zscore): id(tab)201(tab)021C(tab)... wf.write(n) for k in census.keys(): wf.write('\t') zscore = float(census[k] - average_census[k]) / float( std_census[k]) wf.write(str(zscore)) wf.write('\n') G.clear() wf.close()
import multimode as mm import triadic # open the file in_file=csv.reader(open('9_11_edgelist.txt','rb')) g=net.Graph() for line in in_file: g.add_edge(line[0],line[1],weight=line[2],conf=line[3]) #first, let's make sure that all nodes in the graph have the 'flight' attribute for n in g.nodes_iter(): g.node[n]['flight']='None' attrb=csv.reader(open('9_11_attrib.txt','rb')) for line in attrb: g.node[line[0]]['flight']=line[1] # Connected_component_subgraphs() returns a list of components, sorted largest to smallest components=net.connected_component_subgraphs(g) # pick the first and largest component cc = components[0] # type-string tells the function what attribute to differentiate on mm.plot_multimode(cc,type_string='flight') # run triadic analysis census, node_census = triadic.triadic_census(cc)
##containing a graph object and a dictionary that has all the questions ## per user and topic respectively dble = afil.affiliation() # Clear the canvas for plotting a.cla() #Unpack the tuple, g is the graph object and dictz is the dictionary g = dble[0] dictz = dble[1] # Define the layout for the graph see networkx.draw documentation for more pos = nx.spring_layout(g) #Call plot function from library to plot Affiliate network nx.write_graphml(g, "test.graphml") mm.plot_multimode(g, type_string='questions', sizing=dictz) census, node_census = triadic.triadic_census(g) # Define drawing area for plot in GUI canvas = FigureCanvasTkAgg(f, master=w) canvas.show() canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1) ########################################################################### ''' This section of the code defines the actions that will be performed by the buttons of the GUI. These actions have the functionality in the code but in this section they are not binded (related to) the buttons in the GUI ''' def multimode_graph():
def get_triadic_census(g): """carries out triadic census. Returns census for graph as a whole and for each node.""" import triadic return triadic.triadic_census(g)