-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtopology.py
1522 lines (1238 loc) · 51.8 KB
/
topology.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: F. Paul Spitzner
# @Email: [email protected]
# @Created: 2021-02-05 10:37:47
# @Last Modified: 2022-12-16 12:57:52
# ------------------------------------------------------------------------------ #
# Helper classes to simulate axonal growth described in
# Orlandi et al. 2013, DOI: 10.1038/nphys2686
# with additional constraints to confine axons and optionally,
# create modular topologies.
#
# Example
# ```python
# # check what is avaiable in terms of parameters
# MergedTopology().get_parameters()
#
# # default is 160 neurons
# tp = MergedTopology(par_N=100)
#
# tp = ModularTopology(par_k_inter=5)
# bridge_ids = tp.neuron_bridge_ids
# num_n = tp.par_N
# a_ij_sparse = tp.aij_sparse
# mod_ids = tp.neuron_module_ids
# h5_data, h5_desc = tp.get_everything_as_nested_dict(return_descriptions=True)
# ```
# ------------------------------------------------------------------------------ #
import os
import sys
import h5py
import logging
import functools
import numpy as np
from tqdm.contrib.logging import logging_redirect_tqdm
from tqdm.auto import tqdm
from benedict import benedict
logging.basicConfig(
format="%(asctime)s | %(levelname)-8s | %(name)-12s | %(message)s",
datefmt="%y-%m-%d %H:%M",
)
log = logging.getLogger(__name__)
log.setLevel("INFO")
try:
from numba import jit, prange
raise ImportError
log.info("Using numba")
except ImportError:
log.info("Not using Numba")
# replace numba functions if numba not available:
# we only use jit and prange
# helper needed for decorators with kwargs
def parametrized(dec):
def layer(*args, **kwargs):
def repl(f):
return dec(f, *args, **kwargs)
return repl
return layer
@parametrized
def jit(func, **kwargs):
return func
def prange(*args):
return range(*args)
class BaseTopology(object):
def __init__(self, **kwargs):
"""
all kwargs are assumed to be parameters and overwrite defaults.
to see what parameters are avaiable, you could check a default instance:
```
BaseTopology().get_parameters()
```
"""
self.set_default_parameters()
self.update_parameters(**kwargs)
# per default, we have a quadratic substrate of size par_L
self._substrate = np.array([[0, self.par_L], [0, self.par_L]])
log.info("Base Topology")
for k, v in self.get_parameters().items():
log.info(f"{k}: {v}")
self.init_topology()
def set_default_parameters(self):
# lets define parameters of the algorithm.
# Numba does not like dicts, so we use global variables
# with a prefix. these guys should not change much!
self.par_N = 160 # number neurons
self.par_L = 600.0 # [um] maximum linear dish size
# axons, variable length, segments of fixed length with variable angle
self.par_std_l = 800.0 # [um] st. dev. of rayleigh dist. for axon len
# expectation value: <l> = std_l*sqrt(pi/2)
self.par_max_l = 1500.0 # [um] max lentgh allowed for axons
self.par_del_l = 10.0 # [um] length of each segment of axons
self.par_std_phi = 0.1 # [rad] std of Gauss dist. for angles betw segs
# soma, hard sphere
self.par_R_s = 7.5 # [um] radius of soma
# dendritic tree, sphere with variable radius
self.par_mu_d = 150.0 # [um] mean of Gauss dist. for radius
self.par_std_d = 20.0 # [um] std of Gauss dist. for radius
# whether to scale connection porbability with the amount of intersection
# between axon and dendritic tree
self.par_alpha_path_weighted = True
# connection probability alpha when axons intersect dendritic tree
if self.par_alpha_path_weighted:
self.par_alpha = 0.0125
else:
self.par_alpha = 0.33
# after hitting a wall, retry placing axon segment, default: 50
self.par_axon_retry = 5000
# after hitting a wall, we multiply the next random angle with this value
# so the path is a bit more flexible. default: 5
self.par_angle_mod = 5.0
def update_parameters(self, **kwargs):
available_pars = self.get_parameters().keys()
for key in kwargs.keys():
assert key in available_pars, f"Unrecognized parameter `{key}`"
setattr(self, key, kwargs[key])
def init_topology(self):
self.set_neuron_positions()
# it is convenient to have indices sorted by position
sort_idx = np.lexsort(
(
self.neuron_positions[:, 1],
self.neuron_positions[:, 0],
)
)
self.neuron_positions = self.neuron_positions[sort_idx]
# dendritic trees as circles
self.set_dendrite_radii()
# every source neuron has a list of target neuron ids
aij_nested = [np.array([], dtype="int")] * self.par_N
# which are the axon segments that made the connections? (ids)
segs_nested = [np.array([], dtype="int")] * self.par_N
# paths of all axons
axon_paths = [np.array([])] * self.par_N
# grow axons and get connections for all neurons
for n_id in range(0, self.par_N):
path = self.grow_axon(start=self.neuron_positions[n_id, :])
axon_paths[n_id] = path
cids, seg_ids = self.connections_for_neuron(n_id=n_id, axon_path=path)
argsorted = np.argsort(cids)
aij_nested[n_id] = cids[argsorted]
segs_nested[n_id] = seg_ids[argsorted]
# save the details as attributes
self.axon_paths = axon_paths
# this has three columns
aij_sparse = _nested_lists_to_sparse(aij_nested, vals=segs_nested)
# vanilla sparse matrix has two columns
self.aij_sparse = aij_sparse[:, 0:2]
self.aij_nested = aij_nested
# save the third column separately
self.segs_sparse = aij_sparse[:, 2]
self.k_in, self.k_out = _get_degrees(aij_nested)
def get_parameters(self):
res = self.__dict__
res = {key: res[key] for key in res.keys() if key[0:4] == "par_"}
return res
def get_everything_as_nested_dict(self, return_descriptions=False):
"""get most relevant details of the topology as a benedict"""
# fmt: off
h5_data = benedict()
h5_desc = benedict()
h5_data["data.neuron_pos_x"] = self.neuron_positions[:, 0]
h5_data["data.neuron_pos_y"] = self.neuron_positions[:, 1]
h5_data["data.neuron_radius_dendritic_tree"] = self.dendritic_radii
h5_data["data.connectivity_matrix_sparse"] = self.aij_sparse
h5_desc["data.connectivity_matrix_sparse"] = "first column is the id of the source neuron, second column is the id of the target neuron"
h5_data["data.connectivity_segments"] = self.segs_sparse
h5_desc["data.connectivity_segments"] = "this is the third column to connectivity_matrix_sparse, which holds the id of the axon segment that made the connection"
h5_data["data.neuron_k_in"] = self.k_in
h5_data["data.neuron_k_out"] = self.k_out
# restructuring to a nan-padded 2d matrix allows us to save all the paths
# into one dataset. (the padding does not cost storage on disc when compressing.)
nan_padded_segments = _nested_lists_to_2d_nan_padded(self.axon_paths)
h5_data["data.neuron_axon_segments_x"] = nan_padded_segments[:, :, 0]
h5_data["data.neuron_axon_segments_y"] = nan_padded_segments[:, :, 1]
h5_data["data.neuron_axon_length"] = np.array(
[len(x) * self.par_del_l for x in self.axon_paths]
)
h5_data["data.neuron_axon_end_to_end_distance"] = _get_end_to_end_distances(
self.axon_paths
)
h5_data["meta.topology"] = "orlandi base topology"
h5_data["meta.topology_num_neur"] = self.par_N
h5_data["meta.topology_num_outgoing"] = np.mean(self.k_out)
h5_data["meta.topology_sys_size"] = self.par_L
h5_data["meta.topology_alpha"] = self.par_alpha
h5_data["meta.topology_alpha_is_weighted"] = int(self.par_alpha_path_weighted)
# fmt: on
if return_descriptions:
return h5_data, h5_desc
return h5_data
def set_neuron_positions(self):
"""
make sure to set parameters correctly before calling this
"""
rejections = 0
# a 2d np array with x, y poitions, soma_radius and dendritic_tree diameter
neuron_pos = np.ones(shape=(self.par_N, 2)) * -93288.0
neuron_rad = np.ones(shape=(self.par_N)) * self.par_R_s
for i in range(self.par_N):
placed = False
while not placed:
placed = True
x = np.random.uniform(0.0, self.par_L)
y = np.random.uniform(0.0, self.par_L)
# dont place out of bounds
if not self.is_within_substrate(x, y, self.par_R_s):
placed = False
rejections += 1
continue
# dont overlap other neurons
if np.any(
_are_intersecting_circles(
neuron_pos[0:i], neuron_rad[0:i], x, y, self.par_R_s
)
):
placed = False
rejections += 1
continue
neuron_pos[i, 0] = x
neuron_pos[i, 1] = y
self.neuron_positions = neuron_pos
# lets have some overloads so we can use the generic helper functions
# from class instances
def grow_axon(self, start, num_segments=None, start_phi=None):
return _grow_axon(
self.par_std_l,
self.par_del_l,
self.par_axon_retry,
self.par_std_phi,
self.par_angle_mod,
self.is_within_substrate,
start=start,
num_segments=num_segments,
start_phi=start_phi,
)
def grow_axon_to_target(self, start, target, termination_criterion):
return _grow_axon_to_target(
self.par_std_l,
self.par_del_l,
self.par_axon_retry,
self.par_std_phi,
start,
target,
termination_criterion,
)
def set_dendrite_radii(self):
self.dendritic_radii = np.random.normal(
loc=self.par_mu_d, scale=self.par_std_d, size=self.par_N
)
def is_within_substrate(self, x, y, r=0):
return _is_within_rect(x, y, r, self._substrate)
def connections_for_neuron(self, n_id, axon_path):
"""
Get the connections due to a grown path
n_id : int
the id of the source neuron
axon_path : 2d array
positions of all axons segments growing from n_id
# Returns:
ids_to_connect_with : array
indices of neurons to form a connection with
"""
# first, collect all dendritic trees which we intersect and how often
num_intersections = np.zeros(self.par_N, "int")
first_intersections = np.zeros(self.par_N, "int")
for seg_id, seg in enumerate(axon_path):
if not self.is_within_substrate(seg[0], seg[1], r=0):
# we dont count intersection that occur out of our substrate,
# because we want dendritic trees to only be "on the substrate"
continue
intersecting_dendrites = _are_intersecting_circles(
ref_pos=self.neuron_positions,
ref_rad=self.dendritic_radii,
x=seg[0],
y=seg[1],
)
# intersections by dendrite
firsts = np.where(num_intersections == 0 & intersecting_dendrites)[0]
first_intersections[firsts] = seg_id
num_intersections += intersecting_dendrites
num_intersections[n_id] = 0 # dont create self-connections
t_ids = np.where(num_intersections > 0)[0] # target neuron ids
connection_segments = [] # which segment forms the connection
if not self.par_alpha_path_weighted:
# flat probability, independent of the number of intersections
t_ids = t_ids[np.random.uniform(0, 1, size=len(t_ids)) < self.par_alpha]
# find the segment that causes the connection. simply the first intersecting
connection_segments = first_intersections[t_ids]
else:
# probability is applied once, for every intersecting segment
filtered_t_ids = []
for tid in t_ids:
intersecting = (
np.random.uniform(0, 1, size=num_intersections[tid]) < self.par_alpha
)
if np.any(intersecting):
filtered_t_ids.append(tid)
# axons may grow out of trees and back in. the first index in
# of intersecing segs may not actually be the right one (or in tree)
ref = np.sum(intersecting)
for seg_id in range(first_intersections[tid], len(axon_path)):
seg = axon_path[seg_id]
if _is_intersecting_circle(
ref_pos=self.neuron_positions[tid],
ref_rad=self.dendritic_radii[tid],
x=seg[0],
y=seg[1],
):
if ref == 0:
break
ref -= 1
connection_segments.append(seg_id)
t_ids = np.array(filtered_t_ids, dtype="int")
connection_segments = np.array(connection_segments, dtype="int")
return t_ids, connection_segments
class OpenRoundTopology(BaseTopology):
"""
This is where neurons are placed in a circle in the middle but axons can grow
unconstrained.
linear dish size par_L is the diameter of the circle
(Implementationwise, we change the function that checks the substrate after neurons
are placed)
"""
def __init__(self, **kwargs):
self.set_default_parameters()
self.update_parameters(**kwargs)
# per default, we have a quadratic substrate of size par_L
self._substrate = np.array([[0, self.par_L], [0, self.par_L]])
log.info("OpenRound Topology")
for k, v in self.get_parameters().items():
log.info(f"{k}: {v}")
self.init_topology()
def init_topology(self):
log.info("Open Round Topology")
for k, v in self.get_parameters().items():
log.info(f"{k}: {v}")
self.set_neuron_positions()
# it is convenient to have indices sorted by position
sort_idx = np.lexsort(
(
self.neuron_positions[:, 1],
self.neuron_positions[:, 0],
)
)
self.neuron_positions = self.neuron_positions[sort_idx]
# dendritic trees as circles
self.set_dendrite_radii()
# every source neuron has a list of target neuron ids
aij_nested = [np.array([], dtype="int")] * self.par_N
# which are the axon segments that made the connections? (ids)
segs_nested = [np.array([], dtype="int")] * self.par_N
# paths of all axons
axon_paths = [np.array([])] * self.par_N
# do not confine axons: set substrate to be the whole plane
self.is_within_substrate = lambda x, y, r=0: True
# grow axons and get connections for all neurons
for n_id in range(0, self.par_N):
path = self.grow_axon(start=self.neuron_positions[n_id, :])
axon_paths[n_id] = path
cids, seg_ids = self.connections_for_neuron(n_id=n_id, axon_path=path)
argsorted = np.argsort(cids)
aij_nested[n_id] = cids[argsorted]
segs_nested[n_id] = seg_ids[argsorted]
# save the details as attributes
self.axon_paths = axon_paths
# this has three columns
aij_sparse = _nested_lists_to_sparse(aij_nested, vals=segs_nested)
# vanilla sparse matrix has two columns
self.aij_sparse = aij_sparse[:, 0:2]
self.aij_nested = aij_nested
# save the third column separately
self.segs_sparse = aij_sparse[:, 2]
self.k_in, self.k_out = _get_degrees(aij_nested)
def is_within_substrate(self, x, y, r=0):
"""
This check will only be used to place the neurons.
After that, we use a lambda that always returns true.
"""
return _is_intersecting_circle(
[self.par_center_x, self.par_center_y], self.par_L / 2, x, y, r
)
def set_default_parameters(self):
super().set_default_parameters()
self.par_center_x = self.par_L / 2
self.par_center_y = self.par_L / 2
def update_parameters(self, **kwargs):
super().update_parameters(**kwargs)
self.par_center_x = self.par_L / 2
self.par_center_y = self.par_L / 2
if "par_center_x" in kwargs:
self.par_center_x = kwargs["par_center_x"]
if "par_center_y" in kwargs:
self.par_center_y = kwargs["par_center_y"]
def get_everything_as_nested_dict(self, return_descriptions=False):
h5_data, h5_desc = super().get_everything_as_nested_dict(return_descriptions=True)
h5_data["meta.topology"] = "orlandi open round topology"
h5_data["meta.topology_center_x"] = self.par_center_x
h5_data["meta.topology_center_y"] = self.par_center_y
if return_descriptions:
return h5_data, h5_desc
return h5_data
class MergedTopology(BaseTopology):
def __init__(self, assign_submodules=False, **kwargs):
"""
The Base topology already largely does the job.
Make sure to have right system size and set artificial "module ids"
for the neurons that only depend on position, so we can better compare
to modular cultures
"""
self.set_default_parameters()
self.update_parameters(**kwargs)
# per default, we have a quadratic substrate of size par_L
self._substrate = np.array([[0, self.par_L], [0, self.par_L]])
log.info("Merged Topology")
for k, v in self.get_parameters().items():
log.info(f"{k}: {v}")
self.init_topology()
if assign_submodules:
log.info("Assigning sub modules from neuron positions")
# approximate module ids from position
# we might need this to stimulate part of the merged culture
# 0 lower left, 1 upper left, 2 lower right, 3 upper right
self.neuron_module_ids = np.ones(self.par_N, dtype=int) * -1
lim = 200
for nid in range(0, self.par_N):
x = self.neuron_positions[nid, 0]
y = self.neuron_positions[nid, 1]
if x < lim and y < lim:
self.neuron_module_ids[nid] = 0
elif x < lim and y >= lim:
self.neuron_module_ids[nid] = 1
elif x >= lim and y < lim:
self.neuron_module_ids[nid] = 2
elif x >= lim and y >= lim:
self.neuron_module_ids[nid] = 3
else:
self.neuron_module_ids = np.zeros(self.par_N, dtype=int)
def set_default_parameters(self):
super().set_default_parameters()
self.par_L = 400
# to later use same code as for modular systems, use k_inter = -1 for merged
self.par_k_inter = -1
def get_everything_as_nested_dict(self, return_descriptions=False):
h5_data, h5_desc = super().get_everything_as_nested_dict(return_descriptions=True)
h5_data["data.neuron_module_id"] = self.neuron_module_ids[:]
h5_data["meta.topology"] = "orlandi merged topology"
h5_data["meta.topology_k_inter"] = self.par_k_inter
if return_descriptions:
return h5_data, h5_desc
return h5_data
class ModularTopology(BaseTopology):
def __init__(self, **kwargs):
self.set_default_parameters()
self.update_parameters(**kwargs)
self.set_default_modules()
log.info("Modular Topology")
for k, v in self.get_parameters().items():
log.info(f"{k}: {v}")
self.init_topology()
def set_default_parameters(self):
super().set_default_parameters()
# we have 2 200um modules with 200um space inbetween
self.par_L = 600
# number of axons going from each module to its neighbours
self.par_k_inter = 5
def init_topology(self):
# only call this after initalizing parameters and modules!
self.set_neuron_positions()
self.set_neuron_module_ids()
# it is convenient to have indices sorted by modules and position
sort_idx = np.lexsort(
(
self.neuron_positions[:, 1],
self.neuron_positions[:, 0],
self.neuron_module_ids,
)
)
self.neuron_positions = self.neuron_positions[sort_idx]
self.neuron_module_ids = self.neuron_module_ids[sort_idx]
# dendritic trees as circles
self.set_dendrite_radii()
# every source neuron has a list of target neuron ids
aij_nested = [np.array([], dtype="int")] * self.par_N
# which are the axon segments that made the connections? (ids)
segs_nested = [np.array([], dtype="int")] * self.par_N
# paths of all axons
axon_paths = [np.array([])] * self.par_N
# which neurons create bridges
bridge_ids = []
# generate and connect bridging axons
# e.g. (0, 1, 2): "from 0 to 1 and 2"
for b_ids in [(0, 1, 2), (1, 0, 3), (2, 0, 3), (3, 1, 2)]:
src_mod = self.mods[b_ids[0]]
tar_mods = [self.mods[b_ids[1]], self.mods[b_ids[2]]]
# get the ids and axon paths of all neurons that bridge between modules
br_nids, br_paths = self.grow_bridging_axons(
source_mod=src_mod, target_mods=tar_mods
)
bridge_ids.extend(br_nids)
# get connectivity for each neuron
for idx, n_id in enumerate(br_nids):
cids, seg_ids = self.connections_for_neuron(n_id, axon_path=br_paths[idx])
axon_paths[n_id] = br_paths[idx]
argsorted = np.argsort(cids)
aij_nested[n_id] = cids[argsorted]
segs_nested[n_id] = seg_ids[argsorted]
self.neuron_bridge_ids = np.array(bridge_ids, dtype="int")
# grow axons and get connections for the remaining neurons
for n_id in range(0, self.par_N):
if n_id in bridge_ids:
continue
path = self.grow_axon(start=self.neuron_positions[n_id, :])
axon_paths[n_id] = path
cids, seg_ids = self.connections_for_neuron(n_id=n_id, axon_path=path)
argsorted = np.argsort(cids)
aij_nested[n_id] = cids[argsorted]
segs_nested[n_id] = seg_ids[argsorted]
# save the details as attributes
self.axon_paths = axon_paths
# this has three columns
aij_sparse = _nested_lists_to_sparse(aij_nested, vals=segs_nested)
# vanilla sparse matrix has two columns
self.aij_sparse = aij_sparse[:, 0:2]
self.aij_nested = aij_nested
# save the third column separately
self.segs_sparse = aij_sparse[:, 2]
self.k_in, self.k_out = _get_degrees(aij_nested)
def get_everything_as_nested_dict(self, return_descriptions=False):
h5_data, h5_desc = super().get_everything_as_nested_dict(return_descriptions=True)
h5_data["data.neuron_module_id"] = self.neuron_module_ids[:]
h5_data["data.neuron_bridge_ids"] = self.neuron_bridge_ids
h5_desc[
"data.neuron_bridge_ids"
] = "list of ids of neurons connecting two modules"
h5_data["meta.topology"] = "orlandi modular topology"
h5_data["meta.topology_k_inter"] = self.par_k_inter
if return_descriptions:
return h5_data, h5_desc
return h5_data
def set_default_modules(self):
# coordinates of rectangular modules: module_number, x | y, low | high
self.mods = np.ones(shape=(4, 2, 2)) * np.nan
self.mods[0] = np.array([[0, 200], [0, 200]])
self.mods[1] = np.array([[0, 200], [400, 600]])
self.mods[2] = np.array([[400, 600], [0, 200]])
self.mods[3] = np.array([[400, 600], [400, 600]])
def grow_bridging_axons(self, source_mod, target_mods):
# get the indices of neurons in the target module
source_idx = [
_is_within_rect(x, y, 0, source_mod) for x, y in self.neuron_positions
]
source_idx = np.where(source_idx)[0]
source_pos = self.neuron_positions[source_idx]
# get an idea where the modules are located. assuming some clustering in space,
# then the center of mass is a good proxy... or we use the center of modules
source_cm = _center_of_rect(source_mod)
# pick the source closes to cm
source_distances = np.sqrt(
np.power(source_pos[:, 0] - source_cm[0], 2.0)
+ np.power(source_pos[:, 1] - source_cm[1], 2.0)
)
closest_idx = source_idx[np.argsort(source_distances)]
rewired_idx = []
rewired_paths = []
# criteria when an axons has reached another module
# we need a bit of boilerplate to get preset arguments into numba functions
def make_criterion(mod):
@jit(nopython=True, parallel=False, fastmath=True, cache=True)
def f(x, y):
# r=5um so we have a bit of a margin around the border, before accepting
return _is_within_rect(x, y, r=5, rect=mod)
return f
termination_criteria = []
for mod in target_mods:
termination_criteria.append(make_criterion(mod))
for k in range(0, self.par_k_inter):
for mdx, mod in enumerate(target_mods):
n_offset = len(rewired_idx)
# ensure ~ 5um padding at module edges before terminating
# f_term = functools.partial(_is_within_rect, r=5, rect=mod)
criterion = termination_criteria[mdx]
n_idx = closest_idx[n_offset]
# print(n_idx, self.neuron_positions[n_idx])
if n_idx == 58:
global debug_print
debug_print = True
# start by growing to our target
path, num_segs_left, last_phi = self.grow_axon_to_target(
start=self.neuron_positions[n_idx],
target=_center_of_rect(mod),
termination_criterion=criterion,
)
if num_segs_left > 0:
# finish randomly
last_pos = path[-num_segs_left - 1]
rest_of_path = self.grow_axon(
start=last_pos,
num_segments=num_segs_left,
start_phi=last_phi,
)
path[-num_segs_left:] = rest_of_path
if not np.all(np.isfinite(path)):
print("\t", num_segs_left, len(path), len(rest_of_path))
print(path)
print(rest_of_path)
assert np.all(np.isfinite(path))
rewired_idx.append(n_idx)
rewired_paths.append(path)
return rewired_idx, rewired_paths
def set_neuron_positions(self):
"""
make sure to set parameters correctly before calling this
"""
rejections = 0
# a 2d np array with x, y poitions, soma_radius and dendritic_tree diameter
neuron_pos = np.ones(shape=(self.par_N, 2)) * -93288.0
neuron_rad = np.ones(shape=(self.par_N)) * self.par_R_s
# keep track of the number of neurons in every module
neurons_per_module = np.zeros(len(self.mods), "int")
for i in range(self.par_N):
placed = False
while not placed:
placed = True
x = np.random.uniform(0.0, self.par_L)
y = np.random.uniform(0.0, self.par_L)
# dont place out of bounds
if not self.is_within_substrate(x, y, self.par_R_s):
placed = False
rejections += 1
continue
# dont overlap other neurons
if np.any(
_are_intersecting_circles(
neuron_pos[0:i], neuron_rad[0:i], x, y, self.par_R_s
)
):
placed = False
rejections += 1
continue
# for modular topology, we want to ensure the same number of neurons
# in every module
mod_id = self.mod_id_from_coordinate(x, y)
if neurons_per_module[mod_id] != np.min(neurons_per_module):
placed = False
rejections += 1
continue
neuron_pos[i, 0] = x
neuron_pos[i, 1] = y
neurons_per_module[mod_id] += 1
self.neuron_positions = neuron_pos
def set_neuron_module_ids(self):
"""
assign the module id of every neuron
"""
tar_mods = np.ones(self.par_N, dtype="int") * -1
for idx in range(0, self.par_N):
x, y = self.neuron_positions[idx]
tar_mods[idx] = self.mod_id_from_coordinate(x, y)
self.neuron_module_ids = tar_mods
def mod_id_from_coordinate(self, x, y):
num_mods = len(self.mods)
for i in range(0, num_mods):
if _is_within_rect(x, y, 0, self.mods[i]):
return i
raise Exception("Coordinate is not within any known module")
def is_within_substrate(self, x, y, r=0):
for m in range(0, len(self.mods)):
if _is_within_rect(x, y, r, self.mods[m]):
return True
return False
def connections_for_neuron(self, n_id, axon_path):
"""
Get the connections due to a grown path
n_id : int
the id of the source neuron
axon_path : 2d array
positions of all axons segments growing from n_id
# Returns:
ids_to_connect_with : array
indices of neurons to form a connection with
"""
# first, collect all dendritic trees which we intersect and how often
num_intersections = np.zeros(self.par_N, "int")
first_intersections = np.zeros(self.par_N, "int")
for seg_id, seg in enumerate(axon_path):
if not self.is_within_substrate(seg[0], seg[1], r=0):
# we dont count intersection that occur out of our substrate,
# because we want dendritic trees to only be "on the substrate"
continue
intersecting_dendrites = _are_intersecting_circles(
ref_pos=self.neuron_positions,
ref_rad=self.dendritic_radii,
x=seg[0],
y=seg[1],
)
# we also do not want to connect to a hypothetical super large tree
# that comes from another module
# this additioanl check is the only difference to the function in the
# base class.
# other than that, this a carbon copy of BaseTopology :/
seg_mod = self.mod_id_from_coordinate(*seg)
valid_dendrites = self.neuron_module_ids == seg_mod
# only count intersections where valid and intersecting
intersecting_dendrites = intersecting_dendrites & valid_dendrites
# collect
firsts = np.where(num_intersections == 0 & intersecting_dendrites)[0]
first_intersections[firsts] = seg_id
num_intersections += intersecting_dendrites
num_intersections[n_id] = 0 # dont create self-connections
t_ids = np.where(num_intersections > 0)[0] # target neuron ids
connection_segments = [] # which segment forms the connection
if not self.par_alpha_path_weighted:
# flat probability, independent of the number of intersections
t_ids = t_ids[np.random.uniform(0, 1, size=len(t_ids)) < self.par_alpha]
# find the segment that causes the connection. simply the first intersecting
connection_segments = first_intersections[t_ids]
else:
# probability is applied once, for every intersecting segment
filtered_t_ids = []
for tid in t_ids:
intersecting = (
np.random.uniform(0, 1, size=num_intersections[tid]) < self.par_alpha
)
if np.any(intersecting):
filtered_t_ids.append(tid)
# axons may grow out of trees and back in. the first index in
# of intersecing segs may not actually be the right one (or in tree)
ref = np.sum(intersecting)
for seg_id in range(first_intersections[tid], len(axon_path)):
seg = axon_path[seg_id]
if _is_intersecting_circle(
ref_pos=self.neuron_positions[tid],
ref_rad=self.dendritic_radii[tid],
x=seg[0],
y=seg[1],
):
if ref == 0:
break
ref -= 1
connection_segments.append(seg_id)
t_ids = np.array(filtered_t_ids, dtype="int")
connection_segments = np.array(connection_segments, dtype="int")
return t_ids, connection_segments
# ------------------------------------------------------------------------------ #
# constrained in-degrees
# a bit of iteration to find the right alpha
# for a desired in-degre distributions
# ------------------------------------------------------------------------------ #
# 2 levle dict, first: k_inter (topology), second: k_in (desired in-degree)
_alpha_for_kin = dict()
for k_inter in [0, 1, 3, 5, 10, 20, -1]:
_alpha_for_kin[k_inter] = dict()
# k_in 30
_alpha_for_kin[0][30] = 0.0275
_alpha_for_kin[1][30] = 0.02641
_alpha_for_kin[3][30] = 0.02521
_alpha_for_kin[5][30] = 0.02437
_alpha_for_kin[10][30] = 0.0225
_alpha_for_kin[20][30] = 0.02021
_alpha_for_kin[-1][30] = 0.00824
# k_in 25
_alpha_for_kin[0][25] = 0.01728
_alpha_for_kin[1][25] = 0.01698
_alpha_for_kin[3][25] = 0.01667
_alpha_for_kin[5][25] = 0.01635
_alpha_for_kin[10][25] = 0.0156
_alpha_for_kin[20][25] = 0.01498
_alpha_for_kin[-1][25] = 0.00665
# k_in 20
_alpha_for_kin[0][20] = 0.01156
_alpha_for_kin[1][20] = 0.01129
_alpha_for_kin[3][20] = 0.01125
_alpha_for_kin[5][20] = 0.01091
_alpha_for_kin[10][20] = 0.01099
_alpha_for_kin[20][20] = 0.01057
_alpha_for_kin[-1][20] = 0.00519
# k_in 15
_alpha_for_kin[0][15] = 0.007448
_alpha_for_kin[1][15] = 0.007448
_alpha_for_kin[3][15] = 0.007344
_alpha_for_kin[5][15] = 0.007305
_alpha_for_kin[10][15] = 0.007266
_alpha_for_kin[20][15] = 0.007201
_alpha_for_kin[-1][15] = 0.00373
def find_alpha_for_target_in_degree(
k_inter, k_in_target, num_reps=5, rerun=True, **kwargs
):
"""
find the alpha value that leads to a desired in-degree distribution
best set the log level to error before running this function
`topo.log.setLevel("ERROR")`
# Paramters
k_inter : int
number of bridging axons. -1 for merged topology, otherwise modular is used
k_in_target : int
target in-degree
rerun : bool
I have saved some values for k_in = 15, 20, 25, 30.
By default I give you those and avoid the computation.
num_reps : int
how many independent estimates to create
**kwargs : dict
passed to `_iteration`. e.g. init, iterstep, threshold
"""
if not rerun:
try:
log.debug("trying saved (hardcoded) alpha")
return _alpha_for_kin[k_inter][k_in_target]
except KeyError:
log.debug(f"No precomputed value for {k_inter=}, {k_in_target=}")
with logging_redirect_tqdm():
res = np.ones(num_reps) * np.nan
for rep in tqdm(range(num_reps), leave=False):
log.info(f"finding alpha for {k_inter=}")
res[rep] = _iteration(
generator=lambda alpha: _generate_series_of_kin_from_topo(alpha, k_inter),
target=k_in_target,
**kwargs,
)
if not np.all(np.isfinite(res)):
log.warning(f"some found alpha values do not make sense {res=}")
return np.nanmean(res)