Skip to content

Commit

Permalink
Respond to PR feedback
Browse files Browse the repository at this point in the history
- Extract out centroid as a var
- Raise exception and add Literal type sig
- Switch order of branch evaluation
  • Loading branch information
Scott-Simmons committed Jan 19, 2025
1 parent caf6839 commit 987d677
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions tsfresh/feature_extraction/feature_calculators.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import warnings
from builtins import range
from collections import defaultdict
from typing import Literal

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -723,9 +724,9 @@ def variation_coefficient(x):
:return type: float
"""
avg = np.mean(x)
if avg != 0:
return np.std(x) / avg
return np.nan
if avg == 0:
return np.nan
return np.std(x) / avg


@set_property("fctype", "simple")
Expand Down Expand Up @@ -1095,16 +1096,17 @@ def fft_coefficient(x, param):

fft = np.fft.rfft(x)

def complex_agg(x, agg):
def complex_agg(x, agg: Literal["real", "imag", "angle", "abs"]):
if agg == "real":
return x.real
if agg == "imag":
elif agg == "imag":
return x.imag
if agg == "abs":
elif agg == "abs":
return np.abs(x)
if agg == "angle":
elif agg == "angle":
return np.angle(x, deg=True)
return None
else:
raise ValueError('`agg` must be "real", "imag", "angle" or "abs"')

res = [
complex_agg(fft[config["coeff"]], config["attr"])
Expand Down Expand Up @@ -1185,9 +1187,10 @@ def get_skew(y):
# the skew blows up as variance --> 0, hence return nan when variance is smaller than a resolution of 0.5:
if var < 0.5:
return np.nan
return (
get_moment(y, 3) - 3 * get_centroid(y) * var - get_centroid(y) ** 3
) / get_variance(y) ** (1.5)
centroid = get_centroid(y)
return (get_moment(y, 3) - 3 * centroid * var - centroid**3) / get_variance(
y
) ** (1.5)

def get_kurtosis(y):
"""
Expand All @@ -1205,11 +1208,12 @@ def get_kurtosis(y):
# the kurtosis blows up as variance --> 0, hence return nan when variance is smaller than a resolution of 0.5:
if var < 0.5:
return np.nan
centroid = get_centroid(y)
return (
get_moment(y, 4)
- 4 * get_centroid(y) * get_moment(y, 3)
+ 6 * get_moment(y, 2) * get_centroid(y) ** 2
- 3 * get_centroid(y)
- 4 * centroid * get_moment(y, 3)
+ 6 * get_moment(y, 2) * centroid**2
- 3 * centroid
) / get_variance(y) ** 2

calculation = {
Expand Down

0 comments on commit 987d677

Please sign in to comment.