当前位置: 首页>>代码示例>>Python>>正文


Python networkx.from_pandas_dataframe函数代码示例

本文整理汇总了Python中networkx.from_pandas_dataframe函数的典型用法代码示例。如果您正苦于以下问题:Python from_pandas_dataframe函数的具体用法?Python from_pandas_dataframe怎么用?Python from_pandas_dataframe使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了from_pandas_dataframe函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: readDGFrameFile

def readDGFrameFile(filename, interRRI_norRNA=1, support_read=3):
	fn_stat_dict = nested_dict()
	inter, intra = 0, 0
	with open(filename, 'r') as TXT:
		for line in TXT:
			line = line.strip()
			if not line or line.startswith('#'):
				continue
			arr = line.split('\t')
			if arr[1] == arr[5]:
				intra += 1
			else:
				inter += 1
	fn_stat_dict['inter'] = inter
	fn_stat_dict['intra'] = intra
	fn_stat_dict['all'] = intra + inter

	df = pd.read_csv(filename, header=0, sep='\t')
	df['type'] = ['intra' if i == j else 'inter' for i,j in zip(df['lchr'], df['rchr'])]
	df_inter_RRI = df[df['type']=='inter']
	nx_inter_RRI = nx.from_pandas_dataframe(df_inter_RRI, 'lchr', 'rchr')
	fn_stat_dict['uniq RRI']  = len(nx_inter_RRI.edges())
	if interRRI_norRNA:
		df_inter_RRI = df_inter_RRI[(df_inter_RRI['ltype'].isin(['mRNA', 'lncRNA'])) & (df_inter_RRI['rtype'].isin(['mRNA', 'lncRNA']))]
	df_inter_RRI = df_inter_RRI[df_inter_RRI['support']>=support_read]
	nx_inter_RRI = nx.from_pandas_dataframe(df_inter_RRI, 'lchr', 'rchr')
	nx_inter_RRI_info_dict, G_largest = RRI_network_property2(nx_inter_RRI)
	for i,j in nx_inter_RRI_info_dict.items():
		fn_stat_dict[i] = j
	# fn_stat_df['sampling'] = ''
	fn_stat_df = pd.DataFrame(fn_stat_dict, index=[0])
	return fn_stat_df
开发者ID:Tsinghua-gongjing,项目名称:test,代码行数:32,代码来源:network_downsampling_stats.py

示例2: test_from_dataframe_all_attr

 def test_from_dataframe_all_attr(self, ):
     Gtrue = nx.Graph([('E', 'C', {'cost': 9, 'weight': 10}),
                       ('B', 'A', {'cost': 1, 'weight': 7}),
                       ('A', 'D', {'cost': 7, 'weight': 4})])
     G = nx.from_pandas_dataframe(self.df, 0, 'b', True)
     self.assert_equal(G, Gtrue)
     # MultiGraph
     MGtrue = nx.MultiGraph(Gtrue)
     MGtrue.add_edge('A', 'D', cost=16, weight=4)
     MG = nx.from_pandas_dataframe(self.mdf, 0, 'b', True, nx.MultiGraph())
     self.assert_equal(MG, MGtrue)
开发者ID:jklaise,项目名称:networkx,代码行数:11,代码来源:test_convert_pandas.py

示例3: read_dir

