Skip to content

Small error in Check Troughs function and naming convention #88

@HTalNachmani

Description

@HTalNachmani

Hi! Thank you for creating this tool - peakdet has been incredibly useful for my project so far!

I've noticed a couple of small things errors:

  1. Very small thing is that when converting phys2neurokit (super useful function, thank you!), the data is saved with name f"{modality}_Raw" even if you have cleaned data if copy_data is set to True. This caused me some confusion - I just want others to know that the 'raw' is only in the name - if you have clean data, that's what gets copied!

  2. utils/check_troughs function has a comparison sign instead of assignment which causes the manual peak correction to error, indicating that there is a mismatch between peaks and troughs and no index troughs[-1] exists. I've changed it like this and it works.

Original code:

def check_troughs(data, peaks, troughs=None):
"""
Confirms that troughs exists between every set of peaks in data

Parameters
----------
data : array-like
    Input data for which `troughs` and `peaks` were detected
peaks : array-like
    Indices of suspected peak locations in `data`
troughs : array-like or None, optional
    Indices of suspected troughs locations in `data`, if any.

Returns
-------
troughs : np.ndarray
    Indices of trough locations in `data`, dependent on `peaks`
"""
# If there's a trough after all peaks, keep it.
if troughs is not None and troughs[-1] > peaks[-1]:
    all_troughs = np.zeros(peaks.size, dtype=int)
    all_troughs[-1] == troughs[-1]                                             ### ERROR HERE - could simply change to one =. 
else:
    all_troughs = np.zeros(peaks.size - 1, dtype=int)

for f in range(peaks.size - 1):
    dp = data[peaks[f] : peaks[f + 1]]
    idx = peaks[f] + np.argwhere(dp == dp.min())[0]
    all_troughs[f] = idx

return all_troughs

How I've edited:

def check_troughs(data, peaks, troughs=None):
"""
Confirms that troughs exists between every set of peaks in data

Parameters
----------
data : array-like
    Input data for which `troughs` and `peaks` were detected
peaks : array-like
    Indices of suspected peak locations in `data`
troughs : array-like or None, optional
    Indices of suspected troughs locations in `data`, if any.

Returns
-------
troughs : np.ndarray
    Indices of trough locations in `data`, dependent on `peaks`
"""
# Handle edge cases
if peaks is None or len(peaks) == 0:
    return np.array([])

if len(peaks) == 1:
    return np.array([])

has_final_trough = (troughs is not None and 
                   len(troughs) > 0 and 
                   len(peaks) > 0 and 
                   troughs[-1] > peaks[-1])

# If there's a trough after all peaks, keep it.
if has_final_trough:
    all_troughs = np.zeros(peaks.size, dtype=int)
    all_troughs[-1] = troughs[-1]
    n_inter_troughs = peaks.size - 1
else:
    all_troughs = np.zeros(peaks.size - 1, dtype=int)
    n_inter_troughs = peaks.size - 1

for f in range(n_inter_troughs):
    dp = data[peaks[f] : peaks[f + 1]]
    min_idx = np.argmin(dp)
    all_troughs[f] = peaks[f] + min_idx
    
    # idx = peaks[f] + np.argwhere(dp == dp.min())[0]
    # all_troughs[f] = idx

return all_troughs

Metadata

Metadata

Assignees

Labels

BugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions