From 821718e40651754a295e34e187ce81eb8fe536ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Al=C3=A1n=20Mu=C3=B1oz?= <amuoz@ed.ac.uk> Date: Tue, 13 Jul 2021 23:50:13 +0100 Subject: [PATCH] add template and peaks Former-commit-id: 14e1d1850aa6d8f4d122eb65ff81749c48fd9e9c --- core/processes/peaks.py | 44 ++++++++++++++++++++++++++++++++++++++ core/processes/savgol.py | 3 +-- core/processes/template.py | 29 +++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 core/processes/peaks.py create mode 100644 core/processes/template.py diff --git a/core/processes/peaks.py b/core/processes/peaks.py new file mode 100644 index 00000000..15ddf950 --- /dev/null +++ b/core/processes/peaks.py @@ -0,0 +1,44 @@ +from scipy.signal import argrelmax, argrelmin +from postprocessor.core.processes.base import ParametersABC, ProcessABC + + +class PeaksParameters(ParametersABC): + """ + Parameters + type : str {minima, maxima, "all"}. Determines which type of peaks to identify + order : int Parameter to pass to scipy.signal.argrelextrema indicating + how many points to use for comparison. + """ + + def __init__(self, type): + self.type = type + + @classmethod + def default(cls): + return cls.from_dict({"type": "minima", "order": 3}) + + +class Peaks(ProcessABC): + """ + Identifies a signal sharply dropping. + """ + + def __init__(self, parameters: PeaksParameters): + super().__init__(parameters) + + def run(self, signal: pd.DataFrame): + """ + Returns a boolean dataframe with the same shape as the + original signal but with peaks as true values. + """ + peaks_mat = np.zeros_like(signal, dtype=bool) + + comparator = np.less if self.parameters.type is "minima" else np.greater + peaks_ids = argrelextrema(new_df, comparator=comparator, order=order) + peaks_mat[peak_ids] = True + + return pd.DataFrame( + peaks_mat, + index=signal.index, + columns=signal.columns, + ) diff --git a/core/processes/savgol.py b/core/processes/savgol.py index 79fcdcaa..b006c422 100644 --- a/core/processes/savgol.py +++ b/core/processes/savgol.py @@ -25,7 +25,6 @@ class Savgol(ProcessABC): """ Apply Savitzky-Golay filter (works with NaNs, but it might return NaN regions). - """ def __init__(self, parameters: SavgolParameters): @@ -38,7 +37,7 @@ class Savgol(ProcessABC): return signal.apply(savgol_on_srs, 1) @staticmethod - def non_uniform_savgol(x, y, window, polynom): + def non_uniform_savgol(x, y, window: int, polynom: int): """ Applies a Savitzky-Golay filter to y with non-uniform spacing as defined in x diff --git a/core/processes/template.py b/core/processes/template.py new file mode 100644 index 00000000..075f21b2 --- /dev/null +++ b/core/processes/template.py @@ -0,0 +1,29 @@ +from postprocessor.core.processes.base import ParametersABC, ProcessABC +from postprocessor.core.functions.tracks import clean_tracks, merge_tracks, join_tracks + + +class ParametersTemplate(ParametersABC): + """ + Parameters + """ + + def __init__( + self, + ): + super().__init__() + + @classmethod + def default(cls): + return cls.from_dict({}) + + +class ProcessTemplate(ProcessABC): + """ + Template for process class. + """ + + def __init__(self, parameters: ParametersTemplate): + super().__init__(parameters) + + def run(self): + pass -- GitLab