def read_dir(dir='/Share/home/zhangqf7/gongjing/zebrafish/data/paris/shi-zp-5-rep-combine/downsampling_N', to_dgframe=0, get_inter_intra=1, read_nx=1, interRRI_norRNA=1, support_read=3):
	fn_ls = os.listdir(dir)
	# print fn_ls

	fn_stat_dict = nested_dict()
	downsampling_N_draw = dir + '.subnetwork.draw.pdf'
	fig,ax=plt.subplots(10,1)
	for n,fn in enumerate(fn_ls):
		print "process: %s"%(fn)
		dfFile = dir + '/' + fn + '/' + '27-DG'
		frameFile = dfFile + '.txt'
		if to_dgframe:
			paris_dg2frame.DG2Frame(dfFile=dfFile, frameFile=frameFile)
		if get_inter_intra:
			inter, intra = 0, 0
			with open(frameFile, 'r') as TXT:
				for line in TXT:
					line = line.strip()
					if not line or line.startswith('#'):
						continue
					arr = line.split('\t')
					if arr[1] == arr[5]:
						intra += 1
					else:
						inter += 1
			fn_stat_dict[fn]['inter'] = inter
			fn_stat_dict[fn]['intra'] = intra
			fn_stat_dict[fn]['all'] = intra + inter
		if read_nx:
			df = pd.read_csv(frameFile, header=0, sep='\t')
			df['type'] = ['intra' if i == j else 'inter' for i,j in zip(df['lchr'], df['rchr'])]
			df_inter_RRI = df[df['type']=='inter']
			nx_inter_RRI = nx.from_pandas_dataframe(df_inter_RRI, 'lchr', 'rchr')
			fn_stat_dict[fn]['uniq RRI']  = len(nx_inter_RRI.edges())
			if interRRI_norRNA:
				df_inter_RRI = df_inter_RRI[(df_inter_RRI['ltype'].isin(['mRNA', 'lncRNA'])) & (df_inter_RRI['rtype'].isin(['mRNA', 'lncRNA']))]
			df_inter_RRI = df_inter_RRI[df_inter_RRI['support']>=support_read]
			nx_inter_RRI = nx.from_pandas_dataframe(df_inter_RRI, 'lchr', 'rchr')
			nx_inter_RRI_info_dict, G_largest = RRI_network_property2(nx_inter_RRI)
			for i,j in nx_inter_RRI_info_dict.items():
				fn_stat_dict[fn][i] = j
			# fn_stat_dict[fn]['uniq RRI']  = len(nx_inter_RRI.edges())
			if n < 10:
				draw_graph(G_largest, ax=ax[n])
	plt.savefig(downsampling_N_draw)
	savefn = dir + '.stat.txt'
	fn_stat_df = pd.DataFrame.from_dict(fn_stat_dict)
	fn_stat_df = fn_stat_df.T
	fn_stat_df['sampling'] = fn_stat_df.index
	print fn_stat_df.head()

	fn_stat_df.to_csv(savefn, header=True, index=False, sep='\t')

	return fn_stat_df
开发者ID:Tsinghua-gongjing,项目名称:test,代码行数:54,代码来源:network_downsampling_stats.py

示例4: read_graph

def read_graph(filename, directed=True, sep=' ', header = None):
    """
    Create networkx graph using pandas.
    :param filename: every line (u, v)
    :param directed: boolean
    :param sep: separator in file
    :return
    """
    df = pd.read_csv(filename, sep=sep, header = header)
    if directed:
        G = nx.from_pandas_dataframe(df, 0, 1, create_using=nx.DiGraph())
    else:
        G = nx.from_pandas_dataframe(df, 0, 1)
    return G
开发者ID:nd7141,项目名称:PIMUS,代码行数:14,代码来源:MIA.py

示例5: test_from_dataframe_all_attr

 def test_from_dataframe_all_attr(self,):
     Gtrue = nx.Graph(
         [
             ("E", "C", {"cost": 9, "weight": 10}),
             ("B", "A", {"cost": 1, "weight": 7}),
             ("A", "D", {"cost": 7, "weight": 4}),
         ]
     )
     G = nx.from_pandas_dataframe(self.df, 0, "b", True)
     self.assert_equal(G, Gtrue)
     # MultiGraph
     MGtrue = nx.MultiGraph(Gtrue)
     MGtrue.add_edge("A", "D", cost=16, weight=4)
     MG = nx.from_pandas_dataframe(self.mdf, 0, "b", True, nx.MultiGraph())
     self.assert_equal(MG, MGtrue)
开发者ID:CrazyPython,项目名称:networkx,代码行数:15,代码来源:test_convert_pandas.py

示例6: make_graph

def make_graph(edgesdf, edge_attr='fdist', name=None):
	g = nx.from_pandas_dataframe(edgesdf, source='node1',
								 target='node2', edge_attr=edge_attr)
	if name:
		g.add_node('namenode', name=name)

	return g
开发者ID:crupley,项目名称:hoodie-development,代码行数:7,代码来源:graph.py

