-
Notifications
You must be signed in to change notification settings - Fork 2
/
root_gen_v1.py
394 lines (343 loc) · 15.5 KB
/
root_gen_v1.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
#!/usr/bin/python
##########################################################################################################
### Root Generation Version 1
# This is the initial implementation of the synthetic root generation script.
# Several additional features and alterations were suggested following consultation.
# The suggestions were drastic enough that it was therefore decided to version the script.
##########################################################################################################
##########################################################################################################
### Imports
##########################################################################################################
import argparse
import math
import numpy as np
import os
import pandas as pd
import random
from numpy.random import uniform
##########################################################################################################
### Parameters
##########################################################################################################
parser = argparse.ArgumentParser(
description = 'Simulate a root system.'
)
def _add_argument(parser: argparse.ArgumentParser, name: str, default: str, arg_help: str, type = str) -> None:
parser.add_argument(
name,
default=default,
help=f"{arg_help}. Defaults to '{default}'.",
type=type
)
# Ouput
_add_argument(parser, "--dir", "data/root_sim", "Output directory")
_add_argument(parser, "--out", "root_sim.csv", "Output CSV file name")
_add_argument(parser, "--round", 4, "Number of decimal places to round values when writing to file", int)
_add_argument(parser, "--stats", 1, "Calculate statistics for the root system", int)
_add_argument(parser, "--bins", 10, "Number of bins when binning root data", int)
# Roots
_add_argument(parser, "--order", 2, "The maximum root order", int)
## Primary
_add_argument(parser, "--rnum_out", 10, "The number of outer primary roots to be generated", int)
_add_argument(parser, "--rnum_in", 5, "The number of inner primary roots to be generated", int)
### Size
_add_argument(parser, "--min_prlength", 200, "The minimum length of each primary root in centimetres", float)
_add_argument(parser, "--max_prlength", 300, "The maximum length of each primary root in centimetres", float)
### In ArchiSimple model, root diameter is in metres ###
_add_argument(parser, "--min_pdiam", 0.009, "The minimum diameter of primary roots in millimetres", float)
_add_argument(parser, "--max_pdiam", 0.012, "The maximum diameter of primary roots in millimetres", float)
## Secondary
_add_argument(parser, "--srnum_min", 0, "The minimum number of secondary roots to be generated", int)
_add_argument(parser, "--srnum_max", 5, "The maximum number of secondary roots to be generated", int)
### Size
_add_argument(parser, "--min_srlength", 25, "The minimum length of each secondary root in centimetres", float)
_add_argument(parser, "--max_srlength", 100, "The maximum length of each secondary root in centimetres", float)
### In ArchiSimple model, root diameter is in metres ###
_add_argument(parser, "--min_sdiam", 0.004, "The minimum diameter of secondary roots in millimetres", float)
_add_argument(parser, "--max_sdiam", 0.007, "The maximum diameter of secondary roots in millimetres", float)
# Segments
_add_argument(parser, "--snum", 50, "The number of segments per root", int)
_add_argument(parser, "--fix_seg", 1, "Use a fixed segment size for each root", int)
## Size
_add_argument(parser, "--min_pslength", 0.05, "The minimum length of each primary root segment in centimetres", float)
_add_argument(parser, "--max_pslength", 0.1, "The maximum length of each primary root segment in centimetres", float)
_add_argument(parser, "--min_sslength", 0.015, "The minimum length of each secondary root segment in centimetres", float)
_add_argument(parser, "--max_sslength", 0.025, "The maximum length of each secondary root segment in centimetres", float)
# Movement
_add_argument(parser, "--xy_vary", 5, "Random variation in degrees of subsequent segments along x and y axes", int)
_add_argument(parser, "--z_vary", 5, "Random variation in degrees of subsequent segments along z axis", int)
args = parser.parse_args()
##########################################################################################################
### Constants
##########################################################################################################
# Ouput
DIR = args.dir
OUT = f"{DIR}/{args.out}"
ROUND = args.round
CALC_STATS = bool(args.stats)
STATS_OUT = f"{DIR}/root_stats.csv"
BINS = args.bins
# Roots
MAX_ORDER = args.order
## Primary
OUT_ROOTNUM = args.rnum_out
IN_ROOTNUM = args.rnum_in
### Size
MIN_PRLENGTH = args.min_prlength
MAX_PRLENGTH = args.max_prlength
MIN_PDIAMETER = args.min_pdiam
MAX_PDIAMETER = args.max_pdiam
## Secondary
MIN_SRNUM = args.srnum_min
MAX_SRNUM = args.srnum_max
### Size
MIN_SRLENGTH = args.min_srlength
MAX_SRLENGTH = args.max_srlength
MIN_SDIAMETER = args.min_sdiam
MAX_SDIAMETER = args.max_sdiam
# Segments
SEGNUM = args.snum
SEGNUM_FRACTION = int(SEGNUM / 3)
SEGNUM_INTERVAL = int(SEGNUM * 0.1)
FIXED_SEG_LENGTH = bool(args.fix_seg)
## Size
MIN_PSLENGTH = args.min_pslength
MAX_PSLENGTH = args.max_pslength
MIN_SSLENGTH = args.min_sslength
MAX_SSLENGTH = args.max_sslength
# Movement
XY_VARY = args.xy_vary
Z_VARY = args.z_vary
# Set origin point of root system
# The base node is located at origin (0, 0, 0)
# We include a small deviation from this origin for all child nodes to prevent their dimensions from being exactly 0
origin = 1E-1
START_X = float(origin)
START_Y = float(origin)
START_Z = float(origin)
##########################################################################################################
### Main
##########################################################################################################
def get_cart_coords(theta : int, adj_length: float) -> tuple:
"""Calculate the cartesian coordinates of the new segment"""
if (0.0 < theta < 90.0):
x = math.cos(math.radians(theta)) * adj_length
y = math.sin(math.radians(theta)) * adj_length
elif theta == 90.0:
x = 0
y = adj_length
elif (90.0 < theta < 180.0):
theta = 180.0 - theta
x = -(math.cos(math.radians(theta)) * adj_length)
y = math.sin(math.radians(theta)) * adj_length
elif theta == 180.0:
x = -adj_length
y = 0
elif (180.0 < theta < 270.0):
theta = theta - 180.0
x = -(math.cos(math.radians(theta)) * adj_length)
y = -(math.sin(math.radians(theta)) * adj_length)
elif theta == 270.0:
x = 0
y = -adj_length
elif (270.0 < theta < 360.0):
theta = 360.0 - theta
x = math.cos(math.radians(theta)) * adj_length
y = -(math.sin(math.radians(theta)) * adj_length)
else:
x = adj_length
y = 0
return x, y
def generate_roots(rows: list, order: int, start_angle: float, root: int,
phi_angles: int, min_length : float, max_length : float, start_x : float,
start_y: float, start_z: float, diameters, root_bend = {},
parent = 1) -> None:
"""Generate random root system"""
theta_angles = [root * start_angle] #Seperate roots by equal angles at start
root_coords = [{"x" : start_x, "y" : start_y, "z" : start_z}]
if FIXED_SEG_LENGTH:
r_length = uniform(min_length, max_length)
seg_length = r_length / SEGNUM
# Override default angle variance for specified root segments
root_bend_intervals = {}
for segment in root_bend.keys():
for interval_key in range(segment - SEGNUM_INTERVAL, segment + SEGNUM_INTERVAL + 1):
root_bend_intervals[interval_key] = root_bend[segment]
# Randomly select start and end root segment diameters
diameter_diff = 0
while diameter_diff < abs(diameters[1] - diameters[0]) * 0.15:
# Sort diameters from largest to smallest
root_diameters = -np.sort(-uniform(diameters[0], diameters[1], 2))
diameter_diff = abs(root_diameters[1] - root_diameters[0])
# Apply linear interpolation between start and end segment diameters and add random noise
diameter_diff *= 0.025
# Sort random noise from largest to smallest as the root should become progressively become thinner
diameter_noise = -np.sort(-uniform(-diameter_diff, diameter_diff, SEGNUM))
diameters = np.interp(range(1, SEGNUM + 1), [1, SEGNUM], root_diameters) + diameter_noise
for segment in range(0, SEGNUM):
if segment not in root_bend_intervals:
#Vary the angle of subsequent segments by +-Z degrees in horizontal and vertical directions
phi = random.randint(-Z_VARY, Z_VARY)
# if phi > 0:
# phi = -phi
else:
root_bend_interval = root_bend_intervals[segment]
phi = random.randint(root_bend_interval[0], root_bend_interval[1])
phi += phi_angles[segment]
phi_angles.append(phi)
if FIXED_SEG_LENGTH is False:
seg_length = uniform(min_length, max_length)
z = math.sin(math.radians(phi)) * seg_length
adj_length = math.cos(math.radians(phi)) * seg_length
theta = random.randint(-XY_VARY, XY_VARY) + theta_angles[segment]
if theta > 360:
theta = theta - 360
theta_angles.append(theta)
x, y = get_cart_coords(theta, adj_length)
current_coord = root_coords[segment]
# Add a small amount of random noise to coordinates to prevent identical values
noise = uniform(0.0001, 0.00011)
new_coord = {
"x" : current_coord["x"] + x + noise,
"y" : current_coord["y"] + y + noise,
"z" : current_coord["z"] + z + noise
}
#if new_coord["z"] > 0:
# new_coord["z"] = new_coord["z"] - new_coord["z"] - uniform(0.1, 1)
# if phi_angles[segment + 1] > 0:
# phi_angles[segment + 1] = -phi_angles[segment + 1]
root_coords.append(new_coord)
if segment == 0:
parent_root = parent
else:
parent_root = root * SEGNUM + segment + 1
row = {
"organ_id" : root + 1,
"order": order,
"segment_rank" : segment + 1,
"parent": parent_root ,
"coordinates" : ' '.join([str(round(value, ROUND)) for value in current_coord.values()]),
"diameter" : round(diameters[segment], ROUND),
"length" : round(seg_length, ROUND),
"x": current_coord["x"],
"y": current_coord["y"],
"z": current_coord["z"],
}
rows.append(row)
def get_parent_coords(rows: list, root: int, segnum : int, n_roots: int, floor = 0.9, ceiling = 0.05) -> list:
"""Sample segments from the parent root and return their coordinates."""
parent_root_idx = (root + 1) * segnum
#parent_segments = rows[int(parent_root_idx - segnum * floor) : int(parent_root_idx - segnum * ceiling)]
#segment_samples = random.sample(parent_segments, n_roots)
segment_indices = random.sample(range(int(parent_root_idx - segnum * floor), int(parent_root_idx - segnum * ceiling)), n_roots)
parent_segments = np.asarray(rows)
segment_samples = parent_segments[segment_indices]
coords = []
for segment in segment_samples:
start_coordinates = segment["coordinates"].split(" ")
start_coordinates = [float(coord) for coord in start_coordinates]
coords.append(start_coordinates)
return segment_indices, coords
def get_df_coords(df: pd.DataFrame, col: str) -> pd.DataFrame:
"""Get 3D coordinates from dataframe"""
return (df[col]
.str
.split(' ', expand = True)
.astype(float)
.rename(columns = {0 : "x", 1 : "y", 2 : "z"}))
def root_cum_sum(coord: np.array, bins = 10) -> np.array:
"""Compute the cumulative distribution function for root coordinates."""
count, bins_count = np.histogram(coord, bins = bins)
bins_count = np.insert(bins_count, 0, 0)
pdf = count / sum(count)
cdf = np.cumsum(pdf)
cdf = np.insert(cdf, 0, 0)
return cdf, bins_count[1:]
def calc_root_stats(df : pd.DataFrame, out : str) -> None:
"""Calculate root statistics."""
coords = get_df_coords(df, "coordinates")
depth = abs(coords.z)
horizontal = abs(coords.melt(value_vars=["x", "y"]).value)
depth_density, depth_bin = root_cum_sum(depth, BINS)
horizontal_density, horizontal_bin = root_cum_sum(horizontal, BINS)
stats_df = pd.DataFrame({
"depth_cum": depth_density,
"depth_bin": depth_bin,
"horizontal_cum": horizontal_density,
"horizontal_bin": horizontal_bin
})
stats_df.to_csv(out, index = False)
print(f"Stats file written to {out}")
def main() -> None:
"""Generate random root structure"""
print("Generating random root structure...\n")
rows = [{
"organ_id" : 0,
"order": 0,
"segment_rank" : 0,
"parent": -1,
"coordinates" : '0 0 0',
"diameter" : MAX_PDIAMETER,
"length" : MAX_PSLENGTH,
"x": 0,
"y": 0,
"z": 0
}]
# Primary roots
## Outer
start_angle = 360 / OUT_ROOTNUM
end = OUT_ROOTNUM
root_bend = {
SEGNUM_FRACTION : [-Z_VARY, 0],
2 * SEGNUM_FRACTION : [0, Z_VARY]
}
if FIXED_SEG_LENGTH:
min_length, max_length = MIN_PRLENGTH, MAX_PRLENGTH
else:
min_length, max_length = MIN_PSLENGTH, MAX_PSLENGTH
for root in range(0, end):
phi_angles = [-(random.randint(10, 15))] #Generates starting downwards angle of the root
generate_roots(rows, 1, start_angle, root, phi_angles, min_length, max_length,
START_X, START_Y, START_Z, [MIN_PDIAMETER, MAX_PDIAMETER], root_bend = {})
## Inner
start_angle = 360 / IN_ROOTNUM
start = end
end += IN_ROOTNUM
for root in range(start, end):
phi_angles = [-random.randint(20, 90)]
root_bend = None
if phi_angles[0] < 60:
root_bend = { SEGNUM_FRACTION : [0, Z_VARY] }
generate_roots(rows, 1, start_angle, root, phi_angles, min_length, max_length,
START_X, START_Y, START_Z, [MIN_PDIAMETER, MAX_PDIAMETER], root_bend = {})
# Secondary roots
roots = list(range(0, end))
if FIXED_SEG_LENGTH:
min_length, max_length = MIN_SRLENGTH, MAX_SRLENGTH
else:
min_length, max_length = MIN_SSLENGTH, MAX_SSLENGTH
for order in range(2, MAX_ORDER + 1):
secondary_roots = []
for root in roots:
n_roots = random.randint(MIN_SRNUM, MAX_SRNUM)
if n_roots == 0:
continue
parent_indices, parent_coords = get_parent_coords(rows, root, SEGNUM, n_roots)
start_angle = 360 / n_roots
start = end
end += n_roots
for i, secondary_root in enumerate(range(start, end)):
secondary_roots.append(secondary_root)
parent_coord = parent_coords[i]
phi_angles = [-(random.randint(0, 360))]
generate_roots(rows, order, start_angle, secondary_root, phi_angles, min_length, max_length,
parent_coord[0], parent_coord[1], parent_coord[2], [MIN_SDIAMETER, MAX_SDIAMETER], parent = parent_indices[i])
roots = secondary_roots
os.makedirs(DIR, exist_ok = True)
df = pd.DataFrame(rows)
df.index += 1
df.to_csv(OUT, index = True, index_label = "id")
print(f"File written to {OUT}")
if CALC_STATS:
calc_root_stats(df, STATS_OUT)
if __name__ == "__main__":
main()