-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathballtree.py
1405 lines (1098 loc) · 55.9 KB
/
balltree.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
""" BallTreeRegions class to store the BallTree and the resulting regions in input space. """
import os
import typing
from dataclasses import dataclass, field
import graphviz
import networkx as nx
from networkx.drawing.nx_agraph import write_dot
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from prettytable import PrettyTable
import pygraphviz as pgv
from sklearn.neighbors import BallTree
import create_plots
import distance_measures
import distance_stat_wrapper
# import kd_tree_partioning
def print_tree_data_and_structure(tree: BallTree, ):
""" print tree data and structure of Sklearn BallTree object."""
tree_arrays = tree.get_arrays()
print('BallTree arrays:')
print('tree data:')
print(tree_arrays[0])
print('tree index data:')
print(tree_arrays[1])
print('tree node data and bounds:')
print(tree_arrays[2])
print('data structure of tree_arrays[2]')
print("dtype=[('idx_start', ' < i8'), ('idx_end', ' < i8'), ('is_leaf', ' < i8'), ('radius', ' < f8')])")
@dataclass
class BallTreeRegions:
""" Class to store the BallTree and the resulting regions in input space. """
# init empty tree and empty tree structure
tree: BallTree = None
tree_idx_data: np.ndarray = None # indices of the data points in the tree (reordered campared to input data)
# start index (from reordered tree_idx_data) of the data points in the ball
idx_in_ball_start: np.ndarray = None # dimension n_balls x 1,
# end index of the data points in the ball
idx_in_ball_end: np.ndarray = None # dimension n_balls x 1,
# is leaf node or not (1 if leaf, 0 if not)
ball_is_leaf: np.ndarray = None # dimension n_balls x 1
# radius of the balls
radius_array: np.ndarray = None # dimension n_balls x 1
# center points of the balls are the mean of the data points in the ball
center_points: np.ndarray = None # dimensions n_balls x n_features
# statistics per region (e.g. ANEES, MSE, ECE)
stat_per_region: np.ndarray = None # dimension n_balls x 1
combined_stats_per_leaf: np.ndarray = None # dimension n_leafs x 1
def __init__(self, input_data: np.ndarray, leaf_size: int, ) -> None:
# store input data and leaf size
self.input_data = input_data
self.leaf_size = leaf_size
# build tree
self._build_tree()
self._extract_tree_structure()
def _build_tree(self, ):
""" build tree from input data using sklearn BallTree."""
self.tree = BallTree(self.input_data, leaf_size=self.leaf_size)
def _extract_tree_structure(self, ):
(_, self.tree_idx_data, self.idx_in_ball_start, self.idx_in_ball_end,
self.ball_is_leaf, self.radius_array, self.center_points) = extract_nodes(
self.tree)
def print_tree_data_and_structure(self,):
""" print tree data and structure."""
print_tree_data_and_structure(self.tree)
def plot_2d_ball_tree_regions(self, ax: plt.Axes = None,
show_stats: bool = False,
plot_parents: bool = False,
plot_circles: bool = False,
point_size: float = 4,
cmap=None, norm=None) -> plt.Axes:
""" plot 2D ball tree regions."""
if show_stats and self.stat_per_region is None:
raise ValueError('No statistics per region available. Please calculate statistics per region first.')
if show_stats:
statistics = self.stat_per_region
else:
statistics = None
ax = plot_2d_ball_tree_regions(self.input_data, self.tree_idx_data, self.idx_in_ball_start,
self.idx_in_ball_end,
self.ball_is_leaf, self.radius_array, self.center_points,
ax=ax,
plot_parents=plot_parents,
plot_circles=plot_circles,
point_size=point_size,
statisics=statistics, cmap=cmap, norm=norm)
return ax
def get_idx_in_leaf_balls(self, ) -> typing.List:
""" get the indices per leaf ball."""
idx_per_ball = []
for idx, is_leaf in enumerate(self.ball_is_leaf):
if is_leaf:
idx_per_ball.append(self.tree_idx_data[self.idx_in_ball_start[idx]:self.idx_in_ball_end[idx]])
return idx_per_ball
def get_idx_per_ball(self, ) -> typing.List:
""" get the indices per ball. (not only leaf balls)"""
idx_per_ball = []
for idx in range(len(self.ball_is_leaf)):
idx_per_ball.append(self.tree_idx_data[self.idx_in_ball_start[idx]:self.idx_in_ball_end[idx]])
return idx_per_ball
def get_data_points_per_leaf_ball(self, ) -> typing.List:
""" get the data points per leaf ball."""
data_points_per_ball = []
for idx, is_leaf in enumerate(self.ball_is_leaf):
if is_leaf:
data_points_per_ball.append(
self.input_data[self.tree_idx_data[self.idx_in_ball_start[idx]:self.idx_in_ball_end[idx]]])
return data_points_per_ball
def get_data_points_per_ball(self, ) -> typing.List:
""" get the data points per ball. (not only leaf balls)"""
data_points_per_ball = []
for idx in range(len(self.ball_is_leaf)):
data_points_per_ball.append(
self.input_data[self.tree_idx_data[self.idx_in_ball_start[idx]:self.idx_in_ball_end[idx]]])
return data_points_per_ball
def set_stat_per_region(self, stat_per_region: np.ndarray):
""" set the statistics per region."""
self.stat_per_region = stat_per_region
# def set_combined_stats_per_leaf(self, combined_stats_per_leaf: np.ndarray):
def print_stat_per_region(self,):
""" print the region ID, the number of data points per region,
and the statistics per region in a pretty table."""
data_points_per_ball = self.get_data_points_per_leaf_ball()
if len(self.stat_per_region) != len(data_points_per_ball):
data_points_per_ball = self.get_data_points_per_ball()
# print as pretty table
table = PrettyTable()
table.field_names = ['region ID', '# Data Points', 'statistic']
for idx, stat in enumerate(self.stat_per_region):
table.add_row([idx, len(data_points_per_ball[idx]), stat])
print(table)
def extract_nodes(tree: BallTree, ):
""" extract the nodes of the tree.
:param tree: sklearn BallTree object
:type tree: BallTree
:return: tree_data, tree_idx_data, idx_in_ball_start, idx_in_ball_end, ball_is_leaf, radius_array, center_points
:rtype: np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray
"""
tree_arrays = tree.get_arrays()
tree_data = tree_arrays[0]
tree_idx_data = tree_arrays[1]
tree_node_data = tree_arrays[2]
idx_in_ball_start = tree_node_data['idx_start']
idx_in_ball_end = tree_node_data['idx_end']
ball_is_leaf = tree_node_data['is_leaf']
radius_array = tree_node_data['radius']
# center points of the balls are the mean of the data points in the ball
# dimensions n_balls x n_features
center_points = np.zeros((len(ball_is_leaf), tree_data.shape[1]))
for idx in range(len(ball_is_leaf)):
center_points[idx] = np.mean(tree_data[tree_idx_data[idx_in_ball_start[idx]:idx_in_ball_end[idx]]], axis=0)
return tree_data, tree_idx_data, idx_in_ball_start, idx_in_ball_end, ball_is_leaf, radius_array, center_points
@dataclass
class Node:
""" Node class which is used to recursively build the tree."""
start_idx: int # start index of the data points in the ball
end_idx: int # end index of the data points in the ball
is_leaf: bool # is leaf node or not (1 if leaf, 0 if not)
indices: typing.List[int] # indices of the data points in the ball
radius: float # radius of the ball
centroid: np.ndarray # center point of the ball
left: 'Node' # left child node (if not leaf node) # 'Node' is a forward reference
right: 'Node' # right child node (if not leaf node) # 'Node' is a forward reference
node_id: typing.ClassVar[int] = 0 # default node id
parent: 'Node' = None # parent node (if not root node) # 'Node' is a forward reference
stat: float = None # statistic per region (e.g. ANEES, MSE, ECE)
combined_stat: float = None # combined statistic per leaf node
def __post_init__(self):
""" Assign a unique node id by counting the number of instances of the class."""
Node.node_id += 1
self.node_id = Node.node_id
@dataclass
class CustomBallTree:
""" Custom BallTree class to save the ball tree structure.
The structure can be created from scratch or from a sklearn BallTree object.
Note that the kd tree construction is used to build the tree (in both cases).
"""
root: Node = None
leaf_size: int = 40
data_points: np.ndarray = None
split_node_attr: dict = field(default_factory=lambda: {'shape': 'box',
'newrank': 'true', # rank and newrank do not work properly
'rank': 'max',
'style': 'rounded,filled',
'width': '0.0',
'height': '0.5', # default 0.5 # min = 0.02
# 'margin': '0.0',
'color': 'black', # color of the border
'fillcolor': 'white', # color of the node
'fontsize': '1', # default 14
})
leaf_note_attr: dict = field(default_factory=lambda: {'shape': 'plaintext',
'rank': 'min',
'bgcolor': 'transparent',
# 'fillcolor': 'none',
'style': '',
'color': '',
# 'margin': '0',
'height': '0.2',
'width': '0.',
})
graph_attr: dict = field(default_factory=lambda: {'rankdir': 'TB',
'bgcolor': 'transparent',
'ranksep': '0.2', # default 0.5 # min = 0.02
'nodesep': '0.02', # default 0.25 min = 0.02
})
def rebuild_from_sklearn_tree(self, ball_tree: BallTree):
""" Rebuild the tree from a sklearn BallTree object.
:param ball_tree: sklearn BallTree object
:type ball_tree: BallTree
:return: None"""
tree_data, reordered_data_indices, tree_structure, _ = ball_tree.get_arrays()
self.data_points = tree_data
self.root = rebuild_tree(tree_structure, indices=reordered_data_indices, data=tree_data)
self._add_parent_references()
def build_tree(self, points, start_idx, end_idx, ) -> Node:
""" Build the tree recursively using the kd tree construction algorithm.
:param points: data points
:type points: np.ndarray with shape (n_data_points, n_features)
:param start_idx: start index of the data points in the ball
:type start_idx: int
:param end_idx: end index of the data points in the ball
:type end_idx: int
"""
# Base case: if not enough points, make a leaf node
if end_idx - start_idx <= 2 * self.leaf_size:
return Node(
start_idx=start_idx,
end_idx=end_idx,
is_leaf=True,
indices=points[start_idx:end_idx],
radius=calculate_radius(points[start_idx:end_idx]),
centroid=calculate_centroid(points[start_idx:end_idx]),
left=None,
right=None,)
# start_idx, end_idx, True,
# self.calculate_radius(points[start_idx:end_idx]))
# Choose the dimension with the greatest spread
# dimensions = points.shape[1]
spread = calculate_spread(points[start_idx:end_idx], use_variance=False)
split_dim = np.argmax(spread) # splitting dimension is the one with the greatest spread (as in kd tree)
# Split the points into two sets
indices = np.arange(start_idx, end_idx)
sorted_indices = indices[np.argsort(points[indices, split_dim])]
points[indices] = points[sorted_indices]
median_idx = (end_idx - start_idx) // 2
# Recurse on each subset
left_child = self.build_tree(points, start_idx, start_idx + median_idx)
right_child = self.build_tree(points, start_idx + median_idx, end_idx)
# Create a new internal node and return it
return Node(
start_idx=start_idx,
end_idx=end_idx,
is_leaf=False,
indices=points[start_idx:end_idx],
radius=calculate_radius(points[start_idx:end_idx]),
centroid=calculate_centroid(points[start_idx:end_idx]),
left=left_child,
right=right_child
)
def get_all_leaf_nodes(self, node: Node = None, leaf_nodes: list = None) -> typing.List[Node]:
""" Get all leaf nodes of the tree. """
if node is None:
node = self.root
leaf_nodes = []
if node.is_leaf:
leaf_nodes.append(node)
return leaf_nodes
leaf_nodes = self.get_all_leaf_nodes(node.left, leaf_nodes)
leaf_nodes = self.get_all_leaf_nodes(node.right, leaf_nodes)
return leaf_nodes
def get_all_nodes(self, node: Node = None, all_nodes: list = None) -> typing.List[Node]:
""" Get all nodes of the tree. """
if node is None:
node = self.root
all_nodes = []
all_nodes.append(node)
if node.is_leaf:
return all_nodes
all_nodes = self.get_all_nodes(node.left, all_nodes)
all_nodes = self.get_all_nodes(node.right, all_nodes)
return all_nodes
def get_stat_leaf_to_root_path(self, node: Node = None,
stat_path: typing.List[float] = None,
radius_path: typing.List[float] = None) -> typing.Tuple[typing.List, typing.List]:
""" Get the statistics per leaf to root path.
Start at the leaf node and go up to the root node."""
assert node is not None, 'Please provide a node to start the path.'
if node.is_leaf:
stat_path = [node.stat]
radius_path = [node.radius]
else:
stat_path.append(node.stat)
radius_path.append(node.radius)
if node.parent is None:
return stat_path, radius_path
self.get_stat_leaf_to_root_path(node.parent, stat_path, radius_path)
return stat_path, radius_path
def calc_combined_stat_per_leaf(self, weight_fnc: callable = None):
""" Calculate the combined statistics per leaf node. """
if weight_fnc is None:
def weight_fnc(radius: np.ndarray):
return 1/radius
leaf_nodes = self.get_all_leaf_nodes()
for leaf in leaf_nodes:
stat_path, radius_path = self.get_stat_leaf_to_root_path(leaf)
weights = weight_fnc(np.array(radius_path))
combined_stat = weights @ np.array(stat_path) * 1 / np.sum(weights)
leaf.combined_stat = combined_stat
def calc_stat_per_node(self, predictions: np.ndarray, output_data: np.ndarray,
method: str = 'anees', alpha: float = 0.01, m_bins: int = 1):
""" Calculate statistics per region. All regions are used. """
calc_stat_per_node(predictions, output_data, self.root, method=method, alpha=alpha, m_bins=m_bins)
def fit(self, data: np.ndarray) -> None:
""" Fit the tree to the data points.
:param data: data points
:type data: np.ndarray with shape (n_data_points, n_features)
:return: None
"""
self.data_points = data
self.root = self.build_tree(data, 0, len(data))
self._add_parent_references(self.root)
def get_structure_array(self):
""" Get the structure of the tree as an array (Same as sklearn BallTree.get_arrays()[2])"""
return self._get_structure_array(self.root)
def plot_2d_ball_tree_regions(self, ax: plt.Axes = None,
show_stats: bool = False, # show statistics per region
show_combined_stat: bool = False, # show combined statistics per leaf node
plot_parents: bool = False, # plot parent nodes
plot_circles: bool = False, # plot areas with filles circles
alpha_transparency_circles: float = 1, # transparency of the circles
plot_points: bool = False, # plot data points
edgecolor_points: str = 'k', # edge color of the data points
alpha_transparency_points: float = 1, # transparency of the data points
point_size: float = 4, # scatter point size of the data points
edgecolor_circles: str = 'k', # edge color of the circles
line_width_circles: float = 0.5, # line width of the circles
cmap=None, norm=None) -> plt.Axes:
""" plot 2D ball tree regions."""
# get leaf nodes
if plot_parents:
nodes = self.get_all_nodes()
else:
nodes = self.get_all_leaf_nodes()
for node in nodes:
if show_stats:
if show_combined_stat and node.is_leaf:
statistics = node.combined_stat
else:
statistics = node.stat
else:
statistics = None
if plot_circles: # plot areas with filles circles
# for plots with many circles not recommended, since many circles will overlap
circle = plt.Circle(node.centroid, node.radius, edgecolor=edgecolor_circles,
facecolor=cmap(norm(statistics)), linewidth=line_width_circles, alpha=alpha_transparency_circles)
ax.add_artist(circle)
if plot_points:
# tree_data[tree_idx_data[idx_in_ball_start[idx_node]:idx_in_ball_end[idx_node]]]
points = self.data_points[node.indices]
ax.scatter(points[:, 0], points[:, 1], color=cmap(norm(statistics)),
s=point_size, alpha=alpha_transparency_points, edgecolor=edgecolor_points)
return ax
def create_gif(self, data: np.ndarray,
plot_dir='ball_tree_frames',
# filename='balltree',
image_format='png',
dpi=300,
duration=1000,
axis_labels=(r'$x_1$', r'$x_2$'),
scatter_point_size: int = 4,
) -> None:
""" Create a gif of the tree structure."""
# create folder
os.makedirs(plot_dir, exist_ok=True)
# add parent references to make it easier to plot the tree
self._add_parent_references()
# create frames
frame_idx = 0
create_frames(self.root, data, frame_idx, plot_dir, 'balltree',
axis_labels=axis_labels, dpi=dpi, image_format=image_format,
scatter_point_size=scatter_point_size)
# create gif from single frames
create_plots.generate_gif(plot_dir, separation_str='_', duration=duration)
def create_tree_graph(self, node: Node = None, graph=None, use_latex: bool = True):
"""Create a graph from the Ball Tree.
:param node: current node (default is root)
:type node: Node
:param graph: graph object (default is None)
:type graph: networkx.Graph
:return: graph object
:rtype: networkx.Graph
"""
if graph is None:
# graph = nx.DiGraph()
# graph = pgv.AGraph(directed=True)
graph = graphviz.Digraph(node_attr=self.split_node_attr,
graph_attr=self.graph_attr)
if node is None:
node = self.root
def node_str(node):
return f'{centerpoint_to_str(node.centroid, use_latex)}\n{radius_to_str(node.radius, use_latex)}'
if isinstance(graph, (pgv.AGraph, nx.DiGraph)):
graph.add_node(node_str(node))
elif isinstance(graph, graphviz.Digraph):
graph.node(f'{node.node_id}', label=f'{node_str(node)}', _attributes=self.split_node_attr)
else:
raise ValueError('Unknown graph type')
if node.is_leaf:
return graph
# add edges to child nodes
if isinstance(graph, (pgv.AGraph, nx.DiGraph)):
graph.add_edge(node_str(node), node_str(node.left))
graph.add_edge(node_str(node), node_str(node.right))
elif isinstance(graph, graphviz.Digraph):
graph.edge(f'{node.node_id}', f'{node.left.node_id}')
graph.edge(f'{node.node_id}', f'{node.right.node_id}')
else:
raise ValueError('Unknown graph type')
# create graph for left and right child nodes
graph = self.create_tree_graph(node.left, graph, use_latex=use_latex)
graph = self.create_tree_graph(node.right, graph, use_latex=use_latex)
return graph
def draw_tree_graph(self, filename: str = 'balltree', directory='./', file_format='svg', use_latex: bool = False):
"""Draw the graph of the Ball Tree.
:param filename: filename
:type filename: str
:param directory: directory
:type directory: str
:param file_format: file format
:type file_format: str
:param use_latex: use latex for labels
:type use_latex: bool
"""
graph = self.create_tree_graph(use_latex=use_latex)
if isinstance(graph, pgv.AGraph):
# assert isinstance(graph, pgv.AGraph)
graph.layout(prog='dot') # pylint: disable=no-member
graph.draw(f'{filename}.dot', prog='dot') # pylint: disable=no-member
# graph.draw(f'{filename}.svg', prog='dot', format='svg:cairo') # pylint: disable=no-member
elif isinstance(graph, nx.DiGraph):
write_dot(graph, f'{filename}.dot')
# folder = os.getcwd()
# render the dot file to svg and safe it in the given folder
os.system(f'dot -Tsvg {filename}.dot -o {directory}/{filename}.{file_format}')
elif isinstance(graph, graphviz.Digraph):
graph.render(filename=filename, format=file_format, directory=directory, )
# modify svg and replace & with &
# filename = os.path.join(directory, f'{filename}.{file_format}')
# with open(filename, 'r') as file:
# data = file.read()
# data = data.replace('&', '&')
# with open(filename, 'w') as file:
# file.write(data)
else:
raise ValueError('Unknown graph type')
print(f'Dot file {filename} created.')
def create_latex_tree_graph(self, node: Node = None, graph: str = None, depth: int = 1, use_stat: bool = False):
""" recursivly create LaTeX file with tikz tree structure """
split_node = ''
if graph is None:
graph = ''
if node is None:
node = self.root
depth = 1
else:
depth += 1
def node_str(node: Node):
if use_stat:
stat = node.stat
if isinstance(stat, np.ndarray):
stat = float(stat[0])
else:
stat = None
return node_to_aligend_latex_equation(centroid=node.centroid, radius=node.radius, stat=stat)
def tabs(n_tabs):
return '\t' * n_tabs
if node == self.root:
graph += f'\\node[{split_node}] {{{node_str(node)}}}'
else:
graph += f'node[{split_node}] {{{node_str(node)}}}'
if node.is_leaf:
if not use_stat:
return graph
stat = node.combined_stat
if isinstance(stat, np.ndarray):
stat = float(stat[0])
# add statistics after leaf node in tree with invisible edge
graph += '\n' + tabs(depth) + 'child {node[draw=none, fill=none] {' + \
f'{stats_to_str(stat)}' + '} edge from parent[dashed, -latex] }'
return graph
# add edges to child nodes
graph += '\n'+tabs(depth) + 'child {' # + f'{node_str(node.left)}' + '}\n'
graph = self.create_latex_tree_graph(node.left, graph, depth=depth, use_stat=use_stat)
graph += '\n' + tabs(depth) + '}'
graph += '\n'+tabs(depth) + 'child {' # + f'{node_str(node.right)}' + '}\n'
graph = self.create_latex_tree_graph(node.right, graph, depth=depth, use_stat=use_stat)
graph += '\n'+tabs(depth) + '}'
return graph
def get_tree_depth(self, node: Node = None, depth: int = 0) -> int:
""" Get the depth of the tree."""
if node is None:
node = self.root
depth = 1
if node.is_leaf:
return depth
return max(self.get_tree_depth(node.left, depth=depth+1),
self.get_tree_depth(node.right, depth=depth+1))
def draw_latex_tree_graph(self, filename: str = 'balltree',
directory='',
use_stat: bool = False,
compile_pdf: bool = False):
""" Draw the LaTeX tree graph """
graph = self.create_latex_tree_graph(use_stat=use_stat)
required_packages = self._get_required_packages()
# create LaTeX file
file_header_str = r'\documentclass{{standalone}}'+'\n'
file_header_str += f'{required_packages}'+'\n'
file_header_str += r'\begin{document}'+'\n'
file_str = r'\begin{tikzpicture}'+'\n'
file_str += r'[level distance=20mm, ' + self._get_sibling_distance() + ',' \
+ r'every node/.style={fill=white, draw=black, rectangle, rounded corners=2pt,inner sep=1pt, solid},' \
+ r' edge from parent/.style={solid,draw,-}]'+'\n'
# file_str += r'\tikzstyle{split_node} = [rectangle, rounded corners=1pt, draw, fill=white, text centered]'+'\n'
file_str += f'{graph}\n;%\n'
file_str += r'\end{tikzpicture}'+'\n'
file_footer_str = r'\end{document}'+'\n'
# write tikz picture to file
with open(os.path.join(directory, f'{filename}.tex'), 'w', encoding='utf-8') as file:
file.write(file_str)
# write LaTeX standalone file
stanalone_name = f'{filename}_standalone'
standalone_path = os.path.join(directory, stanalone_name)
with open(standalone_path + '.tex', 'w', encoding='utf-8') as file:
file.write(file_header_str+file_str+file_footer_str)
# compile LaTeX standalone file to pdf
if compile_pdf:
try:
os.system(f'pdflatex -output-directory={directory} {standalone_path}.tex')
# delete aux files, log files, etc.
os.system(f'rm {standalone_path}.aux {standalone_path}.log') # {standalone_path}.synctex.gz')
except Exception as e: # pylint: disable=broad-exception-caught
print(f'LaTeX Error: {e}')
def get_combined_stat_per_leaf(self, ) -> np.ndarray:
""" Get the combined statistics per leaf node. """
leaf_nodes = self.get_all_leaf_nodes()
return np.array([leaf.combined_stat for leaf in leaf_nodes])
def _get_required_packages(self, required_packages: tuple = ('tikz',
'amsmath',
'amssymb',
'mathtools',
'siunitx',)
) -> str:
""" Get the required packages for the LaTeX tree graph."""
return '\n'.join([f'\\usepackage{{{package}}}' for package in required_packages])
def _get_sibling_distance(self, leaf_sibling_distance: float = 20.0) -> float:
""" get the sibling distance for the LaTeX tree graph.
return string in form of
level 1/.style={sibling distance=xmm},
...
level {treedepth}}/.style={sibling distance={leaf_sibling_distance}mm},
"""
# get tree depth
treedepth = self.get_tree_depth()
sibling_distance = ''
for i in range(1, treedepth+1):
sibling_distance += f'level {i}/.style={{sibling distance={ 2**(treedepth -i)*leaf_sibling_distance}mm}},\n'
return sibling_distance
def _get_structure_array(self, node):
if node.is_leaf:
return tuple([(node.start_idx, node.end_idx, 1, node.radius)])
else:
return np.concatenate([tuple([(node.start_idx, node.end_idx, 0, node.radius)]),
self._get_structure_array(node.left),
self._get_structure_array(node.right)])
def _add_parent_references(self, node: Node = None) -> None:
""" Add parent references to the nodes of the tree.
:param node: node
:type node: Node
:return: None"""
if node is None:
node = self.root
if node.is_leaf:
return
node.left.parent = node
node.right.parent = node
self._add_parent_references(node.left)
self._add_parent_references(node.right)
def calc_stat_per_node(predictions: np.ndarray, output_data: np.ndarray,
node: Node, method: str = 'anees',
alpha: float = 0.01, m_bins: int = 1):
""" Calculate statistics per region. All regions are used. """
idx_per_region = [node.indices]
stat, acccept_stat = calc_stats_per_region(predictions=predictions, output_data=output_data,
idx_per_region=idx_per_region,
method=method, alpha=alpha, m_bins=m_bins)
# use binary decision per test statistic? E.g. Cauchy combination test
_ = acccept_stat
node.stat = float(stat)
if node.is_leaf:
return
calc_stat_per_node(predictions, output_data, node.left, method=method, alpha=alpha, m_bins=m_bins)
calc_stat_per_node(predictions, output_data, node.right, method=method, alpha=alpha, m_bins=m_bins)
def create_frames(node: Node, data: np.ndarray, frame_idx: int, folder_path: str, file_name: str,
image_format='png', dpi=200, axis_labels=(r'$x_1$', r'$x_2$'), scatter_point_size: int = 4) -> int:
""" recursively create frames for the tree structure."""
if node.is_leaf:
frame_idx = create_frames_single_node(node, data, frame_idx, folder_path, file_name, dpi=dpi,
image_format=image_format, axis_labels=axis_labels, scatter_point_size=scatter_point_size)
else:
frame_idx = create_frames_single_node(node, data, frame_idx, folder_path, file_name, dpi=dpi,
image_format=image_format, axis_labels=axis_labels, scatter_point_size=scatter_point_size)
frame_idx = create_frames(node.left, data, frame_idx, folder_path, file_name, dpi=dpi,
image_format=image_format, axis_labels=axis_labels)
frame_idx = create_frames(node.right, data, frame_idx, folder_path, file_name, dpi=dpi,
image_format=image_format, axis_labels=axis_labels)
return frame_idx
def create_frames_single_node(node: Node, data: np.ndarray, idx: int, folder_path: str,
file_name: str, dpi: int = 200,
image_format='png', axis_labels=(r'$x_1$', r'$x_2$'),
scatter_point_size: float = 4,
) -> int:
""" create frames for a single node in the tree structure.
First frame is the node itself with data points and parent balls (thin lines),
second frame is the split line,
third frame is the splitted points and the resulting balls.
:param node: node
:type node: Node
:param data: data points
:type data: np.ndarray with shape (n_data_points, n_features)
:param idx: index of the frame
:type idx: int
:param folder_path: folder path to save the frames
:type folder_path: str
:param file_name: file name
:type file_name: str
:param dpi: dpi of the image
:type dpi: int
:param image_format: image format
:type image_format: str
:param axis_labels: axis labels
:type axis_labels: tuple
:return: index counter +1 (next index)
:rtype: int
"""
if node.is_leaf:
return idx
# data in node
data_in_node = data[node.indices]
# create subplot
fig, ax = plt.subplots(constrained_layout=False, )
ax.set_aspect('equal')
# set axis labels
ax.set_xlabel(axis_labels[0])
ax.set_ylabel(axis_labels[1])
# set limits
ax.set_xlim([np.min(data[:, 0]), np.max(data[:, 0])])
ax.set_ylim([np.min(data[:, 1]), np.max(data[:, 1])])
# plot all data points transparent
ax.scatter(data[:, 0], data[:, 1], alpha=0.1, s=scatter_point_size)
# plot the data points in the node
ax.scatter(data_in_node[:, 0], data_in_node[:, 1], color='tab:blue', s=scatter_point_size)
# plot the center point of the node
ax.plot(node.centroid[0], node.centroid[1], 'x', color='black')
# plot the circle around the center point
circle = plt.Circle(node.centroid, node.radius, color='black', fill=False,
linewidth=mpl.rcParams['lines.linewidth']) # use default line width from rc settings
ax.add_artist(circle)
# plot the circle around all parent nodes (if not root node)
parent = node.parent
while parent is not None:
circle = plt.Circle(parent.centroid, parent.radius, color='black', fill=False,
linewidth=1/2*mpl.rcParams['lines.linewidth']) # use default line width from rc settings
ax.add_artist(circle)
parent = parent.parent
# save the frame as png
file_path = os.path.join(folder_path, f'{file_name}_{idx}.{image_format}')
fig.savefig(file_path, dpi=dpi, bbox_inches='tight')
idx += 1
# calculate the split of the node, if node is not a leaf node
# calculate the split of the node
split_dim = np.argmax(calculate_spread(data_in_node, use_variance=False))
split_value = np.median(data_in_node[:, split_dim])
# plot the split line
if split_dim == 0:
ax.axvline(split_value, color='black', linestyle='--')
else:
ax.axhline(split_value, color='black', linestyle='--')
# save the frame as png
file_path = os.path.join(folder_path, f'{file_name}_{idx}.{image_format}')
fig.savefig(file_path, dpi=dpi, bbox_inches='tight')
idx += 1
# color the left and right points in differnt colors
left_points = data_in_node[data_in_node[:, split_dim] < split_value]
right_points = data_in_node[data_in_node[:, split_dim] >= split_value]
# create subplot
ax.scatter(left_points[:, 0], left_points[:, 1], color='tab:orange', s=scatter_point_size)
ax.scatter(right_points[:, 0], right_points[:, 1], color='tab:green', s=scatter_point_size)
# add the balls around the splitted points
circle = plt.Circle(node.left.centroid, node.left.radius, color='tab:orange', fill=False,
linewidth=mpl.rcParams['lines.linewidth']) # use default line width from rc settings
ax.add_artist(circle)
circle = plt.Circle(node.right.centroid, node.right.radius, color='tab:green', fill=False,
linewidth=mpl.rcParams['lines.linewidth']) # use default line width from rc settings
ax.add_artist(circle)
# save the frame as png
file_path = os.path.join(folder_path, f'{file_name}_{idx}.{image_format}')
fig.savefig(file_path, dpi=dpi, bbox_inches='tight')
idx += 1
plt.close(fig)
return idx
def calculate_spread(points, use_variance=False) -> np.ndarray:
""" Calculate the spread of the points in each dimension.
:param points: data points
:type points: np.ndarray with shape (n_data_points, n_features)
:param use_variance: use variance instead of max-min spread
:type use_variance: bool
:return: spread of the points in each dimension
:rtype: np.ndarray with shape (n_features,)"""
# Calculate the spread of the points in each dimension
if use_variance:
return np.var(points, axis=0)
return np.max(points, axis=0) - np.min(points, axis=0)
def calculate_radius(points) -> float:
""" Calculate the radius of the ball containing all points.
:param points: data points
:type points: np.ndarray with shape (n_data_points, n_features)
:return: radius of the ball containing all points
:rtype: float
"""
# radius is the maximum distance from the center to any point
center = np.mean(points, axis=0)
return np.max(np.linalg.norm(points - center, axis=1))
def calculate_centroid(points) -> np.ndarray:
""" Calculate the centroid of the points.
:param points: data points
:type points: np.ndarray with shape (n_data_points, n_features)
:return: centroid of the points
:rtype: np.ndarray with shape (n_features,)
"""
return np.mean(points, axis=0)
def compare_two_trees(tree1: CustomBallTree, tree2: CustomBallTree):
""" check if two trees are equal."""
return compare_nodes(tree1.root, tree2.root)
def compare_nodes(node1: Node, node2: Node):
""" check if two nodes are equal."""
# Check if nodes are both of the same type
# (both leaf nodes or both internal nodes)
if node1.is_leaf != node2.is_leaf:
return False
# Check if the start and end indices are the same
if node1.start_idx != node2.start_idx or node1.end_idx != node2.end_idx:
return False
# Check if the radius is the same (within some tolerance)
if not np.isclose(node1.radius, node2.radius, atol=1e-6):
return False
# if both nodes are leaf nodes, return True (no need to check the children)
if node1.is_leaf:
return True
# else, recusively check the left and right children
return compare_nodes(node1.left, node2.left) and compare_nodes(node1.right, node2.right)
def rebuild_tree(tree_structure, indices: typing.List[int], data=np.ndarray, idx=0) -> Node:
""" Rebuild the tree from the tree structure.
:param tree_structure: tree structure
:type tree_structure: np.ndarray
:param indices: indices of the data points in the tree
:type indices: np.ndarray with shape (n_data_points,)
:param data: data points
:type data: np.ndarray with shape (n_data_points, n_features)
:param idx: index of the node in the tree structure
:type idx: int
:return: node
:rtype: Node
"""