示例7: build_file_move_graph

def build_file_move_graph(file_frame):
    move_frame = file_frame[file_frame.move]
    move_graph = nx.from_pandas_dataframe(
        move_frame,
        source='file_path1', target='file_path2',
        edge_attr='hexsha', create_using=nx.DiGraph())
    return move_graph
开发者ID:ashapochka,项目名称:saapy,代码行数:7,代码来源:git_commit_utils.py

示例8: __init__

 def __init__(self, path):
     '''Constructor'''
     edges = pd.read_csv(path, header=0)
     self.l = edges.columns.values.tolist()
     self.l.remove('source')
     self.l.remove('target')
     self.G = nx.from_pandas_dataframe(edges, 'source', 'target', self.l, create_using=nx.DiGraph())
开发者ID:danjamker,项目名称:UrbanDictonaryMinner,代码行数:7,代码来源:GraphPrep.py

示例9: simple_visualization

def simple_visualization (airport_df, routes_df):
	if (airport_df is None) or (routes_df is None):
		print "Data cannot be retrieved and read"
	else:
		airport_us = airport_df[(airport_df.Country == "United States")][['Name','Lat', 'Long', 'IATA', 'ICAO']]
		us_airport_ix = airport_us.index.values
		routes_us = routes_df[(routes_df['Source Airport ID'].isin(us_airport_ix)) &
		                     (routes_df['Dest Airport ID'].isin(us_airport_ix))] #extract routes that flyies from AND to USA
		routes_us =  pd.DataFrame(routes_us.groupby(['Source Airport', 'Dest Airport']).size().reset_index(name='counts'))
		# to find number of flights in and out of an airport
		# it is similar to find number of rows in which each airport occur in either one of the 2 columns
		counts = routes_us['Source Airport'].append(routes_us.loc[routes_us['Source Airport'] != routes_us['Dest Airport'], 'Dest Airport']).value_counts()
		# create a data frame of position based on names in count
		counts = pd.DataFrame({'IATA': counts.index, 'total_flight': counts})
		pos_data = counts.merge(airport_us, on = 'IATA')

		# Create graph
		graph = nx.from_pandas_dataframe(routes_us, source = 'Source Airport', target = 'Dest Airport',
		                        edge_attr = 'counts',create_using = nx.DiGraph())

		# default graph using Networkx inbuilt graph tools
		plt.figure(figsize = (10,9))
		nx.draw_networkx(graph)
		plt.savefig("./images/networkx_basemap/map_0.png", format = "png", dpi = 300)
		plt.show()

		# Set up base map
		plt.figure(figsize=(15,20))
		m = Basemap(
		        projection='merc',
		        llcrnrlon=-180,
		        llcrnrlat=10,
		        urcrnrlon=-50,
		        urcrnrlat=70,
		        lat_ts=0,
		        resolution='l',
		        suppress_ticks=True)

		# import long lat as m attribute
		mx, my = m(pos_data['Long'].values, pos_data['Lat'].values)
		pos = {}
		for count, elem in enumerate (pos_data['IATA']):
		    pos[elem] = (mx[count], my[count])

		# draw nodes and edges and over aly on basemap
		nx.draw_networkx_nodes(G = graph, pos = pos, node_list = graph.nodes(), node_color = 'r', alpha = 0.8,
		                       node_size = [counts['total_flight'][s]*3 for s in graph.nodes()])
		nx.draw_networkx_edges(G = graph, pos = pos, edge_color='g', width = routes_us['counts']*0.75, 
		                       alpha=0.2, arrows = False)

		m.drawcountries(linewidth = 3)
		m.drawstates(linewidth = 0.2)
		m.drawcoastlines(linewidth=3)
		plt.tight_layout()
		plt.savefig("./images/networkx_basemap/map_2.png", format = "png", dpi = 300)
		plt.show()
		print ("successful visualization")
		return 0
开发者ID:rxhmdia,项目名称:Various-projects,代码行数:58,代码来源:flights_networkx.py

