Skip to content

Commit

Permalink
MNT: refactor the remove outliers method
Browse files Browse the repository at this point in the history
  • Loading branch information
Gui-FernandesBR authored Feb 26, 2024
1 parent ccf0c6b commit 44758a8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 37 deletions.
41 changes: 6 additions & 35 deletions rocketpy/mathutils/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -1144,41 +1144,7 @@ def low_pass_filter(self, alpha, file_path=None):
title=self.title,
)

def remove_outliers(self, method="iqr", **kwargs):
"""Remove outliers from the Function source using the specified method.
Parameters
----------
method : string, optional
Method to be used to remove outliers. Options are 'iqr'.
Default is 'iqr'.
**kwargs : optional
Keyword arguments to be passed to specific methods according to the
selected method.
If the selected method is the 'iqr', then the following kwargs are
available:
- threshold : float
Threshold for the interquartile range method. Default is 1.5.
Returns
-------
Function
The new Function object without outliers.
"""
if callable(self.source):
print("Cannot remove outliers if the source is a callable object.")
return self

if method.lower() == "iqr":
return self.__remove_outliers_iqr(**kwargs)
else:
print(
f"Method '{method}' not recognized. No outliers removed."
+ "Please use one of the following supported methods: 'iqr'."
)
return self

def __remove_outliers_iqr(self, threshold=1.5):
def remove_outliers_iqr(self, threshold=1.5):
"""Remove outliers from the Function source using the interquartile
range method.
Expand All @@ -1196,6 +1162,11 @@ def __remove_outliers_iqr(self, threshold=1.5):
----------
[1] https://en.wikipedia.org/wiki/Outlier#Tukey's_fences
"""

if callable(self.source):
print("Cannot remove outliers if the source is a callable object.")
return self

Check warning on line 1168 in rocketpy/mathutils/function.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/mathutils/function.py#L1167-L1168

Added lines #L1167 - L1168 were not covered by tests

x = self.x_array
y = self.y_array
y_q1 = np.percentile(y, 25)
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ def test_set_discrete_based_on_model_non_mutator(linear_func):
],
)
def test_remove_outliers_iqr(x, y, expected_x, expected_y):
"""Test the function __remove_outliers_iqr which is expected to remove
"""Test the function remove_outliers_iqr which is expected to remove
outliers from the data based on the Interquartile Range (IQR) method.
"""
func = Function(source=np.column_stack((x, y)))
filtered_func = func.remove_outliers(method="iqr", threshold=1.5)
filtered_func = func.remove_outliers_iqr(threshold=1.5)

# Check if the outliers are removed
assert np.array_equal(filtered_func.x_array, expected_x)
Expand Down

0 comments on commit 44758a8

Please sign in to comment.