The input contract shared by every Spatex 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 Spatex 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
Element-wise sum of two simulation vectors of the same form (two lists or two tensors).
Decodes a native-endian f64 binary to a list.
The boolean the native side takes for a validated :keep_simulations value: :tensor keeps
the draws like true.
Validates a :keep_simulations option value: true, false or :tensor.
Raises an ArgumentError naming the optional :nx dependency unless Nx is loaded.
Splits a binary of count equal simulation vectors of len draws into count values, each
a list (true) or a {len} tensor (:tensor).
Decodes a simulation binary into the form keep asks for: nested lists (true) or an
Nx.Tensor of type {:f, 64} built straight from the binary (:tensor), both of shape.
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
@spec add_simulations(list() | Nx.Tensor.t(), list() | Nx.Tensor.t()) :: list() | Nx.Tensor.t()
Element-wise sum of two simulation vectors of the same form (two lists or two tensors).
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> Spatex.Input.from_f64_binary(<<1.5::float-64-native>>)
[1.5]
The boolean the native side takes for a validated :keep_simulations value: :tensor keeps
the draws like true.
Validates a :keep_simulations option value: true, false or :tensor.
:tensor needs the optional :nx dependency; the second argument (default: whether Nx is
loaded) lets that check be exercised without it. Returns the value.
Raises an ArgumentError naming the optional :nx dependency unless Nx is loaded.
what names the function that needs it; the second argument (default: whether Nx is
loaded) lets the failure be exercised without removing Nx.
@spec simulation_vectors(binary(), true | :tensor, pos_integer(), non_neg_integer()) :: [ list() | Nx.Tensor.t() ]
Splits a binary of count equal simulation vectors of len draws into count values, each
a list (true) or a {len} tensor (:tensor).
@spec simulations(binary(), true | :tensor, tuple()) :: list() | Nx.Tensor.t()
Decodes a simulation binary into the form keep asks for: nested lists (true) or an
Nx.Tensor of type {:f, 64} built straight from the binary (:tensor), both of shape.
The list form nests by shape, so {n, p} is n lists of p values.
@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} = Spatex.Input.to_f64_binary([1, 2.5])
iex> Spatex.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> Spatex.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} = Spatex.Input.to_points_binary([{0, 1}, {2, 3}])
iex> Spatex.Input.from_f64_binary(bin)
[0.0, 1.0, 2.0, 3.0]