Skip to content

DBS Stimulator

CompNeuroPy.dbs.DBSstimulator #

Class for stimulating a population with DBS.

Warning

If you use auto_implement, pointers to the populations and projections of the model are not valid anymore (new populations and projections are created)! Use a CompNeuroPy model working with names of populations and projections anyway (recommended) or use the update_pointers method.

Examples:

from ANNarchy import Population, Izhikevich, compile, simulate, setup
from CompNeuroPy import DBSstimulator

# setup ANNarchy
setup(dt=0.1)

# create populations
population1 = Population(10, neuron=Izhikevich, name="my_pop1")
population2 = Population(10, neuron=Izhikevich, name="my_pop2")
>>>
# create DBS stimulator
dbs = DBSstimulator(
    stimulated_population=population1,
    population_proportion=0.5,
    dbs_depolarization=30,
    auto_implement=True,
)

# update pointers to correct populations
population1, population2 = dbs.update_pointers(
    pointer_list=[population1, population2]
)

# compile network
compile()

# run simulation
# 1000 ms without dbs
simulate(1000)
# 1000 ms with dbs
dbs.on()
simulate(1000)
# 1000 ms without dbs
dbs.off()
simulate(1000)
Source code in src/CompNeuroPy/dbs.py
 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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
class DBSstimulator:
    """
    Class for stimulating a population with DBS.

    !!! warning
        If you use auto_implement, pointers to the populations and projections of
        the model are not valid anymore (new populations and projections are
        created)! Use a CompNeuroPy model working with names of populations and
        projections anyway (recommended) or use the update_pointers method.

    Examples:
        ```python
        from ANNarchy import Population, Izhikevich, compile, simulate, setup
        from CompNeuroPy import DBSstimulator

        # setup ANNarchy
        setup(dt=0.1)

        # create populations
        population1 = Population(10, neuron=Izhikevich, name="my_pop1")
        population2 = Population(10, neuron=Izhikevich, name="my_pop2")
        >>>
        # create DBS stimulator
        dbs = DBSstimulator(
            stimulated_population=population1,
            population_proportion=0.5,
            dbs_depolarization=30,
            auto_implement=True,
        )

        # update pointers to correct populations
        population1, population2 = dbs.update_pointers(
            pointer_list=[population1, population2]
        )

        # compile network
        compile()

        # run simulation
        # 1000 ms without dbs
        simulate(1000)
        # 1000 ms with dbs
        dbs.on()
        simulate(1000)
        # 1000 ms without dbs
        dbs.off()
        simulate(1000)
        ```
    """

    @check_types()
    def __init__(
        self,
        stimulated_population: Population,
        population_proportion: float = 1.0,
        excluded_populations_list: list[Population] = [],
        dbs_depolarization: float = 0.0,
        orthodromic: bool = False,
        antidromic: bool = False,
        efferents: bool = False,
        afferents: bool = False,
        passing_fibres: bool = False,
        passing_fibres_list: list[Projection] = [],
        passing_fibres_strength: float | list[float] = 1.0,
        sum_branches: bool = True,
        dbs_pulse_frequency_Hz: float = 130.0,
        dbs_pulse_width_us: float = 300.0,
        axon_spikes_per_pulse: float = 1.0,
        axon_rate_amp: float | dict[Population | str, float] = 1.0,
        seed: int | None = None,
        auto_implement: bool = False,
        model: generate_model | None = None,
    ) -> None:
        """
        Initialize DBS stimulator.

        !!! warning
            Do this before compiling the model!

        Args:
            stimulated_population (Population):
                Population which is stimulated by DBS
            population_proportion (float, optional):
                Proportion of the stimulated population which is affected by DBS,
                neurons are distributed randomly. Default: 1.0.
            excluded_populations_list (list, optional):
                List of populations which are excluded from DBS effects, they are not
                affected and their axons do not generate axon spikes. Default: [].
            dbs_depolarization (float, optional):
                Depolarization effect of the DBS pulse to local soma. Default: 0.0.
            orthodromic (bool, optional):
                If True, DBS causes axonal spikes which are forwarded orthodromically.
                Default: False.
            antidromic (bool, optional):
                If True, DBS causes axonal spikes which are forwarded antidromically,
                only available in spiking networks. Default: False.
            efferents (bool, optional):
                If True, DBS affects the efferents of the stimulated population
                (orthodromic and/or antidromic have to be True too). Default: False.
            afferents (bool, optional):
                If True, DBS affects the afferents of the stimulated population
                (orthodromic and/or antidromic have to be True too). Default: False.
            passing_fibres (bool, optional):
                If True, DBS affects the passing fibres of the stimulated region defined
                in passing_fibres_list (orthodromic and/or antidromic have to be True
                too). Default: False.
            passing_fibres_list (list of Projections, optional):
                List of projections which pass the DBS stimulated region and therefore
                are activated by DBS. Default: [], also set passing_fibres True!
            passing_fibres_strength (float or list of float, optional):
                Single value or list of float values between 0 and 1 defining how strong
                the passing fibres are activated by DBS (0: not activated, 1: fully
                activated like the projections in the DBS stimulated region).
                Default: 1.
            sum_branches (bool, optional):
                If True, the antidromic_prob of a presynaptic population (defining how
                many axon spikes affect the pop antidromically) of passing fibres is
                the sum of the passing_fibres_strengths of the single axon branches.
                Default: True.
            dbs_pulse_frequency_Hz (float, optional):
                Frequency of the DBS pulse. Default: 130 Hz.
            dbs_pulse_width_us (float, optional):
                Width of the DBS pulse. Default: 300 us.
            axon_spikes_per_pulse (float, optional):
                Number of average axon spikes per DBS pulse. Default: 1.
            axon_rate_amp (float or dict of float, optional):
                Similar to prob_axon_spike in spiking model. Which rate is forwarded on
                axons caused by DBS. You can specify this for each population
                individually by using a dictionary (keys = Population instances)
                axon_rate_amp = {pop: 1.5} --> the efferent axons of pop forward a rate
                of 1.5 during DBS (all other affected projections forward the default
                value)
                You can specify the default value by using the key "default", e.g.
                {pop: 1.5, "default": 1.0} -> efferent axons of all populations except
                pop forward a rate of 1.0 during DBS. Default: 1.0.
            seed (int, optional):
                Seed for the random distribution of affected neurons based on
                population_proportion. Default: None.
            auto_implement (bool, optional):
                If True, automatically implement DBS mechanisms to the model. Only
                supported for Izhikevich spiking models and rate-coded models.
                Default: False.
                TODO test what happens with mixed models
            model (generate_model, optional):
                CompNeuroPy model which is used to automatically implement DBS
                mechanisms, should not be compiled!. Default: None, i.e., use all
                populations and projections of the current magic model
        """

        if auto_implement:
            ### recreate model with DBS mechanisms
            ### give all variables containing Populations and Projections
            ### and also recreate them during recreating the model
            ### variables are:
            ### - stimulated_population
            ### - excluded_populations_list
            ### - passing_fibres_list
            ### - axon_rate_amp
            if not isinstance(model, type(None)):
                ### CompNeuroPy model given
                ### recreate model with DBS mechanisms
                create_dbs_model_obj = _CreateDBSmodelcnp(
                    model,
                    stimulated_population,
                    excluded_populations_list,
                    passing_fibres_list,
                    axon_rate_amp,
                )
                ### get the new CompNeuroPy model
                model = create_dbs_model_obj.model
            else:
                ### no CompNeuroPy model given --> use all populations and projections of the current magic model
                ### recreate model with DBS mechanisms
                create_dbs_model_obj = _CreateDBSmodel(
                    stimulated_population,
                    excluded_populations_list,
                    passing_fibres_list,
                    axon_rate_amp,
                )
            ### get the new variables containing Populations and Projections
            stimulated_population = create_dbs_model_obj.stimulated_population
            excluded_populations_list = create_dbs_model_obj.excluded_populations_list
            passing_fibres_list = create_dbs_model_obj.passing_fibres_list
            axon_rate_amp = create_dbs_model_obj.axon_rate_amp

        ### set parameters
        self.stimulated_population = stimulated_population
        self.population_proportion = population_proportion
        self.excluded_populations_list = excluded_populations_list
        self.dbs_depolarization = dbs_depolarization
        self.orthodromic = orthodromic
        self.antidromic = antidromic
        self.efferents = efferents
        self.afferents = afferents
        self.passing_fibres = passing_fibres
        self.passing_fibres_list = passing_fibres_list
        self.passing_fibres_strength = passing_fibres_strength
        self.sum_branches = sum_branches
        self.dbs_pulse_width_us = dbs_pulse_width_us
        self.axon_spikes_per_pulse = axon_spikes_per_pulse
        self.axon_rate_amp = axon_rate_amp
        self.seed = seed
        self.model = model

        ### ANNarchy constants for DBS
        self._set_constants(dbs_pulse_frequency_Hz)

        ### randomly select affected neurons i.e. create dbs_on_array
        self.dbs_on_array = self._create_dbs_on_array(population_proportion, seed)

    def _create_dbs_on_array(self, population_proportion: float, seed: int):
        """
        Create an array with the shape of the stimulated population with ones and zeros
        randomly distributed with the specified population_proportion.

        Args:
            population_proportion (float):
                Proportion of the stimulated population which is affected by DBS,
                neurons are distributed randomly
            seed (int):
                Seed for the random distribution of affected neurons based on
                population_proportion

        Returns:
            dbs_on_array (np.array):
                Array with the shape of the stimulated population with ones and zeros
                randomly distributed with the specified population_proportion
        """
        ### create random number generator
        rng = np.random.default_rng(seed)
        ### create an array of zeros with the shape of the population, then flatten it
        dbs_on_array = np.zeros(self.stimulated_population.geometry).flatten()
        ### get the number of affected neurons based on the population_proportion
        number_of_affected_neurons = population_proportion * dbs_on_array.size
        ### randomly ceil or floor the number of affected neurons
        number_of_affected_neurons = int(
            rng.choice(
                [
                    np.ceil(number_of_affected_neurons),
                    np.floor(number_of_affected_neurons),
                ]
            )
        )
        ### insert ones to the dbs_on_array
        dbs_on_array[:number_of_affected_neurons] = 1
        ### shuffle array
        rng.shuffle(dbs_on_array)
        ### reshape array to the shape of the population
        dbs_on_array = dbs_on_array.reshape(self.stimulated_population.geometry)
        ### return array
        return dbs_on_array

    def _set_constants(self, dbs_pulse_frequency_Hz: float):
        """
        Set constants for DBS.

        Args:
            dbs_pulse_frequency_Hz (float):
                Frequency of the DBS pulse in Hz
        """
        # pulse frequency:
        Constant("dbs_pulse_frequency_Hz", dbs_pulse_frequency_Hz)
        # pulse width:
        # Neumant et al.. 2023: 60us but Meier et al. 2022: 300us... 60us = 0.06ms is very small for ANNarchy simulations
        Constant("dbs_pulse_width_us", self.dbs_pulse_width_us)

        ### add global function for DBS pulse
        add_function(
            "pulse(time_ms) = ite(modulo(time_ms*1000, 1000000./dbs_pulse_frequency_Hz) < dbs_pulse_width_us, 1., 0.)"
        )

    def _axon_spikes_per_pulse_to_prob(self, axon_spikes_per_pulse: float):
        """
        Convert number of axon spikes per pulse to probability of axon spikes per
        timestep during DBS pulse

        Args:
            axon_spikes_per_pulse (float):
                Number of average axon spikes per DBS pulse

        Returns:
            prob_axon_spike_time_step (float):
                Probability of axon spikes per timestep during DBS pulse
        """
        return np.clip(
            (axon_spikes_per_pulse * 1000 * dt() / self.dbs_pulse_width_us), 0, 1
        )

    def _set_depolarization(self, dbs_depolarization: float | None = None):
        """
        Set depolarization of population.

        Args:
            dbs_depolarization (float, optional):
                Depolarization effect of the DBS pulse to local soma. Default: None,
                i.e., use value from initialization
        """
        ### either use given depolarization or use default value
        if isinstance(dbs_depolarization, type(None)):
            dbs_depolarization = self.dbs_depolarization

        ### set depolarization of population
        for pop in populations():
            if pop == self.stimulated_population:
                pop.dbs_depolarization = dbs_depolarization
            else:
                pop.dbs_depolarization = 0

    def _set_axon_spikes(
        self,
        orthodromic: bool | None = None,
        antidromic: bool | None = None,
        efferents: bool | None = None,
        afferents: bool | None = None,
        passing_fibres: bool | None = None,
        passing_fibres_strength: float | list[float] | None = None,
        sum_branches: bool | None = None,
        axon_spikes_per_pulse: float | None = None,
        axon_rate_amp: float | dict[Population | str, float] | None = None,
    ):
        """
        Set axon spikes forwarding orthodromic

        Args:
            orthodromic (bool, optional):
                If True, DBS causes axonal spikes which are forwarded orthodromically,
                Default: None, i.e., use value from initialization
            antidromic (bool, optional):
                If True, DBS causes axonal spikes which are forwarded antidromically,
                only available in spiking networks. Default: None, i.e., use value from
                initialization
            efferents (bool, optional):
                If True, DBS affects the efferents of the stimulated population
                (orthodromic and/or antidromic have to be True too). Default: None,
                i.e., use value from initialization
            afferents (bool, optional):
                If True, DBS affects the afferents of the stimulated population
                (orthodromic and/or antidromic have to be True too). Default: None,
                i.e., use value from initialization
            passing_fibres (bool, optional):
                If True, DBS affects the passing fibres of the stimulated region defined
                in passing_fibres_list (orthodromic and/or antidromic have to be True
                too). Default: None, i.e., use value from initialization
            passing_fibres_strength (float | list[float], optional):
                Single value or list of float values between 0 and 1 defining how strong
                the passing fibres are activated by DBS (0: not activated, 1: fully
                activated like the projections in the DBS stimulated region).
                Default: None, i.e., use value from initialization
            sum_branches (bool, optional):
                If True, the antidromic_prob of a presynaptic population (defining how
                many axon spikes affect the pop antidromically) of passing fibres is
                the sum of the passing_fibres_strengths of the single axon branches.
                Default: None, i.e., use value from initialization
            axon_spikes_per_pulse (float, optional):
                Number of average axon spikes per DBS pulse. Default: None, i.e., use
                value from initialization
            axon_rate_amp (float | dict[Population | str, float], optional):
                Similar to prob_axon_spike in spiking model. Which rate is forwarded on
                axons caused by DBS. You can specify this for each population
                individually by using a dictionary (keys = Population instances)
                axon_rate_amp = {pop: 1.5} --> the efferent axons of pop forward a rate
                of 1.5 during DBS (all other affected projections forward the default
                value)
                You can specify the default value by using the key "default", e.g.
                {pop: 1.5, "default": 1.0} -> efferent axons of all populations except
                pop forward a rate of 1.0 during DBS. Default: None, i.e., use value
                from initialization
        """

        ### either use given orthodromic or use default value
        if isinstance(orthodromic, type(None)):
            orthodromic = self.orthodromic
        ### either use given antidromic or use default value
        if isinstance(antidromic, type(None)):
            antidromic = self.antidromic
        ### either use given efferents or use default value
        if isinstance(efferents, type(None)):
            efferents = self.efferents
        ### either use given afferents or use default value
        if isinstance(afferents, type(None)):
            afferents = self.afferents
        ### either use given afferents or use default value
        if isinstance(passing_fibres, type(None)):
            passing_fibres = self.passing_fibres
        ### either use given passing_fibres_strength or use default value
        if isinstance(passing_fibres_strength, type(None)):
            passing_fibres_strength = self.passing_fibres_strength
        ### either use given sum_branches or use default value
        if isinstance(sum_branches, type(None)):
            sum_branches = self.sum_branches
        ### either use given axon_spikes_per_pulse or use default value
        if isinstance(axon_spikes_per_pulse, type(None)):
            axon_spikes_per_pulse = self.axon_spikes_per_pulse
        ### either use given axon_rate_amp or use default value
        if isinstance(axon_rate_amp, type(None)):
            axon_rate_amp = self.axon_rate_amp

        ### check if passing_fibres_strength is a list
        if not isinstance(passing_fibres_strength, list):
            passing_fibres_strength = [passing_fibres_strength] * len(
                self.passing_fibres_list
            )
        ### check if axon_rate_amp is a dict or float
        if isinstance(axon_rate_amp, dict):
            ### check if default key is missing
            if "default" not in axon_rate_amp.keys():
                ### add the key "default" with the value 1.0 to the dict
                axon_rate_amp["default"] = 1.0
        else:
            ### create dict with default value
            axon_rate_amp = {"default": axon_rate_amp}

        ### deactivate DBS axon transmission
        self._deactivate_axon_DBS()

        ### activate orthodromic transmission for all projections
        if orthodromic:
            self._set_orthodromic(
                efferents,
                afferents,
                passing_fibres,
                passing_fibres_strength,
                axon_spikes_per_pulse,
                axon_rate_amp,
            )

        ### activate antidromic transmission for all populations
        if antidromic:
            self._set_antidromic(
                efferents,
                afferents,
                passing_fibres,
                passing_fibres_strength,
                sum_branches,
                axon_spikes_per_pulse,
            )

    def _deactivate_axon_DBS(self):
        """
        Deactivate axon spikes forwarding for both orthodromic and antidromic.
        """
        for pop in populations():
            ### deactivate axon spike genearation for all populations
            pop.prob_axon_spike = 0
            pop.axon_rate_amp = 0
            ### deactivate antidromic transmission for all populations
            pop.antidromic = 0
            pop.antidromic_prob = 0

        ### deactivate orthodromic transmission for all projections
        for proj in projections():
            proj.axon_transmission = 0
            proj.p_axon_spike_trans = 0

    def _set_orthodromic(
        self,
        efferents: bool,
        afferents: bool,
        passing_fibres: bool,
        passing_fibres_strength: list[float],
        axon_spikes_per_pulse: float,
        axon_rate_amp: dict[Population | str, float],
    ):
        """
        Set orthodromic axon spikes forwarding.

        Args:
            efferents (bool):
                If True, DBS affects the efferents of the stimulated population
                (orthodromic and/or antidromic have to be True too)
            afferents (bool):
                If True, DBS affects the afferents of the stimulated population
                (orthodromic and/or antidromic have to be True too)
            passing_fibres (bool):
                If True, DBS affects the passing fibres of the stimulated population
                (orthodromic and/or antidromic have to be True too and there have to
                be passing fibres in the passing_fibres_list)
            passing_fibres_strength (list[float]):
                List of float values between 0 and 1 defining how strong the passing
                fibres are activated by DBS (0: not activated, 1: fully activated
                like projections in DBS stimulated region)
            axon_spikes_per_pulse (float):
                Number of average axon spikes per DBS pulse
            axon_rate_amp (dict[Population | str, float]):
                Similar to prob_axon_spike in spiking model. Which rate is forwarded
                on axons caused by DBS. The dictionary has to contain the key
                "default" with the default value for all projections and can contain
                keys for each population with a different value for the axon_rate of
                the efferent axons of this population.
        """
        if efferents:
            ### activate all efferent projections
            projection_list = projections(pre=self.stimulated_population)
            for proj in projection_list:
                ### skip excluded populations
                if proj.post in self.excluded_populations_list:
                    continue
                ### activate axon transmission
                proj.axon_transmission = 1
                proj.p_axon_spike_trans = 1
                ### set prob_axon_spike for spiking model
                proj.pre.prob_axon_spike = self._axon_spikes_per_pulse_to_prob(
                    axon_spikes_per_pulse
                )
                ### set axon_rate_amp for rate-coded model
                if proj.pre in axon_rate_amp.keys():
                    ### axon_rate_amp is specified for this population
                    proj.pre.axon_rate_amp = axon_rate_amp[proj.pre]
                else:
                    ### axon_rate_amp is not specified for this population, use default value
                    proj.pre.axon_rate_amp = axon_rate_amp["default"]

        if afferents:
            ### activate all afferent projections
            projection_list = projections(post=self.stimulated_population)
            for proj in projection_list:
                ### skip excluded populations
                if proj.pre in self.excluded_populations_list:
                    continue
                ### activate axon transmission
                proj.axon_transmission = 1
                proj.p_axon_spike_trans = 1
                ### set prob_axon_spike for spiking model
                proj.pre.prob_axon_spike = self._axon_spikes_per_pulse_to_prob(
                    axon_spikes_per_pulse
                )
                ### set axon_rate_amp for rate-coded model
                if proj.pre in axon_rate_amp.keys():
                    ### axon_rate_amp is specified for this population
                    proj.pre.axon_rate_amp = axon_rate_amp[proj.pre]
                else:
                    ### axon_rate_amp is not specified for this population, use default value
                    proj.pre.axon_rate_amp = axon_rate_amp["default"]

        if passing_fibres:
            ### activate all passing projections
            for proj_idx, proj in enumerate(self.passing_fibres_list):
                proj.axon_transmission = 1
                proj.p_axon_spike_trans = passing_fibres_strength[proj_idx]
                ### set prob_axon_spike for spiking model
                proj.pre.prob_axon_spike = self._axon_spikes_per_pulse_to_prob(
                    axon_spikes_per_pulse
                )
                ### set axon_rate_amp for rate-coded model
                if proj.pre in axon_rate_amp.keys():
                    ### axon_rate_amp is specified for this population
                    proj.pre.axon_rate_amp = axon_rate_amp[proj.pre]
                else:
                    ### axon_rate_amp is not specified for this population, use default value
                    proj.pre.axon_rate_amp = axon_rate_amp["default"]

    def _set_antidromic(
        self,
        efferents: bool,
        afferents: bool,
        passing_fibres: bool,
        passing_fibres_strength: list[float],
        sum_branches: bool,
        axon_spikes_per_pulse: float,
    ):
        """
        Set antidromic axon spikes forwarding.

        Args:
            efferents (bool):
                If True, DBS affects the efferents of the stimulated population
                (orthodromic and/or antidromic have to be True too)
            afferents (bool):
                If True, DBS affects the afferents of the stimulated population
                (orthodromic and/or antidromic have to be True too)
            passing_fibres (bool):
                If True, DBS affects the passing fibres of the stimulated population
                (orthodromic and/or antidromic have to be True too and there have to
                be passing fibres in the passing_fibres_list)
            passing_fibres_strength (list[float]):
                List of float values between 0 and 1 defining how strong the passing
                fibres are activated by DBS (0: not activated, 1: fully activated
                like projections in DBS stimulated region)
            sum_branches (bool):
                If True, the antidromic_prob of a presynaptic population (defining how
                many axon spikes affect the pop antidromically) of passing fibres is
                the sum of the passing_fibres_strengths of the single axon branches.
            axon_spikes_per_pulse (float):
                Number of average axon spikes per DBS pulse
        """

        if efferents:
            ### activate all efferent projections, i.e. antodromic activation of stimulated population
            pop = self.stimulated_population
            pop.antidromic = 1
            pop.antidromic_prob = 1
            pop.prob_axon_spike = self._axon_spikes_per_pulse_to_prob(
                axon_spikes_per_pulse
            )
        if afferents:
            ### activate all afferent projections, i.e. all presynaptic populations of stimulated population
            ### get presynaptic projections
            projection_list = projections(post=self.stimulated_population)
            ### get presynaptic populations from projections
            presyn_pop_list = []
            presyn_pop_name_list = []
            for proj in projection_list:
                ### skip excluded populations
                if proj.pre in self.excluded_populations_list:
                    continue
                ### check if presynaptic population is already in list
                if proj.pre.name not in presyn_pop_name_list:
                    presyn_pop_name_list.append(proj.pre.name)
                    presyn_pop_list.append(proj.pre)
            ### set antidromic for all presynaptic populations
            for pop in presyn_pop_list:
                pop.antidromic = 1
                pop.antidromic_prob = np.mean(self.stimulated_population.dbs_on)
                pop.prob_axon_spike = self._axon_spikes_per_pulse_to_prob(
                    axon_spikes_per_pulse
                )
        if passing_fibres:
            ### get presynaptic populations from passing fibres projections
            presyn_pop_list = []
            presyn_pop_name_list = []
            for proj in self.passing_fibres_list:
                ### check if presynaptic population is already in list
                if proj.pre.name not in presyn_pop_name_list:
                    presyn_pop_name_list.append(proj.pre.name)
                    presyn_pop_list.append(proj.pre)
            ### get antidomic_prob for each presynatic population with the passing_fibres_strength
            antidromic_prob_list = [0] * len(presyn_pop_list)
            for pop_idx, pop in enumerate(presyn_pop_list):
                ### get all passing fibres and their strength of a presynaptic pop
                passing_fibres_strength_of_pop_list = []
                for proj_idx, proj in enumerate(self.passing_fibres_list):
                    if proj.pre.name == pop.name:
                        passing_fibres_strength_of_pop_list.append(
                            passing_fibres_strength[proj_idx]
                        )
                ### check if the probs of the single axon branches should be summed up
                ### if for example a presynaptic pop contributes to two passing fibres, the axons of the presynaptic pop split up into two branches
                ### thus, if these two branches are both stimulated, they both forward APs antidromic
                ### thus, sum up the antidromic_prob of the single branches to obtain the antidromic spikes which affect the presynaptic pop
                ### if sum_branches is False, then this would represent that the stimulation at the axon is before it splits up into multiple branches and there should not be different passing_fibres_strengths for the same presynaptic pop
                if sum_branches:
                    antidromic_prob_list[pop_idx] = sum(
                        passing_fibres_strength_of_pop_list
                    )
                else:
                    if len(set(passing_fibres_strength_of_pop_list)) != 1:
                        ### list contains different values
                        raise ValueError(
                            "Different passing fibres strengths for the same presynaptic population detected. This is not possible if sum_branches is False."
                        )
                    ### all values are the same, thus take the first one
                    antidromic_prob_list[pop_idx] = passing_fibres_strength_of_pop_list[
                        0
                    ]

                ### TODO
                ### if summing axon branches leads to a prob > 1, then
                ### the prob should be set to 1
                ### the axon spike generation in this pop should be increased
                ### and all axon spike transmissions from this pop should be decreased by the same factor
                ### this is not implemented yet... maybe in the future
                if antidromic_prob_list[pop_idx] > 1:
                    raise ValueError(
                        "Summing the passing fibres strengths of a presynaptic population leads to a antidromic spike probability > 1. This is not possible yet."
                    )

            ### set antidromic for all presynaptic populations
            for pop_idx, pop in enumerate(presyn_pop_list):
                pop.antidromic = 1
                pop.antidromic_prob = antidromic_prob_list[pop_idx]
                pop.prob_axon_spike = self._axon_spikes_per_pulse_to_prob(
                    axon_spikes_per_pulse
                )

    @check_types()
    def on(
        self,
        population_proportion: float | None = None,
        dbs_depolarization: float | None = None,
        orthodromic: bool | None = None,
        antidromic: bool | None = None,
        efferents: bool | None = None,
        afferents: bool | None = None,
        passing_fibres: bool | None = None,
        passing_fibres_strength: float | list[float] | None = None,
        sum_branches: bool | None = None,
        axon_spikes_per_pulse: float | None = None,
        axon_rate_amp: float | dict[Population | str, float] | None = None,
        seed: int | None = None,
    ):
        """
        Activate DBS.

        Args:
            population_proportion (float, optional):
                Proportion of the stimulated population which is affected by DBS,
                neurons are distributed randomly. Default: None, i.e., use value from
                initialization
            dbs_depolarization (float, optional):
                Depolarization effect of the DBS pulse to local soma. Default: None,
                i.e., use value from initialization
            orthodromic (bool, optional):
                If True, DBS causes axonal spikes which are forwarded orthodromically.
                Default: None, i.e., use value from initialization
            antidromic (bool, optional):
                If True, DBS causes axonal spikes which are forwarded antidromically,
                only available in spiking networks. Default: None, i.e., use value from
                initialization
            efferents (bool, optional):
                If True, DBS affects the efferents of the stimulated population
                (orthodromic and/or antidromic have to be True too). Default: None,
                i.e., use value from initialization
            afferents (bool, optional):
                If True, DBS affects the afferents of the stimulated population
                (orthodromic and/or antidromic have to be True too). Default: None,
                i.e., use value from initialization
            passing_fibres (bool, optional):
                If True, DBS affects the passing fibres of the stimulated region defined
                in passing_fibres_list (orthodromic and/or antidromic have to be True
                too). Default: None, i.e., use value from initialization
            passing_fibres_strength (float | list[float], optional):
                Single value or list of float values between 0 and 1 defining how strong
                the passing fibres are activated by DBS (0: not activated, 1: fully
                activated like the projections in the DBS stimulated region).
                Default: None, i.e., use value from initialization
            sum_branches (bool, optional):
                If True, the antidromic_prob of a presynaptic population (defining how
                many axon spikes affect the pop antidromically) of passing fibres is
                the sum of the passing_fibres_strengths of the single axon branches.
                Default: None, i.e., use value from initialization
            axon_spikes_per_pulse (float, optional):
                Number of average axon spikes per DBS pulse. Default: None, i.e., use
                value from initialization
            axon_rate_amp (float | dict[Population | str, float], optional):
                Similar to prob_axon_spike in spiking model. Which rate is forwarded on
                axons caused by DBS. You can specify this for each population
                individually by using a dictionary (keys = Population instances)
                axon_rate_amp = {pop: 1.5} --> the efferent axons of pop forward a rate
                of 1.5 during DBS (all other affected projections forward the default
                value). You can specify the default value by using the key "default",
                e.g. {pop: 1.5, "default": 1.0} -> efferent axons of all populations
                except pop forward a rate of 1.0 during DBS. Default: None, i.e., use
                value from initialization
            seed (int, optional):
                Seed for the random number generator. Default: None, i.e., use value
                from initialization
        """

        ### set DBS on for all populations
        ### also sets the proportion of affected neurons, call this before set_depolarization and set_axon_spikes!
        self._set_dbs_on(population_proportion, seed)

        ### set depolarization of population
        self._set_depolarization(dbs_depolarization)

        ### set axon spikes forwarding
        self._set_axon_spikes(
            orthodromic,
            antidromic,
            efferents,
            afferents,
            passing_fibres,
            passing_fibres_strength,
            sum_branches,
            axon_spikes_per_pulse,
            axon_rate_amp,
        )

    def _set_dbs_on(self, population_proportion: float | None, seed: int | None):
        """
        Set DBS on for all populations, for the stimulated population only the specified
        proportion is affected by DBS.

        Args:
            population_proportion (float, optional):
                Proportion of the stimulated population which is affected by DBS,
                neurons are distributed randomly. Default: None, i.e., use value from
                initialization
            seed (int, optional):
                Seed for the random number generator. Default: None, i.e., use value
                from initialization
        """
        ### set parameters for the creation of the DBS on array
        ### either use given population_proportion or use default value
        if isinstance(population_proportion, type(None)):
            population_proportion = self.population_proportion
        ### either use given seed or use default value
        if isinstance(seed, type(None)):
            seed = self.seed

        ### if seed and population_propotion are the same as in the initialization, use the same dbs_on_array
        if seed == self.seed and population_proportion == self.population_proportion:
            ### use the same dbs_on_array as in the initialization
            dbs_on_array = self.dbs_on_array
        else:
            ### create new dbs_on_array
            dbs_on_array = self._create_dbs_on_array(population_proportion, seed)

        ### set DBS on for all populations
        for pop in populations():
            ### of the stimulated population only the specified proportion is affected by DBS
            if pop == self.stimulated_population:
                pop.dbs_on = dbs_on_array
            else:
                pop.dbs_on = 1

    def off(self):
        """
        Deactivate DBS.
        """
        ### set DBS off for all populations
        for pop in populations():
            pop.dbs_on = 0
            pop.prob_axon_spike = 0
            pop.axon_rate_amp = 0

        ### deactivate DBS axon transmission
        self._deactivate_axon_DBS()

    def update_pointers(self, pointer_list):
        """
        Update pointers to populations and projections after recreating the model.

        Args:
            pointer_list (list):
                List of pointers to populations and projections

        Returns:
            pointer_list_new (list):
                List of pointers to populations and projections of the new model
        """
        ### update pointers
        pointer_list_new: list[Population | Projection] = []
        for pointer in pointer_list:
            compartment_name = pointer.name
            if isinstance(pointer, Population):
                pointer_list_new.append(get_population(compartment_name))
            elif isinstance(pointer, Projection):
                pointer_list_new.append(get_projection(compartment_name))
            else:
                raise TypeError(
                    f"Pointer {pointer} is neither a Population nor a Projection"
                )
        return pointer_list_new

