-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvisualization.py
54 lines (46 loc) · 1.99 KB
/
visualization.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
from .abstract import AbstractBHV, DIMENSION
class DistanceGraph:
@classmethod
def from_scope(cls, local_dict):
return cls(*zip(*[(v, k) for k, v in local_dict.items() if isinstance(v, AbstractBHV)]))
def __init__(self, hvs: 'list[AbstractBHV]', labels: 'list[str]'):
self.hvs = hvs
self.labels = labels
self.adj = [[v.std_relation(w, compact=True) if v.related(w) else None
for v in self.hvs]
for w in self.hvs]
def graphviz(self):
for i, (r, l) in enumerate(zip(self.adj, self.labels)):
print(f"{i} [label=\"{l}\"];")
for j, d in enumerate(r):
if i < j and d is not None:
print(f"{i} -- {j} [label=\"{d}\"];")
class Image:
@classmethod
def load_pbm(cls, file: 'IO[Any]', bhv: 'AbstractBHV', binary=False):
if binary:
header = file.readline().rstrip()
assert header == b"P4"
dimension, n = map(int, file.readline().rstrip().split(b" "))
assert dimension == DIMENSION
hvs = [bhv.from_bytes(file.read(DIMENSION//8)) for _ in range(n)]
return cls(hvs)
else:
header = file.readline().rstrip()
assert header == "P1"
dimension, n = map(int, file.readline().rstrip().split(" "))
assert dimension == DIMENSION
hvs = [bhv.from_bitstring(file.readline().rstrip()) for _ in range(n)]
return cls(hvs)
def __init__(self, hvs: 'list[AbstractBHV]'):
self.hvs = hvs
def pbm(self, file: 'IO[Any]', binary=False):
if binary:
file.write(b"P4\n" + bytes(str(DIMENSION), "ascii") + b" " + bytes(str(len(self.hvs)), "ascii") + b"\n")
for hv in self.hvs:
file.write(hv.to_bytes())
else:
file.write(f"P1\n{DIMENSION} {len(self.hvs)}\n")
for hv in self.hvs:
file.write(hv.bitstring())
file.write('\n')