-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_cell_position.py
executable file
·304 lines (219 loc) · 8.86 KB
/
generate_cell_position.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
#!/usr/bin/env python
"""generate_cell_position.py
Jobs = mf, goc, glo, grc
Usage:
generate_cell_position.py (-o PATH) (-p PATH) [-i PATH] [--stop_method=<conditions>] (all | <jobs>...)
generate_cell_position.py (-h | --help)
generate_cell_position.py --version
Options:
-h --help Show this screen
--version Show version
-o PATH, --output_path=<output_path> Output path
-p PATH, --param_path=<param_dir> Params path
-i PATH, --input_path=<input_path> Input path
--stop_method=<conditions> Stop method. See below.
With <conditions>, you can specify how each job will stop. For example,
`--stop_method=grc:maximal,goc:density`
will make the grc and goc generation use the maximal volume filling and density-based
stopping criterion, respectively. The default is the density-based, e.g., the job will
stop if the number of the cells reaches a target, computed from the given density.
Written by Sanghun Jee and Sungho Hong
Supervised by Erik De Schutter
Computational Neuroscience Unit,
Okinawa Institute of Science and Technology
March, 2020
"""
import numpy as np
import pycabnn
from pycabnn.util import HocParameterParser
from pycabnn.pop_generation.ebeida import ebeida_sampling
from pycabnn.pop_generation.utils import PointCloud
valid_job_list = ["mf", "goc", "glo", "grc"]
def load_input_data(args):
from pathlib import Path
param_file = Path(args["--param_path"]) / "Parameters.hoc"
h = HocParameterParser()
h.load_file(str(param_file))
# Limit the x-range to 700 um and add 50 um in all directions
h.MFxrange += h.range_margin
h.MFyrange += h.range_margin
h.GLdepth += h.range_margin
output_path = Path(args["--output_path"])
foutname = output_path / "cell_positions.npz"
data = {"h": h, "foutname": foutname, "output_path": output_path}
if args["--input_path"]:
existing_data = np.load(args["--input_path"])
for c in existing_data:
data[c + "_points"] = existing_data[c]
return data
def make_mf(data):
h = data["h"]
foutname = data["foutname"]
stop_cond = data['stop_conds']['mf']
def compute_mf_params():
Transverse_range = h.MFyrange
Horizontal_range = h.MFxrange
Vertical_range = h.GLdepth
Volume = Transverse_range * Horizontal_range * Vertical_range
MFdensity = h.MFdensity
Xinstantiate = h.MFxextent
Yinstantiate = h.MFyextent
n_mf = int(
(Transverse_range + (2 * Xinstantiate))
* (Horizontal_range + (2 * Yinstantiate))
* MFdensity
* 1e-6
)
print("Target # MFs = {}".format(n_mf))
return (
(
Horizontal_range + (2 * Yinstantiate),
Transverse_range + (2 * Xinstantiate),
),
n_mf,
)
mf_box, n_mf = compute_mf_params()
mf_points = ebeida_sampling(mf_box, h.spacing_mf, n_mf, True, stop_method=stop_cond)
data["mf_points"] = mf_points
print("Final # MFs = {}".format(mf_points.shape[0]))
np.savez(foutname, mf=mf_points)
np.savetxt(data["output_path"] / "MFcoordinates.dat", data["mf_points"])
return data
def make_goc(data):
h = data["h"]
foutname = data["foutname"]
stop_cond = data['stop_conds']['goc']
def compute_goc_params():
Transverse_range = h.MFyrange
Horizontal_range = h.MFxrange
Vertical_range = h.GLdepth
Volume = Transverse_range * Horizontal_range * Vertical_range
d_goc = h.GoCdensity
n_goc = int(d_goc * Volume * 1e-9)
print("Target # GoCs = {}".format(n_goc))
return ((Horizontal_range, Transverse_range, Vertical_range), n_goc)
goc_box, n_goc = compute_goc_params()
spacing_goc = h.spacing_goc - h.softness_margin_goc
goc_points = ebeida_sampling(goc_box, spacing_goc, n_goc, True, stop_method=stop_cond)
goc_points = (
goc_points
+ np.random.normal(0, 1, size=(len(goc_points), 3)) * h.softness_margin_goc
) # Gaussian noise
data["goc_points"] = goc_points
print("Final # GoCs = {}".format(goc_points.shape[0]))
np.savez(foutname, mf=data["mf_points"], goc=goc_points)
np.savetxt(data["output_path"] / "GoCcoordinates.dat", data["goc_points"])
return data
def make_glo(data):
h = data["h"]
foutname = data["foutname"]
stop_cond = data['stop_conds']['glo']
goc_points = data["goc_points"]
scale_factor = h.scale_factor_glo
spacing_glo = h.diam_glo - h.softness_margin_glo
# minimal distance between GoCs and glomeruli
# softness margin is applied only to glo since only the glo coords will be
# perturbed
d_goc_glo = h.diam_goc / 2 + h.diam_glo / 2 - h.softness_margin_glo
class GoC(PointCloud):
def test_points(self, x):
y = x.copy()
y[:, 1] = y[:, 1] / scale_factor
return super().test_points(y)
def test_cells(self, cell_corners, dgrid, nn=None):
y = cell_corners.copy()
y[:, 1] = y[:, 1] / scale_factor
return super().test_cells(y, dgrid, nn=nn)
goc = GoC(goc_points, d_goc_glo)
goc.dlat[:, 1] = goc.dlat[:, 1] / scale_factor
def compute_glo_params():
Transverse_range = h.MFyrange
Horizontal_range = h.MFxrange
Vertical_range = h.GLdepth
Volume = Transverse_range * Horizontal_range * Vertical_range
d_glo = h.density_glo
n_glo = int(d_glo * Volume * 1e-9)
print("Target # Glomeruli = {}".format(n_glo))
return (
(
Horizontal_range,
int(Transverse_range * scale_factor + 0.5),
Vertical_range,
),
n_glo,
)
globox, n_glo = compute_glo_params()
glo_points = ebeida_sampling(globox, spacing_glo, n_glo, True, ftests=[goc],stop_method=stop_cond)
# Since the glomerulus distribution is stretched in a sagittal direction,
# we generate the coordinates in a scaled volume first, and then stretch
# the sagittal coordinate by 3
glo_points1 = (
glo_points
+ np.random.normal(0, 1, size=glo_points.shape) * h.softness_margin_glo
)
glo_points1[:, 1] = glo_points1[:, 1] / scale_factor
data["glo_points"] = glo_points1
print("Final # Glos = {}".format(glo_points1.shape[0]))
np.savez(
foutname, mf=data["mf_points"], goc=data["goc_points"], glo=data["glo_points"]
)
np.savetxt(data["output_path"] / "GLpoints.dat", data["glo_points"])
return data
def make_grc(data):
h = data["h"]
foutname = data["foutname"]
stop_cond = data['stop_conds']['grc']
goc_points = data["goc_points"]
glo_points = data["glo_points"]
# minimal distance between gocs and grcs
d_goc_grc = h.diam_goc / 2 + h.diam_grc / 2 - h.softness_margin_grc
goc = PointCloud(goc_points, d_goc_grc)
# minimal distance between glomeruli and grcs
d_glo_grc = h.diam_glo / 2 + h.diam_grc / 2 - h.softness_margin_grc
glo = PointCloud(glo_points, d_glo_grc)
def compute_grc_params():
Transverse_range = h.MFyrange
Horizontal_range = h.MFxrange
Vertical_range = h.GLdepth
Volume = Transverse_range * Horizontal_range * Vertical_range
d_grc = h.density_grc
n_grc = int(d_grc * Volume * 1e-9)
print("Target # GrCs = {}".format(n_grc))
return ((Horizontal_range, Transverse_range, Vertical_range), n_grc)
spacing_grc = h.diam_grc - h.softness_margin_grc
grcbox, n_grc = compute_grc_params()
grc_points = ebeida_sampling(grcbox, spacing_grc, n_grc, True, ftests=[glo, goc],stop_method=stop_cond)
grc_points = np.random.normal(0, 1, size=grc_points.shape) * h.softness_margin_grc
data["grc_points"] = grc_points
print("Final # GrCs = {}".format(grc_points.shape[0]))
np.savez(
foutname,
mf=data["mf_points"],
goc=data["goc_points"],
glo=data["glo_points"],
grc=data["grc_points"],
)
np.savetxt(data["output_path"] / "GCcoordinates.dat", data["grc_points"])
return data
def main(args):
data = load_input_data(args)
if args["all"]:
args["<jobs>"] = valid_job_list
if args['--stop_method']:
conds = args['--stop_method'].split(',')
conds = [c.split(':') for c in conds]
conds = [(x.strip(), y.strip()) for x, y in conds]
data['stop_conds'] = dict(conds)
for j in args["<jobs>"]:
if j not in data['stop_conds']:
data['stop_conds'][j] = 'density'
if j not in valid_job_list:
raise RuntimeError(
"Job {} is not valid, not in {}".format(j, valid_job_list)
)
else:
data = eval("make_" + j)(data)
if __name__ == "__main__":
from docopt import docopt
args = docopt(__doc__, version=pycabnn.__version__)
main(args)