__init__(stimulated_population, population_proportion=1.0, excluded_populations_list=[], dbs_depolarization=0.0, orthodromic=False, antidromic=False, efferents=False, afferents=False, passing_fibres=False, passing_fibres_list=[], passing_fibres_strength=1.0, sum_branches=True, dbs_pulse_frequency_Hz=130.0, dbs_pulse_width_us=300.0, axon_spikes_per_pulse=1.0, axon_rate_amp=1.0, seed=None, auto_implement=False, model=None) #

Initialize DBS stimulator.

Warning

Do this before compiling the model!

Parameters:

Name Type Description Default
stimulated_population Population

Population which is stimulated by DBS

required
population_proportion float

Proportion of the stimulated population which is affected by DBS, neurons are distributed randomly. Default: 1.0.

1.0
excluded_populations_list list

List of populations which are excluded from DBS effects, they are not affected and their axons do not generate axon spikes. Default: [].

[]
dbs_depolarization float

Depolarization effect of the DBS pulse to local soma. Default: 0.0.

0.0
orthodromic bool

If True, DBS causes axonal spikes which are forwarded orthodromically. Default: False.

False
antidromic bool

If True, DBS causes axonal spikes which are forwarded antidromically, only available in spiking networks. Default: False.

False
efferents bool

If True, DBS affects the efferents of the stimulated population (orthodromic and/or antidromic have to be True too). Default: False.

