-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathgraph_data.py
More file actions
308 lines (271 loc) · 11.9 KB
/
graph_data.py
File metadata and controls
308 lines (271 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
"""Contains class for identifying, reading, and loading graph data."""
import csv
from typing import Dict, List, Optional, Union, cast
import networkx as nx
from . import data_utils
from .base_data import BaseData
from .csv_data import CSVData
from .filepath_or_buffer import FileOrBufferHandler
class GraphData(BaseData):
"""GraphData class to identify, read, and load graph data."""
data_type: str = "graph"
def __init__(
self,
input_file_path: Optional[str] = None,
data: Optional[nx.Graph] = None,
options: Optional[Dict] = None,
) -> None:
"""
Initialize Data class for identifying, reading, and loading graph data.
Current implementation only accepts file path as input.
An options parameter is also passed in to specify properties of the
input file.
Possible Options::
options = dict(
delimiter= type: str
column_names= type: list(str)
source_node= type: int
destination_node= type: int
target_keywords= type: list(str)
source_keywords= type: list(str)
header= type: any
quotechar= type: str
)
delimiter: delimiter used to decipher the csv input file
column_names: list of column names of the csv
source_node: index of the source node column, range of (0,n-1)
target_node: index of the target node column, range of (0,n-1)
target_keywords: list of keywords to identify target/destination node col
source_keywords: list of keywords to identify source node col
graph_keywords: list of keywords to identify if data has graph data
header: location o the header in the file
quotechar: quote character used in the delimited file
:param input_file_path: path to the file being loaded or None
:type input_file_path: str
:param data: data being loaded into the class instead of an input file
:type data: multiple types
:param options: options pertaining to the data type
:type options: dict
:return: None
"""
options = self._check_and_return_options(options)
BaseData.__init__(self, input_file_path, data, options)
self._source_node: Optional[int] = options.get("source_node", None)
self._destination_node: Optional[int] = options.get("destination_node", None)
self._target_keywords: List[str] = options.get(
"target_keywords", ["target", "destination", "dst"]
)
self._source_keywords: List[str] = options.get(
"source_keywords", ["source", "src", "origin"]
)
self._graph_keywords: List[str] = options.get("graph_keywords", ["node"])
self._column_names: Optional[List[str]] = options.get("column_names", None)
self._delimiter: Optional[str] = options.get("delimiter", None)
self._quotechar: Optional[str] = options.get("quotechar", None)
self._header: Optional[Union[str, int]] = options.get("header", "auto")
self._checked_header: bool = "header" in options and self._header != "auto"
if data is not None:
self._load_data(data)
@classmethod
def _find_target_string_in_column(
self, column_names: List[str], keyword_list: List[str]
) -> int:
"""Find out if col name contains keyword that could refer to target node col."""
column_name_symbols = ["_", ".", "-"]
has_target = False
target_index = -1
# iterate through columns, keywords, and delimiter name symbols to see
# if any permutation is contained in column names
for column in range(0, len(column_names)):
for keyword in keyword_list:
for symbol in column_name_symbols:
append_start_word = symbol + keyword
append_end_word = keyword + symbol
if (
append_start_word in column_names[column]
or append_end_word in column_names[column]
):
target_index = column
has_target = True
break
if has_target:
break
return target_index
@classmethod
def csv_column_names(
cls,
file_path: str,
header: Optional[int],
delimiter: Optional[str],
encoding: str = "utf-8",
) -> List[str]:
"""Fetch a list of column names from the csv file."""
column_names: List[str] = []
if delimiter is None:
delimiter = ","
if header is None:
return column_names
with FileOrBufferHandler(file_path, encoding=encoding) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=delimiter)
# fetch only column names
row_count = 0
for row in csv_reader:
if row_count is header:
column_names = row
break
row_count += 1
# replace all whitespaces in the column names
for index in range(0, len(column_names)):
column_names[index] = column_names[index].replace(" ", "")
return column_names
@classmethod
def is_match(cls, file_path: str, options: Optional[Dict] = None) -> bool:
"""
Determine whether the file is a graph.
Current formats checked:
- attributed edge list
This works by finding whether the file contains a target and a source node
"""
if options is None:
options = dict()
if not CSVData.is_match(file_path, options):
return False
header: int = options.get("header", 0)
delimiter: str = options.get("delimiter", ",")
encoding: str = options.get("encoding", "utf-8")
column_names: List[str] = cls.csv_column_names(
file_path, header, delimiter, encoding
)
source_keywords: List[str] = options.get(
"source_keywords", ["source", "src", "origin"]
)
target_keywords: List[str] = options.get(
"target_keywords", ["target", "destination", "dst"]
)
graph_keywords: List[str] = options.get("graph_keywords", ["node"])
source_index: int = cls._find_target_string_in_column(
column_names, source_keywords
)
destination_index: int = cls._find_target_string_in_column(
column_names, target_keywords
)
graph_index: int = cls._find_target_string_in_column(
column_names, graph_keywords
)
has_source: bool = True if source_index >= 0 else False
has_target: bool = True if destination_index >= 0 else False
has_graph_data: bool = True if graph_index >= 0 else False
if has_target and has_source and has_graph_data:
options.update(source_node=source_index)
options.update(destination_node=destination_index)
options.update(destination_list=target_keywords)
options.update(source_list=source_keywords)
options.update(column_names=column_names)
return True
return False
def _format_data_networkx(self) -> nx.Graph:
"""Format the input file into a networkX graph."""
networkx_graph = nx.Graph()
assert self.input_file_path is not None
# read lines from csv
if not self._checked_header or not self._delimiter:
delimiter, quotechar = None, None
data_as_str = data_utils.load_as_str_from_file(
self.input_file_path, self.file_encoding
)
if not self._delimiter or not self._quotechar:
delimiter, quotechar = CSVData._guess_delimiter_and_quotechar(
data_as_str
)
if not self._delimiter:
self._delimiter = delimiter
if not self._quotechar:
self._quotechar = quotechar
if self._header == "auto":
self._header = CSVData._guess_header_row(
data_as_str, self._delimiter, self._quotechar
)
self._checked_header = True
# if there is only one delimiter at the end of each row,
# set delimiter to None
if self._delimiter:
if len(data_as_str) > 0:
num_lines_read = 0
count_delimiter_last = 0
for line in data_as_str.split("\n"):
if len(line) > 0:
if (
line.count(self._delimiter) == 1
and line.strip()[-1] == self._delimiter
):
count_delimiter_last += 1
num_lines_read += 1
if count_delimiter_last == num_lines_read:
self._delimiter = None
if (
self._column_names is None
and isinstance(self._header, int)
and self.file_encoding is not None
):
self._column_names = self.csv_column_names(
self.input_file_path, self._header, self._delimiter, self.file_encoding
)
if self._source_node is None and self._column_names is not None:
self._source_node = self._find_target_string_in_column(
self._column_names, self._source_keywords
)
if self._destination_node is None and self._column_names is not None:
self._destination_node = self._find_target_string_in_column(
self._column_names, self._target_keywords
)
data_as_pd = data_utils.read_csv_df(
self.input_file_path,
self._delimiter,
cast(Optional[int], self._header),
selected_columns=[],
read_in_string=True,
encoding=self.file_encoding,
)
data_as_pd = data_as_pd.apply(lambda x: x.str.strip())
csv_as_list = data_as_pd.values.tolist()
# grab list of edges from source/dest nodes
for line_index in range(0, len(csv_as_list)):
# fetch attributes in columns
attributes = dict()
for column in range(0, len(csv_as_list[0])):
if csv_as_list[line_index][column] is None:
continue
if (
column is not self._source_node
or column is not self._destination_node
) and self._column_names is not None:
attributes[self._column_names[column]] = float(
csv_as_list[line_index][column]
)
elif column is self._source_node or column is self._destination_node:
networkx_graph.add_node(
self.check_integer(csv_as_list[line_index][column])
)
networkx_graph.add_edge(
self.check_integer(csv_as_list[line_index][self._source_node]),
self.check_integer(csv_as_list[line_index][self._destination_node]),
**attributes
)
# get NetworkX object from list
return networkx_graph
def _load_data(self, data: Optional[nx.Graph] = None) -> nx.Graph:
if data is not None:
if not isinstance(data, nx.Graph):
raise ValueError("Only NetworkX Graph objects allowed as input data.")
self._data = data
else:
self._data = self._format_data_networkx()
def check_integer(self, string: str) -> Union[int, str]:
"""Check whether string is integer and output integer."""
stringVal = string
if string[0] == ("-", "+"):
stringVal = string[1:]
if stringVal.isdigit():
return int(string)
else:
return string