Skip to content

Commit

Permalink
Fix inspect between nodes (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jhonatannunessilva authored Dec 29, 2023
1 parent f3d6666 commit 5c51c26
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 44 deletions.
81 changes: 52 additions & 29 deletions lib/explorer/backend/data_frame.ex
Original file line number Diff line number Diff line change
Expand Up @@ -250,48 +250,71 @@ defmodule Explorer.Backend.DataFrame do
Default inspect implementation for backends.
"""
def inspect(df, backend, n_rows, inspect_opts, opts \\ [])
when is_binary(backend) and (is_integer(n_rows) or is_nil(n_rows)) and is_list(opts) do
when is_binary(backend) and (is_integer(n_rows) or is_nil(n_rows) or is_binary(n_rows)) and
is_list(opts) do
inspect_opts = %{inspect_opts | limit: @default_limit}
elide_columns? = Keyword.get(opts, :elide_columns, false)
open = A.color("[", :list, inspect_opts)
close = A.color("]", :list, inspect_opts)

cols_algebra =
for name <- DataFrame.names(df) do
series = df[name]

series =
case inspect_opts.limit do
:infinity -> series
limit when is_integer(limit) -> Series.slice(series, 0, limit + 1)
end

data =
series
|> Series.to_list()
|> Explorer.Shared.to_doc(inspect_opts)

type =
series
|> Series.dtype()
|> Explorer.Shared.dtype_to_string()

A.concat([
A.line(),
A.color("#{name} ", :map, inspect_opts),
A.color("#{type} ", :atom, inspect_opts),
data
])
end
cols_algebra = build_cols_algebra(df, inspect_opts, elide_columns?)
df_info = if(elide_columns?, do: n_rows, else: "#{n_rows || "???"} x #{length(cols_algebra)}")

A.concat([
A.color(backend, :atom, inspect_opts),
open,
"#{n_rows || "???"} x #{length(cols_algebra)}",
df_info,
close,
groups_algebra(df.groups, inspect_opts) | cols_algebra
])
end

defp build_cols_algebra(df, inspect_opts, true) do
for name <- DataFrame.names(df) do
type =
df
|> DataFrame.dtypes()
|> Map.get(name)
|> Explorer.Shared.dtype_to_string()

A.concat([
A.line(),
A.color("#{name} ", :map, inspect_opts),
A.color("#{type} ", :atom, inspect_opts),
"???"
])
end
end

defp build_cols_algebra(df, inspect_opts, _elide_columns?) do
for name <- DataFrame.names(df) do
series = df[name]

series =
case inspect_opts.limit do
:infinity -> series
limit when is_integer(limit) -> Series.slice(series, 0, limit + 1)
end

data =
series
|> Series.to_list()
|> Explorer.Shared.to_doc(inspect_opts)

type =
series
|> Series.dtype()
|> Explorer.Shared.dtype_to_string()

A.concat([
A.line(),
A.color("#{name} ", :map, inspect_opts),
A.color("#{type} ", :atom, inspect_opts),
data
])
end
end

defp groups_algebra([_ | _] = groups, opts),
do:
A.concat([
Expand Down
31 changes: 20 additions & 11 deletions lib/explorer/backend/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ defmodule Explorer.Backend.Series do
Default inspect implementation for backends.
"""
def inspect(series, backend, n_rows, inspect_opts, opts \\ [])
when is_binary(backend) and (is_integer(n_rows) or is_nil(n_rows)) and is_list(opts) do
when is_binary(backend) and (is_integer(n_rows) or is_nil(n_rows) or is_binary(n_rows)) and
is_list(opts) do
elide_columns? = Keyword.get(opts, :elide_columns, false)
open = A.color("[", :list, inspect_opts)
close = A.color("]", :list, inspect_opts)

Expand All @@ -331,16 +333,7 @@ defmodule Explorer.Backend.Series do

dtype = A.color("#{type} ", :atom, inspect_opts)

series =
case inspect_opts.limit do
:infinity -> series
limit when is_integer(limit) -> Series.slice(series, 0, limit + 1)
end

data =
series
|> Series.to_list()
|> Explorer.Shared.to_doc(inspect_opts)
data = build_series_data(series, inspect_opts, elide_columns?)

A.concat([
A.color(backend, :atom, inspect_opts),
Expand All @@ -352,4 +345,20 @@ defmodule Explorer.Backend.Series do
data
])
end

defp build_series_data(_series, _inspect_opts, true) do
"???"
end

defp build_series_data(series, inspect_opts, _elide_columns?) do
series =
case inspect_opts.limit do
:infinity -> series
limit when is_integer(limit) -> Series.slice(series, 0, limit + 1)
end

series
|> Series.to_list()
|> Explorer.Shared.to_doc(inspect_opts)
end
end
6 changes: 6 additions & 0 deletions lib/explorer/polars_backend/data_frame.ex
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,12 @@ defmodule Explorer.PolarsBackend.DataFrame do
# Inspect

@impl true
def inspect(df, opts) when node(df.data.resource) != node() do
Explorer.Backend.DataFrame.inspect(df, "Polars", "node: #{node(df.data.resource)}", opts,
elide_columns: true
)
end

def inspect(df, opts) do
Explorer.Backend.DataFrame.inspect(df, "Polars", n_rows(df), opts)
end
Expand Down
6 changes: 6 additions & 0 deletions lib/explorer/polars_backend/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,12 @@ defmodule Explorer.PolarsBackend.Series do
end

@impl true
def inspect(series, opts) when node(series.data.resource) != node() do
Explorer.Backend.Series.inspect(series, "Polars", "node: #{node(series.data.resource)}", opts,
elide_columns: true
)
end

def inspect(series, opts) do
Explorer.Backend.Series.inspect(series, "Polars", Series.size(series), opts)
end
Expand Down
3 changes: 1 addition & 2 deletions lib/explorer/polars_backend/shared.ex
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ defmodule Explorer.PolarsBackend.Shared do
end

def build_path_for_entry(%FSS.HTTP.Entry{} = entry) do
hash =
:crypto.hash(:sha256, entry.url) |> Base.url_encode64(padding: false)
hash = :crypto.hash(:sha256, entry.url) |> Base.url_encode64(padding: false)

id = "http-file-#{hash}"

Expand Down
3 changes: 1 addition & 2 deletions test/explorer/series/list_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ defmodule Explorer.Series.ListTest do
end

test "list of lists of booleans" do
series =
Series.from_list([[true], [false], [true, nil, false], []])
series = Series.from_list([[true], [false], [true, nil, false], []])

assert series.dtype == {:list, :boolean}
assert series[0] == [true]
Expand Down

0 comments on commit 5c51c26

Please sign in to comment.