False
afferents bool

If True, DBS affects the afferents of the stimulated population (orthodromic and/or antidromic have to be True too). Default: False.

False
passing_fibres bool

If True, DBS affects the passing fibres of the stimulated region defined in passing_fibres_list (orthodromic and/or antidromic have to be True too). Default: False.

False
passing_fibres_list list of Projections

List of projections which pass the DBS stimulated region and therefore are activated by DBS. Default: [], also set passing_fibres True!

[]
passing_fibres_strength float or list of float

Single value or list of float values between 0 and 1 defining how strong the passing fibres are activated by DBS (0: not activated, 1: fully activated like the projections in the DBS stimulated region). Default: 1.

1.0
sum_branches bool

If True, the antidromic_prob of a presynaptic population (defining how many axon spikes affect the pop antidromically) of passing fibres is the sum of the passing_fibres_strengths of the single axon branches. Default: True.

True
dbs_pulse_frequency_Hz float

Frequency of the DBS pulse. Default: 130 Hz.

130.0
dbs_pulse_width_us float

Width of the DBS pulse. Default: 300 us.

300.0
axon_spikes_per_pulse float

Number of average axon spikes per DBS pulse. Default: 1.

1.0
axon_rate_amp float or dict of float

Similar to prob_axon_spike in spiking model. Which rate is forwarded on axons caused by DBS. You can specify this for each population individually by using a dictionary (keys = Population instances) axon_rate_amp = {pop: 1.5} --> the efferent axons of pop forward a rate of 1.5 during DBS (all other affected projections forward the default value) You can specify the default value by using the key "default", e.g. {pop: 1.5, "default": 1.0} -> efferent axons of all populations except pop forward a rate of 1.0 during DBS. Default: 1.0.

