Skip to content

Commit c2b155f

Browse files
authored
Merge pull request #1395 from brian-team/fix_threshold_location
Fix using indexed morphology for threshold_location
2 parents 063b9ad + 506ec78 commit c2b155f

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

brian2/spatialneuron/spatialneuron.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,12 @@ def __init__(self, morphology=None, model=None, threshold=None,
246246
if hasattr(threshold_location,
247247
'_indices'): # assuming this is a method
248248
threshold_location = threshold_location._indices()
249-
# for now, only a single compartment allowed
250-
if len(threshold_location) == 1:
251-
threshold_location = threshold_location[0]
252-
else:
253-
raise AttributeError("Threshold can only be applied on a "
254-
"single location")
249+
# for now, only a single compartment allowed
250+
try:
251+
treshold_location = int(threshold_location)
252+
except TypeError:
253+
raise AttributeError("Threshold can only be applied on a "
254+
"single location")
255255
threshold = f"({threshold}) and (i == {str(threshold_location)})"
256256

257257
# Check flags (we have point currents)

brian2/tests/test_spatialneuron.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,51 @@ def test_point_current():
822822
assert 'I1/area' in neuron.equations['Im'].expr.code
823823
assert 'I2/area' in neuron.equations['Im'].expr.code # see issue #1160
824824

825+
@pytest.mark.standalone_compatible
826+
@pytest.mark.multiple_runs
827+
def test_spatialneuron_threshold_location():
828+
morpho = Soma(10*um)
829+
morpho.axon = Cylinder(1*um, n=2, length=20*um)
830+
model = '''
831+
Im = 0*nA/cm**2 : amp/meter**2
832+
should_spike : boolean (constant)
833+
'''
834+
neuron = SpatialNeuron(morpho, model, threshold_location=morpho.axon[15*um],
835+
threshold='should_spike')
836+
# Different variants that should do the same thing
837+
neuron2 = SpatialNeuron(morpho, model, threshold_location=morpho.axon.indices[15*um],
838+
threshold='should_spike')
839+
neuron3 = SpatialNeuron(morpho, model, threshold_location=2,
840+
threshold='should_spike')
841+
# Cannot use multiple compartments
842+
with pytest.raises(AttributeError):
843+
SpatialNeuron(morpho, model, threshold_location=[2, 3],
844+
threshold='should_spike')
845+
with pytest.raises(AttributeError):
846+
SpatialNeuron(morpho, model, threshold_location=morpho.axon[5*um:15*um],
847+
threshold='should_spike')
848+
neurons = [neuron, neuron2, neuron3]
849+
monitors = [SpikeMonitor(n) for n in neurons]
850+
851+
net = Network(neurons, monitors)
852+
for n in neurons:
853+
n.should_spike = True # all compartments want to spike
854+
net.run(defaultclock.dt)
855+
for n in neurons:
856+
n.should_spike = False # no compartment wants to spike
857+
net.run(defaultclock.dt)
858+
for n in neurons:
859+
n.should_spike = [False, False, True]
860+
net.run(defaultclock.dt)
861+
for n in neurons:
862+
n.should_spike = [True, True, False]
863+
net.run(defaultclock.dt)
864+
device.build(direct_call=False, **device.build_options)
865+
for mon in monitors:
866+
assert len(mon.i) == 2
867+
assert all(mon.i == 2)
868+
assert_allclose(mon.t, [0*ms, 2*defaultclock.dt])
869+
825870

826871
if __name__ == '__main__':
827872
test_custom_events()

0 commit comments

Comments
 (0)