Geostatex.Input (Geostatex v0.6.0)

Copy Markdown View Source

The input contract shared by every Geostatex 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 Geostatex 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

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

f64_result()

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

special()

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

Functions

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> Geostatex.Input.from_f64_binary(<<1.5::float-64-native>>)
[1.5]

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} = Geostatex.Input.to_f64_binary([1, 2.5])
iex> Geostatex.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> Geostatex.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} = Geostatex.Input.to_points_binary([{0, 1}, {2, 3}])
iex> Geostatex.Input.from_f64_binary(bin)
[0.0, 1.0, 2.0, 3.0]