1.0
seed int

Seed for the random distribution of affected neurons based on population_proportion. Default: None.

None
auto_implement bool

If True, automatically implement DBS mechanisms to the model. Only supported for Izhikevich spiking models and rate-coded models. Default: False. TODO test what happens with mixed models

False
model generate_model

CompNeuroPy model which is used to automatically implement DBS mechanisms, should not be compiled!. Default: None, i.e., use all populations and projections of the current magic model

None
Source code in src/CompNeuroPy/dbs.py
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
@check_types()
def __init__(
    self,
    stimulated_population: Population,
    population_proportion: float = 1.0,
    excluded_populations_list: list[Population] = [],
    dbs_depolarization: float = 0.0,
    orthodromic: bool = False,
    antidromic: bool = False,
    efferents: bool = False,
    afferents: bool = False,
    passing_fibres: bool = False,
    passing_fibres_list: list[Projection] = [],
    passing_fibres_strength: float | list[float] = 1.0,
    sum_branches: bool = True,
    dbs_pulse_frequency_Hz: float = 130.0,
    dbs_pulse_width_us: float = 300.0,
    axon_spikes_per_pulse: float = 1.0,
    axon_rate_amp: float | dict[Population | str, float] = 1.0,
    seed: int | None = None,
    auto_implement: bool = False,
    model: generate_model | None = None,
) -> None:
    """
    Initialize DBS stimulator.

    !!! warning
        Do this before compiling the model!

    Args:
        stimulated_population (Population):
            Population which is stimulated by DBS
        population_proportion (float, optional):
            Proportion of the stimulated population which is affected by DBS,
            neurons are distributed randomly. Default: 1.0.
        excluded_populations_list (list, optional):
            List of populations which are excluded from DBS effects, they are not
            affected and their axons do not generate axon spikes. Default: [].
        dbs_depolarization (float, optional):
            Depolarization effect of the DBS pulse to local soma. Default: 0.0.
        orthodromic (bool, optional):
            If True, DBS causes axonal spikes which are forwarded orthodromically.
            Default: False.
        antidromic (bool, optional):
            If True, DBS causes axonal spikes which are forwarded antidromically,
            only available in spiking networks. Default: False.
        efferents (bool, optional):
            If True, DBS affects the efferents of the stimulated population
            (orthodromic and/or antidromic have to be True too). Default: False.
        afferents (bool, optional):
            If True, DBS affects the afferents of the stimulated population
            (orthodromic and/or antidromic have to be True too). Default: False.
        passing_fibres (bool, optional):
            If True, DBS affects the passing fibres of the stimulated region defined
            in passing_fibres_list (orthodromic and/or antidromic have to be True
            too). Default: False.
        passing_fibres_list (list of Projections, optional):
            List of projections which pass the DBS stimulated region and therefore
            are activated by DBS. Default: [], also set passing_fibres True!
        passing_fibres_strength (float or list of float, optional):
            Single value or list of float values between 0 and 1 defining how strong
            the passing fibres are activated by DBS (0: not activated, 1: fully
            activated like the projections in the DBS stimulated region).
            Default: 1.
        sum_branches (bool, optional):
            If True, the antidromic_prob of a presynaptic population (defining how
            many axon spikes affect the pop antidromically) of passing fibres is
            the sum of the passing_fibres_strengths of the single axon branches.
            Default: True.
        dbs_pulse_frequency_Hz (float, optional):
            Frequency of the DBS pulse. Default: 130 Hz.
        dbs_pulse_width_us (float, optional):
            Width of the DBS pulse. Default: 300 us.
        axon_spikes_per_pulse (float, optional):
            Number of average axon spikes per DBS pulse. Default: 1.
        axon_rate_amp (float or dict of float, optional):
            Similar to prob_axon_spike in spiking model. Which rate is forwarded on
            axons caused by DBS. You can specify this for each population
            individually by using a dictionary (keys = Population instances)
            axon_rate_amp = {pop: 1.5} --> the efferent axons of pop forward a rate
            of 1.5 during DBS (all other affected projections forward the default
            value)
            You can specify the default value by using the key "default", e.g.
            {pop: 1.5, "default": 1.0} -> efferent axons of all populations except
            pop forward a rate of 1.0 during DBS. Default: 1.0.
        seed (int, optional):
            Seed for the random distribution of affected neurons based on
            population_proportion. Default: None.
        auto_implement (bool, optional):
            If True, automatically implement DBS mechanisms to the model. Only
            supported for Izhikevich spiking models and rate-coded models.
            Default: False.
            TODO test what happens with mixed models
        model (generate_model, optional):
            CompNeuroPy model which is used to automatically implement DBS
            mechanisms, should not be compiled!. Default: None, i.e., use all
            populations and projections of the current magic model
    """

    if auto_implement:
        ### recreate model with DBS mechanisms
        ### give all variables containing Populations and Projections
        ### and also recreate them during recreating the model
        ### variables are:
        ### - stimulated_population
        ### - excluded_populations_list
        ### - passing_fibres_list
        ### - axon_rate_amp
        if not isinstance(model, type(None)):
            ### CompNeuroPy model given
            ### recreate model with DBS mechanisms
            create_dbs_model_obj = _CreateDBSmodelcnp(
                model,
                stimulated_population,
                excluded_populations_list,
                passing_fibres_list,
                axon_rate_amp,
            )
            ### get the new CompNeuroPy model
            model = create_dbs_model_obj.model
        else:
            ### no CompNeuroPy model given --> use all populations and projections of the current magic model
            ### recreate model with DBS mechanisms
            create_dbs_model_obj = _CreateDBSmodel(
                stimulated_population,
                excluded_populations_list,
                passing_fibres_list,
                axon_rate_amp,
            )
        ### get the new variables containing Populations and Projections
        stimulated_population = create_dbs_model_obj.stimulated_population
        excluded_populations_list = create_dbs_model_obj.excluded_populations_list
        passing_fibres_list = create_dbs_model_obj.passing_fibres_list
        axon_rate_amp = create_dbs_model_obj.axon_rate_amp

    ### set parameters
    self.stimulated_population = stimulated_population
    self.population_proportion = population_proportion
    self.excluded_populations_list = excluded_populations_list
    self.dbs_depolarization = dbs_depolarization
    self.orthodromic = orthodromic
    self.antidromic = antidromic
    self.efferents = efferents
    self.afferents = afferents
    self.passing_fibres = passing_fibres
    self.passing_fibres_list = passing_fibres_list
    self.passing_fibres_strength = passing_fibres_strength
    self.sum_branches = sum_branches
    self.dbs_pulse_width_us = dbs_pulse_width_us
    self.axon_spikes_per_pulse = axon_spikes_per_pulse
    self.axon_rate_amp = axon_rate_amp
    self.seed = seed
    self.model = model

    ### ANNarchy constants for DBS
    self._set_constants(dbs_pulse_frequency_Hz)

    ### randomly select affected neurons i.e. create dbs_on_array
    self.dbs_on_array = self._create_dbs_on_array(population_proportion, seed)

