1
1
import rsml
2
2
from rsml import data
3
3
4
- from openalea .mtg import traversal
4
+ from openalea .mtg import traversal , algo
5
+
5
6
import networkx as nx
6
7
import numpy as np
7
8
@@ -22,7 +23,8 @@ def distance_polyline(p1, p2):
22
23
closest_point = p1 [closest_index ]
23
24
# Distance to the closest point
24
25
distance = np .linalg .norm (closest_point - point_p2 )
25
- #print("Distance to the closest point ", distance)
26
+ if distance > 5 :
27
+ print ("Distance to the closest point " , distance )
26
28
27
29
return closest_index
28
30
@@ -37,19 +39,19 @@ def convert_fine_mtg(fn):
37
39
geometry = g .property ('geometry' )
38
40
time = g .property ('time' )
39
41
time_hours = g .property ('time_hours' )
42
+ diameters = g .property ('diameters' )
40
43
41
44
indexes = {}
42
45
43
46
g2 = g .copy ()
44
47
for pid in plants :
45
48
aid = next (g .component_roots_iter (pid ))
46
49
for axis_id in traversal .pre_order2 (g , vtx_id = aid ):
47
- print (axis_id )
48
50
poly = geometry [axis_id ]
49
51
time_v = time [axis_id ]
50
52
time_hours_v = time_hours [axis_id ]
53
+ #diams = diameters[axis_id]
51
54
52
- count = 0
53
55
if g .parent (axis_id ) is None :
54
56
# create the axis at segment level
55
57
vid = g2 .add_component (complex_id = axis_id , label = 'Segment' ,
@@ -88,14 +90,48 @@ def convert_fine_mtg(fn):
88
90
indexes [axis_id ].append (vid )
89
91
return g2
90
92
93
+ def split (g ):
94
+ return algo .split (g )
95
+
96
+ def convert_nx (g ):
97
+
98
+ max_scale = g .max_scale ()
99
+ root_id = next (g .component_roots_at_scale_iter (g .root , scale = max_scale ))
100
+
101
+ edge_list = []
102
+ nodes = list (traversal .pre_order2 (g , vtx_id = root_id ))
103
+ for v in nodes :
104
+ parent = g .parent (v )
105
+ if parent is None :
106
+ continue
107
+
108
+ edge_list .append ((parent , v ))
109
+
110
+ g_nx = nx .from_edgelist (edge_list , create_using = nx .DiGraph )
91
111
92
-
112
+ props = ['x' , 'y' , 'time' , 'time_hours' , 'diameters' , 'label' ]
113
+ for node in nodes :
114
+ for prop in props :
115
+ _prop = g .property (prop )
116
+ if node in _prop :
117
+ g_nx .nodes [node ][prop ] = _prop .get (node )
93
118
119
+ return g_nx
94
120
95
121
96
122
123
+ def test_all ():
124
+ """
125
+ Test the conversion of the MTG to a fine MTG and then to a networkx graph.
126
+ """
127
+ g = convert_fine_mtg (fn )
128
+
129
+ gs = split (g )
130
+ dgs = [convert_nx (g ) for g in gs ]
131
+ return dgs
132
+
133
+
97
134
98
-
99
135
100
136
101
137
0 commit comments