Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update window_* backend functions' keyword args to arguments #543

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions lib/explorer/backend/lazy_series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,7 @@ defmodule Explorer.Backend.LazySeries do

for op <- @window_fun_operations do
@impl true
def unquote(op)(%Series{} = series, window_size, opts) do
weights = Keyword.fetch!(opts, :weights)
min_periods = Keyword.fetch!(opts, :min_periods)
center = Keyword.fetch!(opts, :center)

def unquote(op)(%Series{} = series, window_size, weights, min_periods, center) do
args = [lazy_series!(series), window_size, weights, min_periods, center]

if aggregations?(args), do: raise_agg_inside_window(unquote(op))
Expand Down
37 changes: 28 additions & 9 deletions lib/explorer/backend/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,34 @@ defmodule Explorer.Backend.Series do

# Rolling

@type window_option ::
{:weights, [float()] | nil}
| {:min_periods, integer() | nil}
| {:center, boolean()}

@callback window_sum(s, window_size :: integer(), [window_option()]) :: s
@callback window_min(s, window_size :: integer(), [window_option()]) :: s
@callback window_max(s, window_size :: integer(), [window_option()]) :: s
@callback window_mean(s, window_size :: integer(), [window_option()]) :: s
@callback window_sum(
s,
window_size :: integer(),
weights :: [float()] | nil,
min_periods :: integer() | nil,
center :: boolean()
) :: s
@callback window_min(
s,
window_size :: integer(),
weights :: [float()] | nil,
min_periods :: integer() | nil,
center :: boolean()
) :: s
@callback window_max(
s,
window_size :: integer(),
weights :: [float()] | nil,
min_periods :: integer() | nil,
center :: boolean()
) :: s
@callback window_mean(
s,
window_size :: integer(),
weights :: [float()] | nil,
min_periods :: integer() | nil,
center :: boolean()
) :: s

# Exponentially weighted windows

Expand Down
22 changes: 9 additions & 13 deletions lib/explorer/polars_backend/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -376,30 +376,26 @@ defmodule Explorer.PolarsBackend.Series do
# Window

@impl true
def window_max(series, window_size, opts) do
window_function(:s_window_max, series, window_size, opts)
def window_max(series, window_size, weights, min_periods, center) do
window_function(:s_window_max, series, window_size, weights, min_periods, center)
end

@impl true
def window_mean(series, window_size, opts) do
window_function(:s_window_mean, series, window_size, opts)
def window_mean(series, window_size, weights, min_periods, center) do
window_function(:s_window_mean, series, window_size, weights, min_periods, center)
end

@impl true
def window_min(series, window_size, opts) do
window_function(:s_window_min, series, window_size, opts)
def window_min(series, window_size, weights, min_periods, center) do
window_function(:s_window_min, series, window_size, weights, min_periods, center)
end

@impl true
def window_sum(series, window_size, opts) do
window_function(:s_window_sum, series, window_size, opts)
def window_sum(series, window_size, weights, min_periods, center) do
window_function(:s_window_sum, series, window_size, weights, min_periods, center)
end

defp window_function(operation, series, window_size, opts) do
weights = Keyword.fetch!(opts, :weights)
min_periods = Keyword.fetch!(opts, :min_periods)
center = Keyword.fetch!(opts, :center)

defp window_function(operation, series, window_size, weights, min_periods, center) do
Shared.apply_series(series, operation, [window_size, weights, min_periods, center])
end

Expand Down
14 changes: 7 additions & 7 deletions lib/explorer/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3042,7 +3042,7 @@ defmodule Explorer.Series do
"""
@doc type: :window
def window_sum(series, window_size, opts \\ []),
do: Shared.apply_impl(series, :window_sum, [window_size, window_opts_with_defaults(opts)])
do: Shared.apply_impl(series, :window_sum, [window_size | window_args(opts)])

@doc """
Calculate the rolling mean, given a window size and optional list of weights.
Expand Down Expand Up @@ -3075,7 +3075,7 @@ defmodule Explorer.Series do
"""
@doc type: :window
def window_mean(series, window_size, opts \\ []),
do: Shared.apply_impl(series, :window_mean, [window_size, window_opts_with_defaults(opts)])
do: Shared.apply_impl(series, :window_mean, [window_size | window_args(opts)])

@doc """
Calculate the rolling min, given a window size and optional list of weights.
Expand Down Expand Up @@ -3108,7 +3108,7 @@ defmodule Explorer.Series do
"""
@doc type: :window
def window_min(series, window_size, opts \\ []),
do: Shared.apply_impl(series, :window_min, [window_size, window_opts_with_defaults(opts)])
do: Shared.apply_impl(series, :window_min, [window_size | window_args(opts)])

@doc """
Calculate the rolling max, given a window size and optional list of weights.
Expand Down Expand Up @@ -3141,11 +3141,11 @@ defmodule Explorer.Series do
"""
@doc type: :window
def window_max(series, window_size, opts \\ []),
do: Shared.apply_impl(series, :window_max, [window_size, window_opts_with_defaults(opts)])
do: Shared.apply_impl(series, :window_max, [window_size | window_args(opts)])

defp window_opts_with_defaults(opts) do
defaults = [weights: nil, min_periods: 1, center: false]
Keyword.validate!(opts, defaults)
defp window_args(opts) do
opts = Keyword.validate!(opts, weights: nil, min_periods: 1, center: false)
[opts[:weights], opts[:min_periods], opts[:center]]
end

@doc """
Expand Down