on(population_proportion=None, dbs_depolarization=None, orthodromic=None, antidromic=None, efferents=None, afferents=None, passing_fibres=None, passing_fibres_strength=None, sum_branches=None, axon_spikes_per_pulse=None, axon_rate_amp=None, seed=None) #

Activate DBS.

Parameters:

Name Type Description Default
population_proportion float

Proportion of the stimulated population which is affected by DBS, neurons are distributed randomly. Default: None, i.e., use value from initialization

None
dbs_depolarization float

Depolarization effect of the DBS pulse to local soma. Default: None, i.e., use value from initialization

None
orthodromic bool

If True, DBS causes axonal spikes which are forwarded orthodromically. Default: None, i.e., use value from initialization

None
antidromic bool

If True, DBS causes axonal spikes which are forwarded antidromically, only available in spiking networks. Default: None, i.e., use value from initialization

None
efferents bool

If True, DBS affects the efferents of the stimulated population (orthodromic and/or antidromic have to be True too). Default: None, i.e., use value from initialization

None
afferents bool

If True, DBS affects the afferents of the stimulated population (orthodromic and/or antidromic have to be True too). Default: None, i.e., use value from initialization

None
passing_fibres bool

If True, DBS affects the passing fibres of the stimulated region defined in passing_fibres_list (orthodromic and/or antidromic have to be True too). Default: None, i.e., use value from initialization

None
passing_fibres_strength float | list[float]

Single value or list of float values between 0 and 1 defining how strong the passing fibres are activated by DBS (0: not activated, 1: fully activated like the projections in the DBS stimulated region). Default: None, i.e., use value from initialization

None
sum_branches bool

If True, the antidromic_prob of a presynaptic population (defining how many axon spikes affect the pop antidromically) of passing fibres is the sum of the passing_fibres_strengths of the single axon branches. Default: None, i.e., use value from initialization

None
axon_spikes_per_pulse float

Number of average axon spikes per DBS pulse. Default: None, i.e., use value from initialization

None
axon_rate_amp float | dict[Population | str, float]

Similar to prob_axon_spike in spiking model. Which rate is forwarded on axons caused by DBS. You can specify this for each population individually by using a dictionary (keys = Population instances) axon_rate_amp = {pop: 1.5} --> the efferent axons of pop forward a rate of 1.5 during DBS (all other affected projections forward the default value). You can specify the default value by using the key "default", e.g. {pop: 1.5, "default": 1.0} -> efferent axons of all populations except pop forward a rate of 1.0 during DBS. Default: None, i.e., use value from initialization

None
seed int

Seed for the random number generator. Default: None, i.e., use value from initialization

None
Source code in src/CompNeuroPy/dbs.py
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
@check_types()
def on(
    self,
    population_proportion: float | None = None,
    dbs_depolarization: float | None = None,
    orthodromic: bool | None = None,
    antidromic: bool | None = None,
    efferents: bool | None = None,
    afferents: bool | None = None,
    passing_fibres: bool | None = None,
    passing_fibres_strength: float | list[float] | None = None,
    sum_branches: bool | None = None,
    axon_spikes_per_pulse: float | None = None,
    axon_rate_amp: float | dict[Population | str, float] | None = None,
    seed: int | None = None,
):
    """
    Activate DBS.

    Args:
        population_proportion (float, optional):
            Proportion of the stimulated population which is affected by DBS,
            neurons are distributed randomly. Default: None, i.e., use value from
            initialization
        dbs_depolarization (float, optional):
            Depolarization effect of the DBS pulse to local soma. Default: None,
            i.e., use value from initialization
        orthodromic (bool, optional):
            If True, DBS causes axonal spikes which are forwarded orthodromically.
            Default: None, i.e., use value from initialization
        antidromic (bool, optional):
            If True, DBS causes axonal spikes which are forwarded antidromically,
            only available in spiking networks. Default: None, i.e., use value from
            initialization
        efferents (bool, optional):
            If True, DBS affects the efferents of the stimulated population
            (orthodromic and/or antidromic have to be True too). Default: None,
            i.e., use value from initialization
        afferents (bool, optional):
            If True, DBS affects the afferents of the stimulated population
            (orthodromic and/or antidromic have to be True too). Default: None,
            i.e., use value from initialization
        passing_fibres (bool, optional):
            If True, DBS affects the passing fibres of the stimulated region defined
            in passing_fibres_list (orthodromic and/or antidromic have to be True
            too). Default: None, i.e., use value from initialization
        passing_fibres_strength (float | list[float], optional):
            Single value or list of float values between 0 and 1 defining how strong
            the passing fibres are activated by DBS (0: not activated, 1: fully
            activated like the projections in the DBS stimulated region).
            Default: None, i.e., use value from initialization
        sum_branches (bool, optional):
            If True, the antidromic_prob of a presynaptic population (defining how
            many axon spikes affect the pop antidromically) of passing fibres is
            the sum of the passing_fibres_strengths of the single axon branches.
            Default: None, i.e., use value from initialization
        axon_spikes_per_pulse (float, optional):
            Number of average axon spikes per DBS pulse. Default: None, i.e., use
            value from initialization
        axon_rate_amp (float | dict[Population | str, float], optional):
            Similar to prob_axon_spike in spiking model. Which rate is forwarded on
            axons caused by DBS. You can specify this for each population
            individually by using a dictionary (keys = Population instances)
            axon_rate_amp = {pop: 1.5} --> the efferent axons of pop forward a rate
            of 1.5 during DBS (all other affected projections forward the default
            value). You can specify the default value by using the key "default",
            e.g. {pop: 1.5, "default": 1.0} -> efferent axons of all populations
            except pop forward a rate of 1.0 during DBS. Default: None, i.e., use
            value from initialization
        seed (int, optional):
            Seed for the random number generator. Default: None, i.e., use value
            from initialization
    """

    ### set DBS on for all populations
    ### also sets the proportion of affected neurons, call this before set_depolarization and set_axon_spikes!
    self._set_dbs_on(population_proportion, seed)

    ### set depolarization of population
    self._set_depolarization(dbs_depolarization)

    ### set axon spikes forwarding
    self._set_axon_spikes(
        orthodromic,
        antidromic,
        efferents,
        afferents,
        passing_fibres,
        passing_fibres_strength,
        sum_branches,
        axon_spikes_per_pulse,
        axon_rate_amp,
    )