示例10: test_from_dataframe_multi_attr

 def test_from_dataframe_multi_attr(self,):
     Gtrue = nx.Graph(
         [
             ("E", "C", {"cost": 9, "weight": 10}),
             ("B", "A", {"cost": 1, "weight": 7}),
             ("A", "D", {"cost": 7, "weight": 4}),
         ]
     )
     G = nx.from_pandas_dataframe(self.df, 0, "b", ["weight", "cost"])
     self.assert_equal(G, Gtrue)
开发者ID:CrazyPython,项目名称:networkx,代码行数:10,代码来源:test_convert_pandas.py

示例11: make_graph

def make_graph(cutdf):
    """
    Convert dataframe of a list of edges into a networkx graph object

    Args:
        cutdf : pandas dataframe containing list of edges with
            features 'source' and 'target' containing node numbers
    Returns:
        networkx graph object
    """
    g = nx.from_pandas_dataframe(cutdf, 'source', 'target')
    return g
开发者ID:nhu2000,项目名称:hoodie,代码行数:12,代码来源:clusterize.py

示例12: calculate

def calculate(words):
    # instantiate a dictionary to later be filled with word:miscores
    wc = defaultdict(float)
    frames = []
    print("...it will take a while. Wait a sec...")
    for word in words:
        payload = {'searchstring': word.encode('ascii'),
                   'searchpositional':'word',
                   'searchpostag':'all',
                   'contextsize':'60c',
                   'sort2':'right',
                   'terminate':'100',
                   'searchtype':'coll',
                   'mistat':'on',
                   'collocspanleft':'2',
                   'collocspanright':'2',
                   'collocfilter':'noun'}

        r = requests.get("http://clic.cimec.unitn.it/cgi-bin/cqp/cqp.pl?corpuslist=WEBBIT", params=payload)
        soup = BeautifulSoup(r.content, 'lxml')

        # parse the html table and extract words and miscores. Add scores
    
        temp = []
        for tr in soup.find_all('tr')[1:]:
            tds = tr.find_all('td')
            word = tds[0].text.split('~~')[1]
            mi = float(tds[4].text)
            wc[word] += mi
            temp.append(map(lambda x:x.text,tds[0:]))
        x = pd.DataFrame(temp)
        df = pd.DataFrame()
        df['coll'] = x.ix[0:,0].apply(lambda x: x.split('~~')[1])
        df['word'] = x.ix[0:,0].apply(lambda x: x.split('~~')[0])
        df['mi'] = x.ix[0:,4]
        frames.append(df)

    #sort the results in decreasing order        
    results = []
    for w in sorted(wc, key=wc.get, reverse=True):
        results.append((w, wc[w]))

    #spit out the top result. If using ipython you can check the rest of the list by tiping `results`
    #viz part
    results_df = pd.concat(frames)

    G=nx.from_pandas_dataframe(results_df, 'word','coll',['mi'])
    mat = nx.adjacency_matrix(G).todense()
    viz = lgn.force(mat)
    vid = viz.id
    print(vid)
    url = '<iframe src="http://public.lightning-viz.org/visualizations/'+vid+'/iframe/" width=100% height=400px>'
    return (results[0][0].strip(),url)
开发者ID:anbasile,项目名称:mwe,代码行数:53,代码来源:words.py

