-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_manipulator.py
1374 lines (1231 loc) · 54 KB
/
tree_manipulator.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
__author__ = 'Michael'
import math
import dendropy
from view import *
from utilities import *
import copy
import wx
import numpy as np
import scipy.spatial as spat
import sys
import datetime, time
# import c_utilities
GLOBAL_DEBUG = True
# class Singleton(type):
# def __init__(cls, name, bases, dict):
# super(Singleton, cls).__init__(name, bases, dict)
# cls.instance = None
#
# def __call__(cls, *args, **kw):
# if not cls.instance:
# # Not created or has been Destroyed
# obj = super(Singleton, cls).__call__(*args, **kw)
# cls.instance = obj
# return cls.instance
class AnnotatedPhylogramModel():
tree_file = None
annotation_file = None
def __init__(self):
#state information
self.state_tree_loaded=False
self.state_node_annotation_loaded=False
self.rp = None
#picture information
self.node_annotation = None
self.segments=[]
self.node_coordinates={}
self.node_annotation_level=None
self.checked=[]
def initialize_tree(self,tp=None):
if tp!= None:
self.tree_file=tp
self.rp=Radial_Phylogram(self.tree_file)
self.rp.get_segments()
self.segments=self.rp.segments
self.state_tree_loaded=True
def unload_tree(self):
self.rp=None
self.rp.segments=[]
self.state_tree_loaded=False
def initialize_annotation(self,ann_path):
self.annotation_file=ann_path
self.node_annotation=SfldAnnotationData(self.annotation_file)
self.state_node_annotation_loaded=True
pass
class Radial_Phylogram():
'''
Right now it requires the tree to have labels equal to the "efd_id" field from the node_annotation
'''
max_dims = None
myt_copy = None
node_center_index= 1
numnodes=None
def __init__(self,tp=None):
self.treepath=''
self.myt=None
self.node_labels={}
self.segments=[]
self.selected=[]
self.leaf_node_coords={}
self.selected_color=None
self.rotation=0
if tp is not None and tp!='':
self.set_treepath(tp)
# self.print_right_angles()
# def set_rotation(self,rotation):
# '''
#
# :param rotation: rotation given in degrees
# :return:
# '''
# self.rotation=rotation/360 * 2 * math.pi
# tp = self.treepath
# self.set_treepath(tp)
# self.get_radial_phylogram()
# self.get_segments()
# self.get_max_dims()
def spacefill_get_space_filling_phylogram(self):
'''
This function will populate the dendropy tree with a set of properties on each edge and node that
define a 2D tree layout that approximately fills the rectangular viewing area optimally.
The properties are:
'deflect_angle' on each edge,
'location' on each node and an
'aspect_ratio'on the tree object.
The layout is defined by a root-edge, the aspect ratio and the deflect angles (
positive = clockwise). Default aspect ratio is 1.6 (w/h)
:return:
'''
if self.myt.aspect_ratio is None:
self.myt.aspect_ratio = 1.6
to = 0
for i in self.myt.postorder_edge_iter():
if i.length is None:
i.length = 0.0
to += i.length
self.myt.calc_node_ages(ultrametricity_precision=False,is_force_max_age=True)
self.myt.calc_node_root_distances(False)
first = True
for i in self.myt.postorder_edge_iter():
current_opposite_dist=0
if first==True:
root_childs= i.head_node.child_nodes()
root_child_dists = [(j.age+j.edge_length) for j in root_childs]
root_opposite_dists = [(max(root_child_dists[:ind]+root_child_dists[(ind+1):])) for ind in range(len(root_childs))]
first = False
else:
if i.head_node in root_childs:
ind = root_childs.index(i)
current_opposite_dist = root_opposite_dists[ind]
if i.head_node.is_leaf() == True:
i.head_mass = 0
i.tail_mass = to - i.length
# i.head_width = -1
i.head_len = i.length
i.tail_len = i.head_node.parent.root_distance + current_opposite_dist
# i.tail_width =
else:
cn = i.head_node.child_nodes()
i.head_mass = sum([(k.edge.head_mass + k.edge.length) for k in cn])
i.tail_mass = to - i.length - i.head_mass
i.head_len = i.length+i.age
i.tail_len = i.head_node.parent.root_distance + current_opposite_dist
self.cent_edge = self.spacefill_get_centroid_edge()
self.cent_child_nodes = []
for e in self.cent_edge.adjacent_edges:
if e.head_node==self.cent_edge.head_node:
self.cent_child_nodes.append(e.tail_node)
else:
self.cent_child_nodes.append(e.head_node)
self.myt.reroot_at_edge(self.cent_edge)
for i in self.myt.preorder_node_iter():
if i.length is None:
i.length = 0.
self.myt.calc_node_ages(ultrametricity_precision=False,is_force_max_age=True)
self.myt.calc_node_root_distances(False)
d = self.cent_edge.length
for i in self.cent_child_nodes:
ages=[(j.age + j.edge_length) for j in i.child_nodes()]
# TODO: continue here
def spacefill_get_centroid_edge(self):
'''
finds the edge with the minimal squared deviation from a (1/4, 1/4, 1/4, 1/4) split based
on the 'age' attribute of the four child nodes (plus lengths)
:return:
'''
m_sse = 9999999.0
cent_edge = None
for i in self.myt.preorder_edge_iter():
eds = i.adjacent_edges
hn = i.head_node
tn = i.tail_node
if len(eds)>=4:
heads= [(e.head_node==hn or e.head_node==tn) for e in eds]
k = len(heads)
lens = [0.]*k
for ind in range(k):
if heads[ind]==True:
lens[ind]=eds[ind].length+eds[ind].tail_len
else:
lens[ind]=eds[ind].length+eds[ind].head_len
all_lens = sum(lens)
sse=sum([(j/all_lens-1.0/float(k))**2 for j in lens])
if sse < m_sse:
m_sse = sse
cent_edge = i
return cent_edge
def dump_all(self):
del self.myt
self.myt=None
del self.segments
self.segments=None
def set_treepath(self,tp):
self.treepath=tp
self.myt=dendropy.Tree.get(file=open(self.treepath,'r'),schema="newick",preserve_underscores=True)
self.get_radial_phylogram()
self.get_max_dims()
self.get_leaf_node_coords()
self.get_segments()
self.myt.m_matrix = None
print('Successfully imported tree. Number of Taxa: %s' % len(self.myt.leaf_nodes()))
def get_radial_phylogram(self):
# myt=set_treepath(tp)
# theta_vals=[]
# theta_vals2=[]
# theta_vals3=[]
self.node_ct = 0
self.prepare_tree()
for i in self.myt.postorder_node_iter():
self.node_ct +=1
if i.is_leaf()==True:
self.node_labels[i.label]['l']=1
else:
child_l=0
for j in i.child_node_iter():
child_l+=self.node_labels[j.label]['l']
self.node_labels[i.label]['l']=child_l
pr=self.myt.preorder_node_iter()
r=pr.__next__()
self.node_labels[r.label]['x']=(0.0,0.0)
r.location = (0.0,0.0)
r.deflect_angle = 0.
r.wedge_angle = 2*math.pi
r.edge_segment_angle = 0.
r.right_wedge_border = 0.
self.node_labels[r.label]['w']=2*math.pi
self.node_labels[r.label]['t']=0.0
self.node_labels[r.label]['nu']=0.0
leafct=len(r.leaf_nodes())
# k=0
for i in pr:
# k+=1
# print(k)
# if i.edge_length is None:
# i.edge_length = 0.001 # debug
ww=float(len(i.leaf_nodes()))/leafct*2*math.pi
self.node_labels[i.label]['w']=ww # wedge angle
i.wedge_angle=ww
i.right_wedge_border =self.node_labels[i.parent_node.label]['nu']
self.node_labels[i.label]['t']=i.right_wedge_border # angle of right wedge border
# theta_vals.append(self.node_labels[i.label]['t'])
self.node_labels[i.label]['nu']=self.node_labels[i.label]['t']
thetav=self.node_labels[i.parent_node.label]['nu']+ww/2
i.edge_segment_angle=thetav
self.node_labels[i.label]['theta']=thetav
# theta_vals2.append(i.label)
self.node_labels[i.parent_node.label]['nu']+=ww
xu=self.node_labels[i.parent_node.label]['x']
delta=i.edge_length
try:
x1=xu[0]+delta*math.cos(thetav)
x2=xu[1]+delta*math.sin(thetav)
except:
print('thetav is %d' % thetav)
x1=xu[0]
x2=xu[1]
i.location = (x1,x2)
i.deflect_angle = thetav - self.node_labels[i.parent_node.label]['theta']
# theta_vals3.append(self.node_labels[i.label]['t'])
# x1_orig = xu[0] + delta * math.cos(thetav)
# x2_orig = xu[1] + delta * math.sin(thetav)
# x1 = x1_orig*math.cos(self.rotation)-x2_orig*math.sin(self.rotation)
# x2 = x1_orig*math.sin(self.rotation)+x2_orig*math.cos(self.rotation)
self.node_labels[i.label]['x']=(x1,x2)
for i in self.myt.preorder_internal_node_iter():
w = i.wedge_angle
for j in i.child_node_iter():
j.percent_of_parent_wedge=j.wedge_angle/w
ct = 0
self.pts_nparr = np.zeros((self.node_ct,2),dtype=np.float64)
eip = []
for i in self.myt.preorder_node_iter():
i.index = ct
self.pts_nparr[i.index,:]=i.location
if i.parent_node is not None:
eip.append((i.index,i.parent_node.index))
ct +=1
self.edges_as_node_pairs = np.asarray(eip,dtype=np.int32)
# print(self.edges_as_node_pairs.shape)
def make_tree_copy(self,parent, evt):
# if self.myt_copy is not None:
# del self.myt_copy
# self.myt_copy=copy.deepcopy(self.myt)
print('event set in make_tree_copy')
evt.set()
# wx.CallAfter(parent.UpdateDrawing)
def relocate_subtree_by_deflect_angle(self,node):
'''
runs a preorder node iteration and uses the current deflection angles of each node to replace
its current location with a new location.
:param node:
:return:
'''
for i in node.preorder_iter():
t0 = i.parent_node.edge_segment_angle
x0 = i.parent_node.location
t1 = i.deflect_angle + t0
x1x = x0[0]+i.edge_length*math.cos(t1)
x1y = x0[1]+i.edge_length*math.sin(t1)
i.location = (x1x,x1y)
self.pts_nparr[i.index, :] = i.location
def relocate_subtree_by_edge_segment_angle(self,node=None):
'''
runs a preorder node iteration and uses the current deflection angles of each node to replace
its current location with a new location.
:param node:
:return:
'''
if node != None:
for i in node.preorder_iter():
x0 = i.parent_node.location
t1 = i.edge_segment_angle
x1x = x0[0]+i.edge_length*math.cos(t1)
x1y = x0[1]+i.edge_length*math.sin(t1)
i.location = (x1x,x1y)
self.pts_nparr[i.index,:]= i.location
else:
pr = self.myt.preorder_node_iter()
pr.__next__()
for i in pr:
x0 = i.parent_node.location
t1 = i.edge_segment_angle
x1x = x0[0]+i.edge_length*math.cos(t1)
x1y = x0[1]+i.edge_length*math.sin(t1)
i.location = (x1x,x1y)
self.pts_nparr[i.index, :] = i.location
def spacefill_spread_tree_by_levelorder(self, parent):
'''
do a level-order node iteration over the (non-root) leaves. At each one, calculate the
de facto spread angle and compare it to the allowable. Boost the allowable by up to 25%.
:return:
'''
levelo=self.myt.levelorder_node_iter()
rt = levelo.__next__()
ct = 0
max_ct = 30
for i in levelo:
df_min_max = self.get_de_facto_spread_angle(i)
df_spread = df_min_max[1]-df_min_max[0]
if df_spread < .8*i.wedge_angle:
for j in i.child_nodes():
j.percent_of_parent_wedge*=1.25
self.relocate_subtree_by_wedge_properties(i)
ct +=1
print("done with %s expansions" % ct)
if ct % 50 == 0:
self.make_tree_copy(parent)
# print('press any key to continue')
# raw_input()
# parent.UpdateDrawing()
else:
print("wedge angle x .9: %s, de-facto spread: %s" % (.9*i.wedge_angle,df_spread))
# if ct >=max_ct:
# break
print("done with spacefill by levelorder")
def test_1(self):
for i in self.myt.preorder_node_iter():
if i.label == 'label11':
test_nd = i
break
print('---------test 1: label11 -------------------------------')
print(test_nd.__dict__)
dfs = self.get_de_facto_spread_angle(test_nd)
print('min %.4f -- max %.4f -- spread %.4f' % (dfs[0],dfs[1],dfs[1]-dfs[0]))
def test_2(self):
for i in self.myt.preorder_node_iter():
if i.label == 'label11':
test_nd = i
break
for j in test_nd.child_nodes():
j.percent_of_parent_wedge*=1.0
self.relocate_subtree_by_wedge_properties(test_nd)
def test_3(self):
pass
# def test_3_old(self):
# # self.set_segments_as_nparr()
# # print(np_find_intersect_segments_test(self.segments_as_nparr))
# self.numnodes = int(self.pts_nparr.shape[0])
# self.deflect_angles = np.zeros((self.numnodes,1),dtype=np.float64)
# self.wedge_sizes = np.zeros((self.numnodes,1),dtype=np.float64)
# self.right_wedges = np.zeros((self.numnodes,1),dtype=np.float64)
# self.edge_angles = np.zeros((self.numnodes,1),dtype=np.float64)
# self.lengths = np.zeros((self.numnodes,1),dtype=np.float64)
# self.topo = -1*np.ones((self.numnodes,3),dtype=np.int32)
#
# for i in self.myt.preorder_node_iter():
# self.edge_angles[i.index]=i.edge_segment_angle
# self.deflect_angles[i.index]=i.deflect_angle
# self.wedge_sizes[i.index] = i.wedge_angle
# self.right_wedges[i.index] = i.right_wedge_border
# if i.edge_length is not None:
# self.lengths[i.index]=i.edge_length
# if i.parent_node is not None:
# self.topo[i.index,0]=i.parent_node.index
# if len(i.child_nodes())>0:
# self.topo[i.index,1]=i.child_nodes()[0].index
# if len(i.child_nodes())>1:
# self.topo[i.index,2]=i.child_nodes()[1].index
# if len(i.child_nodes())>2 and i.parent_node is None:
# self.topo[i.index,0]=i.child_nodes()[2].index
# # print(self.pts_nparr.__array_interface__['data'])
# # print(self.topo.__array_interface__['data'])
# # print(self.edge_angles.__array_interface__['data'])
# # print(self.deflect_angles.__array_interface__['data'])
# # print(self.lengths.__array_interface__['data'])
# # np.savetxt('resources/cutils/pts_nparr.txt',self.pts_nparr,'%f','\t')
# # c_utilities.dbgWriteSegs(self.pts_nparr, self.topo, self.numnodes)
#
#
# c_utilities.angleSpread(self.pts_nparr, self.topo, self.numnodes,self.deflect_angles,
# self.wedge_sizes, self.edge_angles, self.lengths,self.right_wedges,0)
# # self.node_center_index = 0
# # for i in range(50):
# # c_utilities.centerCladeRot(self.pts_nparr, self.topo,self.edge_angles,
# # self.deflect_angles, self.lengths, self.numnodes, self.node_center_index)
# # self.node_center_index +=1
# # c_utilities.testCheck()
# # print(self.pts_nparr)
# # if self.node_center_index >= self.numnodes:
# # self.node_center_index=0
#
# for i in self.myt.preorder_node_iter():
# i.edge_segment_angle=self.edge_angles[i.index]
# i.deflect_angle = self.deflect_angles[i.index]
# i.location = (np.asscalar(self.pts_nparr[i.index,0]),np.asscalar(self.pts_nparr[i.index,1]))
# # time.sleep(.1)
#
#
# # testing
# # nn=np.asarray(numnodes,dtype=np.int32)
# # nn.tofile('resources/cutils/num_nodes.bin')
# # self.topo.tofile('resources/cutils/topo.bin')
# # self.pts_nparr.tofile('resources/cutils/pts_nparr.bin')
# # self.edge_angles.tofile('resources/cutils/edge_angles.bin')
# # self.deflect_angles.tofile('resources/cutils/deflect_angles.bin')
# # self.lengths.tofile('resources/cutils/lengths.bin')
#
# # np.savetxt('resources/cutils/topo.txt',self.topo,'%f','\t')
#
#
# pass
def test_4(self, parent=None, myevt = None):
'''
delauny triangulation method
:return:
'''
pass
# def test_4_old(self, parent=None, myevt=None):
# ndct, node_order, pts, pts_leaves_bln, tri, pts_nparr = self.get_delaunay_trianglization()
#
#
# # declare some matrices for the partials
#
# ''' M here has a specific definition. A column of M represents a node somewhere on the tree,
# and the columsn are indexed by the node labels. The rows of M are indexed by branches (equivalently,
# also nodes). The (i,j)-th entry of M (row i, col j) is 1 iff branch i is on the path from the root
# to node j.
# '''
# # M = np.zeros((ndct, lct), dtype=np.float64) # for K leaves, M is [(2K-2) X K]
# # using all delaunay distances, not just leaves:
# M = np.zeros((ndct, ndct), dtype=np.float64) # for K leaves, M is [(2K-2) X (2K-2)]
#
# lens = np.zeros(ndct, dtype=np.float64)
# thetas = np.zeros(ndct, dtype=np.float64)
#
# # Make the 'M' matrix:
# if GLOBAL_DEBUG==True:
# print('making the M matrix')
# do_M = (self.myt.m_matrix is None)
# pr = self.myt.preorder_node_iter()
# pr.__next__() #(excluding the first one)
# for nd in pr:
# nd_ind = pts[nd.label]['index']
# t_i = nd.edge_segment_angle
# thetas[nd_ind] = t_i
#
# L_i = nd.edge_length
# lens[nd_ind] = L_i
#
# if do_M == True:
#
# for sub_nd in nd.preorder_iter():
# sub_nd_ind = pts[sub_nd.label]['index']
# M[nd_ind,sub_nd_ind]=1.
# else:
# M = self.myt.m_matrix
# if do_M==True:
# # storing the M matrix and the pts object in the tree for later.
# self.myt.m_matrix=M
#
# # np.savetxt('C:\\Users\\miken\\Dropbox\\Grad School\\Phylogenetics\\work\\phylostrator-testing\\test-M.txt', M, '%.2f', '\t')
#
# if GLOBAL_DEBUG == True:
# print('getting delaunay leaf segments')
# seg_inds = self.get_delaunay_leaf_segments( pts_leaves_bln, tri)
# leaf_to_edge_segs = self.get_delaunay_leaf_to_edge_segments(tri,pts_nparr)
#
# # get the gradient vector
# if GLOBAL_DEBUG == True:
# print('getting the gradient vector')
# gradients = self.get_delaunay_gradients(M, lens, node_order, pts, seg_inds,
# thetas, ndct, pts_nparr, leaf_to_edge_segs)
# # np.savetxt('C:\\Users\\miken\\Dropbox\\Grad School\\Phylogenetics\\work\\phylostrator-testing\\test-gradients.txt', gradients,
# # '%.2f', '\t')
# if GLOBAL_DEBUG == True:
# print('done with delaunay gradients')
#
# loss = self.get_total_loss(pts_nparr, seg_inds)
# print('Starting Loss: %s' % loss)
#
# step = .01
# # max_angle_change = np.pi / (float(ndct)/2.)
# max_angle_change = .017*10
# self.po_ct = 0
#
# ok_to_continue = True
# q=None
#
# # trying the all at once method:
# eff_grads = np.dot(2*np.identity(ndct,dtype=np.float64)-M.transpose(),gradients)
# np.savetxt('C:\\Users\\miken\\Dropbox\\Grad School\\Phylogenetics\\work\\phylostrator-testing\\test-effgrads.txt',eff_grads,'%.2f', '\t')
#
# # ct +=0
# for i in self.myt.preorder_node_iter():
# last_edge_angles = self.get_tree_restore_point(ndct)
# i.edge_segment_angle += min(max(-max_angle_change, step * gradients[i.index]), max_angle_change)
# self.relocate_subtree_by_edge_segment_angle()
# self.set_segments_as_nparr()
# if np_find_intersect_segments(self.segments_as_nparr) == False:
# print("breaking because we found an intersection")
# print(np_find_intersect_segments_allpy(self.segments_as_nparr))
# self.set_tree_to_last_restore_point(last_edge_angles)
# break
#
#
# # print('index: %s -- gradient %s -- angle change %s' % (i.index, gradients[i.index],min(max(-max_angle_change, step * gradients[i.index]), max_angle_change)))
# myevt.set()
# # time.sleep(0.1)
#
# q=raw_input()
# if q == 'q':
# break
# self.relocate_subtree_by_edge_segment_angle()
# self.make_tree_copy(parent,myevt)
# while (self.po_ct < 30):
# ct = 0
# last_edge_angles=self.get_tree_restore_point(ndct)
# all_segs = np.zeros((ndct,4),dtype=np.float64)
# # for i in self.myt.postorder_node_iter():
# for i in self.myt.levelorder_node_iter():
# i.edge_segment_angle += min(max(-max_angle_change,step * gradients[i.index]),max_angle_change)
#
# ct +=1
# if ct % 1 ==0:
# self.make_tree_copy(parent,myevt)
# tic=datetime.datetime.now()
# # gradients, loss = self.refresh_and_redraw(M, lens, ndct, node_order, parent,
# # pts, pts_nparr, seg_inds, thetas, new_delaunay=True, pts_leaves_bln=pts_leaves_bln)
# self.relocate_subtree_by_edge_segment_angle()
# self.update_pts_np_array(pts_nparr)
#
# # check for intersections
# pr = self.myt.preorder_node_iter()
# pr.__next__()
# for i in pr:
# all_segs[i.index, 0:2] = i.location
# all_segs[i.index, 2:4] = i.parent_node.location
# check_intersect = np_find_intersect_segments(all_segs)
# ok_to_continue = check_intersect[0]
# if ok_to_continue == True:
# print('No intersections found, continuing for another round')
# last_edge_angles = self.get_tree_restore_point(ndct)
# else:
# # parent.draw_red_line_pair(check_intersect[1],check_intersect[2])
# # parent.UpdateDrawing()
# self.set_tree_to_last_restore_point(last_edge_angles)
# print('Intersection found, resetting to last restore point')
# print(check_intersect)
# toc = datetime.datetime.now()
# print('Finished with all steps after %s nodes in %s seconds.' % (ct, toc-tic))
# if ct % 25 == 0:
# print('ct = %s' % ct)
# gradients, loss = self.refresh_and_redraw(M, lens, ndct, node_order, parent,
# pts, pts_nparr, seg_inds, thetas,
# new_delaunay=True, pts_leaves_bln=pts_leaves_bln)
# self.po_ct += 1
# print('Done with one full postorder edit. Total loss = %s' % self.get_total_loss(pts_nparr, seg_inds))
# if po_ct % 2 ==0:
# q=raw_input()
# if q=='q':
# # np.savetxt('C:\\Users\\miken\\Dropbox\\Grad School\\Phylogenetics\\work\\phylostrator-testing\\all_segs.txt',all_segs,delimiter='\t')
# break
# self.set_tree_to_last_restore_point(last_edge_angles)
# self.make_tree_copy(parent)
# levelorder_hash = self.get_levelorder_hash()
# levels = levelorder_hash.keys()
#
# levels.sort(reverse=True)
# for level in levels:
# if GLOBAL_DEBUG == True:
# print('starting level %s' % level)
# angle_changes = []
# for i in levelorder_hash[level]:
# # ind = pts[i.label]['index']
# i.edge_segment_angle += step * gradients[i.index]
# angle_changes.append(step * gradients[i.index])
# if len(angle_changes)>30:
# angle_changes = angle_changes[0:30]
# print('angle changes: %s' % map(lambda x: '%.3f' % x, angle_changes))
#
# gradients, loss = self.refresh_and_redraw(M, gradients, lens, loss, ndct, node_order, parent, pts,
# pts_nparr, seg_inds, thetas)
# if GLOBAL_DEBUG==True:
# # # print('done with callback, setting status.')
# print('Level %s\tLoss: %s\n' % (level, loss))
#
# # print('status should say: Done with level %s, press a key to continue (q to exit thread)' % level)
# # parent.parent.set_status('Done with level %s, press a key to continue' % level)
# cmd=raw_input()
# if cmd=='q':
# print('exiting space fill function')
# return
# loss = self.get_total_loss(pts_nparr, seg_inds)
# print('Loss after full iteration: %s' % loss)
# parent.parent.set_status('Ready')
# print('exiting space fill function')
def set_segments_as_nparr(self):
edge_ct = self.edges_as_node_pairs.shape[0]
self.segments_as_nparr = np.zeros((edge_ct,4),dtype=np.float64)
self.segments_as_nparr[:, 0:2] = self.pts_nparr[self.edges_as_node_pairs[:, 0],:]
self.segments_as_nparr[:, 2:4] = self.pts_nparr[self.edges_as_node_pairs[:, 1],:]
def fix_missing_edge_lengths(self):
lens = []
pr = self.myt.preorder_node_iter()
pr.__next__()
for i in pr:
if i.edge.length is not None:
i.edge.orig_edge_length = i.edge.length
lens.append(i.edge.length)
else:
i.edge.orig_edge_length = None
mean_len = np.mean(np.asarray(lens,dtype=np.float64))
pr = self.myt.preorder_node_iter()
pr.__next__()
pr.__next__()
for i in pr:
if i.edge.length is None or i.edge.length==0:
i.edge.length = mean_len
# leaflens=[]
# for i in self.myt.leaf_node_iter():
# leaflens.append(i.edge_length)
# print(leaflens)
def get_tree_restore_point(self, ndct):
last_edge_angles = np.zeros(ndct,dtype=np.float64)
pr = self.myt.preorder_node_iter()
pr.__next__()
for i in pr:
last_edge_angles[i.index]=i.edge_segment_angle
return last_edge_angles
def set_tree_to_last_restore_point(self,last_edge_angles):
pr = self.myt.preorder_node_iter()
pr.__next__()
for i in pr:
i.edge_segment_angle = last_edge_angles[i.index]
self.relocate_subtree_by_edge_segment_angle()
def refresh_and_redraw(self, M, lens, ndct, node_order, parent, pts, pts_nparr, seg_inds, thetas, full_redraw=False, new_delaunay=False, pts_leaves_bln=None):
self.relocate_subtree_by_edge_segment_angle()
# print(full_redraw)
# if full_redraw:
# self.make_tree_copy(parent)
self.update_pts_np_array(pts_nparr)
leaf_to_edge_segs = None
if new_delaunay==True:
tri = spat.Delaunay(pts_nparr)
seg_inds=self.get_delaunay_leaf_segments(pts_leaves_bln,tri)
leaf_to_edge_segs=self.get_delaunay_leaf_to_edge_segments(tri,pts_nparr)
gradients = self.get_delaunay_gradients(M, lens, node_order, pts, seg_inds,
thetas, ndct, pts_nparr, leaf_to_edge_segs)
loss = self.get_total_loss(pts_nparr, seg_inds)
return gradients, loss
def update_pts_np_array(self,pts_nparr):
tic = datetime.datetime.now()
pr = self.myt.preorder_node_iter()
pr.__next__()
for i in pr:
pts_nparr[i.index,:]=i.location
self.pts_nparr[i.index,:]=i.location
toc = datetime.datetime.now()
# print('Points numpy array updated. Time: %s' % (toc - tic))
def get_levelorder_hash(self):
'''
returns a dictionary object indexed by level (integers) and containing a list of references to node
objects at that level. Excludes the root of the tree.
'''
lo = self.myt.levelorder_node_iter()
a=lo.__next__()
l_hash={}
for i in lo:
l = i.level()
if l not in l_hash.keys():
l_hash[l]=[]
l_hash[l].append(i)
return l_hash
def get_total_loss(self,pts_nparr,seg_inds):
s1 = pts_nparr[seg_inds[:, 0]]
s2 = pts_nparr[seg_inds[:, 1]]
return np.sum(np.sqrt(np.sum((s1-s2)**2,1)), 0)
def get_delaunay_gradients(self, M, lens, node_order, pts, seg_inds, thetas, ndct, pts_nparr, leaf_to_edge_segs=None):
tic = datetime.datetime.now()
dLdx = np.zeros(ndct, dtype=np.float64)
dLdy = np.zeros(ndct, dtype=np.float64)
nsegs = seg_inds.shape[0]
s1 = pts_nparr[seg_inds[:,0]]
s2 = pts_nparr[seg_inds[:,1]]
sq_norms = np.sum((s1-s2)**2,1)
dLdx_pre = (s1-s2).transpose()/sq_norms
total_loss = np.sum(np.sqrt(sq_norms),0)
# TODO: take care of the double counting in this:
for i in range(nsegs):
dLdx[seg_inds[i, 0]] += dLdx_pre[0,i]
dLdx[seg_inds[i, 1]] += -dLdx_pre[0,i]
# #DEBUG
# if seg_inds[i,0]==test_nd_ind:
# opp_node=self.node_refs[seg_inds[i,1]]
# debug_file.write('Node\t' + opp_node.label + '\t' + str(dLdx_pre[0,i]) + '\t' + str(dLdx_pre[1,i]) + '\t' + str(sq_norms[i]) + '\n')
# elif seg_inds[i,1]==test_nd_ind:
# opp_node=self.node_refs[seg_inds[i,0]]
# debug_file.write('Node\t' + opp_node.label + '\t' + str(-dLdx_pre[0, i]) + '\t' + str(-dLdx_pre[1, i]) + '\t' + str(sq_norms[i]) + '\n')
# if GLOBAL_DEBUG == True:
# print('running through second O(n) loop in gradients function...')
for i in range(nsegs):
dLdy[seg_inds[i, 0]] += dLdx_pre[1,i]
dLdy[seg_inds[i, 1]] += -dLdx_pre[1,i]
# add in the impacts of the distances to other edges in the delaunay:
if leaf_to_edge_segs is not None:
for i in leaf_to_edge_segs:
lds = np.hstack((pts_nparr[i[0],:], pts_nparr[i[1],:], pts_nparr[i[2],:])) # points of the triangle: (tipp, hypot[1], hypot[2])
denom = lds[0]*(lds[5]-lds[3])-lds[1]*(lds[4]-lds[2])+lds[4]*lds[3]-lds[5]*lds[2]
if np.abs(denom) > .0000000001:
dLdx[i[0]] += np.sign(denom)*(lds[5]-lds[3])/np.abs(denom)
dLdy[i[0]] += -np.sign(denom) * (lds[4] - lds[2]) / np.abs(denom)
# DEBUG
# if i[0]==test_nd_ind:
# opplab = self.node_refs[i[1]].label + '-to-' + self.node_refs[i[2]].label
# debug_file.write('Edge\t' + opplab + '\t' + str(np.sign(denom)*(lds[5]-lds[3])/np.abs(denom))
# + '\t' + str(-np.sign(denom) * (lds[4] - lds[2]) / np.abs(denom)) + '\t'
# + str(np.abs(denom) / np.linalg.norm(lds[2:4]-lds[4:6])) + '\n')
# finally, the distances to sibling edges:
pr = self.myt.preorder_node_iter()
pr.__next__()
for i in pr:
sn = i.sibling_nodes()
if len(sn)>0:
for j in sn:
tipp = pts_nparr[i.index,:]
h0 = pts_nparr[j.index,:]
h1 = pts_nparr[j.parent_node.index,:]
lds = np.hstack((tipp,h0,h1))
i_node = (i.index, j.index, j.parent_node.index)
if np.linalg.norm(h1-h0) > max(np.linalg.norm(h1-tipp),np.linalg.norm(h0-tipp)):
denom = lds[0] * (lds[5] - lds[3]) - lds[1] * (lds[4] - lds[2]) + lds[4] * lds[3] - lds[5] * lds[2]
if np.abs(denom)>.0000000001:
dLdx[i_node[0]] += np.sign(denom) * (lds[5] - lds[3]) / np.abs(denom)
dLdy[i_node[0]] += -np.sign(denom) * (lds[4] - lds[2]) / np.abs(denom)
#DEBUG
# if i_node[0] == test_nd_ind:
# opplab = self.node_refs[i_node[1]].label + '-to-' + self.node_refs[i_node[2]].label
# debug_file.write('Sibling\t' + opplab + '\t' + str(np.sign(denom) * (lds[5] - lds[3]) / np.abs(denom))
# + '\t' + str(-np.sign(denom) * (lds[4] - lds[2]) / np.abs(denom)) + '\t'
# + str(np.abs(denom) / np.linalg.norm(lds[2:4] - lds[4:6])) + '\n')
#
# debug_file.close()
# if GLOBAL_DEBUG == True:
# print('calculating dxdt and dydt')
# dxdt = np.dot(np.diag(np.multiply(-1 * lens, np.sin(thetas))), M) # [(2K-2) X (2K-2)] x [(2K-2) X (2K-2)]
dxdt = (M.transpose()*np.multiply(-1 * lens, np.sin(thetas))).transpose()
# if GLOBAL_DEBUG == True:
# print('dxdt done...')
# dydt = np.dot(np.diag(np.multiply(lens, np.cos(thetas))), M) # [(2K-2) X (2K-2)] x [(2K-2) X (2K-2)]
dydt = (M.transpose() * np.multiply(lens, np.cos(thetas))).transpose()
# if GLOBAL_DEBUG == True:
# print('dydt done...')
# print('Total Log-Norms: %s' % total_loss)
gradients = np.dot(dLdx, dxdt) + np.dot(dLdy, dydt)
# if GLOBAL_DEBUG == True:
# print('done making gradietns, the shape is: %s' % tuple(gradients.shape))
# print(gradients.shape)
toc = datetime.datetime.now()
# print('Total time to calc gradients: %s' % (toc-tic))
return gradients
def get_delaunay_leaf_segments(self, pts_leaves_bln, tri):
seg_inds_list = [] # list of pairs representing segments between leaves based on indices pulled from the simplices
H = tri.simplices.shape[0]
for i in range(H):
# TODO: this version is for when we just consider distances between at least one leaf
j1 = pts_leaves_bln[tri.simplices[i, 0]]
j2 = pts_leaves_bln[tri.simplices[i, 1]]
j3 = pts_leaves_bln[tri.simplices[i, 2]]
if j1 == True or j2 == True:
seg_inds_list.append(tri.simplices[i, 0:2])
if j1 == True or j3 == True:
seg_inds_list.append(np.vstack((tri.simplices[i, 0], tri.simplices[i, 2])).transpose())
if j2 == True or j3 == True:
seg_inds_list.append(tri.simplices[i, 1:3])
# add each segment calc to the partials
seg_inds = np.vstack(tuple(seg_inds_list))
return seg_inds
def get_delaunay_leaf_to_edge_segments(self, tri, pts_nparr):
leaf_to_edge_segs=[]
H = tri.simplices.shape[0]
for i in range(H):
inds = tri.simplices[i,:]
j0j1 = np.linalg.norm(pts_nparr[inds[0], :] - pts_nparr[inds[1], :])
j0j2 = np.linalg.norm(pts_nparr[inds[0], :] - pts_nparr[inds[2], :])
j1j2 = np.linalg.norm(pts_nparr[inds[1], :] - pts_nparr[inds[2], :])
if j0j1>j1j2:
if j0j2>j0j1:
hyp=(0,2); tipp=1
else:
hyp=(0,1); tipp=2
else:
if j0j2>j1j2:
hyp=(0,2); tipp=1
else:
hyp=(1,2); tipp=0
# check to see if the hypoteneuse is an edge:
if self.node_refs[inds[tipp]].is_leaf():
if self.node_refs[inds[hyp[0]]].parent_node is not None and self.node_refs[inds[hyp[1]]].parent_node is not None:
if self.node_refs[inds[hyp[0]]].parent_node.index==inds[hyp[1]] or self.node_refs[inds[hyp[1]]].parent_node.index==inds[hyp[0]]:
leaf_to_edge_segs.append((inds[tipp],inds[hyp[0]],inds[hyp[1]]))
return leaf_to_edge_segs
def get_delaunay_trianglization(self):
pts = {}
ndct = 0
lct = 0
leaf_order = []
node_order = []
pts_lst_np = []
pts_leaves_bln = []
self.node_refs=[]
#DEBUG
# node_ref_file = open('C:\\Users\\miken\\Dropbox\\Grad School\\Phylogenetics\\work\\phylostrator-testing\\node_indices.txt', 'w')
for i in self.myt.preorder_node_iter():
npiloc = np.array([i.location[0], i.location[1]], dtype=np.float64)
pts_lst_np.append(npiloc)
pts[i.label] = {'npiloc': npiloc,
'leaf': i.is_leaf(),
'label': i.label,
'index': ndct,
'leaf_index': None}
# node_ref_file.write(i.label + '\t' + str(ndct) + '\n') #DEBUG
self.node_refs.append(i)
pts_leaves_bln.append(i.is_leaf())
i.index = ndct
if i.is_leaf() == True:
pts[i.label]['leaf_index'] = lct
lct += 1
leaf_order.append(i.label)
ndct += 1
node_order.append(i.label)
# node_ref_file.close() #DEBUG
ptstup = tuple(pts_lst_np)
pts_nparr = np.vstack(ptstup)
tri = spat.Delaunay(pts_nparr)
#DEBUG
# tri_indices_file = 'C:\\Users\\miken\\Dropbox\\Grad School\\Phylogenetics\\work\\phylostrator-testing\\tri_simplices_list.txt'
# np.savetxt(tri_indices_file,tri.simplices,delimiter = '\t')
return ndct, node_order, pts, pts_leaves_bln, tri, pts_nparr
def relocate_subtree_by_wedge_properties(self,node,wedge_angle=None):
'''
:param node:
:return:
'''
preo = node.preorder_iter()
nd = preo.__next__()
temp_nu={}
temp_nu[nd.id]=nd.right_wedge_border
for i in preo:
i.wedge_angle = i.percent_of_parent_wedge*i.parent_node.wedge_angle
i.right_wedge_border = temp_nu[i.parent_node.id]
temp_nu[i.parent_node.id]+=i.wedge_angle
temp_nu[i.id]=i.right_wedge_border
i.edge_segment_angle = i.right_wedge_border + i.wedge_angle/2
xu = i.parent_node.location
delta = i.edge_length
x1 = xu[0] + delta * math.cos(i.edge_segment_angle)
x2 = xu[1] + delta * math.sin(i.edge_segment_angle)
i.location=(x1,x2)
i.deflect_angle = i.edge_segment_angle-i.parent_node.edge_segment_angle
def get_de_facto_spread_angle(self,node):
'''
:param node:
:return:
'''
start = node.parent_node.location
angles = []
for i in node.leaf_iter():
xi = i.location
ang = math.atan2(xi[1]-start[1],xi[0]-start[0])
if ang < 0:
ang = 2*math.pi - ang
angles.append(ang)
return min(angles), max(angles)
# def angle_spread_extension(self):
# pass
def get_max_dims(self):
xma=float(0)
xmi=float(0)
yma=float(0)
ymi=float(0)