The input contract shared by every Networkex function.
User data is normalized in Elixir, before any NIF call, into flat native-endian
binaries (f64 for numeric vectors and points, u32 for index vectors). All
validation errors raise ArgumentError naming the argument and the offending index.
Accepted inputs
| Input | to_f64_binary/2 | to_points_binary/1 |
|---|---|---|
| List of numbers (integers are coerced) | yes | [{x, y}] or [[x, y]] |
Nx.Tensor (optional dep) | rank 1, any numeric type | {n, 2} tensor |
Explorer.Series (optional dep) | numeric dtypes | {xs, ys} pair |
Packed {:f64, binary} | passed through (validated) | - |
Labels (regimes, partitions) go through to_labels/2: a list of any terms, a rank-1 Nx
tensor or an Explorer.Series of any dtype. Panels and other n x t matrices go through
to_matrix/2: a list of numeric rows, a rank-2 Nx tensor or an Explorer.DataFrame (its
numeric columns in order).
Nx and Explorer are optional dependencies; they are only touched when the user
passes one of their structs, so Networkex compiles and runs without them.
Options (to_f64_binary/2)
:name- argument name used in error messages (default"values").:allow_nan- accept NaN and infinities (defaultfalse).:allow_nil- forExplorer.Series, mapnilto NaN instead of raising (defaultfalse). The resulting NaN is still subject to:allow_nan.:length- expected length; a mismatch raises.
NaN and infinity
Elixir floats cannot hold NaN or infinity, so those values are written to binaries
from raw bit patterns and decoded by from_f64_binary/1 as the atoms :nan,
:infinity and :neg_infinity. Lists may contain those atoms when allow_nan: true.
Summary
Functions
Decodes a native-endian f64 binary to a list.
Normalizes a numeric vector to a native-endian f64 binary, returning {binary, n}.
Normalizes a vector of non-negative integer indices to a native-endian u32 binary.
Normalizes per-observation labels (regimes, partitions, groups) to a list of terms.
Normalizes an n x t matrix (one row per observation) to a list of float rows.
Normalizes points to an interleaved x0, y0, x1, y1, ... f64 binary, returning {binary, n}.
Types
@type f64_result() :: {binary(), non_neg_integer()}
@type special() :: :nan | :infinity | :neg_infinity
Functions
Decodes a native-endian f64 binary to a list.
NaN and infinity bit patterns decode to :nan, :infinity and :neg_infinity
because Elixir floats cannot represent them.
Examples
iex> Networkex.Input.from_f64_binary(<<1.5::float-64-native>>)
[1.5]
@spec to_f64_binary(term(), keyword()) :: f64_result()
Normalizes a numeric vector to a native-endian f64 binary, returning {binary, n}.
Examples
iex> {bin, 2} = Networkex.Input.to_f64_binary([1, 2.5])
iex> Networkex.Input.from_f64_binary(bin)
[1.0, 2.5]
@spec to_index_binary(term(), :u32, keyword()) :: {binary(), non_neg_integer()}
Normalizes a vector of non-negative integer indices to a native-endian u32 binary.
Examples
iex> Networkex.Input.to_index_binary([0, 2], :u32) |> elem(1)
2
Normalizes per-observation labels (regimes, partitions, groups) to a list of terms.
Accepts a list of any terms, a rank-1 Nx.Tensor or an Explorer.Series of any dtype
(nil raises). Options: :name (default "labels") and :length, the expected count.
Normalizes an n x t matrix (one row per observation) to a list of float rows.
Accepts a list of numeric rows, a rank-2 Nx.Tensor or an Explorer.DataFrame whose numeric
columns are taken in order (one column per period for panels). Options: :name (default
"matrix") and :rows, the expected row count.
@spec to_points_binary(term()) :: f64_result()
Normalizes points to an interleaved x0, y0, x1, y1, ... f64 binary, returning {binary, n}.
Accepts [{x, y}], [[x, y]], an {n, 2} Nx.Tensor, or a pair {xs, ys} of
anything to_f64_binary/2 accepts. Non-finite coordinates are rejected.
Examples
iex> {bin, 2} = Networkex.Input.to_points_binary([{0, 1}, {2, 3}])
iex> Networkex.Input.from_f64_binary(bin)
[0.0, 1.0, 2.0, 3.0]