Skip to content

Commit

Permalink
Add numpy_function
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-tomasini committed Feb 12, 2025
1 parent 694ae12 commit 84d14cf
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Utility Functions documentation

general_function
polars_function
polars_shapely_function
shapely_function
polars_shapely_function
numpy_function
networkx_function
7 changes: 7 additions & 0 deletions docs/numpy_function.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
numpy\_function
=======================

.. automodule:: numpy_function
:members:
:undoc-members:
:show-inheritance:
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ packages = [
{include = "networkx_function.py", from="src"},
{include = "shapely_function.py", from="src"},
{include = "polars_shapely_function.py", from="src"},
{include = "numpy_function.py", from="src"},
]


Expand Down
30 changes: 30 additions & 0 deletions src/numpy_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import numpy as np

def relative_error_within_boundaries(x: np.array, low: np.array, high: np.array) -> np.array: # type: ignore
"""
Calculate the relative error of values within specified boundaries.
Args:
x (np.array): The array of values.
low (np.array): The lower boundary array.
high (np.array): The upper boundary array.
Returns:
np.array: The array of relative errors.
"""
return np.abs(error_within_boundaries(x, low, high))/x

def error_within_boundaries(x: np.array, low: np.array, high: np.array) -> np.array: # type: ignore
"""
Calculate the error of values within specified boundaries.
Args:
x (np.array): The array of values.
low (np.array): The lower boundary array.
high (np.array): The upper boundary array.
Returns:
np.array: The array of errors.
"""
nearest_boundary = np.where(x < low, low, np.where(x > high, high, x))
return x - nearest_boundary
53 changes: 53 additions & 0 deletions src/polars_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,56 @@ def concat_list_of_list(col_list: pl.Expr) -> pl.Expr:
return pl.concat_list(
col_list.map_elements(lambda x: [x], return_dtype=pl.List(pl.List(pl.Float64)))
)


def linear_interpolation_for_bound(x_col: pl.Expr, y_col: pl.Expr) -> pl.Expr:
"""
Perform linear interpolation for boundary values in a column.
Args:
x_col (pl.Expr): The x-axis column.
y_col (pl.Expr): The y-axis column to interpolate.
Returns:
pl.Expr: The interpolated y-axis column.
"""
a_diff: pl.Expr = y_col.diff()/x_col.diff()
x_diff: pl.Expr = x_col.diff().backward_fill()
y_diff: pl.Expr = pl.coalesce(
pl.when(y_col.is_null().or_(y_col.is_nan()))
.then(a_diff.forward_fill()*x_diff)
.otherwise(pl.lit(0)).cum_sum(),
pl.when(y_col.is_null().or_(y_col.is_nan()))
.then(-a_diff.backward_fill()*x_diff)
.otherwise(pl.lit(0)).cum_sum(reverse=True)
)

return y_col.backward_fill().forward_fill() + y_diff

def linear_interpolation_using_cols(
df: pl.DataFrame, x_col: str, y_col: Union[list[str], str]
) -> pl.DataFrame:
"""
Perform linear interpolation on specified columns of a DataFrame.
Args:
df (pl.DataFrame): The DataFrame containing the data.
x_col (str): The name of the x-axis column.
y_col (Union[list[str], str]): The name(s) of the y-axis column(s) to interpolate.
Returns:
pl.DataFrame: The DataFrame with interpolated y-axis columns.
"""
df = df.sort(x_col)
x = df[x_col].to_numpy()
if isinstance(y_col, str):
y_col = [y_col]
for col in y_col:
y = df[col].to_numpy()
mask = ~np.isnan(y)
df = df.with_columns(
pl.Series(np.interp(x, x[mask], y[mask], left=np.nan, right=np.nan)).fill_nan(None).alias(col)
).with_columns(
linear_interpolation_for_bound(x_col=c(x_col), y_col=c(col)).alias(col)
)
return df

0 comments on commit 84d14cf

Please sign in to comment.