Skip to content

Commit 4111a4b

Browse files
authored
Changed "From/To" to "Source/Target" to be consistent with Gephi and networkx
1 parent aaec929 commit 4111a4b

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

metaknowledge/graphHelpers.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
import metaknowledge
1010

11-
def readGraph(edgeList, nodeList = None, directed = False, idKey = 'ID', eSource = 'From', eDest = 'To'):
11+
def readGraph(edgeList, nodeList = None, directed = False, idKey = 'ID', eSource = 'Source', eDest = 'Target'):
1212
"""Reads the files given by _edgeList_ and _nodeList_ and creates a networkx graph for the files.
1313
1414
This is designed only for the files produced by metaknowledge and is meant to be the reverse of [writeGraph()](#metaknowledge.graphHelpers.writeGraph), if this does not produce the desired results the networkx builtin [networkx.read_edgelist()](https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.readwrite.edgelist.read_edgelist.html) could be tried as it is aimed at a more general usage.
1515
16-
The read edge list format assumes the column named _eSource_ (default `'From'`) is the source node, then the column _eDest_ (default `'To'`) givens the destination and all other columns are attributes of the edges, e.g. weight.
16+
The read edge list format assumes the column named _eSource_ (default `'Source'`) is the source node, then the column _eDest_ (default `'Target'`) givens the destination and all other columns are attributes of the edges, e.g. weight.
1717
1818
The read node list format assumes the column _idKey_ (default `'ID'`) is the ID of the node for the edge list and the resulting network. All other columns are considered attributes of the node, e.g. count.
1919
@@ -41,11 +41,11 @@ def readGraph(edgeList, nodeList = None, directed = False, idKey = 'ID', eSource
4141
4242
_eSource_ : `optional [str]`
4343
44-
> default `'From'`, the name of the source column in the edge list
44+
> default `'Source'`, the name of the source column in the edge list
4545
4646
_eDest_ : `optional [str]`
4747
48-
> default `'To'`, the name of the destination column in the edge list
48+
> default `'Target'`, the name of the destination column in the edge list
4949
5050
# Returns
5151
@@ -100,7 +100,7 @@ def writeGraph(grph, name, edgeInfo = True, typing = False, suffix = 'csv', over
100100
101101
>> name_fileType.suffix
102102
103-
Both files are csv's with comma delimiters and double quote quoting characters. The edge list has two columns for the source and destination of the edge, `'From'` and `'To'` respectively, then, if _edgeInfo_ is `True`, for each attribute of the node another column is created. The node list has one column call "ID" with the node ids used by networkx and all other columns are the node attributes.
103+
Both files are csv's with comma delimiters and double quote quoting characters. The edge list has two columns for the source and destination of the edge, `'Source'` and `'Target'` respectively, then, if _edgeInfo_ is `True`, for each attribute of the node another column is created. The node list has one column call "ID" with the node ids used by networkx and all other columns are the node attributes.
104104
105105
To read back these files use [readGraph()](#metaknowledge.graphHelpers.readGraph) and to write only one type of lsit use [writeEdgeList()](#metaknowledge.graphHelpers.writeEdgeList) or [writeNodeAttributeFile()](#metaknowledge.graphHelpers.writeNodeAttributeFile).
106106
@@ -172,7 +172,7 @@ def writeGraph(grph, name, edgeInfo = True, typing = False, suffix = 'csv', over
172172
def writeEdgeList(grph, name, extraInfo = True, allSameAttribute = False, _progBar = None):
173173
"""Writes an edge list of _grph_ at the destination _name_.
174174
175-
The edge list has two columns for the source and destination of the edge, `'From'` and `'To'` respectively, then, if _edgeInfo_ is `True`, for each attribute of the node another column is created.
175+
The edge list has two columns for the source and destination of the edge, `'Source'` and `'Target'` respectively, then, if _edgeInfo_ is `True`, for each attribute of the node another column is created.
176176
177177
**Note**: If any edges are missing an attribute it will be left blank by default, enable _allSameAttribute_ to cause a `KeyError` to be raised.
178178
@@ -206,14 +206,14 @@ def writeEdgeList(grph, name, extraInfo = True, allSameAttribute = False, _progB
206206
PBar = _ProgressBar(0, "Writing edge list {}".format(name), dummy = True)
207207
if len(grph.edges(data = True)) < 1:
208208
outFile = open(os.path.expanduser(os.path.abspath(name)), 'w')
209-
outFile.write('"From","To"\n')
209+
outFile.write('"Source","Target"\n')
210210
outFile.close()
211211
PBar.updateVal(1, "Done edge list '{}', 0 edges written.".format(name))
212212
else:
213213
if extraInfo:
214214
csvHeader = []
215215
if allSameAttribute:
216-
csvHeader = ['From'] + ['To'] + list(grph.edges(data = True).__next__()[2].keys())
216+
csvHeader = ['Source'] + ['Target'] + list(grph.edges(data = True).__next__()[2].keys())
217217
else:
218218
extraAttribs = set()
219219
for eTuple in grph.edges(data = True):
@@ -224,9 +224,9 @@ def writeEdgeList(grph, name, extraInfo = True, allSameAttribute = False, _progB
224224
if len(s) > 0:
225225
for i in s:
226226
extraAttribs.add(i)
227-
csvHeader = ['From', 'To'] + list(extraAttribs)
227+
csvHeader = ['Source', 'Target'] + list(extraAttribs)
228228
else:
229-
csvHeader = ['From'] + ['To']
229+
csvHeader = ['Source'] + ['Target']
230230
count = 0
231231
PBar.updateVal(.01, "Opening file {}".format(name))
232232
f = open(os.path.expanduser(os.path.abspath(name)), 'w', newline = '')
@@ -238,8 +238,8 @@ def writeEdgeList(grph, name, extraInfo = True, allSameAttribute = False, _progB
238238
if count % 1000 == 0:
239239
PBar.updateVal(count / eMax * .90 + .10, "Writing edge: '{}' to '{}'".format(e[0], e[1]))
240240
eDict = e[2].copy()
241-
eDict['From'] = e[0]
242-
eDict['To'] = e[1]
241+
eDict['Source'] = e[0]
242+
eDict['Target'] = e[1]
243243
try:
244244
outFile.writerow(eDict)
245245
except UnicodeEncodeError:
@@ -253,8 +253,8 @@ def writeEdgeList(grph, name, extraInfo = True, allSameAttribute = False, _progB
253253
count += 1
254254
if count % 1000 == 0:
255255
PBar.updateVal(count / eMax * .90 + .10, "Writing edge: '{}' to '{}'".format(e[0], e[1]))
256-
eDict['From'] = e[0]
257-
eDict['To'] = e[1]
256+
eDict['Source'] = e[0]
257+
eDict['Target'] = e[1]
258258
try:
259259
outFile.writerow(eDict)
260260
except UnicodeEncodeError:

0 commit comments

Comments
 (0)