-
Notifications
You must be signed in to change notification settings - Fork 5
/
import.go
315 lines (292 loc) · 8.68 KB
/
import.go
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
309
310
311
312
313
314
315
package ch
import (
"encoding/csv"
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/pkg/errors"
)
var (
ErrNotEnoughColumns = fmt.Errorf("not enough columns")
ErrColumnNotFound = fmt.Errorf("column not found")
)
// CSVHeaderImportEdges is just an helper structure to evaluate CSV columns for edges file
type CSVHeaderImportEdges struct {
SourceExternal int
TargetExternal int
Weight int
}
// CSVHeaderImportVertices is just an helper structure to evaluate CSV columns for vertices file
type CSVHeaderImportVertices struct {
ID int
OrderPos int
Importance int
}
// CSVHeaderImportShortcuts is just an helper structure to evaluate CSV columns for shortcuts file
type CSVHeaderImportShortcuts struct {
SourceExternal int
TargetExternal int
ViaExternal int
Weight int
}
// ImportFromFile Imports graph (prepared by ExportToFile(fname string) function) from file of CSV-format
// Header of CSV-file containing information about edges:
// from_vertex_id - int64, ID of source vertex
// to_vertex_id - int64, ID of arget vertex
// weight - float64, Weight of an edge
// Header of CSV-file containing information about vertices:
// vertex_id - int64, ID of vertex
// order_pos - int, Position of vertex in hierarchies (evaluted by library)
// importance - int, Importance of vertex in graph (evaluted by library)
// Header of CSV-file containing information about shortcuts between vertices:
// from_vertex_id - int64, ID of source vertex
// to_vertex_id - int64, ID of target vertex
// weight - float64, Weight of an shortcut
// via_vertex_id - int64, ID of vertex through which the shortcut exists
func ImportFromFile(edgesFname, verticesFname, contractionsFname string) (*Graph, error) {
// Read edges first
file, err := os.Open(edgesFname)
if err != nil {
return nil, err
}
defer file.Close()
reader := csv.NewReader(file)
reader.Comma = ';'
graph := Graph{}
// Fill graph with edges informations
// Skip header of CSV-file
edgesHeader, err := reader.Read()
if err != nil {
return nil, err
}
edgesColumns, err := prepareEdgesColumns(edgesHeader)
if err != nil {
return nil, err
}
// Read file line by line
for {
record, err := reader.Read()
if err == io.EOF {
break
}
sourceExternal, err := strconv.ParseInt(record[edgesColumns.SourceExternal], 10, 64)
if err != nil {
return nil, err
}
targetExternal, err := strconv.ParseInt(record[edgesColumns.TargetExternal], 10, 64)
if err != nil {
return nil, err
}
weight, err := strconv.ParseFloat(record[edgesColumns.Weight], 64)
if err != nil {
return nil, err
}
err = graph.CreateVertex(sourceExternal)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("Can't add source vertex with external_ID = '%d'", sourceExternal))
}
err = graph.CreateVertex(targetExternal)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("Can't add target vertex with external_ID = '%d'", targetExternal))
}
err = graph.AddEdge(sourceExternal, targetExternal, weight)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("Can't add edge with source_internal_ID = '%d' and target_internal_ID = '%d'", sourceExternal, targetExternal))
}
}
// Read vertices
fileVertices, err := os.Open(verticesFname)
if err != nil {
return nil, err
}
defer fileVertices.Close()
readerVertices := csv.NewReader(fileVertices)
readerVertices.Comma = ';'
// Skip header of CSV-file
verticesHeader, err := readerVertices.Read()
if err != nil {
return nil, err
}
verticesColumns, err := prepareVerticesColumns(verticesHeader)
if err != nil {
return nil, err
}
// Read file line by line
for {
record, err := readerVertices.Read()
if err == io.EOF {
break
}
vertexExternal, err := strconv.ParseInt(record[verticesColumns.ID], 10, 64)
if err != nil {
return nil, err
}
vertexOrderPos, err := strconv.ParseInt(record[verticesColumns.OrderPos], 10, 64)
if err != nil {
return nil, err
}
vertexImportance, err := strconv.Atoi(record[verticesColumns.Importance])
if err != nil {
return nil, err
}
vertexInternal, vertexFound := graph.FindVertex(vertexExternal)
if !vertexFound {
return nil, fmt.Errorf("Vertex with Label = %d is not found in graph", vertexExternal)
}
graph.Vertices[vertexInternal].SetOrderPos(vertexOrderPos)
graph.Vertices[vertexInternal].SetImportance(vertexImportance)
}
// Read contractions
fileShortcuts, err := os.Open(contractionsFname)
if err != nil {
return nil, err
}
defer fileShortcuts.Close()
readerShortcuts := csv.NewReader(fileShortcuts)
readerShortcuts.Comma = ';'
// Process header of CSV-file
shortcutsHeader, err := readerShortcuts.Read()
if err != nil {
return nil, err
}
shortcutsColumns, err := prepareShortcutsColumns(shortcutsHeader)
if err != nil {
return nil, err
}
// Read file line by line
for {
record, err := readerShortcuts.Read()
if err == io.EOF {
break
}
sourceExternal, err := strconv.ParseInt(record[shortcutsColumns.SourceExternal], 10, 64)
if err != nil {
return nil, err
}
targetExternal, err := strconv.ParseInt(record[shortcutsColumns.TargetExternal], 10, 64)
if err != nil {
return nil, err
}
weight, err := strconv.ParseFloat(record[shortcutsColumns.Weight], 64)
if err != nil {
return nil, err
}
contractionExternal, err := strconv.ParseInt(record[shortcutsColumns.ViaExternal], 10, 64)
if err != nil {
return nil, err
}
err = graph.AddEdge(sourceExternal, targetExternal, weight)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("Can't add shortcut with source_internal_ID = '%d' and target_internal_ID = '%d'", sourceExternal, targetExternal))
}
err = graph.AddShortcut(sourceExternal, targetExternal, contractionExternal, weight)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("Can't add shortcut with source_internal_ID = '%d' and target_internal_ID = '%d' to internal map", sourceExternal, targetExternal))
}
}
return &graph, nil
}
func prepareEdgesColumns(edgesHeader []string) (CSVHeaderImportEdges, error) {
ans := CSVHeaderImportEdges{
SourceExternal: -1,
TargetExternal: -1,
Weight: -1,
}
if len(edgesHeader) < 3 {
return ans, errors.Wrapf(ErrNotEnoughColumns, "Minimum 3 columns are needed. Provided: %d", len(edgesHeader))
}
for i, header := range edgesHeader {
switch strings.ToLower(header) {
case "from_vertex_id":
ans.SourceExternal = i
case "to_vertex_id":
ans.TargetExternal = i
case "weight":
ans.Weight = i
default:
// Nothing
}
}
if ans.SourceExternal < 0 {
return ans, errors.Wrap(ErrColumnNotFound, "Not found: 'from_vertex_id'")
}
if ans.TargetExternal < 0 {
return ans, errors.Wrap(ErrColumnNotFound, "Not found: 'to_vertex_id'")
}
if ans.Weight < 0 {
return ans, errors.Wrap(ErrColumnNotFound, "Not found: 'weight'")
}
return ans, nil
}
func prepareVerticesColumns(verticesHeader []string) (CSVHeaderImportVertices, error) {
ans := CSVHeaderImportVertices{
ID: -1,
OrderPos: -1,
Importance: -1,
}
if len(verticesHeader) < 3 {
return ans, errors.Wrapf(ErrNotEnoughColumns, "Minimum 3 columns are needed. Provided: %d", len(verticesHeader))
}
for i, header := range verticesHeader {
switch strings.ToLower(header) {
case "vertex_id":
ans.ID = i
case "order_pos":
ans.OrderPos = i
case "importance":
ans.Importance = i
default:
// Nothing
}
}
if ans.ID < 0 {
return ans, errors.Wrap(ErrColumnNotFound, "Not found: 'vertex_id'")
}
if ans.OrderPos < 0 {
return ans, errors.Wrap(ErrColumnNotFound, "Not found: 'order_pos'")
}
if ans.Importance < 0 {
return ans, errors.Wrap(ErrColumnNotFound, "Not found: 'importance'")
}
return ans, nil
}
func prepareShortcutsColumns(verticesHeader []string) (CSVHeaderImportShortcuts, error) {
ans := CSVHeaderImportShortcuts{
SourceExternal: -1,
TargetExternal: -1,
ViaExternal: -1,
Weight: -1,
}
if len(verticesHeader) < 4 {
return ans, errors.Wrapf(ErrNotEnoughColumns, "Minimum 4 columns are needed. Provided: %d", len(verticesHeader))
}
for i, header := range verticesHeader {
switch strings.ToLower(header) {
case "from_vertex_id":
ans.SourceExternal = i
case "to_vertex_id":
ans.TargetExternal = i
case "via_vertex_id":
ans.ViaExternal = i
case "weight":
ans.Weight = i
default:
// Nothing
}
}
if ans.SourceExternal < 0 {
return ans, errors.Wrap(ErrColumnNotFound, "Not found: 'from_vertex_id'")
}
if ans.TargetExternal < 0 {
return ans, errors.Wrap(ErrColumnNotFound, "Not found: 'to_vertex_id'")
}
if ans.ViaExternal < 0 {
return ans, errors.Wrap(ErrColumnNotFound, "Not found: 'via_vertex_id'")
}
if ans.Weight < 0 {
return ans, errors.Wrap(ErrColumnNotFound, "Not found: 'weight'")
}
return ans, nil
}