off() #

Deactivate DBS.

Source code in src/CompNeuroPy/dbs.py
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
def off(self):
    """
    Deactivate DBS.
    """
    ### set DBS off for all populations
    for pop in populations():
        pop.dbs_on = 0
        pop.prob_axon_spike = 0
        pop.axon_rate_amp = 0

    ### deactivate DBS axon transmission
    self._deactivate_axon_DBS()

update_pointers(pointer_list) #

Update pointers to populations and projections after recreating the model.

Parameters:

Name Type Description Default
pointer_list list

List of pointers to populations and projections

required

Returns:

Name Type Description
pointer_list_new list

List of pointers to populations and projections of the new model

Source code in src/CompNeuroPy/dbs.py
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
def update_pointers(self, pointer_list):
    """
    Update pointers to populations and projections after recreating the model.

    Args:
        pointer_list (list):
            List of pointers to populations and projections

    Returns:
        pointer_list_new (list):
            List of pointers to populations and projections of the new model
    """
    ### update pointers
    pointer_list_new: list[Population | Projection] = []
    for pointer in pointer_list:
        compartment_name = pointer.name
        if isinstance(pointer, Population):
            pointer_list_new.append(get_population(compartment_name))
        elif isinstance(pointer, Projection):
            pointer_list_new.append(get_projection(compartment_name))
        else:
            raise TypeError(
                f"Pointer {pointer} is neither a Population nor a Projection"
            )
    return pointer_list_new