Spatex.Input (Spatex v0.2.1)

Copy Markdown View Source

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

Inputto_f64_binary/2to_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 (default false).
  • :allow_nil - for Explorer.Series, map nil to NaN instead of raising (default false). 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

f64_result()

@type f64_result() :: {binary(), non_neg_integer()}

special()

@type special() :: :nan | :infinity | :neg_infinity

Functions

add_simulations(a, b)

@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).

from_f64_binary(bin)

@spec from_f64_binary(binary()) :: [float() | special()]

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]

keep_flag(keep)

@spec keep_flag(boolean() | :tensor) :: boolean()

The boolean the native side takes for a validated :keep_simulations value: :tensor keeps the draws like true.

keep_simulations!(value, nx_loaded? \\ Code.ensure_loaded?(Nx))

@spec keep_simulations!(term(), boolean()) :: boolean() | :tensor

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.

require_nx!(what, nx_loaded? \\ Code.ensure_loaded?(Nx))

@spec require_nx!(String.t(), boolean()) :: :ok

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.

simulation_vectors(bin, keep, count, len)

@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).

simulations(bin, arg2, shape)

@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.

to_f64_binary(values, opts \\ [])

@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]

to_index_binary(list, type, opts \\ [])

@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

to_labels(values, opts \\ [])

@spec to_labels(term(), keyword()) :: [term()]

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.

to_matrix(m, opts \\ [])

@spec to_matrix(term(), keyword()) :: [[float()]]

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.

to_points_binary(tensor)

@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]