-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathestimate_performance.py
100 lines (69 loc) · 2.41 KB
/
estimate_performance.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
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
# Copyright © 2022 Alexander L. Hayes
"""
``estimate_performance.py``
===========================
Performance 5-fold cross validation for predicting whether
a keypoint in a building is a "high point" or a "low point."
"""
from time import time
import os
from roofworld.exporter import Network
from gui import FlatWorld
from srlearn.rdn import BoostedRDNClassifier
from srlearn import Background
from relational_datasets.types import RelationalDataset
import numpy as np
from sklearn.model_selection import KFold
from sklearn.metrics import classification_report
from sklearn.metrics import f1_score
buildings = np.array([file for file in os.listdir("saved")])
bk = Background(
modes=[
"highpoint(+node).",
"connected(+node,-node).",
"connected(-node,+node).",
"nneighbors(+node,#neighborcount).",
"angledegrees(+node,+node,-node,#angletype).",
"angledegrees(+node,-node,+node,#angletype).",
"angledegrees(+node,-node,-node,#angletype).",
],
)
kf = KFold(n_splits=5, shuffle=True)
f1_scores = []
for i, (train_id, test_id) in enumerate(kf.split(buildings)):
start_id = 0
train_pos, train_neg, train_facts = [], [], []
test_pos, test_neg, test_facts = [], [], []
for filename in buildings[train_id]:
fw = FlatWorld.from_json("saved/" + filename)
net = Network(fw.points, fw.lines, start=start_id)
data = net.describe()
start_id += len(fw.points)
train_pos += data.pos
train_neg += data.neg
train_facts += data.facts
for filename in buildings[test_id]:
fw = FlatWorld.from_json("saved/" + filename)
net = Network(fw.points, fw.lines, start=start_id)
data = net.describe()
start_id += len(fw.points)
test_pos += data.pos
test_neg += data.neg
test_facts += data.facts
train_db = RelationalDataset(train_pos, train_neg, train_facts)
test_db = RelationalDataset(test_pos, test_neg, test_facts)
rdn = BoostedRDNClassifier(
background=bk,
target="highpoint",
solver="SRLBoost",
)
ts = time()
rdn.fit(train_db)
te = time()
print(f"Finished in {te - ts} seconds")
rdn.to_json(f"highpoint_rdn_fold_{i}.json")
y_pred = rdn.predict(test_db)
y_true = rdn.classes_
print(classification_report(y_true, y_pred))
f1_scores.append(f1_score(y_true, y_pred))
print(np.mean(f1_scores))