Skip to content

Commit

Permalink
Warn the user when no spikes are detected while the voltage is above the
Browse files Browse the repository at this point in the history
detection threshold
  • Loading branch information
Tanguy Pierre Louis Damart committed Oct 17, 2022
1 parent c73276f commit b8d3944
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
3 changes: 3 additions & 0 deletions bluepyefe/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ def append(self, recording):
recording.files
)

if recording.auto_threshold is not None:
self.feature_targets[i]._auto_thresholds.append(recording.auto_threshold)

self.recordings.append(recording)

def as_dict(self):
Expand Down
37 changes: 31 additions & 6 deletions bluepyefe/recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def __init__(self, config_data, reader_data, protocol_name):
)

self.export_attr = None
self.auto_threshold = None

@property
def time(self):
Expand Down Expand Up @@ -212,6 +213,12 @@ def call_efel(self, efeatures, efel_settings=None):
efel_settings = {}

settings = {"stimulus_current": self.amp}

if "Threshold" not in settings and self.auto_threshold is not None:
logger.warning(f"Threshold was not provided and was automatically"
f" set to {self.auto_threshold}")
settings["Threshold"] = self.auto_threshold

for setting in efel_settings:
if setting not in ['stim_start', 'stim_end']:
settings[setting] = efel_settings[setting]
Expand Down Expand Up @@ -249,15 +256,14 @@ def compute_efeatures(
efel_settings (dict): eFEL settings in the form
{setting_name: setting_value}.
"""

if efeature_names is None:
efeature_names = efeatures

for i, f in enumerate(efeatures):
if efeature_names[i] is None:
efeature_names[i] = f

efel_vals = self.call_efel(efeatures, efel_settings)
efel_vals = self.call_efel(efeatures, tmp_settings)
for efeature_name, efeature in zip(efeature_names, efeatures):

value = efel_vals[0][efeature]
Expand All @@ -266,14 +272,33 @@ def compute_efeatures(

self.efeatures[efeature_name] = numpy.nanmean(value)

def compute_spikecount(self, efel_settings=None):
def compute_spikecount(self, efel_settings=None, max_threshold_voltage=30):
"""Compute the number of spikes in the trace"""

tmp_settings = {'strict_stiminterval': True}
tmp_settings.update(efel_settings)
efel_vals = self.call_efel(['peak_time'], tmp_settings)
if efel_settings is not None:
tmp_settings.update(efel_settings)

if tmp_settings.get("Threshold", None) is None:
for thresh in range(-30, max_threshold_voltage, 10):
tmp_settings["Threshold"] = thresh
efel_vals = self.call_efel(['peak_time'], tmp_settings)
spikecount = len(efel_vals[0]['peak_time'])
if spikecount != 0 or numpy.max(self.voltage) < thresh:
self.auto_threshold = thresh
break

else:
efel_vals = self.call_efel(['peak_time'], tmp_settings)
spikecount = len(efel_vals[0]['peak_time'])

if spikecount == 0 and numpy.max(self.voltage) > tmp_settings["Threshold"]:
logger.warning(
f"No spikes were detected in recording {self.files} but the "
"voltage goes higher than the spike detection threshold."
)

self.spikecount = len(efel_vals[0]['peak_time'])
self.spikecount = spikecount

def get_plot_amplitude_title(self):
return " ({:.01f}%)".format(self.amp_rel)
Expand Down
12 changes: 12 additions & 0 deletions bluepyefe/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def __init__(

self._values = []
self._files = []
self._auto_thresholds = []

@property
def mean(self):
Expand Down Expand Up @@ -119,9 +120,18 @@ def clear(self):
self._values = []
self._files = []

def add_effective_threshold(self):
"""If auto threshold detection was used during feature extraction,
update the efel settings with the Threshold that was actually used"""

if self._auto_thresholds:
self.efel_settings["Threshold"] = numpy.median(self._auto_thresholds)

def as_dict(self):
"""Returns the target in the form of a dictionary"""

self.add_effective_threshold()

return {
"efeature_name": self.efeature_name,
"feature": self.efel_feature_name,
Expand All @@ -137,6 +147,8 @@ def as_dict(self):
def as_legacy_dict(self, save_files_used=False):
"""Returns the target in the form of a dictionary in a legacy format"""

self.add_effective_threshold()

std = self.std
if std == 0.0:
logger.warning(
Expand Down

0 comments on commit b8d3944

Please sign in to comment.