示例13: weightGraph

    def weightGraph(self, datacontacts, mi_threshold, time_treshold=0.6):
        if len(self.mol.get('resid', 'name CA')) != len(self.resids):
            raise Exception('The length of the protein doesn\'t match the Mutual Information data')
        contactcat = np.concatenate(datacontacts.dat)
        contacts_matrix = np.zeros([len(self.resids), len(self.resids)])
        for i in range(contactcat.shape[1]):
            counter = np.count_nonzero(contactcat[:, i])
            resid1 = self.residmap[self.mol.resid[datacontacts.description.atomIndexes[i][0]]]
            resid2 = self.residmap[self.mol.resid[datacontacts.description.atomIndexes[i][1]]]
            contacts_matrix[resid1][resid2] = counter

        self.graph_array = np.zeros([contacts_matrix.shape[0], contacts_matrix.shape[0]])
        mask = (self.mi_matrix > mi_threshold) & (contacts_matrix > (time_treshold * contactcat.shape[0]))
        self.graph_array[mask] = self.mi_matrix[mask]

        intermed = []
        for source in range(self.graph_array.shape[0]):
            for target in range(source, self.graph_array.shape[1]):
                if self.graph_array[source, target] != 0 and target > source:
                    intermed.append(
                        [int(self.resids[source]), int(self.resids[target]), float(self.graph_array[source, target])])
        import pandas as pd
        import networkx as nx
        from sklearn.cluster.spectral import SpectralClustering

        pd = pd.DataFrame(intermed, columns=['source', 'target', 'weight'])
        pd[['source', 'target']] = pd[['source', 'target']].astype(type('int', (int,), {}))
        pd['weight'] = pd['weight'].astype(type('float', (float,), {}))
        G = nx.from_pandas_dataframe(pd, 'source', 'target', ['weight'])
        ## setSegment
        segids = self.mol.get('segid', 'name CA')
        seg_res_dict = {key: value for (key, value) in zip(self.resids, segids) if
                        np.any(pd.loc[(pd['source'] == key)].index) or np.any(pd.loc[(pd['target'] == key)].index)}
        nx.set_node_attributes(G, 'Segment', seg_res_dict)
        ## set
        if not nx.is_connected(G):
            G = max(nx.connected_component_subgraphs(G), key=len)
        flow_cent = nx.current_flow_betweenness_centrality(G, weight='weight')
        nx.set_node_attributes(G, 'flowcent', flow_cent)
        Spectre = SpectralClustering(n_clusters=10, affinity='precomputed')
        model = Spectre.fit_predict(self.graph_array)
        model = model.astype(type('float', (float,), {}))
        spectral_dict = {key: value for (key, value) in zip(self.resids, model) if key in G.nodes()}
        nx.set_node_attributes(G, 'spectral', spectral_dict)
        self.graph = G
开发者ID:alejandrovr,项目名称:htmd,代码行数:45,代码来源:mutualinformation.py

示例14: corr_to_graph

def corr_to_graph(roi_corrs, copy_corrs=False):
    """
    >>> import pandas as pd
    >>> import numpy as np
    >>> corrs = pd.DataFrame(np.random.rand(2,2))
    >>> corrs.index = ['A', 'B']
    >>> corrs.columns = ['A', 'B']
    >>> graph = corr_to_graph(corrs)
    >>> ab = graph['A']['B']
    >>> wt, prox, dist = ab['weight'], ab['proximity'], ab['distance']
    >>> assert wt == corrs['B']['A'] #upper triangular
    >>> assert prox == wt
    >>> assert dist == 1 - wt
    >>> assert len(graph) == 2
    """
    roi_corrs = create_convertible_corr_df(roi_corrs, copy_corrs)
    return nx.from_pandas_dataframe(roi_corrs, 'source', 'target',
                                    edge_attr=['distance', 'proximity', 'weight'])
开发者ID:dgclark,项目名称:easton_connectivity,代码行数:18,代码来源:verbal_connectivity.py

示例15: test_from_datafram

 def test_from_datafram(self, ):
     # Pandas DataFrame
     g = nx.cycle_graph(10)
     G = nx.Graph()
     G.add_nodes_from(g)
     G.add_weighted_edges_from((u, v, u) for u, v in g.edges())
     edgelist = nx.to_edgelist(G)
     source = [s for s, t, d in edgelist]
     target = [t for s, t, d in edgelist]
     weight = [d['weight'] for s, t, d in edgelist]
     import pandas as pd
     edges = pd.DataFrame({'source': source,
                           'target': target,
                           'weight': weight})
     GG = nx.from_pandas_dataframe(edges, edge_attr='weight')
     assert_nodes_equal(sorted(G.nodes()), sorted(GG.nodes()))
     assert_edges_equal(sorted(G.edges()), sorted(GG.edges()))
     GW = nx.to_networkx_graph(edges, create_using=nx.Graph())
     assert_nodes_equal(sorted(G.nodes()), sorted(GW.nodes()))
     assert_edges_equal(sorted(G.edges()), sorted(GW.edges()))
开发者ID:jklaise,项目名称:networkx,代码行数:20,代码来源:test_convert_pandas.py


注:本文中的networkx.from_pandas_dataframe函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。