Skip to content

Commit

Permalink
added integer index option
Browse files Browse the repository at this point in the history
  • Loading branch information
fwitte committed May 14, 2019
1 parent af6db92 commit ae072d8
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions cydets/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pandas as pd


def detect_cycles(series, drop_zero_amplitudes=True):
def detect_cycles(series, drop_zero_docs=True, integer_index=False):
r"""
Detect cycles in a time series with information on start, end and depths.
Expand Down Expand Up @@ -38,6 +38,12 @@ def detect_cycles(series, drop_zero_amplitudes=True):
series : pandas.core.series.Series
Input time series.
drop_zero_docs : bool
Drop cycles with doc = 0 at the end?
integer_index : bool
Use an integer index instead of the original input time series index?
Returns
-------
df : pandas.core.frame.DataFrame
Expand Down Expand Up @@ -94,14 +100,25 @@ def detect_cycles(series, drop_zero_amplitudes=True):

# write data to DataFrame
df = pd.DataFrame()
df['t_start'] = series.iloc[cycles[:, 0]]['id'].values
df['t_end'] = series.iloc[cycles[:, 1]]['id'].values
df['t_minimum'] = series.iloc[cycles[:, 2]]['id'].values
if integer_index is True:
# use the integer index as time stamps
df['t_start'] = cycles[:, 0]
df['t_end'] = cycles[:, 1]
df['t_minimum'] = cycles[:, 2]

else:
# use original index as time stamps
df['t_start'] = series.iloc[cycles[:, 0]]['id'].values
df['t_end'] = series.iloc[cycles[:, 1]]['id'].values
df['t_minimum'] = series.iloc[cycles[:, 2]]['id'].values

# write depth of cycle in DataFrame
df['doc'] = cycles[:, 3]
# calculate duration
df['duration'] = df['t_end'] - df['t_start']

# drop cycles where the amplitude (doc) is zero
if drop_zero_amplitudes is True:
if drop_zero_docs is True:
df = df.drop(df[df['doc'] == 0].index)

# reset the index
Expand Down

0 comments on commit ae072d8

Please sign in to comment.