This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsgml.py
61 lines (49 loc) · 1.87 KB
/
parsgml.py
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
from pygmlparser.Parser import Parser
from pathlib import Path
from random import randint
from functools import total_ordering
@total_ordering
class Topology:
def __init__(self, path, nodes, edges):
self.path = path
self.nodes = nodes
self.edges = edges
self.matrix = [[0] * len(nodes) for _ in range(len(nodes))]
self.density = 2 * len(edges) / (len(nodes) * len(nodes))
def random_edges_weights(self, a=10, b=50):
for edge in self.edges:
self.matrix[edge.source][edge.target] = self.matrix[edge.target][
edge.source
] = randint(a, b)
def __eq__(self, other):
return self.density == other.density
def __lt__(self, other):
return (self.density, len(self.nodes)) < (other.density, len(self.nodes))
def __str__(self):
return f"PATH: {self.path}\nNODES: {len(self.nodes)}\nLINKS: {len(self.edges)}\nDENSITY: {self.density}\n"
class GmlManager:
def __init__(self, dpath="./GML_files", flog="density.txt"):
self.dpath = dpath
self.flog = flog
self.topologies = []
def parse(self):
for PATH in Path(self.dpath).glob("*.gml"):
parser = Parser()
parser.loadGML(path=PATH)
parser.parse()
self.topologies.append(
Topology(PATH, parser.graph.graphNodes, parser.graph.graphEdges)
)
self.topologies.sort()
def log_to_file(self):
with open(self.flog, "w") as file:
for topo in sorted(self.topologies):
file.write(
f"PATH={topo.path}, nodes={len(topo.nodes)}, density={topo.density}\n"
)
def find_topo_by_density(self, a=0, b=1):
res = []
for topo in self.topologies:
if a <= topo.density <= b:
res.append(topo)
return res