Skip to content

Commit bf55dca

Browse files
thumbnail plotting in validation (#27)
1 parent f969624 commit bf55dca

3 files changed

Lines changed: 75 additions & 7 deletions

File tree

bluecellulab/analysis/analysis.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ def validate(self, soma_amp, dend_amps, dend_dist, apic_amps, apic_dist):
350350
validated = True
351351
notes = ""
352352
popt_dend, popt_apic = self.fit(soma_amp, dend_amps, dend_dist, apic_amps, apic_dist)
353-
logging.warning(popt_dend)
354353
if dend_amps is not None:
355354
plt.cla()
356355
plt.plot([0], soma_amp, '.')

bluecellulab/validation/validation.py

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from bluecellulab.analysis.inject_sequence import run_multirecordings_stimulus
2626
from bluecellulab.analysis.inject_sequence import run_stimulus
2727
from bluecellulab.cell.core import Cell
28+
from bluecellulab.simulation.neuron_globals import NeuronGlobals
2829
from bluecellulab.stimulus.factory import IDRestTimings
2930
from bluecellulab.stimulus.factory import StimulusFactory
3031
from bluecellulab.tools import calculate_input_resistance
@@ -33,15 +34,17 @@
3334
logger = logging.getLogger(__name__)
3435

3536

36-
def plot_trace(recording, out_dir, fname, title):
37+
def plot_trace(recording, out_dir, fname, title, plot_current=True):
3738
"""Plot a trace with inout current given a recording."""
3839
outpath = out_dir / fname
3940
fig, ax1 = plt.subplots(figsize=(10, 6))
4041
plt.plot(recording.time, recording.voltage, color="black")
41-
current_axis = ax1.twinx()
42-
current_axis.plot(recording.time, recording.current, color="gray", alpha=0.6)
43-
current_axis.set_ylabel("Stimulus Current [nA]")
44-
fig.suptitle(title)
42+
if plot_current:
43+
current_axis = ax1.twinx()
44+
current_axis.plot(recording.time, recording.current, color="gray", alpha=0.6)
45+
current_axis.set_ylabel("Stimulus Current [nA]")
46+
if title:
47+
fig.suptitle(title)
4548
ax1.set_xlabel("Time [ms]")
4649
ax1.set_ylabel("Voltage [mV]")
4750
fig.tight_layout()
@@ -410,20 +413,61 @@ def fi_test(template_params, rheobase, out_dir, spike_threshold_voltage=-30.):
410413
}
411414

412415

416+
def thumbnail_test(template_params, rheobase, out_dir):
417+
"""Thumbnail test: creating a thumbnail."""
418+
stim_factory = StimulusFactory(dt=1.0)
419+
step_stimulus = stim_factory.idrest(threshold_current=rheobase, threshold_percentage=130)
420+
recording = run_stimulus(
421+
template_params,
422+
step_stimulus,
423+
"soma[0]",
424+
0.5,
425+
add_hypamp=True,
426+
)
427+
428+
# plotting
429+
outpath = plot_trace(
430+
recording,
431+
out_dir,
432+
fname="thumbnail.pdf",
433+
title="",
434+
plot_current=False
435+
)
436+
437+
return {
438+
"name": "thumbnail",
439+
"passed": True,
440+
"validation_details": "",
441+
"figures": [outpath],
442+
}
443+
444+
413445
def run_validations(
414-
cell, cell_name, spike_threshold_voltage=-30, output_dir="./memodel_validation_figures"
446+
cell,
447+
cell_name,
448+
spike_threshold_voltage=-30,
449+
v_init=-80.0,
450+
celsius=34.0,
451+
output_dir="./memodel_validation_figures"
415452
):
416453
"""Run all the validations on the cell.
417454
418455
Args:
419456
cell (Cell): The cell to validate.
420457
cell_name (str): The name of the cell, used in the output directory.
421458
spike_threshold_voltage (float): The voltage threshold for spike detection.
459+
v_init: Initial membrane potential. Default is -80.0 mV.
460+
celsius: Temperature in Celsius. Default is 34.0.
422461
output_dir (str): The directory to save the validation figures.
423462
"""
424463
out_dir = pathlib.Path(output_dir) / cell_name
425464
out_dir.mkdir(parents=True, exist_ok=True)
426465

466+
# set initial voltage and temperature
467+
neuron_globals = NeuronGlobals.get_instance()
468+
neuron_globals.temperature = celsius
469+
neuron_globals.v_init = v_init
470+
427471
# get me-model properties
428472
holding_current = cell.hypamp if cell.hypamp else 0.0
429473
if cell.threshold:
@@ -473,6 +517,9 @@ def run_validations(
473517
# Validation 9: FI Test
474518
fi_test_result = fi_test(cell.template_params, rheobase, out_dir, spike_threshold_voltage)
475519

520+
# Validation 10: Thumbnail Test
521+
thumbnail_result = thumbnail_test(cell.template_params, rheobase, out_dir)
522+
476523
return {
477524
"memodel_properties": {
478525
"holding_current": holding_current,
@@ -487,4 +534,5 @@ def run_validations(
487534
"rin_test": rin_result,
488535
"iv_test": iv_test_result,
489536
"fi_test": fi_test_result,
537+
"thumbnail_test": thumbnail_result,
490538
}

tests/test_validation/test_validation.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,24 @@ def test_fi_test(mock_Cell, mock_compute, dummy_template_params, dummy_out_dir):
406406
)
407407

408408

409+
@patch("bluecellulab.validation.validation.plot_trace")
410+
@patch("bluecellulab.validation.validation.run_stimulus")
411+
def test_thumnail_test(
412+
mock_run_stimulus, mock_plot_trace, dummy_template_params, dummy_out_dir
413+
):
414+
# passed case
415+
rec = MagicMock()
416+
rec.spike = [1]
417+
mock_run_stimulus.return_value = rec
418+
mock_plot_trace.return_value = dummy_out_dir / "thumbnail.pdf"
419+
result = validation.thumbnail_test(dummy_template_params, 1.0, dummy_out_dir)
420+
assert result["passed"] is True
421+
assert len(result["figures"]) == 1
422+
assert result["figures"][0] == dummy_out_dir / "thumbnail.pdf"
423+
assert result["validation_details"] == ""
424+
assert result["name"] == "thumbnail"
425+
426+
409427
@patch("bluecellulab.validation.validation.calculate_rheobase")
410428
@patch("bluecellulab.validation.validation.calculate_input_resistance")
411429
@patch("bluecellulab.validation.validation.spiking_test")
@@ -416,7 +434,9 @@ def test_fi_test(mock_Cell, mock_compute, dummy_template_params, dummy_out_dir):
416434
@patch("bluecellulab.validation.validation.rin_test")
417435
@patch("bluecellulab.validation.validation.iv_test")
418436
@patch("bluecellulab.validation.validation.fi_test")
437+
@patch("bluecellulab.validation.validation.thumbnail_test")
419438
def test_run_validations(
439+
mock_thumbnail,
420440
mock_fi,
421441
mock_iv,
422442
mock_rin,
@@ -447,6 +467,7 @@ def test_run_validations(
447467
mock_rin.return_value = {"passed": True}
448468
mock_iv.return_value = {"passed": True}
449469
mock_fi.return_value = {"passed": True}
470+
mock_thumbnail.return_value = {"passed": True}
450471
result = validation.run_validations(cell, "cellname")
451472
assert result["spiking_test"]["passed"] is True
452473
assert result["depolarization_block_test"]["passed"] is True

0 commit comments

Comments
 (0)