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 | class BuiltinBackend(NblastBackend):
"""The built-in multiprocessing NBLAST backend."""
name = "builtin"
priority = 0
def available(self):
# Always available - it's pure navis
return True
def unsupported(self, operation, **params):
# The built-in backend supports every operation and parameter
return super().unsupported(operation)
# ------------------------------------------------------------------ #
# Shared helpers
# ------------------------------------------------------------------ #
def _map(self, jobs, n_cores, progress, desc="NBLASTing"):
"""Run `jobs` and yield ``(job, result)`` tuples.
Each *job* is an ``NBlaster`` carrying the picklable attributes read by
:func:`_run_job` (``_op``, ``_scores`` and the relevant index arrays).
With a single job (or a single core) work runs inline; otherwise it is
spread across a spawn-based process pool.
Override this method to swap in a different dispatcher (joblib, threads,
serial, ...) while reusing the partitioning and stitching logic.
"""
from ..nblast_funcs import set_omp_flag, OMP_NUM_THREADS_LIMIT
multicore = bool(n_cores and n_cores > 1 and len(jobs) > 1)
# Avoid multiple layers of concurrency (see pykdtree/OMP notes)
with set_omp_flag(limits=OMP_NUM_THREADS_LIMIT if (n_cores and n_cores > 1) else None):
if not multicore:
for this in jobs:
yield this, _run_job(this)
return
# Note that we're forcing "spawn" instead of "fork" (default on
# linux) to reduce the memory footprint: "fork" appears to inherit
# all variables (including all neurons) while "spawn" gets only
# what's required to run the job.
with ProcessPoolExecutor(max_workers=n_cores,
mp_context=mp.get_context('spawn')) as pool:
futures = {}
for this in jobs:
this.progress = False # no per-block progress bars
futures[pool.submit(_run_job, this)] = this
# We drop the "N / N_total" bit from the progress bar because
# it's not helpful here.
fmt = '{desc}: {percentage:3.0f}%|{bar}| [{elapsed}<{remaining}]'
for f in config.tqdm(as_completed(futures),
desc=desc,
bar_format=fmt,
total=len(futures),
smoothing=0,
disable=not progress,
leave=False):
yield futures[f], f.result()
def _make_blaster(self, use_alpha, normalized, smat, limit_dist, precision,
approx_nn, progress, smat_kwargs):
from ..nblast_funcs import NBlaster
return NBlaster(use_alpha=use_alpha,
normalized=normalized,
smat=smat,
limit_dist=limit_dist,
dtype=precision,
approx_nn=approx_nn,
progress=progress,
smat_kwargs=smat_kwargs)
def _partition(self, q, t, n_cores, progress, aba=False):
"""Find (n_rows, n_cols) partition of the query/target matrix."""
from ..nblast_funcs import (find_batch_partition, find_optimal_partition,
JOB_SIZE_MULTIPLIER, JOB_MAX_TIME_SECONDS)
if not (n_cores and n_cores > 1):
return 1, 1
if progress:
# If progress bar, we need to make smaller mini batches. These mini
# jobs must not be too small - otherwise the overhead from spawning
# and sending results between processes slows things down
# dramatically. Hence we want each job to run for >10s. The run time
# depends on the system and how big the neurons are, so we run a
# quick test and extrapolate.
return find_batch_partition(q, t, T=10 * JOB_SIZE_MULTIPLIER)
# No progress bar: for a plain query->target NBLAST we aim for each
# batch to finish in a certain amount of time (to avoid stragglers);
# for all-by-all we just split evenly across cores.
if aba:
return find_optimal_partition(n_cores, q, t)
n_rows, n_cols = find_batch_partition(q, t, T=JOB_MAX_TIME_SECONDS)
if (n_rows * n_cols) < n_cores:
n_rows, n_cols = find_optimal_partition(n_cores, q, t)
return n_rows, n_cols
# ------------------------------------------------------------------ #
# Operations
# ------------------------------------------------------------------ #
def nblast(self, query, target, *, scores, normalized, use_alpha, smat,
limit_dist, approx_nn, precision, n_cores, progress, smat_kwargs):
"""Query -> target NBLAST."""
query_dps, target_dps = query, target
n_rows, n_cols = self._partition(query_dps, target_dps, n_cores, progress)
# Calculate self-hits once for all neurons
nb = self._make_blaster(use_alpha, normalized, smat, limit_dist,
precision, approx_nn, progress, smat_kwargs)
query_self_hits = np.array([nb.calc_self_hit(n) for n in query_dps])
target_self_hits = np.array([nb.calc_self_hit(n) for n in target_dps])
# Build one blaster per block of the score matrix
jobs = []
with config.tqdm(desc='Preparing', total=n_rows * n_cols, leave=False,
disable=not progress) as pbar:
for qix in np.array_split(np.arange(len(query_dps)), n_rows):
for tix in np.array_split(np.arange(len(target_dps)), n_cols):
this = self._make_blaster(use_alpha, normalized, smat,
limit_dist, precision, approx_nn,
progress, smat_kwargs)
for ix in qix:
this.append(query_dps[ix], query_self_hits[ix])
for ix in tix:
this.append(target_dps[ix], target_self_hits[ix])
this.queries = np.arange(len(qix))
this.targets = np.arange(len(tix)) + len(qix)
this.queries_ix = qix
this.targets_ix = tix
this.pbar_position = len(jobs) if not utils.is_jupyter() else None
this._op = 'multi_query_target'
this._scores = scores
jobs.append(this)
pbar.update()
# Single block: return its labeled DataFrame directly
if len(jobs) == 1:
(this, res), = self._map(jobs, n_cores, progress)
return res
# Multiple blocks: stitch results into the big matrix
out = pd.DataFrame(np.empty((len(query_dps), len(target_dps)),
dtype=nb.dtype),
index=query_dps.id, columns=target_dps.id)
out.index.name = 'query'
out.columns.name = 'target'
for this, res in self._map(jobs, n_cores, progress):
out.iloc[this.queries_ix, this.targets_ix] = res.values
return out
def nblast_allbyall(self, x, *, normalized, use_alpha, smat, limit_dist,
approx_nn, precision, n_cores, progress, smat_kwargs):
"""All-by-all NBLAST (always forward scores)."""
dps = x
n_rows, n_cols = self._partition(dps, dps, n_cores, progress, aba=True)
# Calculate self-hits once for all neurons
nb = self._make_blaster(use_alpha, normalized, smat, limit_dist,
precision, approx_nn, progress, smat_kwargs)
self_hits = np.array([nb.calc_self_hit(n) for n in dps])
jobs = []
with config.tqdm(desc='Preparing', total=n_rows * n_cols, leave=False,
disable=not progress) as pbar:
for qix in np.array_split(np.arange(len(dps)), n_rows):
for tix in np.array_split(np.arange(len(dps)), n_cols):
this = self._make_blaster(use_alpha, normalized, smat,
limit_dist, precision, approx_nn,
progress, smat_kwargs)
# Make sure we don't add the same neuron twice
to_add = list(set(qix) | set(tix))
ixmap = {}
for i, ix in enumerate(to_add):
this.append(dps[ix], self_hits[ix])
ixmap[ix] = i
this.queries = [ixmap[ix] for ix in qix]
this.targets = [ixmap[ix] for ix in tix]
this.queries_ix = qix
this.targets_ix = tix
this.pbar_position = len(jobs) if not utils.is_jupyter() else None
this._op = 'multi_query_target'
this._scores = 'forward'
jobs.append(this)
pbar.update()
if len(jobs) == 1:
return jobs[0].all_by_all()
out = pd.DataFrame(np.empty((len(dps), len(dps)), dtype=nb.dtype),
index=dps.id, columns=dps.id)
out.index.name = 'query'
out.columns.name = 'target'
for this, res in self._map(jobs, n_cores, progress):
out.iloc[this.queries_ix, this.targets_ix] = res.values
return out
def nblast_smart(self, query, target, *, aba, t, criterion, scores,
return_mask, normalized, use_alpha, smat, limit_dist,
approx_nn, precision, n_cores, progress, smat_kwargs):
"""Smart(er) NBLAST: pre-NBLAST on simplified dotprops, then full."""
query_dps, target_dps = query, target
pre_scores = scores
# For all-by-all's we can compute only forward scores during the
# pre-NBLAST and produce the mean later.
if aba and scores == 'mean':
pre_scores = 'forward'
try:
t = int(t)
except BaseException:
raise TypeError(f'`t` must be (convertable to) integer - got "{type(t)}"')
if criterion == 'percentile':
if (t <= 0 or t >= 100):
raise ValueError('Expected `t` to be integer between 0 and 100 for '
f'criterion "percentile", got {t}')
elif criterion == 'N':
if (t < 0 or t > len(target_dps)):
raise ValueError('`t` must be between 0 and the total number of '
f'targets ({len(target_dps)}) for criterion "N", '
f'got {t}')
# Make simplified dotprops
query_dps_simp = query_dps.downsample(10, inplace=False)
if not aba:
target_dps_simp = target_dps.downsample(10, inplace=False)
else:
target_dps_simp = query_dps_simp
# --- Pre-NBLAST on simplified dotprops --- #
n_rows, n_cols = self._partition(query_dps_simp, target_dps_simp,
n_cores, progress)
nb = self._make_blaster(use_alpha, normalized, smat, limit_dist,
precision, approx_nn, progress, smat_kwargs)
query_self_hits = np.array([nb.calc_self_hit(n) for n in query_dps_simp])
target_self_hits = np.array([nb.calc_self_hit(n) for n in target_dps_simp])
jobs = []
with config.tqdm(desc='Prep. pre-NBLAST', total=n_rows * n_cols,
leave=False, disable=not progress) as pbar:
for qix in np.array_split(np.arange(len(query_dps_simp)), n_rows):
for tix in np.array_split(np.arange(len(target_dps_simp)), n_cols):
this = self._make_blaster(use_alpha, normalized, smat,
limit_dist, precision, approx_nn,
progress, smat_kwargs)
for ix in qix:
this.append(query_dps_simp[ix], query_self_hits[ix])
for ix in tix:
this.append(target_dps_simp[ix], target_self_hits[ix])
this.queries = np.arange(len(qix))
this.targets = np.arange(len(tix)) + len(qix)
this.queries_ix = qix
this.targets_ix = tix
this.pbar_position = len(jobs) if not utils.is_jupyter() else None
this._op = 'multi_query_target'
this._scores = pre_scores
jobs.append(this)
pbar.update()
if len(jobs) == 1:
(this, res), = self._map(jobs, n_cores, progress, desc='Pre-NBLASTs')
scr = res
else:
scr = pd.DataFrame(np.empty((len(query_dps_simp),
len(target_dps_simp)), dtype=nb.dtype),
index=query_dps_simp.id, columns=target_dps_simp.id)
scr.index.name = 'query'
scr.columns.name = 'target'
for this, res in self._map(jobs, n_cores, progress, desc='Pre-NBLASTs'):
scr.iloc[this.queries_ix, this.targets_ix] = res.values
# If this is an all-by-all and we computed only forward scores
if aba and scores == 'mean':
scr = (scr + scr.T.values) / 2
# Now select targets of interest for each query
if criterion == 'percentile':
sel = np.percentile(scr, q=t, axis=1)
mask = scr >= sel.reshape(-1, 1)
elif criterion == 'score':
sel = np.full(scr.shape[0], fill_value=t)
mask = scr >= sel.reshape(-1, 1)
else:
srt = np.argsort(scr.values, axis=1)[:, ::-1]
mask = pd.DataFrame(np.zeros(scr.shape, dtype=bool),
columns=scr.columns, index=scr.index)
_ = np.arange(mask.shape[0])
for N in range(t):
mask.iloc[_, srt[:, N]] = True
# --- Full NBLAST on the selected pairs --- #
query_self_hits = np.array([nb.calc_self_hit(n) for n in query_dps])
target_self_hits = np.array([nb.calc_self_hit(n) for n in target_dps])
jobs = []
with config.tqdm(desc='Prep. full NBLAST', total=n_rows * n_cols,
leave=False, disable=not progress) as pbar:
for qix in np.array_split(np.arange(len(query_dps)), n_rows):
for tix in np.array_split(np.arange(len(target_dps)), n_cols):
this = self._make_blaster(use_alpha, normalized, smat,
limit_dist, precision, approx_nn,
progress, smat_kwargs)
for ix in qix:
this.append(query_dps[ix], query_self_hits[ix])
for ix in tix:
this.append(target_dps[ix], target_self_hits[ix])
# Find the pairs to NBLAST in this part of the matrix
submask = mask.loc[query_dps[qix].id, target_dps[tix].id]
# `pairs` is an array of `[[query, target], [...]]` pairs
this.pairs = np.vstack(np.where(submask)).T
# Offset the target indices
this.pairs[:, 1] += len(qix)
# Track this blaster's mask relative to the original big one
this.mask = np.zeros(mask.shape, dtype=bool)
this.mask[qix[0]:qix[-1] + 1, tix[0]:tix[-1] + 1] = submask
this.pbar_position = len(jobs) if not utils.is_jupyter() else None
this.desc = 'Full NBLAST'
this._op = 'pair_query_target'
this._scores = scores
jobs.append(this)
pbar.update()
if len(jobs) == 1:
(this, res), = self._map(jobs, n_cores, progress)
scr[mask] = res
else:
for this, res in self._map(jobs, n_cores, progress):
scr[this.mask] = res
if return_mask:
return scr, mask
return scr
def synblast(self, query, target, *, by_type, cn_types, scores, normalized,
smat, n_cores, progress):
"""Synapse-based NBLAST (SynBLAST)."""
from ..synblast_funcs import SynBlaster, find_batch_partition
from ..nblast_funcs import find_optimal_partition
def get_connectors(n):
if cn_types is not None:
return n.connectors[n.connectors['type'].isin(cn_types)]
return n.connectors
# Find a partition that produces batches that each run in ~10s
if n_cores and n_cores > 1:
if progress:
n_rows, n_cols = find_batch_partition(query, target, T=10)
else:
n_rows, n_cols = find_optimal_partition(n_cores, query, target)
else:
n_rows = n_cols = 1
# Calculate self-hits once for all neurons
nb = SynBlaster(normalized=normalized, by_type=by_type, smat=smat,
progress=progress)
query_self_hits = np.array([nb.calc_self_hit(get_connectors(n)) for n in query])
target_self_hits = np.array([nb.calc_self_hit(get_connectors(n)) for n in target])
jobs = []
with config.tqdm(desc='Preparing', total=n_rows * n_cols, leave=False,
disable=not progress) as pbar:
for qix in np.array_split(np.arange(len(query)), n_rows):
for tix in np.array_split(np.arange(len(target)), n_cols):
this = SynBlaster(normalized=normalized, by_type=by_type,
smat=smat, progress=progress)
for ix in qix:
n = query[ix]
this.append(get_connectors(n), id=n.id,
self_hit=query_self_hits[ix])
for ix in tix:
n = target[ix]
this.append(get_connectors(n), id=n.id,
self_hit=target_self_hits[ix])
this.queries = np.arange(len(qix))
this.targets = np.arange(len(tix)) + len(qix)
this.queries_ix = qix
this.targets_ix = tix
this.pbar_position = len(jobs) if not utils.is_jupyter() else None
this._op = 'multi_query_target'
this._scores = scores
jobs.append(this)
pbar.update()
if len(jobs) == 1:
(this, res), = self._map(jobs, n_cores, progress)
return res
out = pd.DataFrame(np.empty((len(query), len(target)), dtype=nb.dtype),
index=query.id, columns=target.id)
out.index.name = 'query'
out.columns.name = 'target'
for this, res in self._map(jobs, n_cores, progress):
out.iloc[this.queries_ix, this.targets_ix] = res.values
return out
|