Skip to content

Commit

Permalink
add documentation and some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-khong committed Jul 1, 2023
1 parent a35f551 commit 362e0d1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/explorer/backend/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ defmodule Explorer.Backend.Series do
@callback log(argument :: s, base :: float()) :: s
@callback exp(s) :: s
@callback abs(s) :: s
@callback elemwise_min(s | number(), s | number()) :: s
@callback elemwise_max(s | number(), s | number()) :: s
@callback elemwise_min(s, s | number()) :: s
@callback elemwise_max(s, s | number()) :: s

# Trigonometry

Expand Down
58 changes: 56 additions & 2 deletions lib/explorer/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4149,15 +4149,69 @@ defmodule Explorer.Series do
do: dtype_error("abs/1", dtype, [:integer, :float])

@doc """
TODO
Finds the element-wise min.
When mixing floats and integers, the resulting series will have dtype `:float`.
Note that any comparison to `:nan` and `nil` always favours the element from
the `right` series.
## Supported dtypes
* `:integer`
* `:float`
## Examples
iex> s = Series.from_list([-50, 5, nil, 50])
iex> Series.min(s, 10)
#Explorer.Series<
Polars[4]
integer [-50, 5, nil, 10]
>
iex> left = Series.from_list([-100, nil, nil, :nan, :nan, 20])
iex> right = Series.from_list([-50, -999, :nan, -999, nil, 10])
iex> Series.min(left, right)
#Explorer.Series<
Polars[6]
float [-100.0, -999.0, NaN, -999.0, nil, 10.0]
>
"""
@doc type: :element_wise
@spec min(left :: Series.t() | number(), right :: Series.t() | number()) :: Series.t()
def min(%Series{} = left, right),
do: basic_numeric_operation(:elemwise_min, left, right)

@doc """
TODO
Finds the element-wise max.
When mixing floats and integers, the resulting series will have dtype `:float`.
Note that any comparison to `:nan` and `nil` always favours the element from
the `right` series.
## Supported dtypes
* `:integer`
* `:float`
## Examples
iex> s = Series.from_list([-50, 5, nil, 50])
iex> Series.max(s, 1)
#Explorer.Series<
Polars[4]
integer [1, 5, 1, 50]
>
iex> left = Series.from_list([-100, nil, nil, :nan, :nan, 20])
iex> right = Series.from_list([-50, -999, :nan, -999, nil, 10])
iex> Series.max(left, right)
#Explorer.Series<
Polars[6]
float [-50.0, -999.0, NaN, -999.0, nil, 20.0]
>
"""
@doc type: :element_wise
@spec max(left :: Series.t() | number(), right :: Series.t() | number()) :: Series.t()
Expand Down
7 changes: 6 additions & 1 deletion test/explorer/data_frame_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,12 @@ defmodule Explorer.DataFrameTest do
df1 = DF.mutate(df, c: max(a, b), d: min(a, b))

assert df1 |> DF.to_columns(atom_keys: true) ==
%{a: [-50, 5, nil, 50], b: [-100, 10, nil, 100], c: [-50, 10, nil, 100], d: [-100, 5, nil, 50]}
%{
a: [-50, 5, nil, 50],
b: [-100, 10, nil, 100],
c: [-50, 10, nil, 100],
d: [-100, 5, nil, 50]
}
end

test "correlation and covariance" do
Expand Down
22 changes: 21 additions & 1 deletion test/explorer/series_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2791,20 +2791,40 @@ defmodule Explorer.SeriesTest do
s = Series.from_list([-50, 5, nil, 50])
assert Series.min(s, 10) |> Series.to_list() == [-50, 5, nil, 10]
assert Series.max(s, 1) |> Series.to_list() == [1, 5, 1, 50]
assert Series.min(s, 1) |> Series.dtype() == :integer
assert Series.min(s, 1.0) |> Series.dtype() == :float

s1 = Series.from_list([-100, 10, nil, 100])
assert Series.min(s, s1) |> Series.to_list() == [-100, 5, nil, 50]
assert Series.max(s, s1) |> Series.to_list() == [-50, 10, nil, 100]
end

test "handle nans and infinities correctly" do
s = Series.from_list([:neg_infinity, 5, :nan, :infinity])
s = Series.from_list([:neg_infinity, 5.0, :nan, :infinity])
assert Series.min(s, 10) |> Series.to_list() == [:neg_infinity, 5, 10, 10]
assert Series.max(s, 1) |> Series.to_list() == [1, 5, 1, :infinity]

s1 = Series.from_list([-100, 10, nil, 100])
assert Series.min(s, s1) |> Series.to_list() == [:neg_infinity, 5, nil, 100]
assert Series.max(s, s1) |> Series.to_list() == [-100, 10, nil, :infinity]

s2 = Series.from_list([nil, nil, :nan, :nan])
s3 = Series.from_list([nil, :nan, nil, :nan])
assert Series.min(s2, s3) |> Series.to_list() == [nil, :nan, nil, :nan]
assert Series.max(s2, s3) |> Series.to_list() == [nil, :nan, nil, :nan]
assert Series.min(s3, s2) |> Series.to_list() == [nil, nil, :nan, :nan]
assert Series.max(s3, s2) |> Series.to_list() == [nil, nil, :nan, :nan]
end

test "nils with scalars and series" do
s1 = Series.from_list([1, nil])
s2 = Series.from_list([1, 1])
assert Series.min(s1, s2) |> Series.to_list() == [1, 1]
assert Series.max(s1, s2) |> Series.to_list() == [1, 1]
assert Series.min(s2, s1) |> Series.to_list() == [1, nil]
assert Series.max(s2, s1) |> Series.to_list() == [1, nil]
assert Series.min(s1, 1) |> Series.to_list() == [1, nil]
assert Series.max(s1, 1) |> Series.to_list() == [1, 1]
end
end

Expand Down

0 comments on commit 362e0d1

Please sign in to comment.